hast-util-from-parse5
Advanced tools
Comparing version 2.1.0 to 3.0.0
234
index.js
@@ -1,11 +0,12 @@ | ||
'use strict'; | ||
'use strict' | ||
var information = require('property-information'); | ||
var camelcase = require('camelcase'); | ||
var vfileLocation = require('vfile-location'); | ||
var h = require('hastscript'); | ||
var information = require('property-information') | ||
var camelcase = require('camelcase') | ||
var h = require('hastscript') | ||
var xtend = require('xtend') | ||
var count = require('ccount') | ||
module.exports = wrapper; | ||
module.exports = wrapper | ||
var own = {}.hasOwnProperty; | ||
var own = {}.hasOwnProperty | ||
@@ -19,14 +20,14 @@ /* Handlers. */ | ||
'#documentType': doctype | ||
}; | ||
} | ||
/* Wrapper to normalise options. */ | ||
function wrapper(ast, options) { | ||
var settings = options || {}; | ||
var file; | ||
var settings = options || {} | ||
var file | ||
if (settings.messages) { | ||
file = settings; | ||
settings = {}; | ||
file = settings | ||
settings = {} | ||
} else { | ||
file = settings.file; | ||
file = settings.file | ||
} | ||
@@ -36,6 +37,5 @@ | ||
file: file, | ||
toPosition: file ? vfileLocation(file).toPosition : null, | ||
verbose: settings.verbose, | ||
location: false | ||
}); | ||
}) | ||
} | ||
@@ -45,23 +45,23 @@ | ||
function transform(ast, config) { | ||
var fn = own.call(map, ast.nodeName) ? map[ast.nodeName] : element; | ||
var children; | ||
var node; | ||
var position; | ||
var fn = own.call(map, ast.nodeName) ? map[ast.nodeName] : element | ||
var children | ||
var node | ||
var pos | ||
if (ast.childNodes) { | ||
children = nodes(ast.childNodes, config); | ||
children = nodes(ast.childNodes, config) | ||
} | ||
node = fn(ast, children, config); | ||
node = fn(ast, children, config) | ||
if (ast.__location && config.toPosition) { | ||
config.location = true; | ||
position = location(ast.__location, ast, node, config); | ||
if (ast.sourceCodeLocation && config.file) { | ||
pos = location(node, ast.sourceCodeLocation, config.verbose) | ||
if (position) { | ||
node.position = position; | ||
if (pos) { | ||
config.location = true | ||
node.position = pos | ||
} | ||
} | ||
return node; | ||
return node | ||
} | ||
@@ -71,11 +71,11 @@ | ||
function nodes(children, config) { | ||
var length = children.length; | ||
var index = -1; | ||
var result = []; | ||
var length = children.length | ||
var index = -1 | ||
var result = [] | ||
while (++index < length) { | ||
result[index] = transform(children[index], config); | ||
result[index] = transform(children[index], config) | ||
} | ||
return result; | ||
return result | ||
} | ||
@@ -86,24 +86,21 @@ | ||
function root(ast, children, config) { | ||
var quirks = ast.mode === 'quirks' || ast.mode === 'limited-quirks'; | ||
var node = {type: 'root', children: children}; | ||
var position; | ||
var node = {type: 'root', children: children, data: {}} | ||
var doc | ||
node.data = {quirksMode: quirks}; | ||
node.data.quirksMode = ast.mode === 'quirks' || ast.mode === 'limited-quirks' | ||
if (ast.__location) { | ||
if (config.toPosition) { | ||
config.location = true; | ||
position = ast.__location; | ||
if (config.file && config.location) { | ||
doc = String(config.file) | ||
node.position = { | ||
start: {line: 1, column: 1, offset: 0}, | ||
end: { | ||
line: count(doc, '\n') + 1, | ||
column: doc.length - doc.lastIndexOf('\n'), | ||
offset: doc.length | ||
} | ||
} | ||
} else if (config.file && config.location) { | ||
position = {startOffset: 0, endOffset: String(config.file).length}; | ||
} | ||
position = position && location(position, ast, node, config); | ||
if (position) { | ||
node.position = position; | ||
} | ||
return node; | ||
return node | ||
} | ||
@@ -118,3 +115,3 @@ | ||
system: ast.systemId || null | ||
}; | ||
} | ||
} | ||
@@ -124,3 +121,3 @@ | ||
function text(ast) { | ||
return {type: 'text', value: ast.value}; | ||
return {type: 'text', value: ast.value} | ||
} | ||
@@ -130,3 +127,3 @@ | ||
function comment(ast) { | ||
return {type: 'comment', value: ast.data}; | ||
return {type: 'comment', value: ast.data} | ||
} | ||
@@ -136,91 +133,94 @@ | ||
function element(ast, children, config) { | ||
var props = {}; | ||
var values = ast.attrs; | ||
var length = values.length; | ||
var index = -1; | ||
var attr; | ||
var node; | ||
var fragment; | ||
var props = {} | ||
var values = ast.attrs | ||
var length = values.length | ||
var index = -1 | ||
var attr | ||
var node | ||
var pos | ||
var start | ||
var end | ||
while (++index < length) { | ||
attr = values[index]; | ||
props[(attr.prefix ? attr.prefix + ':' : '') + attr.name] = attr.value; | ||
attr = values[index] | ||
props[(attr.prefix ? attr.prefix + ':' : '') + attr.name] = attr.value | ||
} | ||
node = h(ast.tagName, props, children); | ||
node = h(ast.tagName, props, children) | ||
if (ast.nodeName === 'template' && 'content' in ast) { | ||
fragment = ast.content; | ||
pos = ast.sourceCodeLocation | ||
start = pos && pos.startTag && position(pos.startTag).end | ||
end = pos && pos.endTag && position(pos.endTag).start | ||
if (ast.__location) { | ||
fragment.__location = { | ||
startOffset: ast.__location.startTag.endOffset, | ||
endOffset: ast.__location.endTag.startOffset | ||
}; | ||
node.content = transform(ast.content, config) | ||
if ((start || end) && config.file) { | ||
node.content.position = {start: start, end: end} | ||
} | ||
node.content = transform(ast.content, config); | ||
} | ||
return node; | ||
return node | ||
} | ||
/* Create clean positional information. */ | ||
function loc(toPosition, dirty) { | ||
return { | ||
start: toPosition(dirty.startOffset), | ||
end: toPosition(dirty.endOffset) | ||
}; | ||
} | ||
function location(node, location, verbose) { | ||
var pos = position(location) | ||
var reference | ||
var values | ||
var props | ||
var prop | ||
var name | ||
/* Create clean positional information. */ | ||
function location(info, ast, node, config) { | ||
var start = info.startOffset; | ||
var end = info.endOffset; | ||
var values = info.attrs || {}; | ||
var propPositions = {}; | ||
var prop; | ||
var name; | ||
var reference; | ||
if (node.type === 'element') { | ||
reference = node.children[node.children.length - 1] | ||
for (prop in values) { | ||
name = (information(prop) || {}).propertyName || camelcase(prop); | ||
propPositions[name] = loc(config.toPosition, values[prop]); | ||
} | ||
/* Unclosed with children (upstream: https://github.com/inikulin/parse5/issues/109) */ | ||
if ( | ||
!location.endTag && | ||
reference && | ||
reference.position && | ||
reference.position.end | ||
) { | ||
pos.end = xtend(reference.position.end) | ||
} | ||
/* Upstream: https://github.com/inikulin/parse5/issues/109 */ | ||
if (node.type === 'element' && !info.endTag) { | ||
reference = node.children[node.children.length - 1]; | ||
if (verbose) { | ||
values = location.attrs | ||
props = {} | ||
/* Unclosed with children: */ | ||
if (reference && reference.position) { | ||
if (reference.position.end) { | ||
end = reference.position.end.offset; | ||
} else { | ||
end = null; | ||
for (prop in values) { | ||
name = (information(prop) || {}).propertyName || camelcase(prop) | ||
props[name] = position(values[prop]) | ||
} | ||
/* Unclosed without children: */ | ||
} else if (info.startTag) { | ||
end = info.startTag.endOffset; | ||
} | ||
} | ||
if (config.verbose && node.type === 'element') { | ||
node.data = { | ||
position: { | ||
opening: loc(config.toPosition, info.startTag || info), | ||
closing: info.endTag ? loc(config.toPosition, info.endTag) : null, | ||
properties: propPositions | ||
node.data = { | ||
position: { | ||
opening: position(location.startTag), | ||
closing: location.endTag ? position(location.endTag) : null, | ||
properties: props | ||
} | ||
} | ||
}; | ||
} | ||
} | ||
start = typeof start === 'number' ? config.toPosition(start) : null; | ||
end = typeof end === 'number' ? config.toPosition(end) : null; | ||
return pos | ||
} | ||
if (!start && !end) { | ||
return undefined; | ||
} | ||
function position(loc) { | ||
var start = point({ | ||
line: loc.startLine, | ||
column: loc.startCol, | ||
offset: loc.startOffset | ||
}) | ||
var end = point({ | ||
line: loc.endLine, | ||
column: loc.endCol, | ||
offset: loc.endOffset | ||
}) | ||
return start || end ? {start: start, end: end} : null | ||
} | ||
return {start: start, end: end}; | ||
function point(point) { | ||
return point.line && point.column ? point : null | ||
} |
{ | ||
"name": "hast-util-from-parse5", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "Transform Parse5’s AST to HAST", | ||
@@ -22,33 +22,45 @@ "license": "MIT", | ||
"dependencies": { | ||
"camelcase": "^3.0.0", | ||
"camelcase": "^5.0.0", | ||
"ccount": "^1.0.3", | ||
"hastscript": "^3.0.0", | ||
"property-information": "^3.1.0", | ||
"vfile-location": "^2.0.0" | ||
"xtend": "^4.0.1" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^14.0.0", | ||
"babel-core": "^6.26.3", | ||
"babel-preset-env": "^1.7.0", | ||
"babelify": "^8.0.0", | ||
"browserify": "^16.0.0", | ||
"esmangle": "^1.0.1", | ||
"is-hidden": "^1.1.0", | ||
"not": "^0.1.0", | ||
"nyc": "^11.0.0", | ||
"parse5": "^3.0.0", | ||
"remark-cli": "^3.0.0", | ||
"remark-preset-wooorm": "^3.0.0", | ||
"nyc": "^12.0.0", | ||
"parse5": "^5.0.0", | ||
"prettier": "^1.13.5", | ||
"remark-cli": "^5.0.0", | ||
"remark-preset-wooorm": "^4.0.0", | ||
"tape": "^4.0.0", | ||
"unist-util-visit": "^1.1.3", | ||
"vfile": "^2.0.0", | ||
"xo": "^0.18.0" | ||
"vfile": "^3.0.0", | ||
"xo": "^0.21.0" | ||
}, | ||
"scripts": { | ||
"build-md": "remark . -qfo", | ||
"build-bundle": "browserify index.js --bare -s hastUtilFromParse5 > hast-util-from-parse5.js", | ||
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix", | ||
"build-bundle": "browserify index.js --bare -s hastUtilFromParse5 -g [ babelify --presets [ \"babel-preset-env\" ] ] > hast-util-from-parse5.js", | ||
"build-mangle": "esmangle hast-util-from-parse5.js > hast-util-from-parse5.min.js", | ||
"build": "npm run build-md && npm run build-bundle && npm run build-mangle", | ||
"lint": "xo", | ||
"build": "npm run build-bundle && npm run build-mangle", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test", | ||
"test": "npm run build && npm run lint && npm run test-coverage" | ||
"test": "npm run format && npm run build && npm run test-coverage" | ||
}, | ||
"prettier": { | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"singleQuote": true, | ||
"bracketSpacing": false, | ||
"semi": false, | ||
"trailingComma": "none" | ||
}, | ||
"xo": { | ||
"space": true, | ||
"prettier": true, | ||
"esnext": false, | ||
@@ -55,0 +67,0 @@ "rules": { |
@@ -24,12 +24,12 @@ # hast-util-from-parse5 [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov] | ||
```javascript | ||
var vfile = require('to-vfile'); | ||
var parse5 = require('parse5'); | ||
var inspect = require('unist-util-inspect'); | ||
var fromParse5 = require('hast-util-from-parse5'); | ||
var vfile = require('to-vfile') | ||
var parse5 = require('parse5') | ||
var inspect = require('unist-util-inspect') | ||
var fromParse5 = require('hast-util-from-parse5') | ||
var doc = vfile.readSync('example.html'); | ||
var ast = parse5.parse(String(doc), {locationInfo: true}); | ||
var hast = fromParse5(ast, doc); | ||
var doc = vfile.readSync('example.html') | ||
var ast = parse5.parse(String(doc), {sourceCodeLocationInfo: true}) | ||
var hast = fromParse5(ast, doc) | ||
console.log(inspect(hast)); | ||
console.log(inspect(hast)) | ||
``` | ||
@@ -122,2 +122,10 @@ | ||
## Contribute | ||
See [`contributing.md` in `syntax-tree/hast`][contributing] for ways to get | ||
started. | ||
This organisation has a [Code of Conduct][coc]. By interacting with this | ||
repository, organisation, or community you agree to abide by its terms. | ||
## License | ||
@@ -150,1 +158,5 @@ | ||
[vfile]: https://github.com/vfile/vfile | ||
[contributing]: https://github.com/syntax-tree/hast/blob/master/contributing.md | ||
[coc]: https://github.com/syntax-tree/hast/blob/master/code-of-conduct.md |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11789
177
160
5
16
1
+ Addedccount@^1.0.3
+ Addedxtend@^4.0.1
+ Addedcamelcase@5.3.1(transitive)
+ Addedccount@1.1.0(transitive)
+ Addedxtend@4.0.2(transitive)
- Removedvfile-location@^2.0.0
- Removedvfile-location@2.0.6(transitive)
Updatedcamelcase@^5.0.0