diff --git a/js/src/dom/selectorEngine.js b/js/src/dom/selectorEngine.js index b6b45bacb4..55d2ce4bbc 100644 --- a/js/src/dom/selectorEngine.js +++ b/js/src/dom/selectorEngine.js @@ -3,7 +3,7 @@ import Util from '../util' /** * -------------------------------------------------------------------------- - * Bootstrap (v4.0.0-beta): dom/selectorEngine.js + * Bootstrap (v4.1.1): dom/selectorEngine.js * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) * -------------------------------------------------------------------------- */ @@ -69,6 +69,10 @@ const SelectorEngine = (() => { }, closest(element, selector) { + if (typeof selector !== 'string') { + return null + } + return closest(element, selector) }, diff --git a/js/tests/index.html b/js/tests/index.html index f4a99df44e..d0ff5b82b6 100644 --- a/js/tests/index.html +++ b/js/tests/index.html @@ -119,6 +119,7 @@ + diff --git a/js/tests/unit/.eslintrc.json b/js/tests/unit/.eslintrc.json index 4010b98305..dfcf1eaa50 100644 --- a/js/tests/unit/.eslintrc.json +++ b/js/tests/unit/.eslintrc.json @@ -15,7 +15,8 @@ "Simulator": false, "Toast": false, "EventHandler": false, - "Manipulator": false + "Manipulator": false, + "SelectorEngine": false }, "parserOptions": { "ecmaVersion": 5, diff --git a/js/tests/unit/dom/eventHandler.js b/js/tests/unit/dom/eventHandler.js index f282616328..076a28a8a1 100644 --- a/js/tests/unit/dom/eventHandler.js +++ b/js/tests/unit/dom/eventHandler.js @@ -1,7 +1,7 @@ $(function () { 'use strict' - QUnit.module('event handler') + QUnit.module('eventHandler') QUnit.test('should be defined', function (assert) { assert.expect(1) diff --git a/js/tests/unit/dom/selectorEngine.js b/js/tests/unit/dom/selectorEngine.js new file mode 100644 index 0000000000..80bee7800e --- /dev/null +++ b/js/tests/unit/dom/selectorEngine.js @@ -0,0 +1,77 @@ +$(function () { + 'use strict' + + QUnit.module('selectorEngine') + + QUnit.test('should be defined', function (assert) { + assert.expect(1) + assert.ok(SelectorEngine, 'Manipulator is defined') + }) + + QUnit.test('should determine if an element match the selector', function (assert) { + assert.expect(2) + $(' ').appendTo('#qunit-fixture') + + assert.ok(!SelectorEngine.matches($('#qunit-fixture')[0], '.btn')) + assert.ok(SelectorEngine.matches($('.btn')[0], '.btn')) + }) + + QUnit.test('should find the selector, according to an element or not', function (assert) { + assert.expect(3) + $(' ').appendTo('#qunit-fixture') + + var btn = $('.btn').first()[0] + assert.strictEqual(SelectorEngine.find($('.btn')), null) + assert.equal(SelectorEngine.find('.btn')[0], btn) + assert.equal(SelectorEngine.find('.btn', $('#qunit-fixture')[0])[0], btn) + }) + + QUnit.test('should find the first element which match the selector, according to an element or not', function (assert) { + assert.expect(3) + $(' ').appendTo('#qunit-fixture') + + var btn = $('.btn').first()[0] + assert.strictEqual(SelectorEngine.findOne($('.btn')), null) + assert.equal(SelectorEngine.findOne('.btn'), btn) + assert.equal(SelectorEngine.findOne('.btn', $('#qunit-fixture')[0]), btn) + }) + + QUnit.test('should find children', function (assert) { + assert.expect(2) + $(' ').appendTo('#qunit-fixture') + + assert.strictEqual(SelectorEngine.children($('.btn')), null) + assert.equal(SelectorEngine.children($('#qunit-fixture')[0], '.btn').length, 2) + }) + + QUnit.test('should find the selector in parents', function (assert) { + assert.expect(2) + + $('').appendTo('#qunit-fixture') + assert.strictEqual(SelectorEngine.parents($('.container')[0], {}), null) + assert.strictEqual(SelectorEngine.parents($('input')[0], 'body').length, 1) + }) + + QUnit.test('should find the closest element according to the selector', function (assert) { + assert.expect(2) + var html = + '