Socket
Socket
Sign inDemoInstall

posthtml-parser

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

posthtml-parser - npm Package Compare versions

Comparing version 0.1.3 to 0.2.0

MAINTAINERS

58

index.js

@@ -1,10 +0,18 @@

/*jshint -W082 */
'use strict';
var htmlparser = require('htmlparser2');
var isObject = require('isobject');
/**
* @see https://github.com/fb55/htmlparser2/wiki/Parser-options
*/
var defaultOptions = {lowerCaseTags: false};
/**
* Parse html to PostHTMLTree
* @param {String} html
* @return {Object}
* @param {Object} [options=defaultOptions]
* @return {PostHTMLTree}
*/
module.exports = function postHTMLParser(html) {
function postHTMLParser(html, options) {
var bufArray = [],

@@ -19,3 +27,5 @@ results = [];

onprocessinginstruction: function(name, data) {
name.toLowerCase() === '!doctype' && results.push('<' + data + '>');
if (name.toLowerCase() === '!doctype') {
results.push('<' + data + '>');
}
},

@@ -35,8 +45,8 @@ oncomment: function(data) {

onopentag: function(tag, attrs) {
var buf = {};
var buf = { tag: tag };
buf.tag = tag;
if (Object.keys(attrs).length) {
buf.attrs = attrs;
}
if (!isEmpty(attrs)) buf.attrs = attrs;
bufArray.push(buf);

@@ -47,3 +57,3 @@ },

if (bufArray.length === 0) {
if (!bufArray.length) {
results.push(buf);

@@ -54,5 +64,6 @@ return;

var last = bufArray.last();
if (!(last.content instanceof Array)) {
if (!Array.isArray(last.content)) {
last.content = [];
}
last.content.push(buf);

@@ -70,3 +81,3 @@ },

}
}, {lowerCaseTags: false});
}, options || defaultOptions);

@@ -77,11 +88,22 @@ parser.write(html);

return results;
};
}
function isEmpty(obj) {
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
return false;
}
function parserWrapper() {
var option;
function parser(html) {
var opt = option || defaultOptions;
return postHTMLParser(html, opt);
}
return true;
if (arguments.length === 1 && isObject(arguments[0])) {
option = arguments[0];
return parser;
}
option = arguments[1];
return parser(arguments[0]);
}
module.exports = parserWrapper;
module.exports.defaultOptions = defaultOptions;
{
"name": "posthtml-parser",
"version": "0.1.3",
"version": "0.2.0",
"description": "Parse HTML/XML to PostHTMLTree",

@@ -20,6 +20,3 @@ "keywords": [

},
"repository": {
"type": "git",
"url": "git+https://github.com/posthtml/posthtml-parser.git"
},
"repository": "posthtml/posthtml-parser",
"author": "Ivan Voischev <voischev.ivan@ya.ru>",

@@ -32,3 +29,4 @@ "license": "MIT",

"dependencies": {
"htmlparser2": "^3.8.3"
"htmlparser2": "^3.8.3",
"isobject": "^2.1.0"
},

@@ -40,4 +38,7 @@ "devDependencies": {

"jshint": "^2.8.0",
"mocha": "^2.3.3"
"mocha": "^2.3.3",
"rewire": "^2.5.2",
"sinon": "^1.17.4",
"sinon-chai": "^2.8.0"
}
}

@@ -6,3 +6,3 @@ # posthtml-parser

Parse HTML/XML to [PostHTMLTree](https://github.com/posthtml/posthtml#posthtml-json-tree-example).
Parse HTML/XML to [PostHTML AST](https://github.com/posthtml/posthtml-parser#posthtml-ast-format).
More about [PostHTML](https://github.com/posthtml/posthtml#readme)

@@ -17,5 +17,5 @@

## Usage
## Usage
#### input HTML
#### Input HTML
```html

@@ -27,7 +27,7 @@ <a class="animals" href="#">

```js
var parser = require('posthtml-parser');
var fs = require('fs');
var html = fs.readFileSync('path/to/input.html').toString();
const parser = require('posthtml-parser')
const fs = require('fs')
const html = fs.readFileSync('path/to/input.html').toString()
clonsole.log(parser(html)); // Look #Result PostHTMLTree
console.log(parser(html)) // Logs a PostHTML AST
```

@@ -65,4 +65,22 @@

## PostHTML AST Format
Any parser being used with PostHTML should return a standard PostHTML [Abstract Syntax Tree](https://www.wikiwand.com/en/Abstract_syntax_tree) (AST). Fortunately, this is a very easy format to produce and understand. The AST is an array that can contain strings and objects. Any strings represent plain text content to be written to the output. Any objects represent HTML tags.
Tag objects generally look something like this:
```js
{
tag: 'div',
attrs: {
class: 'foo'
},
content: ['hello world!']
}
```
Tag objects can contain three keys. The `tag` key takes the name of the tag as the value. This can include custom tags. The optional `attrs` key takes an object with key/value pairs representing the attributes of the html tag. A boolean attribute has an empty string as its value. Finally, the optional `content` key takes an array as its value, which is a PostHTML AST. In this manner, the AST is a tree that should be walked recursively.
## License
[MIT](LICENSE)
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