@node-projects/web-component-designer-htmlparserservice-nodehtmlparser 0.1.12 → 0.1.15
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
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { newElementFromString, DesignItem, CssAttributeParser } from "@node-projects/web-component-designer";
|
|
1
|
+
import { newElementFromString, DesignItem, CssAttributeParser, DomConverter } from "@node-projects/web-component-designer";
|
|
2
2
|
import * as parser from "@node-projects/node-html-parser-esm";
|
|
3
3
|
// Alternative Parser, cause when you use the Browser, it instanciates the CusomElements,
|
|
4
4
|
// and some Elements remove attributes from their DOM, so you loose Data
|
|
@@ -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) {
|
|
@@ -33,11 +35,13 @@ export class NodeHtmlParserService {
|
|
|
33
35
|
element = instanceServiceContainer.designerCanvas.rootDesignItem.document.createElement(item.rawTagName);
|
|
34
36
|
manualCreatedElement = true;
|
|
35
37
|
}
|
|
38
|
+
element = DesignItem.updateRenderedNode(serviceContainer, element);
|
|
36
39
|
designItem = DesignItem.GetOrCreateDesignItem(element, item, serviceContainer, instanceServiceContainer);
|
|
37
40
|
if (this._designItemCreatedCallback)
|
|
38
41
|
this._designItemCreatedCallback(designItem);
|
|
39
42
|
if (!snippet && instanceServiceContainer.designItemDocumentPositionService)
|
|
40
43
|
instanceServiceContainer.designItemDocumentPositionService.setPosition(designItem, { start: item.range[0] + positionOffset, length: item.range[1] - item.range[0] });
|
|
44
|
+
this._addAttributeSourceParts(item, designItem, serviceContainer, snippet, positionOffset);
|
|
41
45
|
let style = '';
|
|
42
46
|
let attr = item.attributes;
|
|
43
47
|
for (let a in attr) {
|
|
@@ -55,9 +59,9 @@ export class NodeHtmlParserService {
|
|
|
55
59
|
let styleParser = new CssAttributeParser();
|
|
56
60
|
styleParser.parse(style);
|
|
57
61
|
for (let s of styleParser.entries) {
|
|
58
|
-
designItem._withoutUndoSetStyle(s.name, s.value);
|
|
62
|
+
designItem._withoutUndoSetStyle(s.name, s.value, s.important);
|
|
59
63
|
if (manualCreatedElement) {
|
|
60
|
-
element.style
|
|
64
|
+
element.style.setProperty(s.name, s.value, s.important ? 'important' : '');
|
|
61
65
|
}
|
|
62
66
|
}
|
|
63
67
|
}
|
|
@@ -77,7 +81,8 @@ export class NodeHtmlParserService {
|
|
|
77
81
|
if (di.node instanceof HTMLTemplateElement && di.getAttribute('shadowrootmode') == 'open') {
|
|
78
82
|
try {
|
|
79
83
|
const shadow = designItem.node.attachShadow({ mode: 'open' });
|
|
80
|
-
|
|
84
|
+
const content = di.node.content.cloneNode(true);
|
|
85
|
+
shadow.appendChild(content);
|
|
81
86
|
}
|
|
82
87
|
catch (err) {
|
|
83
88
|
console.error("error attaching shadowdom", err);
|
|
@@ -86,21 +91,101 @@ export class NodeHtmlParserService {
|
|
|
86
91
|
}
|
|
87
92
|
}
|
|
88
93
|
else if (item.nodeType == 3) {
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
94
|
+
const document = instanceServiceContainer.designerCanvas.rootDesignItem.document;
|
|
95
|
+
const parentElementName = item.parentNode?.rawTagName?.toLowerCase();
|
|
96
|
+
const text = DomConverter.isRawTextElementName(parentElementName) ? item.rawText : item.text;
|
|
97
|
+
let element = DesignItem.updateRenderedNode(serviceContainer, document.createTextNode(text));
|
|
92
98
|
designItem = DesignItem.GetOrCreateDesignItem(element, item, serviceContainer, instanceServiceContainer);
|
|
93
99
|
if (!snippet && instanceServiceContainer.designItemDocumentPositionService)
|
|
94
100
|
instanceServiceContainer.designItemDocumentPositionService.setPosition(designItem, { start: item.range[0] + positionOffset, length: item.range[1] - item.range[0] });
|
|
95
101
|
}
|
|
96
102
|
else if (item.nodeType == 8) {
|
|
97
|
-
let element = document.createComment(item.rawText);
|
|
103
|
+
let element = DesignItem.updateRenderedNode(serviceContainer, document.createComment(item.rawText));
|
|
98
104
|
designItem = DesignItem.GetOrCreateDesignItem(element, item, serviceContainer, instanceServiceContainer);
|
|
99
105
|
if (!snippet && instanceServiceContainer.designItemDocumentPositionService)
|
|
100
106
|
instanceServiceContainer.designItemDocumentPositionService.setPosition(designItem, { start: item.range[0] + positionOffset, length: item.range[1] - item.range[0] });
|
|
101
107
|
}
|
|
108
|
+
if (designItem) {
|
|
109
|
+
const renderedNode = DesignItem.updateRenderedNode(serviceContainer, designItem.node);
|
|
110
|
+
if (renderedNode !== designItem.node)
|
|
111
|
+
designItem.replaceNode(renderedNode);
|
|
112
|
+
designItem.refreshRenderedDesignItem();
|
|
113
|
+
}
|
|
102
114
|
if (serviceContainer.designItemService.finishedDesignItem)
|
|
103
115
|
serviceContainer.designItemService.finishedDesignItem(designItem);
|
|
104
116
|
return designItem;
|
|
105
117
|
}
|
|
118
|
+
_addAttributeSourceParts(item, designItem, serviceContainer, snippet, positionOffset) {
|
|
119
|
+
const positionService = designItem.instanceServiceContainer.designItemDocumentPositionService;
|
|
120
|
+
if (snippet || !positionService || !item.rawAttrs)
|
|
121
|
+
return;
|
|
122
|
+
const rawAttrs = item.rawAttrs;
|
|
123
|
+
const openingTag = this._getOpeningTagText(item.outerHTML ?? '');
|
|
124
|
+
const rawAttrsIndex = openingTag.indexOf(rawAttrs);
|
|
125
|
+
if (rawAttrsIndex < 0)
|
|
126
|
+
return;
|
|
127
|
+
const rawAttrsStart = item.range[0] + positionOffset + rawAttrsIndex;
|
|
128
|
+
const attributeRegex = /([^\s"'<>/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g;
|
|
129
|
+
let match;
|
|
130
|
+
while (match = attributeRegex.exec(rawAttrs)) {
|
|
131
|
+
const attributeName = match[1];
|
|
132
|
+
const attributeStart = rawAttrsStart + match.index;
|
|
133
|
+
positionService.addSourcePart({
|
|
134
|
+
designItem,
|
|
135
|
+
kind: 'attribute',
|
|
136
|
+
key: `attribute:${attributeName}`,
|
|
137
|
+
name: attributeName,
|
|
138
|
+
textRange: { start: attributeStart, length: match[0].length }
|
|
139
|
+
});
|
|
140
|
+
const valueRange = this._getAttributeValueRange(match[0]);
|
|
141
|
+
if (valueRange) {
|
|
142
|
+
const textRange = { start: attributeStart + valueRange.start, length: valueRange.length };
|
|
143
|
+
positionService.addSourcePart({
|
|
144
|
+
designItem,
|
|
145
|
+
kind: 'attribute-value',
|
|
146
|
+
key: `attribute:${attributeName}/value`,
|
|
147
|
+
name: attributeName,
|
|
148
|
+
textRange
|
|
149
|
+
});
|
|
150
|
+
const context = { designItem, sourceKind: 'attribute', name: attributeName, value: valueRange.value, valueTextRange: textRange };
|
|
151
|
+
for (const provider of serviceContainer.sourceMapProviders) {
|
|
152
|
+
if (provider.canMap(context))
|
|
153
|
+
positionService.addSourceParts(provider.map(context));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
_getOpeningTagText(outerHtml) {
|
|
159
|
+
let quote = null;
|
|
160
|
+
for (let i = 0; i < outerHtml.length; i++) {
|
|
161
|
+
const char = outerHtml[i];
|
|
162
|
+
if (quote) {
|
|
163
|
+
if (char === quote)
|
|
164
|
+
quote = null;
|
|
165
|
+
}
|
|
166
|
+
else if (char === '"' || char === '\'') {
|
|
167
|
+
quote = char;
|
|
168
|
+
}
|
|
169
|
+
else if (char === '>') {
|
|
170
|
+
return outerHtml.substring(0, i + 1);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return outerHtml;
|
|
174
|
+
}
|
|
175
|
+
_getAttributeValueRange(attributeText) {
|
|
176
|
+
const equalsIndex = attributeText.indexOf('=');
|
|
177
|
+
if (equalsIndex < 0)
|
|
178
|
+
return null;
|
|
179
|
+
let valueStart = equalsIndex + 1;
|
|
180
|
+
while (valueStart < attributeText.length && /\s/.test(attributeText[valueStart]))
|
|
181
|
+
valueStart++;
|
|
182
|
+
const quote = attributeText[valueStart];
|
|
183
|
+
if (quote === '"' || quote === '\'') {
|
|
184
|
+
const contentStart = valueStart + 1;
|
|
185
|
+
const contentEnd = attributeText.lastIndexOf(quote);
|
|
186
|
+
const length = Math.max(0, contentEnd - contentStart);
|
|
187
|
+
return { start: contentStart, length, value: attributeText.substring(contentStart, contentStart + length) };
|
|
188
|
+
}
|
|
189
|
+
return { start: valueStart, length: attributeText.length - valueStart, value: attributeText.substring(valueStart) };
|
|
190
|
+
}
|
|
106
191
|
}
|
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.
|
|
4
|
+
"version": "0.1.15",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/service/htmlParserService/NodeHtmlParserService.js",
|
|
7
7
|
"author": "jochen.kuehner@gmx.de",
|