mirror of
https://github.com/zhigang1992/styled-components.git
synced 2026-04-29 09:45:54 +08:00
31 lines
937 B
JavaScript
31 lines
937 B
JavaScript
// @flow
|
|
|
|
function assertElement(element) {
|
|
if (!(element instanceof HTMLElement)) {
|
|
throw new Error('Must pass a DOM element to find/findAll(element, styledComponent)"');
|
|
}
|
|
}
|
|
|
|
function assertStyledComponent(styledComponent) {
|
|
if (
|
|
!(styledComponent.styledComponentId && typeof styledComponent.styledComponentId === 'string')
|
|
) {
|
|
throw new Error('Must pass a styled component to find/findAll(element, styledComponent)"');
|
|
}
|
|
}
|
|
|
|
function find(element /* : Element */, styledComponent /* : Object */) {
|
|
assertElement(element);
|
|
assertStyledComponent(styledComponent);
|
|
return element.querySelector(`.${styledComponent.styledComponentId}`);
|
|
}
|
|
|
|
function findAll(element /* : Element */, styledComponent /* : Object */) {
|
|
assertElement(element);
|
|
assertStyledComponent(styledComponent);
|
|
return element.querySelectorAll(`.${styledComponent.styledComponentId}`);
|
|
}
|
|
|
|
exports.find = find;
|
|
exports.findAll = findAll;
|