From 99c1d9af689e5325f3cf535c4007b3aeb8325229 Mon Sep 17 00:00:00 2001 From: Minteck Date: Tue, 10 Jan 2023 14:54:04 +0100 Subject: Update - This is an automated commit --- .../location-info/open-element-stack-mixin.js | 35 ++++ .../lib/extensions/location-info/parser-mixin.js | 223 +++++++++++++++++++++ .../extensions/location-info/tokenizer-mixin.js | 146 ++++++++++++++ 3 files changed, 404 insertions(+) create mode 100644 school/node_modules/parse5/lib/extensions/location-info/open-element-stack-mixin.js create mode 100644 school/node_modules/parse5/lib/extensions/location-info/parser-mixin.js create mode 100644 school/node_modules/parse5/lib/extensions/location-info/tokenizer-mixin.js (limited to 'school/node_modules/parse5/lib/extensions/location-info') diff --git a/school/node_modules/parse5/lib/extensions/location-info/open-element-stack-mixin.js b/school/node_modules/parse5/lib/extensions/location-info/open-element-stack-mixin.js new file mode 100644 index 0000000..765fe77 --- /dev/null +++ b/school/node_modules/parse5/lib/extensions/location-info/open-element-stack-mixin.js @@ -0,0 +1,35 @@ +'use strict'; + +const Mixin = require('../../utils/mixin'); + +class LocationInfoOpenElementStackMixin extends Mixin { + constructor(stack, opts) { + super(stack); + + this.onItemPop = opts.onItemPop; + } + + _getOverriddenMethods(mxn, orig) { + return { + pop() { + mxn.onItemPop(this.current); + orig.pop.call(this); + }, + + popAllUpToHtmlElement() { + for (let i = this.stackTop; i > 0; i--) { + mxn.onItemPop(this.items[i]); + } + + orig.popAllUpToHtmlElement.call(this); + }, + + remove(element) { + mxn.onItemPop(this.current); + orig.remove.call(this, element); + } + }; + } +} + +module.exports = LocationInfoOpenElementStackMixin; diff --git a/school/node_modules/parse5/lib/extensions/location-info/parser-mixin.js b/school/node_modules/parse5/lib/extensions/location-info/parser-mixin.js new file mode 100644 index 0000000..e7d3e2d --- /dev/null +++ b/school/node_modules/parse5/lib/extensions/location-info/parser-mixin.js @@ -0,0 +1,223 @@ +'use strict'; + +const Mixin = require('../../utils/mixin'); +const Tokenizer = require('../../tokenizer'); +const LocationInfoTokenizerMixin = require('./tokenizer-mixin'); +const LocationInfoOpenElementStackMixin = require('./open-element-stack-mixin'); +const HTML = require('../../common/html'); + +//Aliases +const $ = HTML.TAG_NAMES; + +class LocationInfoParserMixin extends Mixin { + constructor(parser) { + super(parser); + + this.parser = parser; + this.treeAdapter = this.parser.treeAdapter; + this.posTracker = null; + this.lastStartTagToken = null; + this.lastFosterParentingLocation = null; + this.currentToken = null; + } + + _setStartLocation(element) { + let loc = null; + + if (this.lastStartTagToken) { + loc = Object.assign({}, this.lastStartTagToken.location); + loc.startTag = this.lastStartTagToken.location; + } + + this.treeAdapter.setNodeSourceCodeLocation(element, loc); + } + + _setEndLocation(element, closingToken) { + const loc = this.treeAdapter.getNodeSourceCodeLocation(element); + + if (loc) { + if (closingToken.location) { + const ctLoc = closingToken.location; + const tn = this.treeAdapter.getTagName(element); + + // NOTE: For cases like

- First 'p' closes without a closing + // tag and for cases like

