1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | Object.defineProperty(exports, "__esModule", { value: true });
|
15 | const parse5 = require("parse5");
|
16 | const model_1 = require("../model/model");
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function getLocationInfoForPosition(document, position) {
|
27 | const location = _getLocationInfoForPosition(document.ast, position, document);
|
28 | if (!location) {
|
29 |
|
30 | return { kind: 'text' };
|
31 | }
|
32 | return location;
|
33 | }
|
34 | exports.getLocationInfoForPosition = getLocationInfoForPosition;
|
35 | function _getLocationInfoForPosition(node, position, document) {
|
36 | const sourceRange = document.sourceRangeForNode(node);
|
37 | const location = node.__location;
|
38 | |
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | if (!(sourceRange && location)) {
|
45 | return _findLocationInChildren(node, position, document);
|
46 | }
|
47 | if (!model_1.isPositionInsideRange(position, sourceRange)) {
|
48 |
|
49 | return;
|
50 | }
|
51 | const locationInChildren = _findLocationInChildren(node, position, document);
|
52 | if (locationInChildren) {
|
53 | return locationInChildren;
|
54 | }
|
55 | const attributeLocation = getAttributeLocation(node, position, document, location);
|
56 | if (attributeLocation) {
|
57 | return attributeLocation;
|
58 | }
|
59 | const startTagRange = document.sourceRangeForStartTag(node);
|
60 | const endTagRange = document.sourceRangeForEndTag(node);
|
61 |
|
62 | if (model_1.isPositionInsideRange(position, endTagRange, false)) {
|
63 | return { kind: 'endTag', element: node };
|
64 | }
|
65 | if (startTagRange && model_1.isPositionInsideRange(position, startTagRange, false)) {
|
66 | if (position.line === startTagRange.start.line) {
|
67 |
|
68 | if (position.column <=
|
69 | startTagRange.start.column + (node.tagName || '').length + 1) {
|
70 | return { kind: 'tagName', element: node };
|
71 | }
|
72 | }
|
73 |
|
74 |
|
75 | return { kind: 'attribute', attribute: null, element: node };
|
76 | }
|
77 |
|
78 | if (parse5.treeAdapters.default.isCommentNode(node) &&
|
79 | model_1.isPositionInsideRange(position, sourceRange, false)) {
|
80 | return { kind: 'comment', commentNode: node };
|
81 | }
|
82 | if (parse5.treeAdapters.default.isTextNode(node)) {
|
83 | const parent = node.parentNode;
|
84 | if (parent && parent.tagName === 'script') {
|
85 | return { kind: 'scriptTagContents', textNode: node };
|
86 | }
|
87 | if (parent && parent.tagName === 'style') {
|
88 | return { kind: 'styleTagContents', textNode: node };
|
89 | }
|
90 | return { kind: 'text', textNode: node };
|
91 | }
|
92 | if (model_1.isPositionInsideRange(position, sourceRange, false)) {
|
93 | |
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | if (startTagRange && endTagRange &&
|
103 | model_1.comparePositionAndRange(position, startTagRange, false) > 0 &&
|
104 | model_1.comparePositionAndRange(position, endTagRange, false) < 0) {
|
105 | if (node.tagName === 'script') {
|
106 | return { kind: 'scriptTagContents' };
|
107 | }
|
108 | if (node.tagName === 'style') {
|
109 | return { kind: 'styleTagContents' };
|
110 | }
|
111 | return { kind: 'text' };
|
112 | }
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | if (node.tagName) {
|
121 | if (position.column <=
|
122 | sourceRange.start.column + node.tagName.length + 1) {
|
123 | return { kind: 'tagName', element: node };
|
124 | }
|
125 | return { kind: 'attribute', element: node, attribute: null };
|
126 | }
|
127 | }
|
128 | }
|
129 | function _findLocationInChildren(node, position, document) {
|
130 | for (const child of node.childNodes || []) {
|
131 | const result = _getLocationInfoForPosition(child, position, document);
|
132 | if (result) {
|
133 | return result;
|
134 | }
|
135 | }
|
136 | if (node.tagName === 'template') {
|
137 | const content = parse5.treeAdapters.default.getTemplateContent(node);
|
138 | const result = _getLocationInfoForPosition(content, position, document);
|
139 | if (result) {
|
140 | return result;
|
141 | }
|
142 | }
|
143 | }
|
144 | function isElementLocationInfo(location) {
|
145 | const loc = location;
|
146 | return (loc.startTag && loc.endTag) !== undefined;
|
147 | }
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | function getAttributeLocation(node, position, document, location) {
|
153 | |
154 |
|
155 |
|
156 |
|
157 | const attrs = (isElementLocationInfo(location) && location.startTag.attrs) ||
|
158 | location.attrs || {};
|
159 | for (const attrName in attrs) {
|
160 | const range = document.sourceRangeForAttribute(node, attrName);
|
161 | if (model_1.isPositionInsideRange(position, range)) {
|
162 | if (model_1.isPositionInsideRange(position, document.sourceRangeForAttributeValue(node, attrName))) {
|
163 | return { kind: 'attributeValue', attribute: attrName, element: node };
|
164 | }
|
165 | return { kind: 'attribute', attribute: attrName, element: node };
|
166 | }
|
167 | }
|
168 | }
|
169 |
|
\ | No newline at end of file |