2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-07-21 20:12:00 -04:00
|
|
|
import {CssSelector, SelectorMatcher} from '@angular/compiler/src/selector';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {el} from '@angular/platform-browser/testing/src/browser_util';
|
2014-10-28 17:46:55 -04:00
|
|
|
|
2017-12-16 17:42:55 -05:00
|
|
|
{
|
2014-10-28 17:46:55 -04:00
|
|
|
describe('SelectorMatcher', () => {
|
2016-09-07 18:38:44 -04:00
|
|
|
let matcher: SelectorMatcher;
|
|
|
|
let selectableCollector: (selector: CssSelector, context: any) => void;
|
|
|
|
let s1: any[], s2: any[], s3: any[], s4: any[];
|
|
|
|
let matched: any[];
|
2014-10-28 17:46:55 -04:00
|
|
|
|
2015-06-17 14:17:21 -04:00
|
|
|
function reset() { matched = []; }
|
2014-10-28 17:46:55 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
reset();
|
2017-03-24 12:59:58 -04:00
|
|
|
s1 = s2 = s3 = s4 = null !;
|
2016-09-07 18:38:44 -04:00
|
|
|
selectableCollector =
|
|
|
|
(selector: CssSelector, context: any) => { matched.push(selector, context); };
|
2014-10-28 17:46:55 -04:00
|
|
|
matcher = new SelectorMatcher();
|
|
|
|
});
|
|
|
|
|
2015-11-23 19:02:19 -05:00
|
|
|
it('should select by element name case sensitive', () => {
|
2015-03-19 12:01:42 -04:00
|
|
|
matcher.addSelectables(s1 = CssSelector.parse('someTag'), 1);
|
2014-10-28 17:46:55 -04:00
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(getSelectorFor({tag: 'SOMEOTHERTAG'}), selectableCollector))
|
2015-05-26 12:25:39 -04:00
|
|
|
.toEqual(false);
|
2014-10-28 17:46:55 -04:00
|
|
|
expect(matched).toEqual([]);
|
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(getSelectorFor({tag: 'SOMETAG'}), selectableCollector)).toEqual(false);
|
2015-11-23 19:02:19 -05:00
|
|
|
expect(matched).toEqual([]);
|
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(getSelectorFor({tag: 'someTag'}), selectableCollector)).toEqual(true);
|
2015-05-26 12:25:39 -04:00
|
|
|
expect(matched).toEqual([s1[0], 1]);
|
2014-10-28 17:46:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should select by class name case insensitive', () => {
|
2015-03-19 12:01:42 -04:00
|
|
|
matcher.addSelectables(s1 = CssSelector.parse('.someClass'), 1);
|
|
|
|
matcher.addSelectables(s2 = CssSelector.parse('.someClass.class2'), 2);
|
2014-10-28 17:46:55 -04:00
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(getSelectorFor({classes: 'SOMEOTHERCLASS'}), selectableCollector))
|
2015-05-26 12:25:39 -04:00
|
|
|
.toEqual(false);
|
2014-10-28 17:46:55 -04:00
|
|
|
expect(matched).toEqual([]);
|
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(getSelectorFor({classes: 'SOMECLASS'}), selectableCollector))
|
|
|
|
.toEqual(true);
|
2015-05-26 12:25:39 -04:00
|
|
|
expect(matched).toEqual([s1[0], 1]);
|
2014-10-28 17:46:55 -04:00
|
|
|
|
|
|
|
reset();
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(getSelectorFor({classes: 'someClass class2'}), selectableCollector))
|
2015-05-26 12:25:39 -04:00
|
|
|
.toEqual(true);
|
|
|
|
expect(matched).toEqual([s1[0], 1, s2[0], 2]);
|
2014-10-28 17:46:55 -04:00
|
|
|
});
|
|
|
|
|
2016-10-17 18:19:18 -04:00
|
|
|
it('should not throw for class name "constructor"', () => {
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(getSelectorFor({classes: 'constructor'}), selectableCollector))
|
2016-10-17 18:19:18 -04:00
|
|
|
.toEqual(false);
|
|
|
|
expect(matched).toEqual([]);
|
|
|
|
});
|
|
|
|
|
2015-11-23 19:02:19 -05:00
|
|
|
it('should select by attr name case sensitive independent of the value', () => {
|
2015-03-19 12:01:42 -04:00
|
|
|
matcher.addSelectables(s1 = CssSelector.parse('[someAttr]'), 1);
|
|
|
|
matcher.addSelectables(s2 = CssSelector.parse('[someAttr][someAttr2]'), 2);
|
2014-10-28 17:46:55 -04:00
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(getSelectorFor({attrs: [['SOMEOTHERATTR', '']]}), selectableCollector))
|
2015-05-26 12:25:39 -04:00
|
|
|
.toEqual(false);
|
2014-10-28 17:46:55 -04:00
|
|
|
expect(matched).toEqual([]);
|
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(getSelectorFor({attrs: [['SOMEATTR', '']]}), selectableCollector))
|
|
|
|
.toEqual(false);
|
2015-11-23 19:02:19 -05:00
|
|
|
expect(matched).toEqual([]);
|
2014-10-28 17:46:55 -04:00
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(
|
|
|
|
matcher.match(getSelectorFor({attrs: [['SOMEATTR', 'someValue']]}), selectableCollector))
|
2015-11-23 19:02:19 -05:00
|
|
|
.toEqual(false);
|
|
|
|
expect(matched).toEqual([]);
|
2014-11-11 20:33:47 -05:00
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(
|
|
|
|
matcher.match(
|
|
|
|
getSelectorFor({attrs: [['someAttr', ''], ['someAttr2', '']]}), selectableCollector))
|
2015-05-26 12:25:39 -04:00
|
|
|
.toEqual(true);
|
|
|
|
expect(matched).toEqual([s1[0], 1, s2[0], 2]);
|
2015-06-11 14:35:02 -04:00
|
|
|
|
|
|
|
reset();
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(matcher.match(
|
2016-12-27 18:23:49 -05:00
|
|
|
getSelectorFor({attrs: [['someAttr', 'someValue'], ['someAttr2', '']]}),
|
|
|
|
selectableCollector))
|
2015-06-11 14:35:02 -04:00
|
|
|
.toEqual(true);
|
|
|
|
expect(matched).toEqual([s1[0], 1, s2[0], 2]);
|
|
|
|
|
|
|
|
reset();
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(matcher.match(
|
2016-12-27 18:23:49 -05:00
|
|
|
getSelectorFor({attrs: [['someAttr2', ''], ['someAttr', 'someValue']]}),
|
|
|
|
selectableCollector))
|
2015-06-11 14:35:02 -04:00
|
|
|
.toEqual(true);
|
|
|
|
expect(matched).toEqual([s1[0], 1, s2[0], 2]);
|
|
|
|
|
|
|
|
reset();
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(matcher.match(
|
2016-12-27 18:23:49 -05:00
|
|
|
getSelectorFor({attrs: [['someAttr2', 'someValue'], ['someAttr', '']]}),
|
|
|
|
selectableCollector))
|
2015-06-11 14:35:02 -04:00
|
|
|
.toEqual(true);
|
|
|
|
expect(matched).toEqual([s1[0], 1, s2[0], 2]);
|
2014-10-28 17:46:55 -04:00
|
|
|
});
|
|
|
|
|
2016-12-09 17:39:18 -05:00
|
|
|
it('should support "." in attribute names', () => {
|
|
|
|
matcher.addSelectables(s1 = CssSelector.parse('[foo.bar]'), 1);
|
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(getSelectorFor({attrs: [['barfoo', '']]}), selectableCollector))
|
|
|
|
.toEqual(false);
|
2016-12-09 17:39:18 -05:00
|
|
|
expect(matched).toEqual([]);
|
|
|
|
|
|
|
|
reset();
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(getSelectorFor({attrs: [['foo.bar', '']]}), selectableCollector))
|
|
|
|
.toEqual(true);
|
2016-12-09 17:39:18 -05:00
|
|
|
expect(matched).toEqual([s1[0], 1]);
|
|
|
|
});
|
|
|
|
|
2014-11-11 20:33:47 -05:00
|
|
|
it('should select by attr name only once if the value is from the DOM', () => {
|
2015-03-19 12:01:42 -04:00
|
|
|
matcher.addSelectables(s1 = CssSelector.parse('[some-decor]'), 1);
|
2014-11-11 20:33:47 -05:00
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const elementSelector = new CssSelector();
|
2016-09-07 18:38:44 -04:00
|
|
|
const element = el('<div attr></div>');
|
2017-03-24 12:59:58 -04:00
|
|
|
const empty = getDOM().getAttribute(element, 'attr') !;
|
2014-11-11 20:33:47 -05:00
|
|
|
elementSelector.addAttribute('some-decor', empty);
|
|
|
|
matcher.match(elementSelector, selectableCollector);
|
2015-05-26 12:25:39 -04:00
|
|
|
expect(matched).toEqual([s1[0], 1]);
|
2014-11-11 20:33:47 -05:00
|
|
|
});
|
|
|
|
|
2015-11-23 19:02:19 -05:00
|
|
|
it('should select by attr name case sensitive and value case insensitive', () => {
|
2015-03-19 12:01:42 -04:00
|
|
|
matcher.addSelectables(s1 = CssSelector.parse('[someAttr=someValue]'), 1);
|
2014-10-28 17:46:55 -04:00
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(
|
|
|
|
getSelectorFor({attrs: [['SOMEATTR', 'SOMEOTHERATTR']]}), selectableCollector))
|
2015-05-26 12:25:39 -04:00
|
|
|
.toEqual(false);
|
2014-10-28 17:46:55 -04:00
|
|
|
expect(matched).toEqual([]);
|
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(
|
|
|
|
matcher.match(getSelectorFor({attrs: [['SOMEATTR', 'SOMEVALUE']]}), selectableCollector))
|
2015-11-23 19:02:19 -05:00
|
|
|
.toEqual(false);
|
|
|
|
expect(matched).toEqual([]);
|
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(
|
|
|
|
matcher.match(getSelectorFor({attrs: [['someAttr', 'SOMEVALUE']]}), selectableCollector))
|
2015-05-26 12:25:39 -04:00
|
|
|
.toEqual(true);
|
|
|
|
expect(matched).toEqual([s1[0], 1]);
|
2014-10-28 17:46:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should select by element name, class name and attribute name with value', () => {
|
2015-03-19 12:01:42 -04:00
|
|
|
matcher.addSelectables(s1 = CssSelector.parse('someTag.someClass[someAttr=someValue]'), 1);
|
2014-10-28 17:46:55 -04:00
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(
|
|
|
|
matcher.match(
|
|
|
|
getSelectorFor(
|
|
|
|
{tag: 'someOtherTag', classes: 'someOtherClass', attrs: [['someOtherAttr', '']]}),
|
|
|
|
selectableCollector))
|
2015-05-26 12:25:39 -04:00
|
|
|
.toEqual(false);
|
2014-10-28 17:46:55 -04:00
|
|
|
expect(matched).toEqual([]);
|
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(
|
|
|
|
getSelectorFor(
|
|
|
|
{tag: 'someTag', classes: 'someOtherClass', attrs: [['someOtherAttr', '']]}),
|
|
|
|
selectableCollector))
|
2015-05-26 12:25:39 -04:00
|
|
|
.toEqual(false);
|
2014-10-28 17:46:55 -04:00
|
|
|
expect(matched).toEqual([]);
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(matcher.match(
|
2016-12-27 18:23:49 -05:00
|
|
|
getSelectorFor(
|
|
|
|
{tag: 'someTag', classes: 'someClass', attrs: [['someOtherAttr', '']]}),
|
|
|
|
selectableCollector))
|
2015-05-26 12:25:39 -04:00
|
|
|
.toEqual(false);
|
2014-10-28 17:46:55 -04:00
|
|
|
expect(matched).toEqual([]);
|
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(
|
|
|
|
getSelectorFor({tag: 'someTag', classes: 'someClass', attrs: [['someAttr', '']]}),
|
|
|
|
selectableCollector))
|
2015-05-26 12:25:39 -04:00
|
|
|
.toEqual(false);
|
2014-10-28 17:46:55 -04:00
|
|
|
expect(matched).toEqual([]);
|
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(
|
|
|
|
getSelectorFor(
|
|
|
|
{tag: 'someTag', classes: 'someClass', attrs: [['someAttr', 'someValue']]}),
|
|
|
|
selectableCollector))
|
2015-05-26 12:25:39 -04:00
|
|
|
.toEqual(true);
|
|
|
|
expect(matched).toEqual([s1[0], 1]);
|
2014-10-28 17:46:55 -04:00
|
|
|
});
|
|
|
|
|
2015-03-19 18:38:48 -04:00
|
|
|
it('should select by many attributes and independent of the value', () => {
|
|
|
|
matcher.addSelectables(s1 = CssSelector.parse('input[type=text][control]'), 1);
|
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const cssSelector = new CssSelector();
|
2015-03-19 18:38:48 -04:00
|
|
|
cssSelector.setElement('input');
|
|
|
|
cssSelector.addAttribute('type', 'text');
|
|
|
|
cssSelector.addAttribute('control', 'one');
|
|
|
|
|
|
|
|
expect(matcher.match(cssSelector, selectableCollector)).toEqual(true);
|
|
|
|
expect(matched).toEqual([s1[0], 1]);
|
|
|
|
});
|
|
|
|
|
2014-10-28 17:46:55 -04:00
|
|
|
it('should select independent of the order in the css selector', () => {
|
2015-03-19 12:01:42 -04:00
|
|
|
matcher.addSelectables(s1 = CssSelector.parse('[someAttr].someClass'), 1);
|
|
|
|
matcher.addSelectables(s2 = CssSelector.parse('.someClass[someAttr]'), 2);
|
|
|
|
matcher.addSelectables(s3 = CssSelector.parse('.class1.class2'), 3);
|
|
|
|
matcher.addSelectables(s4 = CssSelector.parse('.class2.class1'), 4);
|
2014-10-28 17:46:55 -04:00
|
|
|
|
2015-05-26 12:25:39 -04:00
|
|
|
expect(matcher.match(CssSelector.parse('[someAttr].someClass')[0], selectableCollector))
|
|
|
|
.toEqual(true);
|
|
|
|
expect(matched).toEqual([s1[0], 1, s2[0], 2]);
|
2014-10-28 17:46:55 -04:00
|
|
|
|
|
|
|
reset();
|
2015-05-26 12:25:39 -04:00
|
|
|
expect(matcher.match(CssSelector.parse('.someClass[someAttr]')[0], selectableCollector))
|
|
|
|
.toEqual(true);
|
|
|
|
expect(matched).toEqual([s1[0], 1, s2[0], 2]);
|
2014-10-28 17:46:55 -04:00
|
|
|
|
|
|
|
reset();
|
2015-05-26 12:25:39 -04:00
|
|
|
expect(matcher.match(CssSelector.parse('.class1.class2')[0], selectableCollector))
|
|
|
|
.toEqual(true);
|
|
|
|
expect(matched).toEqual([s3[0], 3, s4[0], 4]);
|
2014-10-28 17:46:55 -04:00
|
|
|
|
|
|
|
reset();
|
2015-05-26 12:25:39 -04:00
|
|
|
expect(matcher.match(CssSelector.parse('.class2.class1')[0], selectableCollector))
|
|
|
|
.toEqual(true);
|
|
|
|
expect(matched).toEqual([s4[0], 4, s3[0], 3]);
|
2014-10-28 17:46:55 -04:00
|
|
|
});
|
2015-03-12 04:44:49 -04:00
|
|
|
|
|
|
|
it('should not select with a matching :not selector', () => {
|
2015-03-19 12:01:42 -04:00
|
|
|
matcher.addSelectables(CssSelector.parse('p:not(.someClass)'), 1);
|
|
|
|
matcher.addSelectables(CssSelector.parse('p:not([someAttr])'), 2);
|
|
|
|
matcher.addSelectables(CssSelector.parse(':not(.someClass)'), 3);
|
|
|
|
matcher.addSelectables(CssSelector.parse(':not(p)'), 4);
|
|
|
|
matcher.addSelectables(CssSelector.parse(':not(p[someAttr])'), 5);
|
2015-03-12 04:44:49 -04:00
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(
|
|
|
|
getSelectorFor({tag: 'p', classes: 'someClass', attrs: [['someAttr', '']]}),
|
|
|
|
selectableCollector))
|
2015-05-26 12:25:39 -04:00
|
|
|
.toEqual(false);
|
2015-03-12 04:44:49 -04:00
|
|
|
expect(matched).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should select with a non matching :not selector', () => {
|
2015-03-19 12:01:42 -04:00
|
|
|
matcher.addSelectables(s1 = CssSelector.parse('p:not(.someClass)'), 1);
|
|
|
|
matcher.addSelectables(s2 = CssSelector.parse('p:not(.someOtherClass[someAttr])'), 2);
|
|
|
|
matcher.addSelectables(s3 = CssSelector.parse(':not(.someClass)'), 3);
|
|
|
|
matcher.addSelectables(s4 = CssSelector.parse(':not(.someOtherClass[someAttr])'), 4);
|
2015-03-12 04:44:49 -04:00
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(
|
|
|
|
matcher.match(
|
|
|
|
getSelectorFor({tag: 'p', attrs: [['someOtherAttr', '']], classes: 'someOtherClass'}),
|
|
|
|
selectableCollector))
|
2015-05-26 12:25:39 -04:00
|
|
|
.toEqual(true);
|
|
|
|
expect(matched).toEqual([s1[0], 1, s2[0], 2, s3[0], 3, s4[0], 4]);
|
2015-03-19 12:01:42 -04:00
|
|
|
});
|
|
|
|
|
2016-12-09 13:45:48 -05:00
|
|
|
it('should match * with :not selector', () => {
|
|
|
|
matcher.addSelectables(CssSelector.parse(':not([a])'), 1);
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(getSelectorFor({tag: 'div'}), () => {})).toEqual(true);
|
2016-12-09 13:45:48 -05:00
|
|
|
});
|
|
|
|
|
2015-06-01 17:24:19 -04:00
|
|
|
it('should match with multiple :not selectors', () => {
|
|
|
|
matcher.addSelectables(s1 = CssSelector.parse('div:not([a]):not([b])'), 1);
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(getSelectorFor({tag: 'div', attrs: [['a', '']]}), selectableCollector))
|
|
|
|
.toBe(false);
|
|
|
|
expect(matcher.match(getSelectorFor({tag: 'div', attrs: [['b', '']]}), selectableCollector))
|
|
|
|
.toBe(false);
|
|
|
|
expect(matcher.match(getSelectorFor({tag: 'div', attrs: [['c', '']]}), selectableCollector))
|
|
|
|
.toBe(true);
|
2015-06-01 17:24:19 -04:00
|
|
|
});
|
|
|
|
|
2015-03-19 12:01:42 -04:00
|
|
|
it('should select with one match in a list', () => {
|
2015-05-26 12:25:39 -04:00
|
|
|
matcher.addSelectables(s1 = CssSelector.parse('input[type=text], textbox'), 1);
|
2015-03-19 12:01:42 -04:00
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(getSelectorFor({tag: 'textbox'}), selectableCollector)).toEqual(true);
|
2015-05-26 12:25:39 -04:00
|
|
|
expect(matched).toEqual([s1[1], 1]);
|
2015-03-19 12:01:42 -04:00
|
|
|
|
2015-05-26 12:25:39 -04:00
|
|
|
reset();
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(matcher.match(
|
|
|
|
getSelectorFor({tag: 'input', attrs: [['type', 'text']]}), selectableCollector))
|
2015-05-26 12:25:39 -04:00
|
|
|
.toEqual(true);
|
|
|
|
expect(matched).toEqual([s1[0], 1]);
|
2015-03-19 12:01:42 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not select twice with two matches in a list', () => {
|
2015-05-26 12:25:39 -04:00
|
|
|
matcher.addSelectables(s1 = CssSelector.parse('input, .someClass'), 1);
|
2015-03-19 12:01:42 -04:00
|
|
|
|
2016-12-27 18:23:49 -05:00
|
|
|
expect(
|
|
|
|
matcher.match(getSelectorFor({tag: 'input', classes: 'someclass'}), selectableCollector))
|
2015-05-26 12:25:39 -04:00
|
|
|
.toEqual(true);
|
|
|
|
expect(matched.length).toEqual(2);
|
|
|
|
expect(matched).toEqual([s1[0], 1]);
|
2015-03-12 04:44:49 -04:00
|
|
|
});
|
2014-10-28 17:46:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('CssSelector.parse', () => {
|
|
|
|
it('should detect element names', () => {
|
2016-09-07 18:38:44 -04:00
|
|
|
const cssSelector = CssSelector.parse('sometag')[0];
|
2014-10-28 17:46:55 -04:00
|
|
|
expect(cssSelector.element).toEqual('sometag');
|
|
|
|
expect(cssSelector.toString()).toEqual('sometag');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect class names', () => {
|
2016-09-07 18:38:44 -04:00
|
|
|
const cssSelector = CssSelector.parse('.someClass')[0];
|
2014-10-28 17:46:55 -04:00
|
|
|
expect(cssSelector.classNames).toEqual(['someclass']);
|
|
|
|
|
|
|
|
expect(cssSelector.toString()).toEqual('.someclass');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect attr names', () => {
|
2016-09-07 18:38:44 -04:00
|
|
|
const cssSelector = CssSelector.parse('[attrname]')[0];
|
2014-11-11 20:33:47 -05:00
|
|
|
expect(cssSelector.attrs).toEqual(['attrname', '']);
|
|
|
|
|
|
|
|
expect(cssSelector.toString()).toEqual('[attrname]');
|
2014-10-28 17:46:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect attr values', () => {
|
2016-09-07 18:38:44 -04:00
|
|
|
const cssSelector = CssSelector.parse('[attrname=attrvalue]')[0];
|
2014-11-11 20:33:47 -05:00
|
|
|
expect(cssSelector.attrs).toEqual(['attrname', 'attrvalue']);
|
2014-10-28 17:46:55 -04:00
|
|
|
expect(cssSelector.toString()).toEqual('[attrname=attrvalue]');
|
|
|
|
});
|
|
|
|
|
2017-03-01 14:19:22 -05:00
|
|
|
it('should detect attr values with double quotes', () => {
|
|
|
|
const cssSelector = CssSelector.parse('[attrname="attrvalue"]')[0];
|
|
|
|
expect(cssSelector.attrs).toEqual(['attrname', 'attrvalue']);
|
|
|
|
expect(cssSelector.toString()).toEqual('[attrname=attrvalue]');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect attr values with single quotes', () => {
|
|
|
|
const cssSelector = CssSelector.parse('[attrname=\'attrvalue\']')[0];
|
|
|
|
expect(cssSelector.attrs).toEqual(['attrname', 'attrvalue']);
|
|
|
|
expect(cssSelector.toString()).toEqual('[attrname=attrvalue]');
|
|
|
|
});
|
|
|
|
|
2014-10-28 17:46:55 -04:00
|
|
|
it('should detect multiple parts', () => {
|
2016-09-07 18:38:44 -04:00
|
|
|
const cssSelector = CssSelector.parse('sometag[attrname=attrvalue].someclass')[0];
|
2014-10-28 17:46:55 -04:00
|
|
|
expect(cssSelector.element).toEqual('sometag');
|
2014-11-11 20:33:47 -05:00
|
|
|
expect(cssSelector.attrs).toEqual(['attrname', 'attrvalue']);
|
2014-10-28 17:46:55 -04:00
|
|
|
expect(cssSelector.classNames).toEqual(['someclass']);
|
|
|
|
|
|
|
|
expect(cssSelector.toString()).toEqual('sometag.someclass[attrname=attrvalue]');
|
|
|
|
});
|
2015-03-12 04:44:49 -04:00
|
|
|
|
2015-03-19 18:38:48 -04:00
|
|
|
it('should detect multiple attributes', () => {
|
2016-09-07 18:38:44 -04:00
|
|
|
const cssSelector = CssSelector.parse('input[type=text][control]')[0];
|
2015-03-19 18:38:48 -04:00
|
|
|
expect(cssSelector.element).toEqual('input');
|
|
|
|
expect(cssSelector.attrs).toEqual(['type', 'text', 'control', '']);
|
|
|
|
|
|
|
|
expect(cssSelector.toString()).toEqual('input[type=text][control]');
|
|
|
|
});
|
|
|
|
|
2015-03-12 04:44:49 -04:00
|
|
|
it('should detect :not', () => {
|
2016-09-07 18:38:44 -04:00
|
|
|
const cssSelector = CssSelector.parse('sometag:not([attrname=attrvalue].someclass)')[0];
|
2015-03-12 04:44:49 -04:00
|
|
|
expect(cssSelector.element).toEqual('sometag');
|
|
|
|
expect(cssSelector.attrs.length).toEqual(0);
|
|
|
|
expect(cssSelector.classNames.length).toEqual(0);
|
|
|
|
|
2016-09-07 18:38:44 -04:00
|
|
|
const notSelector = cssSelector.notSelectors[0];
|
2015-03-12 04:44:49 -04:00
|
|
|
expect(notSelector.element).toEqual(null);
|
|
|
|
expect(notSelector.attrs).toEqual(['attrname', 'attrvalue']);
|
|
|
|
expect(notSelector.classNames).toEqual(['someclass']);
|
|
|
|
|
|
|
|
expect(cssSelector.toString()).toEqual('sometag:not(.someclass[attrname=attrvalue])');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect :not without truthy', () => {
|
2016-09-07 18:38:44 -04:00
|
|
|
const cssSelector = CssSelector.parse(':not([attrname=attrvalue].someclass)')[0];
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(cssSelector.element).toEqual('*');
|
2015-03-12 04:44:49 -04:00
|
|
|
|
2016-09-07 18:38:44 -04:00
|
|
|
const notSelector = cssSelector.notSelectors[0];
|
2015-03-12 04:44:49 -04:00
|
|
|
expect(notSelector.attrs).toEqual(['attrname', 'attrvalue']);
|
|
|
|
expect(notSelector.classNames).toEqual(['someclass']);
|
|
|
|
|
|
|
|
expect(cssSelector.toString()).toEqual('*:not(.someclass[attrname=attrvalue])');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw when nested :not', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(() => {
|
|
|
|
CssSelector.parse('sometag:not(:not([attrname=attrvalue].someclass))')[0];
|
|
|
|
}).toThrowError('Nesting :not is not allowed in a selector');
|
2015-03-12 04:44:49 -04:00
|
|
|
});
|
2015-03-19 12:01:42 -04:00
|
|
|
|
2015-06-01 17:24:19 -04:00
|
|
|
it('should throw when multiple selectors in :not', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(() => {
|
|
|
|
CssSelector.parse('sometag:not(a,b)');
|
|
|
|
}).toThrowError('Multiple selectors in :not are not supported');
|
2015-06-01 17:24:19 -04:00
|
|
|
});
|
|
|
|
|
2015-03-19 12:01:42 -04:00
|
|
|
it('should detect lists of selectors', () => {
|
2016-09-07 18:38:44 -04:00
|
|
|
const cssSelectors = CssSelector.parse('.someclass,[attrname=attrvalue], sometag');
|
2015-03-19 12:01:42 -04:00
|
|
|
expect(cssSelectors.length).toEqual(3);
|
|
|
|
|
|
|
|
expect(cssSelectors[0].classNames).toEqual(['someclass']);
|
|
|
|
expect(cssSelectors[1].attrs).toEqual(['attrname', 'attrvalue']);
|
|
|
|
expect(cssSelectors[2].element).toEqual('sometag');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect lists of selectors with :not', () => {
|
2016-09-07 18:38:44 -04:00
|
|
|
const cssSelectors =
|
2015-05-26 12:25:39 -04:00
|
|
|
CssSelector.parse('input[type=text], :not(textarea), textbox:not(.special)');
|
2015-03-19 12:01:42 -04:00
|
|
|
expect(cssSelectors.length).toEqual(3);
|
|
|
|
|
|
|
|
expect(cssSelectors[0].element).toEqual('input');
|
|
|
|
expect(cssSelectors[0].attrs).toEqual(['type', 'text']);
|
|
|
|
|
|
|
|
expect(cssSelectors[1].element).toEqual('*');
|
2015-06-01 17:24:19 -04:00
|
|
|
expect(cssSelectors[1].notSelectors[0].element).toEqual('textarea');
|
2015-03-19 12:01:42 -04:00
|
|
|
|
|
|
|
expect(cssSelectors[2].element).toEqual('textbox');
|
2015-06-01 17:24:19 -04:00
|
|
|
expect(cssSelectors[2].notSelectors[0].classNames).toEqual(['special']);
|
2015-03-19 12:01:42 -04:00
|
|
|
});
|
2014-10-28 17:46:55 -04:00
|
|
|
});
|
2015-07-28 14:38:40 -04:00
|
|
|
|
|
|
|
describe('CssSelector.getMatchingElementTemplate', () => {
|
2015-11-23 19:02:19 -05:00
|
|
|
it('should create an element with a tagName, classes, and attributes with the correct casing',
|
|
|
|
() => {
|
2016-09-07 18:38:44 -04:00
|
|
|
const selector = CssSelector.parse('Blink.neon.hotpink[Sweet][Dismissable=false]')[0];
|
|
|
|
const template = selector.getMatchingElementTemplate();
|
2015-07-28 14:38:40 -04:00
|
|
|
|
2015-11-23 19:02:19 -05:00
|
|
|
expect(template).toEqual('<Blink class="neon hotpink" Sweet Dismissable="false"></Blink>');
|
|
|
|
});
|
2015-07-28 14:38:40 -04:00
|
|
|
|
|
|
|
it('should create an element without a tag name', () => {
|
2016-09-07 18:38:44 -04:00
|
|
|
const selector = CssSelector.parse('[fancy]')[0];
|
|
|
|
const template = selector.getMatchingElementTemplate();
|
2015-07-28 14:38:40 -04:00
|
|
|
|
|
|
|
expect(template).toEqual('<div fancy></div>');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should ignore :not selectors', () => {
|
2016-09-07 18:38:44 -04:00
|
|
|
const selector = CssSelector.parse('grape:not(.red)')[0];
|
|
|
|
const template = selector.getMatchingElementTemplate();
|
2015-07-28 14:38:40 -04:00
|
|
|
|
|
|
|
expect(template).toEqual('<grape></grape>');
|
|
|
|
});
|
2016-09-07 18:40:00 -04:00
|
|
|
|
|
|
|
it('should support void tags', () => {
|
|
|
|
const selector = CssSelector.parse('input[fancy]')[0];
|
|
|
|
const template = selector.getMatchingElementTemplate();
|
|
|
|
expect(template).toEqual('<input fancy/>');
|
|
|
|
});
|
2015-07-28 14:38:40 -04:00
|
|
|
});
|
2015-06-01 17:24:19 -04:00
|
|
|
}
|
2016-12-27 18:23:49 -05:00
|
|
|
|
|
|
|
function getSelectorFor(
|
|
|
|
{tag = '', attrs = [], classes = ''}: {tag?: string, attrs?: any[], classes?: string} = {}):
|
|
|
|
CssSelector {
|
|
|
|
const selector = new CssSelector();
|
|
|
|
selector.setElement(tag);
|
|
|
|
|
|
|
|
attrs.forEach(nameValue => { selector.addAttribute(nameValue[0], nameValue[1]); });
|
|
|
|
|
|
|
|
classes.trim().split(/\s+/g).forEach(cName => { selector.addClassName(cName); });
|
|
|
|
|
|
|
|
return selector;
|
2017-03-02 15:12:46 -05:00
|
|
|
}
|