Parse5DomAdapter.prototype.elementMatches = function (node, selector, matcher) {
     if (matcher === void 0) { matcher = null; }
     var result = false;
     if (selector && selector.charAt(0) == "#") {
         result = this.getAttribute(node, 'id') == selector.substring(1);
     }
     else if (selector) {
         var result = false;
         if (matcher == null) {
             matcher = new selector_1.SelectorMatcher();
             matcher.addSelectables(selector_1.CssSelector.parse(selector));
         }
         var cssSelector = new selector_1.CssSelector();
         cssSelector.setElement(this.tagName(node));
         if (node.attribs) {
             for (var attrName in node.attribs) {
                 cssSelector.addAttribute(attrName, node.attribs[attrName]);
             }
         }
         var classList = this.classList(node);
         for (var i = 0; i < classList.length; i++) {
             cssSelector.addClassName(classList[i]);
         }
         matcher.match(cssSelector, function (selector, cb) { result = true; });
     }
     return result;
 };
Ejemplo n.º 2
0
    it('should not select twice with two matches in a list', () => {
       matcher.addSelectables(s1 = CssSelector.parse('input, .someClass'), 1);

       expect(matcher.match(CssSelector.parse('input.someclass')[0], selectableCollector)).toEqual(true);
       expect(matched.length).toEqual(2);
       expect(matched).toEqual([s1[0],1]);
    });
Ejemplo n.º 3
0
  process(parent:CompileElement, current:CompileElement, control:CompileControl) {
    var attrs = current.attrs();
    var classList = current.classList();

    var cssSelector = new CssSelector();
    var nodeName = DOM.nodeName(current.element);
    cssSelector.setElement(nodeName);
    for (var i=0; i < classList.length; i++) {
      cssSelector.addClassName(classList[i]);
    }

    MapWrapper.forEach(attrs, (attrValue, attrName) => {
      cssSelector.addAttribute(attrName, attrValue);
    });

    var componentDirective;

    this._selectorMatcher.match(cssSelector, (selector, directiveIndex) => {
      var elementBinder = current.bindElement();
      var directive = this._directives[directiveIndex];
      var directiveBinderBuilder = elementBinder.bindDirective(directiveIndex);
      current.compileChildren = current.compileChildren && directive.compileChildren;
      if (isPresent(directive.properties)) {
        MapWrapper.forEach(directive.properties, (bindConfig, dirProperty) => {
          this._bindDirectiveProperty(dirProperty, bindConfig, current, directiveBinderBuilder);
        });
      }
      if (isPresent(directive.hostListeners)) {
        MapWrapper.forEach(directive.hostListeners, (action, eventName) => {
          this._bindDirectiveEvent(eventName, action, current, directiveBinderBuilder);
        });
      }
      if (isPresent(directive.hostProperties)) {
        MapWrapper.forEach(directive.hostProperties, (hostPropertyName, directivePropertyName) => {
          this._bindHostProperty(hostPropertyName, directivePropertyName, current, directiveBinderBuilder);
        });
      }
      if (isPresent(directive.hostAttributes)) {
        MapWrapper.forEach(directive.hostAttributes, (hostAttrValue, hostAttrName) => {
          if (!DOM.hasAttribute(current.element, hostAttrName)) {
            DOM.setAttribute(current.element, hostAttrName, hostAttrValue);
          }
        });
      }
      if (isPresent(directive.readAttributes)) {
        ListWrapper.forEach(directive.readAttributes, (attrName) => {
          elementBinder.readAttribute(attrName);
        });
      }
      if (directive.type === DirectiveMetadata.COMPONENT_TYPE) {
        if (isPresent(componentDirective)) {
          throw new BaseException(`Only one component directive is allowed per element - check ${current.elementDescription}`);
        }
        componentDirective = directive;
        elementBinder.setComponentId(directive.id);
      }
    });
  }
Ejemplo n.º 4
0
    it('should select with a non matching :not selector', () => {
      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);

      expect(matcher.match(CssSelector.parse('p[someOtherAttr].someOtherClass')[0], selectableCollector)).toEqual(true);
      expect(matched).toEqual([s1[0],1,s2[0],2,s3[0],3,s4[0],4]);
    });
