Emit different warning message when jQuery and bootstrap.js are both absent; fixes #72

This commit is contained in:
Chris Rebert 2015-01-18 22:58:11 -08:00
Родитель c04de81891
Коммит cff261c4ca
5 изменённых файлов: 58 добавлений и 23 удалений

Просмотреть файл

@ -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: {

Просмотреть файл

@ -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
@ -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 */
@ -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 () {
@ -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 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));
}
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;
}
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 = [

Просмотреть файл

@ -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.');
@ -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) {

24
test/fixtures/jquery/and_bs_js_both_missing.html поставляемый Normal file
Просмотреть файл

@ -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
test/fixtures/jquery/missing.html поставляемый
Просмотреть файл

@ -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>
@ -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>