-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: accept HTMLCollection or NodeList as needle
- Loading branch information
Showing
6 changed files
with
143 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export function getCommonParent (needle: Element[]) { | ||
if (needle.length > 0) { | ||
let parent = needle[0].parentElement | ||
|
||
// optimization for single element | ||
if (needle.length === 1) { | ||
return parent | ||
} | ||
|
||
// find common parent for multiple elements | ||
while (parent) { | ||
if (needle.every(element => parent.contains(element))) { | ||
return parent | ||
} | ||
parent = parent.parentElement | ||
} | ||
} | ||
return null | ||
} | ||
|
||
export function * parentsGenerator (needle: Element[]) { | ||
// TODO | ||
} | ||
|
||
export function * viableParentsGenerator () { | ||
// TODO | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export function createRoot() { | ||
return document.body.appendChild(document.createElement("div")); | ||
} | ||
|
||
/** | ||
* Simple way to retrieve target element for test. | ||
* @returns {Element} | ||
*/ | ||
export function getTargetElement(root: Element): Element { | ||
return root.querySelector("[data-target]"); | ||
} | ||
|
||
/** | ||
* Simple way to retrieve multiple target elements for test. | ||
* @returns {Element[]} | ||
*/ | ||
export function getTargetElements(root: Element): Element[] { | ||
return [...root.querySelectorAll("[data-target]")]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {sanitizeSelectorNeedle} from "../src/utilities-selectors"; | ||
import {assert} from "chai"; | ||
import {createRoot} from "./test-utilities"; | ||
|
||
describe('Utilities - selectors', () => { | ||
let root: Element; | ||
beforeEach(() => { root = createRoot(); }); | ||
afterEach(() => { root.parentNode.removeChild(root); }); | ||
|
||
describe('sanitizeSelectorNeedle', () => { | ||
it('should remove non-element inputs', () => { | ||
const result = sanitizeSelectorNeedle([1, false, null, 'aaa', undefined]); | ||
assert.equal(result.length, 0); | ||
}); | ||
it('should convert single element into an array', () => { | ||
const result = sanitizeSelectorNeedle(root) | ||
assert.equal(result.length, 1); | ||
}); | ||
it('should return array of elements', () => { | ||
root.innerHTML = '<div></div><div></div>'; | ||
const result = sanitizeSelectorNeedle([root.children[0], root.children[1]]); | ||
assert.equal(result.length, 2) | ||
}) | ||
it('should convert NodeList into array of elements', () => { | ||
root.innerHTML = '<div></div><div></div>'; | ||
const result = sanitizeSelectorNeedle(root.querySelectorAll('div')); | ||
assert.equal(result.length, 2) | ||
}); | ||
it('should convert HTMLCollection into array of elements', () => { | ||
root.innerHTML = '<form></form><form></form>'; | ||
const result = sanitizeSelectorNeedle(document.forms); | ||
assert.equal(result.length, 2) | ||
}); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { | ||
createRoot, | ||
getTargetElement, | ||
getTargetElements | ||
} from "./test-utilities"; | ||
import {assert} from "chai"; | ||
import {getCommonParent} from "../src/utilities"; | ||
|
||
describe('Utilities', () => { | ||
|
||
describe('getCommonParent', () => { | ||
let root: Element; | ||
beforeEach(() => { root = createRoot(); }); | ||
afterEach(() => { root.parentNode.removeChild(root); }); | ||
|
||
it('should get direct parent of single element', () => { | ||
root.innerHTML = ` | ||
<div> | ||
<div class="directParent"> | ||
<div data-target></div> | ||
</div> | ||
</div> | ||
` | ||
const target = getTargetElement(root); | ||
const result = getCommonParent([target]); | ||
assert.equal(result.className, 'directParent') | ||
}); | ||
it('should get common parent of multiple elements', () => { | ||
root.innerHTML = ` | ||
<div> | ||
<div class="commonParent"> | ||
<div> | ||
<div data-target></div> | ||
</div> | ||
<div data-target></div> | ||
</div> | ||
</div> | ||
` | ||
const target = getTargetElements(root); | ||
const result = getCommonParent(target); | ||
assert.equal(result.className, 'commonParent') | ||
}); | ||
it('should return `null` if there is no common parent', () => { | ||
const result = getCommonParent([ | ||
document.createElement('div'), | ||
document.createElement('div') | ||
]) | ||
assert.isNull(result) | ||
}); | ||
}) | ||
|
||
}) |