Browse Source

Update grunt and travis

Fix ignoreSubdomain functionality
Return the hostname and the tld if ignoreSubdomain is check.
The regex will catch the hostname and tld, all subdomain will be ignored. By @florianaudon
pull/67/head
brantje 2 years ago
parent
commit
8dbc5eac6f
No account linked to committer's email address
4 changed files with 60 additions and 66 deletions
  1. 4
    1
      .travis.yml
  2. 1
    1
      Gruntfile.js
  3. 4
    9
      js/lib/parseUrl.js
  4. 51
    55
      tests/unit/lib/parseUrl.js

+ 4
- 1
.travis.yml View File

@@ -5,4 +5,7 @@ before_install:
5 5
 before_script:
6 6
     - npm install
7 7
 script:
8
-    - grunt hint
8
+    - grunt test
9
+
10
+addons:
11
+    firefox: "latest"

+ 1
- 1
Gruntfile.js View File

@@ -106,7 +106,7 @@ module.exports = function (grunt) {
106 106
     grunt.loadNpmTasks('grunt-karma');
107 107
     // Default task(s).
108 108
 
109
-    grunt.registerTask('hint', ['jshint']);
109
+    grunt.registerTask('test', ['karma', 'jshint']);
110 110
     grunt.registerTask('build', ['execute:fixLocale','jshint', 'clean:dist', 'mkdir:dist', 'copy:dist', 'compress:dist']);
111 111
     grunt.registerTask('dist', ['']);
112 112
 

+ 4
- 9
js/lib/parseUrl.js View File

@@ -49,13 +49,8 @@ function processURL(URL, ignoreProtocol, ignoreSubdomain, ignorePath, ignorePort
49 49
         baseHost = host;
50 50
     }
51 51
     else {
52
-        var result = host.match(/[^./]+\.[^./]+$/);
53
-        var TLDlength = 0;
54
-        if(result){
55
-            TLDlength = result[0].length;
56
-        }
57
-
58
-        baseHost = splittedURL.slice(-TLDlength - 1).join(".");
52
+        var result = host.match(/[^./]+\.[^./]+$/); // catch the two last parts, it's de hostname and the tld
53
+        baseHost=result[0];
59 54
     }
60 55
     var returnURL = "";
61 56
     if (!ignoreProtocol) {
@@ -65,7 +60,7 @@ function processURL(URL, ignoreProtocol, ignoreSubdomain, ignorePath, ignorePort
65 60
         returnURL += host;
66 61
     }
67 62
     else {
68
-        returnURL += host;
63
+        returnURL += baseHost;//return the hostname and the tld of the website if ignoreSubdomain is check
69 64
     }
70 65
     if (ignorePort) {
71 66
         returnURL = returnURL.replace(':' + port, "");
@@ -77,4 +72,4 @@ function processURL(URL, ignoreProtocol, ignoreSubdomain, ignorePath, ignorePort
77 72
         returnURL = returnURL.slice(0, -1);
78 73
     }
79 74
     return returnURL;
80
-}
75
+}

+ 51
- 55
tests/unit/lib/parseUrl.js View File

@@ -60,7 +60,7 @@ var testCombinations = [{
60 60
     "ignoreSubdomain": true,
61 61
     "ignorePath": true,
62 62
     "ignorePort": true,
63
-    "expected": "http//example.com"
63
+    "expected": "http://example.com"
64 64
 }, {
65 65
     "url": "http://example.com",
66 66
     "ignoreProtocol": false,
@@ -74,7 +74,7 @@ var testCombinations = [{
74 74
     "ignoreSubdomain": true,
75 75
     "ignorePath": false,
76 76
     "ignorePort": true,
77
-    "expected": "http//example.com"
77
+    "expected": "http://example.com"
78 78
 }, {
79 79
     "url": "http://example.com",
80 80
     "ignoreProtocol": false,
@@ -88,7 +88,7 @@ var testCombinations = [{
88 88
     "ignoreSubdomain": false,
89 89
     "ignorePath": true,
90 90
     "ignorePort": true,
91
-    "expected": "http//example.com"
91
+    "expected": "http://example.com"
92 92
 }, {
93 93
     "url": "http://example.com",
94 94
     "ignoreProtocol": false,
@@ -102,7 +102,7 @@ var testCombinations = [{
102 102
     "ignoreSubdomain": false,
103 103
     "ignorePath": false,
104 104
     "ignorePort": true,
105
-    "expected": "http//example.com"
105
+    "expected": "http://example.com"
106 106
 }, {
107 107
     "url": "http://example.com",
108 108
     "ignoreProtocol": false,
@@ -123,7 +123,7 @@ var testCombinations = [{
123 123
     "ignoreSubdomain": true,
124 124
     "ignorePath": true,
125 125
     "ignorePort": false,
126
-    "expected": "example.com"
126
+    "expected": "example.com:8080"
127 127
 }, {
128 128
     "url": "http://example.com:8080",
129 129
     "ignoreProtocol": true,
@@ -137,7 +137,7 @@ var testCombinations = [{
137 137
     "ignoreSubdomain": true,
138 138
     "ignorePath": false,
139 139
     "ignorePort": false,
140
-    "expected": "example.com"
140
+    "expected": "example.com:8080"
141 141
 }, {
142 142
     "url": "http://example.com:8080",
143 143
     "ignoreProtocol": true,
@@ -151,7 +151,7 @@ var testCombinations = [{
151 151
     "ignoreSubdomain": false,
152 152
     "ignorePath": true,
153 153
     "ignorePort": false,
154
-    "expected": "example.com"
154
+    "expected": "example.com:8080"
155 155
 }, {
156 156
     "url": "http://example.com:8080",
157 157
     "ignoreProtocol": true,
@@ -165,7 +165,7 @@ var testCombinations = [{
165 165
     "ignoreSubdomain": false,
166 166
     "ignorePath": false,
167 167
     "ignorePort": false,
168
-    "expected": "example.com"
168
+    "expected": "example.com:8080"
169 169
 }, {
170 170
     "url": "http://example.com:8080",
171 171
     "ignoreProtocol": false,
@@ -179,7 +179,7 @@ var testCombinations = [{
179 179
     "ignoreSubdomain": true,
180 180
     "ignorePath": true,
181 181
     "ignorePort": false,
182
-    "expected": "http://example.com"
182
+    "expected": "http://example.com:8080"
183 183
 }, {
184 184
     "url": "http://example.com:8080",
185 185
     "ignoreProtocol": false,
@@ -193,7 +193,7 @@ var testCombinations = [{
193 193
     "ignoreSubdomain": true,
194 194
     "ignorePath": false,
195 195
     "ignorePort": false,
196
-    "expected": "http://example.com"
196
+    "expected": "http://example.com:8080"
197 197
 }, {
198 198
     "url": "http://example.com:8080",
199 199
     "ignoreProtocol": false,
@@ -207,7 +207,7 @@ var testCombinations = [{
207 207
     "ignoreSubdomain": false,
208 208
     "ignorePath": true,
209 209
     "ignorePort": false,
210
-    "expected": "http://example.com"
210
+    "expected": "http://example.com:8080"
211 211
 }, {
212 212
     "url": "http://example.com:8080",
213 213
     "ignoreProtocol": false,
@@ -221,7 +221,7 @@ var testCombinations = [{
221 221
     "ignoreSubdomain": false,
222 222
     "ignorePath": false,
223 223
     "ignorePort": false,
224
-    "expected": "http://example.com"
224
+    "expected": "http://example.com:8080"
225 225
 }, {
226 226
     "url": "https://example.com:8080",
227 227
     "ignoreProtocol": true,
@@ -235,7 +235,7 @@ var testCombinations = [{
235 235
     "ignoreSubdomain": true,
236 236
     "ignorePath": true,
237 237
     "ignorePort": false,
238
-    "expected": "example.com"
238
+    "expected": "example.com:8080"
239 239
 }, {
240 240
     "url": "https://example.com:8080",
241 241
     "ignoreProtocol": true,
@@ -249,7 +249,7 @@ var testCombinations = [{
249 249
     "ignoreSubdomain": true,
250 250
     "ignorePath": false,
251 251
     "ignorePort": false,
252
-    "expected": "example.com"
252
+    "expected": "example.com:8080"
253 253
 }, {
254 254
     "url": "https://example.com:8080",
255 255
     "ignoreProtocol": true,
@@ -263,7 +263,7 @@ var testCombinations = [{
263 263
     "ignoreSubdomain": false,
264 264
     "ignorePath": true,
265 265
     "ignorePort": false,
266
-    "expected": "example.com"
266
+    "expected": "example.com:8080"
267 267
 }, {
268 268
     "url": "https://example.com:8080",
269 269
     "ignoreProtocol": true,
@@ -277,7 +277,7 @@ var testCombinations = [{
277 277
     "ignoreSubdomain": false,
278 278
     "ignorePath": false,
279 279
     "ignorePort": false,
280
-    "expected": "example.com"
280
+    "expected": "example.com:8080"
281 281
 }, {
282 282
     "url": "https://example.com:8080",
283 283
     "ignoreProtocol": false,
@@ -291,7 +291,7 @@ var testCombinations = [{
291 291
     "ignoreSubdomain": true,
292 292
     "ignorePath": true,
293 293
     "ignorePort": false,
294
-    "expected": "https://example.com"
294
+    "expected": "https://example.com:8080"
295 295
 }, {
296 296
     "url": "https://example.com:8080",
297 297
     "ignoreProtocol": false,
@@ -305,7 +305,7 @@ var testCombinations = [{
305 305
     "ignoreSubdomain": true,
306 306
     "ignorePath": false,
307 307
     "ignorePort": false,
308
-    "expected": "https://example.com"
308
+    "expected": "https://example.com:8080"
309 309
 }, {
310 310
     "url": "https://example.com:8080",
311 311
     "ignoreProtocol": false,
@@ -319,7 +319,7 @@ var testCombinations = [{
319 319
     "ignoreSubdomain": false,
320 320
     "ignorePath": true,
321 321
     "ignorePort": false,
322
-    "expected": "https://example.com"
322
+    "expected": "https://example.com:8080"
323 323
 }, {
324 324
     "url": "https://example.com:8080",
325 325
     "ignoreProtocol": false,
@@ -333,7 +333,7 @@ var testCombinations = [{
333 333
     "ignoreSubdomain": false,
334 334
     "ignorePath": false,
335 335
     "ignorePort": false,
336
-    "expected": "https://example.com"
336
+    "expected": "https://example.com:8080"
337 337
 }, {
338 338
     "url": "https://example.com",
339 339
     "ignoreProtocol": true,
@@ -396,7 +396,7 @@ var testCombinations = [{
396 396
     "ignoreSubdomain": true,
397 397
     "ignorePath": true,
398 398
     "ignorePort": true,
399
-    "expected": "https//example.com"
399
+    "expected": "https://example.com"
400 400
 }, {
401 401
     "url": "https://example.com",
402 402
     "ignoreProtocol": false,
@@ -410,7 +410,7 @@ var testCombinations = [{
410 410
     "ignoreSubdomain": true,
411 411
     "ignorePath": false,
412 412
     "ignorePort": true,
413
-    "expected": "https//example.com"
413
+    "expected": "https://example.com"
414 414
 }, {
415 415
     "url": "https://example.com",
416 416
     "ignoreProtocol": false,
@@ -424,7 +424,7 @@ var testCombinations = [{
424 424
     "ignoreSubdomain": false,
425 425
     "ignorePath": true,
426 426
     "ignorePort": true,
427
-    "expected": "https//example.com"
427
+    "expected": "https://example.com"
428 428
 }, {
429 429
     "url": "https://example.com",
430 430
     "ignoreProtocol": false,
@@ -438,7 +438,7 @@ var testCombinations = [{
438 438
     "ignoreSubdomain": false,
439 439
     "ignorePath": false,
440 440
     "ignorePort": true,
441
-    "expected": "https//example.com"
441
+    "expected": "https://example.com"
442 442
 }, {
443 443
     "url": "https://example.com",
444 444
     "ignoreProtocol": false,
@@ -508,7 +508,7 @@ var testCombinations = [{
508 508
     "ignoreSubdomain": true,
509 509
     "ignorePath": true,
510 510
     "ignorePort": true,
511
-    "expected": "http//example.com"
511
+    "expected": "http://example.com"
512 512
 }, {
513 513
     "url": "http://subdomain.example.com",
514 514
     "ignoreProtocol": false,
@@ -522,7 +522,7 @@ var testCombinations = [{
522 522
     "ignoreSubdomain": true,
523 523
     "ignorePath": false,
524 524
     "ignorePort": true,
525
-    "expected": "http//example.com"
525
+    "expected": "http://example.com"
526 526
 }, {
527 527
     "url": "http://subdomain.example.com",
528 528
     "ignoreProtocol": false,
@@ -536,7 +536,7 @@ var testCombinations = [{
536 536
     "ignoreSubdomain": false,
537 537
     "ignorePath": true,
538 538
     "ignorePort": true,
539
-    "expected": "http//subdomain.example.com"
539
+    "expected": "http://subdomain.example.com"
540 540
 }, {
541 541
     "url": "http://subdomain.example.com",
542 542
     "ignoreProtocol": false,
@@ -550,7 +550,7 @@ var testCombinations = [{
550 550
     "ignoreSubdomain": false,
551 551
     "ignorePath": false,
552 552
     "ignorePort": true,
553
-    "expected": "http//subdomain.example.com"
553
+    "expected": "http://subdomain.example.com"
554 554
 }, {
555 555
     "url": "http://subdomain.example.com",
556 556
     "ignoreProtocol": false,
@@ -662,7 +662,7 @@ var testCombinations = [{
662 662
     "ignoreSubdomain": false,
663 663
     "ignorePath": false,
664 664
     "ignorePort": true,
665
-    "expected": "http://subdomain.example.com:8080"
665
+    "expected": "http://subdomain.example.com"
666 666
 }, {
667 667
     "url": "http://subdomain.example.com:8080",
668 668
     "ignoreProtocol": false,
@@ -704,7 +704,7 @@ var testCombinations = [{
704 704
     "ignoreSubdomain": false,
705 705
     "ignorePath": true,
706 706
     "ignorePort": true,
707
-    "expected": "subdomain.example.com:8080"
707
+    "expected": "subdomain.example.com"
708 708
 }, {
709 709
     "url": "https://subdomain.example.com:8080",
710 710
     "ignoreProtocol": true,
@@ -767,7 +767,7 @@ var testCombinations = [{
767 767
     "ignoreSubdomain": false,
768 768
     "ignorePath": true,
769 769
     "ignorePort": false,
770
-    "expected": "https://subdomain.example.com"
770
+    "expected": "https://subdomain.example.com:8080"
771 771
 }, {
772 772
     "url": "https://subdomain.example.com:8080",
773 773
     "ignoreProtocol": false,
@@ -844,7 +844,7 @@ var testCombinations = [{
844 844
     "ignoreSubdomain": true,
845 845
     "ignorePath": true,
846 846
     "ignorePort": true,
847
-    "expected": "https//example.com"
847
+    "expected": "https://example.com"
848 848
 }, {
849 849
     "url": "https://subdomain.example.com",
850 850
     "ignoreProtocol": false,
@@ -858,7 +858,7 @@ var testCombinations = [{
858 858
     "ignoreSubdomain": true,
859 859
     "ignorePath": false,
860 860
     "ignorePort": true,
861
-    "expected": "https//example.com"
861
+    "expected": "https://example.com"
862 862
 }, {
863 863
     "url": "https://subdomain.example.com",
864 864
     "ignoreProtocol": false,
@@ -872,7 +872,7 @@ var testCombinations = [{
872 872
     "ignoreSubdomain": false,
873 873
     "ignorePath": true,
874 874
     "ignorePort": true,
875
-    "expected": "https//subdomain.example.com"
875
+    "expected": "https://subdomain.example.com"
876 876
 }, {
877 877
     "url": "https://subdomain.example.com",
878 878
     "ignoreProtocol": false,
@@ -886,7 +886,7 @@ var testCombinations = [{
886 886
     "ignoreSubdomain": false,
887 887
     "ignorePath": false,
888 888
     "ignorePort": true,
889
-    "expected": "https//subdomain.example.com"
889
+    "expected": "https://subdomain.example.com"
890 890
 }, {
891 891
     "url": "https://subdomain.example.com",
892 892
     "ignoreProtocol": false,
@@ -956,7 +956,7 @@ var testCombinations = [{
956 956
     "ignoreSubdomain": true,
957 957
     "ignorePath": true,
958 958
     "ignorePort": true,
959
-    "expected": "http//example.com"
959
+    "expected": "http://example.com"
960 960
 }, {
961 961
     "url": "http://subdomain.example.com/page/login",
962 962
     "ignoreProtocol": false,
@@ -970,7 +970,7 @@ var testCombinations = [{
970 970
     "ignoreSubdomain": true,
971 971
     "ignorePath": false,
972 972
     "ignorePort": true,
973
-    "expected": "http//example.com/page/login"
973
+    "expected": "http://example.com/page/login"
974 974
 }, {
975 975
     "url": "http://subdomain.example.com/page/login",
976 976
     "ignoreProtocol": false,
@@ -984,7 +984,7 @@ var testCombinations = [{
984 984
     "ignoreSubdomain": false,
985 985
     "ignorePath": true,
986 986
     "ignorePort": true,
987
-    "expected": "http//subdomain.example.com"
987
+    "expected": "http://subdomain.example.com"
988 988
 }, {
989 989
     "url": "http://subdomain.example.com/page/login",
990 990
     "ignoreProtocol": false,
@@ -998,7 +998,7 @@ var testCombinations = [{
998 998
     "ignoreSubdomain": false,
999 999
     "ignorePath": false,
1000 1000
     "ignorePort": true,
1001
-    "expected": "http//subdomain.example.com/page/login"
1001
+    "expected": "http://subdomain.example.com/page/login"
1002 1002
 }, {
1003 1003
     "url": "http://subdomain.example.com/page/login",
1004 1004
     "ignoreProtocol": false,
@@ -1180,14 +1180,14 @@ var testCombinations = [{
1180 1180
     "ignoreSubdomain": true,
1181 1181
     "ignorePath": true,
1182 1182
     "ignorePort": true,
1183
-    "expected": "https://example.com:8080"
1183
+    "expected": "https://example.com"
1184 1184
 }, {
1185 1185
     "url": "https://subdomain.example.com:8080/page/login",
1186 1186
     "ignoreProtocol": false,
1187 1187
     "ignoreSubdomain": true,
1188 1188
     "ignorePath": true,
1189 1189
     "ignorePort": false,
1190
-    "expected": "https://example.com"
1190
+    "expected": "https://example.com:8080"
1191 1191
 }, {
1192 1192
     "url": "https://subdomain.example.com:8080/page/login",
1193 1193
     "ignoreProtocol": false,
@@ -1201,14 +1201,14 @@ var testCombinations = [{
1201 1201
     "ignoreSubdomain": true,
1202 1202
     "ignorePath": false,
1203 1203
     "ignorePort": false,
1204
-    "expected": "https://example.com/page/login"
1204
+    "expected": "https://example.com:8080/page/login"
1205 1205
 }, {
1206 1206
     "url": "https://subdomain.example.com:8080/page/login",
1207 1207
     "ignoreProtocol": false,
1208 1208
     "ignoreSubdomain": false,
1209 1209
     "ignorePath": true,
1210 1210
     "ignorePort": true,
1211
-    "expected": "https://subdomain.example.com:8080"
1211
+    "expected": "https://subdomain.example.com"
1212 1212
 }, {
1213 1213
     "url": "https://subdomain.example.com:8080/page/login",
1214 1214
     "ignoreProtocol": false,
@@ -1264,7 +1264,7 @@ var testCombinations = [{
1264 1264
     "ignoreSubdomain": false,
1265 1265
     "ignorePath": true,
1266 1266
     "ignorePort": true,
1267
-    "expected": "example.com"
1267
+    "expected": "subdomain.example.com"
1268 1268
 }, {
1269 1269
     "url": "https://subdomain.example.com/page/login",
1270 1270
     "ignoreProtocol": true,
@@ -1292,7 +1292,7 @@ var testCombinations = [{
1292 1292
     "ignoreSubdomain": true,
1293 1293
     "ignorePath": true,
1294 1294
     "ignorePort": true,
1295
-    "expected": "https//example.com"
1295
+    "expected": "https://example.com"
1296 1296
 }, {
1297 1297
     "url": "https://subdomain.example.com/page/login",
1298 1298
     "ignoreProtocol": false,
@@ -1306,7 +1306,7 @@ var testCombinations = [{
1306 1306
     "ignoreSubdomain": true,
1307 1307
     "ignorePath": false,
1308 1308
     "ignorePort": true,
1309
-    "expected": "https//example.com/page/login"
1309
+    "expected": "https://example.com/page/login"
1310 1310
 }, {
1311 1311
     "url": "https://subdomain.example.com/page/login",
1312 1312
     "ignoreProtocol": false,
@@ -1320,7 +1320,7 @@ var testCombinations = [{
1320 1320
     "ignoreSubdomain": false,
1321 1321
     "ignorePath": true,
1322 1322
     "ignorePort": true,
1323
-    "expected": "https//subdomain.example.com"
1323
+    "expected": "https://subdomain.example.com"
1324 1324
 }, {
1325 1325
     "url": "https://subdomain.example.com/page/login",
1326 1326
     "ignoreProtocol": false,
@@ -1334,7 +1334,7 @@ var testCombinations = [{
1334 1334
     "ignoreSubdomain": false,
1335 1335
     "ignorePath": false,
1336 1336
     "ignorePort": true,
1337
-    "expected": "https//subdomain.example.com/page/login"
1337
+    "expected": "https://subdomain.example.com/page/login"
1338 1338
 }, {
1339 1339
     "url": "https://subdomain.example.com/page/login",
1340 1340
     "ignoreProtocol": false,
@@ -1348,18 +1348,14 @@ describe('Testing url parser', function () {
1348 1348
     for (var i = 0; i < testCombinations.length; i++) {
1349 1349
         var url = testCombinations[i];
1350 1350
         (function (url) {
1351
-            var props = 'url: ' + url.url + ' ignoreProtocol: ' + url.ignoreProtocol + ' ignoreSubdomain:' + url.ignoreSubdomain + ' ignorePath: ' + url.ignorePath + ' ignorePort:' + url.ignorePort
1351
+
1352
+            var props = 'ignoreProtocol: ' + url.ignoreProtocol + ' ignoreSubdomain:' + url.ignoreSubdomain + ' ignorePath: ' + url.ignorePath + ' ignorePort:' + url.ignorePort
1352 1353
 
1353 1354
             it(props, function () {
1354 1355
                 var result = processURL(url.url, url.ignoreProtocol, url.ignoreSubdomain, url.ignorePath, url.ignorePort);
1355 1356
                 var parser = document.createElement('a');
1356 1357
                 parser.href = url.url;
1357
-                try {
1358
-                    expect(result).toEqual(url.expected)
1359
-                } catch (e) {
1360
-                    console.log(url);
1361
-                    throw e
1362
-                }
1358
+                expect(url.url + '->' + result).toEqual(url.url + '->' + url.expected)
1363 1359
             });
1364 1360
         }(url));
1365 1361
 

Loading…
Cancel
Save