From 6dd6777cf2ac215beea686d0b7b4c7fe0f90d989 Mon Sep 17 00:00:00 2001 From: Heinrich Fenkart Date: Wed, 12 Nov 2014 21:29:14 +0100 Subject: [PATCH] E034: Fix firing when `.close` button is not preceded by anything Fixes #181. --- dist/browser/bootlint.js | 4 ++- src/bootlint.js | 4 ++- test/bootlint_test.js | 6 +++- .../close-preceded-by-nothing.html | 30 +++++++++++++++++++ .../close-preceded-by-text.html | 8 ++--- 5 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/alert-dismiss-close/close-preceded-by-nothing.html diff --git a/dist/browser/bootlint.js b/dist/browser/bootlint.js index 93c660c8..31493585 100644 --- a/dist/browser/bootlint.js +++ b/dist/browser/bootlint.js @@ -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) { diff --git a/src/bootlint.js b/src/bootlint.js index f3258645..f1c480b6 100644 --- a/src/bootlint.js +++ b/src/bootlint.js @@ -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) { diff --git a/test/bootlint_test.js b/test/bootlint_test.js index 2df3e4ca..1a8d5fc0 100644 --- a/test/bootlint_test.js +++ b/test/bootlint_test.js @@ -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.' diff --git a/test/fixtures/alert-dismiss-close/close-preceded-by-nothing.html b/test/fixtures/alert-dismiss-close/close-preceded-by-nothing.html new file mode 100644 index 00000000..53f11002 --- /dev/null +++ b/test/fixtures/alert-dismiss-close/close-preceded-by-nothing.html @@ -0,0 +1,30 @@ + + + + + + + Test + + + + + + + + + +
+
+
+
This text should be after the button.
+
+
+
+
+
    + + diff --git a/test/fixtures/alert-dismiss-close/close-preceded-by-text.html b/test/fixtures/alert-dismiss-close/close-preceded-by-text.html index 054fc0cb..b27862c4 100644 --- a/test/fixtures/alert-dismiss-close/close-preceded-by-text.html +++ b/test/fixtures/alert-dismiss-close/close-preceded-by-text.html @@ -20,10 +20,10 @@
    -
    - This text should be after the button. - -
    +
    + This text should be after the button. + +