Skip to content

Commit b996e3f

Browse files
authored
Merge pull request #11 from jasonlyu123/script-style-like-component
script and style like component
2 parents 6f7ad58 + 3cc648c commit b996e3f

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

src/htmlxparser.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,25 @@ function walkAst(doc: DefaultTreeElement, action: (c: DefaultTreeElement) => voi
1515
export function findVerbatimElements(htmlx: string) {
1616
let elements:Node[] = []
1717
let tag_names = ['script', 'style'];
18-
18+
1919
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+
2131
walkAst(doc as DefaultTreeElement, el => {
2232
if (tag_names.includes(el.nodeName)) {
2333
let content = (el.childNodes && el.childNodes.length > 0) ? el.childNodes[0] as DefaultTreeTextNode : null;
34+
if(!checkCase(content, el)) {
35+
return;
36+
}
2437
elements.push({
2538
start: el.sourceCodeLocation.startOffset,
2639
end: el.sourceCodeLocation.endOffset,
@@ -51,7 +64,6 @@ export function findVerbatimElements(htmlx: string) {
5164
return elements;
5265
}
5366

54-
5567
export function blankVerbatimContent(htmlx: string, verbatimElements: Node[]) {
5668
let output = htmlx;
5769
for (var node of verbatimElements) {
@@ -67,14 +79,14 @@ export function blankVerbatimContent(htmlx: string, verbatimElements: Node[]) {
6779

6880

6981
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.
7183
//HTMLx spec says they should just be retained after processing as is, so this is fine
7284
let verbatimElements = findVerbatimElements(htmlx);
7385
let deconstructed = blankVerbatimContent(htmlx, verbatimElements);
74-
86+
7587
//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+
7890
//restore our script and style tags as nodes to maintain validity with HTMLx
7991
for (var s of verbatimElements) {
8092
svelteHtmlxAst.children.push(s);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<></>;function render() {
2+
3+
let Script, Style;
4+
;
5+
<>
6+
7+
<Script>
8+
<p></p>
9+
</Script>
10+
<Style /></>
11+
return { props: {}, slots: {} }}
12+
13+
export default class {
14+
$$prop_def = __sveltets_partial(render().props)
15+
$$slot_def = render().slots
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
let Script, Style;
3+
</script>
4+
5+
<Script>
6+
<p></p>
7+
</Script>
8+
<Style />

0 commit comments

Comments
 (0)