Skip to content

Commit 64c5bca

Browse files
committed
Fixes #135
1 parent 5bf2c1a commit 64c5bca

File tree

5 files changed

+50
-20
lines changed

5 files changed

+50
-20
lines changed

src/ast.js

+1-20
Original file line numberDiff line numberDiff line change
@@ -829,25 +829,6 @@ class AstSerializer {
829829
return rawContent;
830830
}
831831

832-
addImpliedWebCAttributes(node) {
833-
// if(AstQuery.getTagName(node) === "template") {
834-
if(AstQuery.isDeclarativeShadowDomNode(node)) {
835-
AstModify.addAttribute(node, AstSerializer.attrs.RAW, "");
836-
}
837-
838-
// webc:type="js" (WebC v0.9.0+) has implied webc:is="template" webc:nokeep
839-
if(AstQuery.getAttributeValue(node, AstSerializer.attrs.TYPE) === AstSerializer.transformTypes.JS) {
840-
// this check is perhaps unnecessary since KEEP has a higher precedence than NOKEEP
841-
if(!AstQuery.hasAttribute(node, AstSerializer.attrs.KEEP)) {
842-
AstModify.addAttribute(node, AstSerializer.attrs.NOKEEP, "");
843-
}
844-
845-
if(!AstQuery.hasAttribute(node, AstSerializer.attrs.IS)) {
846-
AstModify.addAttribute(node, AstSerializer.attrs.IS, "template");
847-
}
848-
}
849-
}
850-
851832
addToInheritedBuckets(newBucketName, node, tagName, options) {
852833
if(!newBucketName) {
853834
return;
@@ -978,7 +959,7 @@ class AstSerializer {
978959
return { html, currentNodeMetadata };
979960
}
980961

981-
this.addImpliedWebCAttributes(node);
962+
ComponentManager.addImpliedWebCAttributes(node);
982963

983964
let tagName = AstQuery.getTagName(node);
984965
let content = "";

src/componentManager.js

+32
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createHash } from "crypto";
22

33
import { WebC } from "../webc.js";
44
import { AstQuery } from "./astQuery.js";
5+
import { AstModify } from "./astModify.js";
56
import { AstSerializer } from "./ast.js";
67
import { ModuleScript } from "./moduleScript.cjs";
78

@@ -133,6 +134,31 @@ class ComponentManager {
133134
return this.getDigest(hash);
134135
}
135136
}
137+
138+
/* Careful, this one mutates */
139+
static addImpliedWebCAttributes(node) {
140+
if(node._webcImpliedAttributesAdded) {
141+
return;
142+
}
143+
144+
node._webcImpliedAttributesAdded = true;
145+
146+
if(AstQuery.isDeclarativeShadowDomNode(node)) {
147+
AstModify.addAttribute(node, AstSerializer.attrs.RAW, "");
148+
}
149+
150+
// webc:type="js" (WebC v0.9.0+) has implied webc:is="template" webc:nokeep
151+
if(AstQuery.getAttributeValue(node, AstSerializer.attrs.TYPE) === AstSerializer.transformTypes.JS) {
152+
// this check is perhaps unnecessary since KEEP has a higher precedence than NOKEEP
153+
if(!AstQuery.hasAttribute(node, AstSerializer.attrs.KEEP)) {
154+
AstModify.addAttribute(node, AstSerializer.attrs.NOKEEP, "");
155+
}
156+
157+
if(!AstQuery.hasAttribute(node, AstSerializer.attrs.IS)) {
158+
AstModify.addAttribute(node, AstSerializer.attrs.IS, "template");
159+
}
160+
}
161+
}
136162

137163
has(filePath) {
138164
return filePath in this.components;
@@ -178,6 +204,12 @@ class ComponentManager {
178204
let hasDeclarativeShadowDom = AstQuery.hasDeclarativeShadowDomChild(ast);
179205

180206
let topLevelNodes = AstQuery.getTopLevelNodes(ast);
207+
208+
// important for ignoreComponentParentTag, issue #135
209+
for(let node of topLevelNodes) {
210+
ComponentManager.addImpliedWebCAttributes(node);
211+
}
212+
181213
let rootAttributeMode = this.getRootMode(topLevelNodes);
182214
let ignoreRootTag = this.ignoreComponentParentTag(topLevelNodes, rootAttributeMode, hasDeclarativeShadowDom);
183215

test/issue-135-test.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import test from "ava";
2+
import { WebC } from "../webc.js";
3+
4+
test("script render function implied template should still be HTML-only #135", async t => {
5+
let component = new WebC();
6+
7+
component.setInputPath("./test/stubs/issue-135/page.webc");
8+
component.defineComponents("./test/stubs/issue-135/component.webc");
9+
10+
let { html } = await component.compile();
11+
12+
t.is(html.trim(), `<p>hello</p>`);
13+
});

test/stubs/issue-135/component.webc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script webc:type="js">
2+
"<p>hello</p>";
3+
</script>

test/stubs/issue-135/page.webc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<component></component>

0 commit comments

Comments
 (0)