Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
Emit different warning message when jQuery and bootstrap.js are both …
Browse files Browse the repository at this point in the history
…absent; fixes #72
  • Loading branch information
cvrebert committed Jan 19, 2015
1 parent c04de81 commit cff261c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Expand Up @@ -54,7 +54,7 @@ module.exports = function (grunt) {
options: {
timeout: 10000
},
files: ['test/fixtures/**/*.html', '!test/fixtures/jquery/missing.html', '!test/fixtures/charset/not-utf8.html']
files: ['test/fixtures/**/*.html', '!test/fixtures/jquery/missing.html', '!test/fixtures/jquery/and_bs_js_both_missing.html', '!test/fixtures/charset/not-utf8.html']
},
jshint: {
options: {
Expand Down
45 changes: 26 additions & 19 deletions src/bootlint.js
Expand Up @@ -161,6 +161,24 @@ var LocationIndex = _location.LocationIndex;
return runs;
}

function bootstrapScriptsIn($) {
var longhands = $('script[src*="bootstrap.js"]').filter(function (i, script) {
var url = $(script).attr('src');
var filename = filenameFromUrl(url);
return filename === "bootstrap.js";
});
var minifieds = $('script[src*="bootstrap.min.js"]').filter(function (i, script) {
var url = $(script).attr('src');
var filename = filenameFromUrl(url);
return filename === "bootstrap.min.js";
});

return {
longhands: longhands,
minifieds: minifieds
};
}

/**
* @param {integer} id Unique string ID for this type of lint error. Of the form "E###" (e.g. "E123").
* @param {string} message Human-readable string describing the error
Expand Down Expand Up @@ -333,7 +351,10 @@ var LocationIndex = _location.LocationIndex;
});
addLinter("W005", function lintJquery($, reporter) {
var OLD_JQUERY = "Found what might be an outdated version of jQuery; Bootstrap requires jQuery v" + MIN_JQUERY_VERSION + " or higher";
var NO_JQUERY = "Unable to locate jQuery, which is required for Bootstrap's JavaScript plugins to work";
var NO_JQUERY_BUT_BS_JS = "Unable to locate jQuery, which is required for Bootstrap's JavaScript plugins to work";
var NO_JQUERY_NOR_BS_JS = "Unable to locate jQuery, which is required for Bootstrap's JavaScript plugins to work; however, you might not be using Bootstrap's JavaScript";
var bsScripts = bootstrapScriptsIn($);
var hasBsJs = !!(bsScripts.minifieds.length || bsScripts.longhands.length);
var theWindow = null;
try {
/*eslint-disable no-undef, block-scoped-var */
Expand Down Expand Up @@ -384,7 +405,7 @@ var LocationIndex = _location.LocationIndex;
'script[src*="jQuery"]'
].join(','));
if (!jqueries.length) {
reporter(NO_JQUERY);
reporter(hasBsJs ? NO_JQUERY_BUT_BS_JS : NO_JQUERY_NOR_BS_JS);
return;
}
jqueries.each(function () {
Expand Down Expand Up @@ -420,24 +441,10 @@ var LocationIndex = _location.LocationIndex;
}
});
addLinter("E007", function lintBootstrapJs($, reporter) {
var longhands = $('script[src*="bootstrap.js"]').filter(function (i, script) {
var url = $(script).attr('src');
var filename = filenameFromUrl(url);
return filename === "bootstrap.js";
});
if (!longhands.length) {
return;
}
var minifieds = $('script[src*="bootstrap.min.js"]').filter(function (i, script) {
var url = $(script).attr('src');
var filename = filenameFromUrl(url);
return filename === "bootstrap.min.js";
});
if (!minifieds.length) {
return;
var scripts = bootstrapScriptsIn($);
if (scripts.longhands.length && scripts.minifieds.length) {
reporter("Only one copy of Bootstrap's JS should be included; currently the webpage includes both bootstrap.js and bootstrap.min.js", scripts.longhands.add(scripts.minifieds));
}

reporter("Only one copy of Bootstrap's JS should be included; currently the webpage includes both bootstrap.js and bootstrap.min.js", longhands.add(minifieds));
});
addLinter("W006", function lintTooltipsOnDisabledElems($, reporter) {
var selector = [
Expand Down
7 changes: 5 additions & 2 deletions test/bootlint_test.js
Expand Up @@ -176,7 +176,7 @@ exports.bootlint = {
test.done();
},
'jQuery': function (test) {
test.expect(4);
test.expect(5);
test.deepEqual(lintHtml(utf8Fixture('jquery/present.html')),
[],
'should not complain when jQuery is present.');
Expand All @@ -188,7 +188,10 @@ exports.bootlint = {
'should complain about old version of jQuery based on URL');
test.deepEqual(lintHtml(utf8Fixture('jquery/missing.html')),
["Unable to locate jQuery, which is required for Bootstrap's JavaScript plugins to work"],
'should complain when jQuery appears to be missing.');
"should complain when jQuery appears to be missing.");
test.deepEqual(lintHtml(utf8Fixture('jquery/and_bs_js_both_missing.html')),
["Unable to locate jQuery, which is required for Bootstrap's JavaScript plugins to work; however, you might not be using Bootstrap's JavaScript"],
"should complain differently when jQuery appears to be missing but Bootstrap's JS is also missing.");
test.done();
},
'bootstrap[.min].js': function (test) {
Expand Down
24 changes: 24 additions & 0 deletions test/fixtures/jquery/and_bs_js_both_missing.html
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Test</title>
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->

<link rel="stylesheet" href="../../lib/qunit.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/qunit/1.14.0/qunit.min.js"></script>
<script src="../../../dist/browser/bootlint.js"></script>
<script src="../generic-qunit.js"></script>
</head>
<body>
<div id="qunit"></div>
<ol id="bootlint">
<li data-lint="Unable to locate jQuery, which is required for Bootstrap's JavaScript plugins to work"></li>
</ol>
</body>
</html>
3 changes: 2 additions & 1 deletion test/fixtures/jquery/missing.html
Expand Up @@ -9,6 +9,7 @@
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<script src="/js/bootstrap.js"></script>

<link rel="stylesheet" href="../../lib/qunit.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/qunit/1.14.0/qunit.min.js"></script>
Expand All @@ -18,7 +19,7 @@
<body>
<div id="qunit"></div>
<ol id="bootlint">
<li data-lint="Unable to locate jQuery, which is required for Bootstrap's JavaScript plugins to work"></li>
<li data-lint="Unable to locate jQuery, which is required for Bootstrap's JavaScript plugins to work; however, you might not be using Bootstrap's JavaScript"></li>
</ol>
</body>
</html>

1 comment on commit cff261c

@cvrebert
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Derp, this fixes #210 and helps fix cvrebert/lmvtfy#72

Please sign in to comment.