Added logging, changed some directory structure

This commit is contained in:
2018-01-13 21:33:40 -05:00
parent f079a5f067
commit 8e72ffb917
73656 changed files with 35284 additions and 53718 deletions

View File

@@ -0,0 +1,167 @@
'use strict';
//Const
var NOAH_ARK_CAPACITY = 3;
//List of formatting elements
var FormattingElementList = module.exports = function (treeAdapter) {
this.length = 0;
this.entries = [];
this.treeAdapter = treeAdapter;
this.bookmark = null;
};
//Entry types
FormattingElementList.MARKER_ENTRY = 'MARKER_ENTRY';
FormattingElementList.ELEMENT_ENTRY = 'ELEMENT_ENTRY';
//Noah Ark's condition
//OPTIMIZATION: at first we try to find possible candidates for exclusion using
//lightweight heuristics without thorough attributes check.
FormattingElementList.prototype._getNoahArkConditionCandidates = function (newElement) {
var candidates = [];
if (this.length >= NOAH_ARK_CAPACITY) {
var neAttrsLength = this.treeAdapter.getAttrList(newElement).length,
neTagName = this.treeAdapter.getTagName(newElement),
neNamespaceURI = this.treeAdapter.getNamespaceURI(newElement);
for (var i = this.length - 1; i >= 0; i--) {
var entry = this.entries[i];
if (entry.type === FormattingElementList.MARKER_ENTRY)
break;
var element = entry.element,
elementAttrs = this.treeAdapter.getAttrList(element);
if (this.treeAdapter.getTagName(element) === neTagName &&
this.treeAdapter.getNamespaceURI(element) === neNamespaceURI &&
elementAttrs.length === neAttrsLength) {
candidates.push({idx: i, attrs: elementAttrs});
}
}
}
return candidates.length < NOAH_ARK_CAPACITY ? [] : candidates;
};
FormattingElementList.prototype._ensureNoahArkCondition = function (newElement) {
var candidates = this._getNoahArkConditionCandidates(newElement),
cLength = candidates.length;
if (cLength) {
var neAttrs = this.treeAdapter.getAttrList(newElement),
neAttrsLength = neAttrs.length,
neAttrsMap = {};
//NOTE: build attrs map for the new element so we can perform fast lookups
for (var i = 0; i < neAttrsLength; i++) {
var neAttr = neAttrs[i];
neAttrsMap[neAttr.name] = neAttr.value;
}
for (var i = 0; i < neAttrsLength; i++) {
for (var j = 0; j < cLength; j++) {
var cAttr = candidates[j].attrs[i];
if (neAttrsMap[cAttr.name] !== cAttr.value) {
candidates.splice(j, 1);
cLength--;
}
if (candidates.length < NOAH_ARK_CAPACITY)
return;
}
}
//NOTE: remove bottommost candidates until Noah's Ark condition will not be met
for (var i = cLength - 1; i >= NOAH_ARK_CAPACITY - 1; i--) {
this.entries.splice(candidates[i].idx, 1);
this.length--;
}
}
};
//Mutations
FormattingElementList.prototype.insertMarker = function () {
this.entries.push({type: FormattingElementList.MARKER_ENTRY});
this.length++;
};
FormattingElementList.prototype.pushElement = function (element, token) {
this._ensureNoahArkCondition(element);
this.entries.push({
type: FormattingElementList.ELEMENT_ENTRY,
element: element,
token: token
});
this.length++;
};
FormattingElementList.prototype.insertElementAfterBookmark = function (element, token) {
var bookmarkIdx = this.length - 1;
for (; bookmarkIdx >= 0; bookmarkIdx--) {
if (this.entries[bookmarkIdx] === this.bookmark)
break;
}
this.entries.splice(bookmarkIdx + 1, 0, {
type: FormattingElementList.ELEMENT_ENTRY,
element: element,
token: token
});
this.length++;
};
FormattingElementList.prototype.removeEntry = function (entry) {
for (var i = this.length - 1; i >= 0; i--) {
if (this.entries[i] === entry) {
this.entries.splice(i, 1);
this.length--;
break;
}
}
};
FormattingElementList.prototype.clearToLastMarker = function () {
while (this.length) {
var entry = this.entries.pop();
this.length--;
if (entry.type === FormattingElementList.MARKER_ENTRY)
break;
}
};
//Search
FormattingElementList.prototype.getElementEntryInScopeWithTagName = function (tagName) {
for (var i = this.length - 1; i >= 0; i--) {
var entry = this.entries[i];
if (entry.type === FormattingElementList.MARKER_ENTRY)
return null;
if (this.treeAdapter.getTagName(entry.element) === tagName)
return entry;
}
return null;
};
FormattingElementList.prototype.getElementEntry = function (element) {
for (var i = this.length - 1; i >= 0; i--) {
var entry = this.entries[i];
if (entry.type === FormattingElementList.ELEMENT_ENTRY && entry.element == element)
return entry;
}
return null;
};