- 'p' closes without a closing tag. + const isClosingEndTag = closingToken.type === Tokenizer.END_TAG_TOKEN && tn === closingToken.tagName; + const endLoc = {}; + if (isClosingEndTag) { + endLoc.endTag = Object.assign({}, ctLoc); + endLoc.endLine = ctLoc.endLine; + endLoc.endCol = ctLoc.endCol; + endLoc.endOffset = ctLoc.endOffset; + } else { + endLoc.endLine = ctLoc.startLine; + endLoc.endCol = ctLoc.startCol; + endLoc.endOffset = ctLoc.startOffset; + } + + this.treeAdapter.updateNodeSourceCodeLocation(element, endLoc); + } + } + } + + _getOverriddenMethods(mxn, orig) { + return { + _bootstrap(document, fragmentContext) { + orig._bootstrap.call(this, document, fragmentContext); + + mxn.lastStartTagToken = null; + mxn.lastFosterParentingLocation = null; + mxn.currentToken = null; + + const tokenizerMixin = Mixin.install(this.tokenizer, LocationInfoTokenizerMixin); + + mxn.posTracker = tokenizerMixin.posTracker; + + Mixin.install(this.openElements, LocationInfoOpenElementStackMixin, { + onItemPop: function(element) { + mxn._setEndLocation(element, mxn.currentToken); + } + }); + }, + + _runParsingLoop(scriptHandler) { + orig._runParsingLoop.call(this, scriptHandler); + + // NOTE: generate location info for elements + // that remains on open element stack + for (let i = this.openElements.stackTop; i >= 0; i--) { + mxn._setEndLocation(this.openElements.items[i], mxn.currentToken); + } + }, + + //Token processing + _processTokenInForeignContent(token) { + mxn.currentToken = token; + orig._processTokenInForeignContent.call(this, token); + }, + + _processToken(token) { + mxn.currentToken = token; + orig._processToken.call(this, token); + + //NOTE: and are never popped from the stack, so we need to updated + //their end location explicitly. + const requireExplicitUpdate = + token.type === Tokenizer.END_TAG_TOKEN && + (token.tagName === $.HTML || (token.tagName === $.BODY && this.openElements.hasInScope($.BODY))); + + if (requireExplicitUpdate) { + for (let i = this.openElements.stackTop; i >= 0; i--) { + const element = this.openElements.items[i]; + + if (this.treeAdapter.getTagName(element) === token.tagName) { + mxn._setEndLocation(element, token); + break; + } + } + } + }, + + //Doctype + _setDocumentType(token) { + orig._setDocumentType.call(this, token); + + const documentChildren = this.treeAdapter.getChildNodes(this.document); + const cnLength = documentChildren.length; + + for (let i = 0; i < cnLength; i++) { + const node = documentChildren[i]; + + if (this.treeAdapter.isDocumentTypeNode(node)) { + this.treeAdapter.setNodeSourceCodeLocation(node, token.location); + break; + } + } + }, + + //Elements + _attachElementToTree(element) { + //NOTE: _attachElementToTree is called from _appendElement, _insertElement and _insertTemplate methods. + //So we will use token location stored in this methods for the element. + mxn._setStartLocation(element); + mxn.lastStartTagToken = null; + orig._attachElementToTree.call(this, element); + }, + + _appendElement(token, namespaceURI) { + mxn.lastStartTagToken = token; + orig._appendElement.call(this, token, namespaceURI); + }, + + _insertElement(token, namespaceURI) { + mxn.lastStartTagToken = token; + orig._insertElement.call(this, token, namespaceURI); + }, + + _insertTemplate(token) { + mxn.lastStartTagToken = token; + orig._insertTemplate.call(this, token); + + const tmplContent = this.treeAdapter.getTemplateContent(this.openElements.current); + + this.treeAdapter.setNodeSourceCodeLocation(tmplContent, null); + }, + + _insertFakeRootElement() { + orig._insertFakeRootElement.call(this); + this.treeAdapter.setNodeSourceCodeLocation(this.openElements.current, null); + }, + + //Comments + _appendCommentNode(token, parent) { + orig._appendCommentNode.call(this, token, parent); + + const children = this.treeAdapter.getChildNodes(parent); + const commentNode = children[children.length - 1]; + + this.treeAdapter.setNodeSourceCodeLocation(commentNode, token.location); + }, + + //Text + _findFosterParentingLocation() { + //NOTE: store last foster parenting location, so we will be able to find inserted text + //in case of foster parenting + mxn.lastFosterParentingLocation = orig._findFosterParentingLocation.call(this); + + return mxn.lastFosterParentingLocation; + }, + + _insertCharacters(token) { + orig._insertCharacters.call(this, token); + + const hasFosterParent = this._shouldFosterParentOnInsertion(); + + const parent = + (hasFosterParent && mxn.lastFosterParentingLocation.parent) || + this.openElements.currentTmplContent || + this.openElements.current; + + const siblings = this.treeAdapter.getChildNodes(parent); + + const textNodeIdx = + hasFosterParent && mxn.lastFosterParentingLocation.beforeElement + ? siblings.indexOf(mxn.lastFosterParentingLocation.beforeElement) - 1 + : siblings.length - 1; + + const textNode = siblings[textNodeIdx]; + + //NOTE: if we have location assigned by another token, then just update end position + const tnLoc = this.treeAdapter.getNodeSourceCodeLocation(textNode); + + if (tnLoc) { + const { endLine, endCol, endOffset } = token.location; + this.treeAdapter.updateNodeSourceCodeLocation(textNode, { endLine, endCol, endOffset }); + } else { + this.treeAdapter.setNodeSourceCodeLocation(textNode, token.location); + } + } + }; + } +} + +module.exports = LocationInfoParserMixin; diff --git a/school/node_modules/parse5/lib/extensions/location-info/tokenizer-mixin.js b/school/node_modules/parse5/lib/extensions/location-info/tokenizer-mixin.js new file mode 100644 index 0000000..3c1ef5f --- /dev/null +++ b/school/node_modules/parse5/lib/extensions/location-info/tokenizer-mixin.js @@ -0,0 +1,146 @@ +'use strict'; + +const Mixin = require('../../utils/mixin'); +const Tokenizer = require('../../tokenizer'); +const PositionTrackingPreprocessorMixin = require('../position-tracking/preprocessor-mixin'); + +class LocationInfoTokenizerMixin extends Mixin { + constructor(tokenizer) { + super(tokenizer); + + this.tokenizer = tokenizer; + this.posTracker = Mixin.install(tokenizer.preprocessor, PositionTrackingPreprocessorMixin); + this.currentAttrLocation = null; + this.ctLoc = null; + } + + _getCurrentLocation() { + return { + startLine: this.posTracker.line, + startCol: this.posTracker.col, + startOffset: this.posTracker.offset, + endLine: -1, + endCol: -1, + endOffset: -1 + }; + } + + _attachCurrentAttrLocationInfo() { + this.currentAttrLocation.endLine = this.posTracker.line; + this.currentAttrLocation.endCol = this.posTracker.col; + this.currentAttrLocation.endOffset = this.posTracker.offset; + + const currentToken = this.tokenizer.currentToken; + const currentAttr = this.tokenizer.currentAttr; + + if (!currentToken.location.attrs) { + currentToken.location.attrs = Object.create(null); + } + + currentToken.location.attrs[currentAttr.name] = this.currentAttrLocation; + } + + _getOverriddenMethods(mxn, orig) { + const methods = { + _createStartTagToken() { + orig._createStartTagToken.call(this); + this.currentToken.location = mxn.ctLoc; + }, + + _createEndTagToken() { + orig._createEndTagToken.call(this); + this.currentToken.location = mxn.ctLoc; + }, + + _createCommentToken() { + orig._createCommentToken.call(this); + this.currentToken.location = mxn.ctLoc; + }, + + _createDoctypeToken(initialName) { + orig._createDoctypeToken.call(this, initialName); + this.currentToken.location = mxn.ctLoc; + }, + + _createCharacterToken(type, ch) { + orig._createCharacterToken.call(this, type, ch); + this.currentCharacterToken.location = mxn.ctLoc; + }, + + _createEOFToken() { + orig._createEOFToken.call(this); + this.currentToken.location = mxn._getCurrentLocation(); + }, + + _createAttr(attrNameFirstCh) { + orig._createAttr.call(this, attrNameFirstCh); + mxn.currentAttrLocation = mxn._getCurrentLocation(); + }, + + _leaveAttrName(toState) { + orig._leaveAttrName.call(this, toState); + mxn._attachCurrentAttrLocationInfo(); + }, + + _leaveAttrValue(toState) { + orig._leaveAttrValue.call(this, toState); + mxn._attachCurrentAttrLocationInfo(); + }, + + _emitCurrentToken() { + const ctLoc = this.currentToken.location; + + //NOTE: if we have pending character token make it's end location equal to the + //current token's start location. + if (this.currentCharacterToken) { + this.currentCharacterToken.location.endLine = ctLoc.startLine; + this.currentCharacterToken.location.endCol = ctLoc.startCol; + this.currentCharacterToken.location.endOffset = ctLoc.startOffset; + } + + if (this.currentToken.type === Tokenizer.EOF_TOKEN) { + ctLoc.endLine = ctLoc.startLine; + ctLoc.endCol = ctLoc.startCol; + ctLoc.endOffset = ctLoc.startOffset; + } else { + ctLoc.endLine = mxn.posTracker.line; + ctLoc.endCol = mxn.posTracker.col + 1; + ctLoc.endOffset = mxn.posTracker.offset + 1; + } + + orig._emitCurrentToken.call(this); + }, + + _emitCurrentCharacterToken() { + const ctLoc = this.currentCharacterToken && this.currentCharacterToken.location; + + //NOTE: if we have character token and it's location wasn't set in the _emitCurrentToken(), + //then set it's location at the current preprocessor position. + //We don't need to increment preprocessor position, since character token + //emission is always forced by the start of the next character token here. + //So, we already have advanced position. + if (ctLoc && ctLoc.endOffset === -1) { + ctLoc.endLine = mxn.posTracker.line; + ctLoc.endCol = mxn.posTracker.col; + ctLoc.endOffset = mxn.posTracker.offset; + } + + orig._emitCurrentCharacterToken.call(this); + } + }; + + //NOTE: patch initial states for each mode to obtain token start position + Object.keys(Tokenizer.MODE).forEach(modeName => { + const state = Tokenizer.MODE[modeName]; + + methods[state] = function(cp) { + mxn.ctLoc = mxn._getCurrentLocation(); + orig[state].call(this, cp); + }; + }); + + return methods; + } +} + +module.exports = LocationInfoTokenizerMixin; -- cgit