|
1 | | -import { CommentToken, DoctypeToken, CharacterToken } from '../../common/token'; |
2 | 1 | import { Mixin } from '../../utils/mixin.js'; |
3 | | -import { TAG_NAMES as $, NAMESPACES as NS } from '../../common/html.js'; |
| 2 | +import { TAG_NAMES as $ } from '../../common/html.js'; |
4 | 3 | import type { TreeAdapterTypeMap, ElementLocation } from '../../tree-adapters/interface'; |
5 | 4 | import type { Parser } from '../../parser/index.js'; |
6 | | -import { TokenType, Token, TagToken } from '../../common/token.js'; |
| 5 | +import { TokenType, Token } from '../../common/token.js'; |
7 | 6 |
|
8 | 7 | export class LocationInfoParserMixin<T extends TreeAdapterTypeMap> extends Mixin<Parser<T>> { |
9 | | - lastStartTagToken: null | TagToken = null; |
10 | 8 | lastFosterParentingLocation: null | ReturnType<Parser<T>['_findFosterParentingLocation']> = null; |
11 | 9 | currentToken: Token | null = null; |
12 | 10 |
|
13 | 11 | constructor(private parser: Parser<T>) { |
14 | 12 | super(parser); |
15 | 13 | } |
16 | 14 |
|
17 | | - _setStartLocation(element: T['element']) { |
18 | | - let loc = null; |
19 | | - |
20 | | - if (this.lastStartTagToken) { |
21 | | - loc = { |
22 | | - ...this.lastStartTagToken.location!, |
23 | | - startTag: this.lastStartTagToken.location!, |
24 | | - }; |
25 | | - } |
26 | | - |
27 | | - this.parser.treeAdapter.setNodeSourceCodeLocation(element, loc); |
28 | | - } |
29 | | - |
30 | 15 | _setEndLocation(element: T['element'], closingToken: Token) { |
31 | 16 | const loc = this.parser.treeAdapter.getNodeSourceCodeLocation(element); |
32 | 17 |
|
@@ -58,7 +43,6 @@ export class LocationInfoParserMixin<T extends TreeAdapterTypeMap> extends Mixin |
58 | 43 | _bootstrap(this: Parser<T>, document: T['document'], fragmentContext: T['element'] | null) { |
59 | 44 | orig._bootstrap.call(this, document, fragmentContext); |
60 | 45 |
|
61 | | - mxn.lastStartTagToken = null; |
62 | 46 | mxn.lastFosterParentingLocation = null; |
63 | 47 | mxn.currentToken = null; |
64 | 48 |
|
@@ -101,100 +85,6 @@ export class LocationInfoParserMixin<T extends TreeAdapterTypeMap> extends Mixin |
101 | 85 | } |
102 | 86 | } |
103 | 87 | }, |
104 | | - |
105 | | - //Doctype |
106 | | - _setDocumentType(this: Parser<T>, token: DoctypeToken) { |
107 | | - orig._setDocumentType.call(this, token); |
108 | | - |
109 | | - const documentChildren = this.treeAdapter.getChildNodes(this.document); |
110 | | - const docTypeNode = documentChildren.find((node) => this.treeAdapter.isDocumentTypeNode(node)); |
111 | | - |
112 | | - if (docTypeNode) { |
113 | | - this.treeAdapter.setNodeSourceCodeLocation(docTypeNode, token.location!); |
114 | | - } |
115 | | - }, |
116 | | - |
117 | | - //Elements |
118 | | - _attachElementToTree(this: Parser<T>, element: T['element']) { |
119 | | - //NOTE: _attachElementToTree is called from _appendElement, _insertElement and _insertTemplate methods. |
120 | | - //So we will use token location stored in this methods for the element. |
121 | | - mxn._setStartLocation(element); |
122 | | - mxn.lastStartTagToken = null; |
123 | | - orig._attachElementToTree.call(this, element); |
124 | | - }, |
125 | | - |
126 | | - _appendElement(this: Parser<T>, token: TagToken, namespaceURI: NS) { |
127 | | - mxn.lastStartTagToken = token; |
128 | | - orig._appendElement.call(this, token, namespaceURI); |
129 | | - }, |
130 | | - |
131 | | - _insertElement(this: Parser<T>, token: TagToken, namespaceURI: NS) { |
132 | | - mxn.lastStartTagToken = token; |
133 | | - orig._insertElement.call(this, token, namespaceURI); |
134 | | - }, |
135 | | - |
136 | | - _insertTemplate(this: Parser<T>, token: TagToken) { |
137 | | - mxn.lastStartTagToken = token; |
138 | | - orig._insertTemplate.call(this, token); |
139 | | - |
140 | | - const tmplContent = this.treeAdapter.getTemplateContent(this.openElements.current); |
141 | | - |
142 | | - this.treeAdapter.setNodeSourceCodeLocation(tmplContent, null); |
143 | | - }, |
144 | | - |
145 | | - _insertFakeRootElement(this: Parser<T>) { |
146 | | - orig._insertFakeRootElement.call(this); |
147 | | - this.treeAdapter.setNodeSourceCodeLocation(this.openElements.current, null); |
148 | | - }, |
149 | | - |
150 | | - //Comments |
151 | | - _appendCommentNode(this: Parser<T>, token: CommentToken, parent: T['parentNode']) { |
152 | | - orig._appendCommentNode.call(this, token, parent); |
153 | | - |
154 | | - const children = this.treeAdapter.getChildNodes(parent); |
155 | | - const commentNode = children[children.length - 1]; |
156 | | - |
157 | | - this.treeAdapter.setNodeSourceCodeLocation(commentNode, token.location!); |
158 | | - }, |
159 | | - |
160 | | - //Text |
161 | | - _findFosterParentingLocation(this: Parser<T>) { |
162 | | - //NOTE: store last foster parenting location, so we will be able to find inserted text |
163 | | - //in case of foster parenting |
164 | | - mxn.lastFosterParentingLocation = orig._findFosterParentingLocation.call(this); |
165 | | - |
166 | | - return mxn.lastFosterParentingLocation; |
167 | | - }, |
168 | | - |
169 | | - _insertCharacters(this: Parser<T>, token: CharacterToken) { |
170 | | - orig._insertCharacters.call(this, token); |
171 | | - |
172 | | - const hasFosterParent = this._shouldFosterParentOnInsertion(); |
173 | | - |
174 | | - const parent = |
175 | | - (hasFosterParent && mxn.lastFosterParentingLocation!.parent) || |
176 | | - this.openElements.currentTmplContent || |
177 | | - this.openElements.current; |
178 | | - |
179 | | - const siblings = this.treeAdapter.getChildNodes(parent); |
180 | | - |
181 | | - const textNodeIdx = |
182 | | - hasFosterParent && mxn.lastFosterParentingLocation!.beforeElement |
183 | | - ? siblings.indexOf(mxn.lastFosterParentingLocation!.beforeElement) - 1 |
184 | | - : siblings.length - 1; |
185 | | - |
186 | | - const textNode = siblings[textNodeIdx]; |
187 | | - |
188 | | - //NOTE: if we have location assigned by another token, then just update end position |
189 | | - const tnLoc = this.treeAdapter.getNodeSourceCodeLocation(textNode); |
190 | | - |
191 | | - if (tnLoc) { |
192 | | - const { endLine, endCol, endOffset } = token.location!; |
193 | | - this.treeAdapter.updateNodeSourceCodeLocation(textNode, { endLine, endCol, endOffset }); |
194 | | - } else { |
195 | | - this.treeAdapter.setNodeSourceCodeLocation(textNode, token.location!); |
196 | | - } |
197 | | - }, |
198 | 88 | }; |
199 | 89 | } |
200 | 90 | } |
0 commit comments