Skip to content

Commit

Permalink
Merge 04dd3d9 into 0f41560
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Jun 28, 2019
2 parents 0f41560 + 04dd3d9 commit f4c95d4
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .huskyrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"hooks": {
"commit-msg": "commitlint -e $HUSKY_GIT_PARAMS",
"pre-commit": "lint-staged"
"pre-commit": "npm test && lint-staged"
}
}
2 changes: 1 addition & 1 deletion .lintstagedrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"npm run lint:fix",
"git add"
],
"*.{md,yml}": [
"*.{html,json,md,yml}": [
"prettier --write",
"git add"
]
Expand Down
5 changes: 0 additions & 5 deletions .npmignore

This file was deleted.

20 changes: 10 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
language: node_js
node_js:
- stable
- stable
cache:
directories:
- node_modules
directories:
- node_modules
install:
- npm install
- npm install
script:
- npm run lint
- npm run dtslint
- npm run cover
after_script:
- npm run coveralls
- npm run lint
- npm run dtslint
- npm run test:coverage
after_success:
- npm run coveralls
notifications:
email: false
email: false
114 changes: 90 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,40 @@
[![Coverage Status](https://coveralls.io/repos/github/remarkablemark/html-dom-parser/badge.svg?branch=master)](https://coveralls.io/github/remarkablemark/html-dom-parser?branch=master)
[![Dependency status](https://david-dm.org/remarkablemark/html-dom-parser.svg)](https://david-dm.org/remarkablemark/html-dom-parser)

An HTML to DOM parser that works on both the server and the browser:
HTML to DOM parser that works on both the server (Node.js) and the client (browser):

```
HTMLDOMParser(string[, options])
```

The parser converts an HTML string to a JavaScript object that describes the DOM tree.
It converts an HTML string to a JavaScript object that describes the DOM tree.

[repl.it](https://repl.it/@remarkablemark/html-dom-parser) | [JSFiddle](https://jsfiddle.net/remarkablemark/ff9yg1yz/)
#### Example:

```js
var parse = require('html-dom-parser');
parse('<div>text</div>');
```

Output:

```js
[
{
type: 'tag',
name: 'div',
attribs: {},
children: [
{ data: 'text', type: 'text', next: null, prev: null, parent: [Circular] }
],
next: null,
prev: null,
parent: null
}
];
```

[Repl.it](https://repl.it/@remarkablemark/html-dom-parser) | [JSFiddle](https://jsfiddle.net/remarkablemark/ff9yg1yz/) | [Examples](https://github.com/remarkablemark/html-dom-parser/tree/master/examples)

## Installation

Expand All @@ -31,7 +56,7 @@ $ npm install html-dom-parser --save
$ yarn add html-dom-parser
```

[unpkg](https://unpkg.com/html-dom-parser/) (CDN):
[CDN](https://unpkg.com/html-dom-parser/):

```html
<script src="https://unpkg.com/html-dom-parser@latest/dist/html-dom-parser.js"></script>
Expand All @@ -42,51 +67,92 @@ $ yarn add html-dom-parser

## Usage

Import parser:
Import the module:

```js
// server
var parser = require('html-dom-parser');
// CommonJS
var parse = require('html-dom-parser');

// client
var parser = window.HTMLDOMParser;
// ES Modules
import parse from 'html-dom-parser';
```

Parse input:
Parse markup:

```js
parser('<p>Hello, world!</p>');
parse('<p class="primary" style="color: skyblue;">Hello world</p>');
```

Get output:
Output:

```js
[ { type: 'tag',
[
{
type: 'tag',
name: 'p',
attribs: {},
children:
[ { data: 'Hello, world!',
type: 'text',
next: null,
prev: null,
parent: [Circular] } ],
attribs: { class: 'primary', style: 'color: skyblue;' },
children: [
{
data: 'Hello world',
type: 'text',
next: null,
prev: null,
parent: [Circular]
}
],
next: null,
prev: null,
parent: null } ]
parent: null
}
];
```

On the server-side (Node.js), the parser is a wrapper of `parseDOM` from [htmlparser2](https://github.com/fb55/htmlparser2).
The **server parser** is a wrapper of [htmlparser2](https://github.com/fb55/htmlparser2)'s `parseDOM`.

On the client-side (browser), the parser uses the [DOM](https://developer.mozilla.org/docs/Web/API/Document_Object_Model/Introduction) API to mimic the output schema of the server parser.
The **client parser** mimics the server parser using the [DOM](https://developer.mozilla.org/docs/Web/API/Document_Object_Model/Introduction) API.

## Testing

Run tests:

```sh
$ npm test
$ npm run lint # npm run lint:fix
$ npm run dtslint
```

Run tests with coverage:

```sh
$ npm run test:coverage
```

Lint files:

```sh
$ npm run lint
$ npm run dtslint
```

Fix lint errors:

```sh
$ npm run lint:fix
```

## Release

Only collaborators with credentials can release and publish:

```sh
$ npm run release
$ git push --follow-tags && npm publish
```

## Special Thanks

- [Contributors](https://github.com/remarkablemark/html-dom-parser/graphs/contributors)
- [htmlparser2](https://github.com/fb55/htmlparser2)

## License

[MIT](https://github.com/remarkablemark/html-dom-parser/blob/master/LICENSE)
35 changes: 35 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<textarea cols="50" rows="5"><div>text</div></textarea>
<pre><code></code></pre>
<script src="../dist/html-dom-parser.min.js"></script>
<script>
var code = document.querySelector('code');
var textarea = document.querySelector('textarea');

function renderOutput(event) {
cache = [];
code.innerText = JSON.stringify(
window.HTMLDOMParser(event.target.value),
replacer,
2
);
}

textarea.addEventListener('input', renderOutput);
renderOutput({ target: textarea });

/**
* @see {@link https://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json}
*/
var cache;
function replacer(key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// duplicate reference found
return '[Circular]';
}
// store value in our collection
cache.push(value);
}
return value;
}
</script>
46 changes: 27 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
"name": "html-dom-parser",
"version": "0.2.2",
"description": "An HTML to DOM parser that works on the server and client.",
"description": "HTML to DOM parser.",
"author": "Mark <mark@remarkablemark.org>",
"main": "index.js",
"scripts": {
"build": "npm run build:min && npm run build:unmin",
"build:min": "webpack index.js dist/html-dom-parser.min.js -p --output-library HTMLDOMParser --output-library-target umd",
"build:unmin": "webpack index.js dist/html-dom-parser.js --output-library HTMLDOMParser --output-library-target umd",
"build:min": "webpack index.js -o dist/html-dom-parser.min.js --mode production --output-library HTMLDOMParser --output-library-target umd",
"build:unmin": "webpack index.js -o dist/html-dom-parser.js --mode development --output-library HTMLDOMParser --output-library-target umd",
"clean": "rm -rf dist",
"test": "mocha",
"coveralls": "cat coverage/lcov.info | coveralls",
"dtslint": "dtslint .",
"lint": "eslint . --ignore-path .gitignore",
"lint:fix": "npm run lint -- --fix",
"dtslint": "dtslint .",
"cover": "istanbul cover _mocha -- -R spec",
"coveralls": "cat coverage/lcov.info | coveralls",
"prepublish": "npm run clean && npm run build",
"release": "standard-version --no-verify"
"prepublishOnly": "npm run clean && npm run build",
"release": "standard-version --no-verify",
"test": "mocha",
"test:coverage": "istanbul cover _mocha -- -R spec"
},
"repository": {
"type": "git",
Expand All @@ -26,34 +26,42 @@
"url": "https://github.com/remarkablemark/html-dom-parser/issues"
},
"keywords": [
"html-dom-parser",
"html",
"dom",
"parser",
"htmlparser2"
"htmlparser2",
"pojo"
],
"dependencies": {
"@types/domhandler": "2.4.1",
"domhandler": "2.3.0",
"htmlparser2": "3.9.1"
"domhandler": "2.4.2",
"htmlparser2": "3.10.1"
},
"devDependencies": {
"@commitlint/cli": "^8.0.0",
"@commitlint/config-conventional": "^8.0.0",
"chai": "^3.5.0",
"coveralls": "^2.11.14",
"dtslint": "^0.5.9",
"chai": "^4.2.0",
"coveralls": "^3.0.4",
"dtslint": "^0.8.0",
"eslint": "^5.16.0",
"eslint-plugin-prettier": "^3.1.0",
"html-minifier": "^3.1.0",
"html-minifier": "^4.0.0",
"husky": "^2.4.1",
"istanbul": "^0.4.5",
"jsdomify": "^3.1.0",
"lint-staged": "^8.2.0",
"mocha": "^3.4.2",
"mocha": "^6.1.4",
"prettier": "^1.18.2",
"standard-version": "^5.0.2",
"webpack": "^2.6.1"
"standard-version": "^6.0.1",
"webpack": "^4.34.0",
"webpack-cli": "^3.3.4"
},
"files": [
"/dist",
"/index.d.ts",
"/lib"
],
"browser": {
"./index.js": "./lib/html-to-dom-client.js"
},
Expand Down

0 comments on commit f4c95d4

Please sign in to comment.