View File

@@ -0,0 +1,197 @@
'use strict';
var OpenElementStack = require('./open_element_stack'),
Tokenizer = require('../tokenization/tokenizer'),
HTML = require('../common/html');
//Aliases
var $ = HTML.TAG_NAMES;
function setEndLocation(element, closingToken, treeAdapter) {
var loc = element.__location;
if (!loc)
return;
if (!loc.startTag) {
loc.startTag = {
start: loc.start,
end: loc.end
};
}
if (closingToken.location) {
var tn = treeAdapter.getTagName(element),
// NOTE: For cases like <p> <p> </p> - First 'p' closes without a closing tag and
// for cases like <td> <p> </td> - 'p' closes without a closing tag
isClosingEndTag = closingToken.type === Tokenizer.END_TAG_TOKEN &&
tn === closingToken.tagName;
if (isClosingEndTag) {
loc.endTag = {
start: closingToken.location.start,
end: closingToken.location.end
};
}
loc.end = closingToken.location.end;
}
}
//NOTE: patch open elements stack, so we can assign end location for the elements
function patchOpenElementsStack(stack, parser) {
var treeAdapter = parser.treeAdapter;
stack.pop = function () {
setEndLocation(this.current, parser.currentToken, treeAdapter);
OpenElementStack.prototype.pop.call(this);
};
stack.popAllUpToHtmlElement = function () {
for (var i = this.stackTop; i > 0; i--)
setEndLocation(this.items[i], parser.currentToken, treeAdapter);
OpenElementStack.prototype.popAllUpToHtmlElement.call(this);
};
stack.remove = function (element) {
setEndLocation(element, parser.currentToken, treeAdapter);
OpenElementStack.prototype.remove.call(this, element);
};
}
exports.assign = function (parser) {
//NOTE: obtain Parser proto this way to avoid module circular references
var parserProto = Object.getPrototypeOf(parser),
treeAdapter = parser.treeAdapter;
//NOTE: patch _reset method
parser._reset = function (html, document, fragmentContext) {
parserProto._reset.call(this, html, document, fragmentContext);
this.attachableElementLocation = null;
this.lastFosterParentingLocation = null;
this.currentToken = null;
patchOpenElementsStack(this.openElements, parser);
};
parser._processTokenInForeignContent = function (token) {
this.currentToken = token;
parserProto._processTokenInForeignContent.call(this, token);
};
parser._processToken = function (token) {
this.currentToken = token;
parserProto._processToken.call(this, token);
//NOTE: <body> and <html> are never popped from the stack, so we need to updated
//their end location explicitly.
if (token.type === Tokenizer.END_TAG_TOKEN &&
(token.tagName === $.HTML ||
(token.tagName === $.BODY && this.openElements.hasInScope($.BODY)))) {
for (var i = this.openElements.stackTop; i >= 0; i--) {
var element = this.openElements.items[i];
if (this.treeAdapter.getTagName(element) === token.tagName) {
setEndLocation(element, token, treeAdapter);
break;
}
}
}
};
//Doctype
parser._setDocumentType = function (token) {
parserProto._setDocumentType.call(this, token);
var documentChildren = this.treeAdapter.getChildNodes(this.document),
cnLength = documentChildren.length;
for (var i = 0; i < cnLength; i++) {
var node = documentChildren[i];
if (this.treeAdapter.isDocumentTypeNode(node)) {
node.__location = token.location;
break;
}
}
};
//Elements
parser._attachElementToTree = function (element) {
//NOTE: _attachElementToTree is called from _appendElement, _insertElement and _insertTemplate methods.
//So we will use token location stored in this methods for the element.
element.__location = this.attachableElementLocation || null;
this.attachableElementLocation = null;
parserProto._attachElementToTree.call(this, element);
};
parser._appendElement = function (token, namespaceURI) {
this.attachableElementLocation = token.location;
parserProto._appendElement.call(this, token, namespaceURI);
};
parser._insertElement = function (token, namespaceURI) {
this.attachableElementLocation = token.location;
parserProto._insertElement.call(this, token, namespaceURI);
};
parser._insertTemplate = function (token) {
this.attachableElementLocation = token.location;
parserProto._insertTemplate.call(this, token);
var tmplContent = this.treeAdapter.getChildNodes(this.openElements.current)[0];
tmplContent.__location = null;
};
parser._insertFakeRootElement = function () {
parserProto._insertFakeRootElement.call(this);
this.openElements.current.__location = null;
};
//Comments
parser._appendCommentNode = function (token, parent) {
parserProto._appendCommentNode.call(this, token, parent);
var children = this.treeAdapter.getChildNodes(parent),
commentNode = children[children.length - 1];
commentNode.__location = token.location;
};
//Text
parser._findFosterParentingLocation = function () {
//NOTE: store last foster parenting location, so we will be able to find inserted text
//in case of foster parenting
this.lastFosterParentingLocation = parserProto._findFosterParentingLocation.call(this);
return this.lastFosterParentingLocation;
};
parser._insertCharacters = function (token) {
parserProto._insertCharacters.call(this, token);
var hasFosterParent = this._shouldFosterParentOnInsertion(),
parentingLocation = this.lastFosterParentingLocation,
parent = (hasFosterParent && parentingLocation.parent) ||
this.openElements.currentTmplContent ||
this.openElements.current,
siblings = this.treeAdapter.getChildNodes(parent),
textNodeIdx = hasFosterParent && parentingLocation.beforeElement ?
siblings.indexOf(parentingLocation.beforeElement) - 1 :
siblings.length - 1,
textNode = siblings[textNodeIdx];
//NOTE: if we have location assigned by another token, then just update end position
if (textNode.__location)
textNode.__location.end = token.location.end;
else
textNode.__location = token.location;
};
};

