posthtml-render
Advanced tools
Comparing version 1.1.5 to 1.2.0
@@ -5,2 +5,4 @@ # Changelog | ||
## [1.2.0](https://github.com/posthtml/posthtml-render/compare/v1.1.5...v1.2.0) (2020-02-25) | ||
### [1.1.5](https://github.com/posthtml/posthtml-render/compare/v1.1.4...v1.1.5) (2019-05-06) | ||
@@ -7,0 +9,0 @@ |
161
lib/index.js
@@ -25,7 +25,7 @@ var SINGLE_TAGS = [ | ||
/** | ||
* Render PostHTML Tree to HTML | ||
var ATTRIBUTE_QUOTES_REQUIRED = /[\t\n\f\r " '`=<>]/ | ||
/** Render PostHTML Tree to HTML | ||
* | ||
* @param {Array|Object} tree PostHTML Tree | ||
* @param {Object} options Options | ||
* @param {Array|Object} tree PostHTML Tree @param {Object} options Options | ||
* | ||
@@ -35,4 +35,3 @@ * @return {String} HTML | ||
function render (tree, options) { | ||
/** | ||
* Options | ||
/** Options | ||
* | ||
@@ -42,3 +41,5 @@ * @type {Object} | ||
* @prop {Array<String|RegExp>} singleTags Custom single tags (selfClosing) | ||
* @prop {String} closingSingleTag Closing format for single tag | ||
* @prop {String} closingSingleTag Closing format for single tag @prop | ||
* @prop {Boolean} quoteAllAttributes If all attributes should be quoted. | ||
* Otherwise attributes will be unquoted when allowed. | ||
* | ||
@@ -51,11 +52,62 @@ * Formats: | ||
var singleTags = SINGLE_TAGS.concat(options.singleTags || []) | ||
var singleTags = options.singleTags ? SINGLE_TAGS.concat(options.singleTags) : SINGLE_TAGS | ||
var singleRegExp = singleTags.filter(function (tag) { | ||
return tag instanceof RegExp ? tag : false | ||
return tag instanceof RegExp | ||
}) | ||
var closingSingleTag = options.closingSingleTag | ||
var quoteAllAttributes = options.quoteAllAttributes | ||
if (typeof quoteAllAttributes === 'undefined') { | ||
quoteAllAttributes = 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) | ||
} | ||
} | ||
if (singleTags.indexOf(tag) === -1) { | ||
return false | ||
} | ||
return true | ||
} | ||
/** @private */ | ||
function attrs (obj) { | ||
var 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 | ||
} else { | ||
attr += ' ' + key + '=' + obj[key] | ||
} | ||
} else if (obj[key] === true) { | ||
attr += ' ' + key | ||
} else if (typeof obj[key] === 'number') { | ||
attr += ' ' + key + '="' + obj[key] + '"' | ||
} | ||
} | ||
return attr | ||
} | ||
/** @private */ | ||
function traverse (tree, cb) { | ||
if (tree !== undefined) { | ||
for (var i = 0, length = tree.length; i < length; i++) { | ||
traverse(cb(tree[i]), cb) | ||
} | ||
} | ||
} | ||
/** | ||
@@ -71,20 +123,32 @@ * HTML Stringifier | ||
traverse([].concat(tree), function (node) { | ||
if (!node) return | ||
if (!Array.isArray(tree)) { | ||
tree = [tree] | ||
} | ||
if (typeof node === 'string' || typeof node === 'number') { | ||
result += node | ||
traverse(tree, function (node) { | ||
// undefined, null, '', [], NaN | ||
if (node === undefined || | ||
node === null || | ||
node === false || | ||
node.length === 0 || | ||
Number.isNaN(node)) { | ||
return | ||
} | ||
// treat as new root tree if node is an array | ||
if (Array.isArray(node)) { | ||
result += html(node) | ||
return | ||
} | ||
if (typeof node.tag === 'boolean' && !node.tag) { | ||
typeof node.content !== 'object' && (result += node.content) | ||
if (typeof node === 'string' || typeof node === 'number') { | ||
result += node | ||
return node.content | ||
return | ||
} | ||
// treat as new root tree if node is an array | ||
if (Array.isArray(node)) { | ||
result += html(node) | ||
// skip node | ||
if (node.tag === false) { | ||
result += html(node.content) | ||
@@ -96,5 +160,9 @@ return | ||
if (isSingleTag(tag, singleTags, singleRegExp)) { | ||
result += '<' + tag + attrs(node.attrs) | ||
result += '<' + tag | ||
if (node.attrs) { | ||
result += attrs(node.attrs) | ||
} | ||
if (isSingleTag(tag)) { | ||
switch (closingSingleTag) { | ||
@@ -113,5 +181,5 @@ case 'tag': | ||
result += node.content ? html(node.content) : '' | ||
result += html(node.content) | ||
} else { | ||
result += '<' + tag + (node.attrs ? attrs(node.attrs) : '') + '>' + (node.content ? html(node.content) : '') + '</' + tag + '>' | ||
result += '>' + html(node.content) + '</' + tag + '>' | ||
} | ||
@@ -127,50 +195,5 @@ }) | ||
* | ||
* @version 1.0.7 | ||
* @version 1.1.5 | ||
* @license MIT | ||
*/ | ||
module.exports = render | ||
/** @private */ | ||
function attrs (obj) { | ||
var attr = '' | ||
for (var key in obj) { | ||
if (typeof obj[key] === 'boolean' && obj[key]) { | ||
attr += ' ' + key | ||
} else if (typeof obj[key] === 'number') { | ||
attr += ' ' + key + '="' + obj[key] + '"' | ||
} else if (typeof obj[key] === 'string') { | ||
attr += ' ' + key + '="' + obj[key].replace(/"/g, '"') + '"' | ||
} | ||
} | ||
return attr | ||
} | ||
/** @private */ | ||
function traverse (tree, cb) { | ||
if (Array.isArray(tree)) { | ||
for (var i = 0, length = tree.length; i < length; i++) { | ||
traverse(cb(tree[i]), cb) | ||
} | ||
} else if (typeof tree === 'object' && tree.hasOwnProperty('content')) { | ||
traverse(tree.content, cb) | ||
} | ||
return tree | ||
} | ||
/** @private */ | ||
function isSingleTag (tag, singleTags, singleRegExp) { | ||
if (singleRegExp.length) { | ||
for (var i = 0; i < singleRegExp.length; i++) { | ||
return !!tag.match(singleRegExp[i]) | ||
} | ||
} | ||
if (singleTags.indexOf(tag) === -1) { | ||
return false | ||
} | ||
return true | ||
} |
{ | ||
"name": "posthtml-render", | ||
"version": "1.1.5", | ||
"version": "1.2.0", | ||
"description": "Renders PostHTML Tree to HTML/XML", | ||
@@ -16,8 +16,8 @@ "author": "Ivan Voischev <voischev.ivan@ya.ru>", | ||
"chai": "^4.0.0", | ||
"coveralls": "^3.0.3", | ||
"jsdoc-to-markdown": "^4.0.1", | ||
"mocha": "^6.1.4", | ||
"nyc": "^14.1.0", | ||
"standard": "^12.0.1", | ||
"standard-version": "^6.0.1" | ||
"coveralls": "^3.0.9", | ||
"jsdoc-to-markdown": "^5.0.3", | ||
"mocha": "^7.0.1", | ||
"nyc": "^15.0.0", | ||
"standard": "^14.3.1", | ||
"standard-version": "^7.1.0" | ||
}, | ||
@@ -24,0 +24,0 @@ "scripts": { |
[![npm][npm]][npm-url] | ||
[![node][node]][node-url] | ||
[![node]]() | ||
[![deps][deps]][deps-url] | ||
@@ -90,2 +90,3 @@ [![tests][tests]][tests-url] | ||
|**[`closingSingleTag`](#closingSingleTag)**|`{String}`|[`>`](#default)|Specify the single tag closing format| | ||
|**[`quoteAllAttributes`](#quoteAllAttributes)**|`{Boolean}`|[`true`](#default)|Put double quotes around all tags, even when not necessary.| | ||
@@ -170,3 +171,18 @@ ### `singleTags` | ||
### `quoteAllAttributes` | ||
Specify if all attributes should be quoted. | ||
##### `true (Default)` | ||
```html | ||
<script src="index.js"></script> | ||
``` | ||
##### `false` | ||
```html | ||
<script src=index.js></script> | ||
``` | ||
[npm]: https://img.shields.io/npm/v/posthtml-render.svg | ||
@@ -173,0 +189,0 @@ [npm-url]: https://npmjs.com/package/posthtml-render |
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
11530
161
203