Skip to content

Commit ea72d7b

Browse files
committed
Update for parse5@^5.0.0
1 parent aaca142 commit ea72d7b

File tree

4 files changed

+124
-133
lines changed

4 files changed

+124
-133
lines changed

index.js

Lines changed: 61 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
var information = require('property-information');
44
var camelcase = require('camelcase');
5-
var vfileLocation = require('vfile-location');
65
var h = require('hastscript');
6+
var xtend = require('xtend');
7+
var count = require('ccount');
78

89
module.exports = wrapper;
910

@@ -32,7 +33,6 @@ function wrapper(ast, options) {
3233

3334
return transform(ast, {
3435
file: file,
35-
toPosition: file ? vfileLocation(file).toPosition : null,
3636
verbose: settings.verbose,
3737
location: false
3838
});
@@ -43,20 +43,20 @@ function transform(ast, config) {
4343
var fn = own.call(map, ast.nodeName) ? map[ast.nodeName] : element;
4444
var children;
4545
var node;
46-
var position;
46+
var pos;
4747

4848
if (ast.childNodes) {
4949
children = nodes(ast.childNodes, config);
5050
}
5151

5252
node = fn(ast, children, config);
5353

54-
if (ast.__location && config.toPosition) {
55-
config.location = true;
56-
position = location(ast.__location, ast, node, config);
54+
if (ast.sourceCodeLocation && config.file) {
55+
pos = location(node, ast.sourceCodeLocation, config.verbose);
5756

58-
if (position) {
59-
node.position = position;
57+
if (pos) {
58+
config.location = true;
59+
node.position = pos;
6060
}
6161
}
6262

@@ -79,25 +79,18 @@ function nodes(children, config) {
7979
/* Transform a document.
8080
* Stores `ast.quirksMode` in `node.data.quirksMode`. */
8181
function root(ast, children, config) {
82-
var quirks = ast.mode === 'quirks' || ast.mode === 'limited-quirks';
83-
var node = {type: 'root', children: children};
84-
var position;
85-
86-
node.data = {quirksMode: quirks};
82+
var node = {type: 'root', children: children, data: {}};
83+
var doc;
8784

88-
if (ast.__location) {
89-
if (config.toPosition) {
90-
config.location = true;
91-
position = ast.__location;
92-
}
93-
} else if (config.file && config.location) {
94-
position = {startOffset: 0, endOffset: String(config.file).length};
95-
}
85+
node.data.quirksMode = ast.mode === 'quirks' || ast.mode === 'limited-quirks';
9686

97-
position = position && location(position, ast, node, config);
87+
if (config.file && config.location) {
88+
doc = String(config.file);
9889

99-
if (position) {
100-
node.position = position;
90+
node.position = {
91+
start: {line: 1, column: 1, offset: 0},
92+
end: {line: count(doc, '\n') + 1, column: doc.length - doc.lastIndexOf('\n'), offset: doc.length}
93+
};
10194
}
10295

10396
return node;
@@ -131,7 +124,9 @@ function element(ast, children, config) {
131124
var index = -1;
132125
var attr;
133126
var node;
134-
var fragment;
127+
var pos;
128+
var start;
129+
var end;
135130

136131
while (++index < length) {
137132
attr = values[index];
@@ -141,74 +136,65 @@ function element(ast, children, config) {
141136
node = h(ast.tagName, props, children);
142137

143138
if (ast.nodeName === 'template' && 'content' in ast) {
144-
fragment = ast.content;
145-
146-
if (ast.__location) {
147-
fragment.__location = {
148-
startOffset: ast.__location.startTag.endOffset,
149-
endOffset: ast.__location.endTag.startOffset
150-
};
151-
}
139+
pos = ast.sourceCodeLocation;
140+
start = pos && pos.startTag && position(pos.startTag).end;
141+
end = pos && pos.endTag && position(pos.endTag).start;
152142

153143
node.content = transform(ast.content, config);
144+
145+
if ((start || end) && config.file) {
146+
node.content.position = {start: start, end: end};
147+
}
154148
}
155149

156150
return node;
157151
}
158152

159153
/* Create clean positional information. */
160-
function loc(toPosition, dirty) {
161-
return {
162-
start: toPosition(dirty.startOffset),
163-
end: toPosition(dirty.endOffset)
164-
};
165-
}
166-
167-
/* Create clean positional information. */
168-
function location(info, ast, node, config) {
169-
var start = info.startOffset;
170-
var end = info.endOffset;
171-
var values = info.attrs || {};
172-
var propPositions = {};
154+
function location(node, location, verbose) {
155+
var pos = position(location);
156+
var reference;
157+
var values;
158+
var props;
173159
var prop;
174160
var name;
175-
var reference;
176-
177-
for (prop in values) {
178-
name = (information(prop) || {}).propertyName || camelcase(prop);
179-
propPositions[name] = loc(config.toPosition, values[prop]);
180-
}
181161

182-
/* Upstream: https://github.com/inikulin/parse5/issues/109 */
183-
if (node.type === 'element' && !info.endTag) {
162+
if (node.type === 'element') {
184163
reference = node.children[node.children.length - 1];
185164

186-
/* Unclosed with children: */
187-
if (reference && reference.position) {
188-
if (reference.position.end) {
189-
end = reference.position.end.offset;
190-
} else {
191-
end = null;
192-
}
165+
/* Unclosed with children (upstream: https://github.com/inikulin/parse5/issues/109) */
166+
if (!location.endTag && reference && reference.position && reference.position.end) {
167+
pos.end = xtend(reference.position.end);
193168
}
194-
}
195169

196-
if (config.verbose && node.type === 'element') {
197-
node.data = {
198-
position: {
199-
opening: loc(config.toPosition, info.startTag),
200-
closing: info.endTag ? loc(config.toPosition, info.endTag) : null,
201-
properties: propPositions
170+
if (verbose) {
171+
values = location.attrs;
172+
props = {};
173+
174+
for (prop in values) {
175+
name = (information(prop) || {}).propertyName || camelcase(prop);
176+
props[name] = position(values[prop]);
202177
}
203-
};
178+
179+
node.data = {
180+
position: {
181+
opening: position(location.startTag),
182+
closing: location.endTag ? position(location.endTag) : null,
183+
properties: props
184+
}
185+
};
186+
}
204187
}
205188

206-
start = typeof start === 'number' ? config.toPosition(start) : null;
207-
end = typeof end === 'number' ? config.toPosition(end) : null;
189+
return pos;
190+
}
208191

209-
if (!start && !end) {
210-
return undefined;
211-
}
192+
function position(loc) {
193+
var start = point({line: loc.startLine, column: loc.startCol, offset: loc.startOffset});
194+
var end = point({line: loc.endLine, column: loc.endCol, offset: loc.endOffset});
195+
return start || end ? {start: start, end: end} : null;
196+
}
212197

213-
return {start: start, end: end};
198+
function point(point) {
199+
return point.line && point.column ? point : null;
214200
}

package.json

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,32 @@
1919
"index.js"
2020
],
2121
"dependencies": {
22-
"camelcase": "^3.0.0",
22+
"camelcase": "^5.0.0",
23+
"ccount": "^1.0.3",
2324
"hastscript": "^3.0.0",
2425
"property-information": "^3.1.0",
25-
"vfile-location": "^2.0.0"
26+
"xtend": "^4.0.1"
2627
},
2728
"devDependencies": {
28-
"browserify": "^14.0.0",
29+
"babel-core": "^6.26.3",
30+
"babel-preset-env": "^1.7.0",
31+
"babelify": "^8.0.0",
32+
"browserify": "^16.0.0",
2933
"esmangle": "^1.0.1",
3034
"is-hidden": "^1.1.0",
3135
"not": "^0.1.0",
32-
"nyc": "^11.0.0",
33-
"parse5": "^3.0.0",
34-
"remark-cli": "^4.0.0",
35-
"remark-preset-wooorm": "^3.0.0",
36+
"nyc": "^12.0.0",
37+
"parse5": "^5.0.0",
38+
"remark-cli": "^5.0.0",
39+
"remark-preset-wooorm": "^4.0.0",
3640
"tape": "^4.0.0",
3741
"unist-util-visit": "^1.1.3",
38-
"vfile": "^2.0.0",
39-
"xo": "^0.18.0"
42+
"vfile": "^3.0.0",
43+
"xo": "^0.21.0"
4044
},
4145
"scripts": {
4246
"build-md": "remark . -qfo",
43-
"build-bundle": "browserify index.js --bare -s hastUtilFromParse5 > hast-util-from-parse5.js",
47+
"build-bundle": "browserify index.js --bare -s hastUtilFromParse5 -g [ babelify --presets [ \"babel-preset-env\" ] ] > hast-util-from-parse5.js",
4448
"build-mangle": "esmangle hast-util-from-parse5.js > hast-util-from-parse5.min.js",
4549
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
4650
"lint": "xo",

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var inspect = require('unist-util-inspect');
2727
var fromParse5 = require('hast-util-from-parse5');
2828

2929
var doc = vfile.readSync('example.html');
30-
var ast = parse5.parse(String(doc), {locationInfo: true});
30+
var ast = parse5.parse(String(doc), {sourceCodeLocationInfo: true});
3131
var hast = fromParse5(ast, doc);
3232

3333
console.log(inspect(hast));

0 commit comments

Comments
 (0)