parser_mixin.js 7.6 KB

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