htmlstr-parser
Version:
Simple HTML to JSON parser use Regexp and String.indexOf
2 lines (1 loc) • 3.31 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.htmlParser=t()}(this,function(){"use strict";function e(e){return e.split(",").reduce(function(e,t){return e[t]=!0,e},{})}function t(e){return!!l[e]}function n(e){return!!p[e]}function r(e){var t=e.name,n=e.attributes;return e instanceof g?{type:_,tagName:t,attributes:n}:{type:_,tagName:t,attributes:n,children:[]}}function i(e){var t=e.text;return{type:x,content:t}}function o(e,t){switch(e){case _:return r(t);case x:return i(t)}}function a(e){var t={tag:"root",children:[]},n=[t];n.last=function(){return n[n.length-1]};for(var r=0;r<e.length;r++){var i=e[r];if(i instanceof y){var a=o(_,i);a.children?n.push(a):n.last().children.push(a)}else if(i instanceof v){var u=n[n.length-2],s=n.pop();u.children.push(s)}else i instanceof w&&n.last().children.push(o(x,i))}return t}function u(e){for(var n=e,r=[],i=Date.now()+1e3;n;)if(0!==n.indexOf("\x3c!--"))if(0!==n.indexOf("</"))if(0!==n.indexOf("<")){var o=n.indexOf("<"),a=o<0?n:n.substring(0,o);if(n=o<0?"":n.substring(o),r.push(new w(a)),Date.now()>=i)break}else{var u=n.match(c);if(!u)continue;n=n.substring(u[0].length);var s=u[1],l=u[2],p=t(s)?new g(s,l):new y(s,l);r.push(p)}else{var h=n.match(f);if(!h)continue;n=n.substring(h[0].length);var b=h[1];if(t(b))continue;r.push(new v(b))}else{var d=n.indexOf("--\x3e")+3;n=n.substring(d)}return r}function s(e){return a(u(e))}var c=/^<([-A-Za-z0-9_]+)((?:\s+[a-zA-Z_:][-a-zA-Z0-9_:.]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/,f=/^<\/([-A-Za-z0-9_]+)[^>]*>/,l=e("area,base,basefont,br,col,frame,hr,img,input,link,meta,param,embed,command,keygen,source,track,wbr"),p=e("checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected"),h=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},b=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),d=function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)},m=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t},y=function(){function e(t,n){h(this,e),this.name=t,this.attributes=this.getAttributes(n)}return b(e,[{key:"getAttributes",value:function(e){var t={};return e.replace(/([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g,function(e,r){var i=Array.prototype.slice.call(arguments),o=i[2]?i[2]:i[3]?i[3]:i[4]?i[4]:n(r)?r:"";t[r]=o.replace(/(^|[^\\])"/g,'$1\\"')}),t}}]),e}(),g=function(e){function t(e,n){return h(this,t),m(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e,n))}return d(t,e),t}(y),v=function e(t){h(this,e),this.name=t},w=function e(t){h(this,e),this.text=t},_="Element",x="Text";return s});