Skip to content

Commit

Permalink
(fix) correctly extract script/style tag when there're whitespace bef…
Browse files Browse the repository at this point in the history
…ore block name (#1494)

#1430
  • Loading branch information
jasonlyu123 committed May 28, 2022
1 parent 7a443cc commit f8e58d8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
17 changes: 8 additions & 9 deletions packages/language-server/src/lib/documents/utils.ts
Expand Up @@ -39,19 +39,18 @@ function parseAttributes(
}
}

const regexIf = new RegExp('{#if\\s.*?}', 'gms');
const regexIfEnd = new RegExp('{/if}', 'gms');
const regexEach = new RegExp('{#each\\s.*?}', 'gms');
const regexEachEnd = new RegExp('{/each}', 'gms');
const regexAwait = new RegExp('{#await\\s.*?}', 'gms');
const regexAwaitEnd = new RegExp('{/await}', 'gms');
const regexHtml = new RegExp('{@html\\s.*?', 'gms');
const regexIf = new RegExp('{\\s*#if\\s.*?}', 'gms');
const regexIfEnd = new RegExp('{\\s*/if}', 'gms');
const regexEach = new RegExp('{\\s*#each\\s.*?}', 'gms');
const regexEachEnd = new RegExp('{\\s*/each}', 'gms');
const regexAwait = new RegExp('{\\s*#await\\s.*?}', 'gms');
const regexAwaitEnd = new RegExp('{\\s*/await}', 'gms');
const regexHtml = new RegExp('{\\s*@html\\s.*?', 'gms');

/**
* Extracts a tag (style or script) from the given text
* and returns its start, end and the attributes on that tag.
*
* @param source text content to extract tag from
* @param text text content to extract tag from
* @param tag the tag to extract
*/
function extractTags(
Expand Down
17 changes: 17 additions & 0 deletions packages/language-server/test/lib/documents/utils.test.ts
Expand Up @@ -209,6 +209,23 @@ describe('document/utils', () => {
});
});

it("extracts top level script when there're whitespace before block name", () => {
const text = `
<script>top level script</script>
{ #if myvar } {/if}
`;

assert.deepStrictEqual(extractScriptTags(text)?.script, {
content: 'top level script',
attributes: {},
start: 25,
end: 41,
startPos: Position.create(1, 24),
endPos: Position.create(1, 40),
container: { start: 17, end: 50 }
});
});

it('ignores script tag in svelte:head', () => {
// https://github.com/sveltejs/language-tools/issues/143#issuecomment-636422045
const text = `
Expand Down

0 comments on commit f8e58d8

Please sign in to comment.