public void IsMatchMethod(object value, ExpressionToken token, IsMatchResult isMatchResult) { if (isMatchResult.ReturnValue != null && isMatchResult.ToString() != string.Empty) isMatchResult.ReturnValue += " && "; //Custom attribute if searching by style string tokenAttribute = token.Attribute.Replace("parentElement", "parentNode").Replace("parentWindow", "ownerDocument.defaultView"); string tokenValue = token.Value; if (tokenAttribute.Equals("style", StringComparison.OrdinalIgnoreCase)) { if (tokenValue.Contains("#")) tokenValue = HexToDecimal(tokenValue); tokenAttribute += ".cssText"; } string parentString = ""; while (tokenAttribute.StartsWith("parentNode.")) { tokenAttribute = tokenAttribute.Remove(0, "parentNode.".Length); parentString += ".parentNode"; } tokenAttribute = AttributeNormalizer.Normalize(tokenAttribute); if (tokenValue == "") tokenValue = "[]*"; if (!string.IsNullOrEmpty(tokenValue)) { if (token.MatchType == MatchType.Contains) { if (token.ExpectedMatchCount != int.MinValue) { isMatchResult.ReturnValue += string.Format( " ((elems[a]" + parentString + " != null && elems[a]" + parentString + ".hasAttributes() == true && elems[a]" + parentString + ".getAttribute('{0}') != null && elems[a]" + parentString + ".getAttribute('{0}').toString().match(/{1}/gim) != null && elems[a]" + parentString + ".getAttribute('{0}').toString().match(/{1}/gim).length == {2}) || (elems[a]" + parentString + " != null && elems[a]" + parentString + ".{0} != null && elems[a]" + parentString + ".{0}.toString().match(/{1}/gim) != null && elems[a]" + parentString + ".{0}.toString().match(/{1}/gim).length == {2}))", tokenAttribute, tokenValue, token.ExpectedMatchCount); } else { isMatchResult.ReturnValue += string.Format( " ((elems[a]" + parentString + " != null && elems[a]" + parentString + ".hasAttributes() == true && elems[a]" + parentString + ".getAttribute('{0}') != null && elems[a]" + parentString + ".getAttribute('{0}').toString().match(/{1}/gim) != null && elems[a]" + parentString + ".getAttribute('{0}').toString().match(/{1}/gim).length >= 0) || (elems[a]" + parentString + " != null && elems[a]" + parentString + ".{0} != null && elems[a]" + parentString + ".{0}.toString().match(/{1}/gim) != null && elems[a]" + parentString + ".{0}.toString().match(/{1}/gim).length >= 0))", tokenAttribute, tokenValue); } } else { isMatchResult.ReturnValue += string.Format( " ((elems[a]" + parentString + " != null && elems[a]" + parentString + ".hasAttributes() == true && elems[a]" + parentString + ".getAttribute('{0}') != null && elems[a]" + parentString + ".getAttribute('{0}').toString().match(/{1}/gim) != null && elems[a]" + parentString + ".getAttribute('{0}').toString().match(/{1}/gim)[0].length == elems[a]" + parentString + ".getAttribute('{0}').toString().length) || (elems[a]" + parentString + " != null && elems[a]" + parentString + ".{0} != null && elems[a]" + parentString + ".{0}.toString().match(/{1}/gim) != null && elems[a]" + parentString + ".{0}.toString().match(/{1}/gim)[0].length == elems[a]" + parentString + ".{0}.toString().length))", tokenAttribute, tokenValue, token.Value.Length); } } else { isMatchResult.ReturnValue += string.Format( " ((elems[a]" + parentString + " != null && elems[a]" + parentString + ".hasAttributes() == true && elems[a]" + parentString + ".getAttribute('{0}') != null && elems[a]" + parentString + ".getAttribute('{0}').toString() != null && elems[a]" + parentString + ".getAttribute('{0}').toString() == '') || (elems[a]" + parentString + " != null && elems[a]" + parentString + ".{0} != null && elems[a]" + parentString + ".{0}.toString() != null && elems[a]" + parentString + ".{0}.toString() == '')) ", tokenAttribute, tokenValue, token.Value.Length); } isMatchResult.ContinueChecking = true; }
public void IsMatchMethod(object value, ExpressionToken token, IsMatchResult isMatchResult) { IHTMLElement elem = (IHTMLElement)value; string copyOfAttribute = token.Attribute; while (copyOfAttribute.StartsWith("parentElement.")) { if (elem.parentElement == null) { isMatchResult.ContinueChecking = false; isMatchResult.ReturnValue = false; return; } elem = elem.parentElement; copyOfAttribute = copyOfAttribute.Remove(0, "parentElement.".Length); } if (!token.Attribute.Equals(copyOfAttribute)) //if we removed parentElement we should re-normalize. copyOfAttribute = AttributeNormalizer.Normalize(copyOfAttribute); if (copyOfAttribute.StartsWith("parentWindow")) { string attr; isMatchResult.ContinueChecking = false; isMatchResult.ReturnValue = false; try { attr = copyOfAttribute.Split('.')[1]; } catch (Exception ex) { //need to throw expression wrong format exception throw ex; } if (attr == "location") { if (token.IsMatch(((HTMLDocument)elem.document).parentWindow.location.href)) { isMatchResult.ContinueChecking = true; isMatchResult.ReturnValue = true; } } if (attr == "name") { if (token.IsMatch(((HTMLDocument)elem.document).parentWindow.name)) { isMatchResult.ContinueChecking = true; isMatchResult.ReturnValue = true; } } } else { if (elem != null && !(elem is IHTMLUnknownElement)) { string attr; if (copyOfAttribute.Equals("style", StringComparison.OrdinalIgnoreCase)) { //the border, padding, and margin attributes if specified inline get applied to all 4 sides: //EG: border-right, border-left, border-top, border-bottom //Assumption: use -right for the global search if (token.Value.ToLower().Contains("border:")) token.Value = token.Value.Replace("border:", "border-right:"); if (token.Value.ToLower().Contains("padding:")) token.Value = token.Value.Replace("padding:", "padding-right:"); if (token.Value.ToLower().Contains("margin:")) token.Value = token.Value.Replace("margin:", "margin-right:"); attr = elem.style.cssText; } else { object elemAttribute = elem.getAttribute(copyOfAttribute, 0); if (!(elem.getAttribute(token.Attribute, 0) is string) && !(elemAttribute is Boolean) && !(elemAttribute is int)) if (((IHTMLElement4)elem).getAttributeNode(copyOfAttribute) != null && ((IHTMLElement4)elem).getAttributeNode(copyOfAttribute).nodeValue != null) attr = ((IHTMLElement4)elem).getAttributeNode(copyOfAttribute).nodeValue.ToString(); else attr = elemAttribute != null ? elemAttribute.ToString() : string.Empty; else attr = elemAttribute.ToString(); } if (token.IsMatch(attr)) { isMatchResult.ContinueChecking = true; isMatchResult.ReturnValue = true; } else { if (token.IsPartOfStyle) { string origAttr = token.Attribute; string origValue = token.Value; if (token.Attribute == "style" && token.Value.Contains(":")) { token.Attribute = token.Value.Split(new char[] { ':' })[0]; token.Value = token.Value.Split(new char[] { ':' })[1]; } if (((IHTMLElement2)elem).currentStyle.getAttribute(token.Attribute, 0) != null) attr = ((IHTMLElement2)elem).currentStyle.getAttribute(token.Attribute, 0).ToString(); if (token.IsMatch(attr)) { isMatchResult.ContinueChecking = true; isMatchResult.ReturnValue = true; } else { isMatchResult.ContinueChecking = false; isMatchResult.ReturnValue = false; } token.Attribute = origAttr; token.Value = origValue; } else { isMatchResult.ContinueChecking = false; isMatchResult.ReturnValue = false; } } } else { isMatchResult.ContinueChecking = false; isMatchResult.ReturnValue = false; } } }
public object IsMatch(object value, BrowserType browserType) { IsMatchResult result = new IsMatchResult(); bool inStyle = false; foreach (ExpressionToken token in this.InnerList) { if (token.Attribute == "style") inStyle = true; token.IsPartOfStyle = inStyle; _isMatchMethod(value, token, result); if (!result.ContinueChecking) break; } return result.ReturnValue; }