Comparing version 0.1.3 to 0.2.0
# ultrahtml | ||
## 0.2.0 | ||
### Minor Changes | ||
- 97b297f: Add `walkSync` export | ||
## 0.1.3 | ||
@@ -4,0 +10,0 @@ |
@@ -14,2 +14,5 @@ export interface Node { | ||
} | ||
export interface VisitorSync { | ||
(node: Node, parent?: Node, index?: number): void; | ||
} | ||
export declare function __unsafeHTML(str: string): { | ||
@@ -25,2 +28,3 @@ value: string; | ||
export declare function walk(node: Node, callback: Visitor): Promise<void>; | ||
export declare function walkSync(node: Node, callback: VisitorSync): void; | ||
export interface SanitizeOptions { | ||
@@ -27,0 +31,0 @@ /** An Array of strings indicating elements that the sanitizer should not remove. All elements not in the array will be dropped. */ |
@@ -160,2 +160,16 @@ "use strict"; | ||
} | ||
class WalkerSync { | ||
constructor(callback) { | ||
this.callback = callback; | ||
} | ||
visit(node, parent, index) { | ||
this.callback(node, parent, index); | ||
if (Array.isArray(node.children)) { | ||
for (let i = 0; i < node.children.length; i++) { | ||
const child = node.children[i]; | ||
this.visit(child, node, i); | ||
} | ||
} | ||
} | ||
} | ||
const HTMLString = Symbol("HTMLString"); | ||
@@ -217,2 +231,6 @@ const AttrString = Symbol("AttrString"); | ||
} | ||
export function walkSync(node, callback) { | ||
const walker = new WalkerSync(callback); | ||
return walker.visit(node); | ||
} | ||
function resolveSantizeOptions({ | ||
@@ -219,0 +237,0 @@ components = {}, |
{ | ||
"name": "ultrahtml", | ||
"type": "module", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"types": "./dist/index.d.ts", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -17,2 +17,4 @@ # `ultrahtml` | ||
> **Note** > `walk` is `async` and **must** be `await`ed. Use `walkSync` if it is guaranteed there are no `async` components in the tree. | ||
```js | ||
@@ -22,3 +24,3 @@ import { parse, walk, ELEMENT_NODE } from "ultrahtml"; | ||
const ast = parse(`<h1>Hello world!</h1>`); | ||
walk(ast, (node) => { | ||
await walk(ast, async (node) => { | ||
if (node.type === ELEMENT_NODE && node.name === "script") { | ||
@@ -30,2 +32,17 @@ throw new Error("Found a script!"); | ||
#### `walkSync` | ||
The `walkSync` function is identical to the `walk` function, but is synchronous. This should only be used when it is guaranteed there are no `async` components in the tree. | ||
```js | ||
import { parse, walkSync, ELEMENT_NODE } from "ultrahtml"; | ||
const ast = parse(`<h1>Hello world!</h1>`); | ||
walkSync(ast, (node) => { | ||
if (node.type === ELEMENT_NODE && node.name === "script") { | ||
throw new Error("Found a script!"); | ||
} | ||
}); | ||
``` | ||
#### `render` | ||
@@ -32,0 +49,0 @@ |
24022
440
97