Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn if selector contains a pseudo-class #591

Merged
merged 2 commits into from
Sep 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,27 @@ export function splitSelector(selector) {
return selector.split(/(?=\.|\[.*\])|(?=#|\[#.*\])/);
}

export function isSimpleSelector(selector) {
// any of these characters pretty much guarantee it's a complex selector
return !/[~\s:>]/.test(selector);

const containsQuotes = /'|"/;
const containsColon = /:/;


export function isPseudoClassSelector(selector) {
if (containsColon.test(selector)) {
if (!containsQuotes.test(selector)) {
return true;
}
const tokens = selector.split(containsQuotes);
return tokens.some((token, i) =>
containsColon.test(token) && i % 2 === 0
);
}
return false;
}

export function selectorError(selector) {
export function selectorError(selector, type = '') {
return new TypeError(
`Enzyme received a complex CSS selector ('${selector}') that it does not currently support`
`Enzyme received a ${type} CSS selector ('${selector}') that it does not currently support`
);
}

Expand All @@ -187,6 +200,9 @@ export const SELECTOR = {
};

export function selectorType(selector) {
if (isPseudoClassSelector(selector)) {
throw selectorError(selector, 'pseudo-class');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there usually a hyphen in pseudoclass?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hyphenated in the spec.

}
if (selector[0] === '.') {
return SELECTOR.CLASS_TYPE;
} else if (selector[0] === '#') {
Expand Down
4 changes: 2 additions & 2 deletions test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,12 @@ describeWithDOM('mount', () => {
React.createElement('div', null, React.createElement('span', {
'123-foo': 'bar',
'-foo': 'bar',
':foo': 'bar',
'+foo': 'bar',
}))
);

expect(wrapper.find('[-foo]')).to.have.length(0, '-foo');
expect(wrapper.find('[:foo]')).to.have.length(0, ':foo');
expect(wrapper.find('[+foo]')).to.have.length(0, '+foo');
expect(wrapper.find('[123-foo]')).to.have.length(0, '123-foo');
});

Expand Down
50 changes: 27 additions & 23 deletions test/Utils-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
coercePropValue,
getNode,
nodeEqual,
isSimpleSelector,
isPseudoClassSelector,
propFromEvent,
SELECTOR,
selectorType,
Expand Down Expand Up @@ -246,39 +246,43 @@ describe('Utils', () => {
});


describe('isSimpleSelector', () => {
describe('isPseudoClassSelector', () => {


describe('prohibited selectors', () => {
function isComplex(selector) {
function isNotPseudo(selector) {
it(selector, () => {
expect(isSimpleSelector(selector)).to.equal(false);
expect(isPseudoClassSelector(selector)).to.equal(false);
});
}

isComplex('.foo .bar');
isComplex(':visible');
isComplex('.foo>.bar');
isComplex('.foo > .bar');
isComplex('.foo~.bar');

isNotPseudo('.foo');
isNotPseudo('div');
isNotPseudo('.foo .bar');
isNotPseudo('[hover]');
isNotPseudo('[checked=""]');
isNotPseudo('[checked=":checked"]');
isNotPseudo('[checked=\':checked\']');
isNotPseudo('.foo>.bar');
isNotPseudo('.foo > .bar');
isNotPseudo('.foo~.bar');
isNotPseudo('#foo');
});

describe('allowed selectors', () => {
function isSimple(selector) {
function isPseudo(selector) {
it(selector, () => {
expect(isSimpleSelector(selector)).to.equal(true);
expect(isPseudoClassSelector(selector)).to.equal(true);
});
}

isSimple('.foo');
isSimple('.foo-and-foo');
isSimple('input[foo="bar"]');
isSimple('input[foo="bar"][bar="baz"][baz="foo"]');
isSimple('.FoOaNdFoO');
isSimple('tag');
isSimple('.foo.bar');
isSimple('input.foo');

isPseudo(':checked');
isPseudo(':focus');
isPseudo(':hover');
isPseudo(':disabled');
isPseudo(':any');
isPseudo(':last-child');
isPseudo(':nth-child(1)');
isPseudo('div:checked');
isPseudo('[data-foo=":hover"]:hover');
});

});
Expand Down