update `<style>` patterns

master
Brad Cornes 2021-09-10 12:33:35 +01:00
parent a1e085d0cd
commit 6367de3870
2 changed files with 6 additions and 17 deletions

View File

@ -9,10 +9,7 @@ export interface LanguageBoundaries {
css: Range[] css: Range[]
} }
export function getLanguageBoundaries( export function getLanguageBoundaries(state: State, doc: TextDocument): LanguageBoundaries | null {
state: State,
doc: TextDocument
): LanguageBoundaries | null {
if (isVueDoc(doc)) { if (isVueDoc(doc)) {
let text = doc.getText() let text = doc.getText()
let blocks = findAll( let blocks = findAll(
@ -23,10 +20,7 @@ export function getLanguageBoundaries(
let cssRanges: Range[] = [] let cssRanges: Range[] = []
for (let i = 0; i < blocks.length; i++) { for (let i = 0; i < blocks.length; i++) {
let range = { let range = {
start: indexToPosition( start: indexToPosition(text, blocks[i].index + blocks[i].groups.open.length),
text,
blocks[i].index + blocks[i].groups.open.length
),
end: indexToPosition( end: indexToPosition(
text, text,
blocks[i].index + blocks[i][0].length - blocks[i].groups.close.length blocks[i].index + blocks[i][0].length - blocks[i].groups.close.length
@ -48,7 +42,7 @@ export function getLanguageBoundaries(
if (isHtmlDoc(state, doc) || isJsDoc(state, doc) || isSvelteDoc(doc)) { if (isHtmlDoc(state, doc) || isJsDoc(state, doc) || isSvelteDoc(doc)) {
let text = doc.getText() let text = doc.getText()
let styleBlocks = findAll( let styleBlocks = findAll(
/(?<open><style(?:\s[^>]*>|>)).*?(?<close><\/style>|$)/gis, /(?<open><style(?:\s[^>]*[^\/]>|>|[^\/]>)).*?(?<close><\/style>|$)/gis,
text text
) )
let htmlRanges: Range[] = [] let htmlRanges: Range[] = []
@ -61,15 +55,10 @@ export function getLanguageBoundaries(
end: indexToPosition(text, styleBlocks[i].index), end: indexToPosition(text, styleBlocks[i].index),
}) })
cssRanges.push({ cssRanges.push({
start: indexToPosition( start: indexToPosition(text, styleBlocks[i].index + styleBlocks[i].groups.open.length),
text,
styleBlocks[i].index + styleBlocks[i].groups.open.length
),
end: indexToPosition( end: indexToPosition(
text, text,
styleBlocks[i].index + styleBlocks[i].index + styleBlocks[i][0].length - styleBlocks[i].groups.close.length
styleBlocks[i][0].length -
styleBlocks[i].groups.close.length
), ),
}) })
currentIndex = styleBlocks[i].index + styleBlocks[i][0].length currentIndex = styleBlocks[i].index + styleBlocks[i][0].length

View File

@ -44,7 +44,7 @@ export function isInsideTag(str: string, tag: string | string[]): boolean {
let close = 0 let close = 0
let match: RegExpExecArray let match: RegExpExecArray
let tags = Array.isArray(tag) ? tag : [tag] let tags = Array.isArray(tag) ? tag : [tag]
let regex = new RegExp(`<(?<slash>/?)(?:${tags.join('|')})\\b`, 'ig') let regex = new RegExp(`<(?<slash>/?)(?:${tags.join('|')})(?:\\s[^>]*[^\/]>|>|[^\/]>)`, 'ig')
while ((match = regex.exec(str)) !== null) { while ((match = regex.exec(str)) !== null) {
if (match.groups.slash) { if (match.groups.slash) {
close += 1 close += 1