Skip to content

Commit 63b9aa1

Browse files
committed
add support for native arrays of html elements
1 parent f646d9e commit 63b9aa1

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project tries its best to use [Semantic Versioning](http://semver.org/)
1515
- New `clean()` method removes specific generated styles and event listeners. [#227](https://github.com/jlmakes/scrollreveal/issues/227)
1616
- New `destroy()` method removes all generated styles and event listeners. [#227](https://github.com/jlmakes/scrollreveal/issues/227)
1717
- New `debug` instance property toggles error messages in console. [#351](https://github.com/jlmakes/scrollreveal/issues/351)
18+
- Instance methods now accept native arrays of HTML elements.
1819

1920
### Changed
2021
- **Breaking:** The `reveal()` method will not accept sequence intervals less than 16 milliseconds.

src/utils/core.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,17 @@ export function getNode (target, container = document) {
8080

8181

8282
export function getNodes (target, container = document) {
83+
if (target instanceof Array) {
84+
return target
85+
}
8386
if (isNode(target)) {
8487
return [target]
8588
}
8689
if (isNodeList(target)) {
8790
return Array.prototype.slice.call(target)
8891
}
89-
let query
9092
if (typeof target === 'string') {
93+
let query
9194
try {
9295
query = container.querySelectorAll(target)
9396
} catch (e) {
@@ -96,8 +99,8 @@ export function getNodes (target, container = document) {
9699
if (query.length === 0) {
97100
throw new Error(`The selector "${target}" matches 0 elements.`)
98101
}
102+
return Array.prototype.slice.call(query)
99103
}
100-
return Array.prototype.slice.call(query)
101104
}
102105

103106

test/utils/core.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ describe('Core Utilities', () => {
4242

4343
describe('getNodes()', () => {
4444

45+
it('should return the same array of nodes when passed an array of nodes', () => {
46+
const actual = [].concat(document.querySelectorAll('html, body, script'))
47+
const result = getNodes(actual)
48+
expect(result).to.equal(actual)
49+
})
50+
4551
it('should return an array containing the same node when passed a node', () => {
4652
const element = document.documentElement
4753
const actual = [element]

0 commit comments

Comments
 (0)