Ejemplo n.º 1
0
        public void DescendantCombinator_WithNesting_Matches()
        {
            var selector = CssSelector.WithElement("div", null)
                           .Combine(CssCombinator.Descendant, CssSelector.WithElement("ol", null))
                           .Combine(CssCombinator.Descendant, CssSelector.WithElement("li", null))
                           .Combine(CssCombinator.Descendant, CssSelector.WithElement("p", null));

            var xdoc = XDocument.Parse(@"
				<html>
					<head />
					<body>
						<div id='div00'>
							<ol id='ol01'>
								<li id='li01'><p id='p01' /></li>
								<li id='li02'><p id='p02_1' /><p id='p02_2' /></li>
								<li id='li03' />
								<li id='li04'><div id='div04'><p id='p04' /></div></li>
								<div id='div05'>
									<li id='li05'><p id='p05' /></li>
								</div>
							</ol>
						</div>
						<ol id='ol02'>
							<li id='li06'><p id='p06' /></li>
						</ol>
					</body>
				</html>"                );

            var matchingElements = Match(xdoc, selector);

            Assert.That(matchingElements.Select(e => e.Attribute("id").Value), Is.EquivalentTo(new[] { "p01", "p02_1", "p02_2", "p04", "p05" }));
        }
Ejemplo n.º 2
0
        public void SelectorSequence_WithThreeSelectors_Matches()
        {
            var selector = CssSelector.WithElement("h1", null)
                           .Add(CssSelector.WithId("chapter1"));

            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>
						<p id='chapter1' istarget='true' />
						<h1 id='chapter1' istarget='true' />
						<h1 id='ch2' istarget='true' />
						<h1 id='ch3' />
					</body>
				</html>"                );

            var matchingElements = Match(xdoc, selector);

            Assert.That(matchingElements.Select(e => e.Name.LocalName + "." + e.Attribute("id").Value), Is.EquivalentTo(new[] { "h1.chapter1" }));
        }
Ejemplo n.º 3
0
        public void SelectorSequence_WithTwoSelectors_ToString()
        {
            var selector = CssSelector.WithElement("h1", null)
                           .Add(CssSelector.WithId("chapter1"));

            Assert.That(selector.ToString(), Is.EqualTo("h1#chapter1"));
        }
Ejemplo n.º 4
0
        public void ChildCombinator_WithoutNesting_ToString()
        {
            var leftSelector  = CssSelector.WithElement("h1", null);
            var rightSelector = CssSelector.WithElement("em", null);
            var selector      = leftSelector.Combine(CssCombinator.Child, rightSelector);

            Assert.That(selector.ToString(), Is.EqualTo("h1 > em"));
        }
Ejemplo n.º 5
0
        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"));
        }
Ejemplo n.º 6
0
        public void DescendantCombinator_WithNesting_ToString()
        {
            var selector = CssSelector.WithElement("div", null)
                           .Combine(CssCombinator.Descendant, CssSelector.WithElement("ol", null))
                           .Combine(CssCombinator.Descendant, CssSelector.WithElement("li", null))
                           .Combine(CssCombinator.Descendant, CssSelector.WithElement("p", null));

            Assert.That(selector.ToString(), Is.EqualTo("div ol li p"));
        }
Ejemplo n.º 7
0
        public void ChildCombinator_WithoutNesting()
        {
            var leftSelector  = CssSelector.WithElement("h1", null);
            var rightSelector = CssSelector.WithElement("em", null);
            var selector      = leftSelector.Combine(CssCombinator.Child, rightSelector);

            Assert.Multiple(() =>
            {
                Assert.That(selector.Subject, Is.SameAs(rightSelector), nameof(selector.Subject));
                Assert.That(selector.Specificity, Is.EqualTo(new CssSpecificity(0, 0, 2)), nameof(selector.Specificity));
            });
        }
Ejemplo n.º 8
0
        public void ElementSelector_ForNameInUnspecifiedNamespace()
        {
            var selector = CssSelector.WithElement("h1");

            Assert.Multiple(() =>
            {
                Assert.That(selector, Is.InstanceOf <CssSimpleSelector>());
                Assert.That(selector.LocalName, Is.EqualTo("h1"), nameof(selector.LocalName));
                Assert.That(selector.NamespacePrefix, Is.Null, nameof(selector.NamespacePrefix));
                Assert.That(selector.TypeSelector, Is.SameAs(selector), nameof(selector.TypeSelector));
                Assert.That(selector.Specificity, Is.EqualTo(new CssSpecificity(0, 0, 1)), nameof(selector.Specificity));
            });
        }
Ejemplo n.º 9
0
        public void SelectorSequence_WithTwoSelectors()
        {
            var typeSelector  = CssSelector.WithElement("h1");
            var extraSelector = CssSelector.WithId("chapter1");
            var selector      = typeSelector.Add(extraSelector);

            Assert.Multiple(() =>
            {
                Assert.That(selector.TypeSelector, Is.SameAs(typeSelector), nameof(selector.TypeSelector));
                Assert.That(selector.OtherSelectors, Is.EquivalentTo(new[] { extraSelector }), nameof(selector.OtherSelectors));
                Assert.That(selector.Subject, Is.SameAs(selector), nameof(selector.Subject));
                Assert.That(selector.Specificity, Is.EqualTo(new CssSpecificity(1, 0, 1)), nameof(selector.Specificity));
            });
        }
