Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) correctly extract script/style tag when there're whitespace before block name #1494

Merged
merged 4 commits into from May 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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