Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hast-util-to-parse5

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hast-util-to-parse5 - npm Package Compare versions

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",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc