UNPKG

6.12 kBJavaScriptView Raw
1// Node v4 requires "use strict" to allow block scoped let & const
2"use strict";
3var assert = require("assert");
4var url = require('url');
5
6describe("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(resultCss, expectedCss);
26 };
27
28 // no change
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 // relative urls
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 with path", function() {
109 assertUrl(
110 "body { background-image:url(c/d/bg.jpg); }",
111 "body { background-image:url(\"https://x.y.z/a/c/d/bg.jpg\"); }"
112 );
113 });
114 it("Relative url with dot slash", function() {
115 assertUrl(
116 "body { background-image:url(./c/d/bg.jpg); }",
117 "body { background-image:url(\"https://x.y.z/a/c/d/bg.jpg\"); }"
118 );
119 });
120
121 it("Multiple relative urls", function() {
122 assertUrl(
123 "body { background-image:url(bg.jpg); }\ndiv.main { background-image:url(./c/d/bg.jpg); }",
124 "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\"); }"
125 );
126 });
127 it("Relative url that looks like data-uri", function() {
128 assertUrl(
129 "body { background-image:url(data/image/png.base64); }",
130 "body { background-image:url(\"https://x.y.z/a/data/image/png.base64\"); }"
131 );
132 });
133
134 // urls with hashes
135 it("Relative url with hash are not changed", function() {
136 assertUrl("body { background-image:url(#bg.jpg); }");
137 });
138
139 // rooted urls
140 it("Rooted url", function() {
141 assertUrl(
142 "body { background-image:url(/bg.jpg); }",
143 "body { background-image:url(\"https://x.y.z/bg.jpg\"); }"
144 );
145 });
146 it("Rooted url with path", function() {
147 assertUrl(
148 "body { background-image:url(/a/b/bg.jpg); }",
149 "body { background-image:url(\"https://x.y.z/a/b/bg.jpg\"); }"
150 );
151 });
152
153 //special locations
154 it("Location with no path, filename only", function() {
155 assertUrl(
156 "body { background-image:url(bg.jpg); }",
157 "body { background-image:url(\"http://x.y.z/bg.jpg\"); }",
158 "http://x.y.z"
159 );
160 });
161
162 it("Location with no path, path with filename", function() {
163 assertUrl(
164 "body { background-image:url(a/bg.jpg); }",
165 "body { background-image:url(\"http://x.y.z/a/bg.jpg\"); }",
166 "http://x.y.z"
167 );
168 });
169 it("Location with no path, rel 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, root 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});