Skip to content

Commit 81ec094

Browse files
committed
Add JSDoc based types
1 parent 0cb2fdc commit 81ec094

12 files changed

+310
-143
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.DS_Store
2+
*.d.ts
23
*.log
34
coverage/
45
node_modules/

Diff for: index.js

+4-135
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,5 @@
1-
import {html, svg, find} from 'property-information'
2-
import {toH} from 'hast-to-hyperscript'
3-
import {webNamespaces} from 'web-namespaces'
4-
import {zwitch} from 'zwitch'
1+
/**
2+
* @typedef {import('./lib/index.js').Space} Space
3+
*/
54

6-
var own = {}.hasOwnProperty
7-
8-
var one = zwitch('type', {handlers: {root, element, text, comment, doctype}})
9-
10-
// Transform a tree from hast to Parse5’s AST.
11-
export function toParse5(tree, space) {
12-
return one(tree, space === 'svg' ? svg : html)
13-
}
14-
15-
function root(node, schema) {
16-
return patch(
17-
node,
18-
{
19-
nodeName: '#document',
20-
mode: (node.data || {}).quirksMode ? 'quirks' : 'no-quirks'
21-
},
22-
schema
23-
)
24-
}
25-
26-
function fragment(node, schema) {
27-
return patch(node, {nodeName: '#document-fragment'}, schema)
28-
}
29-
30-
function doctype(node, schema) {
31-
return patch(
32-
node,
33-
{
34-
nodeName: '#documentType',
35-
name: node.name,
36-
publicId: node.public || '',
37-
systemId: node.system || ''
38-
},
39-
schema
40-
)
41-
}
42-
43-
function text(node, schema) {
44-
return patch(node, {nodeName: '#text', value: node.value}, schema)
45-
}
46-
47-
function comment(node, schema) {
48-
return patch(node, {nodeName: '#comment', data: node.value}, schema)
49-
}
50-
51-
function element(node, schema) {
52-
return toH(h, Object.assign({}, node, {children: []}), {space: schema.space})
53-
54-
function h(name, attrs) {
55-
var values = []
56-
var info
57-
var value
58-
var key
59-
var index
60-
var p5
61-
62-
for (key in attrs) {
63-
if (!own.call(attrs, key) || attrs[key] === false) {
64-
continue
65-
}
66-
67-
info = find(schema, key)
68-
69-
if (info.boolean && !attrs[key]) {
70-
continue
71-
}
72-
73-
value = {name: key, value: attrs[key] === true ? '' : String(attrs[key])}
74-
75-
if (info.space && info.space !== 'html' && info.space !== 'svg') {
76-
index = key.indexOf(':')
77-
78-
if (index < 0) {
79-
value.prefix = ''
80-
} else {
81-
value.name = key.slice(index + 1)
82-
value.prefix = key.slice(0, index)
83-
}
84-
85-
value.namespace = webNamespaces[info.space]
86-
}
87-
88-
values.push(value)
89-
}
90-
91-
p5 = patch(node, {nodeName: name, tagName: name, attrs: values}, schema)
92-
93-
if (name === 'template') p5.content = fragment(node.content, schema)
94-
95-
return p5
96-
}
97-
}
98-
99-
// Patch specific properties.
100-
function patch(node, p5, parentSchema) {
101-
var schema = parentSchema
102-
var position = node.position
103-
var childNodes = []
104-
var index = -1
105-
var child
106-
107-
if (node.type === 'element') {
108-
if (schema.space === 'html' && node.tagName === 'svg') schema = svg
109-
p5.namespaceURI = webNamespaces[schema.space]
110-
}
111-
112-
if (node.type === 'element' || node.type === 'root') {
113-
p5.childNodes = childNodes
114-
115-
if (node.children) {
116-
while (++index < node.children.length) {
117-
child = one(node.children[index], schema)
118-
child.parentNode = p5
119-
childNodes[index] = child
120-
}
121-
}
122-
}
123-
124-
if (position && position.start && position.end) {
125-
p5.sourceCodeLocation = {
126-
startLine: position.start.line,
127-
startCol: position.start.column,
128-
startOffset: position.start.offset,
129-
endLine: position.end.line,
130-
endCol: position.end.column,
131-
endOffset: position.end.offset
132-
}
133-
}
134-
135-
return p5
136-
}
5+
export {toParse5} from './lib/index.js'

0 commit comments

Comments
 (0)