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

posthtml

Package Overview
Dependencies
Maintainers
4
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

posthtml - npm Package Compare versions

Comparing version 0.13.3 to 0.13.4

20

lib/api.js

@@ -83,4 +83,4 @@ 'use strict'

return Array.isArray(expression)
? traverse(this, function (node) {
for (var i = 0; i < expression.length; i++) {
? traverse(this, node => {
for (let i = 0; i < expression.length; i++) {
if (compare(expression[i], node)) return cb(node)

@@ -91,3 +91,3 @@ }

})
: traverse(this, function (node) {
: traverse(this, node => {
if (compare(expression, node)) return cb(node)

@@ -106,3 +106,3 @@

if (Array.isArray(tree)) {
for (var i = 0; i < tree.length; i++) {
for (let i = 0; i < tree.length; i++) {
tree[i] = traverse(cb(tree[i]), cb)

@@ -132,12 +132,8 @@ }

if (Array.isArray(expected)) {
return expected.every(function (exp) {
return [].some.call(actual, function (act) {
return compare(exp, act)
})
})
return expected.every(exp => [].some.call(actual, act => compare(exp, act)))
}
return Object.keys(expected).every(function (key) {
var ao = actual[key]
var eo = expected[key]
return Object.keys(expected).every(key => {
const ao = actual[key]
const eo = expected[key]

@@ -144,0 +140,0 @@ if (typeof eo === 'object' && eo !== null && ao !== null) {

385

lib/index.js

@@ -1,6 +0,6 @@

var pkg = require('../package.json')
var Api = require('./api.js')
const pkg = require('../package.json')
const Api = require('./api.js')
var parser = require('posthtml-parser')
var render = require('posthtml-render')
let parser = require('posthtml-parser')
let render = require('posthtml-render')

@@ -18,225 +18,226 @@ /**

*/
function PostHTML (plugins) {
/**
* PostHTML Instance
*
* @prop plugins
* @prop options
*/
this.version = pkg.version
this.name = pkg.name
this.plugins = typeof plugins === 'function' ? [plugins] : plugins || []
this.source = ''
class PostHTML {
constructor (plugins) {
/**
* Tree messages to store and pass metadata between plugins
* PostHTML Instance
*
* @memberof tree
* @type {Array} messages
*
* @example
* ```js
* export default function plugin (options = {}) {
* return function (tree) {
* tree.messages.push({
* type: 'dependency',
* file: 'path/to/dependency.html',
* from: tree.options.from
* })
*
* return tree
* }
* }
* ```
* @prop plugins
* @prop options
*/
this.messages = []
this.version = pkg.version
this.name = pkg.name
this.plugins = typeof plugins === 'function' ? [plugins] : plugins || []
this.source = ''
/**
* Tree messages to store and pass metadata between plugins
*
* @memberof tree
* @type {Array} messages
*
* @example
* ```js
* export default function plugin (options = {}) {
* return function (tree) {
* tree.messages.push({
* type: 'dependency',
* file: 'path/to/dependency.html',
* from: tree.options.from
* })
*
* return tree
* }
* }
* ```
*/
this.messages = []
/**
* Tree method parsing string inside plugins.
*
* @memberof tree
* @type {Function} parser
*
* @example
* ```js
* export default function plugin (options = {}) {
* return function (tree) {
* tree.match({ tag: 'include' }, function(node) {
* node.tag = false;
* node.content = tree.parser(fs.readFileSync(node.attr.src))
* return node
* })
*
* return tree
* }
* }
* ```
*/
this.parser = parser
/**
* Tree method rendering tree to string inside plugins.
*
* @memberof tree
* @type {Function} render
*
* @example
* ```js
* export default function plugin (options = {}) {
* return function (tree) {
* var outherTree = ['\n', {tag: 'div', content: ['1']}, '\n\t', {tag: 'div', content: ['2']}, '\n'];
* var htmlWitchoutSpaceless = tree.render(outherTree).replace(/[\n|\t]/g, '');
* return tree.parser(htmlWitchoutSpaceless)
* }
* }
* ```
*/
this.render = render
// extend api methods
Api.call(this)
}
/**
* Tree method parsing string inside plugins.
* @this posthtml
* @param {Function} plugin - A PostHTML plugin
* @returns {Constructor} - this(PostHTML)
*
* **Usage**
* ```js
* ph.use((tree) => { tag: 'div', content: tree })
* .process('<html>..</html>', {})
* .then((result) => result))
* ```
*/
use (...args) {
this.plugins.push(...args)
return this
}
/**
* @param {String} html - Input (HTML)
* @param {?Object} options - PostHTML Options
* @returns {Object<{html: String, tree: PostHTMLTree}>} - Sync Mode
* @returns {Promise<{html: String, tree: PostHTMLTree}>} - Async Mode (default)
*
* @memberof tree
* @type {Function} parser
* **Usage**
*
* @example
* **Sync**
* ```js
* export default function plugin (options = {}) {
* return function (tree) {
* tree.match({ tag: 'include' }, function(node) {
* node.tag = false;
* node.content = tree.parser(fs.readFileSync(node.attr.src))
* return node
* })
*
* return tree
* }
* }
* ph.process('<html>..</html>', { sync: true }).html
* ```
*/
this.parser = parser
/**
* Tree method rendering tree to string inside plugins.
*
* @memberof tree
* @type {Function} render
*
* @example
* **Async**
* ```js
* export default function plugin (options = {}) {
* return function (tree) {
* var outherTree = ['\n', {tag: 'div', content: ['1']}, '\n\t', {tag: 'div', content: ['2']}, '\n'];
* var htmlWitchoutSpaceless = tree.render(outherTree).replace(/[\n|\t]/g, '');
* return tree.parser(htmlWitchoutSpaceless)
* }
* }
* ph.process('<html>..</html>', {}).then((result) => result))
* ```
*/
this.render = render
process (tree, options = {}) {
/**
* ## PostHTML Options
*
* @type {Object}
* @prop {?Boolean} options.sync - enables sync mode, plugins will run synchronously, throws an error when used with async plugins
* @prop {?Function} options.parser - use custom parser, replaces default (posthtml-parser)
* @prop {?Function} options.render - use custom render, replaces default (posthtml-render)
* @prop {?Boolean} options.skipParse - disable parsing
* @prop {?Array} options.directives - Adds processing of custom [directives](https://github.com/posthtml/posthtml-parser#directives).
*/
this.options = options
this.source = tree
// extend api methods
Api.call(this)
}
if (options.parser) parser = this.parser = options.parser
if (options.render) render = this.render = options.render
/**
* @this posthtml
* @param {Function} plugin - A PostHTML plugin
* @returns {Constructor} - this(PostHTML)
*
* **Usage**
* ```js
* ph.use((tree) => { tag: 'div', content: tree })
* .process('<html>..</html>', {})
* .then((result) => result))
* ```
*/
PostHTML.prototype.use = function () {
[].push.apply(this.plugins, arguments)
return this
}
tree = options.skipParse
? tree || []
: parser(tree, options)
/**
* @param {String} html - Input (HTML)
* @param {?Object} options - PostHTML Options
* @returns {Object<{html: String, tree: PostHTMLTree}>} - Sync Mode
* @returns {Promise<{html: String, tree: PostHTMLTree}>} - Async Mode (default)
*
* **Usage**
*
* **Sync**
* ```js
* ph.process('<html>..</html>', { sync: true }).html
* ```
*
* **Async**
* ```js
* ph.process('<html>..</html>', {}).then((result) => result))
* ```
*/
PostHTML.prototype.process = function (tree, options) {
/**
* ## PostHTML Options
*
* @type {Object}
* @prop {?Boolean} options.sync - enables sync mode, plugins will run synchronously, throws an error when used with async plugins
* @prop {?Function} options.parser - use custom parser, replaces default (posthtml-parser)
* @prop {?Function} options.render - use custom render, replaces default (posthtml-render)
* @prop {?Boolean} options.skipParse - disable parsing
* @prop {?Array} options.directives - Adds processing of custom [directives](https://github.com/posthtml/posthtml-parser#directives).
*/
options = this.options = options || {}
this.source = tree
// sync mode
if (options.sync === true) {
this.plugins.forEach((plugin, index) => {
_treeExtendApi(tree, this)
if (options.parser) parser = this.parser = options.parser
if (options.render) render = this.render = options.render
let result
tree = options.skipParse
? tree || []
: parser(tree, options)
if (plugin.length === 2 || isPromise(result = plugin(tree))) {
throw new Error(
`Can’t process contents in sync mode because of async plugin: ${plugin.name}`
)
}
// sync mode
if (options.sync === true) {
this.plugins.forEach(function (plugin, index) {
_treeExtendApi(tree, this)
// clearing the tree of options
if (index !== this.plugins.length - 1 && !options.skipParse) {
tree = [].concat(tree)
}
var result
// return the previous tree unless result is fulfilled
tree = result || tree
})
if (plugin.length === 2 || isPromise(result = plugin(tree))) {
throw new Error(
'Can’t process contents in sync mode because of async plugin: ' + plugin.name
)
}
return lazyResult(render, tree)
}
// clearing the tree of options
if (index !== this.plugins.length - 1 && !options.skipParse) {
tree = [].concat(tree)
}
// async mode
let i = 0
// return the previous tree unless result is fulfilled
tree = result || tree
}.bind(this))
const next = (result, cb) => {
_treeExtendApi(result, this)
return lazyResult(render, tree)
}
// all plugins called
if (this.plugins.length <= i) {
cb(null, result)
return
}
// async mode
var i = 0
// little helper to go to the next iteration
function _next (res) {
if (res && !options.skipParse) {
res = [].concat(res)
}
var next = function (result, cb) {
_treeExtendApi(result, this)
return next(res || result, cb)
}
// all plugins called
if (this.plugins.length <= i) {
cb(null, result)
return
}
// call next
const plugin = this.plugins[i++]
// little helper to go to the next iteration
function _next (res) {
if (res && !options.skipParse) {
res = [].concat(res)
if (plugin.length === 2) {
plugin(result, (err, res) => {
if (err) return cb(err)
_next(res)
})
return
}
return next(res || result, cb)
}
// sync and promised plugins
let err = null
// call next
var plugin = this.plugins[i++]
if (plugin.length === 2) {
plugin(result, function (err, res) {
if (err) return cb(err)
_next(res)
const res = tryCatch(() => plugin(result), e => {
err = e
return e
})
return
}
// sync and promised plugins
var err = null
if (err) {
cb(err)
return
}
var res = tryCatch(function () {
return plugin(result)
}, function (e) {
err = e
return e
})
if (isPromise(res)) {
res.then(_next).catch(cb)
return
}
if (err) {
cb(err)
return
_next(res)
}
if (isPromise(res)) {
res.then(_next).catch(cb)
return
}
_next(res)
}.bind(this)
return new Promise(function (resolve, reject) {
next(tree, function (err, tree) {
if (err) reject(err)
else resolve(lazyResult(render, tree))
return new Promise((resolve, reject) => {
next(tree, (err, tree) => {
if (err) reject(err)
else resolve(lazyResult(render, tree))
})
})
})
}
}

@@ -258,5 +259,3 @@

*/
module.exports = function (plugins) {
return new PostHTML(plugins)
}
module.exports = plugins => new PostHTML(plugins)

@@ -321,5 +320,5 @@ /**

},
tree: tree,
tree,
messages: tree.messages
}
}
{
"name": "posthtml",
"version": "0.13.3",
"version": "0.13.4",
"description": "HTML/XML processor",

@@ -33,3 +33,2 @@ "keywords": [

"chai-subset": "^1.6.0",
"coveralls": "^3.0.11",
"jsdoc-to-markdown": "^6.0.1",

@@ -44,3 +43,2 @@ "mocha": "^8.1.1",

"test": "npm run lint && nyc mocha",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"docs:api": "jsdoc2md lib/api.js > docs/api.md",

@@ -47,0 +45,0 @@ "docs:core": "jsdoc2md lib/index.js > docs/core.md",

@@ -7,3 +7,2 @@ [![NPM][npm]][npm-url]

[![Twitter][twitter]][twitter-url]
[![Chat][chat]][chat-url]

@@ -36,2 +35,8 @@ # PostHTML <img align="right" width="220" height="200" title="PostHTML" src="http://posthtml.github.io/posthtml/logo.svg">

## Create to your project
```bash
npm init posthtml
```
## Install

@@ -863,7 +868,7 @@

[twitter]: https://img.shields.io/badge/twitter-%40PostHTML-00ACEE.svg?style=flat
[twitter]: https://badgen.net/twitter/follow/posthtml
[twitter-url]: https://twitter.com/PostHTML
[chat]: https://badges.gitter.im/posthtml/posthtml.svg
[chat]: https://badges.gitter.im/posthtml/PostHTML.svg
[chat-url]: https://gitter.im/posthtml/posthtml?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge"
[docs-url]: https://github.com/posthtml/posthtml/tree/master/docs
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