Skip to content

Commit 9333432

Browse files
authored
feat: added new methods: children, clear, childNodes, insertAfter, isInput, isVisible, nextUntil, parents, prepend, remove, siblings, text, triggerEvent(#167)
* * fix: changed children helper to use a shorter approach * feat: added new method: attribute, renamed children to childElements * fix: attribute value can be boolean or null
1 parent bd676f5 commit 9333432

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+598
-18
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"rules": {
44
"no-nested-ternary": "off",
55
"consistent-return": "off",
6+
"import/extensions": "off",
67
"import/no-mutable-exports": "off",
78
"@typescript-eslint/member-delimiter-style": "off"
89
}

.size-snapshot.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"lib/dist/dom-helpers.js": {
3-
"bundled": 15481,
4-
"minified": 8329,
5-
"gzipped": 3124,
3+
"bundled": 24575,
4+
"minified": 12882,
5+
"gzipped": 4632,
66
"treeshaked": {
77
"rollup": {
8-
"code": 653,
8+
"code": 716,
99
"import_statements": 0
1010
},
1111
"webpack": {
12-
"code": 1940
12+
"code": 2003
1313
}
1414
}
1515
}

src/activeElement.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import ownerDocument from './ownerDocument'
22

33
/**
4-
* Return the actively focused element safely.
4+
* Returns the actively focused element safely.
55
*
6-
* @param doc the document to checl
6+
* @param doc the document to check
77
*/
88
export default function activeElement(doc = ownerDocument()) {
99
// Support: IE 9 only

src/addClass.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import hasClass from './hasClass'
22

3+
/**
4+
* Adds a CSS class to a given element.
5+
*
6+
* @param element the element
7+
* @param className the CSS class name
8+
*/
39
export default function addClass(
410
element: Element | SVGElement,
511
className: string

src/addEventListener.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@ export type EventHandler<K extends keyof HTMLElementEventMap> = (
3030
export type TaggedEventHandler<
3131
K extends keyof HTMLElementEventMap
3232
> = EventHandler<K> & { __once?: EventHandler<K> }
33+
3334
/**
3435
* An `addEventListener` ponyfill, supports the `once` option
36+
*
37+
* @param node the element
38+
* @param eventName the event name
39+
* @param handle the handler
40+
* @param options event options
3541
*/
3642
function addEventListener<K extends keyof HTMLElementEventMap>(
3743
node: HTMLElement,

src/attribute.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Gets or sets an attribute of a given element.
3+
*
4+
* @param node the element
5+
* @param attr the attribute to get or set
6+
* @param val the attribute value
7+
*/
8+
export default function attribute(
9+
node: Element | null,
10+
attr: string,
11+
val?: string | boolean | null
12+
): string | null | undefined {
13+
if (node) {
14+
if (typeof val === 'undefined') {
15+
return node.getAttribute(attr);
16+
}
17+
if (!val && val !== '') {
18+
node.removeAttribute(attr);
19+
} else {
20+
node.setAttribute(attr, String(val));
21+
}
22+
}
23+
}
24+

src/childElements.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Collects all child elements of an element.
3+
*
4+
* @param node the element
5+
*/
6+
export default function childElements(
7+
node: Element | null
8+
): Element[] {
9+
return node ? Array.from(node.children) : [];
10+
}

src/childNodes.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const toArray = Function.prototype.bind.call(Function.prototype.call, [].slice)
2+
3+
/**
4+
* Collects all child nodes of an element.
5+
*
6+
* @param node the node
7+
*/
8+
export default function childNodes(
9+
node: Element | null
10+
): Node[] {
11+
return node ? toArray(node.childNodes) : [];
12+
}

src/clear.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Removes all child nodes from a given node.
3+
*
4+
* @param node the node to clear
5+
*/
6+
export default function clear(
7+
node: Node | null
8+
): Node | null {
9+
if (node) {
10+
while (node.firstChild) {
11+
node.removeChild(node.firstChild);
12+
}
13+
return node;
14+
}
15+
return null;
16+
}

src/closest.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import matches from './matches'
22

3+
/**
4+
* Returns the closest parent element that matches a given selector.
5+
*
6+
* @param node the reference element
7+
* @param selector the selector to match
8+
* @param stopAt stop traversing when this element is found
9+
*/
310
export default function closest(
411
node: Element,
512
selector: string,

0 commit comments

Comments
 (0)