hast-util-from-parse5
Advanced tools
Comparing version 7.0.0 to 7.1.0
@@ -7,5 +7,7 @@ /** | ||
*/ | ||
export function fromParse5(ast: P5Node, options?: Options | VFile): Node | ||
export function fromParse5( | ||
ast: P5Node, | ||
options?: import('vfile').VFile | Options | undefined | ||
): Node | ||
export type VFile = import('vfile').VFile | ||
export type VFileLocation = import('vfile-location').Location | ||
export type Schema = import('property-information').Schema | ||
@@ -36,3 +38,10 @@ export type Position = import('unist').Position | ||
node: P5Node, | ||
children: Array<Child> | ||
children?: | ||
| ( | ||
| import('hast').Element | ||
| import('hast').Text | ||
| import('hast').Comment | ||
| import('hast').DocType | ||
)[] | ||
| undefined | ||
) => Node | ||
@@ -43,17 +52,17 @@ export type Options = { | ||
*/ | ||
space?: Space | ||
space?: Space | undefined | ||
/** | ||
* `VFile`, used to add positional information to nodes. If given, the file should have the original HTML source as its contents | ||
*/ | ||
file?: VFile | ||
file?: import('vfile').VFile | undefined | ||
/** | ||
* Whether to add extra positional information about starting tags, closing tags, and attributes to elements. Note: not used without `file` | ||
*/ | ||
verbose?: boolean | ||
verbose?: boolean | undefined | ||
} | ||
export type Context = { | ||
schema: Schema | ||
file: VFile | ||
verbose: boolean | ||
file: VFile | undefined | ||
verbose: boolean | undefined | ||
location: boolean | ||
} |
130
lib/index.js
/** | ||
* @typedef {import('vfile').VFile} VFile | ||
* @typedef {import('vfile-location').Location} VFileLocation | ||
* @typedef {import('property-information').Schema} Schema | ||
@@ -31,3 +30,3 @@ * @typedef {import('unist').Position} Position | ||
* @param {P5Node} node | ||
* @param {Array.<Child>} children | ||
* @param {Array.<Child>} [children] | ||
* @returns {Node} | ||
@@ -42,4 +41,4 @@ * | ||
* @property {Schema} schema | ||
* @property {VFile} file | ||
* @property {boolean} verbose | ||
* @property {VFile|undefined} file | ||
* @property {boolean|undefined} verbose | ||
* @property {boolean} location | ||
@@ -50,9 +49,9 @@ */ | ||
import {html, svg, find} from 'property-information' | ||
import vfileLocation from 'vfile-location' | ||
import {location} from 'vfile-location' | ||
import {webNamespaces} from 'web-namespaces' | ||
var own = {}.hasOwnProperty | ||
const own = {}.hasOwnProperty | ||
// Handlers. | ||
var map = { | ||
const map = { | ||
'#document': root, | ||
@@ -73,5 +72,5 @@ '#document-fragment': root, | ||
/** @type {Options} */ | ||
var settings | ||
/** @type {VFile} */ | ||
var file | ||
let settings | ||
/** @type {VFile|undefined} */ | ||
let file | ||
@@ -105,11 +104,8 @@ if (isFile(options)) { | ||
function transform(ctx, ast) { | ||
var schema = ctx.schema | ||
const schema = ctx.schema | ||
/** @type {Handler} */ | ||
var fn = own.call(map, ast.nodeName) ? map[ast.nodeName] : element | ||
/** @type {Array.<Child>} */ | ||
var children | ||
/** @type {Node} */ | ||
var result | ||
/** @type {Position} */ | ||
var position | ||
// @ts-expect-error: index is fine. | ||
const fn = own.call(map, ast.nodeName) ? map[ast.nodeName] : element | ||
/** @type {Array.<Child>|undefined} */ | ||
let children | ||
@@ -125,7 +121,7 @@ // Element. | ||
result = fn(ctx, ast, children) | ||
const result = fn(ctx, ast, children) | ||
if ('sourceCodeLocation' in ast && ast.sourceCodeLocation && ctx.file) { | ||
// @ts-ignore It’s fine. | ||
position = location(ctx, result, ast.sourceCodeLocation) | ||
// @ts-expect-error It’s fine. | ||
const position = createLocation(ctx, result, ast.sourceCodeLocation) | ||
@@ -151,8 +147,8 @@ if (position) { | ||
function nodes(ctx, children) { | ||
var index = -1 | ||
let index = -1 | ||
/** @type {Array.<Child>} */ | ||
var result = [] | ||
const result = [] | ||
while (++index < children.length) { | ||
// @ts-ignore Assume no roots in children. | ||
// @ts-expect-error Assume no roots in children. | ||
result[index] = transform(ctx, children[index]) | ||
@@ -175,3 +171,3 @@ } | ||
/** @type {Root} */ | ||
var result = { | ||
const result = { | ||
type: 'root', | ||
@@ -181,13 +177,9 @@ children, | ||
} | ||
/** @type {string} */ | ||
var doc | ||
/** @type {VFileLocation} */ | ||
var location | ||
if (ctx.file && ctx.location) { | ||
doc = String(ctx.file) | ||
location = vfileLocation(doc) | ||
const doc = String(ctx.file) | ||
const loc = location(doc) | ||
result.position = { | ||
start: location.toPoint(0), | ||
end: location.toPoint(doc.length) | ||
start: loc.toPoint(0), | ||
end: loc.toPoint(doc.length) | ||
} | ||
@@ -206,3 +198,3 @@ } | ||
function doctype() { | ||
// @ts-ignore Types are out of date. | ||
// @ts-expect-error Types are out of date. | ||
return {type: 'doctype'} | ||
@@ -242,19 +234,9 @@ } | ||
function element(ctx, ast, children) { | ||
var fn = ctx.schema.space === 'svg' ? s : h | ||
var index = -1 | ||
const fn = ctx.schema.space === 'svg' ? s : h | ||
let index = -1 | ||
/** @type {Object.<string, string>} */ | ||
var props = {} | ||
/** @type {Element} */ | ||
var result | ||
/** @type {P5Attribute} */ | ||
var attribute | ||
/** @type {P5ElementLocation} */ | ||
var pos | ||
/** @type {Point} */ | ||
var start | ||
/** @type {Point} */ | ||
var end | ||
const props = {} | ||
while (++index < ast.attrs.length) { | ||
attribute = ast.attrs[index] | ||
const attribute = ast.attrs[index] | ||
props[(attribute.prefix ? attribute.prefix + ':' : '') + attribute.name] = | ||
@@ -264,16 +246,18 @@ attribute.value | ||
result = fn(ast.tagName, props, children) | ||
const result = fn(ast.tagName, props, children) | ||
if (result.tagName === 'template' && 'content' in ast) { | ||
// @ts-ignore Types are wrong. | ||
pos = ast.sourceCodeLocation | ||
start = pos && pos.startTag && position(pos.startTag).end | ||
end = pos && pos.endTag && position(pos.endTag).start | ||
const pos = ast.sourceCodeLocation | ||
const startTag = pos && pos.startTag && position(pos.startTag) | ||
const endTag = pos && pos.endTag && position(pos.endTag) | ||
// @ts-ignore Types are wrong. | ||
result.content = transform(ctx, ast.content) | ||
/** @type {Root} */ | ||
// @ts-expect-error Types are wrong. | ||
const content = transform(ctx, ast.content) | ||
if ((start || end) && ctx.file) { | ||
result.content.position = {start, end} | ||
if (startTag && endTag && ctx.file) { | ||
content.position = {start: startTag.end, end: endTag.start} | ||
} | ||
result.content = content | ||
} | ||
@@ -290,19 +274,19 @@ | ||
* @param {P5ElementLocation} location | ||
* @returns {Position} | ||
* @returns {Position|null} | ||
*/ | ||
function location(ctx, node, location) { | ||
var result = position(location) | ||
/** @type {ElementChild} */ | ||
var tail | ||
/** @type {string} */ | ||
var key | ||
/** @type {Object.<string, Position>} */ | ||
var props | ||
function createLocation(ctx, node, location) { | ||
const result = position(location) | ||
if (node.type === 'element') { | ||
tail = node.children[node.children.length - 1] | ||
const tail = node.children[node.children.length - 1] | ||
// Bug for unclosed with children. | ||
// See: <https://github.com/inikulin/parse5/issues/109>. | ||
if (!location.endTag && tail && tail.position && tail.position.end) { | ||
if ( | ||
result && | ||
!location.endTag && | ||
tail && | ||
tail.position && | ||
tail.position.end | ||
) { | ||
result.end = Object.assign({}, tail.position.end) | ||
@@ -312,3 +296,6 @@ } | ||
if (ctx.verbose) { | ||
props = {} | ||
/** @type {Object.<string, Position|null>} */ | ||
const props = {} | ||
/** @type {string} */ | ||
let key | ||
@@ -339,3 +326,3 @@ for (key in location.attrs) { | ||
function position(loc) { | ||
var start = point({ | ||
const start = point({ | ||
line: loc.startLine, | ||
@@ -345,3 +332,3 @@ column: loc.startCol, | ||
}) | ||
var end = point({ | ||
const end = point({ | ||
line: loc.endLine, | ||
@@ -351,2 +338,3 @@ column: loc.endCol, | ||
}) | ||
// @ts-expect-error `null` is fine. | ||
return start || end ? {start, end} : null | ||
@@ -353,0 +341,0 @@ } |
{ | ||
"name": "hast-util-from-parse5", | ||
"version": "7.0.0", | ||
"version": "7.1.0", | ||
"description": "hast utility to transform from Parse5’s AST", | ||
@@ -41,4 +41,4 @@ "license": "MIT", | ||
"property-information": "^6.0.0", | ||
"vfile": "^4.0.0", | ||
"vfile-location": "^3.2.0", | ||
"vfile": "^5.0.0", | ||
"vfile-location": "^4.0.0", | ||
"web-namespaces": "^2.0.0" | ||
@@ -56,7 +56,7 @@ }, | ||
"tape": "^5.0.0", | ||
"to-vfile": "^6.0.0", | ||
"to-vfile": "^7.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"unist-util-visit": "^3.0.0", | ||
"xo": "^0.39.0" | ||
"xo": "^0.42.0" | ||
}, | ||
@@ -80,7 +80,3 @@ "scripts": { | ||
"xo": { | ||
"prettier": true, | ||
"rules": { | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
} | ||
"prettier": true | ||
}, | ||
@@ -87,0 +83,0 @@ "remarkConfig": { |
@@ -36,10 +36,10 @@ # hast-util-from-parse5 | ||
```js | ||
import vfile from 'to-vfile' | ||
import parse5 from 'parse5' | ||
import {readSync} from 'to-vfile' | ||
import {inspect} from 'unist-util-inspect' | ||
import {fromParse5} from 'hast-util-from-parse5' | ||
var file = vfile.readSync('example.html') | ||
var p5ast = parse5.parse(String(file), {sourceCodeLocationInfo: true}) | ||
var hast = fromParse5(p5ast, file) | ||
const file = readSync('example.html') | ||
const p5ast = parse5.parse(String(file), {sourceCodeLocationInfo: true}) | ||
const hast = fromParse5(p5ast, file) | ||
@@ -46,0 +46,0 @@ console.log(inspect(hast)) |
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
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
0
21023
370
+ Addedunist-util-stringify-position@3.0.3(transitive)
+ Addedvfile@5.3.7(transitive)
+ Addedvfile-location@4.1.0(transitive)
+ Addedvfile-message@3.1.4(transitive)
- Removedunist-util-stringify-position@2.0.3(transitive)
- Removedvfile@4.2.1(transitive)
- Removedvfile-location@3.2.0(transitive)
- Removedvfile-message@2.0.4(transitive)
Updatedvfile@^5.0.0
Updatedvfile-location@^4.0.0