|
| 1 | +import test from 'tape'; |
| 2 | +import {fixupCSSUrls} from '../src/inject-css'; |
| 3 | + |
| 4 | +// tests partly copied from |
| 5 | +// https://github.com/webpack-contrib/style-loader/blob/master/test/fixUrls.test.js |
| 6 | +test('fixupCSSUrls throws on null/undefined', t => { |
| 7 | + t.throws(() => fixupCSSUrls('foo/bar', null)); |
| 8 | + t.throws(() => fixupCSSUrls('foo/bar', undefined)); |
| 9 | + t.end(); |
| 10 | +}); |
| 11 | + |
| 12 | +test('fixupCSSUrls: Blank css is not modified', t => { |
| 13 | + const css = ''; |
| 14 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 15 | + t.end(); |
| 16 | +}); |
| 17 | + |
| 18 | +test('fixupCSSUrls: No url is not modified', t => { |
| 19 | + const css = 'body { }'; |
| 20 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 21 | + t.end(); |
| 22 | +}); |
| 23 | + |
| 24 | +test("fixupCSSUrls: Full url isn't changed (no quotes)", t => { |
| 25 | + const css = 'body { background-image:url ( http://example.com/bg.jpg ); }'; |
| 26 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 27 | + t.end(); |
| 28 | +}); |
| 29 | + |
| 30 | +test("fixupCSSUrls: Full url isn't changed (no quotes, spaces)", t => { |
| 31 | + const css = 'body { background-image:url ( http://example.com/bg.jpg ); }'; |
| 32 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 33 | + t.end(); |
| 34 | +}); |
| 35 | + |
| 36 | +test("fixupCSSUrls: Full url isn't changed (double quotes)", t => { |
| 37 | + const css = 'body { background-image:url("http://example.com/bg.jpg"); }'; |
| 38 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 39 | + t.end(); |
| 40 | +}); |
| 41 | + |
| 42 | +test("fixupCSSUrls: Full url isn't changed (double quotes, spaces)", t => { |
| 43 | + const css = 'body { background-image:url ( "http://example.com/bg.jpg" ); }'; |
| 44 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 45 | + t.end(); |
| 46 | +}); |
| 47 | + |
| 48 | +test("fixupCSSUrls: Full url isn't changed (single quotes)", t => { |
| 49 | + const css = 'body { background-image:url(\'http://example.com/bg.jpg\'); }'; |
| 50 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 51 | + t.end(); |
| 52 | +}); |
| 53 | + |
| 54 | +test("fixupCSSUrls: Full url isn't changed (single quotes, spaces)", t => { |
| 55 | + const css = 'body { background-image:url ( \'http://example.com/bg.jpg\' ); }'; |
| 56 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 57 | + t.end(); |
| 58 | +}); |
| 59 | + |
| 60 | +test('fixupCSSUrls: Multiple full urls are not changed', t => { |
| 61 | + const css = "body { background-image:url(http://example.com/bg.jpg); }\ndiv.main { background-image:url ( 'https://www.anothersite.com/another.png' ); }"; |
| 62 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 63 | + t.end(); |
| 64 | +}); |
| 65 | + |
| 66 | +test("fixupCSSUrls: Http url isn't changed", t => { |
| 67 | + const css = 'body { background-image:url(http://example.com/bg.jpg); }'; |
| 68 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 69 | + t.end(); |
| 70 | +}); |
| 71 | + |
| 72 | +test("fixupCSSUrls: Https url isn't changed", t => { |
| 73 | + const css = 'body { background-image:url(https://example.com/bg.jpg); }'; |
| 74 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 75 | + t.end(); |
| 76 | +}); |
| 77 | + |
| 78 | +test("fixupCSSUrls: HTTPS url isn't changed", t => { |
| 79 | + const css = 'body { background-image:url(HTTPS://example.com/bg.jpg); }'; |
| 80 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 81 | + t.end(); |
| 82 | +}); |
| 83 | + |
| 84 | +test("fixupCSSUrls: File url isn't changed", t => { |
| 85 | + const css = 'body { background-image:url(file:///example.com/bg.jpg); }'; |
| 86 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 87 | + t.end(); |
| 88 | +}); |
| 89 | + |
| 90 | +test("fixupCSSUrls: Double slash url isn't changed", t => { |
| 91 | + const css = 'body { background-image:url(//example.com/bg.jpg); }'; |
| 92 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 93 | + t.end(); |
| 94 | +}); |
| 95 | + |
| 96 | +test("fixupCSSUrls: Image data uri url isn't changed", t => { |
| 97 | + const css = 'body { background-image:url(data:image/png;base64,qsrwABYuwNkimqm3gAAAABJRU5ErkJggg==); }'; |
| 98 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 99 | + t.end(); |
| 100 | +}); |
| 101 | + |
| 102 | +test("fixupCSSUrls: Font data uri url isn't changed", t => { |
| 103 | + const css = 'body { background-image:url(data:application/x-font-woff;charset=utf-8;base64,qsrwABYuwNkimqm3gAAAABJRU5ErkJggg); }'; |
| 104 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 105 | + t.end(); |
| 106 | +}); |
| 107 | + |
| 108 | +test('fixupCSSUrls: Relative url with dot slash', t => { |
| 109 | + const css = 'body { background-image:url(./c/d/bg.jpg); }'; |
| 110 | + const expected = "body { background-image:url('foo/c/d/bg.jpg'); }"; |
| 111 | + t.equal(fixupCSSUrls('foo/bar', css), expected); |
| 112 | + t.end(); |
| 113 | +}); |
| 114 | + |
| 115 | +test('fixupCSSUrls: Multiple relative urls', t => { |
| 116 | + const css = 'body { background-image:URL ( "./bg.jpg" ); }\ndiv.main { background-image:url(../c/d/bg.jpg); }'; |
| 117 | + const expected = "body { background-image:url('foo/bg.jpg'); }\ndiv.main { background-image:url('c/d/bg.jpg'); }"; |
| 118 | + t.equal(fixupCSSUrls('foo/bar', css), expected); |
| 119 | + t.end(); |
| 120 | +}); |
| 121 | + |
| 122 | +test("fixupCSSUrls: url with hash isn't changed", t => { |
| 123 | + const css = 'body { background-image:url(#bg.jpg); }'; |
| 124 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 125 | + t.end(); |
| 126 | +}); |
| 127 | + |
| 128 | +test('fixupCSSUrls: Empty url should be skipped', t => { |
| 129 | + let css = 'body { background-image:url(); }'; |
| 130 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 131 | + css = 'body { background-image:url( ); }'; |
| 132 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 133 | + css = 'body { background-image:url(\n); }'; |
| 134 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 135 | + css = 'body { background-image:url(\'\'); }'; |
| 136 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 137 | + css = 'body { background-image:url(\' \'); }'; |
| 138 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 139 | + css = 'body { background-image:url(""); }'; |
| 140 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 141 | + css = 'body { background-image:url(" "); }'; |
| 142 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 143 | + t.end(); |
| 144 | +}); |
| 145 | + |
| 146 | +test("fixupCSSUrls: Rooted url isn't changed", t => { |
| 147 | + let css = 'body { background-image:url(/bg.jpg); }'; |
| 148 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 149 | + css = 'body { background-image:url(/a/b/bg.jpg); }'; |
| 150 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 151 | + t.end(); |
| 152 | +}); |
| 153 | + |
| 154 | +test("fixupCSSUrls doesn't break inline SVG", t => { |
| 155 | + const css = "body { background-image:url('data:image/svg+xml;charset=utf-8,<svg><feFlood flood-color=\"rgba(0,0,0,0.5)\" /></svg>'); }"; |
| 156 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 157 | + t.end(); |
| 158 | +}); |
| 159 | + |
| 160 | +test("fixupCSSUrls doesn't break inline SVG with HTML comment", t => { |
| 161 | + const css = "body { background-image:url('data:image/svg+xml,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%3E%0A%3C!--%20Comment%20--%3E%0A%3Csvg%3E%3C%2Fsvg%3E%0A'); }"; |
| 162 | + t.equal(fixupCSSUrls('foo/bar', css), css); |
| 163 | + t.end(); |
| 164 | +}); |
0 commit comments