Ejemplo n.º 5
0
    it('should select by element name case insensitive', () => {
      matcher.addSelectables(s1 = CssSelector.parse('someTag'), 1);

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

      expect(matcher.match(CssSelector.parse('SOMETAG')[0], selectableCollector)).toEqual(true);
      expect(matched).toEqual([s1[0],1]);
    });
Ejemplo n.º 6
0
    it('should select by attr name 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(true);
      expect(matched).toEqual([s1[0],1]);
    });
Ejemplo n.º 7
0
    it('should select with one match in a list', () => {
       matcher.addSelectables(s1 = CssSelector.parse('input[type=text], textbox'), 1);

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

       reset();
       expect(matcher.match(CssSelector.parse('input[type=text]')[0], selectableCollector)).toEqual(true);
       expect(matched).toEqual([s1[0],1]);
    });
Ejemplo n.º 8
0
    it('should select by attr name only once if the value is from the DOM', () => {
      matcher.addSelectables(s1 = CssSelector.parse('[some-decor]'), 1);

      var elementSelector = new CssSelector();
      var element = el('<div attr></div>');
      var empty = DOM.getAttribute(element, 'attr');
      elementSelector.addAttribute('some-decor', empty);
      matcher.match(elementSelector, selectableCollector);
      expect(matched).toEqual([s1[0],1]);
    });
 DirectiveParser.prototype.processElement = function (parent, current, control) {
     var _this = this;
     var attrs = current.attrs();
     var classList = current.classList();
     var cssSelector = new selector_1.CssSelector();
     var foundDirectiveIndices = [];
     var elementBinder = null;
     cssSelector.setElement(dom_adapter_1.DOM.nodeName(current.element));
     for (var i = 0; i < classList.length; i++) {
         cssSelector.addClassName(classList[i]);
     }
     collection_1.MapWrapper.forEach(attrs, function (attrValue, attrName) { cssSelector.addAttribute(attrName, attrValue); });
     this._selectorMatcher.match(cssSelector, function (selector, directiveIndex) {
         var directive = _this._directives[directiveIndex];
         elementBinder = current.bindElement();
         if (directive.type === api_1.RenderDirectiveMetadata.COMPONENT_TYPE) {
             _this._ensureHasOnlyOneComponent(elementBinder, current.elementDescription);
             // components need to go first, so it is easier to locate them in the result.
             collection_1.ListWrapper.insert(foundDirectiveIndices, 0, directiveIndex);
             elementBinder.setComponentId(directive.id);
         }
         else {
             foundDirectiveIndices.push(directiveIndex);
         }
     });
     collection_1.ListWrapper.forEach(foundDirectiveIndices, function (directiveIndex) {
         var dirMetadata = _this._directives[directiveIndex];
         var directiveBinderBuilder = elementBinder.bindDirective(directiveIndex);
         current.compileChildren = current.compileChildren && dirMetadata.compileChildren;
         if (lang_1.isPresent(dirMetadata.properties)) {
             collection_1.ListWrapper.forEach(dirMetadata.properties, function (bindConfig) {
                 _this._bindDirectiveProperty(bindConfig, current, directiveBinderBuilder);
             });
         }
         if (lang_1.isPresent(dirMetadata.hostListeners)) {
             _this._sortedKeysForEach(dirMetadata.hostListeners, function (action, eventName) {
                 _this._bindDirectiveEvent(eventName, action, current, directiveBinderBuilder);
             });
         }
         if (lang_1.isPresent(dirMetadata.hostProperties)) {
             _this._sortedKeysForEach(dirMetadata.hostProperties, function (expression, hostPropertyName) {
                 _this._bindHostProperty(hostPropertyName, expression, current, directiveBinderBuilder);
             });
         }
         if (lang_1.isPresent(dirMetadata.hostAttributes)) {
             _this._sortedKeysForEach(dirMetadata.hostAttributes, function (hostAttrValue, hostAttrName) {
                 _this._addHostAttribute(hostAttrName, hostAttrValue, current);
             });
         }
         if (lang_1.isPresent(dirMetadata.readAttributes)) {
             collection_1.ListWrapper.forEach(dirMetadata.readAttributes, function (attrName) { elementBinder.readAttribute(attrName); });
         }
     });
 };
Ejemplo n.º 10
0
    it('should select by many attributes and independent of the value', () => {
      matcher.addSelectables(s1 = CssSelector.parse('input[type=text][control]'), 1);

      var cssSelector = new CssSelector();
      cssSelector.setElement('input');
      cssSelector.addAttribute('type', 'text');
      cssSelector.addAttribute('control', 'one');

      expect(matcher.match(cssSelector, selectableCollector)).toEqual(true);
      expect(matched).toEqual([s1[0], 1]);
    });
Ejemplo n.º 11
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]);
    });
Ejemplo n.º 12
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]');
    });
Ejemplo n.º 13
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');
    });
Ejemplo n.º 14
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]');
    });
 function DirectiveParser(_parser, _directives) {
     this._parser = _parser;
     this._directives = _directives;
     this._selectorMatcher = new selector_1.SelectorMatcher();
     for (var i = 0; i < _directives.length; i++) {
         var directive = _directives[i];
         var selector = selector_1.CssSelector.parse(directive.selector);
         this._selectorMatcher.addSelectables(selector, i);
     }
 }
Ejemplo n.º 16
0
    it('should detect :not without truthy', () => {
      var cssSelector = CssSelector.parse(':not([attrname=attrvalue].someclass)')[0];
      expect(cssSelector.element).toEqual("*");

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

      expect(cssSelector.toString()).toEqual('*:not(.someclass[attrname=attrvalue])');
    });
Ejemplo n.º 17
0
 constructor(parser: Parser, directives:List<DirectiveMetadata>) {
   super();
   this._parser = parser;
   this._selectorMatcher = new SelectorMatcher();
   this._directives = directives;
   for (var i=0; i<directives.length; i++) {
     var selector = CssSelector.parse(directives[i].selector);
     this._selectorMatcher.addSelectables(selector, i);
   }
 }
Ejemplo n.º 18
0
 constructor(parser: Parser, directives:List<DirectiveMetadata>) {
   super();
   this._parser = parser;
   this._selectorMatcher = new SelectorMatcher();
   this._directives = directives;
   for (var i=0; i<directives.length; i++) {
     var directive = directives[i];
     var selector = CssSelector.parse(directive.selector);
     this._ensureComponentOnlyHasElementSelector(selector, directive);
     this._selectorMatcher.addSelectables(selector, i);
   }
 }
Ejemplo n.º 19
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].notSelector.element).toEqual('textarea');

      expect(cssSelectors[2].element).toEqual('textbox');
      expect(cssSelectors[2].notSelector.classNames).toEqual(['special']);
    });
Ejemplo n.º 20
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.notSelector;
      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])');
    });
Ejemplo n.º 21
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([]);
    });
 querySelectorAll(el, selector) {
     var res = [];
     var _recursive = (result, node, selector, matcher) => {
         var cNodes = node.childNodes;
         if (cNodes && cNodes.length > 0) {
             for (var i = 0; i < cNodes.length; i++) {
                 var childNode = cNodes[i];
                 if (this.elementMatches(childNode, selector, matcher)) {
                     result.push(childNode);
                 }
                 _recursive(result, childNode, selector, matcher);
             }
         }
     };
     var matcher = new SelectorMatcher();
     matcher.addSelectables(CssSelector.parse(selector));
     _recursive(res, el, selector, matcher);
     return res;
 }
Ejemplo n.º 23
0
    it('should select independent of the order in the css selector', () => {
      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);

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

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

      reset();
      expect(matcher.match(CssSelector.parse('.class1.class2')[0], selectableCollector)).toEqual(true);
      expect(matched).toEqual([s3[0],3,s4[0],4]);

      reset();
      expect(matcher.match(CssSelector.parse('.class2.class1')[0], selectableCollector)).toEqual(true);
      expect(matched).toEqual([s4[0],4,s3[0],3]);
    });
 Parse5DomAdapter.prototype.querySelectorAll = function (el, selector) {
     var _this = this;
     var res = collection_1.ListWrapper.create();
     var _recursive = function (result, node, selector, matcher) {
         var cNodes = node.childNodes;
         if (cNodes && cNodes.length > 0) {
             for (var i = 0; i < cNodes.length; i++) {
                 var childNode = cNodes[i];
                 if (_this.elementMatches(childNode, selector, matcher)) {
                     collection_1.ListWrapper.push(result, childNode);
                 }
                 _recursive(result, childNode, selector, matcher);
             }
         }
     };
     var matcher = new selector_1.SelectorMatcher();
     matcher.addSelectables(selector_1.CssSelector.parse(selector));
     _recursive(res, el, selector, matcher);
     return res;
 };
