UNPKG

html2incdom

Version:

Converts html strings into incremental dom calls

59 lines 6.22 kB
/* * HTML5 Parser By Sam Blowes * * Designed for HTML5 documents * * Original code by John Resig (ejohn.org) * http://ejohn.org/blog/pure-javascript-html-parser/ * Original code by Erik Arvidsson, Mozilla Public License * http://erik.eae.net/simplehtmlparser/simplehtmlparser.js * * ---------------------------------------------------------------------------- * License * ---------------------------------------------------------------------------- * * This code is triple licensed using Apache Software License 2.0, * Mozilla Public License or GNU Public License * * //////////////////////////////////////////////////////////////////////////// * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * //////////////////////////////////////////////////////////////////////////// * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations * under the License. * * The Original Code is Simple HTML Parser. * * The Initial Developer of the Original Code is Erik Arvidsson. * Portions created by Erik Arvidssson are Copyright (C) 2004. All Rights * Reserved. * * //////////////////////////////////////////////////////////////////////////// * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * @license */ (function(){function e(e){for(var t={},n=e.split(","),r=0;r<n.length;r++)t[n[r]]=!0;return t}var t=/^<([-A-Za-z0-9_]+)((?:\s+[a-zA-Z_:][-a-zA-Z0-9_:.]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/,n=/^<\/([-A-Za-z0-9_]+)[^>]*>/,r=/([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g,a=e("area,base,basefont,br,col,frame,hr,img,input,link,meta,param,embed,command,keygen,source,track,wbr"),s=e("a,address,article,applet,aside,audio,blockquote,button,canvas,center,dd,del,dir,div,dl,dt,fieldset,figcaption,figure,footer,form,frameset,h1,h2,h3,h4,h5,h6,header,hgroup,hr,iframe,ins,isindex,li,map,menu,noframes,noscript,object,ol,output,p,pre,section,script,table,tbody,td,tfoot,th,thead,tr,ul,video"),i=e("abbr,acronym,applet,b,basefont,bdo,big,br,button,cite,code,del,dfn,em,font,i,iframe,img,input,ins,kbd,label,map,object,q,s,samp,script,select,small,span,strike,strong,sub,sup,textarea,tt,u,var"),o=e("colgroup,dd,dt,li,options,p,td,tfoot,th,thead,tr"),u=e("checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected"),l=e("script,style");window.HTMLParser=function(e,c){function f(e,t,n,l){if(t=t.toLowerCase(),s[t])for(;g.last()&&i[g.last()]&&"span"!==g.last();)d("",g.last());if(o[t]&&g.last()==t&&d("",t),l=a[t]||!!l,l||g.push(t),c.start){var f=[];n.replace(r,function(e,t){var n=arguments[2]?arguments[2]:arguments[3]?arguments[3]:arguments[4]?arguments[4]:u[t]?t:"";f.push({name:t,value:n,escaped:n.replace(/(^|[^\\])"/g,'$1\\"')})}),c.start&&c.start(t,f,l)}}function d(e,t){if(t)for(var n=g.length-1;n>=0&&g[n]!=t;n--);else var n=0;if(n>=0){for(var r=g.length-1;r>=n;r--)c.end&&c.end(g[r]);g.length=n}}var p,m,h,g=[],b=e;for(g.last=function(){return this[this.length-1]};e;){if(m=!0,g.last()&&l[g.last()])e=e.replace(new RegExp("([\\s\\S]*?)</"+g.last()+"[^>]*>"),function(e,t){return t=t.replace(/<!--([\s\S]*?)-->|<!\[CDATA\[([\s\S]*?)]]>/g,"$1$2"),c.chars&&c.chars(t),""}),d("",g.last());else if(0==e.indexOf("<!--")?(p=e.indexOf("-->"),p>=0&&(c.comment&&c.comment(e.substring(4,p)),e=e.substring(p+3),m=!1)):0==e.indexOf("</")?(h=e.match(n),h&&(e=e.substring(h[0].length),h[0].replace(n,d),m=!1)):0==e.indexOf("<")&&(h=e.match(t),h&&(e=e.substring(h[0].length),h[0].replace(t,f),m=!1)),m){p=e.indexOf("<");var v=0>p?e:e.substring(0,p);e=0>p?"":e.substring(p),c.chars&&c.chars(v)}if(e==b)throw"Parse Error: "+e;b=e}d()}}).call(this),function(e,t){if("function"==typeof define&&define.amd)define(["exports","./unescape"],t);else if("undefined"!=typeof exports)t(exports,require("./unescape"));else{var n={exports:{}};t(n.exports,e.unescape),e.HTML2IncDom=n.exports}}(this,function(e,t){"use strict";function n(e){return e&&e.__esModule?e:{"default":e}}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var a,s=n(t),i=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}}(),o=function(){function e(){r(this,e)}return i(e,null,[{key:"buildFn",value:function(t){return function(){return e.run(t)}}},{key:"getParser",value:function(){return a||window.HTMLParser}},{key:"run",value:function(t){e.getParser()(t,{start:function(e,t,n){for(var r=n?IncrementalDOM.elementVoid:IncrementalDOM.elementOpen,a=[e,null,[]],s=0;s<t.length;s++)a.push(t[s].name,t[s].value);r.apply(null,a)},end:function(e){IncrementalDOM.elementClose(e)},chars:function(e){IncrementalDOM.text(e,s["default"])}})}},{key:"setParser",value:function(e){a=e}}]),e}();e["default"]=o});