View File

@@ -0,0 +1,379 @@
'use strict';
var HTML = require('../common/html');
//Aliases
var $ = HTML.TAG_NAMES,
NS = HTML.NAMESPACES;
//Element utils
//OPTIMIZATION: Integer comparisons are low-cost, so we can use very fast tag name length filters here.
//It's faster than using dictionary.
function isImpliedEndTagRequired(tn) {
switch (tn.length) {
case 1:
return tn === $.P;
case 2:
return tn === $.RP || tn === $.RT || tn === $.DD || tn === $.DT || tn === $.LI;
case 6:
return tn === $.OPTION;
case 8:
return tn === $.OPTGROUP;
}
return false;
}
function isScopingElement(tn, ns) {
switch (tn.length) {
case 2:
if (tn === $.TD || tn === $.TH)
return ns === NS.HTML;
else if (tn === $.MI || tn === $.MO || tn == $.MN || tn === $.MS)
return ns === NS.MATHML;
break;
case 4:
if (tn === $.HTML)
return ns === NS.HTML;
else if (tn === $.DESC)
return ns === NS.SVG;
break;
case 5:
if (tn === $.TABLE)
return ns === NS.HTML;
else if (tn === $.MTEXT)
return ns === NS.MATHML;
else if (tn === $.TITLE)
return ns === NS.SVG;
break;
case 6:
return (tn === $.APPLET || tn === $.OBJECT) && ns === NS.HTML;
case 7:
return (tn === $.CAPTION || tn === $.MARQUEE) && ns === NS.HTML;
case 8:
return tn === $.TEMPLATE && ns === NS.HTML;
case 13:
return tn === $.FOREIGN_OBJECT && ns === NS.SVG;
case 14:
return tn === $.ANNOTATION_XML && ns === NS.MATHML;
}
return false;
}
//Stack of open elements
var OpenElementStack = module.exports = function (document, treeAdapter) {
this.stackTop = -1;
this.items = [];
this.current = document;
this.currentTagName = null;
this.currentTmplContent = null;
this.tmplCount = 0;
this.treeAdapter = treeAdapter;
};
//Index of element
OpenElementStack.prototype._indexOf = function (element) {
var idx = -1;
for (var i = this.stackTop; i >= 0; i--) {
if (this.items[i] === element) {
idx = i;
break;
}
}
return idx;
};
//Update current element
OpenElementStack.prototype._isInTemplate = function () {
if (this.currentTagName !== $.TEMPLATE)
return false;
return this.treeAdapter.getNamespaceURI(this.current) === NS.HTML;
};
OpenElementStack.prototype._updateCurrentElement = function () {
this.current = this.items[this.stackTop];
this.currentTagName = this.current && this.treeAdapter.getTagName(this.current);
this.currentTmplContent = this._isInTemplate() ? this.treeAdapter.getChildNodes(this.current)[0] : null;
};
//Mutations
OpenElementStack.prototype.push = function (element) {
this.items[++this.stackTop] = element;
this._updateCurrentElement();
if (this._isInTemplate())
this.tmplCount++;
};
OpenElementStack.prototype.pop = function () {
this.stackTop--;
if (this.tmplCount > 0 && this._isInTemplate())
this.tmplCount--;
this._updateCurrentElement();
};
OpenElementStack.prototype.replace = function (oldElement, newElement) {
var idx = this._indexOf(oldElement);
this.items[idx] = newElement;
if (idx === this.stackTop)
this._updateCurrentElement();
};
OpenElementStack.prototype.insertAfter = function (referenceElement, newElement) {
var insertionIdx = this._indexOf(referenceElement) + 1;
this.items.splice(insertionIdx, 0, newElement);
if (insertionIdx == ++this.stackTop)
this._updateCurrentElement();
};
OpenElementStack.prototype.popUntilTagNamePopped = function (tagName) {
while (this.stackTop > -1) {
var tn = this.currentTagName;
this.pop();
if (tn === tagName)
break;
}
};
OpenElementStack.prototype.popUntilTemplatePopped = function () {
while (this.stackTop > -1) {
var tn = this.currentTagName,
ns = this.treeAdapter.getNamespaceURI(this.current);
this.pop();
if (tn === $.TEMPLATE && ns === NS.HTML)
break;
}
};
OpenElementStack.prototype.popUntilElementPopped = function (element) {
while (this.stackTop > -1) {
var poppedElement = this.current;
this.pop();
if (poppedElement === element)
break;
}
};
OpenElementStack.prototype.popUntilNumberedHeaderPopped = function () {
while (this.stackTop > -1) {
var tn = this.currentTagName;
this.pop();
if (tn === $.H1 || tn === $.H2 || tn === $.H3 || tn === $.H4 || tn === $.H5 || tn === $.H6)
break;
}
};
OpenElementStack.prototype.popAllUpToHtmlElement = function () {
//NOTE: here we assume that root <html> element is always first in the open element stack, so
//we perform this fast stack clean up.
this.stackTop = 0;
this._updateCurrentElement();
};
OpenElementStack.prototype.clearBackToTableContext = function () {
while (this.currentTagName !== $.TABLE && this.currentTagName !== $.TEMPLATE && this.currentTagName !== $.HTML)
this.pop();
};
OpenElementStack.prototype.clearBackToTableBodyContext = function () {
while (this.currentTagName !== $.TBODY && this.currentTagName !== $.TFOOT &&
this.currentTagName !== $.THEAD && this.currentTagName !== $.TEMPLATE &&
this.currentTagName !== $.HTML) {
this.pop();
}
};
OpenElementStack.prototype.clearBackToTableRowContext = function () {
while (this.currentTagName !== $.TR && this.currentTagName !== $.TEMPLATE && this.currentTagName !== $.HTML)
this.pop();
};
OpenElementStack.prototype.remove = function (element) {
for (var i = this.stackTop; i >= 0; i--) {
if (this.items[i] === element) {
this.items.splice(i, 1);
this.stackTop--;
this._updateCurrentElement();
break;
}
}
};
//Search
OpenElementStack.prototype.tryPeekProperlyNestedBodyElement = function () {
//Properly nested <body> element (should be second element in stack).
var element = this.items[1];
return element && this.treeAdapter.getTagName(element) === $.BODY ? element : null;
};
OpenElementStack.prototype.contains = function (element) {
return this._indexOf(element) > -1;
};
OpenElementStack.prototype.getCommonAncestor = function (element) {
var elementIdx = this._indexOf(element);
return --elementIdx >= 0 ? this.items[elementIdx] : null;
};
OpenElementStack.prototype.isRootHtmlElementCurrent = function () {
return this.stackTop === 0 && this.currentTagName === $.HTML;
};
//Element in scope
OpenElementStack.prototype.hasInScope = function (tagName) {
for (var i = this.stackTop; i >= 0; i--) {
var tn = this.treeAdapter.getTagName(this.items[i]);
if (tn === tagName)
return true;
var ns = this.treeAdapter.getNamespaceURI(this.items[i]);
if (isScopingElement(tn, ns))
return false;
}
return true;
};
OpenElementStack.prototype.hasNumberedHeaderInScope = function () {
for (var i = this.stackTop; i >= 0; i--) {
var tn = this.treeAdapter.getTagName(this.items[i]);
if (tn === $.H1 || tn === $.H2 || tn === $.H3 || tn === $.H4 || tn === $.H5 || tn === $.H6)
return true;
if (isScopingElement(tn, this.treeAdapter.getNamespaceURI(this.items[i])))
return false;
}
return true;
};
OpenElementStack.prototype.hasInListItemScope = function (tagName) {
for (var i = this.stackTop; i >= 0; i--) {
var tn = this.treeAdapter.getTagName(this.items[i]);
if (tn === tagName)
return true;
var ns = this.treeAdapter.getNamespaceURI(this.items[i]);
if (((tn === $.UL || tn === $.OL) && ns === NS.HTML) || isScopingElement(tn, ns))
return false;
}
return true;
};
OpenElementStack.prototype.hasInButtonScope = function (tagName) {
for (var i = this.stackTop; i >= 0; i--) {
var tn = this.treeAdapter.getTagName(this.items[i]);
if (tn === tagName)
return true;
var ns = this.treeAdapter.getNamespaceURI(this.items[i]);
if ((tn === $.BUTTON && ns === NS.HTML) || isScopingElement(tn, ns))
return false;
}
return true;
};
OpenElementStack.prototype.hasInTableScope = function (tagName) {
for (var i = this.stackTop; i >= 0; i--) {
var tn = this.treeAdapter.getTagName(this.items[i]);
if (tn === tagName)
return true;
var ns = this.treeAdapter.getNamespaceURI(this.items[i]);
if ((tn === $.TABLE || tn === $.TEMPLATE || tn === $.HTML) && ns === NS.HTML)
return false;
}
return true;
};
OpenElementStack.prototype.hasTableBodyContextInTableScope = function () {
for (var i = this.stackTop; i >= 0; i--) {
var tn = this.treeAdapter.getTagName(this.items[i]);
if (tn === $.TBODY || tn === $.THEAD || tn === $.TFOOT)
return true;
var ns = this.treeAdapter.getNamespaceURI(this.items[i]);
if ((tn === $.TABLE || tn === $.HTML) && ns === NS.HTML)
return false;
}
return true;
};
OpenElementStack.prototype.hasInSelectScope = function (tagName) {
for (var i = this.stackTop; i >= 0; i--) {
var tn = this.treeAdapter.getTagName(this.items[i]);
if (tn === tagName)
return true;
var ns = this.treeAdapter.getNamespaceURI(this.items[i]);
if (tn !== $.OPTION && tn !== $.OPTGROUP && ns === NS.HTML)
return false;
}
return true;
};
//Implied end tags
OpenElementStack.prototype.generateImpliedEndTags = function () {
while (isImpliedEndTagRequired(this.currentTagName))
this.pop();
};
OpenElementStack.prototype.generateImpliedEndTagsWithExclusion = function (exclusionTagName) {
while (isImpliedEndTagRequired(this.currentTagName) && this.currentTagName !== exclusionTagName)
this.pop();
};

File diff suppressed because it is too large Load Diff