Skip to content

Commit

Permalink
E034: Fix firing when .close button is not preceded by anything
Browse files Browse the repository at this point in the history
Fixes twbs#181.
  • Loading branch information
hnrch02 committed Nov 12, 2014
1 parent df507dc commit 6dd6777
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
4 changes: 3 additions & 1 deletion dist/browser/bootlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -11039,7 +11039,9 @@ var LocationIndex = _location.LocationIndex;
addLinter("E034", function lintAlertDismissStructure($, reporter) {
var nonFirstChildCloses = $('.alert>.close:not(:first-child)');
var closesPrecededByText = $('.alert>.close').filter(function () {
return !!($(this).parent().contents().eq(0).text().trim());
var firstNode = $(this).parent().contents().eq(0);
var firstNodeIsText = IN_NODE_JS ? firstNode[0].type === 'text' : firstNode[0].nodeType === 3;
return !!(firstNodeIsText && firstNode.text().trim());
});
var problematicCloses = nonFirstChildCloses.add(closesPrecededByText);
if (problematicCloses.length) {
Expand Down
4 changes: 3 additions & 1 deletion src/bootlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,9 @@ var LocationIndex = _location.LocationIndex;
addLinter("E034", function lintAlertDismissStructure($, reporter) {
var nonFirstChildCloses = $('.alert>.close:not(:first-child)');
var closesPrecededByText = $('.alert>.close').filter(function () {
return !!($(this).parent().contents().eq(0).text().trim());
var firstNode = $(this).parent().contents().eq(0);
var firstNodeIsText = IN_NODE_JS ? firstNode[0].type === 'text' : firstNode[0].nodeType === 3;
return !!(firstNodeIsText && firstNode.text().trim());
});
var problematicCloses = nonFirstChildCloses.add(closesPrecededByText);
if (problematicCloses.length) {
Expand Down
6 changes: 5 additions & 1 deletion test/bootlint_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,11 +576,15 @@ exports.bootlint = {
},

'incorrect alerts with dismiss/close buttons': function (test) {
test.expect(4);
test.expect(5);
test.deepEqual(lintHtml(utf8Fixture('alert-dismiss-close/valid.html')),
[],
'should not complain when dismissible alert markup structure is correct.'
);
test.deepEqual(lintHtml(utf8Fixture('alert-dismiss-close/close-preceded-by-nothing.html')),
[],
'should not complain when close button is preceded by nothing.'
);
test.deepEqual(lintHtml(utf8Fixture('alert-dismiss-close/missing-alert-dismissible.html')),
['`.alert` with dismiss button must have class `.alert-dismissible`'],
'should complain when alert with dismiss button is missing .alert-dismissible class.'
Expand Down
30 changes: 30 additions & 0 deletions test/fixtures/alert-dismiss-close/close-preceded-by-nothing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!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]-->
<script src="../../lib/jquery.min.js"></script>

<link rel="stylesheet" href="../../lib/qunit.css">
<script src="../../lib/qunit.js"></script>
<script src="../../../dist/browser/bootlint.js"></script>
<script src="../generic-qunit.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="alert alert-danger alert-dismissible"><button type="button" class="close">Close</button>This text should be after the button.</div>
</div>
</div>
</div>
<div id="qunit"></div>
<ol id="bootlint"></ol>
</body>
</html>
8 changes: 4 additions & 4 deletions test/fixtures/alert-dismiss-close/close-preceded-by-text.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="alert alert-danger alert-dismissible">
This text should be after the button.
<button type="button" class="close">Close</button>
</div>
<div class="alert alert-danger alert-dismissible">
This text should be after the button.
<button type="button" class="close">Close</button>
</div>
</div>
</div>
</div>
Expand Down

0 comments on commit 6dd6777

Please sign in to comment.