2015-03-21 06:50:45 +03:00
|
|
|
/*
|
|
|
|
* DO NOT MODIFY THIS FILE DIRECTLY!
|
|
|
|
*
|
|
|
|
* This is a shared library that is maintained in an external repo:
|
|
|
|
* https://github.com/mozilla/readability
|
|
|
|
*/
|
|
|
|
|
2012-08-10 10:30:46 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a relatively lightweight DOMParser that is safe to use in a web
|
|
|
|
* worker. This is far from a complete DOM implementation; however, it should
|
|
|
|
* contain the minimal set of functionality necessary for Readability.js.
|
|
|
|
*
|
|
|
|
* Aside from not implementing the full DOM API, there are other quirks to be
|
|
|
|
* aware of when using the JSDOMParser:
|
|
|
|
*
|
|
|
|
* 1) Properly formed HTML/XML must be used. This means you should be extra
|
|
|
|
* careful when using this parser on anything received directly from an
|
|
|
|
* XMLHttpRequest. Providing a serialized string from an XMLSerializer,
|
|
|
|
* however, should be safe (since the browser's XMLSerializer should
|
|
|
|
* generate valid HTML/XML). Therefore, if parsing a document from an XHR,
|
|
|
|
* the recommended approach is to do the XHR in the main thread, use
|
|
|
|
* XMLSerializer.serializeToString() on the responseXML, and pass the
|
|
|
|
* resulting string to the worker.
|
|
|
|
*
|
|
|
|
* 2) Live NodeLists are not supported. DOM methods and properties such as
|
|
|
|
* getElementsByTagName() and childNodes return standard arrays. If you
|
|
|
|
* want these lists to be updated when nodes are removed or added to the
|
|
|
|
* document, you must take care to manually update them yourself.
|
|
|
|
*/
|
|
|
|
(function (global) {
|
|
|
|
|
|
|
|
function error(m) {
|
2015-03-18 07:06:06 +03:00
|
|
|
dump("JSDOMParser error: " + m + "\n");
|
2012-08-10 10:30:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// When a style is set in JS, map it to the corresponding CSS attribute
|
2015-03-21 06:50:45 +03:00
|
|
|
var styleMap = {
|
2012-08-10 10:30:46 +04:00
|
|
|
"alignmentBaseline": "alignment-baseline",
|
|
|
|
"background": "background",
|
|
|
|
"backgroundAttachment": "background-attachment",
|
|
|
|
"backgroundClip": "background-clip",
|
|
|
|
"backgroundColor": "background-color",
|
|
|
|
"backgroundImage": "background-image",
|
|
|
|
"backgroundOrigin": "background-origin",
|
|
|
|
"backgroundPosition": "background-position",
|
|
|
|
"backgroundPositionX": "background-position-x",
|
|
|
|
"backgroundPositionY": "background-position-y",
|
|
|
|
"backgroundRepeat": "background-repeat",
|
|
|
|
"backgroundRepeatX": "background-repeat-x",
|
|
|
|
"backgroundRepeatY": "background-repeat-y",
|
|
|
|
"backgroundSize": "background-size",
|
|
|
|
"baselineShift": "baseline-shift",
|
|
|
|
"border": "border",
|
|
|
|
"borderBottom": "border-bottom",
|
|
|
|
"borderBottomColor": "border-bottom-color",
|
|
|
|
"borderBottomLeftRadius": "border-bottom-left-radius",
|
|
|
|
"borderBottomRightRadius": "border-bottom-right-radius",
|
|
|
|
"borderBottomStyle": "border-bottom-style",
|
|
|
|
"borderBottomWidth": "border-bottom-width",
|
|
|
|
"borderCollapse": "border-collapse",
|
|
|
|
"borderColor": "border-color",
|
|
|
|
"borderImage": "border-image",
|
|
|
|
"borderImageOutset": "border-image-outset",
|
|
|
|
"borderImageRepeat": "border-image-repeat",
|
|
|
|
"borderImageSlice": "border-image-slice",
|
|
|
|
"borderImageSource": "border-image-source",
|
|
|
|
"borderImageWidth": "border-image-width",
|
|
|
|
"borderLeft": "border-left",
|
|
|
|
"borderLeftColor": "border-left-color",
|
|
|
|
"borderLeftStyle": "border-left-style",
|
|
|
|
"borderLeftWidth": "border-left-width",
|
|
|
|
"borderRadius": "border-radius",
|
|
|
|
"borderRight": "border-right",
|
|
|
|
"borderRightColor": "border-right-color",
|
|
|
|
"borderRightStyle": "border-right-style",
|
|
|
|
"borderRightWidth": "border-right-width",
|
|
|
|
"borderSpacing": "border-spacing",
|
|
|
|
"borderStyle": "border-style",
|
|
|
|
"borderTop": "border-top",
|
|
|
|
"borderTopColor": "border-top-color",
|
|
|
|
"borderTopLeftRadius": "border-top-left-radius",
|
|
|
|
"borderTopRightRadius": "border-top-right-radius",
|
|
|
|
"borderTopStyle": "border-top-style",
|
|
|
|
"borderTopWidth": "border-top-width",
|
|
|
|
"borderWidth": "border-width",
|
|
|
|
"bottom": "bottom",
|
|
|
|
"boxShadow": "box-shadow",
|
|
|
|
"boxSizing": "box-sizing",
|
|
|
|
"captionSide": "caption-side",
|
|
|
|
"clear": "clear",
|
|
|
|
"clip": "clip",
|
|
|
|
"clipPath": "clip-path",
|
|
|
|
"clipRule": "clip-rule",
|
|
|
|
"color": "color",
|
|
|
|
"colorInterpolation": "color-interpolation",
|
|
|
|
"colorInterpolationFilters": "color-interpolation-filters",
|
|
|
|
"colorProfile": "color-profile",
|
|
|
|
"colorRendering": "color-rendering",
|
|
|
|
"content": "content",
|
|
|
|
"counterIncrement": "counter-increment",
|
|
|
|
"counterReset": "counter-reset",
|
|
|
|
"cursor": "cursor",
|
|
|
|
"direction": "direction",
|
|
|
|
"display": "display",
|
|
|
|
"dominantBaseline": "dominant-baseline",
|
|
|
|
"emptyCells": "empty-cells",
|
|
|
|
"enableBackground": "enable-background",
|
|
|
|
"fill": "fill",
|
|
|
|
"fillOpacity": "fill-opacity",
|
|
|
|
"fillRule": "fill-rule",
|
|
|
|
"filter": "filter",
|
|
|
|
"cssFloat": "float",
|
|
|
|
"floodColor": "flood-color",
|
|
|
|
"floodOpacity": "flood-opacity",
|
|
|
|
"font": "font",
|
|
|
|
"fontFamily": "font-family",
|
|
|
|
"fontSize": "font-size",
|
|
|
|
"fontStretch": "font-stretch",
|
|
|
|
"fontStyle": "font-style",
|
|
|
|
"fontVariant": "font-variant",
|
|
|
|
"fontWeight": "font-weight",
|
|
|
|
"glyphOrientationHorizontal": "glyph-orientation-horizontal",
|
|
|
|
"glyphOrientationVertical": "glyph-orientation-vertical",
|
|
|
|
"height": "height",
|
|
|
|
"imageRendering": "image-rendering",
|
|
|
|
"kerning": "kerning",
|
|
|
|
"left": "left",
|
|
|
|
"letterSpacing": "letter-spacing",
|
|
|
|
"lightingColor": "lighting-color",
|
|
|
|
"lineHeight": "line-height",
|
|
|
|
"listStyle": "list-style",
|
|
|
|
"listStyleImage": "list-style-image",
|
|
|
|
"listStylePosition": "list-style-position",
|
|
|
|
"listStyleType": "list-style-type",
|
|
|
|
"margin": "margin",
|
|
|
|
"marginBottom": "margin-bottom",
|
|
|
|
"marginLeft": "margin-left",
|
|
|
|
"marginRight": "margin-right",
|
|
|
|
"marginTop": "margin-top",
|
|
|
|
"marker": "marker",
|
|
|
|
"markerEnd": "marker-end",
|
|
|
|
"markerMid": "marker-mid",
|
|
|
|
"markerStart": "marker-start",
|
|
|
|
"mask": "mask",
|
|
|
|
"maxHeight": "max-height",
|
|
|
|
"maxWidth": "max-width",
|
|
|
|
"minHeight": "min-height",
|
|
|
|
"minWidth": "min-width",
|
|
|
|
"opacity": "opacity",
|
|
|
|
"orphans": "orphans",
|
|
|
|
"outline": "outline",
|
|
|
|
"outlineColor": "outline-color",
|
|
|
|
"outlineOffset": "outline-offset",
|
|
|
|
"outlineStyle": "outline-style",
|
|
|
|
"outlineWidth": "outline-width",
|
|
|
|
"overflow": "overflow",
|
|
|
|
"overflowX": "overflow-x",
|
|
|
|
"overflowY": "overflow-y",
|
|
|
|
"padding": "padding",
|
|
|
|
"paddingBottom": "padding-bottom",
|
|
|
|
"paddingLeft": "padding-left",
|
|
|
|
"paddingRight": "padding-right",
|
|
|
|
"paddingTop": "padding-top",
|
|
|
|
"page": "page",
|
|
|
|
"pageBreakAfter": "page-break-after",
|
|
|
|
"pageBreakBefore": "page-break-before",
|
|
|
|
"pageBreakInside": "page-break-inside",
|
|
|
|
"pointerEvents": "pointer-events",
|
|
|
|
"position": "position",
|
|
|
|
"quotes": "quotes",
|
|
|
|
"resize": "resize",
|
|
|
|
"right": "right",
|
|
|
|
"shapeRendering": "shape-rendering",
|
|
|
|
"size": "size",
|
|
|
|
"speak": "speak",
|
|
|
|
"src": "src",
|
|
|
|
"stopColor": "stop-color",
|
|
|
|
"stopOpacity": "stop-opacity",
|
|
|
|
"stroke": "stroke",
|
|
|
|
"strokeDasharray": "stroke-dasharray",
|
|
|
|
"strokeDashoffset": "stroke-dashoffset",
|
|
|
|
"strokeLinecap": "stroke-linecap",
|
|
|
|
"strokeLinejoin": "stroke-linejoin",
|
|
|
|
"strokeMiterlimit": "stroke-miterlimit",
|
|
|
|
"strokeOpacity": "stroke-opacity",
|
|
|
|
"strokeWidth": "stroke-width",
|
|
|
|
"tableLayout": "table-layout",
|
|
|
|
"textAlign": "text-align",
|
|
|
|
"textAnchor": "text-anchor",
|
|
|
|
"textDecoration": "text-decoration",
|
|
|
|
"textIndent": "text-indent",
|
|
|
|
"textLineThrough": "text-line-through",
|
|
|
|
"textLineThroughColor": "text-line-through-color",
|
|
|
|
"textLineThroughMode": "text-line-through-mode",
|
|
|
|
"textLineThroughStyle": "text-line-through-style",
|
|
|
|
"textLineThroughWidth": "text-line-through-width",
|
|
|
|
"textOverflow": "text-overflow",
|
|
|
|
"textOverline": "text-overline",
|
|
|
|
"textOverlineColor": "text-overline-color",
|
|
|
|
"textOverlineMode": "text-overline-mode",
|
|
|
|
"textOverlineStyle": "text-overline-style",
|
|
|
|
"textOverlineWidth": "text-overline-width",
|
|
|
|
"textRendering": "text-rendering",
|
|
|
|
"textShadow": "text-shadow",
|
|
|
|
"textTransform": "text-transform",
|
|
|
|
"textUnderline": "text-underline",
|
|
|
|
"textUnderlineColor": "text-underline-color",
|
|
|
|
"textUnderlineMode": "text-underline-mode",
|
|
|
|
"textUnderlineStyle": "text-underline-style",
|
|
|
|
"textUnderlineWidth": "text-underline-width",
|
|
|
|
"top": "top",
|
|
|
|
"unicodeBidi": "unicode-bidi",
|
|
|
|
"unicodeRange": "unicode-range",
|
|
|
|
"vectorEffect": "vector-effect",
|
|
|
|
"verticalAlign": "vertical-align",
|
|
|
|
"visibility": "visibility",
|
|
|
|
"whiteSpace": "white-space",
|
|
|
|
"widows": "widows",
|
|
|
|
"width": "width",
|
|
|
|
"wordBreak": "word-break",
|
|
|
|
"wordSpacing": "word-spacing",
|
|
|
|
"wordWrap": "word-wrap",
|
|
|
|
"writingMode": "writing-mode",
|
|
|
|
"zIndex": "z-index",
|
|
|
|
"zoom": "zoom",
|
|
|
|
};
|
|
|
|
|
|
|
|
// Elements that can be self-closing
|
2015-03-21 06:50:45 +03:00
|
|
|
var voidElems = {
|
2012-08-10 10:30:46 +04:00
|
|
|
"area": true,
|
|
|
|
"base": true,
|
|
|
|
"br": true,
|
|
|
|
"col": true,
|
|
|
|
"command": true,
|
|
|
|
"embed": true,
|
|
|
|
"hr": true,
|
|
|
|
"img": true,
|
|
|
|
"input": true,
|
|
|
|
"link": true,
|
|
|
|
"meta": true,
|
|
|
|
"param": true,
|
|
|
|
"source": true,
|
|
|
|
};
|
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
var whitespace = [" ", "\t", "\n", "\r"];
|
|
|
|
|
2012-08-10 10:30:46 +04:00
|
|
|
// See http://www.w3schools.com/dom/dom_nodetype.asp
|
2015-03-21 06:50:45 +03:00
|
|
|
var nodeTypes = {
|
2012-08-10 10:30:46 +04:00
|
|
|
ELEMENT_NODE: 1,
|
|
|
|
ATTRIBUTE_NODE: 2,
|
|
|
|
TEXT_NODE: 3,
|
|
|
|
CDATA_SECTION_NODE: 4,
|
|
|
|
ENTITY_REFERENCE_NODE: 5,
|
|
|
|
ENTITY_NODE: 6,
|
|
|
|
PROCESSING_INSTRUCTION_NODE: 7,
|
|
|
|
COMMENT_NODE: 8,
|
|
|
|
DOCUMENT_NODE: 9,
|
|
|
|
DOCUMENT_TYPE_NODE: 10,
|
|
|
|
DOCUMENT_FRAGMENT_NODE: 11,
|
|
|
|
NOTATION_NODE: 12
|
|
|
|
};
|
|
|
|
|
|
|
|
function getElementsByTagName(tag) {
|
|
|
|
tag = tag.toUpperCase();
|
2015-03-21 06:50:45 +03:00
|
|
|
var elems = [];
|
|
|
|
var allTags = (tag === "*");
|
2012-08-10 10:30:46 +04:00
|
|
|
function getElems(node) {
|
2015-03-21 06:50:45 +03:00
|
|
|
var length = node.children.length;
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
var child = node.children[i];
|
2013-05-08 06:38:16 +04:00
|
|
|
if (allTags || (child.tagName === tag))
|
2012-08-10 10:30:46 +04:00
|
|
|
elems.push(child);
|
|
|
|
getElems(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
getElems(this);
|
|
|
|
return elems;
|
|
|
|
}
|
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
var Node = function () {};
|
2012-08-10 10:30:46 +04:00
|
|
|
|
|
|
|
Node.prototype = {
|
|
|
|
attributes: null,
|
|
|
|
childNodes: null,
|
|
|
|
localName: null,
|
|
|
|
nodeName: null,
|
|
|
|
parentNode: null,
|
|
|
|
textContent: null,
|
2015-03-21 06:50:45 +03:00
|
|
|
nextSibling: null,
|
|
|
|
previousSibling: null,
|
2012-08-10 10:30:46 +04:00
|
|
|
|
|
|
|
get firstChild() {
|
|
|
|
return this.childNodes[0] || null;
|
|
|
|
},
|
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
get firstElementChild() {
|
|
|
|
return this.children[0] || null;
|
|
|
|
},
|
|
|
|
|
|
|
|
get lastChild() {
|
|
|
|
return this.childNodes[this.childNodes.length - 1] || null;
|
|
|
|
},
|
2012-08-10 10:30:46 +04:00
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
get lastElementChild() {
|
|
|
|
return this.children[this.children.length - 1] || null;
|
2012-08-10 10:30:46 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
appendChild: function (child) {
|
|
|
|
if (child.parentNode) {
|
|
|
|
child.parentNode.removeChild(child);
|
|
|
|
}
|
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
var last = this.lastChild;
|
|
|
|
if (last)
|
|
|
|
last.nextSibling = child;
|
|
|
|
child.previousSibling = last;
|
|
|
|
|
|
|
|
if (child.nodeType === Node.ELEMENT_NODE) {
|
|
|
|
child.previousElementSibling = this.children[this.children.length - 1] || null;
|
|
|
|
this.children.push(child);
|
|
|
|
child.previousElementSibling && (child.previousElementSibling.nextElementSibling = child);
|
|
|
|
}
|
2012-08-10 10:30:46 +04:00
|
|
|
this.childNodes.push(child);
|
|
|
|
child.parentNode = this;
|
|
|
|
},
|
|
|
|
|
|
|
|
removeChild: function (child) {
|
2015-03-21 06:50:45 +03:00
|
|
|
var childNodes = this.childNodes;
|
|
|
|
var childIndex = childNodes.indexOf(child);
|
2013-05-08 06:38:16 +04:00
|
|
|
if (childIndex === -1) {
|
2012-08-10 10:30:46 +04:00
|
|
|
throw "removeChild: node not found";
|
|
|
|
} else {
|
2012-08-14 23:51:11 +04:00
|
|
|
child.parentNode = null;
|
2015-03-21 06:50:45 +03:00
|
|
|
var prev = child.previousSibling;
|
|
|
|
var next = child.nextSibling;
|
|
|
|
if (prev)
|
|
|
|
prev.nextSibling = next;
|
|
|
|
if (next)
|
|
|
|
next.previousSibling = prev;
|
|
|
|
|
|
|
|
if (child.nodeType === Node.ELEMENT_NODE) {
|
|
|
|
prev = child.previousElementSibling;
|
|
|
|
next = child.nextElementSibling;
|
|
|
|
if (prev)
|
|
|
|
prev.nextElementSibling = next;
|
|
|
|
if (next)
|
|
|
|
next.previousElementSibling = prev;
|
|
|
|
this.children.splice(this.children.indexOf(child), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
child.previousSibling = child.nextSibling = null;
|
|
|
|
child.previousElementSibling = child.nextElementSibling = null;
|
|
|
|
|
2012-08-10 10:30:46 +04:00
|
|
|
return childNodes.splice(childIndex, 1)[0];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
replaceChild: function (newNode, oldNode) {
|
2015-03-21 06:50:45 +03:00
|
|
|
var childNodes = this.childNodes;
|
|
|
|
var childIndex = childNodes.indexOf(oldNode);
|
2013-05-08 06:38:16 +04:00
|
|
|
if (childIndex === -1) {
|
2012-08-10 10:30:46 +04:00
|
|
|
throw "replaceChild: node not found";
|
|
|
|
} else {
|
2015-03-21 06:50:45 +03:00
|
|
|
// This will take care of updating the new node if it was somewhere else before:
|
2012-08-25 14:27:27 +04:00
|
|
|
if (newNode.parentNode)
|
|
|
|
newNode.parentNode.removeChild(newNode);
|
|
|
|
|
2012-08-10 10:30:46 +04:00
|
|
|
childNodes[childIndex] = newNode;
|
2015-03-21 06:50:45 +03:00
|
|
|
|
|
|
|
// update the new node's sibling properties, and its new siblings' sibling properties
|
|
|
|
newNode.nextSibling = oldNode.nextSibling;
|
|
|
|
newNode.previousSibling = oldNode.previousSibling;
|
|
|
|
if (newNode.nextSibling)
|
|
|
|
newNode.nextSibling.previousSibling = newNode;
|
|
|
|
if (newNode.previousSibling)
|
|
|
|
newNode.previousSibling.nextSibling = newNode;
|
|
|
|
|
2012-08-10 10:30:46 +04:00
|
|
|
newNode.parentNode = this;
|
2015-03-21 06:50:45 +03:00
|
|
|
|
|
|
|
// Now deal with elements before we clear out those values for the old node,
|
|
|
|
// because it can help us take shortcuts here:
|
|
|
|
if (newNode.nodeType === Node.ELEMENT_NODE) {
|
|
|
|
if (oldNode.nodeType === Node.ELEMENT_NODE) {
|
|
|
|
// Both were elements, which makes this easier, we just swap things out:
|
|
|
|
newNode.previousElementSibling = oldNode.previousElementSibling;
|
|
|
|
newNode.nextElementSibling = oldNode.nextElementSibling;
|
|
|
|
if (newNode.previousElementSibling)
|
|
|
|
newNode.previousElementSibling.nextElementSibling = newNode;
|
|
|
|
if (newNode.nextElementSibling)
|
|
|
|
newNode.nextElementSibling.previousElementSibling = newNode;
|
|
|
|
this.children[this.children.indexOf(oldNode)] = newNode;
|
|
|
|
} else {
|
|
|
|
// Hard way:
|
|
|
|
newNode.previousElementSibling = (function() {
|
|
|
|
for (var i = childIndex - 1; i >= 0; i--) {
|
|
|
|
if (childNodes[i].nodeType === Node.ELEMENT_NODE)
|
|
|
|
return childNodes[i];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
})();
|
|
|
|
if (newNode.previousElementSibling) {
|
|
|
|
newNode.nextElementSibling = newNode.previousElementSibling.nextElementSibling;
|
|
|
|
} else {
|
|
|
|
newNode.nextElementSibling = (function() {
|
|
|
|
for (var i = childIndex + 1; i < childNodes.length; i++) {
|
|
|
|
if (childNodes[i].nodeType === Node.ELEMENT_NODE)
|
|
|
|
return childNodes[i];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
if (newNode.previousElementSibling)
|
|
|
|
newNode.previousElementSibling.nextElementSibling = newNode;
|
|
|
|
if (newNode.nextElementSibling)
|
|
|
|
newNode.nextElementSibling.previousElementSibling = newNode;
|
|
|
|
|
|
|
|
if (newNode.nextElementSibling)
|
|
|
|
this.children.splice(this.children.indexOf(newNode.nextElementSibling), 0, newNode);
|
|
|
|
else
|
|
|
|
this.children.push(newNode);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// new node is not an element node.
|
|
|
|
// if the old one was, update its element siblings:
|
|
|
|
if (oldNode.nodeType === Node.ELEMENT_NODE) {
|
|
|
|
if (oldNode.previousElementSibling)
|
|
|
|
oldNode.previousElementSibling.nextElementSibling = oldNode.nextElementSibling;
|
|
|
|
if (oldNode.nextElementSibling)
|
|
|
|
oldNode.nextElementSibling.previousElementSibling = oldNode.previousElementSibling;
|
|
|
|
this.children.splice(this.children.indexOf(oldNode), 1);
|
|
|
|
}
|
|
|
|
// If the old node wasn't an element, neither the new nor the old node was an element,
|
|
|
|
// and the children array and its members shouldn't need any updating.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-10 10:30:46 +04:00
|
|
|
oldNode.parentNode = null;
|
2015-03-21 06:50:45 +03:00
|
|
|
oldNode.previousSibling = null;
|
|
|
|
oldNode.nextSibling = null;
|
|
|
|
if (oldNode.nodeType === Node.ELEMENT_NODE) {
|
|
|
|
oldNode.previousElementSibling = null;
|
|
|
|
oldNode.nextElementSibling = null;
|
|
|
|
}
|
2012-08-10 10:30:46 +04:00
|
|
|
return oldNode;
|
|
|
|
}
|
2015-04-10 05:55:51 +03:00
|
|
|
}
|
2012-08-10 10:30:46 +04:00
|
|
|
};
|
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
for (var i in nodeTypes) {
|
2012-08-10 10:30:46 +04:00
|
|
|
Node[i] = Node.prototype[i] = nodeTypes[i];
|
|
|
|
}
|
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
var Attribute = function (name, value) {
|
2012-08-10 10:30:46 +04:00
|
|
|
this.name = name;
|
2015-04-10 05:55:51 +03:00
|
|
|
this.value = value;
|
2012-08-10 10:30:46 +04:00
|
|
|
};
|
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
var Comment = function () {
|
2012-08-10 10:30:46 +04:00
|
|
|
this.childNodes = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
Comment.prototype = {
|
|
|
|
__proto__: Node.prototype,
|
|
|
|
|
|
|
|
nodeName: "#comment",
|
|
|
|
nodeType: Node.COMMENT_NODE
|
|
|
|
};
|
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
var Text = function () {
|
2012-08-10 10:30:46 +04:00
|
|
|
this.childNodes = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
Text.prototype = {
|
|
|
|
__proto__: Node.prototype,
|
|
|
|
|
|
|
|
nodeName: "#text",
|
|
|
|
nodeType: Node.TEXT_NODE,
|
2015-04-10 05:55:51 +03:00
|
|
|
textContent: ""
|
2012-08-10 10:30:46 +04:00
|
|
|
}
|
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
var Document = function () {
|
2012-08-10 10:30:46 +04:00
|
|
|
this.styleSheets = [];
|
|
|
|
this.childNodes = [];
|
2015-03-21 06:50:45 +03:00
|
|
|
this.children = [];
|
2012-08-10 10:30:46 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
Document.prototype = {
|
|
|
|
__proto__: Node.prototype,
|
|
|
|
|
|
|
|
nodeName: "#document",
|
|
|
|
nodeType: Node.DOCUMENT_NODE,
|
|
|
|
title: "",
|
|
|
|
|
|
|
|
getElementsByTagName: getElementsByTagName,
|
|
|
|
|
|
|
|
getElementById: function (id) {
|
|
|
|
function getElem(node) {
|
2015-03-21 06:50:45 +03:00
|
|
|
var length = node.children.length;
|
2013-05-08 06:38:16 +04:00
|
|
|
if (node.id === id)
|
2012-08-10 10:30:46 +04:00
|
|
|
return node;
|
2015-03-21 06:50:45 +03:00
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
var el = getElem(node.children[i]);
|
2012-08-10 10:30:46 +04:00
|
|
|
if (el)
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return getElem(this);
|
|
|
|
},
|
|
|
|
|
|
|
|
createElement: function (tag) {
|
2015-03-21 06:50:45 +03:00
|
|
|
var node = new Element(tag);
|
2012-08-10 10:30:46 +04:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
var Element = function (tag) {
|
2012-08-10 10:30:46 +04:00
|
|
|
this.attributes = [];
|
|
|
|
this.childNodes = [];
|
2015-03-21 06:50:45 +03:00
|
|
|
this.children = [];
|
|
|
|
this.nextElementSibling = this.previousElementSibling = null;
|
2012-08-10 10:30:46 +04:00
|
|
|
this.localName = tag.toLowerCase();
|
|
|
|
this.tagName = tag.toUpperCase();
|
|
|
|
this.style = new Style(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype = {
|
|
|
|
__proto__: Node.prototype,
|
|
|
|
|
|
|
|
nodeType: Node.ELEMENT_NODE,
|
|
|
|
|
|
|
|
getElementsByTagName: getElementsByTagName,
|
|
|
|
|
|
|
|
get className() {
|
|
|
|
return this.getAttribute("class") || "";
|
|
|
|
},
|
|
|
|
|
|
|
|
set className(str) {
|
|
|
|
this.setAttribute("class", str);
|
|
|
|
},
|
|
|
|
|
|
|
|
get id() {
|
|
|
|
return this.getAttribute("id") || "";
|
|
|
|
},
|
|
|
|
|
|
|
|
set id(str) {
|
|
|
|
this.setAttribute("id", str);
|
|
|
|
},
|
|
|
|
|
|
|
|
get href() {
|
|
|
|
return this.getAttribute("href") || "";
|
|
|
|
},
|
|
|
|
|
|
|
|
set href(str) {
|
|
|
|
this.setAttribute("href", str);
|
|
|
|
},
|
|
|
|
|
|
|
|
get src() {
|
|
|
|
return this.getAttribute("src") || "";
|
|
|
|
},
|
|
|
|
|
|
|
|
set src(str) {
|
|
|
|
this.setAttribute("src", str);
|
|
|
|
},
|
|
|
|
|
|
|
|
get nodeName() {
|
|
|
|
return this.tagName;
|
|
|
|
},
|
|
|
|
|
|
|
|
get innerHTML() {
|
|
|
|
function getHTML(node) {
|
2015-03-21 06:50:45 +03:00
|
|
|
var i = 0;
|
2012-08-10 10:30:46 +04:00
|
|
|
for (i = 0; i < node.childNodes.length; i++) {
|
2015-03-21 06:50:45 +03:00
|
|
|
var child = node.childNodes[i];
|
2012-08-10 10:30:46 +04:00
|
|
|
if (child.localName) {
|
2012-09-01 02:56:13 +04:00
|
|
|
arr.push("<" + child.localName);
|
2012-08-10 10:30:46 +04:00
|
|
|
|
|
|
|
// serialize attribute list
|
2015-03-21 06:50:45 +03:00
|
|
|
for (var j = 0; j < child.attributes.length; j++) {
|
|
|
|
var attr = child.attributes[j];
|
2015-04-10 05:55:51 +03:00
|
|
|
var quote = (attr.value.indexOf('"') === -1 ? '"' : "'");
|
|
|
|
arr.push(" " + attr.name + '=' + quote + attr.value + quote);
|
2012-08-10 10:30:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (child.localName in voidElems) {
|
|
|
|
// if this is a self-closing element, end it here
|
2015-04-10 05:55:51 +03:00
|
|
|
arr.push("/>");
|
2012-08-10 10:30:46 +04:00
|
|
|
} else {
|
|
|
|
// otherwise, add its children
|
2012-09-01 02:56:13 +04:00
|
|
|
arr.push(">");
|
2012-08-10 10:30:46 +04:00
|
|
|
getHTML(child);
|
2012-09-01 02:56:13 +04:00
|
|
|
arr.push("</" + child.localName + ">");
|
2012-08-10 10:30:46 +04:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-10 05:55:51 +03:00
|
|
|
arr.push(child.textContent);
|
2012-08-10 10:30:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-04 22:24:52 +04:00
|
|
|
// Using Array.join() avoids the overhead from lazy string concatenation.
|
|
|
|
// See http://blog.cdleary.com/2012/01/string-representation-in-spidermonkey/#ropes
|
2015-03-21 06:50:45 +03:00
|
|
|
var arr = [];
|
2012-08-10 10:30:46 +04:00
|
|
|
getHTML(this);
|
2012-09-01 02:56:13 +04:00
|
|
|
return arr.join("");
|
2012-08-10 10:30:46 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
set innerHTML(html) {
|
2015-03-21 06:50:45 +03:00
|
|
|
var parser = new JSDOMParser();
|
|
|
|
var node = parser.parse(html);
|
|
|
|
for (var i = this.childNodes.length; --i >= 0;) {
|
2012-08-10 10:30:46 +04:00
|
|
|
this.childNodes[i].parentNode = null;
|
|
|
|
}
|
|
|
|
this.childNodes = node.childNodes;
|
2015-04-02 23:48:31 +03:00
|
|
|
this.children = node.children;
|
2015-03-21 06:50:45 +03:00
|
|
|
for (var i = this.childNodes.length; --i >= 0;) {
|
2012-08-10 10:30:46 +04:00
|
|
|
this.childNodes[i].parentNode = this;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-08-22 21:15:46 +04:00
|
|
|
set textContent(text) {
|
|
|
|
// clear parentNodes for existing children
|
2015-03-21 06:50:45 +03:00
|
|
|
for (var i = this.childNodes.length; --i >= 0;) {
|
2012-08-22 21:15:46 +04:00
|
|
|
this.childNodes[i].parentNode = null;
|
|
|
|
}
|
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
var node = new Text();
|
2012-08-10 10:30:46 +04:00
|
|
|
this.childNodes = [ node ];
|
2015-04-02 23:48:31 +03:00
|
|
|
this.children = [];
|
2012-08-10 10:30:46 +04:00
|
|
|
node.textContent = text;
|
|
|
|
node.parentNode = this;
|
|
|
|
},
|
|
|
|
|
|
|
|
get textContent() {
|
|
|
|
function getText(node) {
|
2015-03-21 06:50:45 +03:00
|
|
|
var nodes = node.childNodes;
|
|
|
|
for (var i = 0; i < nodes.length; i++) {
|
|
|
|
var child = nodes[i];
|
2013-05-08 06:38:16 +04:00
|
|
|
if (child.nodeType === 3) {
|
2012-09-01 02:56:13 +04:00
|
|
|
text.push(child.textContent);
|
|
|
|
} else {
|
|
|
|
getText(child);
|
|
|
|
}
|
2012-08-10 10:30:46 +04:00
|
|
|
}
|
|
|
|
}
|
2012-09-01 02:56:13 +04:00
|
|
|
|
2012-09-04 22:24:52 +04:00
|
|
|
// Using Array.join() avoids the overhead from lazy string concatenation.
|
|
|
|
// See http://blog.cdleary.com/2012/01/string-representation-in-spidermonkey/#ropes
|
2015-03-21 06:50:45 +03:00
|
|
|
var text = [];
|
2012-08-10 10:30:46 +04:00
|
|
|
getText(this);
|
2012-09-01 02:56:13 +04:00
|
|
|
return text.join("");
|
2012-08-10 10:30:46 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
getAttribute: function (name) {
|
2015-03-21 06:50:45 +03:00
|
|
|
for (var i = this.attributes.length; --i >= 0;) {
|
|
|
|
var attr = this.attributes[i];
|
2013-05-08 06:38:16 +04:00
|
|
|
if (attr.name === name)
|
2015-04-10 05:55:51 +03:00
|
|
|
return attr.value;
|
2012-08-10 10:30:46 +04:00
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
},
|
|
|
|
|
|
|
|
setAttribute: function (name, value) {
|
2015-03-21 06:50:45 +03:00
|
|
|
for (var i = this.attributes.length; --i >= 0;) {
|
|
|
|
var attr = this.attributes[i];
|
2013-05-08 06:38:16 +04:00
|
|
|
if (attr.name === name) {
|
2015-04-10 05:55:51 +03:00
|
|
|
attr.value = value;
|
2012-08-10 10:30:46 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-04-10 05:55:51 +03:00
|
|
|
this.attributes.push(new Attribute(name, value));
|
2012-08-10 10:30:46 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
removeAttribute: function (name) {
|
2015-03-21 06:50:45 +03:00
|
|
|
for (var i = this.attributes.length; --i >= 0;) {
|
|
|
|
var attr = this.attributes[i];
|
2013-05-08 06:38:16 +04:00
|
|
|
if (attr.name === name) {
|
2012-08-10 10:30:46 +04:00
|
|
|
this.attributes.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
var Style = function (node) {
|
2012-08-10 10:30:46 +04:00
|
|
|
this.node = node;
|
|
|
|
};
|
|
|
|
|
|
|
|
// getStyle() and setStyle() use the style attribute string directly. This
|
|
|
|
// won't be very efficient if there are a lot of style manipulations, but
|
|
|
|
// it's the easiest way to make sure the style attribute string and the JS
|
|
|
|
// style property stay in sync. Readability.js doesn't do many style
|
|
|
|
// manipulations, so this should be okay.
|
|
|
|
Style.prototype = {
|
|
|
|
getStyle: function (styleName) {
|
2015-03-21 06:50:45 +03:00
|
|
|
var attr = this.node.getAttribute("style");
|
2012-08-10 10:30:46 +04:00
|
|
|
if (!attr)
|
|
|
|
return undefined;
|
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
var styles = attr.split(";");
|
|
|
|
for (var i = 0; i < styles.length; i++) {
|
|
|
|
var style = styles[i].split(":");
|
|
|
|
var name = style[0].trim();
|
2013-05-08 06:38:16 +04:00
|
|
|
if (name === styleName)
|
2012-08-10 10:30:46 +04:00
|
|
|
return style[1].trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
},
|
|
|
|
|
|
|
|
setStyle: function (styleName, styleValue) {
|
2015-03-21 06:50:45 +03:00
|
|
|
var value = this.node.getAttribute("style") || "";
|
|
|
|
var index = 0;
|
2012-08-10 10:30:46 +04:00
|
|
|
do {
|
2015-03-21 06:50:45 +03:00
|
|
|
var next = value.indexOf(";", index) + 1;
|
|
|
|
var length = next - index - 1;
|
|
|
|
var style = (length > 0 ? value.substr(index, length) : value.substr(index));
|
2013-05-08 06:38:16 +04:00
|
|
|
if (style.substr(0, style.indexOf(":")).trim() === styleName) {
|
2012-08-10 10:30:46 +04:00
|
|
|
value = value.substr(0, index).trim() + (next ? " " + value.substr(next).trim() : "");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
index = next;
|
|
|
|
} while (index);
|
|
|
|
|
|
|
|
value += " " + styleName + ": " + styleValue + ";";
|
|
|
|
this.node.setAttribute("style", value.trim());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// For each item in styleMap, define a getter and setter on the style
|
|
|
|
// property.
|
2015-03-21 06:50:45 +03:00
|
|
|
for (var jsName in styleMap) {
|
2012-08-10 10:30:46 +04:00
|
|
|
(function (cssName) {
|
|
|
|
Style.prototype.__defineGetter__(jsName, function () {
|
|
|
|
return this.getStyle(cssName);
|
|
|
|
});
|
|
|
|
Style.prototype.__defineSetter__(jsName, function (value) {
|
|
|
|
this.setStyle(cssName, value);
|
|
|
|
});
|
|
|
|
}) (styleMap[jsName]);
|
|
|
|
}
|
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
var JSDOMParser = function () {
|
2012-08-10 10:30:46 +04:00
|
|
|
this.currentChar = 0;
|
2015-03-17 01:13:14 +03:00
|
|
|
|
|
|
|
// In makeElementNode() we build up many strings one char at a time. Using
|
|
|
|
// += for this results in lots of short-lived intermediate strings. It's
|
|
|
|
// better to build an array of single-char strings and then join() them
|
|
|
|
// together at the end. And reusing a single array (i.e. |this.strBuf|)
|
|
|
|
// over and over for this purpose uses less memory than using a new array
|
|
|
|
// for each string.
|
|
|
|
this.strBuf = [];
|
2015-03-17 01:13:16 +03:00
|
|
|
|
|
|
|
// Similarly, we reuse this array to return the two arguments from
|
|
|
|
// makeElementNode(), which saves us from having to allocate a new array
|
|
|
|
// every time.
|
|
|
|
this.retPair = [];
|
2012-08-10 10:30:46 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
JSDOMParser.prototype = {
|
|
|
|
/**
|
|
|
|
* Look at the next character without advancing the index.
|
|
|
|
*/
|
|
|
|
peekNext: function () {
|
|
|
|
return this.html[this.currentChar];
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next character and advance the index.
|
|
|
|
*/
|
|
|
|
nextChar: function () {
|
|
|
|
return this.html[this.currentChar++];
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called after a quote character is read. This finds the next quote
|
|
|
|
* character and returns the text string in between.
|
|
|
|
*/
|
|
|
|
readString: function (quote) {
|
2015-03-21 06:50:45 +03:00
|
|
|
var str;
|
|
|
|
var n = this.html.indexOf(quote, this.currentChar);
|
2013-05-08 06:38:16 +04:00
|
|
|
if (n === -1) {
|
2012-08-10 10:30:46 +04:00
|
|
|
this.currentChar = this.html.length;
|
|
|
|
str = null;
|
|
|
|
} else {
|
|
|
|
str = this.html.substring(this.currentChar, n);
|
|
|
|
this.currentChar = n + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when parsing a node. This finds the next name/value attribute
|
|
|
|
* pair and adds the result to the attributes list.
|
|
|
|
*/
|
|
|
|
readAttribute: function (node) {
|
2015-03-21 06:50:45 +03:00
|
|
|
var name = "";
|
2012-08-10 10:30:46 +04:00
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
var n = this.html.indexOf("=", this.currentChar);
|
2013-05-08 06:38:16 +04:00
|
|
|
if (n === -1) {
|
2012-08-10 10:30:46 +04:00
|
|
|
this.currentChar = this.html.length;
|
|
|
|
} else {
|
|
|
|
// Read until a '=' character is hit; this will be the attribute key
|
|
|
|
name = this.html.substring(this.currentChar, n);
|
|
|
|
this.currentChar = n + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// After a '=', we should see a '"' for the attribute value
|
2015-03-21 06:50:45 +03:00
|
|
|
var c = this.nextChar();
|
2013-05-08 06:38:16 +04:00
|
|
|
if (c !== '"' && c !== "'") {
|
2015-03-21 06:50:45 +03:00
|
|
|
error("Error reading attribute " + name + ", expecting '\"'");
|
2012-08-10 10:30:46 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the attribute value (and consume the matching quote)
|
2015-03-21 06:50:45 +03:00
|
|
|
var value = this.readString(c);
|
2012-08-10 10:30:46 +04:00
|
|
|
|
2015-04-10 05:55:51 +03:00
|
|
|
if (!value)
|
|
|
|
return;
|
|
|
|
|
2012-08-10 10:30:46 +04:00
|
|
|
node.attributes.push(new Attribute(name, value));
|
|
|
|
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses and returns an Element node. This is called after a '<' has been
|
|
|
|
* read.
|
|
|
|
*
|
|
|
|
* @returns an array; the first index of the array is the parsed node;
|
|
|
|
* the second index is a boolean indicating whether this is a void
|
|
|
|
* Element
|
|
|
|
*/
|
2015-03-17 01:13:16 +03:00
|
|
|
makeElementNode: function (retPair) {
|
2015-03-21 06:50:45 +03:00
|
|
|
var c = this.nextChar();
|
2012-08-10 10:30:46 +04:00
|
|
|
|
|
|
|
// Read the Element tag name
|
2015-03-21 06:50:45 +03:00
|
|
|
var strBuf = this.strBuf;
|
2015-03-17 01:13:14 +03:00
|
|
|
strBuf.length = 0;
|
2015-03-21 06:50:45 +03:00
|
|
|
while (whitespace.indexOf(c) == -1 && c !== ">" && c !== "/") {
|
2012-08-10 10:30:46 +04:00
|
|
|
if (c === undefined)
|
2015-03-17 01:13:16 +03:00
|
|
|
return false;
|
2015-03-17 01:13:14 +03:00
|
|
|
strBuf.push(c);
|
2012-08-10 10:30:46 +04:00
|
|
|
c = this.nextChar();
|
|
|
|
}
|
2015-03-21 06:50:45 +03:00
|
|
|
var tag = strBuf.join('');
|
2012-08-10 10:30:46 +04:00
|
|
|
|
|
|
|
if (!tag)
|
2015-03-17 01:13:16 +03:00
|
|
|
return false;
|
2012-08-10 10:30:46 +04:00
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
var node = new Element(tag);
|
2012-08-10 10:30:46 +04:00
|
|
|
|
|
|
|
// Read Element attributes
|
2013-05-08 06:38:16 +04:00
|
|
|
while (c !== "/" && c !== ">") {
|
2012-08-10 10:30:46 +04:00
|
|
|
if (c === undefined)
|
2015-03-17 01:13:16 +03:00
|
|
|
return false;
|
2015-03-21 06:50:45 +03:00
|
|
|
while (whitespace.indexOf(this.html[this.currentChar++]) != -1);
|
|
|
|
this.currentChar--;
|
2012-08-10 10:30:46 +04:00
|
|
|
c = this.nextChar();
|
2013-05-08 06:38:16 +04:00
|
|
|
if (c !== "/" && c !== ">") {
|
2012-08-10 10:30:46 +04:00
|
|
|
--this.currentChar;
|
|
|
|
this.readAttribute(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a self-closing tag, read '/>'
|
2015-03-21 06:50:45 +03:00
|
|
|
var closed = tag in voidElems;
|
2013-05-08 06:38:16 +04:00
|
|
|
if (c === "/") {
|
2012-08-10 10:30:46 +04:00
|
|
|
closed = true;
|
|
|
|
c = this.nextChar();
|
2013-05-08 06:38:16 +04:00
|
|
|
if (c !== ">") {
|
2015-03-21 06:50:45 +03:00
|
|
|
error("expected '>' to close " + tag);
|
2015-03-17 01:13:16 +03:00
|
|
|
return false;
|
2012-08-10 10:30:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 01:13:16 +03:00
|
|
|
retPair[0] = node;
|
|
|
|
retPair[1] = closed;
|
|
|
|
return true
|
2012-08-10 10:30:46 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the current input matches this string, advance the input index;
|
|
|
|
* otherwise, do nothing.
|
|
|
|
*
|
|
|
|
* @returns whether input matched string
|
|
|
|
*/
|
|
|
|
match: function (str) {
|
2015-03-21 06:50:45 +03:00
|
|
|
var strlen = str.length;
|
2015-04-10 05:55:51 +03:00
|
|
|
if (this.html.substr(this.currentChar, strlen) === str) {
|
2012-08-10 10:30:46 +04:00
|
|
|
this.currentChar += strlen;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Searches the input until a string is found and discards all input up to
|
|
|
|
* and including the matched string.
|
|
|
|
*/
|
|
|
|
discardTo: function (str) {
|
2015-03-21 06:50:45 +03:00
|
|
|
var index = this.html.indexOf(str, this.currentChar) + str.length;
|
2013-05-08 06:38:16 +04:00
|
|
|
if (index === -1)
|
2012-08-10 10:30:46 +04:00
|
|
|
this.currentChar = this.html.length;
|
|
|
|
this.currentChar = index;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads child nodes for the given node.
|
|
|
|
*/
|
|
|
|
readChildren: function (node) {
|
2015-03-21 06:50:45 +03:00
|
|
|
var child;
|
2014-01-11 03:28:51 +04:00
|
|
|
while ((child = this.readNode())) {
|
2012-08-10 10:30:46 +04:00
|
|
|
// Don't keep Comment nodes
|
2013-05-08 06:38:16 +04:00
|
|
|
if (child.nodeType !== 8) {
|
2015-03-21 06:50:45 +03:00
|
|
|
node.appendChild(child);
|
2012-08-10 10:30:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
readScript: function (node) {
|
2015-04-02 23:48:31 +03:00
|
|
|
while (this.currentChar < this.html.length) {
|
|
|
|
var c = this.nextChar();
|
|
|
|
var nextC = this.peekNext();
|
|
|
|
if (c === "<") {
|
|
|
|
if (nextC === "!" || nextC === "?") {
|
|
|
|
// We're still before the ! or ? that is starting this comment:
|
|
|
|
this.currentChar++;
|
|
|
|
node.appendChild(this.discardNextComment());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (nextC === "/" && this.html.substr(this.currentChar, 8 /*"/script>".length */).toLowerCase() == "/script>") {
|
|
|
|
// Go back before the '<' so we find the end tag.
|
|
|
|
this.currentChar--;
|
|
|
|
// Done with this script tag, the caller will close:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Either c wasn't a '<' or it was but we couldn't find either a comment
|
|
|
|
// or a closing script tag, so we should just parse as text until the next one
|
|
|
|
// comes along:
|
|
|
|
|
|
|
|
var haveTextNode = node.lastChild && node.lastChild.nodeType === Node.TEXT_NODE;
|
|
|
|
var textNode = haveTextNode ? node.lastChild : new Text();
|
|
|
|
var n = this.html.indexOf("<", this.currentChar);
|
|
|
|
// Decrement this to include the current character *afterwards* so we don't get stuck
|
|
|
|
// looking for the same < all the time.
|
|
|
|
this.currentChar--;
|
|
|
|
if (n === -1) {
|
2015-04-10 05:55:51 +03:00
|
|
|
textNode.textContent += this.html.substring(this.currentChar, this.html.length);
|
2015-04-02 23:48:31 +03:00
|
|
|
this.currentChar = this.html.length;
|
|
|
|
} else {
|
2015-04-10 05:55:51 +03:00
|
|
|
textNode.textContent += this.html.substring(this.currentChar, n);
|
2015-04-02 23:48:31 +03:00
|
|
|
this.currentChar = n;
|
|
|
|
}
|
|
|
|
if (!haveTextNode)
|
|
|
|
node.appendChild(textNode);
|
2015-03-21 06:50:45 +03:00
|
|
|
}
|
2015-04-02 23:48:31 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
discardNextComment: function() {
|
|
|
|
if (this.match("--")) {
|
|
|
|
this.discardTo("-->");
|
|
|
|
} else {
|
|
|
|
var c = this.nextChar();
|
|
|
|
while (c !== ">") {
|
|
|
|
if (c === undefined)
|
|
|
|
return null;
|
|
|
|
if (c === '"' || c === "'")
|
|
|
|
this.readString(c);
|
|
|
|
c = this.nextChar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new Comment();
|
2015-03-21 06:50:45 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
2012-08-10 10:30:46 +04:00
|
|
|
/**
|
|
|
|
* Reads the next child node from the input. If we're reading a closing
|
|
|
|
* tag, or if we've reached the end of input, return null.
|
|
|
|
*
|
|
|
|
* @returns the node
|
|
|
|
*/
|
|
|
|
readNode: function () {
|
2015-03-21 06:50:45 +03:00
|
|
|
var c = this.nextChar();
|
2015-04-10 05:55:51 +03:00
|
|
|
|
2012-08-10 10:30:46 +04:00
|
|
|
if (c === undefined)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
// Read any text as Text node
|
2013-05-08 06:38:16 +04:00
|
|
|
if (c !== "<") {
|
2012-08-10 10:30:46 +04:00
|
|
|
--this.currentChar;
|
2015-03-21 06:50:45 +03:00
|
|
|
var node = new Text();
|
|
|
|
var n = this.html.indexOf("<", this.currentChar);
|
2013-05-08 06:38:16 +04:00
|
|
|
if (n === -1) {
|
2015-04-10 05:55:51 +03:00
|
|
|
node.textContent = this.html.substring(this.currentChar, this.html.length);
|
2012-08-10 10:30:46 +04:00
|
|
|
this.currentChar = this.html.length;
|
|
|
|
} else {
|
2015-04-10 05:55:51 +03:00
|
|
|
node.textContent = this.html.substring(this.currentChar, n);
|
2012-08-10 10:30:46 +04:00
|
|
|
this.currentChar = n;
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = this.peekNext();
|
|
|
|
|
|
|
|
// Read Comment node. Normally, Comment nodes know their inner
|
|
|
|
// textContent, but we don't really care about Comment nodes (we throw
|
|
|
|
// them away in readChildren()). So just returning an empty Comment node
|
|
|
|
// here is sufficient.
|
2013-05-08 06:38:16 +04:00
|
|
|
if (c === "!" || c === "?") {
|
2015-04-02 23:48:31 +03:00
|
|
|
// We're still before the ! or ? that is starting this comment:
|
2012-08-10 10:30:46 +04:00
|
|
|
this.currentChar++;
|
2015-04-02 23:48:31 +03:00
|
|
|
return this.discardNextComment();
|
2012-08-10 10:30:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we're reading a closing tag, return null. This means we've reached
|
|
|
|
// the end of this set of child nodes.
|
2013-05-08 06:38:16 +04:00
|
|
|
if (c === "/") {
|
2012-08-10 10:30:46 +04:00
|
|
|
--this.currentChar;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we're looking at an Element node
|
2015-03-21 06:50:45 +03:00
|
|
|
var result = this.makeElementNode(this.retPair);
|
2015-03-17 01:13:16 +03:00
|
|
|
if (!result)
|
2012-08-10 10:30:46 +04:00
|
|
|
return null;
|
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
var node = this.retPair[0];
|
|
|
|
var closed = this.retPair[1];
|
|
|
|
var localName = node.localName;
|
2012-08-10 10:30:46 +04:00
|
|
|
|
|
|
|
// If this isn't a void Element, read its child nodes
|
|
|
|
if (!closed) {
|
2015-03-21 06:50:45 +03:00
|
|
|
if (localName == "script") {
|
|
|
|
this.readScript(node);
|
|
|
|
} else {
|
|
|
|
this.readChildren(node);
|
|
|
|
}
|
|
|
|
var closingTag = "</" + localName + ">";
|
2012-08-10 10:30:46 +04:00
|
|
|
if (!this.match(closingTag)) {
|
|
|
|
error("expected '" + closingTag + "'");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-21 06:50:45 +03:00
|
|
|
// Only use the first title, because SVG might have other
|
|
|
|
// title elements which we don't care about (medium.com
|
|
|
|
// does this, at least).
|
|
|
|
if (localName === "title" && !this.doc.title) {
|
2012-08-10 10:30:46 +04:00
|
|
|
this.doc.title = node.textContent.trim();
|
2013-05-08 06:38:16 +04:00
|
|
|
} else if (localName === "head") {
|
2012-08-10 10:30:46 +04:00
|
|
|
this.doc.head = node;
|
2013-05-08 06:38:16 +04:00
|
|
|
} else if (localName === "body") {
|
2012-08-10 10:30:46 +04:00
|
|
|
this.doc.body = node;
|
2013-05-08 06:38:16 +04:00
|
|
|
} else if (localName === "html") {
|
2012-08-10 10:30:46 +04:00
|
|
|
this.doc.documentElement = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses an HTML string and returns a JS implementation of the Document.
|
|
|
|
*/
|
|
|
|
parse: function (html) {
|
|
|
|
this.html = html;
|
2015-03-21 06:50:45 +03:00
|
|
|
var doc = this.doc = new Document();
|
2012-08-10 10:30:46 +04:00
|
|
|
this.readChildren(doc);
|
|
|
|
|
|
|
|
// If this is an HTML document, remove root-level children except for the
|
|
|
|
// <html> node
|
|
|
|
if (doc.documentElement) {
|
2015-03-21 06:50:45 +03:00
|
|
|
for (var i = doc.childNodes.length; --i >= 0;) {
|
|
|
|
var child = doc.childNodes[i];
|
2013-05-08 06:38:16 +04:00
|
|
|
if (child !== doc.documentElement) {
|
2012-08-10 10:30:46 +04:00
|
|
|
doc.removeChild(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Attach the standard DOM types to the global scope
|
|
|
|
global.Node = Node;
|
|
|
|
global.Comment = Comment;
|
|
|
|
global.Document = Document;
|
|
|
|
global.Element = Element;
|
|
|
|
global.Text = Text;
|
|
|
|
|
|
|
|
// Attach JSDOMParser to the global scope
|
|
|
|
global.JSDOMParser = JSDOMParser;
|
|
|
|
|
|
|
|
}) (this);
|