posthtml-render
Advanced tools
Comparing version 1.2.3 to 1.3.0
@@ -18,3 +18,3 @@ declare namespace render { | ||
*/ | ||
closingSingleTag: "tag" | "slash"; | ||
closingSingleTag: 'tag' | 'slash'; | ||
@@ -21,0 +21,0 @@ /** |
142
lib/index.js
@@ -1,2 +0,2 @@ | ||
var SINGLE_TAGS = [ | ||
const SINGLE_TAGS = [ | ||
'area', | ||
@@ -19,5 +19,5 @@ 'base', | ||
'wbr' | ||
] | ||
]; | ||
var ATTRIBUTE_QUOTES_REQUIRED = /[\t\n\f\r " '`=<>]/ | ||
const ATTRIBUTE_QUOTES_REQUIRED = /[\t\n\f\r "'`=<>]/; | ||
@@ -30,3 +30,3 @@ /** Render PostHTML Tree to HTML | ||
*/ | ||
function render (tree, options) { | ||
function render(tree, options) { | ||
/** Options | ||
@@ -40,2 +40,3 @@ * | ||
* Otherwise attributes will be unquoted when allowed. | ||
* @prop {Boolean} replaceQuote Replaces quotes in attribute values with `"e;` | ||
* | ||
@@ -46,60 +47,69 @@ * Formats: | ||
*/ | ||
options = options || {} | ||
options = options || {}; | ||
var singleTags = options.singleTags ? SINGLE_TAGS.concat(options.singleTags) : SINGLE_TAGS | ||
var singleRegExp = singleTags.filter(function (tag) { | ||
return tag instanceof RegExp | ||
}) | ||
const singleTags = options.singleTags ? SINGLE_TAGS.concat(options.singleTags) : SINGLE_TAGS; | ||
const singleRegExp = singleTags.filter(tag => { | ||
return tag instanceof RegExp; | ||
}); | ||
var closingSingleTag = options.closingSingleTag | ||
var quoteAllAttributes = options.quoteAllAttributes | ||
if (typeof quoteAllAttributes === 'undefined') { | ||
quoteAllAttributes = true | ||
const {closingSingleTag} = options; | ||
let {quoteAllAttributes} = options; | ||
if (quoteAllAttributes === undefined) { | ||
quoteAllAttributes = true; | ||
} | ||
return html(tree) | ||
let {replaceQuote} = options; | ||
if (replaceQuote === undefined) { | ||
replaceQuote = true; | ||
} | ||
return html(tree); | ||
/** @private */ | ||
function isSingleTag (tag) { | ||
if (singleRegExp.length !== 0) { | ||
for (var i = 0; i < singleRegExp.length; i++) { | ||
return singleRegExp[i].test(tag) | ||
} | ||
function isSingleTag(tag) { | ||
if (singleRegExp.length > 0) { | ||
return singleRegExp.some(reg => reg.test(tag)) | ||
} | ||
if (singleTags.indexOf(tag) === -1) { | ||
return false | ||
if (!singleTags.includes(tag)) { | ||
return false; | ||
} | ||
return true | ||
return true; | ||
} | ||
/** @private */ | ||
function attrs (obj) { | ||
var attr = '' | ||
function attrs(object) { | ||
let attr = ''; | ||
for (var key in obj) { | ||
if (typeof obj[key] === 'string') { | ||
if (quoteAllAttributes || obj[key].match(ATTRIBUTE_QUOTES_REQUIRED)) { | ||
attr += ' ' + key + '="' + obj[key].replace(/"/g, '"') + '"' | ||
} else if (obj[key] === '') { | ||
attr += ' ' + key | ||
for (const key in object) { | ||
if (typeof object[key] === 'string') { | ||
if (quoteAllAttributes || object[key].match(ATTRIBUTE_QUOTES_REQUIRED)) { | ||
let attrValue = object[key]; | ||
if (replaceQuote) { | ||
attrValue = object[key].replace(/"/g, '"'); | ||
} | ||
attr += ' ' + key + '="' + attrValue + '"'; | ||
} else if (object[key] === '') { | ||
attr += ' ' + key; | ||
} else { | ||
attr += ' ' + key + '=' + obj[key] | ||
attr += ' ' + key + '=' + object[key]; | ||
} | ||
} else if (obj[key] === true) { | ||
attr += ' ' + key | ||
} else if (typeof obj[key] === 'number') { | ||
attr += ' ' + key + '="' + obj[key] + '"' | ||
} else if (object[key] === true) { | ||
attr += ' ' + key; | ||
} else if (typeof object[key] === 'number') { | ||
attr += ' ' + key + '="' + object[key] + '"'; | ||
} | ||
} | ||
return attr | ||
return attr; | ||
} | ||
/** @private */ | ||
function traverse (tree, cb) { | ||
function traverse(tree, cb) { | ||
if (tree !== undefined) { | ||
for (var i = 0, length = tree.length; i < length; i++) { | ||
traverse(cb(tree[i]), cb) | ||
for (let i = 0, {length} = tree; i < length; i++) { | ||
traverse(cb(tree[i]), cb); | ||
} | ||
@@ -116,11 +126,11 @@ } | ||
*/ | ||
function html (tree) { | ||
var result = '' | ||
function html(tree) { | ||
let result = ''; | ||
if (!Array.isArray(tree)) { | ||
tree = [tree] | ||
tree = [tree]; | ||
} | ||
traverse(tree, function (node) { | ||
// undefined, null, '', [], NaN | ||
traverse(tree, node => { | ||
// Undefined, null, '', [], NaN | ||
if (node === undefined || | ||
@@ -131,31 +141,31 @@ node === null || | ||
Number.isNaN(node)) { | ||
return | ||
return; | ||
} | ||
// treat as new root tree if node is an array | ||
// Treat as new root tree if node is an array | ||
if (Array.isArray(node)) { | ||
result += html(node) | ||
result += html(node); | ||
return | ||
return; | ||
} | ||
if (typeof node === 'string' || typeof node === 'number') { | ||
result += node | ||
result += node; | ||
return | ||
return; | ||
} | ||
// skip node | ||
// Skip node | ||
if (node.tag === false) { | ||
result += html(node.content) | ||
result += html(node.content); | ||
return | ||
return; | ||
} | ||
var tag = node.tag || 'div' | ||
const tag = node.tag || 'div'; | ||
result += '<' + tag | ||
result += '<' + tag; | ||
if (node.attrs) { | ||
result += attrs(node.attrs) | ||
result += attrs(node.attrs); | ||
} | ||
@@ -166,20 +176,20 @@ | ||
case 'tag': | ||
result += '></' + tag + '>' | ||
result += '></' + tag + '>'; | ||
break | ||
break; | ||
case 'slash': | ||
result += ' />' | ||
result += ' />'; | ||
break | ||
break; | ||
default: | ||
result += '>' | ||
result += '>'; | ||
} | ||
result += html(node.content) | ||
result += html(node.content); | ||
} else { | ||
result += '>' + html(node.content) + '</' + tag + '>' | ||
result += '>' + html(node.content) + '</' + tag + '>'; | ||
} | ||
}) | ||
}); | ||
return result | ||
return result; | ||
} | ||
@@ -194,2 +204,2 @@ } | ||
*/ | ||
module.exports = render | ||
module.exports = render; |
{ | ||
"name": "posthtml-render", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"description": "Renders PostHTML Tree to HTML/XML", | ||
"license": "MIT", | ||
"repository": "posthtml/posthtml-render", | ||
"author": "Ivan Voischev <voischev.ivan@ya.ru>", | ||
"license": "MIT", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=10" | ||
}, | ||
"scripts": { | ||
"version": "conventional-changelog -i changelog.md -s -r 0 && git add changelog.md", | ||
"test": "c8 mocha", | ||
"docs": "jsdoc2md lib/*.js > render.md", | ||
"pretest": "clinton" | ||
}, | ||
"files": [ | ||
"lib" | ||
], | ||
"devDependencies": { | ||
"chai": "^4.0.0", | ||
"coveralls": "^3.0.9", | ||
"jsdoc-to-markdown": "^6.0.1", | ||
"mocha": "^8.0.1", | ||
"nyc": "^15.0.0", | ||
"standard": "^14.3.1", | ||
"standard-version": "^8.0.1" | ||
}, | ||
"scripts": { | ||
"lint": "standard --env mocha", | ||
"test": "npm run lint && nyc mocha", | ||
"coverage": "nyc report --reporter=text-lcov | coveralls", | ||
"docs": "jsdoc2md lib/*.js > RENDER.md", | ||
"release": "standard-version" | ||
}, | ||
"keywords": [ | ||
@@ -37,5 +27,16 @@ "posthtml", | ||
], | ||
"bugs": "https://github.com/posthtml/posthtml-render/issues", | ||
"homepage": "https://github.com/posthtml/posthtml-render#readme", | ||
"repository": "https://github.com/posthtml/posthtml-render.git" | ||
"devDependencies": { | ||
"@commitlint/cli": "^11.0.0", | ||
"@commitlint/config-angular": "^11.0.0", | ||
"c8": "^7.3.5", | ||
"chai": "^4.2.0", | ||
"clinton": "^0.14.0", | ||
"conventional-changelog-cli": "^2.1.1", | ||
"husky": "^4.3.0", | ||
"jsdoc-to-markdown": "^6.0.1", | ||
"lint-staged": "^10.5.1", | ||
"mocha": "^8.2.1", | ||
"xo": "^0.34.2" | ||
}, | ||
"types": "lib/index.d.ts" | ||
} |
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
No website
QualityPackage does not have a website.
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 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
22501
211
220
11
1