Skip to content

Commit

Permalink
(fix) adjust wrong "is inside other tag" check (#247)
Browse files Browse the repository at this point in the history
Fixes #244
  • Loading branch information
dummdidumm authored Aug 26, 2021
1 parent 3d2ef3d commit 0c77275
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# prettier-plugin-svelte changelog

## Unreleased

* (fix) Adjust "is inside other tag"-checks when snipping tags ([#244](https://github.com/sveltejs/prettier-plugin-svelte/issues/244))

## 2.3.1

* (fix) More robust check for `hardline` to prevent trailing empty lines in `<style>`/`<script>` tags ([#222](https://github.com/sveltejs/prettier-plugin-svelte/issues/222))
Expand Down
13 changes: 4 additions & 9 deletions src/lib/snipTagContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ export function snipScriptAndStyleTagContent(source: string): string {
const styleMatchSpans = getMatchIndexes('style');

return snipTagContent(
snipTagContent(source, 'script', '{}', scriptMatchSpans, styleMatchSpans),
snipTagContent(source, 'script', '{}', styleMatchSpans),
'style',
'',
styleMatchSpans,
scriptMatchSpans,
);

Expand All @@ -26,23 +25,19 @@ export function snipScriptAndStyleTagContent(source: string): string {
_source: string,
tagName: string,
placeholder: string,
ownSpans: [number, number][],
otherSpans: [number, number][],
) {
const regex = getRegexp(tagName);
let idx = 0;
return _source.replace(regex, (match, attributes, content) => {
if (match.startsWith('<!--') || withinOtherSpan(idx)) {
return _source.replace(regex, (match, attributes, content, index) => {
if (match.startsWith('<!--') || withinOtherSpan(index)) {
return match;
}
const encodedContent = Buffer.from(content).toString('base64');
return `<${tagName}${attributes} ${snippedTagContentAttribute}="${encodedContent}">${placeholder}</${tagName}>`;
});

function withinOtherSpan(idx: number) {
return otherSpans.some(
(otherSpan) => ownSpans[idx][0] > otherSpan[0] && ownSpans[idx][1] < otherSpan[1],
);
return otherSpans.some((otherSpan) => idx > otherSpan[0] && idx < otherSpan[1]);
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/printer/samples/script-with-html-comment-and-style.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
const abc = "<!-- foo -->";
</script>

<style>
div {
color: green;
}
</style>

0 comments on commit 0c77275

Please sign in to comment.