hast-util-to-parse5
Advanced tools
Comparing version 3.0.0 to 4.0.0
225
index.js
'use strict' | ||
var xtend = require('xtend') | ||
var html = require('property-information/html') | ||
var svg = require('property-information/svg') | ||
var find = require('property-information/find') | ||
var toH = require('hast-to-hyperscript') | ||
var NS = require('web-namespaces') | ||
var ns = require('web-namespaces') | ||
var zwitch = require('zwitch') | ||
var mapz = require('mapz') | ||
module.exports = transform | ||
var own = {}.hasOwnProperty | ||
var ignoredSpaces = ['svg', 'html'] | ||
var one = zwitch('type') | ||
var all = mapz(one, {key: 'children', indices: false}) | ||
var customProps = [ | ||
'sourceCodeLocation', | ||
'childNodes', | ||
'content', | ||
'parentNode', | ||
'namespaceURI' | ||
] | ||
one.handlers.root = root | ||
@@ -29,56 +23,67 @@ one.handlers.element = element | ||
/* Map of tag-names starting new namespaces. */ | ||
var namespaces = { | ||
math: NS.mathml, | ||
svg: NS.svg | ||
// Transform a tree from HAST to Parse5’s AST. | ||
function transform(tree, space) { | ||
return one(tree, space === 'svg' ? svg : html) | ||
} | ||
/* Map of attributes with namespaces. */ | ||
var attributeSpaces = { | ||
'xlink:actuate': {prefix: 'xlink', name: 'actuate', namespace: NS.xlink}, | ||
'xlink:arcrole': {prefix: 'xlink', name: 'arcrole', namespace: NS.xlink}, | ||
'xlink:href': {prefix: 'xlink', name: 'href', namespace: NS.xlink}, | ||
'xlink:role': {prefix: 'xlink', name: 'role', namespace: NS.xlink}, | ||
'xlink:show': {prefix: 'xlink', name: 'show', namespace: NS.xlink}, | ||
'xlink:title': {prefix: 'xlink', name: 'title', namespace: NS.xlink}, | ||
'xlink:type': {prefix: 'xlink', name: 'type', namespace: NS.xlink}, | ||
'xml:base': {prefix: 'xml', name: 'base', namespace: NS.xml}, | ||
'xml:lang': {prefix: 'xml', name: 'lang', namespace: NS.xml}, | ||
'xml:space': {prefix: 'xml', name: 'space', namespace: NS.xml}, | ||
xmlns: {prefix: '', name: 'xmlns', namespace: NS.xmlns}, | ||
'xmlns:xlink': {prefix: 'xmlns', name: 'xlink', namespace: NS.xmlns} | ||
function root(node, schema) { | ||
var data = node.data || {} | ||
var mode = data.quirksMode ? 'quirks' : 'no-quirks' | ||
return patch(node, {nodeName: '#document', mode: mode}, schema) | ||
} | ||
/* Transform a tree from HAST to Parse5’s AST. */ | ||
function transform(tree) { | ||
return patch(one(tree), null, NS.html) | ||
function fragment(node, schema) { | ||
return patch(node, {nodeName: '#document-fragment'}, schema) | ||
} | ||
function root(node) { | ||
var data = node.data || {} | ||
var qs = own.call(data, 'quirksMode') ? Boolean(data.quirksMode) : false | ||
function doctype(node, schema) { | ||
return patch( | ||
node, | ||
{ | ||
nodeName: '#documentType', | ||
name: node.name, | ||
publicId: node.public || '', | ||
systemId: node.system || '' | ||
}, | ||
schema | ||
) | ||
} | ||
return { | ||
nodeName: '#document', | ||
mode: qs ? 'quirks' : 'no-quirks', | ||
childNodes: all(node) | ||
} | ||
function text(node, schema) { | ||
return patch(node, {nodeName: '#text', value: node.value}, schema) | ||
} | ||
function element(node) { | ||
var shallow = xtend(node) | ||
function comment(node, schema) { | ||
return patch(node, {nodeName: '#comment', data: node.value}, schema) | ||
} | ||
shallow.children = [] | ||
function element(node, schema) { | ||
var space = schema.space | ||
var shallow = xtend(node, {children: []}) | ||
return toH(function(name, attrs) { | ||
return toH(h, shallow, {space: space}) | ||
function h(name, attrs) { | ||
var values = [] | ||
var content | ||
var p5 | ||
var value | ||
var key | ||
var info | ||
var pos | ||
for (key in attrs) { | ||
info = find(schema, key) | ||
value = {name: key, value: attrs[key]} | ||
if (own.call(attributeSpaces, key)) { | ||
value = xtend(value, attributeSpaces[key]) | ||
if (info.space && ignoredSpaces.indexOf(info.space) === -1) { | ||
pos = key.indexOf(':') | ||
if (pos === -1) { | ||
value.prefix = '' | ||
} else { | ||
value.name = key.slice(pos + 1) | ||
value.prefix = key.slice(0, pos) | ||
} | ||
value.namespace = ns[info.space] | ||
} | ||
@@ -89,50 +94,42 @@ | ||
p5 = patch(node, {nodeName: name, tagName: name, attrs: values}, schema) | ||
if (name === 'template') { | ||
content = transform(shallow.content) | ||
delete content.mode | ||
content.nodeName = '#document-fragment' | ||
p5.content = fragment(shallow.content, schema) | ||
} | ||
return wrap( | ||
node, | ||
{ | ||
nodeName: node.tagName, | ||
tagName: node.tagName, | ||
attrs: values, | ||
childNodes: node.children ? all(node) : [] | ||
}, | ||
content | ||
) | ||
}, shallow) | ||
return p5 | ||
} | ||
} | ||
function doctype(node) { | ||
return wrap(node, { | ||
nodeName: '#documentType', | ||
name: node.name, | ||
publicId: node.public || '', | ||
systemId: node.system || '' | ||
}) | ||
} | ||
// Patch specific properties. | ||
function patch(node, p5, parentSchema) { | ||
var schema = parentSchema | ||
var position = node.position | ||
var children = node.children | ||
var childNodes = [] | ||
var length = children ? children.length : 0 | ||
var index = -1 | ||
var child | ||
function text(node) { | ||
return wrap(node, { | ||
nodeName: '#text', | ||
value: node.value | ||
}) | ||
} | ||
if (node.type === 'element') { | ||
if (schema.space === 'html' && node.tagName === 'svg') { | ||
schema = svg | ||
} | ||
function comment(node) { | ||
return wrap(node, { | ||
nodeName: '#comment', | ||
data: node.value | ||
}) | ||
} | ||
p5.namespaceURI = ns[schema.space] | ||
} | ||
/* Patch position. */ | ||
function wrap(node, ast, content) { | ||
var position = node.position | ||
while (++index < length) { | ||
child = one(children[index], schema) | ||
child.parentNode = p5 | ||
childNodes[index] = child | ||
} | ||
if (node.type === 'element' || node.type === 'root') { | ||
p5.childNodes = childNodes | ||
} | ||
if (position && position.start && position.end) { | ||
ast.sourceCodeLocation = { | ||
p5.sourceCodeLocation = { | ||
startLine: position.start.line, | ||
@@ -147,57 +144,3 @@ startCol: position.start.column, | ||
if (content) { | ||
ast.content = content | ||
} | ||
return ast | ||
return p5 | ||
} | ||
/* Patch a tree recursively, by adding namespaces | ||
* and parent references where needed. */ | ||
function patch(node, parent, ns) { | ||
var location = node.sourceCodeLocation | ||
var children = node.childNodes | ||
var name = node.tagName | ||
var replacement = {} | ||
var length | ||
var index | ||
var key | ||
for (key in node) { | ||
if (customProps.indexOf(key) === -1) { | ||
replacement[key] = node[key] | ||
} | ||
} | ||
if (own.call(namespaces, name)) { | ||
ns = namespaces[name] | ||
} | ||
if (own.call(replacement, 'tagName')) { | ||
replacement.namespaceURI = ns | ||
} | ||
if (children) { | ||
replacement.childNodes = children | ||
length = children.length | ||
index = -1 | ||
while (++index < length) { | ||
children[index] = patch(children[index], replacement, ns) | ||
} | ||
} | ||
if (name === 'template') { | ||
replacement.content = patch(node.content, null, ns) | ||
} | ||
if (parent) { | ||
replacement.parentNode = parent | ||
} | ||
if (location) { | ||
replacement.sourceCodeLocation = location | ||
} | ||
return replacement | ||
} |
{ | ||
"name": "hast-util-to-parse5", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "Transform HAST to Parse5’s AST", | ||
@@ -22,4 +22,4 @@ "license": "MIT", | ||
"dependencies": { | ||
"hast-to-hyperscript": "^4.0.0", | ||
"mapz": "^1.0.0", | ||
"hast-to-hyperscript": "^5.0.0", | ||
"property-information": "^4.0.0", | ||
"web-namespaces": "^1.0.0", | ||
@@ -31,3 +31,3 @@ "xtend": "^4.0.1", | ||
"browserify": "^16.0.0", | ||
"esmangle": "^1.0.1", | ||
"json-stringify-safe": "^5.0.1", | ||
"nyc": "^12.0.0", | ||
@@ -39,2 +39,3 @@ "parse5": "^5.0.0", | ||
"tape": "^4.0.0", | ||
"tinyify": "^2.4.3", | ||
"xo": "^0.21.0" | ||
@@ -44,4 +45,4 @@ }, | ||
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix", | ||
"build-bundle": "browserify index.js --bare -s hastUtilToParse5 > hast-util-to-parse5.js", | ||
"build-mangle": "esmangle hast-util-to-parse5.js > hast-util-to-parse5.min.js", | ||
"build-bundle": "browserify index.js -s hastUtilToParse5 > hast-util-to-parse5.js", | ||
"build-mangle": "browserify index.js -p tinyify -s hastUtilToParse5 > hast-util-to-parse5.min.js", | ||
"build": "npm run build-bundle && npm run build-mangle", | ||
@@ -48,0 +49,0 @@ "test-api": "node test", |
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
8238
10
114
1
+ Addedproperty-information@^4.0.0
+ Addedhast-to-hyperscript@5.0.0(transitive)
+ Addedinline-style-parser@0.1.1(transitive)
+ Addedproperty-information@4.2.0(transitive)
+ Addedstyle-to-object@0.2.3(transitive)
- Removedmapz@^1.0.0
- Removedcall-bind@1.0.7(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removeddefine-properties@1.2.1(transitive)
- Removedes-define-property@1.0.0(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-intrinsic@1.2.4(transitive)
- Removedgopd@1.0.1(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedhas-proto@1.0.3(transitive)
- Removedhas-symbols@1.0.3(transitive)
- Removedhasown@2.0.2(transitive)
- Removedhast-to-hyperscript@4.0.0(transitive)
- Removedis-nan@1.3.2(transitive)
- Removedkebab-case@1.0.2(transitive)
- Removedmapz@1.0.4(transitive)
- Removedobject-keys@1.1.1(transitive)
- Removedproperty-information@3.2.0(transitive)
- Removedset-function-length@1.2.2(transitive)
- Removedtrim@0.0.1(transitive)
- Removedx-is-array@0.1.0(transitive)
Updatedhast-to-hyperscript@^5.0.0