项目作者: dogma-io

项目描述 :
HTML parser and compiler
高级语言: JavaScript
项目地址: git://github.com/dogma-io/dogma-html-parser.git
创建时间: 2017-09-11T14:13:55Z
项目社区:https://github.com/dogma-io/dogma-html-parser

开源协议:

下载


dogma-html-parser

HTML parser and compiler.

Installation

  1. npm install dogma-html-parser

Usage

Parser

To parse an HTML string into an AST you can do the following:

  1. import {parse} from 'dogma-html-parser'
  2. const html = '<div class="test">Foo bar</div>'
  3. const ast = parse(html)

The above example will set the constant ast to the following AST:

  1. {
  2. "attributes": {
  3. "class": "test"
  4. },
  5. "children": [
  6. {
  7. "text": "Foo bar",
  8. "type": "text"
  9. }
  10. ],
  11. "name": "div",
  12. "type": "element"
  13. }

It is worth noting the parser tries to be forgiving in terms of invalid
whitespace, and by default will strip out unnecessary whitespace that doesn’t
actually get rendered by browsers. For example if you parse then compile the
following:

  1. < div > Test < /div >

You’ll end up with:

  1. <div>Test</div>

Compiler

The compiler simply takes an AST and converts it to an HTML string like so:

  1. import {compile} from 'dogma-html-parser'
  2. const ast = {
  3. attributes: {
  4. 'class': 'test',
  5. },
  6. children: [
  7. {
  8. text: 'Foo bar',
  9. type: 'text',
  10. },
  11. ],
  12. name: 'div',
  13. type: 'element',
  14. }
  15. const html = compile(ast)

The above example will set the constant html to the following HTML string:

  1. <div class="test">Foo bar</div>