/// <summary> /// Returns a verbose selector that will select this node. /// </summary> public INodeMatcher GenerateCssSelector(bool includeNthChild) { AndSelector selector = new AndSelector(); // name if (this.Name != null) { selector.Selectors.Add(new NameSelector(this.Name)); } // attributes foreach (string s in this.AttributeNames) { string[] parts = this[s].Split(' '); if (parts.Length == 1) { // 'class' is the only attribute I know of that allows mix and match, everything else should be exact match var op = (s != "class") ? AttributeSelector.EqualToOp // exact match : AttributeSelector.ContainsWordOp; // mix and match - also allows string to have .class for single classed elements selector.Selectors.Add(new AttributeSelector(s, op, parts[0])); } else { foreach (string part in parts) { selector.Selectors.Add(new AttributeSelector(s, AttributeSelector.ContainsWordOp, part)); } } } if (this.SiblingCount > 1 && includeNthChild) { selector.Selectors.Add(new SiblingSelector(0, this.SiblingIndex + 1)); } // : selectors var candidates = new INodeMatcher[] { // new ColonSelector(":first-child") // ,new ColonSelector(":last-child") // ,new ColonSelector(":empty") // ,new ColonSelector(":parent") }; foreach (var c in candidates) { if (c.IsMatch(this)) { selector.Selectors.Add(c); } } return(selector); }
internal void AddParent(INodeMatcher parentSelector) { AndSelector a = this.Selector as AndSelector; if (a == null) { this.Selector = a = new AndSelector(this.Selector); } // add at begining so it .ToString()s in the correct place a.Selectors.Insert(0, new HasParentSelector(parentSelector)); }
internal void AddSibling(INodeMatcher sibSelector, bool any) { AndSelector a = this.Selector as AndSelector; if (a == null) { this.Selector = a = new AndSelector(this.Selector); } // add at begining so it .ToString()s in the correct place a.Selectors.Insert(0, new HasPreviousSiblingSelector(sibSelector, any)); }
/// <summary> /// used for when a parent node must have a specific child /// </summary> internal void AddChild(INodeMatcher childSelector) { AndSelector a = this.Selector as AndSelector; if (a == null) { this.Selector = a = new AndSelector(this.Selector); } // add at begining so it .ToString()s in the correct place a.Selectors.Insert(0, new HasChildSelector(childSelector)); this.HasChildSelector = true; }