• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Jxck/html2json: html2json & json2html

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

Jxck/html2json

开源软件地址:

https://github.com/Jxck/html2json

开源编程语言:

JavaScript 86.3%

开源软件介绍:

html2json & json2html

How to use

browser

include htmlparser.js & html2json.js scripts:

<script src="https://cdn.rawgit.com/blowsie/Pure-JavaScript-HTML5-Parser/master/htmlparser.js"></script>
<script src="src/html2json.js"></script>

node

$ npm install html2json
var html2json = require('html2json').html2json;
var json2html = require('html2json').json2html;

API

json === html2json(document.body.innerHTML);
html === json2html(json);

console.assert(json === html);

JSON format

every json has node

members of node are

  • root
  • element
  • text
  • comment

root node is the root of JSON, every JSON must have only one root root, could have child.

element node represents html element, could have tag, attr, child.

text node represents text element, could have text.

comment node represents commment element, could have text.

Sample

html:

<div id="1" class="foo">
<h2>sample text with <code>inline tag</code></h2>
<pre id="demo" class="foo bar">foo</pre>
<pre id="output" class="goo">goo</pre>
<input id="execute" type="button" value="execute"/>
</div>

json:

{
  node: 'root',
  child: [
    {
      node: 'element',
      tag: 'div',
      attr: { id: '1', class: 'foo' },
      child: [
        {
          node: 'element',
          tag: 'h2',
          child: [
            { node: 'text', text: 'sample text with ' },
            { node: 'element', tag: 'code', child: [{ node: 'text', text: 'inline tag' }] }
          ]
        },
        {
          node: 'element',
          tag: 'pre',
          attr: { id: 'demo', class: ['foo', 'bar'] },
          child: [{ node: 'text', text: 'foo' }]
        },
        {
          node: 'element',
          tag: 'pre',
          attr: { id: 'output', class: 'goo' },
          child: [{ node: 'text', text: 'goo' }]
        },
        {
          node: 'element',
          tag: 'input',
          attr: { id: 'execute', type: 'button', value: 'execute' }
        }
      ]
    }
  ]
}

Dependencies

htmlparser.js

repositry includes this at lib/ as git subtree.

CHANGELOG

before 1.0.0

  • Basically inline tag is melted into text
  • Input, textarea, image tags are act like block tag

License

MIT




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap