Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions documentation/docs/html-rewriter/Element/prototype/after.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
hide_title: false
hide_table_of_contents: false
pagination_next: null
pagination_prev: null
---

# after

The `after` method inserts content after the closing tag of the element.

## Syntax

```js
element.after(content);
element.after(content, options);
```

### Parameters

- `content` _: string_
- The content to insert after the element's closing tag.

- `options` _: ElementRewriterOptions_
- An optional object that can have the following properties:
- `escapeHTML` _: boolean_
- If `true`, any HTML markup in `content` will be escaped so it is safe to insert as text.

### Examples

```js
// Assuming `e` is an Element representing <div>Hello</div>
e.after("World");
// Result: <div>Hello</div>World
```
35 changes: 35 additions & 0 deletions documentation/docs/html-rewriter/Element/prototype/append.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
hide_title: false
hide_table_of_contents: false
pagination_next: null
pagination_prev: null
---

# append

The `append` method inserts content at the end of the element's content.

## Syntax

```js
element.append(content);
element.append(content, options);
```

### Parameters

- `content` _: string_
- The content to insert at the end of the element's content.

- `options` _: ElementRewriterOptions_
- An optional object that can have the following properties:
- `escapeHTML` _: boolean_
- If `true`, any HTML markup in `content` will be escaped so it is safe to insert as text.

### Examples

```js
// Assuming `e` is an Element representing <div>Hello</div>
e.append(", World");
// Result: <div>Hello, World</div>
```
35 changes: 35 additions & 0 deletions documentation/docs/html-rewriter/Element/prototype/before.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
hide_title: false
hide_table_of_contents: false
pagination_next: null
pagination_prev: null
---

# before

The `before` method inserts content before the opening tag of the element.

## Syntax

```js
element.before(content);
element.before(content, options);
```

### Parameters

- `content` _: string_
- The content to insert before the element's opening tag.

- `options` _: ElementRewriterOptions_
- An optional object that can have the following properties:
- `escapeHTML` _: boolean_
- If `true`, any HTML markup in `content` will be escaped so it is safe to insert as text.

### Examples

```js
// Assuming `e` is an Element representing <div>Hello</div>
e.before("Well");
// Result: Well<div>Hello</div>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
hide_title: false
hide_table_of_contents: false
pagination_next: null
pagination_prev: null
---

# hasAttribute

The `hasAttribute` method returns a `boolean` value indicating whether the specified attribute is present on the element.

## Syntax

```js
element.hasAttribute(attributeName);
```

### Parameters

- `attributeName` _: string_
- The name of the attribute to check for.

### Return value

A boolean value indicating whether the attribute is present.

35 changes: 35 additions & 0 deletions documentation/docs/html-rewriter/Element/prototype/prepend.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
hide_title: false
hide_table_of_contents: false
pagination_next: null
pagination_prev: null
---

# prepend

The `prepend` method inserts content at the beginning of the element's content.

## Syntax

```js
element.prepend(content);
element.prepend(content, options);
```

### Parameters

- `content` _: string_
- The content to insert at the beginning of the element's content.

- `options` _: ElementRewriterOptions_
- An optional object that can have the following properties:
- `escapeHTML` _: boolean_
- If `true`, any HTML markup in `content` will be escaped so it is safe to insert as text.

### Examples

```js
// Assuming `e` is an Element representing <div>Hello</div>
e.prepend("Well, ");
// Result: <div>Well, Hello</div>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
hide_title: false
hide_table_of_contents: false
pagination_next: null
pagination_prev: null
---

# removeAttribute

The `removeAttribute` method removes the specified attribute from the element.

## Syntax

```js
element.removeAttribute(attributeName);
```

### Parameters

- `attributeName` _: string_
- The name of the attribute to remove.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
hide_title: false
hide_table_of_contents: false
pagination_next: null
pagination_prev: null
---

# replaceChildren

The `replaceChildren` method replaces the element's children with new content.

## Syntax

```js
element.replaceChildren(content);
element.replaceChildren(content, options);
```

### Parameters

- `content` _: string_
- The content to replace the element's children with.

- `options` _: ElementRewriterOptions_
- An optional object that can have the following properties:
- `escapeHTML` _: boolean_
- If `true`, any HTML markup in `content` will be escaped so it is safe to insert as text.

### Examples

```js
// Assuming `e` is an Element representing <div>Hello</div>
e.replaceChildren("Greetings!");
// Result: <div>Greetings!</div>
```
35 changes: 35 additions & 0 deletions documentation/docs/html-rewriter/Element/prototype/replaceWith.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
hide_title: false
hide_table_of_contents: false
pagination_next: null
pagination_prev: null
---

# replaceWith

The `replaceWith` method replaces the element with new content.

## Syntax

```js
element.replaceWith(content);
element.replaceWith(content, options);
```

### Parameters

- `content` _: string_
- The content to replace the element with.

- `options` _: ElementRewriterOptions_
- An optional object that can have the following properties:
- `escapeHTML` _: boolean_
- If `true`, any HTML markup in `content` will be escaped so it is safe to insert as text.

### Examples

```js
// Assuming `e` is an Element representing <div>Hello</div>
e.replaceWith("<p>Greetings!</p>");
// Result: <p>Greetings!</p>
```
11 changes: 11 additions & 0 deletions documentation/docs/html-rewriter/Element/prototype/selector.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
hide_title: false
hide_table_of_contents: false
pagination_next: null
pagination_prev: null
---

# selector

The `selector` read-only property is a `string` representing the [CSS selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_selectors) that matches the element.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
hide_title: false
hide_table_of_contents: false
pagination_next: null
pagination_prev: null
---

# setAttribute

The `setAttribute` method sets the value of the specified attribute on the element. If the value already exists, it will be updated; otherwise, a new attribute with the specified name and value will be added to the element.

## Syntax

```js
element.setAttribute(attributeName, value);
```

### Parameters

- `attributeName` _: string_
- The name of the attribute to set.
- `value` _: string_
- The value to assign to the attribute.

10 changes: 10 additions & 0 deletions documentation/docs/html-rewriter/Element/prototype/tag.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
hide_title: false
hide_table_of_contents: false
pagination_next: null
pagination_prev: null
---

# tag

The `tag` read-only property is a `string` representing the tag name of the element.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
hide_title: false
hide_table_of_contents: false
pagination_next: null
pagination_prev: null
---
# `HTMLRewritingStream()`

The **`HTMLRewritingStream`** lets you rewrite HTML by registering callbacks on CSS selectors. When an element matching the selector is encountered, the rewriter calls your callback. This callback can manipulate the attributes of the element, and add or remove content from the immediate context.

## Syntax

```js
new HTMLRewritingStream()
```

### Return value

A new `HTMLRewritingStream` object.

## Examples

In this example, we fetch an HTML page and use the HTML rewriter to add an attribute to all `div` tags and prepend the text `Header:` to all `h1` tags:

```js
/// <reference types="@fastly/js-compute" />

import { HTMLRewritingStream } from 'fastly/html-rewriter';

async function handleRequest(event) {
let transformer = new HTMLRewritingStream()
.onElement("h1", e => e.prepend("Header: "))
.onElement("div", e => e.setAttribute("special-attribute", "top-secret"));
let body = (await fetch("https://example.com/")).body.pipeThrough(transformer);

return new Response(body, {
status: 200,
headers: new Headers({
"content-type": "text/html"
})
})
}

addEventListener("fetch", (event) => event.respondWith(handleRequest(event)));
```
Loading
Loading