Example #1
0
    it('should select by attr name case sensitive and value case insensitive', () => {
      matcher.addSelectables(s1 = CssSelector.parse('[someAttr=someValue]'), 1);

      expect(matcher.match(CssSelector.parse('[SOMEATTR=SOMEOTHERATTR]')[0], selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      expect(matcher.match(CssSelector.parse('[SOMEATTR=SOMEVALUE]')[0], selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      expect(matcher.match(CssSelector.parse('[someAttr=SOMEVALUE]')[0], selectableCollector))
          .toEqual(true);
      expect(matched).toEqual([s1[0], 1]);
    });
Example #2
0
 function parse() {
   var result = [];
   for (var i = 0; i < count; i++) {
     result.push(CssSelector.parse(fixedSelectorStrings[i]));
   }
   return result;
 }
Example #3
0
    it('should select by class name case insensitive', () => {
      matcher.addSelectables(s1 = CssSelector.parse('.someClass'), 1);
      matcher.addSelectables(s2 = CssSelector.parse('.someClass.class2'), 2);

      expect(matcher.match(CssSelector.parse('.SOMEOTHERCLASS')[0], selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      expect(matcher.match(CssSelector.parse('.SOMECLASS')[0], selectableCollector)).toEqual(true);
      expect(matched).toEqual([s1[0], 1]);

      reset();
      expect(matcher.match(CssSelector.parse('.someClass.class2')[0], selectableCollector))
          .toEqual(true);
      expect(matched).toEqual([s1[0], 1, s2[0], 2]);
    });
Example #4
0
    it('should detect multiple attributes', () => {
      var cssSelector = CssSelector.parse('input[type=text][control]')[0];
      expect(cssSelector.element).toEqual('input');
      expect(cssSelector.attrs).toEqual(['type', 'text', 'control', '']);

      expect(cssSelector.toString()).toEqual('input[type=text][control]');
    });
Example #5
0
    it('should detect lists of selectors', () => {
      var cssSelectors = CssSelector.parse('.someclass,[attrname=attrvalue], sometag');
      expect(cssSelectors.length).toEqual(3);

      expect(cssSelectors[0].classNames).toEqual(['someclass']);
      expect(cssSelectors[1].attrs).toEqual(['attrname', 'attrvalue']);
      expect(cssSelectors[2].element).toEqual('sometag');
    });
Example #6
0
    it('should detect multiple parts', () => {
      var cssSelector = CssSelector.parse('sometag[attrname=attrvalue].someclass')[0];
      expect(cssSelector.element).toEqual('sometag');
      expect(cssSelector.attrs).toEqual(['attrname', 'attrvalue']);
      expect(cssSelector.classNames).toEqual(['someclass']);

      expect(cssSelector.toString()).toEqual('sometag.someclass[attrname=attrvalue]');
    });
Example #7
0
    it('should not select twice with two matches in a list', () => {
      matcher.addSelectables(s1 = CssSelector.parse('input, .someClass'), 1);

      expect(
          matcher.match(getSelectorFor({tag: 'input', classes: 'someclass'}), selectableCollector))
          .toEqual(true);
      expect(matched.length).toEqual(2);
      expect(matched).toEqual([s1[0], 1]);
    });
Example #8
0
 it('should match with multiple :not selectors', () => {
   matcher.addSelectables(s1 = CssSelector.parse('div:not([a]):not([b])'), 1);
   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);
 });
Example #9
0
    it('should select by attr name case sensitive independent of the value', () => {
      matcher.addSelectables(s1 = CssSelector.parse('[someAttr]'), 1);
      matcher.addSelectables(s2 = CssSelector.parse('[someAttr][someAttr2]'), 2);

      expect(matcher.match(getSelectorFor({attrs: [['SOMEOTHERATTR', '']]}), selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      expect(matcher.match(getSelectorFor({attrs: [['SOMEATTR', '']]}), selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      expect(
          matcher.match(getSelectorFor({attrs: [['SOMEATTR', 'someValue']]}), selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      expect(
          matcher.match(
              getSelectorFor({attrs: [['someAttr', ''], ['someAttr2', '']]}), selectableCollector))
          .toEqual(true);
      expect(matched).toEqual([s1[0], 1, s2[0], 2]);

      reset();
      expect(matcher.match(
                 getSelectorFor({attrs: [['someAttr', 'someValue'], ['someAttr2', '']]}),
                 selectableCollector))
          .toEqual(true);
      expect(matched).toEqual([s1[0], 1, s2[0], 2]);

      reset();
      expect(matcher.match(
                 getSelectorFor({attrs: [['someAttr2', ''], ['someAttr', 'someValue']]}),
                 selectableCollector))
          .toEqual(true);
      expect(matched).toEqual([s1[0], 1, s2[0], 2]);

      reset();
      expect(matcher.match(
                 getSelectorFor({attrs: [['someAttr2', 'someValue'], ['someAttr', '']]}),
                 selectableCollector))
          .toEqual(true);
      expect(matched).toEqual([s1[0], 1, s2[0], 2]);
    });
Example #10
0
    it('should detect :not without truthy', () => {
      var cssSelector = CssSelector.parse(':not([attrname=attrvalue].someclass)')[0];
      expect(cssSelector.element).toEqual('*');

      var notSelector = cssSelector.notSelectors[0];
      expect(notSelector.attrs).toEqual(['attrname', 'attrvalue']);
      expect(notSelector.classNames).toEqual(['someclass']);

      expect(cssSelector.toString()).toEqual('*:not(.someclass[attrname=attrvalue])');
    });
Example #11
0
    it('should select with one match in a list', () => {
      matcher.addSelectables(s1 = CssSelector.parse('input[type=text], textbox'), 1);

      expect(matcher.match(getSelectorFor({tag: 'textbox'}), selectableCollector)).toEqual(true);
      expect(matched).toEqual([s1[1], 1]);

      reset();
      expect(matcher.match(
                 getSelectorFor({tag: 'input', attrs: [['type', 'text']]}), selectableCollector))
          .toEqual(true);
      expect(matched).toEqual([s1[0], 1]);
    });
Example #12
0
    it('should support "." in attribute names', () => {
      matcher.addSelectables(s1 = CssSelector.parse('[foo.bar]'), 1);

      expect(matcher.match(getSelectorFor({attrs: [['barfoo', '']]}), selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      reset();
      expect(matcher.match(getSelectorFor({attrs: [['foo.bar', '']]}), selectableCollector))
          .toEqual(true);
      expect(matched).toEqual([s1[0], 1]);
    });
Example #13
0
    it('should select by element name, class name and attribute name with value', () => {
      matcher.addSelectables(s1 = CssSelector.parse('someTag.someClass[someAttr=someValue]'), 1);

      expect(matcher.match(
                 CssSelector.parse('someOtherTag.someOtherClass[someOtherAttr]')[0],
                 selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      expect(
          matcher.match(
              CssSelector.parse('someTag.someOtherClass[someOtherAttr]')[0], selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      expect(matcher.match(
                 CssSelector.parse('someTag.someClass[someOtherAttr]')[0], selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      expect(
          matcher.match(CssSelector.parse('someTag.someClass[someAttr]')[0], selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      expect(
          matcher.match(
              CssSelector.parse('someTag.someClass[someAttr=someValue]')[0], selectableCollector))
          .toEqual(true);
      expect(matched).toEqual([s1[0], 1]);
    });
Example #14
0
    it('should select by element name case sensitive', () => {
      matcher.addSelectables(s1 = CssSelector.parse('someTag'), 1);

      expect(matcher.match(getSelectorFor({tag: 'SOMEOTHERTAG'}), selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      expect(matcher.match(getSelectorFor({tag: 'SOMETAG'}), selectableCollector)).toEqual(false);
      expect(matched).toEqual([]);

      expect(matcher.match(getSelectorFor({tag: 'someTag'}), selectableCollector)).toEqual(true);
      expect(matched).toEqual([s1[0], 1]);
    });
Example #15
0
    it('should detect :not', () => {
      var cssSelector = CssSelector.parse('sometag:not([attrname=attrvalue].someclass)')[0];
      expect(cssSelector.element).toEqual('sometag');
      expect(cssSelector.attrs.length).toEqual(0);
      expect(cssSelector.classNames.length).toEqual(0);

      var notSelector = cssSelector.notSelectors[0];
      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])');
    });
Example #16
0
    it('should detect lists of selectors with :not', () => {
      var cssSelectors =
          CssSelector.parse('input[type=text], :not(textarea), textbox:not(.special)');
      expect(cssSelectors.length).toEqual(3);

      expect(cssSelectors[0].element).toEqual('input');
      expect(cssSelectors[0].attrs).toEqual(['type', 'text']);

      expect(cssSelectors[1].element).toEqual('*');
      expect(cssSelectors[1].notSelectors[0].element).toEqual('textarea');

      expect(cssSelectors[2].element).toEqual('textbox');
      expect(cssSelectors[2].notSelectors[0].classNames).toEqual(['special']);
    });
Example #17
0
    it('should not select with a matching :not selector', () => {
      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);

      expect(matcher.match(CssSelector.parse('p.someClass[someAttr]')[0], selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);
    });
Example #18
0
export function main() {
  BrowserDomAdapter.makeCurrent();
  var count = getIntParameter('selectors');

  var fixedMatcher;
  var fixedSelectorStrings = [];
  var fixedSelectors = [];
  for (var i = 0; i < count; i++) {
    fixedSelectorStrings.push(randomSelector());
  }
  for (var i = 0; i < count; i++) {
    fixedSelectors.push(CssSelector.parse(fixedSelectorStrings[i]));
  }
  fixedMatcher = new SelectorMatcher();
  for (var i = 0; i < count; i++) {
    fixedMatcher.addSelectables(fixedSelectors[i], i);
  }

  function parse() {
    var result = [];
    for (var i = 0; i < count; i++) {
      result.push(CssSelector.parse(fixedSelectorStrings[i]));
    }
    return result;
  }

  function addSelectable() {
    var matcher = new SelectorMatcher();
    for (var i = 0; i < count; i++) {
      matcher.addSelectables(fixedSelectors[i], i);
    }
    return matcher;
  }

  function match() {
    var matchCount = 0;
    for (var i = 0; i < count; i++) {
      fixedMatcher.match(fixedSelectors[i][0], (selector, selected) => { matchCount += selected; });
    }
    return matchCount;
  }

  bindAction('#parse', parse);
  bindAction('#addSelectable', addSelectable);
  bindAction('#match', match);
}
Example #19
0
    it('should select by attr name case sensitive independent of the value', () => {
      matcher.addSelectables(s1 = CssSelector.parse('[someAttr]'), 1);
      matcher.addSelectables(s2 = CssSelector.parse('[someAttr][someAttr2]'), 2);

      expect(matcher.match(CssSelector.parse('[SOMEOTHERATTR]')[0], selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      expect(matcher.match(CssSelector.parse('[SOMEATTR]')[0], selectableCollector)).toEqual(false);
      expect(matched).toEqual([]);

      expect(matcher.match(CssSelector.parse('[SOMEATTR=someValue]')[0], selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      expect(matcher.match(CssSelector.parse('[someAttr][someAttr2]')[0], selectableCollector))
          .toEqual(true);
      expect(matched).toEqual([s1[0], 1, s2[0], 2]);

      reset();
      expect(matcher.match(
                 CssSelector.parse('[someAttr=someValue][someAttr2]')[0], selectableCollector))
          .toEqual(true);
      expect(matched).toEqual([s1[0], 1, s2[0], 2]);

      reset();
      expect(matcher.match(
                 CssSelector.parse('[someAttr2][someAttr=someValue]')[0], selectableCollector))
          .toEqual(true);
      expect(matched).toEqual([s1[0], 1, s2[0], 2]);

      reset();
      expect(matcher.match(
                 CssSelector.parse('[someAttr2=someValue][someAttr]')[0], selectableCollector))
          .toEqual(true);
      expect(matched).toEqual([s1[0], 1, s2[0], 2]);
    });
Example #20
0
    it('should select by attr name case sensitive and value case insensitive', () => {
      matcher.addSelectables(s1 = CssSelector.parse('[someAttr=someValue]'), 1);

      expect(matcher.match(
                 getSelectorFor({attrs: [['SOMEATTR', 'SOMEOTHERATTR']]}), selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      expect(
          matcher.match(getSelectorFor({attrs: [['SOMEATTR', 'SOMEVALUE']]}), selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      expect(
          matcher.match(getSelectorFor({attrs: [['someAttr', 'SOMEVALUE']]}), selectableCollector))
          .toEqual(true);
      expect(matched).toEqual([s1[0], 1]);
    });
Example #21
0
    it('should select by element name, class name and attribute name with value', () => {
      matcher.addSelectables(s1 = CssSelector.parse('someTag.someClass[someAttr=someValue]'), 1);

      expect(
          matcher.match(
              getSelectorFor(
                  {tag: 'someOtherTag', classes: 'someOtherClass', attrs: [['someOtherAttr', '']]}),
              selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      expect(matcher.match(
                 getSelectorFor(
                     {tag: 'someTag', classes: 'someOtherClass', attrs: [['someOtherAttr', '']]}),
                 selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      expect(matcher.match(
                 getSelectorFor(
                     {tag: 'someTag', classes: 'someClass', attrs: [['someOtherAttr', '']]}),
                 selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      expect(matcher.match(
                 getSelectorFor({tag: 'someTag', classes: 'someClass', attrs: [['someAttr', '']]}),
                 selectableCollector))
          .toEqual(false);
      expect(matched).toEqual([]);

      expect(matcher.match(
                 getSelectorFor(
                     {tag: 'someTag', classes: 'someClass', attrs: [['someAttr', 'someValue']]}),
                 selectableCollector))
          .toEqual(true);
      expect(matched).toEqual([s1[0], 1]);
    });
Example #22
0
 it('should detect element names', () => {
   var cssSelector = CssSelector.parse('sometag')[0];
   expect(cssSelector.element).toEqual('sometag');
   expect(cssSelector.toString()).toEqual('sometag');
 });
Example #23
0
 it('should detect attr values', () => {
   var cssSelector = CssSelector.parse('[attrname=attrvalue]')[0];
   expect(cssSelector.attrs).toEqual(['attrname', 'attrvalue']);
   expect(cssSelector.toString()).toEqual('[attrname=attrvalue]');
 });
Example #24
0
    it('should ignore :not selectors', () => {
      let selector = CssSelector.parse('grape:not(.red)')[0];
      let template = selector.getMatchingElementTemplate();

      expect(template).toEqual('<grape></grape>');
    });
Example #25
0
    it('should create an element without a tag name', () => {
      let selector = CssSelector.parse('[fancy]')[0];
      let template = selector.getMatchingElementTemplate();

      expect(template).toEqual('<div fancy></div>');
    });
Example #26
0
       () => {
         let selector = CssSelector.parse('Blink.neon.hotpink[Sweet][Dismissable=false]')[0];
         let template = selector.getMatchingElementTemplate();

         expect(template).toEqual('<Blink class="neon hotpink" Sweet Dismissable="false"></Blink>');
       });
Example #27
0
    it('should detect class names', () => {
      var cssSelector = CssSelector.parse('.someClass')[0];
      expect(cssSelector.classNames).toEqual(['someclass']);

      expect(cssSelector.toString()).toEqual('.someclass');
    });
Example #28
0
 expect(() => {
   CssSelector.parse('sometag:not(a,b)');
 }).toThrowError('Multiple selectors in :not are not supported');
Example #29
0
 expect(() => {
   CssSelector.parse('sometag:not(:not([attrname=attrvalue].someclass))')[0];
 }).toThrowError('Nesting :not is not allowed in a selector');
Example #30
0
 it('should match with multiple :not selectors', () => {
   matcher.addSelectables(s1 = CssSelector.parse('div:not([a]):not([b])'), 1);
   expect(matcher.match(CssSelector.parse('div[a]')[0], selectableCollector)).toBe(false);
   expect(matcher.match(CssSelector.parse('div[b]')[0], selectableCollector)).toBe(false);
   expect(matcher.match(CssSelector.parse('div[c]')[0], selectableCollector)).toBe(true);
 });