parser-mixin.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. 'use strict';
  2. const Mixin = require('../../utils/mixin');
  3. const Tokenizer = require('../../tokenizer');
  4. const LocationInfoTokenizerMixin = require('./tokenizer-mixin');
  5. const LocationInfoOpenElementStackMixin = require('./open-element-stack-mixin');
  6. const HTML = require('../../common/html');
  7. //Aliases
  8. const $ = HTML.TAG_NAMES;
  9. class LocationInfoParserMixin extends Mixin {
  10. constructor(parser) {
  11. super(parser);
  12. this.parser = parser;
  13. this.treeAdapter = this.parser.treeAdapter;
  14. this.posTracker = null;
  15. this.lastStartTagToken = null;
  16. this.lastFosterParentingLocation = null;
  17. this.currentToken = null;
  18. }
  19. _setStartLocation(element) {
  20. let loc = null;
  21. if (this.lastStartTagToken) {
  22. loc = Object.assign({}, this.lastStartTagToken.location);
  23. loc.startTag = this.lastStartTagToken.location;
  24. }
  25. this.treeAdapter.setNodeSourceCodeLocation(element, loc);
  26. }
  27. _setEndLocation(element, closingToken) {
  28. const loc = this.treeAdapter.getNodeSourceCodeLocation(element);
  29. if (loc) {
  30. if (closingToken.location) {
  31. const ctLoc = closingToken.location;
  32. const tn = this.treeAdapter.getTagName(element);
  33. // NOTE: For cases like <p> <p> </p> - First 'p' closes without a closing
  34. // tag and for cases like <td> <p> </td> - 'p' closes without a closing tag.
  35. const isClosingEndTag = closingToken.type === Tokenizer.END_TAG_TOKEN && tn === closingToken.tagName;
  36. const endLoc = {};
  37. if (isClosingEndTag) {
  38. endLoc.endTag = Object.assign({}, ctLoc);
  39. endLoc.endLine = ctLoc.endLine;
  40. endLoc.endCol = ctLoc.endCol;
  41. endLoc.endOffset = ctLoc.endOffset;
  42. } else {
  43. endLoc.endLine = ctLoc.startLine;
  44. endLoc.endCol = ctLoc.startCol;
  45. endLoc.endOffset = ctLoc.startOffset;
  46. }
  47. this.treeAdapter.updateNodeSourceCodeLocation(element, endLoc);
  48. }
  49. }
  50. }
  51. _getOverriddenMethods(mxn, orig) {
  52. return {
  53. _bootstrap(document, fragmentContext) {
  54. orig._bootstrap.call(this, document, fragmentContext);
  55. mxn.lastStartTagToken = null;
  56. mxn.lastFosterParentingLocation = null;
  57. mxn.currentToken = null;
  58. const tokenizerMixin = Mixin.install(this.tokenizer, LocationInfoTokenizerMixin);
  59. mxn.posTracker = tokenizerMixin.posTracker;
  60. Mixin.install(this.openElements, LocationInfoOpenElementStackMixin, {
  61. onItemPop: function(element) {
  62. mxn._setEndLocation(element, mxn.currentToken);
  63. }
  64. });
  65. },
  66. _runParsingLoop(scriptHandler) {
  67. orig._runParsingLoop.call(this, scriptHandler);
  68. // NOTE: generate location info for elements
  69. // that remains on open element stack
  70. for (let i = this.openElements.stackTop; i >= 0; i--) {
  71. mxn._setEndLocation(this.openElements.items[i], mxn.currentToken);
  72. }
  73. },
  74. //Token processing
  75. _processTokenInForeignContent(token) {
  76. mxn.currentToken = token;
  77. orig._processTokenInForeignContent.call(this, token);
  78. },
  79. _processToken(token) {
  80. mxn.currentToken = token;
  81. orig._processToken.call(this, token);
  82. //NOTE: <body> and <html> are never popped from the stack, so we need to updated
  83. //their end location explicitly.
  84. const requireExplicitUpdate =
  85. token.type === Tokenizer.END_TAG_TOKEN &&
  86. (token.tagName === $.HTML || (token.tagName === $.BODY && this.openElements.hasInScope($.BODY)));
  87. if (requireExplicitUpdate) {
  88. for (let i = this.openElements.stackTop; i >= 0; i--) {
  89. const element = this.openElements.items[i];
  90. if (this.treeAdapter.getTagName(element) === token.tagName) {
  91. mxn._setEndLocation(element, token);
  92. break;
  93. }
  94. }
  95. }
  96. },
  97. //Doctype
  98. _setDocumentType(token) {
  99. orig._setDocumentType.call(this, token);
  100. const documentChildren = this.treeAdapter.getChildNodes(this.document);
  101. const cnLength = documentChildren.length;
  102. for (let i = 0; i < cnLength; i++) {
  103. const node = documentChildren[i];
  104. if (this.treeAdapter.isDocumentTypeNode(node)) {
  105. this.treeAdapter.setNodeSourceCodeLocation(node, token.location);
  106. break;
  107. }
  108. }
  109. },
  110. //Elements
  111. _attachElementToTree(element) {
  112. //NOTE: _attachElementToTree is called from _appendElement, _insertElement and _insertTemplate methods.
  113. //So we will use token location stored in this methods for the element.
  114. mxn._setStartLocation(element);
  115. mxn.lastStartTagToken = null;
  116. orig._attachElementToTree.call(this, element);
  117. },
  118. _appendElement(token, namespaceURI) {
  119. mxn.lastStartTagToken = token;
  120. orig._appendElement.call(this, token, namespaceURI);
  121. },
  122. _insertElement(token, namespaceURI) {
  123. mxn.lastStartTagToken = token;
  124. orig._insertElement.call(this, token, namespaceURI);
  125. },
  126. _insertTemplate(token) {
  127. mxn.lastStartTagToken = token;
  128. orig._insertTemplate.call(this, token);
  129. const tmplContent = this.treeAdapter.getTemplateContent(this.openElements.current);
  130. this.treeAdapter.setNodeSourceCodeLocation(tmplContent, null);
  131. },
  132. _insertFakeRootElement() {
  133. orig._insertFakeRootElement.call(this);
  134. this.treeAdapter.setNodeSourceCodeLocation(this.openElements.current, null);
  135. },
  136. //Comments
  137. _appendCommentNode(token, parent) {
  138. orig._appendCommentNode.call(this, token, parent);
  139. const children = this.treeAdapter.getChildNodes(parent);
  140. const commentNode = children[children.length - 1];
  141. this.treeAdapter.setNodeSourceCodeLocation(commentNode, token.location);
  142. },
  143. //Text
  144. _findFosterParentingLocation() {
  145. //NOTE: store last foster parenting location, so we will be able to find inserted text
  146. //in case of foster parenting
  147. mxn.lastFosterParentingLocation = orig._findFosterParentingLocation.call(this);
  148. return mxn.lastFosterParentingLocation;
  149. },
  150. _insertCharacters(token) {
  151. orig._insertCharacters.call(this, token);
  152. const hasFosterParent = this._shouldFosterParentOnInsertion();
  153. const parent =
  154. (hasFosterParent && mxn.lastFosterParentingLocation.parent) ||
  155. this.openElements.currentTmplContent ||
  156. this.openElements.current;
  157. const siblings = this.treeAdapter.getChildNodes(parent);
  158. const textNodeIdx =
  159. hasFosterParent && mxn.lastFosterParentingLocation.beforeElement
  160. ? siblings.indexOf(mxn.lastFosterParentingLocation.beforeElement) - 1
  161. : siblings.length - 1;
  162. const textNode = siblings[textNodeIdx];
  163. //NOTE: if we have location assigned by another token, then just update end position
  164. const tnLoc = this.treeAdapter.getNodeSourceCodeLocation(textNode);
  165. if (tnLoc) {
  166. const { endLine, endCol, endOffset } = token.location;
  167. this.treeAdapter.updateNodeSourceCodeLocation(textNode, { endLine, endCol, endOffset });
  168. } else {
  169. this.treeAdapter.setNodeSourceCodeLocation(textNode, token.location);
  170. }
  171. }
  172. };
  173. }
  174. }
  175. module.exports = LocationInfoParserMixin;