Ejemplo n.º 10
0
        public void ElementSelector_ForNameInAnyNamespace_Matches()
        {
            var selector = CssSelector.WithElement("h1", "*");
            var xdoc     = XDocument.Parse(string.Format(@"
				<html xmlns:x='{0}'>
					<head />
					<body>
						<h1 id='h1_1'>Title 1</h1>
						<x:h1 id='h1_2'>Title 2</x:h1>
						<h2 id='h2'>Title 2.1</h2>
					</body>
				</html>"                , XHTML_NAMESPACE));

            var matchingElements = Match(xdoc, selector);

            Assert.That(matchingElements.Select(e => e.Attribute("id").Value), Is.EquivalentTo(new[] { "h1_1", "h1_2" }));
        }
Ejemplo n.º 11
0
        public void DescendantCombinator_WithNesting()
        {
            var selector1 = CssSelector.WithElement("div");
            var selector2 = CssSelector.WithElement("ol");
            var selector3 = CssSelector.WithElement("li");
            var selector4 = CssSelector.WithElement("p");
            var selector  = selector1
                            .Combine(CssCombinator.Descendant, selector2)
                            .Combine(CssCombinator.Descendant, selector3)
                            .Combine(CssCombinator.Descendant, selector4);

            Assert.Multiple(() =>
            {
                Assert.That(selector.Subject, Is.SameAs(selector4), nameof(selector.Subject));
                Assert.That(selector.Specificity, Is.EqualTo(new CssSpecificity(0, 0, 4)), nameof(selector.Specificity));
            });
        }
Ejemplo n.º 12
0
        public void ElementSelector_ForNameInNonDefaultNamespace_Matches()
        {
            _namespaceManager.AddNamespace("x", XHTML_NAMESPACE);
            _namespaceManager.AddNamespace("s", SOME_NAMESPACE);

            var selector = CssSelector.WithElement("h1", "s");
            var xdoc     = XDocument.Parse(string.Format(@"
				<html xmlns:x='{0}' xmlns:t='{1}'>
					<head />
					<body>
						<x:h1 id='h1_1'>Title 1</x:h1>
						<t:h1 id='h1_2'>Title 1</t:h1>
					</body>
				</html>"                , XHTML_NAMESPACE, SOME_NAMESPACE));

            var matchingElements = Match(xdoc, selector);

            Assert.That(matchingElements.Select(e => e.Attribute("id").Value), Is.EquivalentTo(new[] { "h1_2" }));
        }
Ejemplo n.º 13
0
        public void ElementSelector_ForNameInUnspecifiedNamespace_MatchesNamesInDefaultNamespace_WhenDefaultNamespaceDefined()
        {
            _namespaceManager.AddNamespace("", XHTML_NAMESPACE);

            var selector = CssSelector.WithElement("h1");
            var xdoc     = XDocument.Parse(string.Format(@"
				<html xmlns='{0}' xmlns:s='{1}'>
					<head />
					<body>
						<h1 id='h1_1'>Title 1</h1>
						<s:h1 id='h1_2'>Title 2</s:h1>
						<h2 id='h2'>Title 2.1</h2>
					</body>
				</html>"                , XHTML_NAMESPACE, SOME_NAMESPACE));

            var matchingElements = Match(xdoc, selector);

            Assert.That(matchingElements.Select(e => e.Attribute("id").Value), Is.EquivalentTo(new[] { "h1_1" }));
        }
Ejemplo n.º 14
0
        public void SelectorSequence_WithTwoSelectors_Matches()
        {
            var selector = CssSelector.WithElement("h1", null)
                           .Add(CssSelector.WithId("chapter1"));

            var xdoc = XDocument.Parse(@"
				<html>
					<head />
					<body>
						<p id='chapter1' />
						<h1 id='chapter1' />
						<h1 id='ch2' />
						<h1 id='ch3' />
					</body>
				</html>"                );

            var matchingElements = Match(xdoc, selector);

            Assert.That(matchingElements.Select(e => e.Name.LocalName + "." + e.Attribute("id").Value), Is.EquivalentTo(new[] { "h1.chapter1" }));
        }
Ejemplo n.º 15
0
        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));
            });
        }
Ejemplo n.º 16
0
 public void ElementSelector_ForNameInUnspecifiedNamespace_ToString()
 {
     Assert.That(CssSelector.WithElement("h1").ToString(), Is.EqualTo("h1"));
 }
Ejemplo n.º 17
0
 public void ElementSelector_ForNameWithoutNamespace_ToString()
 {
     Assert.That(CssSelector.WithElement("h1", "").ToString(), Is.EqualTo("|h1"));
 }
Ejemplo n.º 18
0
 public void ElementSelector_ForNameInNonDefaultNamespace_ToString()
 {
     Assert.That(CssSelector.WithElement("h1", "x").ToString(), Is.EqualTo("x|h1"));
 }