Loading src/once.js +17 −8 Original line number Diff line number Diff line Loading @@ -130,8 +130,8 @@ function checkElement(itemToCheck) { * * @param {NodeList|Array.<Element>|Element|string} selector * A NodeList or array of elements. * @param {Document|Element} [context=document] * An element to use as context for querySelectorAll. * @param {Document|DocumentFragment|Element} [context=document] * An element or document object to use as context for querySelectorAll. * * @return {Array.<Element>} * An array with the processed Id and the list of elements to process. Loading @@ -149,11 +149,20 @@ function getElements(selector, context = doc) { else if (!selector) { throw new TypeError('Selector must not be empty'); } // This is a selector, query the elements. // Context doesn't implement querySelectorAll, error out. else if ( typeof selector === 'string' && (context === doc || checkElement(context)) !( context instanceof Document || context instanceof DocumentFragment || context instanceof Element ) ) { throw new TypeError( 'Context must be an object of type "Document", "DocumentFragment", or "Element".', ); } // This is a selector, query the elements. else if (typeof selector === 'string') { elements = context.querySelectorAll(selector); } // This is a single element. Loading Loading @@ -266,8 +275,8 @@ function updateAttribute(element, { add, remove }) { * The id of the once call. * @param {NodeList|Array.<Element>|Element|string} selector * A NodeList or array of elements. * @param {Document|Element} [context=document] * An element to use as context for querySelectorAll. * @param {Document|DocumentFragment|Element} [context=document] * An element or document object to use as context for querySelectorAll. * * @return {Array.<Element>} * An array of elements that have not yet been processed by a once call Loading Loading @@ -379,7 +388,7 @@ once.filter = (id, selector, context) => * * @param {string} [id] * The id of the once call. * @param {Document|Element} [context=document] * @param {Document|DocumentFragment|Element} [context=document] * Scope of the search for matching elements. * * @return {Array.<Element>} Loading test/once.test.js +17 −0 Original line number Diff line number Diff line Loading @@ -161,4 +161,21 @@ describe('once', () => { it('Calling once with null (from a querySelector) should return an empty array', () => { expectArrayOfLength(once('test15', null), 0); }); it('Context can be of type `Document`, `DocumentFragment`, or `Element`', () => { expectArrayOfLength(once('test16', 'span', document), 2); expectArrayOfLength(once('test17', 'span', context), 1); // Make sure we find elements inside a DocumentFragment. const documentFragment = new DocumentFragment(); documentFragment.append(document.createElement('span')); expectArrayOfLength(once('test18', 'span', documentFragment), 1); }); it('Using a context of type other than `Document`, `DocumentFragment`, or `Element` throws error', () => { assert.throws( () => once('test19', 'span', 'p'), TypeError, 'Context must be an object of type "Document", "DocumentFragment", or "Element".', ); }); }); Loading
src/once.js +17 −8 Original line number Diff line number Diff line Loading @@ -130,8 +130,8 @@ function checkElement(itemToCheck) { * * @param {NodeList|Array.<Element>|Element|string} selector * A NodeList or array of elements. * @param {Document|Element} [context=document] * An element to use as context for querySelectorAll. * @param {Document|DocumentFragment|Element} [context=document] * An element or document object to use as context for querySelectorAll. * * @return {Array.<Element>} * An array with the processed Id and the list of elements to process. Loading @@ -149,11 +149,20 @@ function getElements(selector, context = doc) { else if (!selector) { throw new TypeError('Selector must not be empty'); } // This is a selector, query the elements. // Context doesn't implement querySelectorAll, error out. else if ( typeof selector === 'string' && (context === doc || checkElement(context)) !( context instanceof Document || context instanceof DocumentFragment || context instanceof Element ) ) { throw new TypeError( 'Context must be an object of type "Document", "DocumentFragment", or "Element".', ); } // This is a selector, query the elements. else if (typeof selector === 'string') { elements = context.querySelectorAll(selector); } // This is a single element. Loading Loading @@ -266,8 +275,8 @@ function updateAttribute(element, { add, remove }) { * The id of the once call. * @param {NodeList|Array.<Element>|Element|string} selector * A NodeList or array of elements. * @param {Document|Element} [context=document] * An element to use as context for querySelectorAll. * @param {Document|DocumentFragment|Element} [context=document] * An element or document object to use as context for querySelectorAll. * * @return {Array.<Element>} * An array of elements that have not yet been processed by a once call Loading Loading @@ -379,7 +388,7 @@ once.filter = (id, selector, context) => * * @param {string} [id] * The id of the once call. * @param {Document|Element} [context=document] * @param {Document|DocumentFragment|Element} [context=document] * Scope of the search for matching elements. * * @return {Array.<Element>} Loading
test/once.test.js +17 −0 Original line number Diff line number Diff line Loading @@ -161,4 +161,21 @@ describe('once', () => { it('Calling once with null (from a querySelector) should return an empty array', () => { expectArrayOfLength(once('test15', null), 0); }); it('Context can be of type `Document`, `DocumentFragment`, or `Element`', () => { expectArrayOfLength(once('test16', 'span', document), 2); expectArrayOfLength(once('test17', 'span', context), 1); // Make sure we find elements inside a DocumentFragment. const documentFragment = new DocumentFragment(); documentFragment.append(document.createElement('span')); expectArrayOfLength(once('test18', 'span', documentFragment), 1); }); it('Using a context of type other than `Document`, `DocumentFragment`, or `Element` throws error', () => { assert.throws( () => once('test19', 'span', 'p'), TypeError, 'Context must be an object of type "Document", "DocumentFragment", or "Element".', ); }); });