html-parser.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * HTML5 Parser By Sam Blowes
  3. *
  4. * Designed for HTML5 documents
  5. *
  6. * Original code by John Resig (ejohn.org)
  7. * http://ejohn.org/blog/pure-javascript-html-parser/
  8. * Original code by Erik Arvidsson, Mozilla Public License
  9. * http://erik.eae.net/simplehtmlparser/simplehtmlparser.js
  10. *
  11. * ----------------------------------------------------------------------------
  12. * License
  13. * ----------------------------------------------------------------------------
  14. *
  15. * This code is triple licensed using Apache Software License 2.0,
  16. * Mozilla Public License or GNU Public License
  17. *
  18. * ////////////////////////////////////////////////////////////////////////////
  19. *
  20. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  21. * use this file except in compliance with the License. You may obtain a copy
  22. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  23. *
  24. * ////////////////////////////////////////////////////////////////////////////
  25. *
  26. * The contents of this file are subject to the Mozilla Public License
  27. * Version 1.1 (the "License"); you may not use this file except in
  28. * compliance with the License. You may obtain a copy of the License at
  29. * http://www.mozilla.org/MPL/
  30. *
  31. * Software distributed under the License is distributed on an "AS IS"
  32. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  33. * License for the specific language governing rights and limitations
  34. * under the License.
  35. *
  36. * The Original Code is Simple HTML Parser.
  37. *
  38. * The Initial Developer of the Original Code is Erik Arvidsson.
  39. * Portions created by Erik Arvidssson are Copyright (C) 2004. All Rights
  40. * Reserved.
  41. *
  42. * ////////////////////////////////////////////////////////////////////////////
  43. *
  44. * This program is free software; you can redistribute it and/or
  45. * modify it under the terms of the GNU General Public License
  46. * as published by the Free Software Foundation; either version 2
  47. * of the License, or (at your option) any later version.
  48. *
  49. * This program is distributed in the hope that it will be useful,
  50. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  51. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  52. * GNU General Public License for more details.
  53. *
  54. * You should have received a copy of the GNU General Public License
  55. * along with this program; if not, write to the Free Software
  56. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  57. *
  58. * ----------------------------------------------------------------------------
  59. * Usage
  60. * ----------------------------------------------------------------------------
  61. *
  62. * // Use like so:
  63. * HTMLParser(htmlString, {
  64. * start: function(tag, attrs, unary) {},
  65. * end: function(tag) {},
  66. * chars: function(text) {},
  67. * comment: function(text) {}
  68. * });
  69. *
  70. * // or to get an XML string:
  71. * HTMLtoXML(htmlString);
  72. *
  73. * // or to get an XML DOM Document
  74. * HTMLtoDOM(htmlString);
  75. *
  76. * // or to inject into an existing document/DOM node
  77. * HTMLtoDOM(htmlString, document);
  78. * HTMLtoDOM(htmlString, document.body);
  79. *
  80. */
  81. // Regular Expressions for parsing tags and attributes
  82. var startTag = /^<([-A-Za-z0-9_]+)((?:\s+[a-zA-Z_:][-a-zA-Z0-9_:.]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/;
  83. var endTag = /^<\/([-A-Za-z0-9_]+)[^>]*>/;
  84. var attr = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g; // Empty Elements - HTML 5
  85. var empty = makeMap('area,base,basefont,br,col,frame,hr,img,input,link,meta,param,embed,command,keygen,source,track,wbr'); // Block Elements - HTML 5
  86. // fixed by xxx 将 ins 标签从块级名单中移除
  87. var block = makeMap('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,isindex,li,map,menu,noframes,noscript,object,ol,output,p,pre,section,script,table,tbody,td,tfoot,th,thead,tr,ul,video'); // Inline Elements - HTML 5
  88. var inline = makeMap('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'); // Elements that you can, intentionally, leave open
  89. // (and which close themselves)
  90. var closeSelf = makeMap('colgroup,dd,dt,li,options,p,td,tfoot,th,thead,tr'); // Attributes that have their values filled in disabled="disabled"
  91. var fillAttrs = makeMap('checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected'); // Special Elements (can contain anything)
  92. var special = makeMap('script,style');
  93. function HTMLParser(html, handler) {
  94. var index;
  95. var chars;
  96. var match;
  97. var stack = [];
  98. var last = html;
  99. stack.last = function () {
  100. return this[this.length - 1];
  101. };
  102. while (html) {
  103. chars = true; // Make sure we're not in a script or style element
  104. if (!stack.last() || !special[stack.last()]) {
  105. // Comment
  106. if (html.indexOf('<!--') == 0) {
  107. index = html.indexOf('-->');
  108. if (index >= 0) {
  109. if (handler.comment) {
  110. handler.comment(html.substring(4, index));
  111. }
  112. html = html.substring(index + 3);
  113. chars = false;
  114. } // end tag
  115. } else if (html.indexOf('</') == 0) {
  116. match = html.match(endTag);
  117. if (match) {
  118. html = html.substring(match[0].length);
  119. match[0].replace(endTag, parseEndTag);
  120. chars = false;
  121. } // start tag
  122. } else if (html.indexOf('<') == 0) {
  123. match = html.match(startTag);
  124. if (match) {
  125. html = html.substring(match[0].length);
  126. match[0].replace(startTag, parseStartTag);
  127. chars = false;
  128. }
  129. }
  130. if (chars) {
  131. index = html.indexOf('<');
  132. var text = index < 0 ? html : html.substring(0, index);
  133. html = index < 0 ? '' : html.substring(index);
  134. if (handler.chars) {
  135. handler.chars(text);
  136. }
  137. }
  138. } else {
  139. html = html.replace(new RegExp('([\\s\\S]*?)<\/' + stack.last() + '[^>]*>'), function (all, text) {
  140. text = text.replace(/<!--([\s\S]*?)-->|<!\[CDATA\[([\s\S]*?)]]>/g, '$1$2');
  141. if (handler.chars) {
  142. handler.chars(text);
  143. }
  144. return '';
  145. });
  146. parseEndTag('', stack.last());
  147. }
  148. if (html == last) {
  149. throw 'Parse Error: ' + html;
  150. }
  151. last = html;
  152. } // Clean up any remaining tags
  153. parseEndTag();
  154. function parseStartTag(tag, tagName, rest, unary) {
  155. tagName = tagName.toLowerCase();
  156. if (block[tagName]) {
  157. while (stack.last() && inline[stack.last()]) {
  158. parseEndTag('', stack.last());
  159. }
  160. }
  161. if (closeSelf[tagName] && stack.last() == tagName) {
  162. parseEndTag('', tagName);
  163. }
  164. unary = empty[tagName] || !!unary;
  165. if (!unary) {
  166. stack.push(tagName);
  167. }
  168. if (handler.start) {
  169. var attrs = [];
  170. rest.replace(attr, function (match, name) {
  171. var value = arguments[2] ? arguments[2] : arguments[3] ? arguments[3] : arguments[4] ? arguments[4] : fillAttrs[name] ? name : '';
  172. attrs.push({
  173. name: name,
  174. value: value,
  175. escaped: value.replace(/(^|[^\\])"/g, '$1\\\"') // "
  176. });
  177. });
  178. if (handler.start) {
  179. handler.start(tagName, attrs, unary);
  180. }
  181. }
  182. }
  183. function parseEndTag(tag, tagName) {
  184. // If no tag name is provided, clean shop
  185. if (!tagName) {
  186. var pos = 0;
  187. } // Find the closest opened tag of the same type
  188. else {
  189. for (var pos = stack.length - 1; pos >= 0; pos--) {
  190. if (stack[pos] == tagName) {
  191. break;
  192. }
  193. }
  194. }
  195. if (pos >= 0) {
  196. // Close all the open elements, up the stack
  197. for (var i = stack.length - 1; i >= pos; i--) {
  198. if (handler.end) {
  199. handler.end(stack[i]);
  200. }
  201. } // Remove the open elements from the stack
  202. stack.length = pos;
  203. }
  204. }
  205. }
  206. function makeMap(str) {
  207. var obj = {};
  208. var items = str.split(',');
  209. for (var i = 0; i < items.length; i++) {
  210. obj[items[i]] = true;
  211. }
  212. return obj;
  213. }
  214. function removeDOCTYPE(html) {
  215. return html.replace(/<\?xml.*\?>\n/, '').replace(/<!doctype.*>\n/, '').replace(/<!DOCTYPE.*>\n/, '');
  216. }
  217. function parseAttrs(attrs) {
  218. return attrs.reduce(function (pre, attr) {
  219. var value = attr.value;
  220. var name = attr.name;
  221. if (pre[name]) {
  222. pre[name] = pre[name] + " " + value;
  223. } else {
  224. pre[name] = value;
  225. }
  226. return pre;
  227. }, {});
  228. }
  229. function parseHtml(html) {
  230. html = removeDOCTYPE(html);
  231. var stacks = [];
  232. var results = {
  233. node: 'root',
  234. children: []
  235. };
  236. HTMLParser(html, {
  237. start: function start(tag, attrs, unary) {
  238. var node = {
  239. name: tag
  240. };
  241. if (attrs.length !== 0) {
  242. node.attrs = parseAttrs(attrs);
  243. }
  244. if (unary) {
  245. var parent = stacks[0] || results;
  246. if (!parent.children) {
  247. parent.children = [];
  248. }
  249. parent.children.push(node);
  250. } else {
  251. stacks.unshift(node);
  252. }
  253. },
  254. end: function end(tag) {
  255. var node = stacks.shift();
  256. if (node.name !== tag) console.error('invalid state: mismatch end tag');
  257. if (stacks.length === 0) {
  258. results.children.push(node);
  259. } else {
  260. var parent = stacks[0];
  261. if (!parent.children) {
  262. parent.children = [];
  263. }
  264. parent.children.push(node);
  265. }
  266. },
  267. chars: function chars(text) {
  268. var node = {
  269. type: 'text',
  270. text: text
  271. };
  272. if (stacks.length === 0) {
  273. results.children.push(node);
  274. } else {
  275. var parent = stacks[0];
  276. if (!parent.children) {
  277. parent.children = [];
  278. }
  279. parent.children.push(node);
  280. }
  281. },
  282. comment: function comment(text) {
  283. var node = {
  284. node: 'comment',
  285. text: text
  286. };
  287. var parent = stacks[0];
  288. if (!parent.children) {
  289. parent.children = [];
  290. }
  291. parent.children.push(node);
  292. }
  293. });
  294. return results.children;
  295. }
  296. export default parseHtml;