Skip to content

Commit 21e9223

Browse files
committed
Add JSDoc based types
1 parent 20e9416 commit 21e9223

File tree

10 files changed

+451
-311
lines changed

10 files changed

+451
-311
lines changed

.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/

index.js

+4-212
Original file line numberDiff line numberDiff line change
@@ -1,213 +1,5 @@
1-
import {h, s} from 'hastscript'
2-
import {html, svg, find} from 'property-information'
3-
import vfileLocation from 'vfile-location'
4-
import {webNamespaces} from 'web-namespaces'
1+
/**
2+
* @typedef {import('./lib/index.js').Options} Options
3+
*/
54

6-
var own = {}.hasOwnProperty
7-
8-
// Handlers.
9-
var map = {
10-
'#document': root,
11-
'#document-fragment': root,
12-
'#text': text,
13-
'#comment': comment,
14-
'#documentType': doctype
15-
}
16-
17-
// Wrapper to normalise options.
18-
export function fromParse5(ast, options) {
19-
var settings = options || {}
20-
var file
21-
22-
if (settings.messages) {
23-
file = settings
24-
settings = {}
25-
} else {
26-
file = settings.file
27-
}
28-
29-
return transform(ast, {
30-
schema: settings.space === 'svg' ? svg : html,
31-
file,
32-
verbose: settings.verbose
33-
})
34-
}
35-
36-
// Transform a node.
37-
function transform(ast, config) {
38-
var schema = config.schema
39-
var fn = own.call(map, ast.nodeName) ? map[ast.nodeName] : element
40-
var children
41-
var result
42-
var position
43-
44-
if (fn === element) {
45-
config.schema = ast.namespaceURI === webNamespaces.svg ? svg : html
46-
}
47-
48-
if (ast.childNodes) {
49-
children = nodes(ast.childNodes, config)
50-
}
51-
52-
result = fn(ast, children, config)
53-
54-
if (ast.sourceCodeLocation && config.file) {
55-
position = location(result, ast.sourceCodeLocation, config)
56-
57-
if (position) {
58-
config.location = true
59-
result.position = position
60-
}
61-
}
62-
63-
config.schema = schema
64-
65-
return result
66-
}
67-
68-
// Transform children.
69-
function nodes(children, config) {
70-
var index = -1
71-
var result = []
72-
73-
while (++index < children.length) {
74-
result[index] = transform(children[index], config)
75-
}
76-
77-
return result
78-
}
79-
80-
// Transform a document.
81-
// Stores `ast.quirksMode` in `node.data.quirksMode`.
82-
function root(ast, children, config) {
83-
var result = {
84-
type: 'root',
85-
children,
86-
data: {quirksMode: ast.mode === 'quirks' || ast.mode === 'limited-quirks'}
87-
}
88-
var doc
89-
var location
90-
91-
if (config.file && config.location) {
92-
doc = String(config.file)
93-
location = vfileLocation(doc)
94-
result.position = {
95-
start: location.toPoint(0),
96-
end: location.toPoint(doc.length)
97-
}
98-
}
99-
100-
return result
101-
}
102-
103-
// Transform a doctype.
104-
function doctype(ast) {
105-
return {
106-
type: 'doctype',
107-
name: ast.name || '',
108-
public: ast.publicId || null,
109-
system: ast.systemId || null
110-
}
111-
}
112-
113-
// Transform a text.
114-
function text(ast) {
115-
return {type: 'text', value: ast.value}
116-
}
117-
118-
// Transform a comment.
119-
function comment(ast) {
120-
return {type: 'comment', value: ast.data}
121-
}
122-
123-
// Transform an element.
124-
function element(ast, children, config) {
125-
var fn = config.schema.space === 'svg' ? s : h
126-
var props = {}
127-
var index = -1
128-
var result
129-
var attribute
130-
var pos
131-
var start
132-
var end
133-
134-
while (++index < ast.attrs.length) {
135-
attribute = ast.attrs[index]
136-
props[(attribute.prefix ? attribute.prefix + ':' : '') + attribute.name] =
137-
attribute.value
138-
}
139-
140-
result = fn(ast.tagName, props, children)
141-
142-
if (result.tagName === 'template' && 'content' in ast) {
143-
pos = ast.sourceCodeLocation
144-
start = pos && pos.startTag && position(pos.startTag).end
145-
end = pos && pos.endTag && position(pos.endTag).start
146-
147-
result.content = transform(ast.content, config)
148-
149-
if ((start || end) && config.file) {
150-
result.content.position = {start, end}
151-
}
152-
}
153-
154-
return result
155-
}
156-
157-
// Create clean positional information.
158-
function location(node, location, config) {
159-
var result = position(location)
160-
var tail
161-
var key
162-
var props
163-
164-
if (node.type === 'element') {
165-
tail = node.children[node.children.length - 1]
166-
167-
// Bug for unclosed with children.
168-
// See: <https://github.com/inikulin/parse5/issues/109>.
169-
if (!location.endTag && tail && tail.position && tail.position.end) {
170-
result.end = Object.assign({}, tail.position.end)
171-
}
172-
173-
if (config.verbose) {
174-
props = {}
175-
176-
for (key in location.attrs) {
177-
if (own.call(location.attrs, key)) {
178-
props[find(config.schema, key).property] = position(
179-
location.attrs[key]
180-
)
181-
}
182-
}
183-
184-
node.data = {
185-
position: {
186-
opening: position(location.startTag),
187-
closing: location.endTag ? position(location.endTag) : null,
188-
properties: props
189-
}
190-
}
191-
}
192-
}
193-
194-
return result
195-
}
196-
197-
function position(loc) {
198-
var start = point({
199-
line: loc.startLine,
200-
column: loc.startCol,
201-
offset: loc.startOffset
202-
})
203-
var end = point({
204-
line: loc.endLine,
205-
column: loc.endCol,
206-
offset: loc.endOffset
207-
})
208-
return start || end ? {start, end} : null
209-
}
210-
211-
function point(point) {
212-
return point.line && point.column ? point : null
213-
}
5+
export {fromParse5} from './lib/index.js'

0 commit comments

Comments
 (0)