1 |
|
2 | "use strict";
|
3 | var assert = require("assert");
|
4 | var url = require('url');
|
5 |
|
6 | describe("fix urls tests", function() {
|
7 | var fixUrls = require("../fixUrls");
|
8 | var defaultUrl = "https://x.y.z/a/b.html";
|
9 |
|
10 | beforeEach(function() {
|
11 | global.window = {
|
12 | location: url.parse(defaultUrl)
|
13 | };
|
14 | });
|
15 |
|
16 | var assertUrl = function (origCss, expectedCss, specialUrl) {
|
17 | if (specialUrl) {
|
18 | global.window = {
|
19 | location: url.parse(specialUrl)
|
20 | };
|
21 | }
|
22 | var resultCss = fixUrls(origCss, specialUrl || defaultUrl);
|
23 | expectedCss = expectedCss || origCss;
|
24 |
|
25 | assert.equal(expectedCss, resultCss);
|
26 | };
|
27 |
|
28 |
|
29 | it("Null css is not modified", function() {
|
30 | assertUrl(null)
|
31 | });
|
32 |
|
33 | it("Blank css is not modified", function() { assertUrl("") });
|
34 |
|
35 | it("No url is not modified", function () { assertUrl("body { }") });
|
36 |
|
37 | it("Full url isn't changed (no quotes)", function() {
|
38 | assertUrl("body { background-image:url(http://example.com/bg.jpg); }")
|
39 | });
|
40 |
|
41 | it("Full url isn't changed (no quotes, spaces)", function() {
|
42 | assertUrl("body { background-image:url ( http://example.com/bg.jpg ); }");
|
43 | });
|
44 |
|
45 | it("Full url isn't changed (double quotes)", function() {
|
46 | assertUrl("body { background-image:url(\"http://example.com/bg.jpg\"); }")
|
47 | });
|
48 |
|
49 | it("Full url isn't changed (double quotes, spaces)", function() {
|
50 | assertUrl("body { background-image:url ( \"http://example.com/bg.jpg\" ); }")
|
51 | });
|
52 |
|
53 | it("Full url isn't changed (single quotes)", function() {
|
54 | assertUrl("body { background-image:url('http://example.com/bg.jpg'); }")
|
55 | });
|
56 |
|
57 | it("Full url isn't changed (single quotes, spaces)", function() {
|
58 | assertUrl("body { background-image:url ( 'http://example.com/bg.jpg' ); }")
|
59 | });
|
60 |
|
61 | it("Multiple full urls are not changed", function() {
|
62 | assertUrl(
|
63 | "body { background-image:url(http://example.com/bg.jpg); }\ndiv.main { background-image:url ( 'https://www.anothersite.com/another.png' ); }"
|
64 | );
|
65 | });
|
66 |
|
67 | it("Http url isn't changed", function() {
|
68 | assertUrl("body { background-image:url(http://example.com/bg.jpg); }");
|
69 | });
|
70 |
|
71 | it("Https url isn't changed", function() {
|
72 | assertUrl("body { background-image:url(https://example.com/bg.jpg); }");
|
73 | });
|
74 |
|
75 | it("HTTPS url isn't changed", function() {
|
76 | assertUrl("body { background-image:url(HTTPS://example.com/bg.jpg); }")
|
77 | });
|
78 |
|
79 | it("File url isn't changed", function() {
|
80 | assertUrl("body { background-image:url(file:///example.com/bg.jpg); }")
|
81 | });
|
82 |
|
83 | it("Double slash url isn't changed", function() {
|
84 | assertUrl(
|
85 | "body { background-image:url(//example.com/bg.jpg); }",
|
86 | "body { background-image:url(\"//example.com/bg.jpg\"); }"
|
87 | )
|
88 | });
|
89 |
|
90 | it("Image data uri url isn't changed", function() {
|
91 | assertUrl("body { background-image:url(data:image/png;base64,qsrwABYuwNkimqm3gAAAABJRU5ErkJggg==); }")
|
92 | });
|
93 |
|
94 | it("Font data uri url isn't changed", function() {
|
95 | assertUrl(
|
96 | "body { background-image:url(data:application/x-font-woff;charset=utf-8;base64,qsrwABYuwNkimqm3gAAAABJRU5ErkJggg); }"
|
97 | );
|
98 | });
|
99 |
|
100 |
|
101 | it("Relative url", function() {
|
102 | assertUrl(
|
103 | "body { background-image:url (bg.jpg); }",
|
104 | "body { background-image:url(\"https://x.y.z/a/bg.jpg\"); }"
|
105 | );
|
106 | });
|
107 |
|
108 | it("Relative url case sensitivity", function() {
|
109 | assertUrl(
|
110 | "body { background-image:URL (bg.jpg); }",
|
111 | "body { background-image:url(\"https://x.y.z/a/bg.jpg\"); }"
|
112 | );
|
113 | });
|
114 |
|
115 | it("Relative url with path", function() {
|
116 | assertUrl(
|
117 | "body { background-image:url(c/d/bg.jpg); }",
|
118 | "body { background-image:url(\"https://x.y.z/a/c/d/bg.jpg\"); }"
|
119 | );
|
120 | });
|
121 | it("Relative url with dot slash", function() {
|
122 | assertUrl(
|
123 | "body { background-image:url(./c/d/bg.jpg); }",
|
124 | "body { background-image:url(\"https://x.y.z/a/c/d/bg.jpg\"); }"
|
125 | );
|
126 | });
|
127 |
|
128 | it("Multiple relative urls", function() {
|
129 | assertUrl(
|
130 | "body { background-image:url(bg.jpg); }\ndiv.main { background-image:url(./c/d/bg.jpg); }",
|
131 | "body { background-image:url(\"https://x.y.z/a/bg.jpg\"); }\ndiv.main { background-image:url(\"https://x.y.z/a/c/d/bg.jpg\"); }"
|
132 | );
|
133 | });
|
134 | it("Relative url that looks like data-uri", function() {
|
135 | assertUrl(
|
136 | "body { background-image:url(data/image/png.base64); }",
|
137 | "body { background-image:url(\"https://x.y.z/a/data/image/png.base64\"); }"
|
138 | );
|
139 | });
|
140 |
|
141 |
|
142 | it("Relative url with hash are not changed", function() {
|
143 | assertUrl("body { background-image:url(#bg.jpg); }");
|
144 | });
|
145 |
|
146 |
|
147 | it("Rooted url", function() {
|
148 | assertUrl(
|
149 | "body { background-image:url(/bg.jpg); }",
|
150 | "body { background-image:url(\"https://x.y.z/bg.jpg\"); }"
|
151 | );
|
152 | });
|
153 | it("Rooted url with path", function() {
|
154 | assertUrl(
|
155 | "body { background-image:url(/a/b/bg.jpg); }",
|
156 | "body { background-image:url(\"https://x.y.z/a/b/bg.jpg\"); }"
|
157 | );
|
158 | });
|
159 |
|
160 |
|
161 | it("Location with no path, filename only", function() {
|
162 | assertUrl(
|
163 | "body { background-image:url(bg.jpg); }",
|
164 | "body { background-image:url(\"http://x.y.z/bg.jpg\"); }",
|
165 | "http://x.y.z"
|
166 | );
|
167 | });
|
168 |
|
169 | it("Location with no path, path with filename", function() {
|
170 | assertUrl(
|
171 | "body { background-image:url(a/bg.jpg); }",
|
172 | "body { background-image:url(\"http://x.y.z/a/bg.jpg\"); }",
|
173 | "http://x.y.z"
|
174 | );
|
175 | });
|
176 | it("Location with no path, rel path with filename", function() {
|
177 | assertUrl(
|
178 | "body { background-image:url(./a/bg.jpg); }",
|
179 | "body { background-image:url(\"http://x.y.z/a/bg.jpg\"); }",
|
180 | "http://x.y.z"
|
181 | );
|
182 | });
|
183 | it("Location with no path, root filename", function() {
|
184 | assertUrl(
|
185 | "body { background-image:url(/a/bg.jpg); }",
|
186 | "body { background-image:url(\"http://x.y.z/a/bg.jpg\"); }",
|
187 | "http://x.y.z"
|
188 | );
|
189 | });
|
190 |
|
191 | it("Doesn't break inline SVG", function() {
|
192 | const svg = "url('data:image/svg+xml;charset=utf-8,<svg><feFlood flood-color=\"rgba(0,0,0,0.5)\" /></svg>')";
|
193 |
|
194 | assertUrl(
|
195 | "body: { background: " + svg + " }"
|
196 | );
|
197 | });
|
198 | });
|