@@ -15,12 +15,25 @@ function walkAst(doc: DefaultTreeElement, action: (c: DefaultTreeElement) => voi
15
15
export function findVerbatimElements ( htmlx : string ) {
16
16
let elements :Node [ ] = [ ]
17
17
let tag_names = [ 'script' , 'style' ] ;
18
-
18
+
19
19
let doc : DefaultTreeDocumentFragment = parse5 . parseFragment ( htmlx , { sourceCodeLocationInfo : true } ) as DefaultTreeDocumentFragment ;
20
-
20
+
21
+ const checkCase = ( content : DefaultTreeTextNode , el : parse5 . DefaultTreeElement ) => {
22
+ const orgStart = el . sourceCodeLocation . startOffset || 0 ;
23
+ const orgEnd = el . sourceCodeLocation . endOffset || 0 ;
24
+ const outerHtml = htmlx . substring ( orgStart , orgEnd ) ;
25
+ const onlyTag = content ? outerHtml . replace ( content . value , '' ) : outerHtml ;
26
+
27
+ return tag_names . some ( tag => onlyTag . match ( tag ) ) ;
28
+ }
29
+
30
+
21
31
walkAst ( doc as DefaultTreeElement , el => {
22
32
if ( tag_names . includes ( el . nodeName ) ) {
23
33
let content = ( el . childNodes && el . childNodes . length > 0 ) ? el . childNodes [ 0 ] as DefaultTreeTextNode : null ;
34
+ if ( ! checkCase ( content , el ) ) {
35
+ return ;
36
+ }
24
37
elements . push ( {
25
38
start : el . sourceCodeLocation . startOffset ,
26
39
end : el . sourceCodeLocation . endOffset ,
@@ -51,7 +64,6 @@ export function findVerbatimElements(htmlx: string) {
51
64
return elements ;
52
65
}
53
66
54
-
55
67
export function blankVerbatimContent ( htmlx : string , verbatimElements : Node [ ] ) {
56
68
let output = htmlx ;
57
69
for ( var node of verbatimElements ) {
@@ -67,14 +79,14 @@ export function blankVerbatimContent(htmlx: string, verbatimElements: Node[]) {
67
79
68
80
69
81
export function parseHtmlx ( htmlx : string ) : Node {
70
- //Svelte tries to parse style and script tags which doesn't play well with typescript, so we blank them out.
82
+ //Svelte tries to parse style and script tags which doesn't play well with typescript, so we blank them out.
71
83
//HTMLx spec says they should just be retained after processing as is, so this is fine
72
84
let verbatimElements = findVerbatimElements ( htmlx ) ;
73
85
let deconstructed = blankVerbatimContent ( htmlx , verbatimElements ) ;
74
-
86
+
75
87
//extract the html content parsed as htmlx this excludes our script and style tags
76
- let svelteHtmlxAst = compiler . parse ( deconstructed ) . html ;
77
-
88
+ let svelteHtmlxAst = compiler . parse ( deconstructed ) . html ;
89
+
78
90
//restore our script and style tags as nodes to maintain validity with HTMLx
79
91
for ( var s of verbatimElements ) {
80
92
svelteHtmlxAst . children . push ( s ) ;
0 commit comments