Skip to content

Commit 0386ce6

Browse files
authored
fix: add namespaces to support SVG nodes
1 parent 62ed4e0 commit 0386ce6

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
11
# @homebots/parse-html-runtime
22

3-
Runtime extensions for @homebots/parse-html
3+
Runtime extensions for @homebots/parse-html.
4+
5+
Useful in runtime to work with a parsed tree created from the parser.
6+
7+
## API
8+
9+
10+
**normalize(nodes)**
11+
12+
Remove empty text nodes from parsed tree:
13+
14+
```ts
15+
import { normalize } from 'https://unpkg.com/@homebots/parse-html-runtime@latest/index.js'
16+
17+
const cleanNodes = normalize(nodes);
18+
```
19+
20+
**serialize(nodes)**
21+
22+
Convert a parsed tree back to HTML text
23+
24+
```ts
25+
import { serialize } from 'https://unpkg.com/@homebots/parse-html-runtime@latest/index.js'
26+
27+
const html = normalize(nodes);
28+
```
29+
30+
**materialize(nodes)**
31+
32+
Convert a parsed tree into elements
33+
34+
```ts
35+
import { materialize } from 'https://unpkg.com/@homebots/parse-html-runtime@latest/index.js'
36+
37+
const visitor = (el, node) => {
38+
// Example: save node structure in the element
39+
el['@node'] = node
40+
};
41+
42+
const html = materialize(nodes, visitor);
43+
```

src/materialize.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const validAttribute = /^[a-zA-Z_][a-zA-Z0-9\-_:.]*$/;
44

55
export type Visitor = <T>(el: T, node: ParserNode) => T | null | undefined | void;
66

7+
let namespace = '';
8+
79
export function materialize(node: ParserNode, visitor: Visitor|null = null) {
810
let el: any;
911

@@ -23,8 +25,16 @@ export function materialize(node: ParserNode, visitor: Visitor|null = null) {
2325
break;
2426

2527
case 'element': {
28+
if (node.tag === 'svg') {
29+
namespace = 'http://www.w3.org/2000/svg'
30+
}
31+
2632
el = createElement(node);
2733
el.append(...node.children.map((n) => materialize(n, visitor)));
34+
35+
if (node.tag === 'svg') {
36+
namespace = 'http://www.w3.org/2000/svg'
37+
}
2838
break;
2939
}
3040

@@ -44,16 +54,14 @@ export function createTextNode(node: TextNode) {
4454
}
4555

4656
export function createElement(node: ElementNode) {
47-
const el = document.createElement(node.tag);
48-
el['@attributes'] = node.attributes;
49-
el['@node'] = node;
57+
const el = namespace ? document.createElementNS(namespace, node.tag) : document.createElement(node.tag);
5058

5159
node.attributes.forEach((a: ParserAttribute) => setAttribute(el, a.name, a.value));
5260

5361
return el;
5462
}
5563

56-
export function setAttribute(el: HTMLElement, attribute: string, value: string | number | boolean) {
64+
export function setAttribute(el: Element, attribute: string, value: string | number | boolean) {
5765
if (!validAttribute.test(attribute)) {
5866
return;
5967
}

0 commit comments

Comments
 (0)