123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- '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 === $.RB || tn === $.RP || tn === $.RT || tn === $.DD || tn === $.DT || tn === $.LI;
- case 3:
- return tn === $.RTC;
- case 6:
- return tn === $.OPTION;
- case 8:
- return tn === $.OPTGROUP || tn === $.MENUITEM;
- }
- 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 () {
- return this.currentTagName === $.TEMPLATE && 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.getTemplateContent(this.current) : 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,
- ns = this.treeAdapter.getNamespaceURI(this.current);
- this.pop();
- if (tn === tagName && 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,
- ns = this.treeAdapter.getNamespaceURI(this.current);
- this.pop();
- if (tn === $.H1 || tn === $.H2 || tn === $.H3 || tn === $.H4 || tn === $.H5 || tn === $.H6 && ns === NS.HTML)
- break;
- }
- };
- OpenElementStack.prototype.popUntilTableCellPopped = function () {
- while (this.stackTop > -1) {
- var tn = this.currentTagName,
- ns = this.treeAdapter.getNamespaceURI(this.current);
- this.pop();
- if (tn === $.TD || tn === $.TH && ns === NS.HTML)
- 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.treeAdapter.getNamespaceURI(this.current) !== NS.HTML)
- this.pop();
- };
- OpenElementStack.prototype.clearBackToTableBodyContext = function () {
- while (this.currentTagName !== $.TBODY &&
- this.currentTagName !== $.TFOOT &&
- this.currentTagName !== $.THEAD &&
- this.currentTagName !== $.TEMPLATE &&
- this.currentTagName !== $.HTML ||
- this.treeAdapter.getNamespaceURI(this.current) !== NS.HTML)
- this.pop();
- };
- OpenElementStack.prototype.clearBackToTableRowContext = function () {
- while (this.currentTagName !== $.TR &&
- this.currentTagName !== $.TEMPLATE &&
- this.currentTagName !== $.HTML ||
- this.treeAdapter.getNamespaceURI(this.current) !== NS.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]),
- ns = this.treeAdapter.getNamespaceURI(this.items[i]);
- if (tn === tagName && ns === NS.HTML)
- return true;
- 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]),
- ns = this.treeAdapter.getNamespaceURI(this.items[i]);
- if ((tn === $.H1 || tn === $.H2 || tn === $.H3 || tn === $.H4 || tn === $.H5 || tn === $.H6) && ns === NS.HTML)
- return true;
- if (isScopingElement(tn, ns))
- 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]),
- ns = this.treeAdapter.getNamespaceURI(this.items[i]);
- if (tn === tagName && ns === NS.HTML)
- return true;
- 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]),
- ns = this.treeAdapter.getNamespaceURI(this.items[i]);
- if (tn === tagName && ns === NS.HTML)
- return true;
- 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]),
- ns = this.treeAdapter.getNamespaceURI(this.items[i]);
- if (ns !== NS.HTML)
- continue;
- if (tn === tagName)
- return true;
- if (tn === $.TABLE || tn === $.TEMPLATE || tn === $.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]),
- ns = this.treeAdapter.getNamespaceURI(this.items[i]);
- if (ns !== NS.HTML)
- continue;
- if (tn === $.TBODY || tn === $.THEAD || tn === $.TFOOT)
- return true;
- if (tn === $.TABLE || tn === $.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]),
- ns = this.treeAdapter.getNamespaceURI(this.items[i]);
- if (ns !== NS.HTML)
- continue;
- if (tn === tagName)
- return true;
- if (tn !== $.OPTION && tn !== $.OPTGROUP)
- 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();
- };
|