Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/__tests__/source/scoped_css.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const md5 = (content: string) => {
return createHash('md5').update(content).digest('hex')
}


describe('source scoped_css', () => {
let appCon: Element
beforeAll(() => {
Expand Down Expand Up @@ -101,12 +100,12 @@ describe('source scoped_css', () => {
setAppName('test-app3')
// 动态创建style
const dynamicStyle = document.createElement('style')
dynamicStyle.textContent = '@font-face {font-family: test-font;} @media screen and (max-width: 300px) {body {background:lightblue;}} @supports (display: grid) {div {display: grid;}} @unknown {}'
dynamicStyle.textContent = '@font-face {font-family: test-font;} @property --tw-translate-x { syntax: "*"; inherits: false; initial-value: 0; } @media screen and (max-width: 300px) {body {background:lightblue;}} @supports (display: grid) {div {display: grid;}} @unknown {}'

document.head.appendChild(dynamicStyle)

defer(() => {
expect(dynamicStyle.textContent).toBe('@font-face {font-family: test-font;} @media screen and (max-width: 300px) {micro-app[name=test-app3] micro-app-body{background:lightblue;}} @supports (display: grid) {micro-app[name=test-app3] div{display: grid;}} micro-app[name=test-app3] @unknown{}')
expect(dynamicStyle.textContent).toBe('@font-face {font-family: test-font;} @property --tw-translate-x { syntax: "*"; inherits: false; initial-value: 0; } @media screen and (max-width: 300px) {micro-app[name=test-app3] micro-app-body{background:lightblue;}} @supports (display: grid) {micro-app[name=test-app3] div{display: grid;}} micro-app[name=test-app3] @unknown{}')
resolve(true)
})
}, false)
Expand Down Expand Up @@ -405,6 +404,12 @@ describe('source scoped_css', () => {
document.head.appendChild(dynamicStyle7)
expect(dynamicStyle7.textContent).toBe('micro-app[name=test-app11] .test1, micro-app[name=test-app11] .test2{color: red}')

// keep commas inside pseudo-class arguments and escaped arbitrary values
const dynamicStyle8 = document.createElement('style')
dynamicStyle8.textContent = ':is(.a, .b):has(+ .c, + .d){} .grid-cols-\\[minmax\\(0\\,_1fr\\)\\]{grid-template-columns:minmax(0, 1fr)}'
document.head.appendChild(dynamicStyle8)
expect(dynamicStyle8.textContent).toBe('micro-app[name=test-app11] :is(.a, .b):has(+ .c, + .d){} micro-app[name=test-app11] .grid-cols-\\[minmax\\(0\\,_1fr\\)\\]{grid-template-columns:minmax(0, 1fr)}')

resolve(true)
}, false)
})
Expand Down
47 changes: 45 additions & 2 deletions src/sandbox/scoped_css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class CSSParser {
return match.replace(p2, mock)
})

return matchRes.replace(/(^|,[\n\s]*)([^,]+)/g, (_, separator, selector) => {
const scopeSelector = (separator: string, selector: string): string => {
selector = trim(selector)
selector = selector.replace(/\[[^\]=]+(?:=([^\]]+))?\]/g, (match:string, p1: string) => {
if (attributeValues[p1]) {
Expand All @@ -141,7 +141,42 @@ class CSSParser {
}

return separator + selector
})
}

const selectors: Array<string> = []
let selectorStart = 0
let selectorSeparator = ''
let bracketDepth = 0
let parenDepth = 0

for (let i = 0; i < matchRes.length; i++) {
const char = matchRes[i]
if (char === '\\') {
i++
continue
}

if (char === '[') {
bracketDepth++
} else if (char === ']') {
bracketDepth = Math.max(0, bracketDepth - 1)
} else if (char === '(') {
parenDepth++
} else if (char === ')') {
parenDepth = Math.max(0, parenDepth - 1)
} else if (char === ',' && bracketDepth === 0 && parenDepth === 0) {
selectors.push(scopeSelector(selectorSeparator, matchRes.slice(selectorStart, i)))

const separatorMatch = /^,[\n\s]*/.exec(matchRes.slice(i))
selectorSeparator = separatorMatch ? separatorMatch[0] : ','
i += selectorSeparator.length - 1
selectorStart = i + 1
}
}

selectors.push(scopeSelector(selectorSeparator, matchRes.slice(selectorStart)))

return selectors.join('')
}

// https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration
Expand Down Expand Up @@ -220,6 +255,7 @@ class CSSParser {
this.pageRule() ||
this.hostRule() ||
this.fontFaceRule() ||
this.propertyRule() ||
this.layerRule()
}

Expand Down Expand Up @@ -299,6 +335,13 @@ class CSSParser {
return this.commonHandlerForAtRuleWithSelfRule('font-face')
}

// https://developer.mozilla.org/en-US/docs/Web/CSS/@property
private propertyRule (): boolean | void {
if (!this.commonMatch(/^@property\s+([^{]+)/)) return false

return this.commonHandlerForAtRuleWithSelfRule('property')
}

// https://developer.mozilla.org/en-US/docs/Web/CSS/@layer
private layerRule (): boolean | void {
if (!this.commonMatch(/^@layer\s*([^{;]+)/)) return false
Expand Down