From 1f111831d9ff6f3eef19a858a6b9c1447aaf8f37 Mon Sep 17 00:00:00 2001 From: Ohkubo KOHEI Date: Wed, 26 Jan 2022 15:08:43 +0900 Subject: [PATCH 1/9] rename to mjs --- htmlparser.js => htmlparser.mjs | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename htmlparser.js => htmlparser.mjs (100%) diff --git a/htmlparser.js b/htmlparser.mjs similarity index 100% rename from htmlparser.js rename to htmlparser.mjs From 45efceeae63f18bda293bccb6505d85e147d879b Mon Sep 17 00:00:00 2001 From: Ohkubo KOHEI Date: Wed, 26 Jan 2022 15:08:59 +0900 Subject: [PATCH 2/9] unindent --- htmlparser.mjs | 494 ++++++++++++++++++++++++------------------------- 1 file changed, 246 insertions(+), 248 deletions(-) diff --git a/htmlparser.mjs b/htmlparser.mjs index c6c6d23..e5b1f5a 100644 --- a/htmlparser.mjs +++ b/htmlparser.mjs @@ -79,290 +79,288 @@ * */ -(function () { - - // Regular Expressions for parsing tags and attributes - var startTag = /^<([-A-Za-z0-9_]+)((?:\s+[a-zA-Z_:][-a-zA-Z0-9_:.]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/, - endTag = /^<\/([-A-Za-z0-9_]+)[^>]*>/, - attr = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g; - - // Empty Elements - HTML 5 - 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 - 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,ins,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 - 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 - // (and which close themselves) - var closeSelf = makeMap("colgroup,dd,dt,li,options,p,td,tfoot,th,thead,tr"); - - // Attributes that have their values filled in disabled="disabled" - var fillAttrs = makeMap("checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected"); - - // Special Elements (can contain anything) - var special = makeMap("script,style"); - - var HTMLParser = this.HTMLParser = function (html, handler) { - var index, chars, match, stack = [], last = html; - stack.last = function () { - return this[this.length - 1]; - }; - - while (html) { - chars = true; - - // Make sure we're not in a script or style element - if (!stack.last() || !special[stack.last()]) { - - // Comment - if (html.indexOf(""); - - if (index >= 0) { - if (handler.comment) - handler.comment(html.substring(4, index)); - html = html.substring(index + 3); - chars = false; - } - - // end tag - } else if (html.indexOf("\s]+))?)*)\s*(\/?)>/, + endTag = /^<\/([-A-Za-z0-9_]+)[^>]*>/, + attr = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g; + +// Empty Elements - HTML 5 +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 +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,ins,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 +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 +// (and which close themselves) +var closeSelf = makeMap("colgroup,dd,dt,li,options,p,td,tfoot,th,thead,tr"); + +// Attributes that have their values filled in disabled="disabled" +var fillAttrs = makeMap("checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected"); + +// Special Elements (can contain anything) +var special = makeMap("script,style"); + +var HTMLParser = this.HTMLParser = function (html, handler) { + var index, chars, match, stack = [], last = html; + stack.last = function () { + return this[this.length - 1]; + }; + + while (html) { + chars = true; + + // Make sure we're not in a script or style element + if (!stack.last() || !special[stack.last()]) { + + // Comment + if (html.indexOf(""); + + if (index >= 0) { + if (handler.comment) + handler.comment(html.substring(4, index)); + html = html.substring(index + 3); + chars = false; } - if (chars) { - index = html.indexOf("<"); + // end tag + } else if (html.indexOf("]*>"), function (all, text) { - text = text.replace(/|/g, "$1$2"); - if (handler.chars) - handler.chars(text); + if (chars) { + index = html.indexOf("<"); - return ""; - }); + var text = index < 0 ? html : html.substring(0, index); + html = index < 0 ? "" : html.substring(index); - parseEndTag("", stack.last()); + if (handler.chars) + handler.chars(text); } - if (html == last) - throw "Parse Error: " + html; - last = html; + } else { + html = html.replace(new RegExp("([\\s\\S]*?)<\/" + stack.last() + "[^>]*>"), function (all, text) { + text = text.replace(/|/g, "$1$2"); + if (handler.chars) + handler.chars(text); + + return ""; + }); + + parseEndTag("", stack.last()); } - // Clean up any remaining tags - parseEndTag(); + if (html == last) + throw "Parse Error: " + html; + last = html; + } + + // Clean up any remaining tags + parseEndTag(); - function parseStartTag(tag, tagName, rest, unary) { - tagName = tagName.toLowerCase(); + function parseStartTag(tag, tagName, rest, unary) { + tagName = tagName.toLowerCase(); - if (block[tagName]) { - while (stack.last() && inline[stack.last()]) { - parseEndTag("", stack.last()); - } + if (block[tagName]) { + while (stack.last() && inline[stack.last()]) { + parseEndTag("", stack.last()); } + } - if (closeSelf[tagName] && stack.last() == tagName) { - parseEndTag("", tagName); - } + if (closeSelf[tagName] && stack.last() == tagName) { + parseEndTag("", tagName); + } - unary = empty[tagName] || !!unary; + unary = empty[tagName] || !!unary; - if (!unary) - stack.push(tagName); + if (!unary) + stack.push(tagName); - if (handler.start) { - var attrs = []; + if (handler.start) { + var attrs = []; - rest.replace(attr, function (match, name) { - var value = arguments[2] ? arguments[2] : - arguments[3] ? arguments[3] : - arguments[4] ? arguments[4] : - fillAttrs[name] ? name : ""; + rest.replace(attr, function (match, name) { + var value = arguments[2] ? arguments[2] : + arguments[3] ? arguments[3] : + arguments[4] ? arguments[4] : + fillAttrs[name] ? name : ""; - attrs.push({ - name: name, - value: value, - escaped: value.replace(/(^|[^\\])"/g, '$1\\\"') //" - }); + attrs.push({ + name: name, + value: value, + escaped: value.replace(/(^|[^\\])"/g, '$1\\\"') //" }); + }); - if (handler.start) - handler.start(tagName, attrs, unary); - } + if (handler.start) + handler.start(tagName, attrs, unary); } + } - function parseEndTag(tag, tagName) { - if(tagName!== undefined) tagName = tagName.toLowerCase(); - // If no tag name is provided, clean shop - if (!tagName) - var pos = 0; - - // Find the closest opened tag of the same type - else - for (var pos = stack.length - 1; pos >= 0; pos--) - if (stack[pos] == tagName) - break; - - if (pos >= 0) { - // Close all the open elements, up the stack - for (var i = stack.length - 1; i >= pos; i--) - if (handler.end) - handler.end(stack[i]); - - // Remove the open elements from the stack - stack.length = pos; - } + function parseEndTag(tag, tagName) { + if(tagName!== undefined) tagName = tagName.toLowerCase(); + // If no tag name is provided, clean shop + if (!tagName) + var pos = 0; + + // Find the closest opened tag of the same type + else + for (var pos = stack.length - 1; pos >= 0; pos--) + if (stack[pos] == tagName) + break; + + if (pos >= 0) { + // Close all the open elements, up the stack + for (var i = stack.length - 1; i >= pos; i--) + if (handler.end) + handler.end(stack[i]); + + // Remove the open elements from the stack + stack.length = pos; } - }; + } +}; + +this.HTMLtoXML = function (html) { + var results = ""; + + HTMLParser(html, { + start: function (tag, attrs, unary) { + results += "<" + tag; + + for (var i = 0; i < attrs.length; i++) + results += " " + attrs[i].name + '="' + attrs[i].escaped + '"'; + results += ">"; + }, + end: function (tag) { + results += ""; + }, + chars: function (text) { + results += text; + }, + comment: function (text) { + results += ""; + } + }); - this.HTMLtoXML = function (html) { - var results = ""; - - HTMLParser(html, { - start: function (tag, attrs, unary) { - results += "<" + tag; - - for (var i = 0; i < attrs.length; i++) - results += " " + attrs[i].name + '="' + attrs[i].escaped + '"'; - results += ">"; - }, - end: function (tag) { - results += ""; - }, - chars: function (text) { - results += text; - }, - comment: function (text) { - results += ""; - } - }); + return results; +}; + +this.HTMLtoDOM = function (html, doc) { + // There can be only one of these elements + var one = makeMap("html,head,body,title"); - return results; + // Enforce a structure for the document + var structure = { + link: "head", + base: "head" }; - this.HTMLtoDOM = function (html, doc) { - // There can be only one of these elements - var one = makeMap("html,head,body,title"); - - // Enforce a structure for the document - var structure = { - link: "head", - base: "head" - }; - - if (!doc) { - if (typeof DOMDocument != "undefined") - doc = new DOMDocument(); - else if (typeof document != "undefined" && document.implementation && document.implementation.createDocument) - doc = document.implementation.createDocument("", "", null); - else if (typeof ActiveX != "undefined") - doc = new ActiveXObject("Msxml.DOMDocument"); - - } else - doc = doc.ownerDocument || - doc.getOwnerDocument && doc.getOwnerDocument() || - doc; - - var elems = [], - documentElement = doc.documentElement || - doc.getDocumentElement && doc.getDocumentElement(); - - // If we're dealing with an empty document then we - // need to pre-populate it with the HTML document structure - if (!documentElement && doc.createElement) (function () { - var html = doc.createElement("html"); - var head = doc.createElement("head"); - head.appendChild(doc.createElement("title")); - html.appendChild(head); - html.appendChild(doc.createElement("body")); - doc.appendChild(html); - })(); - - // Find all the unique elements - if (doc.getElementsByTagName) - for (var i in one) - one[i] = doc.getElementsByTagName(i)[0]; - - // If we're working with a document, inject contents into - // the body element - var curParentNode = one.body; - - HTMLParser(html, { - start: function (tagName, attrs, unary) { - // If it's a pre-built element, then we can ignore - // its construction - if (one[tagName]) { - curParentNode = one[tagName]; - if (!unary) { - elems.push(curParentNode); - } - return; + if (!doc) { + if (typeof DOMDocument != "undefined") + doc = new DOMDocument(); + else if (typeof document != "undefined" && document.implementation && document.implementation.createDocument) + doc = document.implementation.createDocument("", "", null); + else if (typeof ActiveX != "undefined") + doc = new ActiveXObject("Msxml.DOMDocument"); + + } else + doc = doc.ownerDocument || + doc.getOwnerDocument && doc.getOwnerDocument() || + doc; + + var elems = [], + documentElement = doc.documentElement || + doc.getDocumentElement && doc.getDocumentElement(); + + // If we're dealing with an empty document then we + // need to pre-populate it with the HTML document structure + if (!documentElement && doc.createElement) (function () { + var html = doc.createElement("html"); + var head = doc.createElement("head"); + head.appendChild(doc.createElement("title")); + html.appendChild(head); + html.appendChild(doc.createElement("body")); + doc.appendChild(html); + })(); + + // Find all the unique elements + if (doc.getElementsByTagName) + for (var i in one) + one[i] = doc.getElementsByTagName(i)[0]; + + // If we're working with a document, inject contents into + // the body element + var curParentNode = one.body; + + HTMLParser(html, { + start: function (tagName, attrs, unary) { + // If it's a pre-built element, then we can ignore + // its construction + if (one[tagName]) { + curParentNode = one[tagName]; + if (!unary) { + elems.push(curParentNode); } + return; + } - var elem = doc.createElement(tagName); + var elem = doc.createElement(tagName); - for (var attr in attrs) - elem.setAttribute(attrs[attr].name, attrs[attr].value); + for (var attr in attrs) + elem.setAttribute(attrs[attr].name, attrs[attr].value); - if (structure[tagName] && typeof one[structure[tagName]] != "boolean") - one[structure[tagName]].appendChild(elem); + if (structure[tagName] && typeof one[structure[tagName]] != "boolean") + one[structure[tagName]].appendChild(elem); - else if (curParentNode && curParentNode.appendChild) - curParentNode.appendChild(elem); + else if (curParentNode && curParentNode.appendChild) + curParentNode.appendChild(elem); - if (!unary) { - elems.push(elem); - curParentNode = elem; - } - }, - end: function (tag) { - elems.length -= 1; - - // Init the new parentNode - curParentNode = elems[elems.length - 1]; - }, - chars: function (text) { - curParentNode.appendChild(doc.createTextNode(text)); - }, - comment: function (text) { - // create comment node + if (!unary) { + elems.push(elem); + curParentNode = elem; } - }); + }, + end: function (tag) { + elems.length -= 1; + + // Init the new parentNode + curParentNode = elems[elems.length - 1]; + }, + chars: function (text) { + curParentNode.appendChild(doc.createTextNode(text)); + }, + comment: function (text) { + // create comment node + } + }); - return doc; - }; + return doc; +}; - function makeMap(str) { - var obj = {}, items = str.split(","); - for (var i = 0; i < items.length; i++) - obj[items[i]] = true; - return obj; - } -})(); +function makeMap(str) { + var obj = {}, items = str.split(","); + for (var i = 0; i < items.length; i++) + obj[items[i]] = true; + return obj; +} From 43fe1f43337c8663379e661b3cb969b415f74afb Mon Sep 17 00:00:00 2001 From: Ohkubo KOHEI Date: Wed, 26 Jan 2022 15:09:56 +0900 Subject: [PATCH 3/9] var -> const --- htmlparser.mjs | 56 +++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/htmlparser.mjs b/htmlparser.mjs index e5b1f5a..ed273db 100644 --- a/htmlparser.mjs +++ b/htmlparser.mjs @@ -81,31 +81,31 @@ // Regular Expressions for parsing tags and attributes -var startTag = /^<([-A-Za-z0-9_]+)((?:\s+[a-zA-Z_:][-a-zA-Z0-9_:.]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/, +const startTag = /^<([-A-Za-z0-9_]+)((?:\s+[a-zA-Z_:][-a-zA-Z0-9_:.]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/, endTag = /^<\/([-A-Za-z0-9_]+)[^>]*>/, attr = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g; // Empty Elements - HTML 5 -var empty = makeMap("area,base,basefont,br,col,frame,hr,img,input,link,meta,param,embed,command,keygen,source,track,wbr"); +const empty = makeMap("area,base,basefont,br,col,frame,hr,img,input,link,meta,param,embed,command,keygen,source,track,wbr"); // Block Elements - HTML 5 -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,ins,isindex,li,map,menu,noframes,noscript,object,ol,output,p,pre,section,script,table,tbody,td,tfoot,th,thead,tr,ul,video"); +const 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,ins,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 -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"); +const 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 // (and which close themselves) -var closeSelf = makeMap("colgroup,dd,dt,li,options,p,td,tfoot,th,thead,tr"); +const closeSelf = makeMap("colgroup,dd,dt,li,options,p,td,tfoot,th,thead,tr"); // Attributes that have their values filled in disabled="disabled" -var fillAttrs = makeMap("checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected"); +const fillAttrs = makeMap("checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected"); // Special Elements (can contain anything) -var special = makeMap("script,style"); +const special = makeMap("script,style"); -var HTMLParser = this.HTMLParser = function (html, handler) { - var index, chars, match, stack = [], last = html; +const HTMLParser = this.HTMLParser = function (html, handler) { + const index, chars, match, stack = [], last = html; stack.last = function () { return this[this.length - 1]; }; @@ -151,7 +151,7 @@ var HTMLParser = this.HTMLParser = function (html, handler) { if (chars) { index = html.indexOf("<"); - var text = index < 0 ? html : html.substring(0, index); + const text = index < 0 ? html : html.substring(0, index); html = index < 0 ? "" : html.substring(index); if (handler.chars) @@ -197,10 +197,10 @@ var HTMLParser = this.HTMLParser = function (html, handler) { stack.push(tagName); if (handler.start) { - var attrs = []; + const attrs = []; rest.replace(attr, function (match, name) { - var value = arguments[2] ? arguments[2] : + const value = arguments[2] ? arguments[2] : arguments[3] ? arguments[3] : arguments[4] ? arguments[4] : fillAttrs[name] ? name : ""; @@ -221,17 +221,17 @@ var HTMLParser = this.HTMLParser = function (html, handler) { if(tagName!== undefined) tagName = tagName.toLowerCase(); // If no tag name is provided, clean shop if (!tagName) - var pos = 0; + const pos = 0; // Find the closest opened tag of the same type else - for (var pos = stack.length - 1; pos >= 0; pos--) + for (const pos = stack.length - 1; pos >= 0; pos--) if (stack[pos] == tagName) break; if (pos >= 0) { // Close all the open elements, up the stack - for (var i = stack.length - 1; i >= pos; i--) + for (const i = stack.length - 1; i >= pos; i--) if (handler.end) handler.end(stack[i]); @@ -242,13 +242,13 @@ var HTMLParser = this.HTMLParser = function (html, handler) { }; this.HTMLtoXML = function (html) { - var results = ""; + const results = ""; HTMLParser(html, { start: function (tag, attrs, unary) { results += "<" + tag; - for (var i = 0; i < attrs.length; i++) + for (const i = 0; i < attrs.length; i++) results += " " + attrs[i].name + '="' + attrs[i].escaped + '"'; results += ">"; }, @@ -268,10 +268,10 @@ this.HTMLtoXML = function (html) { this.HTMLtoDOM = function (html, doc) { // There can be only one of these elements - var one = makeMap("html,head,body,title"); + const one = makeMap("html,head,body,title"); // Enforce a structure for the document - var structure = { + const structure = { link: "head", base: "head" }; @@ -289,15 +289,15 @@ this.HTMLtoDOM = function (html, doc) { doc.getOwnerDocument && doc.getOwnerDocument() || doc; - var elems = [], + const elems = [], documentElement = doc.documentElement || doc.getDocumentElement && doc.getDocumentElement(); // If we're dealing with an empty document then we // need to pre-populate it with the HTML document structure if (!documentElement && doc.createElement) (function () { - var html = doc.createElement("html"); - var head = doc.createElement("head"); + const html = doc.createElement("html"); + const head = doc.createElement("head"); head.appendChild(doc.createElement("title")); html.appendChild(head); html.appendChild(doc.createElement("body")); @@ -306,12 +306,12 @@ this.HTMLtoDOM = function (html, doc) { // Find all the unique elements if (doc.getElementsByTagName) - for (var i in one) + for (const i in one) one[i] = doc.getElementsByTagName(i)[0]; // If we're working with a document, inject contents into // the body element - var curParentNode = one.body; + const curParentNode = one.body; HTMLParser(html, { start: function (tagName, attrs, unary) { @@ -325,9 +325,9 @@ this.HTMLtoDOM = function (html, doc) { return; } - var elem = doc.createElement(tagName); + const elem = doc.createElement(tagName); - for (var attr in attrs) + for (const attr in attrs) elem.setAttribute(attrs[attr].name, attrs[attr].value); if (structure[tagName] && typeof one[structure[tagName]] != "boolean") @@ -359,8 +359,8 @@ this.HTMLtoDOM = function (html, doc) { }; function makeMap(str) { - var obj = {}, items = str.split(","); - for (var i = 0; i < items.length; i++) + const obj = {}, items = str.split(","); + for (const i = 0; i < items.length; i++) obj[items[i]] = true; return obj; } From c68de6f4c541505cad190e8607ebbf1132483be1 Mon Sep 17 00:00:00 2001 From: Ohkubo KOHEI Date: Wed, 26 Jan 2022 15:11:48 +0900 Subject: [PATCH 4/9] export --- htmlparser.mjs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/htmlparser.mjs b/htmlparser.mjs index ed273db..de7f7d0 100644 --- a/htmlparser.mjs +++ b/htmlparser.mjs @@ -104,7 +104,7 @@ const fillAttrs = makeMap("checked,compact,declare,defer,disabled,ismap,multiple // Special Elements (can contain anything) const special = makeMap("script,style"); -const HTMLParser = this.HTMLParser = function (html, handler) { +export const HTMLParser = function (html, handler) { const index, chars, match, stack = [], last = html; stack.last = function () { return this[this.length - 1]; @@ -241,7 +241,7 @@ const HTMLParser = this.HTMLParser = function (html, handler) { } }; -this.HTMLtoXML = function (html) { +export const HTMLtoXML = function (html) { const results = ""; HTMLParser(html, { @@ -266,7 +266,7 @@ this.HTMLtoXML = function (html) { return results; }; -this.HTMLtoDOM = function (html, doc) { +export const HTMLtoDOM = function (html, doc) { // There can be only one of these elements const one = makeMap("html,head,body,title"); @@ -360,7 +360,7 @@ this.HTMLtoDOM = function (html, doc) { function makeMap(str) { const obj = {}, items = str.split(","); - for (const i = 0; i < items.length; i++) + for (let i = 0; i < items.length; i++) obj[items[i]] = true; return obj; } From 8355d4f99fa0c45bb68c0d1b688ac31c8ef248aa Mon Sep 17 00:00:00 2001 From: Ohkubo KOHEI Date: Wed, 26 Jan 2022 06:29:26 +0000 Subject: [PATCH 5/9] lint pass --- htmlparser.mjs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/htmlparser.mjs b/htmlparser.mjs index de7f7d0..0fec935 100644 --- a/htmlparser.mjs +++ b/htmlparser.mjs @@ -159,7 +159,7 @@ export const HTMLParser = function (html, handler) { } } else { - html = html.replace(new RegExp("([\\s\\S]*?)<\/" + stack.last() + "[^>]*>"), function (all, text) { + html = html.replace(new RegExp("([\\s\\S]*?)<\/" + stack.last() + "[^>]*>"), function (_all, text) { text = text.replace(/|/g, "$1$2"); if (handler.chars) handler.chars(text); @@ -178,7 +178,7 @@ export const HTMLParser = function (html, handler) { // Clean up any remaining tags parseEndTag(); - function parseStartTag(tag, tagName, rest, unary) { + function parseStartTag(_tag, tagName, rest, unary) { tagName = tagName.toLowerCase(); if (block[tagName]) { @@ -199,7 +199,7 @@ export const HTMLParser = function (html, handler) { if (handler.start) { const attrs = []; - rest.replace(attr, function (match, name) { + rest.replace(attr, function (_match, name) { const value = arguments[2] ? arguments[2] : arguments[3] ? arguments[3] : arguments[4] ? arguments[4] : @@ -217,14 +217,12 @@ export const HTMLParser = function (html, handler) { } } - function parseEndTag(tag, tagName) { + function parseEndTag(_tag, tagName) { if(tagName!== undefined) tagName = tagName.toLowerCase(); // If no tag name is provided, clean shop - if (!tagName) - const pos = 0; - + let pos = 0 + if (tagName) // Find the closest opened tag of the same type - else for (const pos = stack.length - 1; pos >= 0; pos--) if (stack[pos] == tagName) break; @@ -245,7 +243,7 @@ export const HTMLtoXML = function (html) { const results = ""; HTMLParser(html, { - start: function (tag, attrs, unary) { + start: function (tag, attrs, _unary) { results += "<" + tag; for (const i = 0; i < attrs.length; i++) @@ -341,7 +339,7 @@ export const HTMLtoDOM = function (html, doc) { curParentNode = elem; } }, - end: function (tag) { + end: function (_tag) { elems.length -= 1; // Init the new parentNode @@ -350,7 +348,7 @@ export const HTMLtoDOM = function (html, doc) { chars: function (text) { curParentNode.appendChild(doc.createTextNode(text)); }, - comment: function (text) { + comment: function (_text) { // create comment node } }); From f1a9336071342668df69fd321b5332b100faf33c Mon Sep 17 00:00:00 2001 From: Ohkubo KOHEI Date: Wed, 26 Jan 2022 06:39:40 +0000 Subject: [PATCH 6/9] fix some const --- htmlparser.mjs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/htmlparser.mjs b/htmlparser.mjs index 0fec935..fae4f2d 100644 --- a/htmlparser.mjs +++ b/htmlparser.mjs @@ -105,7 +105,7 @@ const fillAttrs = makeMap("checked,compact,declare,defer,disabled,ismap,multiple const special = makeMap("script,style"); export const HTMLParser = function (html, handler) { - const index, chars, match, stack = [], last = html; + let index, chars, match, stack = [], last = html; stack.last = function () { return this[this.length - 1]; }; @@ -220,7 +220,7 @@ export const HTMLParser = function (html, handler) { function parseEndTag(_tag, tagName) { if(tagName!== undefined) tagName = tagName.toLowerCase(); // If no tag name is provided, clean shop - let pos = 0 + let pos = 0; if (tagName) // Find the closest opened tag of the same type for (const pos = stack.length - 1; pos >= 0; pos--) @@ -229,7 +229,7 @@ export const HTMLParser = function (html, handler) { if (pos >= 0) { // Close all the open elements, up the stack - for (const i = stack.length - 1; i >= pos; i--) + for (let i = stack.length - 1; i >= pos; i--) if (handler.end) handler.end(stack[i]); From f84c2ea7fc8d6b95ee76d9f920cf457512f82355 Mon Sep 17 00:00:00 2001 From: Ohkubo KOHEI Date: Wed, 26 Jan 2022 06:41:46 +0000 Subject: [PATCH 7/9] fix find --- htmlparser.mjs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/htmlparser.mjs b/htmlparser.mjs index fae4f2d..8004b8c 100644 --- a/htmlparser.mjs +++ b/htmlparser.mjs @@ -221,12 +221,9 @@ export const HTMLParser = function (html, handler) { if(tagName!== undefined) tagName = tagName.toLowerCase(); // If no tag name is provided, clean shop let pos = 0; - if (tagName) - // Find the closest opened tag of the same type - for (const pos = stack.length - 1; pos >= 0; pos--) - if (stack[pos] == tagName) - break; - + if (tagName) { + pos = stack.find(x => x === tagName) + } if (pos >= 0) { // Close all the open elements, up the stack for (let i = stack.length - 1; i >= pos; i--) From 68866eeb9188c4af6e28f2f7adcd81810184ef0b Mon Sep 17 00:00:00 2001 From: Ohkubo KOHEI Date: Wed, 26 Jan 2022 06:49:41 +0000 Subject: [PATCH 8/9] skip doctype --- htmlparser.mjs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/htmlparser.mjs b/htmlparser.mjs index 8004b8c..31fa540 100644 --- a/htmlparser.mjs +++ b/htmlparser.mjs @@ -137,8 +137,11 @@ export const HTMLParser = function (html, handler) { chars = false; } - // start tag + } else if (html.indexOf("") + 1); } else if (html.indexOf("<") == 0) { + // start tag match = html.match(startTag); if (match) { @@ -359,3 +362,4 @@ function makeMap(str) { obj[items[i]] = true; return obj; } +HTMLParser('
test
', {}) \ No newline at end of file From 426a1c653da1af9df2aee5307425883b4968ae51 Mon Sep 17 00:00:00 2001 From: Ohkubo KOHEI Date: Wed, 26 Jan 2022 07:09:16 +0000 Subject: [PATCH 9/9] fix find --- htmlparser.mjs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/htmlparser.mjs b/htmlparser.mjs index 31fa540..f7d2b4a 100644 --- a/htmlparser.mjs +++ b/htmlparser.mjs @@ -174,7 +174,7 @@ export const HTMLParser = function (html, handler) { } if (html == last) - throw "Parse Error: " + html; + throw new Error("Parse Error: " + JSON.stringify(stack) + html.substring(0, 20)); last = html; } @@ -225,7 +225,7 @@ export const HTMLParser = function (html, handler) { // If no tag name is provided, clean shop let pos = 0; if (tagName) { - pos = stack.find(x => x === tagName) + pos = stack.indexOf(tagName) } if (pos >= 0) { // Close all the open elements, up the stack @@ -362,4 +362,10 @@ function makeMap(str) { obj[items[i]] = true; return obj; } -HTMLParser('
test
', {}) \ No newline at end of file + +function test() { + const url = 'https://example.com' + fetch(url).then(x=>x.text()).then(html=>{ + HTMLParser(html, {}) + }) +}