public void SelectorSequence_WithThreeSelectors_ToString() { var selector = CssSelector.WithElement("h1", null) .Add(CssSelector.WithId("chapter1")) .Add(CssSelector.WithPseudoClass("target")); Assert.That(selector.ToString(), Is.EqualTo("h1#chapter1:target")); }
public void PseudoClassSelector_TargetPseudoClass_Matches() { var selector = CssSelector.WithPseudoClass("target"); var pseudoClassInfo = Substitute.For <XElementPseudoClassInfo>(); pseudoClassInfo.IsTarget(Arg.Any <XElement>()) .Returns(call => "true".Equals(call.Arg <XElement>().Attribute("istarget")?.Value, StringComparison.OrdinalIgnoreCase)); var xdoc = XDocument.Parse(@" <html> <head /> <body> <h1 id='ch1' /> <h1 id='ch2' istarget='true' /> <h1 id='ch3' /> </body> </html>" ); var matchingElements = xdoc.Descendants().Where(e => selector.Matches(new XElementInfo(e, pseudoClassInfo))).ToList(); Assert.That(matchingElements.Select(e => e.Attribute("id").Value), Is.EquivalentTo(new[] { "ch2" })); }
public void SelectorSequence_WithThreeSelectors() { var typeSelector = CssSelector.WithElement("h1", null); var extraSelectors = new CssSimpleSelector[] { CssSelector.WithId("chapter1"), CssSelector.WithPseudoClass("target") }; var selector = typeSelector.Add(extraSelectors[0]); for (var i = 1; i < extraSelectors.Length; i++) { selector = selector.Add(extraSelectors[i]); } Assert.Multiple(() => { Assert.That(selector.TypeSelector, Is.SameAs(typeSelector), nameof(selector.TypeSelector)); Assert.That(selector.OtherSelectors, Is.EquivalentTo(extraSelectors), nameof(selector.OtherSelectors)); Assert.That(selector.Subject, Is.SameAs(selector), nameof(selector.Subject)); Assert.That(selector.Specificity, Is.EqualTo(new CssSpecificity(1, 1, 1)), nameof(selector.Specificity)); }); }
public void PseudoClassSelector_TargetPseudoClass_ToString() { Assert.That(CssSelector.WithPseudoClass("target").ToString(), Is.EqualTo(":target")); }
public void PseudoClassSelector_ForNonStandardPseudoclass() { var selector = CssSelector.WithPseudoClass("unknown"); Assert.That(selector.Specificity, Is.EqualTo(new CssSpecificity(0, 1, 0)), nameof(selector.Specificity)); }