hast-util-to-dom
Advanced tools
Comparing version 1.1.0 to 2.0.0
@@ -5,41 +5,69 @@ 'use strict'; | ||
var ns = _interopDefault(require('web-namespaces')); | ||
var info = _interopDefault(require('property-information')); | ||
var ROOT_NODE = 'root'; | ||
var TEXT_NODE = 'text'; | ||
var ELEMENT_NODE = 'element'; | ||
var DOCUMENT_TYPE_NODE = 'doctype'; | ||
var COMMENT_NODE = 'comment'; | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
function transform(node) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
return obj; | ||
} | ||
function _objectSpread(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
function transform(node, options) { | ||
switch (node.type) { | ||
case ROOT_NODE: | ||
case 'root': | ||
return root(node, options); | ||
case TEXT_NODE: | ||
case 'text': | ||
return text(node, options); | ||
case ELEMENT_NODE: | ||
case 'element': | ||
return element(node, options); | ||
case DOCUMENT_TYPE_NODE: | ||
case 'doctype': | ||
return doctype(node, options); | ||
case COMMENT_NODE: | ||
case 'comment': | ||
return comment(node, options); | ||
default: | ||
return element(node, options); | ||
} | ||
} | ||
} // Create a document. | ||
/** | ||
* Transform a document | ||
*/ | ||
function root(node) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var fragment = options.fragment, | ||
function root(node, options) { | ||
var doc = options.doc, | ||
fragment = options.fragment, | ||
optionsNamespace = options.namespace; | ||
var _node$children = node.children, | ||
children = _node$children === undefined ? [] : _node$children; | ||
children = _node$children === void 0 ? [] : _node$children; | ||
var childrenLength = children.length; | ||
var namespace = optionsNamespace; | ||
@@ -51,77 +79,63 @@ var rootIsDocument = childrenLength === 0; | ||
tagName = _children$i.tagName, | ||
_children$i$propertie = _children$i.properties; | ||
_children$i$propertie = _children$i$propertie === undefined ? {} : _children$i$propertie; | ||
var xmlns = _children$i$propertie.xmlns; | ||
_children$i$propertie = _children$i.properties, | ||
properties = _children$i$propertie === void 0 ? {} : _children$i$propertie; | ||
if (tagName === 'html') { | ||
// If we have a root HTML node, we don't need to render as a fragment | ||
rootIsDocument = true; | ||
// Take namespace of first child | ||
// If we have a root HTML node, we don’t need to render as a fragment. | ||
rootIsDocument = true; // Take namespace of the first child. | ||
if (typeof optionsNamespace === 'undefined') { | ||
if (xmlns) { | ||
namespace = xmlns; | ||
} else if (children[0].tagName === 'html') { | ||
namespace = 'http://www.w3.org/1999/xhtml'; | ||
} | ||
namespace = properties.xmlns || ns.html; | ||
} | ||
} | ||
} | ||
} // The root node will be a Document, DocumentFragment, or HTMLElement. | ||
// The root node will be a Document, DocumentFragment, or HTMLElement | ||
var el = void 0; | ||
var el; | ||
if (rootIsDocument) { | ||
el = document.implementation.createDocument(namespace, '', null); | ||
el = doc.implementation.createDocument(namespace, '', null); | ||
} else if (fragment) { | ||
el = document.createDocumentFragment(); | ||
el = doc.createDocumentFragment(); | ||
} else { | ||
el = document.createElement('html'); | ||
el = doc.createElement('html'); | ||
} | ||
// Transform children | ||
var childOptions = Object.assign({ fragment: fragment, namespace: namespace }, options); | ||
for (var _i = 0; _i < childrenLength; _i += 1) { | ||
var childEl = transform(children[_i], childOptions); | ||
if (childEl) { | ||
el.appendChild(childEl); | ||
} | ||
} | ||
return appendAll(el, children, Object.assign({ | ||
fragment: fragment, | ||
namespace: namespace | ||
}, options)); | ||
} // Create a `doctype`. | ||
return el; | ||
} | ||
/** | ||
* Transform a DOCTYPE | ||
*/ | ||
function doctype(node) { | ||
return document.implementation.createDocumentType(node.name || 'html', node.public || '', node.system || ''); | ||
} | ||
function doctype(node, _ref) { | ||
var doc = _ref.doc; | ||
return doc.implementation.createDocumentType(node.name || 'html', node["public"] || '', node.system || ''); | ||
} // Create a `text`. | ||
/** | ||
* Transform text node | ||
*/ | ||
function text(node) { | ||
return document.createTextNode(node.value); | ||
} | ||
/** | ||
* Transform a comment node | ||
*/ | ||
function comment(node) { | ||
return document.createComment(node.value); | ||
} | ||
function text(node, _ref2) { | ||
var doc = _ref2.doc; | ||
return doc.createTextNode(node.value); | ||
} // Create a `comment`. | ||
/** | ||
* Transform an element | ||
*/ | ||
function element(node) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var namespace = options.namespace; | ||
var tagName = node.tagName, | ||
properties = node.properties, | ||
function comment(node, _ref3) { | ||
var doc = _ref3.doc; | ||
return doc.createComment(node.value); | ||
} // Create an `element`. | ||
function element(node, options) { | ||
var namespace = options.namespace, | ||
doc = options.doc; // TODO: use `g` in SVG space. | ||
var _node$tagName = node.tagName, | ||
tagName = _node$tagName === void 0 ? 'div' : _node$tagName, | ||
_node$properties = node.properties, | ||
properties = _node$properties === void 0 ? {} : _node$properties, | ||
_node$children2 = node.children, | ||
children = _node$children2 === undefined ? [] : _node$children2; | ||
children = _node$children2 === void 0 ? [] : _node$children2; | ||
var el = typeof namespace !== 'undefined' ? doc.createElementNS(namespace, tagName) : doc.createElement(tagName); // Add HTML attributes. | ||
var el = typeof namespace !== 'undefined' ? document.createElementNS(namespace, tagName) : document.createElement(tagName); | ||
// Add HTML attributes | ||
var props = Object.keys(properties); | ||
@@ -133,63 +147,48 @@ var length = props.length; | ||
var _ref = info.find(info.html, key) || { | ||
attribute: key, | ||
property: key | ||
}, | ||
attribute = _ref.attribute, | ||
property = _ref.property, | ||
mustUseAttribute = _ref.mustUseAttribute, | ||
mustUseProperty = _ref.mustUseProperty, | ||
boolean = _ref.boolean, | ||
booleanish = _ref.booleanish, | ||
overloadedBoolean = _ref.overloadedBoolean, | ||
commaSeparated = _ref.commaSeparated, | ||
spaceSeparated = _ref.spaceSeparated; | ||
var _info$find = info.find(info.html, key), | ||
attribute = _info$find.attribute, | ||
property = _info$find.property, | ||
mustUseProperty = _info$find.mustUseProperty, | ||
_boolean = _info$find["boolean"], | ||
booleanish = _info$find.booleanish, | ||
overloadedBoolean = _info$find.overloadedBoolean, | ||
commaSeparated = _info$find.commaSeparated; | ||
var value = properties[key]; | ||
if (Array.isArray(value)) { | ||
if (commaSeparated) { | ||
value = value.join(', '); | ||
} else if (spaceSeparated) { | ||
value = value.join(' '); | ||
} else { | ||
value = value.join(' '); | ||
} | ||
value = value.join(commaSeparated ? ', ' : ' '); | ||
} | ||
try { | ||
if (mustUseProperty) { | ||
el[property] = value; | ||
} | ||
if (boolean || overloadedBoolean && typeof value === 'boolean') { | ||
if (value) { | ||
el.setAttribute(attribute, ''); | ||
} else { | ||
el.removeAttribute(attribute); | ||
} | ||
} else if (booleanish) { | ||
el.setAttribute(attribute, value); | ||
} else if (value === true) { | ||
if (mustUseProperty) { | ||
el[property] = value; | ||
} | ||
if (_boolean || overloadedBoolean && typeof value === 'boolean') { | ||
if (value) { | ||
el.setAttribute(attribute, ''); | ||
} else if (value || value === 0 || value === '') { | ||
el.setAttribute(attribute, value); | ||
} else { | ||
el.removeAttribute(attribute); | ||
} | ||
} catch (e) { | ||
if (!mustUseAttribute && property) { | ||
el[property] = value; | ||
} | ||
// Otherwise silently ignore | ||
} else if (booleanish) { | ||
el.setAttribute(attribute, value); | ||
} else if (value === true) { | ||
el.setAttribute(attribute, ''); | ||
} else if (value || value === 0 || value === '') { | ||
el.setAttribute(attribute, value); | ||
} | ||
} | ||
// Transform children | ||
return appendAll(el, children, options); | ||
} // Add all children. | ||
function appendAll(node, children, options) { | ||
var childrenLength = children.length; | ||
for (var _i2 = 0; _i2 < childrenLength; _i2 += 1) { | ||
var childEl = transform(children[_i2], options); | ||
if (childEl) { | ||
el.appendChild(childEl); | ||
} | ||
for (var i = 0; i < childrenLength; i += 1) { | ||
node.appendChild(transform(children[i], options)); | ||
} | ||
return el; | ||
return node; | ||
} | ||
@@ -199,6 +198,7 @@ | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
return transform(hast, options); | ||
return transform(hast, _objectSpread({}, options, { | ||
doc: options.document || document | ||
})); | ||
} | ||
module.exports = toDOM; |
113
package.json
{ | ||
"name": "hast-util-to-dom", | ||
"version": "1.1.0", | ||
"description": "Transform HAST to DOM", | ||
"version": "2.0.0", | ||
"description": "hast utility to transform to the DOM", | ||
"license": "ISC", | ||
"keywords": [ | ||
"unist", | ||
"hast", | ||
"util", | ||
"utility", | ||
"rehype", | ||
"dom", | ||
"html" | ||
], | ||
"repository": "syntax-tree/hast-util-to-dom", | ||
"bugs": "https://github.com/syntax-tree/hast-util-to-dom/issues", | ||
"author": "Keith McKnight <keith@mcknig.ht> (https://keith.mcknig.ht)", | ||
"contributors": [ | ||
"Keith McKnight <keith@mcknig.ht> (https://keith.mcknig.ht)", | ||
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)" | ||
], | ||
"main": "dist/hast-util-to-dom.js", | ||
"module": "dist/hast-util-to-dom.mjs", | ||
"repository": "https://github.com/syntax-tree/hast-util-to-dom", | ||
"author": "Keith McKnight <keith@mcknig.ht> (https://keith.mcknig.ht)", | ||
"license": "ISC", | ||
"files": [ | ||
"dist/" | ||
], | ||
"dependencies": { | ||
"property-information": "^5.1.0", | ||
"web-namespaces": "^1.1.3" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.4.5", | ||
"@babel/plugin-external-helpers": "^7.0.0", | ||
"@babel/preset-env": "^7.4.5", | ||
"babel-core": "^7.0.0-bridge.0", | ||
"babel-eslint": "^10.0.0", | ||
"babel-jest": "^24.0.0", | ||
"eslint": "^5.0.0", | ||
"eslint-config-airbnb-base": "^13.0.0", | ||
"eslint-plugin-import": "^2.0.0", | ||
"glob": "^7.0.0", | ||
"hastscript": "^5.0.0", | ||
"jasmine-core": "^3.0.0", | ||
"jest-cli": "^24.0.0", | ||
"karma": "^4.0.0", | ||
"karma-chrome-launcher": "^2.0.0", | ||
"karma-firefox-launcher": "^1.0.0", | ||
"karma-jasmine": "^2.0.0", | ||
"karma-mocha-reporter": "^2.0.0", | ||
"karma-rollup-preprocessor": "^7.0.0", | ||
"karma-safari-launcher": "^1.0.0", | ||
"remark-cli": "^6.0.1", | ||
"remark-preset-wooorm": "^5.0.0", | ||
"rollup": "^1.0.0", | ||
"rollup-plugin-babel": "^4.0.1", | ||
"rollup-plugin-commonjs": "^10.0.0", | ||
"rollup-plugin-json": "^4.0.0", | ||
"rollup-plugin-node-resolve": "^5.0.0", | ||
"w3c-xmlserializer": "^1.1.2" | ||
}, | ||
"scripts": { | ||
"clean": "rm -rf dist", | ||
"build": "rollup -c", | ||
"lint": "eslint .", | ||
"lint": "remark . -qfo && eslint .", | ||
"test": "jest", | ||
@@ -21,35 +72,21 @@ "test:karma": "karma start --single-run --browsers ChromeHeadless,FirefoxHeadless,Safari karma.conf.js", | ||
}, | ||
"dependencies": { | ||
"property-information": "^4.2.0" | ||
"jest": { | ||
"collectCoverage": true, | ||
"coveragePathIgnorePatterns": [ | ||
"/src/utils.js" | ||
], | ||
"coverageThreshold": { | ||
"global": { | ||
"branches": 100, | ||
"functions": 100, | ||
"lines": 100, | ||
"statements": 100 | ||
} | ||
} | ||
}, | ||
"devDependencies": { | ||
"babel-core": "^6.26.3", | ||
"babel-eslint": "^8.2.3", | ||
"babel-jest": "^23.0.1", | ||
"babel-plugin-external-helpers": "^6.22.0", | ||
"babel-preset-env": "^1.7.0", | ||
"babelrc-rollup": "^3.0.0", | ||
"bowser": "^1.9.3", | ||
"eslint": "^4.19.1", | ||
"eslint-config-airbnb-base": "^12.1.0", | ||
"eslint-plugin-import": "^2.12.0", | ||
"glob": "^7.1.2", | ||
"hastscript": "^4.1.0", | ||
"jasmine-core": "^3.1.0", | ||
"jest-cli": "^23.1.0", | ||
"karma": "^2.0.2", | ||
"karma-chrome-launcher": "^2.2.0", | ||
"karma-firefox-launcher": "^1.1.0", | ||
"karma-jasmine": "^1.1.2", | ||
"karma-mocha-reporter": "^2.2.5", | ||
"karma-rollup-preprocessor": "^6.0.0", | ||
"karma-safari-launcher": "^1.0.0", | ||
"rollup": "^0.59.4", | ||
"rollup-plugin-babel": "^3.0.4", | ||
"rollup-plugin-commonjs": "^9.1.3", | ||
"rollup-plugin-node-resolve": "^3.3.0" | ||
}, | ||
"files": [ | ||
"dist" | ||
] | ||
"remarkConfig": { | ||
"plugins": [ | ||
"preset-wooorm" | ||
] | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
18182
161
2
28
306
2
+ Addedweb-namespaces@^1.1.3
+ Addedproperty-information@5.6.0(transitive)
+ Addedweb-namespaces@1.1.4(transitive)
- Removedproperty-information@4.2.0(transitive)
Updatedproperty-information@^5.1.0