|
| 1 | +import { expect, test } from 'vitest' |
| 2 | +import { rewriteUrls } from './urls' |
| 3 | + |
| 4 | +const css = String.raw |
| 5 | + |
| 6 | +test('URLs can be rewritten', async () => { |
| 7 | + let root = '/root' |
| 8 | + |
| 9 | + let result = await rewriteUrls({ |
| 10 | + root, |
| 11 | + base: '/root/foo/bar', |
| 12 | + // prettier-ignore |
| 13 | + css: css` |
| 14 | + .foo { |
| 15 | + /* Relative URLs: replaced */ |
| 16 | + background: url(./image.jpg); |
| 17 | + background: url(../image.jpg); |
| 18 | + background: url('./image.jpg'); |
| 19 | + background: url("./image.jpg"); |
| 20 | +
|
| 21 | + /* External URL: ignored */ |
| 22 | + background: url(http://example.com/image.jpg); |
| 23 | + background: url('http://example.com/image.jpg'); |
| 24 | + background: url("http://example.com/image.jpg"); |
| 25 | +
|
| 26 | + /* Data URI: ignored */ |
| 27 | + /* background: url(data:image/png;base64,abc==); */ |
| 28 | + background: url('data:image/png;base64,abc=='); |
| 29 | + background: url("data:image/png;base64,abc=="); |
| 30 | +
|
| 31 | + /* Function calls: ignored */ |
| 32 | + background: url(var(--foo)); |
| 33 | + background: url(var(--foo, './image.jpg')); |
| 34 | + background: url(var(--foo, "./image.jpg")); |
| 35 | +
|
| 36 | + /* Fragments: ignored */ |
| 37 | + background: url(#dont-touch-this); |
| 38 | +
|
| 39 | + /* Image Sets - Raw URL: replaced */ |
| 40 | + background: image-set( |
| 41 | + image1.jpg 1x, |
| 42 | + image2.jpg 2x |
| 43 | + ); |
| 44 | + background: image-set( |
| 45 | + 'image1.jpg' 1x, |
| 46 | + 'image2.jpg' 2x |
| 47 | + ); |
| 48 | + background: image-set( |
| 49 | + "image1.jpg" 1x, |
| 50 | + "image2.jpg" 2x |
| 51 | + ); |
| 52 | +
|
| 53 | + /* Image Sets - Relative URLs: replaced */ |
| 54 | + background: image-set( |
| 55 | + url('image1.jpg') 1x, |
| 56 | + url('image2.jpg') 2x |
| 57 | + ); |
| 58 | + background: image-set( |
| 59 | + url("image1.jpg") 1x, |
| 60 | + url("image2.jpg") 2x |
| 61 | + ); |
| 62 | + background: image-set( |
| 63 | + url('image1.avif') type('image/avif'), |
| 64 | + url('image2.jpg') type('image/jpeg') |
| 65 | + ); |
| 66 | + background: image-set( |
| 67 | + url("image1.avif") type('image/avif'), |
| 68 | + url("image2.jpg") type('image/jpeg') |
| 69 | + ); |
| 70 | +
|
| 71 | + /* Image Sets - Function calls: ignored */ |
| 72 | + background: image-set( |
| 73 | + linear-gradient(blue, white) 1x, |
| 74 | + linear-gradient(blue, green) 2x |
| 75 | + ); |
| 76 | +
|
| 77 | + /* Image Sets - Mixed: replaced */ |
| 78 | + background: image-set( |
| 79 | + linear-gradient(blue, white) 1x, |
| 80 | + url("image2.jpg") 2x |
| 81 | + ); |
| 82 | + } |
| 83 | +
|
| 84 | + /* Fonts - Multiple URLs: replaced */ |
| 85 | + @font-face { |
| 86 | + font-family: "Newman"; |
| 87 | + src: |
| 88 | + local("Newman"), |
| 89 | + url("newman-COLRv1.otf") format("opentype") tech(color-COLRv1), |
| 90 | + url("newman-outline.otf") format("opentype"), |
| 91 | + url("newman-outline.woff") format("woff"); |
| 92 | + } |
| 93 | + `, |
| 94 | + }) |
| 95 | + |
| 96 | + expect(result).toMatchInlineSnapshot(` |
| 97 | + ".foo { |
| 98 | + background: url(./foo/bar/image.jpg); |
| 99 | + background: url(./foo/image.jpg); |
| 100 | + background: url('./foo/bar/image.jpg'); |
| 101 | + background: url("./foo/bar/image.jpg"); |
| 102 | + background: url(http://example.com/image.jpg); |
| 103 | + background: url('http://example.com/image.jpg'); |
| 104 | + background: url("http://example.com/image.jpg"); |
| 105 | + background: url('data:image/png;base64,abc=='); |
| 106 | + background: url("data:image/png;base64,abc=="); |
| 107 | + background: url(var(--foo)); |
| 108 | + background: url(var(--foo, './image.jpg')); |
| 109 | + background: url(var(--foo, "./image.jpg")); |
| 110 | + background: url(#dont-touch-this); |
| 111 | + background: image-set(url(./foo/bar/image1.jpg) 1x, url(./foo/bar/image2.jpg) 2x); |
| 112 | + background: image-set(url('./foo/bar/image1.jpg') 1x, url('./foo/bar/image2.jpg') 2x); |
| 113 | + background: image-set(url("./foo/bar/image1.jpg") 1x, url("./foo/bar/image2.jpg") 2x); |
| 114 | + background: image-set(url('./foo/bar/image1.jpg') 1x, url('./foo/bar/image2.jpg') 2x); |
| 115 | + background: image-set(url("./foo/bar/image1.jpg") 1x, url("./foo/bar/image2.jpg") 2x); |
| 116 | + background: image-set(url('./foo/bar/image1.avif') type('image/avif'), url('./foo/bar/image2.jpg') type('image/jpeg')); |
| 117 | + background: image-set(url("./foo/bar/image1.avif") type('image/avif'), url("./foo/bar/image2.jpg") type('image/jpeg')); |
| 118 | + background: image-set(linear-gradient(blue, white) 1x, linear-gradient(blue, green) 2x); |
| 119 | + background: image-set(linear-gradient(blue, white) 1x, url("./foo/bar/image2.jpg") 2x); |
| 120 | + } |
| 121 | + @font-face { |
| 122 | + font-family: "Newman"; |
| 123 | + src: local("Newman"), url("./foo/bar/newman-COLRv1.otf") format("opentype") tech(color-COLRv1), url("./foo/bar/newman-outline.otf") format("opentype"), url("./foo/bar/newman-outline.woff") format("woff"); |
| 124 | + } |
| 125 | + " |
| 126 | + `) |
| 127 | +}) |
0 commit comments