-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
164 additions
and
0 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
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,7 @@ | ||
export default function selectAll(context, selector) { | ||
if (!context || typeof context.querySelectorAll !== 'function') { | ||
return []; | ||
} | ||
|
||
return [].slice.call(context.querySelectorAll(selector)); | ||
} |
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,7 @@ | ||
export default function select(context, selector) { | ||
if (!context || typeof context.querySelector !== 'function') { | ||
return null; | ||
} | ||
|
||
return context.querySelector(selector); | ||
} |
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,56 @@ | ||
import {selectAll} from '../..'; | ||
import createFixture from '../fixture'; | ||
|
||
describe('selectAll()', () => { | ||
let fixture; | ||
|
||
beforeEach(() => { | ||
fixture = createFixture(); | ||
fixture.append( | ||
'<span id="foo" class="bar"><span id="nested" class="baz"></span></span>' | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
fixture.destroy(); | ||
fixture = null; | ||
}); | ||
|
||
it('queries against a CSS selector', () => { | ||
const span = document.querySelector('#foo'); | ||
const nested = document.querySelector('#nested'); | ||
|
||
assert.equal(selectAll(span, 'span').length, 1); | ||
assert.equal(selectAll(span, 'span')[0], nested); | ||
|
||
assert.equal(selectAll(document, '#foo').length, 1); | ||
assert.equal(selectAll(document, '#foo')[0], span); | ||
|
||
assert.equal(selectAll(span, '#nested').length, 1); | ||
assert.equal(selectAll(span, '#nested')[0], nested); | ||
|
||
assert.equal(selectAll(document, 'body .bar').length, 1); | ||
assert.equal(selectAll(document, 'body .bar')[0], span); | ||
|
||
assert.equal(selectAll(span, 'p').length, 0); | ||
|
||
assert.equal(selectAll(span, '#bar').length, 0); | ||
}); | ||
|
||
it('works for non-nodes', () => { | ||
assert.equal(selectAll(undefined, '#foo').length, 0); | ||
assert.equal(selectAll('string', '#foo').length, 0); | ||
assert.equal(selectAll(true, '#foo').length, 0); | ||
assert.equal(selectAll(null, '#foo').length, 0); | ||
assert.equal(selectAll(1, '#foo').length, 0); | ||
assert.equal(selectAll(1.2, '#foo').length, 0); | ||
assert.equal(selectAll({foo: 'bar'}, '#foo').length, 0); | ||
assert.equal(selectAll(['bar'], '#foo').length, 0); | ||
}); | ||
|
||
it('throws syntax error for invalid selector', () => { | ||
assert.throws(() => { | ||
selectAll(document, ''); | ||
}); | ||
}); | ||
}); |
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 {select} from '../..'; | ||
import createFixture from '../fixture'; | ||
|
||
describe('select()', () => { | ||
let fixture; | ||
|
||
beforeEach(() => { | ||
fixture = createFixture(); | ||
fixture.append( | ||
'<span id="foo" class="bar"><span id="nested" class="baz"></span></span>' | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
fixture.destroy(); | ||
fixture = null; | ||
}); | ||
|
||
it('queries against a CSS selector', () => { | ||
const span = document.querySelector('#foo'); | ||
const nested = document.querySelector('#nested'); | ||
|
||
assert.equal(select(span, 'span'), nested); | ||
|
||
assert.equal(select(document, '#foo'), span); | ||
|
||
assert.equal(select(span, '#nested'), nested); | ||
|
||
assert.equal(select(document, 'body .bar'), span); | ||
|
||
assert.isNull(select(span, 'p')); | ||
|
||
assert.isNull(select(span, '#bar')); | ||
}); | ||
|
||
it('works for non-nodes', () => { | ||
assert.isNull(select(undefined, '#foo')); | ||
assert.isNull(select('string', '#foo')); | ||
assert.isNull(select(true, '#foo')); | ||
assert.isNull(select(null, '#foo')); | ||
assert.isNull(select(1, '#foo')); | ||
assert.isNull(select(1.2, '#foo')); | ||
assert.isNull(select({foo: 'bar'}, '#foo')); | ||
assert.isNull(select(['bar'], '#foo')); | ||
}); | ||
|
||
it('throws syntax error for invalid selector', () => { | ||
assert.throws(() => { | ||
select(document, ''); | ||
}); | ||
}); | ||
}); |