|
1 | | -import parse5, { |
2 | | - DefaultTreeDocumentFragment, |
3 | | - DefaultTreeElement, |
4 | | - DefaultTreeTextNode, |
5 | | -} from 'parse5'; |
6 | 1 | import compiler from 'svelte/compiler'; |
7 | 2 | import { Node } from 'estree-walker'; |
8 | 3 |
|
9 | | -function walkAst(doc: DefaultTreeElement, action: (c: DefaultTreeElement) => void) { |
10 | | - action(doc); |
11 | | - if (!doc.childNodes) return; |
12 | | - for (let i = 0; i < doc.childNodes.length; i++) { |
13 | | - walkAst(doc.childNodes[i] as DefaultTreeElement, action); |
14 | | - } |
| 4 | +function parseAttributeValue(value: string): string { |
| 5 | + return /^['"]/.test(value) ? value.slice(1, -1) : value; |
15 | 6 | } |
16 | 7 |
|
17 | | -export function findVerbatimElements(htmlx: string) { |
18 | | - const elements: Node[] = []; |
19 | | - const tagNames = ['script', 'style']; |
20 | | - |
21 | | - const parseOpts = { sourceCodeLocationInfo: true }; |
22 | | - const doc = parse5.parseFragment(htmlx, parseOpts) as DefaultTreeDocumentFragment; |
23 | | - |
24 | | - const checkCase = (content: DefaultTreeTextNode, el: parse5.DefaultTreeElement) => { |
25 | | - const orgStart = el.sourceCodeLocation.startOffset || 0; |
26 | | - const orgEnd = el.sourceCodeLocation.endOffset || 0; |
27 | | - const outerHtml = htmlx.substring(orgStart, orgEnd); |
28 | | - const onlyTag = content ? outerHtml.replace(content.value, '') : outerHtml; |
29 | | - |
30 | | - return tagNames.some((tag) => onlyTag.match(tag)); |
31 | | - }; |
32 | | - |
33 | | - walkAst(doc as DefaultTreeElement, (el) => { |
34 | | - const parseValue = (attr: parse5.Attribute) => { |
35 | | - const sourceCodeLocation = el.sourceCodeLocation.attrs[attr.name]; |
36 | | - const { startOffset, endOffset } = sourceCodeLocation; |
37 | | - const beforeAttrEnd = htmlx.substring(0, endOffset); |
38 | | - const valueStartIndex = beforeAttrEnd.indexOf('=', startOffset); |
39 | | - const isBare = valueStartIndex === -1; |
40 | | - |
41 | | - return { |
| 8 | +function parseAttributes(str: string, start: number) { |
| 9 | + const attrs: Node[] = []; |
| 10 | + str.split(/\s+/) |
| 11 | + .filter(Boolean) |
| 12 | + .forEach((attr) => { |
| 13 | + const attrStart = start + str.indexOf(attr); |
| 14 | + const [name, value] = attr.split('='); |
| 15 | + attrs[name] = value ? parseAttributeValue(value) : name; |
| 16 | + attrs.push({ |
42 | 17 | type: 'Attribute', |
43 | | - name: attr.name, |
44 | | - value: isBare || [ |
| 18 | + name, |
| 19 | + value: !value || [ |
45 | 20 | { |
46 | 21 | type: 'Text', |
47 | | - start: valueStartIndex + 1, |
48 | | - end: endOffset, |
49 | | - raw: attr.value, |
| 22 | + start: attrStart + attr.indexOf('=') + 1, |
| 23 | + end: attrStart + attr.length, |
| 24 | + raw: parseAttributeValue(value), |
50 | 25 | }, |
51 | 26 | ], |
52 | | - start: startOffset, |
53 | | - end: endOffset, |
54 | | - }; |
55 | | - }; |
| 27 | + start: attrStart, |
| 28 | + end: attrStart + attr.length, |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + return attrs; |
| 33 | +} |
56 | 34 |
|
57 | | - if (tagNames.includes(el.nodeName)) { |
58 | | - const hasNodes = el.childNodes && el.childNodes.length > 0; |
59 | | - const content = hasNodes ? (el.childNodes[0] as DefaultTreeTextNode) : null; |
60 | | - if (!checkCase(content, el)) { |
61 | | - return; |
62 | | - } |
| 35 | +function extractTag(htmlx: string, tag: 'script' | 'style') { |
| 36 | + const exp = new RegExp(`(<${tag}([\\S\\s]*?)>)([\\S\\s]*?)<\\/${tag}>`, 'g'); |
| 37 | + const matches: Node[] = []; |
63 | 38 |
|
64 | | - elements.push({ |
65 | | - start: el.sourceCodeLocation.startOffset, |
66 | | - end: el.sourceCodeLocation.endOffset, |
67 | | - type: el.nodeName[0].toUpperCase() + el.nodeName.substr(1), |
68 | | - attributes: !el.attrs ? [] : el.attrs.map((a) => parseValue(a)), |
69 | | - content: |
70 | | - content === null |
71 | | - ? { |
72 | | - type: 'Text', |
73 | | - start: el.sourceCodeLocation.startTag.endCol, |
74 | | - end: el.sourceCodeLocation.endTag.startCol, |
75 | | - value: '', |
76 | | - raw: '', |
77 | | - } |
78 | | - : { |
79 | | - type: 'Text', |
80 | | - start: content.sourceCodeLocation.startOffset, |
81 | | - end: content.sourceCodeLocation.endOffset, |
82 | | - value: content.value, |
83 | | - raw: content.value, |
84 | | - }, |
85 | | - }); |
| 39 | + let match: RegExpExecArray | null = null; |
| 40 | + while ((match = exp.exec(htmlx)) != null) { |
| 41 | + const content = match[3]; |
| 42 | + |
| 43 | + if (!content) { |
| 44 | + // Self-closing/empty tags don't need replacement |
| 45 | + continue; |
86 | 46 | } |
87 | | - }); |
88 | 47 |
|
89 | | - return elements; |
| 48 | + const start = match.index + match[1].length; |
| 49 | + const end = start + content.length; |
| 50 | + const containerStart = match.index; |
| 51 | + const containerEnd = match.index + match[0].length; |
| 52 | + |
| 53 | + matches.push({ |
| 54 | + start: containerStart, |
| 55 | + end: containerEnd, |
| 56 | + type: tag === 'style' ? 'Style' : 'Script', |
| 57 | + attributes: parseAttributes(match[2], containerStart + `<${tag}`.length), |
| 58 | + content: { |
| 59 | + type: 'Text', |
| 60 | + start, |
| 61 | + end, |
| 62 | + value: content, |
| 63 | + raw: content, |
| 64 | + }, |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + return matches; |
| 69 | +} |
| 70 | + |
| 71 | +function findVerbatimElements(htmlx: string) { |
| 72 | + return [...extractTag(htmlx, 'script'), ...extractTag(htmlx, 'style')]; |
90 | 73 | } |
91 | 74 |
|
92 | | -export function blankVerbatimContent(htmlx: string, verbatimElements: Node[]) { |
| 75 | +function blankVerbatimContent(htmlx: string, verbatimElements: Node[]) { |
93 | 76 | let output = htmlx; |
94 | 77 | for (const node of verbatimElements) { |
95 | 78 | const content = node.content; |
|
0 commit comments