Skip to content

Commit 6bbb189

Browse files
committed
Merge pull request twbs#182 from twbs/W005
improve W005 checker by properly parsing URL and checking filename
2 parents 58f8812 + 41f1b89 commit 6bbb189

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"browser": {
7272
"binary-search": false,
7373
"cheerio": "jquery",
74+
"url": "./src/url.js",
7475
"./cli": false,
7576
"./location": false
7677
},

src/bootlint.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/*eslint-env node */
99

1010
var cheerio = require('cheerio');
11+
var parseUrl = require('url').parse;
1112
var semver = require('semver');
1213
var _location = require('./location');
1314
var LocationIndex = _location.LocationIndex;
@@ -359,19 +360,27 @@ var LocationIndex = _location.LocationIndex;
359360

360361
// check for jQuery <script>s
361362
var jqueries = $([
362-
'script[src*="jquery.min"]',
363-
'script[src*="jQuery.min"]',
364-
'script[src*="jquery.js"]',
365-
'script[src*="jQuery.js"]'
363+
'script[src*="jquery"]',
364+
'script[src*="jQuery"]'
366365
].join(','));
367366
if (!jqueries.length) {
368367
reporter(NO_JQUERY);
369368
return;
370369
}
371370
jqueries.each(function () {
372371
var script = $(this);
373-
var matches = script.attr('src').match(/\d+\.\d+\.\d+/g);
374-
if (!matches) {
372+
var pathSegments = parseUrl(script.attr('src')).pathname.split('/');
373+
var filename = pathSegments[pathSegments.length - 1];
374+
if (!/^j[qQ]uery(\.min)?\.js$/.test(filename)) {
375+
return;
376+
}
377+
var matches = pathSegments.map(function (segment) {
378+
var match = segment.match(/^\d+\.\d+\.\d+$/);
379+
return match ? match[0] : null;
380+
}).filter(function (match) {
381+
return match !== null;
382+
});
383+
if (!matches.length) {
375384
return;
376385
}
377386
var version = matches[matches.length - 1];

src/url.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*eslint-env node, browser */
2+
/* jshint browser: true */
3+
/**
4+
* Simple lightweight shim of Node.js's `url.parse()`
5+
* ( http://nodejs.org/docs/latest/api/url.html )
6+
* for use within browsers.
7+
*/
8+
(function () {
9+
'use strict';
10+
11+
// Only properties common to both browsers and Node.js are supported.
12+
// For what browsers support, see https://developer.mozilla.org/en-US/docs/Web/API/URLUtils
13+
var URL_PROPERTIES = [
14+
'hash',
15+
'host',
16+
'hostname',
17+
'href',
18+
'pathname',
19+
'port',
20+
'protocol',
21+
'search'
22+
];
23+
24+
/**
25+
* @param {string} urlStr URL to parse
26+
* @returns {object} Object with fields representing the various parts of the parsed URL.
27+
*/
28+
function parse(urlStr) {
29+
var anchor = document.createElement('a');
30+
anchor.href = urlStr;
31+
var urlObj = {};
32+
URL_PROPERTIES.forEach(function (property) {
33+
urlObj[property] = anchor[property];
34+
});
35+
return urlObj;
36+
}
37+
exports.parse = parse;
38+
})();

0 commit comments

Comments
 (0)