Ejemplo n.º 25
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]);
    });
Ejemplo n.º 26
0
    it('should select by attr name case insensitive 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(true);
      expect(matched).toEqual([s1[0],1]);

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

      reset();
      expect(matcher.match(CssSelector.parse('[someAttr][someAttr2]')[0], selectableCollector)).toEqual(true);
      expect(matched).toEqual([s1[0],1,s2[0],2]);
    });
Ejemplo n.º 27
0
 MapWrapper.forEach(attrs, (attrValue, attrName) => {
   cssSelector.addAttribute(attrName, attrValue);
 });
Ejemplo n.º 28
0
  process(parent:CompileElement, current:CompileElement, control:CompileControl) {
    var attrs = current.attrs();
    var classList = current.classList();

    var cssSelector = new CssSelector();
    var nodeName = DOM.nodeName(current.element);
    cssSelector.setElement(nodeName);
    for (var i=0; i < classList.length; i++) {
      cssSelector.addClassName(classList[i]);
    }

    MapWrapper.forEach(attrs, (attrValue, attrName) => {
      cssSelector.addAttribute(attrName, attrValue);
    });

    var viewportDirective;
    var componentDirective;
    // Note: We assume that the ViewSplitter already did its work, i.e. template directive should
    // only be present on <template> elements!
    var isTemplateElement = DOM.isTemplateElement(current.element);

    this._selectorMatcher.match(cssSelector, (selector, directiveIndex) => {
      var elementBinder = current.bindElement();
      var directive = this._directives[directiveIndex];
      var directiveBinder = elementBinder.bindDirective(directiveIndex);
      current.compileChildren = current.compileChildren && directive.compileChildren;
      if (isPresent(directive.properties)) {
        MapWrapper.forEach(directive.properties, (bindConfig, dirProperty) => {
          this._bindDirectiveProperty(dirProperty, bindConfig, current, directiveBinder);
        });
      }
      if (isPresent(directive.hostListeners)) {
        MapWrapper.forEach(directive.hostListeners, (action, eventName) => {
          this._bindDirectiveEvent(eventName, action, current, directiveBinder);
        });
      }
      if (isPresent(directive.setters)) {
        ListWrapper.forEach(directive.setters, (propertyName) => {
          elementBinder.bindPropertySetter(propertyName);
        });
      }
      if (isPresent(directive.readAttributes)) {
        ListWrapper.forEach(directive.readAttributes, (attrName) => {
          elementBinder.readAttribute(attrName);
        });
      }
      if (directive.type === DirectiveMetadata.VIEWPORT_TYPE) {
        if (!isTemplateElement) {
          throw new BaseException(`Viewport directives need to be placed on <template> elements or elements ` +
            `with template attribute - check ${current.elementDescription}`);
        }
        if (isPresent(viewportDirective)) {
          throw new BaseException(`Only one viewport directive is allowed per element - check ${current.elementDescription}`);
        }
        viewportDirective = directive;
      } else {
        if (isTemplateElement) {
          throw new BaseException(`Only template directives are allowed on template elements - check ${current.elementDescription}`);
        }
        if (directive.type === DirectiveMetadata.COMPONENT_TYPE) {
          if (isPresent(componentDirective)) {
            throw new BaseException(`Only one component directive is allowed per element - check ${current.elementDescription}`);
          }
          componentDirective = directive;
          elementBinder.setComponentId(directive.id);
        }
      }
    });
  }
Ejemplo n.º 29
0
 expect(() => {
   CssSelector.parse('sometag:not(:not([attrname=attrvalue].someclass))')[0];
 }).toThrowError('Nesting :not is not allowed in a selector');
Ejemplo n.º 30
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]');
 });