Skip to content

Commit f102dd1

Browse files
committed
Prevent removing styles/scripts within templates
A template should not be modified. Scripts and styles (and anything else) should remain within the template. This fixes that by first extracting templates from within the strings and processing without them. Fixes #7
1 parent 2b09d52 commit f102dd1

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

codepen-data.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
var scriptRegExp = /<script\s([^>]+)>([\s\S]*?)<\/script>/ig;
22
var styleRegExp = /<style>([\s\S]*?)<\/style>/i;
3+
var templateRegExp = /<template>([\s\S]*?)<\/template>/ig;
34
var moduleTest = /type=["']([\w\/]+)["']/;
45
var srcTest = /src=/;
56
var DEFAULT_EDITORS = "0011";
67

78
var types = {
89
html: function htmlType(text) {
9-
1010
var result;
11-
var HTML = text;
12-
13-
text.replace(scriptRegExp, function(match, attrs, code) {
11+
var HTML = text;
12+
var textWithoutTemplates = text.replace(templateRegExp, "");
1413

14+
textWithoutTemplates.replace(scriptRegExp, function(match, attrs, code) {
1515
var matchTest = attrs.match(moduleTest);
16+
var HTMLwithoutTemplates = HTML.replace(templateRegExp, "");
1617

1718
// This has a src="". We look for codepen-external
1819
if(srcTest.test(attrs)) {
1920

2021
}
2122
// It doesn't have a src, so we assume this has a body
2223
else if (matchTest) {
23-
2424
HTML = HTML.replace(match, "").trim();
2525
var CSS;
26-
var styleResults = HTML.match(styleRegExp);
26+
var styleResults = HTMLwithoutTemplates.match(styleRegExp);
2727
if (styleResults) {
2828
HTML = HTML.replace(styleResults[0], "").trim();
2929
CSS = styleResults[1].trim();

test.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,53 @@ describe("bit-docs-html-codepen-link", function() {
136136

137137
assert(data, "got data");
138138
assert.equal(data.html.trim(), "<div>Hello world</div>");
139-
})
139+
});
140+
141+
it("Does not remove styles from within a template", function() {
142+
var data = codepenData.html(`
143+
<template>
144+
<style>.root { display: block; }</style>
145+
</template>
146+
<script type="module"></script>
147+
`);
148+
149+
assert(!data.css, "There should not be css");
150+
assert.equal(data.html.trim(), `
151+
<template>
152+
<style>.root { display: block; }</style>
153+
</template>
154+
`.trim());
155+
});
156+
157+
it("Does not remove scripts from within a template", function() {
158+
var data = codepenData.html(`
159+
<template>
160+
<script>console.log('testing');</script>
161+
</template>
162+
<script type="module"></script>
163+
`);
164+
165+
assert(!data.js, "There should not be js");
166+
assert.equal(data.html.trim(), `
167+
<template>
168+
<script>console.log('testing');</script>
169+
</template>
170+
`.trim());
171+
});
172+
173+
it("Does not remove module scripts from within a template", function() {
174+
var data = codepenData.html(`
175+
<template>
176+
<script type="module">console.log('testing');</script>
177+
</template>
178+
<script type="module"></script>
179+
`);
180+
181+
assert(!data.js, "There should not be js");
182+
assert.equal(data.html.trim(), `
183+
<template>
184+
<script type="module">console.log('testing');</script>
185+
</template>
186+
`.trim());
187+
});
140188
});

0 commit comments

Comments
 (0)