Skip to content

Commit

Permalink
fix(css): filter out function name suffixes with url (#3752)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sociosarbis authored Jun 14, 2021
1 parent 076c5a4 commit 9aa255a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
43 changes: 43 additions & 0 deletions packages/vite/src/node/__tests__/plugins/css.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { cssUrlRE } from '../../plugins/css'

describe('search css url function', () => {
test('some spaces before it', () => {
expect(
cssUrlRE.test("list-style-image: url('../images/bullet.jpg');")
).toBe(true)
})

test('no space after colon', () => {
expect(cssUrlRE.test("list-style-image:url('../images/bullet.jpg');")).toBe(
true
)
})

test('at the beginning of line', () => {
expect(cssUrlRE.test("url('../images/bullet.jpg');")).toBe(true)
})

test('as suffix of a function name', () => {
expect(
cssUrlRE.test(`@function svg-url($string) {
@return "";
}`)
).toBe(false)
})

test('after parenthesis', () => {
expect(
cssUrlRE.test(
'mask-image: image(url(mask.png), skyblue, linear-gradient(rgba(0, 0, 0, 1.0), transparent));'
)
).toBe(true)
})

test('after comma', () => {
expect(
cssUrlRE.test(
'mask-image: image(skyblue,url(mask.png), linear-gradient(rgba(0, 0, 0, 1.0), transparent));'
)
).toBe(true)
})
})
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,9 @@ type CssUrlReplacer = (
url: string,
importer?: string
) => string | Promise<string>
const cssUrlRE = /url\(\s*('[^']+'|"[^"]+"|[^'")]+)\s*\)/
// https://drafts.csswg.org/css-syntax-3/#identifier-code-point
export const cssUrlRE =
/(?:^|[^\w\-\u0080-\uffff])url\(\s*('[^']+'|"[^"]+"|[^'")]+)\s*\)/
const cssImageSetRE = /image-set\(([^)]+)\)/

const UrlRewritePostcssPlugin: Postcss.PluginCreator<{
Expand Down

0 comments on commit 9aa255a

Please sign in to comment.