@node-projects/web-component-designer-htmlparserservice-nodehtmlparser 0.1.11 → 0.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,4 +4,7 @@ export declare class NodeHtmlParserService implements IHtmlParserService {
4
4
  constructor(designItemCreatedCallback?: (IDesignItem: any) => void);
5
5
  parse(html: string, serviceContainer: ServiceContainer, instanceServiceContainer: InstanceServiceContainer, parseSnippet: boolean, positionOffset?: number): Promise<IDesignItem[]>;
6
6
  _createDesignItemsRecursive(item: any, serviceContainer: ServiceContainer, instanceServiceContainer: InstanceServiceContainer, namespace: string, snippet: boolean, positionOffset?: number): IDesignItem;
7
+ private _addAttributeSourceParts;
8
+ private _getOpeningTagText;
9
+ private _getAttributeValueRange;
7
10
  }
@@ -8,6 +8,8 @@ export class NodeHtmlParserService {
8
8
  this._designItemCreatedCallback = designItemCreatedCallback;
9
9
  }
10
10
  async parse(html, serviceContainer, instanceServiceContainer, parseSnippet, positionOffset = 0) {
11
+ if (!parseSnippet)
12
+ instanceServiceContainer.designItemDocumentPositionService?.clearSourceParts();
11
13
  const parsed = parser.parse(html, { comment: true });
12
14
  let designItems = [];
13
15
  for (let p of parsed.childNodes) {
@@ -38,6 +40,7 @@ export class NodeHtmlParserService {
38
40
  this._designItemCreatedCallback(designItem);
39
41
  if (!snippet && instanceServiceContainer.designItemDocumentPositionService)
40
42
  instanceServiceContainer.designItemDocumentPositionService.setPosition(designItem, { start: item.range[0] + positionOffset, length: item.range[1] - item.range[0] });
43
+ this._addAttributeSourceParts(item, designItem, serviceContainer, snippet, positionOffset);
41
44
  let style = '';
42
45
  let attr = item.attributes;
43
46
  for (let a in attr) {
@@ -99,6 +102,81 @@ export class NodeHtmlParserService {
99
102
  if (!snippet && instanceServiceContainer.designItemDocumentPositionService)
100
103
  instanceServiceContainer.designItemDocumentPositionService.setPosition(designItem, { start: item.range[0] + positionOffset, length: item.range[1] - item.range[0] });
101
104
  }
105
+ if (serviceContainer.designItemService.finishedDesignItem)
106
+ serviceContainer.designItemService.finishedDesignItem(designItem);
102
107
  return designItem;
103
108
  }
109
+ _addAttributeSourceParts(item, designItem, serviceContainer, snippet, positionOffset) {
110
+ const positionService = designItem.instanceServiceContainer.designItemDocumentPositionService;
111
+ if (snippet || !positionService || !item.rawAttrs)
112
+ return;
113
+ const rawAttrs = item.rawAttrs;
114
+ const openingTag = this._getOpeningTagText(item.outerHTML ?? '');
115
+ const rawAttrsIndex = openingTag.indexOf(rawAttrs);
116
+ if (rawAttrsIndex < 0)
117
+ return;
118
+ const rawAttrsStart = item.range[0] + positionOffset + rawAttrsIndex;
119
+ const attributeRegex = /([^\s"'<>/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g;
120
+ let match;
121
+ while (match = attributeRegex.exec(rawAttrs)) {
122
+ const attributeName = match[1];
123
+ const attributeStart = rawAttrsStart + match.index;
124
+ positionService.addSourcePart({
125
+ designItem,
126
+ kind: 'attribute',
127
+ key: `attribute:${attributeName}`,
128
+ name: attributeName,
129
+ textRange: { start: attributeStart, length: match[0].length }
130
+ });
131
+ const valueRange = this._getAttributeValueRange(match[0]);
132
+ if (valueRange) {
133
+ const textRange = { start: attributeStart + valueRange.start, length: valueRange.length };
134
+ positionService.addSourcePart({
135
+ designItem,
136
+ kind: 'attribute-value',
137
+ key: `attribute:${attributeName}/value`,
138
+ name: attributeName,
139
+ textRange
140
+ });
141
+ const context = { designItem, sourceKind: 'attribute', name: attributeName, value: valueRange.value, valueTextRange: textRange };
142
+ for (const provider of serviceContainer.sourceMapProviders) {
143
+ if (provider.canMap(context))
144
+ positionService.addSourceParts(provider.map(context));
145
+ }
146
+ }
147
+ }
148
+ }
149
+ _getOpeningTagText(outerHtml) {
150
+ let quote = null;
151
+ for (let i = 0; i < outerHtml.length; i++) {
152
+ const char = outerHtml[i];
153
+ if (quote) {
154
+ if (char === quote)
155
+ quote = null;
156
+ }
157
+ else if (char === '"' || char === '\'') {
158
+ quote = char;
159
+ }
160
+ else if (char === '>') {
161
+ return outerHtml.substring(0, i + 1);
162
+ }
163
+ }
164
+ return outerHtml;
165
+ }
166
+ _getAttributeValueRange(attributeText) {
167
+ const equalsIndex = attributeText.indexOf('=');
168
+ if (equalsIndex < 0)
169
+ return null;
170
+ let valueStart = equalsIndex + 1;
171
+ while (valueStart < attributeText.length && /\s/.test(attributeText[valueStart]))
172
+ valueStart++;
173
+ const quote = attributeText[valueStart];
174
+ if (quote === '"' || quote === '\'') {
175
+ const contentStart = valueStart + 1;
176
+ const contentEnd = attributeText.lastIndexOf(quote);
177
+ const length = Math.max(0, contentEnd - contentStart);
178
+ return { start: contentStart, length, value: attributeText.substring(contentStart, contentStart + length) };
179
+ }
180
+ return { start: valueStart, length: attributeText.length - valueStart, value: attributeText.substring(valueStart) };
181
+ }
104
182
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "description": "web-component-designer addon: Parser Service using the Node HTML Parser",
3
3
  "name": "@node-projects/web-component-designer-htmlparserservice-nodehtmlparser",
4
- "version": "0.1.11",
4
+ "version": "0.1.13",
5
5
  "type": "module",
6
6
  "main": "./dist/service/htmlParserService/NodeHtmlParserService.js",
7
7
  "author": "jochen.kuehner@gmx.de",
@@ -9,11 +9,12 @@
9
9
  "scripts": {
10
10
  "tsc": "tsc",
11
11
  "build": "tsc",
12
+ "link": "npm link",
12
13
  "prepublishOnly": "npm run build"
13
14
  },
14
15
  "dependencies": {
15
- "@node-projects/node-html-parser-esm": "^6.1.5",
16
- "@node-projects/web-component-designer": ">=0.1.161"
16
+ "@node-projects/node-html-parser-esm": "^6.2.0",
17
+ "@node-projects/web-component-designer": ">=0.1.278"
17
18
  },
18
19
  "repository": {
19
20
  "type": "git",