hast-util-from-parse5
Advanced tools
Comparing version 6.0.0 to 6.0.1
106
index.js
'use strict' | ||
var s = require('hastscript/svg') | ||
var h = require('hastscript') | ||
var find = require('property-information/find') | ||
var html = require('property-information/html') | ||
var svg = require('property-information/svg') | ||
var find = require('property-information/find') | ||
var vfileLocation = require('vfile-location') | ||
var ns = require('web-namespaces') | ||
var s = require('hastscript/svg') | ||
var h = require('hastscript') | ||
var count = require('ccount') | ||
@@ -39,4 +39,3 @@ module.exports = wrapper | ||
file: file, | ||
verbose: settings.verbose, | ||
location: false | ||
verbose: settings.verbose | ||
}) | ||
@@ -50,4 +49,4 @@ } | ||
var children | ||
var node | ||
var pos | ||
var result | ||
var position | ||
@@ -62,10 +61,10 @@ if (fn === element) { | ||
node = fn(ast, children, config) | ||
result = fn(ast, children, config) | ||
if (ast.sourceCodeLocation && config.file) { | ||
pos = location(node, ast.sourceCodeLocation, config) | ||
position = location(result, ast.sourceCodeLocation, config) | ||
if (pos) { | ||
if (position) { | ||
config.location = true | ||
node.position = pos | ||
result.position = position | ||
} | ||
@@ -76,3 +75,3 @@ } | ||
return node | ||
return result | ||
} | ||
@@ -82,7 +81,6 @@ | ||
function nodes(children, config) { | ||
var length = children.length | ||
var index = -1 | ||
var result = [] | ||
while (++index < length) { | ||
while (++index < children.length) { | ||
result[index] = transform(children[index], config) | ||
@@ -97,21 +95,20 @@ } | ||
function root(ast, children, config) { | ||
var node = {type: 'root', children: children, data: {}} | ||
var result = { | ||
type: 'root', | ||
children: children, | ||
data: {quirksMode: ast.mode === 'quirks' || ast.mode === 'limited-quirks'} | ||
} | ||
var doc | ||
var location | ||
node.data.quirksMode = ast.mode === 'quirks' || ast.mode === 'limited-quirks' | ||
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 | ||
} | ||
location = vfileLocation(doc) | ||
result.position = { | ||
start: location.toPoint(0), | ||
end: location.toPoint(doc.length) | ||
} | ||
} | ||
return node | ||
return result | ||
} | ||
@@ -142,10 +139,6 @@ | ||
var fn = config.schema.space === 'svg' ? s : h | ||
var name = ast.tagName | ||
var attributes = ast.attrs | ||
var length = attributes.length | ||
var props = {} | ||
var index = -1 | ||
var result | ||
var attribute | ||
var prop | ||
var node | ||
var pos | ||
@@ -155,11 +148,11 @@ var start | ||
while (++index < length) { | ||
attribute = attributes[index] | ||
prop = (attribute.prefix ? attribute.prefix + ':' : '') + attribute.name | ||
props[prop] = attribute.value | ||
while (++index < ast.attrs.length) { | ||
attribute = ast.attrs[index] | ||
props[(attribute.prefix ? attribute.prefix + ':' : '') + attribute.name] = | ||
attribute.value | ||
} | ||
node = fn(name, props, children) | ||
result = fn(ast.tagName, props, children) | ||
if (name === 'template' && 'content' in ast) { | ||
if (result.tagName === 'template' && 'content' in ast) { | ||
pos = ast.sourceCodeLocation | ||
@@ -169,10 +162,10 @@ start = pos && pos.startTag && position(pos.startTag).end | ||
node.content = transform(ast.content, config) | ||
result.content = transform(ast.content, config) | ||
if ((start || end) && config.file) { | ||
node.content.position = {start: start, end: end} | ||
result.content.position = {start: start, end: end} | ||
} | ||
} | ||
return node | ||
return result | ||
} | ||
@@ -182,32 +175,21 @@ | ||
function location(node, location, config) { | ||
var schema = config.schema | ||
var verbose = config.verbose | ||
var pos = position(location) | ||
var reference | ||
var attributes | ||
var attribute | ||
var result = position(location) | ||
var tail | ||
var key | ||
var props | ||
var prop | ||
if (node.type === 'element') { | ||
reference = node.children[node.children.length - 1] | ||
tail = node.children[node.children.length - 1] | ||
// Bug for unclosed with children. | ||
// See: <https://github.com/inikulin/parse5/issues/109>. | ||
if ( | ||
!location.endTag && | ||
reference && | ||
reference.position && | ||
reference.position.end | ||
) { | ||
pos.end = Object.assign({}, reference.position.end) | ||
if (!location.endTag && tail && tail.position && tail.position.end) { | ||
result.end = Object.assign({}, tail.position.end) | ||
} | ||
if (verbose) { | ||
attributes = location.attrs | ||
if (config.verbose) { | ||
props = {} | ||
for (attribute in attributes) { | ||
prop = find(schema, attribute).property | ||
props[prop] = position(attributes[attribute]) | ||
for (key in location.attrs) { | ||
props[find(config.schema, key).property] = position(location.attrs[key]) | ||
} | ||
@@ -225,3 +207,3 @@ | ||
return pos | ||
return result | ||
} | ||
@@ -228,0 +210,0 @@ |
{ | ||
"name": "hast-util-from-parse5", | ||
"version": "6.0.0", | ||
"version": "6.0.1", | ||
"description": "hast utility to transform from Parse5’s AST", | ||
@@ -33,11 +33,11 @@ "license": "MIT", | ||
"@types/parse5": "^5.0.0", | ||
"ccount": "^1.0.0", | ||
"hastscript": "^5.0.0", | ||
"hastscript": "^6.0.0", | ||
"property-information": "^5.0.0", | ||
"vfile": "^4.0.0", | ||
"vfile-location": "^3.2.0", | ||
"web-namespaces": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^16.0.0", | ||
"dtslint": "^3.0.0", | ||
"browserify": "^17.0.0", | ||
"dtslint": "^4.0.0", | ||
"is-hidden": "^1.0.0", | ||
@@ -48,12 +48,12 @@ "not": "^0.1.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^8.0.0", | ||
"remark-preset-wooorm": "^7.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"tape": "^5.0.0", | ||
"tinyify": "^2.0.0", | ||
"tinyify": "^3.0.0", | ||
"to-vfile": "^6.0.0", | ||
"unist-util-visit": "^2.0.0", | ||
"xo": "^0.30.0" | ||
"xo": "^0.34.0" | ||
}, | ||
"scripts": { | ||
"format": "remark . -qfo && prettier --write . && xo --fix", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"build-bundle": "browserify index.js -s hastUtilFromParse5 > hast-util-from-parse5.js", | ||
@@ -80,2 +80,3 @@ "build-mangle": "browserify index.js -p tinyify -s hastUtilFromParse5 > hast-util-from-parse5.min.js", | ||
"unicorn/no-fn-reference-in-iterator": "off", | ||
"unicorn/prefer-optional-catch-binding": "off", | ||
"guard-for-in": "off" | ||
@@ -82,0 +83,0 @@ }, |
@@ -38,5 +38,5 @@ # hast-util-from-parse5 | ||
var doc = vfile.readSync('example.html') | ||
var ast = parse5.parse(String(doc), {sourceCodeLocationInfo: true}) | ||
var hast = fromParse5(ast, doc) | ||
var file = vfile.readSync('example.html') | ||
var p5ast = parse5.parse(String(file), {sourceCodeLocationInfo: true}) | ||
var hast = fromParse5(p5ast, file) | ||
@@ -49,13 +49,21 @@ console.log(inspect(hast)) | ||
```text | ||
root[2] (1:1-2:1, 0-70) [data={"quirksMode":false}] | ||
├─ doctype (1:1-1:16, 0-15) [name="html"] | ||
└─ element[2] [tagName="html"] | ||
├─ element[1] [tagName="head"] | ||
│ └─ element[1] (1:16-1:37, 15-36) [tagName="title"] | ||
│ └─ text: "Hello!" (1:23-1:29, 22-28) | ||
└─ element[1] [tagName="body"] | ||
└─ element[3] (1:37-2:1, 36-70) [tagName="h1"][properties={"id":"world"}] | ||
├─ text: "World!" (1:52-1:58, 51-57) | ||
├─ comment: "after" (1:58-1:70, 57-69) | ||
└─ text: "\n" (1:70-2:1, 69-70) | ||
root[2] (1:1-2:1, 0-70) | ||
│ data: {"quirksMode":false} | ||
├─0 doctype<html> (1:1-1:16, 0-15) | ||
│ public: null | ||
│ system: null | ||
└─1 element<html>[2] | ||
│ properties: {} | ||
├─0 element<head>[1] | ||
│ │ properties: {} | ||
│ └─0 element<title>[1] (1:16-1:37, 15-36) | ||
│ │ properties: {} | ||
│ └─0 text "Hello!" (1:23-1:29, 22-28) | ||
└─1 element<body>[1] | ||
│ properties: {} | ||
└─0 element<h1>[3] (1:37-2:1, 36-70) | ||
│ properties: {"id":"world"} | ||
├─0 text "World!" (1:52-1:58, 51-57) | ||
├─1 comment "after" (1:58-1:70, 57-69) | ||
└─2 text "\n" (1:70-2:1, 69-70) | ||
``` | ||
@@ -65,3 +73,3 @@ | ||
### `fromParse5(ast[, options])` | ||
### `fromParse5(ast[, file|options])` | ||
@@ -198,5 +206,5 @@ Transform [Parse5’s AST][ast] to a [**hast**][hast] [*tree*][tree]. | ||
[chat-badge]: https://img.shields.io/badge/chat-spectrum-7b16ff.svg | ||
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg | ||
[chat]: https://spectrum.chat/unified/syntax-tree | ||
[chat]: https://github.com/syntax-tree/unist/discussions | ||
@@ -209,9 +217,9 @@ [npm]: https://docs.npmjs.com/cli/install | ||
[contributing]: https://github.com/syntax-tree/.github/blob/master/contributing.md | ||
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md | ||
[support]: https://github.com/syntax-tree/.github/blob/master/support.md | ||
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/master/code-of-conduct.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md | ||
[ast]: https://github.com/inikulin/parse5/blob/master/packages/parse5/docs/tree-adapter/default/interface-list.md | ||
[ast]: https://github.com/inikulin/parse5/blob/HEAD/packages/parse5/docs/tree-adapter/default/interface-list.md | ||
@@ -218,0 +226,0 @@ [vfile]: https://github.com/vfile/vfile |
@@ -43,3 +43,3 @@ // TypeScript Version: 3.5 | ||
/** | ||
* Transform [Parse5’s AST](https://github.com/inikulin/parse5/blob/master/packages/parse5/docs/tree-adapter/default/interface-list.md) | ||
* Transform [Parse5’s AST](https://github.com/inikulin/parse5/blob/HEAD/packages/parse5/docs/tree-adapter/default/interface-list.md) | ||
* to a [**hast**](https://github.com/syntax-tree/hast) | ||
@@ -46,0 +46,0 @@ * [*tree*](https://github.com/syntax-tree/unist#tree). |
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
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
236
16759
227
+ Addedvfile-location@^3.2.0
+ Added@types/hast@2.3.10(transitive)
+ Addedhastscript@6.0.0(transitive)
+ Addedvfile-location@3.2.0(transitive)
- Removedccount@^1.0.0
- Removedccount@1.1.0(transitive)
- Removedhastscript@5.1.2(transitive)
Updatedhastscript@^6.0.0