npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

html2obj

v1.0.10

Published

pure js,html to obj,obj to html

Downloads

22

Readme

html2obj

html 解析, html 转 json 对象,json 对象转 html

使用方式

npm install -S html2obj

输入的html

<!DOCTYPE html>
<html lang="en">
    <head>
        <title id='title-id'>测试标题</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="static/css/bootstrap.min.css">
        <link rel="stylesheet" href="static/css/jquery-confirm.min.css">
        <link rel="stylesheet" type="text/css" href="style.css">

        <script src="static/js/jquery-3.2.1.min.js"></script>
        <style>
            .CodeMirror{
                height: 100%;
                width: 100%;
            }
        </style>
    </head>
    <body role="document" style="background-color: #2c3e50;">
    <h1>Heading</h1>
    <hr>
    <h2>&inr;</h2>
        <pre id='pre-id'>
            <h1>Heading</h1>
            <hr>
            <h2>&inr;</h2>
        </pre>
        <script>
          let highlightedLine = null;
          let editor;
            <!-- this should not be parsed separately -->
          function updateLength(){
              const xmlData = editor.getValue();
              $("#lengthxml")[0].innerText = xmlData.replace(/>\s*</g, "><").length;
          }
        </script>
    </body>
</html>

js 代码可参考 example/parse.example.js

var dom = new Html2Obj().parseHtml(html)[0]
console.log(JSON.stringify(dom))

输出结果

{
    "tagName": "html",
    "child": [
        {
            "tagName": "head",
            "child": [
                {
                    "tagName": "title",
                    "child": [
                        {
                            "tagName": "#text",
                            "child": [
                                "测试标题"
                            ],
                            "attrs": {}
                        }
                    ],
                    "attrs": {
                        "id": "title-id"
                    }
                },
                {
                    "tagName": "meta",
                    "child": [],
                    "attrs": {
                        "charset": "UTF-8"
                    }
                },
                {
                    "tagName": "meta",
                    "child": [],
                    "attrs": {
                        "name": "viewport",
                        "content": "width=device-width, initial-scale=1"
                    }
                },
                {
                    "tagName": "link",
                    "child": [],
                    "attrs": {
                        "rel": "stylesheet",
                        "href": "static/css/bootstrap.min.css"
                    }
                },
                {
                    "tagName": "link",
                    "child": [],
                    "attrs": {
                        "rel": "stylesheet",
                        "href": "static/css/jquery-confirm.min.css"
                    }
                },
                {
                    "tagName": "link",
                    "child": [],
                    "attrs": {
                        "rel": "stylesheet",
                        "type": "text/css",
                        "href": "style.css"
                    }
                },
                {
                    "tagName": "script",
                    "child": [],
                    "attrs": {
                        "src": "static/js/jquery-3.2.1.min.js"
                    }
                },
                {
                    "tagName": "style",
                    "child": [
                        {
                            "tagName": "#text",
                            "child": [
                                "\n                    .CodeMirror{\n                        height: 100%;\n                        width: 100%;\n                    }\n                "
                            ],
                            "attrs": {}
                        }
                    ],
                    "attrs": {}
                }
            ],
            "attrs": {}
        },
        {
            "tagName": "body",
            "child": [
                {
                    "tagName": "h1",
                    "child": [
                        {
                            "tagName": "#text",
                            "child": [
                                "Heading"
                            ],
                            "attrs": {}
                        }
                    ],
                    "attrs": {}
                },
                {
                    "tagName": "hr",
                    "child": [],
                    "attrs": {}
                },
                {
                    "tagName": "h2",
                    "child": [
                        {
                            "tagName": "#text",
                            "child": [
                                "&inr;"
                            ],
                            "attrs": {}
                        }
                    ],
                    "attrs": {}
                },
                {
                    "tagName": "pre",
                    "child": [
                        {
                            "tagName": "#text",
                            "child": [
                                "\n                    <h1>Heading</h1>\n                    <hr>\n                    <h2>&inr;</h2>\n                "
                            ],
                            "attrs": {}
                        }
                    ],
                    "attrs": {
                        "id": "pre-id"
                    }
                },
                {
                    "tagName": "script",
                    "child": [
                        {
                            "tagName": "#text",
                            "child": [
                                "\n                  let highlightedLine = null;\n                  let editor;\n                    <!-- this should not be parsed separately -->\n                  function updateLength(){\n                      const xmlData = editor.getValue();\n                      $(\"#lengthxml\")[0].innerText = xmlData.replace(/>s*</g, \"><\").length;\n                  }\n                "
                            ],
                            "attrs": {}
                        }
                    ],
                    "attrs": {}
                }
            ],
            "attrs": {
                "role": "document",
                "style": "background-color: #2c3e50;"
            }
        }
    ],
    "attrs": {
        "lang": "en"
    }
}

目前支持根据属性id查找节点

console.log(dom.extractById('title-id').toText())
console.log(dom.extractById('pre-id').toHtml())
  • toText 返回节点内的文本内容
  • toHtml 将节点还原成 html

节点结构

{
  "tagName": "div", 
  "attrs": {},	
  "chid": []
}
  • tagName:标签名
  • attrs:属性对象
  • chid:子节点数组

文本节点 tagName 为 #text

高级用法

var dom = new Html2Obj({
    tagValueProcessor: function (tagName, val) {
        return val;
    },
    attributeValueProcessor: function (attrName, val) {
        return val;
    },
    nodePostProcessor(node) {
        if(node.tagName === 'h1'){
            node.tagName = 'h2'
        }
    }
}).parseHtml(html)[0]
  • tagValueProcessor 标签中如果有文本节点,会将文本节点内容(val) 经过此方法处理
  • attributeValueProcessor 标签的属性处理时会调用此方法
  • nodePostProcessor 标签节点创建后调用。注意:此时还未解析子节点,所以 child 是空,只有 tagName 和 attrs