-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from remarkablemark/client-parser-refactor
Refactor and improve client parser
- Loading branch information
Showing
3 changed files
with
183 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Constants. | ||
*/ | ||
var HTML_TAG_NAME = 'html'; | ||
var BODY_TAG_NAME = 'body'; | ||
var HEAD_TAG_NAME = 'head'; | ||
var FIRST_TAG_REGEX = /<([a-zA-Z]+[0-9]?)/; // e.g., <h1> | ||
var HEAD_REGEX = /<head[\s\S]*>[\s\S]*<\/head>/i; | ||
var BODY_REGEX = /<body[\s\S]*>[\s\S]*<\/body>/i; | ||
|
||
/** | ||
* DOMParser (performance: slow). | ||
* | ||
* https://developer.mozilla.org/docs/Web/API/DOMParser#Parsing_an_SVG_or_HTML_document | ||
*/ | ||
var parseFromString; | ||
if (typeof window.DOMParser === 'function') { | ||
var domParser = new window.DOMParser(); | ||
var MIME_TYPE = 'text/' + HTML_TAG_NAME; | ||
|
||
/** | ||
* Creates an HTML document using `DOMParser.parseFromString`. | ||
* | ||
* @param {String} html - The HTML string. | ||
* @param {String} [tagName] - The element to render the HTML. | ||
* @return {HTMLDocument} | ||
*/ | ||
parseFromString = function domStringParser(html, tagName) { | ||
if (tagName) { | ||
html = ['<', tagName, '>', html, '</', tagName, '>'].join(''); | ||
} | ||
return domParser.parseFromString(html, MIME_TYPE); | ||
}; | ||
} | ||
|
||
/** | ||
* DOMImplementation (performance: fair). | ||
* | ||
* https://developer.mozilla.org/docs/Web/API/DOMImplementation/createHTMLDocument | ||
*/ | ||
var parseFromDocument; | ||
if (typeof document.implementation === 'object') { | ||
var doc = document.implementation.createHTMLDocument(); | ||
|
||
/** | ||
* Use HTML document created by `document.implementation.createHTMLDocument`. | ||
* | ||
* @param {String} html - The HTML string. | ||
* @param {String} [tagName] - The element to render the HTML. | ||
* @return {HTMLDocument} | ||
*/ | ||
parseFromDocument = function createHTMLDocument(html, tagName) { | ||
if (tagName) { | ||
doc.documentElement.getElementsByTagName(tagName)[0].innerHTML = html; | ||
} else { | ||
doc.documentElement.innerHTML = html; | ||
} | ||
return doc; | ||
}; | ||
} | ||
|
||
/** | ||
* Template (performance: fast). | ||
* | ||
* https://developer.mozilla.org/docs/Web/HTML/Element/template | ||
*/ | ||
var parseFromTemplate; | ||
var template = document.createElement('template'); | ||
if (template.content) { | ||
|
||
/** | ||
* Uses a template element (content fragment) to parse HTML. | ||
* | ||
* @param {String} html - The HTML string. | ||
* @return {NodeList} | ||
*/ | ||
parseFromTemplate = function templateParser(html) { | ||
template.innerHTML = html; | ||
return template.content.childNodes; | ||
}; | ||
} | ||
|
||
/** Fallback document parser. */ | ||
var parseWithFallback = parseFromDocument || parseFromString; | ||
|
||
/** | ||
* Parses HTML string to DOM nodes. | ||
* | ||
* @param {String} html - The HTML string. | ||
* @param {String} [tagName] - The tag name. | ||
* @return {NodeList|Array} | ||
*/ | ||
module.exports = function domparser(html) { | ||
// try to match first tag | ||
var tagName; | ||
var match = html.match(FIRST_TAG_REGEX); | ||
if (match && match[1]) { | ||
tagName = match[1]; | ||
} | ||
|
||
var doc; | ||
var element; | ||
var elements; | ||
|
||
switch (tagName) { | ||
case HTML_TAG_NAME: | ||
if (parseFromString) { | ||
doc = parseFromString(html); | ||
|
||
// strip elements if not found | ||
if (!HEAD_REGEX.test(html)) { | ||
element = doc.getElementsByTagName(HEAD_TAG_NAME)[0]; | ||
element.parentNode.removeChild(element); | ||
} | ||
|
||
if (!BODY_REGEX.test(html)) { | ||
element = doc.getElementsByTagName(BODY_TAG_NAME)[0]; | ||
element.parentNode.removeChild(element); | ||
} | ||
|
||
return doc.getElementsByTagName(HTML_TAG_NAME); | ||
} | ||
break; | ||
|
||
case HEAD_TAG_NAME: | ||
if (parseWithFallback) { | ||
elements = parseWithFallback(html).getElementsByTagName(HEAD_TAG_NAME); | ||
|
||
// account for possibility of sibling | ||
if (BODY_REGEX.test(html)) { | ||
return elements[0].parentNode.childNodes; | ||
} | ||
return elements; | ||
} | ||
break; | ||
|
||
case BODY_TAG_NAME: | ||
if (parseWithFallback) { | ||
elements = parseWithFallback(html).getElementsByTagName(BODY_TAG_NAME); | ||
|
||
// account for possibility of sibling | ||
if (HEAD_REGEX.test(html)) { | ||
return elements[0].parentNode.childNodes; | ||
} | ||
return elements; | ||
} | ||
break; | ||
|
||
// low-level tag or text | ||
default: | ||
if (parseFromTemplate) return parseFromTemplate(html); | ||
if (parseWithFallback) { | ||
return parseWithFallback(html, BODY_TAG_NAME).getElementsByTagName(BODY_TAG_NAME)[0].childNodes; | ||
} | ||
break; | ||
} | ||
|
||
return []; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters