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

refactor: remove unnecessary stuff #198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
69 changes: 60 additions & 9 deletions src/__tests__/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ test('select elements with or without a namespace', '[*|href]', (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
});

test('select elements with or without a namespace (3)', '[ |href ]', (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[0].namespace, true);
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
});

test('namespace with escapes', '[\\31 \\#\\32 |href]', (t, tree) => {
let attr = tree.nodes[0].nodes[0];
t.deepEqual(attr.namespace, '1#2');
Expand Down Expand Up @@ -99,6 +104,30 @@ test('attribute selector with escaped quote', '[title="Something \\"weird\\""]',
t.deepEqual(tree.toString(), '[title="Something \\"weird\\""]');
});

test('attribute selector with escaped quote with comments', '[/*t*/title/*t*/=/*t*/"Something \\"weird\\""/*t*/]', (t, tree) => {
let attr = tree.nodes[0].nodes[0];
t.deepEqual(attr.value, 'Something "weird"');
t.deepEqual(attr.getQuotedValue(), '\"Something \\"weird\\"\"');
t.deepEqual(attr.getQuotedValue({smart: true}), '\'Something "weird"\'');
t.deepEqual(attr.getQuotedValue({quoteMark: null}), 'Something\\ \\"weird\\"');
t.deepEqual(attr.quoteMark, '"');
t.truthy(attr.quoted);
t.deepEqual(attr.raws.value, '"Something \\"weird\\""/*t*/');
t.deepEqual(tree.toString(), '[/*t*/title/*t*/=/*t*/"Something \\"weird\\""/*t*/]');
});

test('attribute selector with escaped quote with comments (2)', '[ /*t*/ title /*t*/ = /*t*/ "Something \\"weird\\"" /*t*/ ]', (t, tree) => {
let attr = tree.nodes[0].nodes[0];
t.deepEqual(attr.value, 'Something "weird"');
t.deepEqual(attr.getQuotedValue(), '\"Something \\"weird\\"\"');
t.deepEqual(attr.getQuotedValue({smart: true}), '\'Something "weird"\'');
t.deepEqual(attr.getQuotedValue({quoteMark: null}), 'Something\\ \\"weird\\"');
t.deepEqual(attr.quoteMark, '"');
t.truthy(attr.quoted);
t.deepEqual(attr.raws.value, '"Something \\"weird\\""');
t.deepEqual(tree.toString(), '[ /*t*/ title /*t*/ = /*t*/ "Something \\"weird\\"" /*t*/ ]');
});

test('attribute selector with escaped colon', '[ng\\:cloak]', (t, tree) => {
t.deepEqual(tree.toString(), '[ng\\:cloak]');
let attr = tree.nodes[0].nodes[0];
Expand Down Expand Up @@ -328,36 +357,58 @@ test('spaces in attribute selectors', 'h1[ href *= "test" ]', (t, tree) => {
t.truthy(tree.nodes[0].nodes[1].quoted);
});

test('insensitive attribute selector 1', '[href="test" i]', (t, tree) => {
test('insensitive attribute selector', '[href=test i]', (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[0].value, 'test');
t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
});

test('insensitive attribute selector with a empty value', '[href="" i]', (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
t.deepEqual(tree.nodes[0].nodes[0].value, '');
test('insensitive attribute selector 2', '[href="test" i]', (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[0].value, 'test');
t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
t.true(tree.nodes[0].nodes[0].quoted);
});

test('insensitive attribute selector 2', '[href=TEsT i ]', (t, tree) => {
test('insensitive attribute selector 3', '[href=TEsT i ]', (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[0].value, 'TEsT');
t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
t.deepEqual(tree.nodes[0].nodes[0].spaces.value.after, ' ');
t.deepEqual(tree.nodes[0].nodes[0].spaces.insensitive.after, ' ');
});

test('insensitive attribute selector 3', '[href=test i]', (t, tree) => {
test('insensitive attribute selector 4', '[href=test i]', (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[0].value, 'test');
t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
});

test('insensitive attribute selector 5', '[href="" i]', (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[0].value, '');
t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
});

test('sensitive attribute selector', '[href=test s]', (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[0].value, 'test');
t.deepEqual(tree.nodes[0].nodes[0].insensitive, false);
t.deepEqual(tree.nodes[0].nodes[0].raws.insensitiveFlag, 's');
});

test('multiple flags attribute selector', '[href=test qwer]', (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[0].value, 'test');
t.deepEqual(tree.nodes[0].nodes[0].insensitive, false);
t.deepEqual(tree.nodes[0].nodes[0].raws.insensitiveFlag, 'qwer');
});

Copy link
Collaborator Author

@alexander-akait alexander-akait Mar 19, 2019

Choose a reason for hiding this comment

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

just rename and add more, nothing changes

test('capitalized insensitive attribute selector 3', '[href=test I]', (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[0].value, 'test');
t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
});

test('insensitive attribute selector with a empty value', '[href="" i]', (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
t.deepEqual(tree.nodes[0].nodes[0].value, '');
t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
t.true(tree.nodes[0].nodes[0].quoted);
});

test('extraneous non-combinating whitespace', ' [href] , [class] ', (t, tree) => {
t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
t.deepEqual(tree.nodes[0].nodes[0].spaces.before, ' ');
Expand Down
43 changes: 28 additions & 15 deletions src/__tests__/exceptions.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
import {throws} from './util/helpers';

// Unclosed elements
throws('unclosed string', 'a[href="wow]');
throws('unclosed comment', '/* oops');
throws('unclosed pseudo element', 'button::');
throws('unclosed pseudo class', 'a:');
throws('unclosed attribute selector', '[name="james"][href');
throws('unclosed string', 'a[href="wow]', 'Unclosed quote');
throws('unclosed comment', '/* oops', 'Unclosed comment');
throws('unclosed pseudo element', 'button::', 'Expected a pseudo-class or pseudo-element.');
throws('unclosed pseudo class', 'a:', 'Expected a pseudo-class or pseudo-element.');
throws('unclosed attribute selector', '[name', 'Expected a closing square bracket.');
throws('unclosed attribute selector (2)', '[name=', 'Expected a closing square bracket.');
throws('unclosed attribute selector (3)', '[name="james"', 'Expected a closing square bracket.');
throws('unclosed attribute selector (4)', '[name="james"][href', 'Expected a closing square bracket.');
throws('unclosed attribute selector (5)', '[name="james"][href', 'Expected a closing square bracket.');

throws('no opening parenthesis', ')');
throws('no opening parenthesis (2)', ':global.foo)');
throws('no opening parenthesis (3)', 'h1:not(h2:not(h3)))');
throws('invalid attribute selector', '[]', 'Expected an attribute.');
throws('invalid attribute selector (2)', '["hello"]', 'Expected an attribute.');
throws('invalid attribute selector (3)', '[="hello"]', 'Expected an attribute, found "=" instead.');

throws('no opening square bracket', ']');
throws('no opening square bracket (2)', ':global.foo]');
throws('no opening square bracket (3)', '[global]]');
throws('no opening parenthesis', ')', 'Expected an opening parenthesis.');
throws('no opening parenthesis (2)', ':global.foo)', 'Expected an opening parenthesis.');
throws('no opening parenthesis (3)', 'h1:not(h2:not(h3)))', 'Expected an opening parenthesis.');

throws('bad pseudo element', 'button::"after"');
throws('missing closing parenthesis in pseudo', ':not([attr="test"]:not([attr="test"])');
throws('no opening square bracket', ']', 'Expected an opening square bracket.');
throws('no opening square bracket (2)', ':global.foo]', 'Expected an opening square bracket.');
throws('no opening square bracket (3)', '[global]]', 'Expected an opening square bracket.');

throws('bad syntax', '-moz-osx-font-smoothing: grayscale');
throws('bad syntax (2)', '! .body');
throws('bad pseudo element', 'button::"after"', 'Expected a pseudo-class or pseudo-element.');
throws('missing closing parenthesis in pseudo', ':not([attr="test"]:not([attr="test"])', 'Expected a closing parenthesis.');

throws('bad syntax', '-moz-osx-font-smoothing: grayscale', 'Expected a pseudo-class or pseudo-element.');
throws('bad syntax (2)', '! .body', 'Unexpected \'!\'. Escaping special characters with \\ may help.');

throws('missing backslash for semicolon', '.;');
throws('missing backslash for semicolon (2)', '.\;');
throws('unexpected / foo', '-Option\/root', "Unexpected '/'. Escaping special characters with \\ may help.");
throws('bang in selector', '.foo !optional', "Unexpected '!'. Escaping special characters with \\ may help.");

throws('misplaced parenthesis', ':not(', 'Expected a closing parenthesis.');
throws('misplaced parenthesis (2)', ':not)', 'Expected an opening parenthesis.');
throws('misplaced parenthesis (3)', ':not((', 'Expected a closing parenthesis.');
throws('misplaced parenthesis (4)', ':not))', 'Expected an opening parenthesis.');
Copy link
Collaborator Author

@alexander-akait alexander-akait Mar 19, 2019

Choose a reason for hiding this comment

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

Many errors was broken (throw invalid error on ( and )) because we don't validate message

Loading