コード例 #1
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestBeforeElements()
        {
            var element1  = new HElement("h1");
            var element2  = new HElement("h2");
            var container = new HElement("div", element1, element2).AddClass("container");

            container.Elements().Before(new HElement("p"));

            Assert.Equal(new String[] { "p", "h1", "p", "h2" }, container.Elements().Select(e => e.Name));
        }
コード例 #2
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestBeforeElementsByCallback()
        {
            var element1  = new HElement("h1");
            var element2  = new HElement("h2");
            var container = new HElement("div", element1, element2).AddClass("container");

            container.Elements().Before((e, i) => new HElement[] { new HElement(e.Name + "-" + i.ToString()) });

            Assert.Equal(new String[] { "h1-0", "h1", "h2-1", "h2" }, container.Elements().Select(e => e.Name));
        }
コード例 #3
0
ファイル: HElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestClone()
        {
            var parent = new HElement("parent");

            var elm = new HElement("test", "content");

            elm.Add(
                new HAttribute("attr1", "val1"),
                new HAttribute("attr3", "val3")
                );
            Assert.Equal(1, elm.Nodes().Count());
            parent.Add(elm);

            var newElm = new HElement("test", elm);
            var c      = newElm.Elements().First();

            Assert.Equal(2, c.Attributes().Count());
            Assert.Equal(0, c.Attributes("Attr2").Count());
            Assert.Equal(1, c.Nodes().Count());

            var cloneElm = c.Clone();

            Assert.Equal(2, cloneElm.Attributes().Count());
            Assert.Equal(0, cloneElm.Attributes("Attr2").Count());
            Assert.Equal(1, cloneElm.Nodes().Count());
        }
コード例 #4
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestSlice()
        {
            var n1   = new HElement("div", "Content 1");
            var n2   = new HElement("div", "Content 2");
            var n3   = new HElement("div", "Content 3");
            var n4   = new HElement("div", "Content 4");
            var n5   = new HElement("div", "Content 5");
            var root = new HElement("div", n1, n2, n3, n4, n5);

            IEnumerable <HElement> elements = root.Elements().ToArray();

            Assert.Equal(new HElement[] { n1, n2, n3, n4, n5 }, elements.Slice(0).ToArray());
            Assert.Equal(new HElement[] { n3, n4, n5 }, elements.Slice(2).ToArray());
            Assert.Equal(new HElement[] { n4, n5 }, elements.Slice(-2).ToArray());

            Assert.Equal(new HElement[] { n1, n2, n3, n4, n5 }, elements.Slice(0, 4).ToArray());
            Assert.Equal(new HElement[] { n3, n4 }, elements.Slice(2, 3).ToArray());
            Assert.Equal(new HElement[] { n3 }, elements.Slice(2, 2).ToArray());
            Assert.Equal(new HElement[] { n4, n5 }, elements.Slice(-2, 4).ToArray());
            Assert.Equal(new HElement[] { n4, n5 }, elements.Slice(-2, -1).ToArray());
            Assert.Equal(new HElement[] { }, elements.Slice(-2, 2).ToArray());

            elements = null;
            Assert.Equal(0, elements.Slice(0).Count());
            Assert.Equal(0, elements.Slice(0, 4).Count());
        }
コード例 #5
0
        public void TestElementsWithName()
        {
            HContainer container = new HElement("Test");

            HElement[] elements = container.Elements("Test").ToArray();
            Assert.Equal(0, elements.Length);

            container.Add("Content 1");
            container.Add(new HElement("test"));
            container.Add("Content 2");
            container.Add(new HElement("element"));
            container.Add("Content 3");

            elements = container.Elements("Test").ToArray();
            Assert.Equal(1, elements.Length);
        }
コード例 #6
0
ファイル: HBuilder2.cs プロジェクト: undyings/Commune
 public static IEnumerable<HElement> RoundPage(HElement page)
 {
   yield return page;
   foreach (HElement node in page.Elements())
   {
     foreach (HElement element in RoundPage(node))
       yield return element;
   }
 }
コード例 #7
0
        public void TestElements()
        {
            HContainer container = new HElement("Test");

            Assert.False(container.HasElements);
            HElement[] elements = container.Elements().ToArray();
            Assert.Equal(0, elements.Length);

            container.Add("Content 1");
            Assert.False(container.HasElements);
            container.Add(new HElement("test"));
            Assert.True(container.HasElements);
            container.Add("Content 2");
            container.Add(new HElement("element"));
            container.Add("Content 3");

            elements = container.Elements().ToArray();
            Assert.Equal(2, elements.Length);
        }
コード例 #8
0
        public void TestDeserializeDocument()
        {
            var serializer = new HSerializer();

            var hdoc = serializer.DeserializeDocument(new StringReader("<html><body><h1>Document</h1><p>Content &amp; more &entity;.</p></body></html>"));

            Assert.Same(Encoding.UTF8, hdoc.Encoding);

            // Document with one root
            Assert.Equal(1, hdoc.Nodes().Count());
            Assert.Equal("html", hdoc.Root.Name);

            // Root with one body
            Assert.Equal(1, hdoc.Root.Nodes().Count());
            HElement body = hdoc.Root.FirstNode as HElement;

            Assert.NotNull(body);
            Assert.Equal("body", body.Name);

            // Body contains two elements
            Assert.Equal(2, body.Nodes().Count());
            var elms = body.Elements().ToArray();

            Assert.Equal(2, elms.Length);

            // First h1
            Assert.Equal("h1", elms[0].Name);
            Assert.Equal(1, elms[0].Nodes().Count());
            Assert.IsType <HText>(elms[0].FirstNode);
            Assert.Equal("Document", ((HText)elms[0].FirstNode).Value);

            // Second p
            Assert.Equal("p", elms[1].Name);
            Assert.Equal(1, elms[1].Nodes().Count());
            Assert.IsType <HText>(elms[1].FirstNode);
            Assert.Equal("Content & more &entity;.", ((HText)elms[1].FirstNode).Value);

            // Test from stream
            using (var ms = new MemoryStream(Encoding.ASCII.GetBytes("<html><body><h1>Document</h1><p>Content &amp; more &entity;.</p></body></html>")))
            {
                serializer.RemoveUnknownOrInvalidEntities = true;
                hdoc = serializer.DeserializeDocument(new StreamReader(ms, Encoding.ASCII));
                Assert.Same(Encoding.ASCII, hdoc.Encoding);
                Assert.Equal(6, hdoc.DescendantNodes().Count());
                Assert.Equal(0, hdoc.ParseErrors.Length);

                HElement elm = hdoc.Root.Elements("body").First().Elements().Last();
                Assert.Equal("p", elm.Name);
                Assert.Equal(1, elm.Nodes().Count());
                Assert.IsType <HText>(elm.FirstNode);
                Assert.Equal("Content & more .", ((HText)elm.FirstNode).Value);
            }

            Assert.Throws <ArgumentNullException>(() => serializer.DeserializeDocument(null));
        }
コード例 #9
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestBeforeElement()
        {
            var element1  = new HElement("h1");
            var element2  = new HElement("h2");
            var container = new HElement("div", element1, element2).AddClass("container");

            element1.Before(new HElement("p"));

            Assert.Equal(new String[] { "p", "h1", "h2" }, container.Elements().Select(e => e.Name));

            var ex = Assert.Throws <InvalidOperationException>(() => container.Before(new HElement("p")));

            Assert.Equal("No parent found.", ex.Message);
        }
コード例 #10
0
        public void TestDeserializeDocument_WithErrors()
        {
            var serializer = new HSerializer();

            var hdoc = serializer.DeserializeDocument(new StringReader("<html><body><h1>Document</h1><p class=>Content &amp; more.</p></body></html>"));

            Assert.Same(Encoding.UTF8, hdoc.Encoding);

            // Document with one root
            Assert.Equal(1, hdoc.Nodes().Count());
            Assert.Equal("html", hdoc.Root.Name);

            // Root with one body
            Assert.Equal(1, hdoc.Root.Nodes().Count());
            HElement body = hdoc.Root.FirstNode as HElement;

            Assert.NotNull(body);
            Assert.Equal("body", body.Name);

            // Body contains two elements
            Assert.Equal(2, body.Nodes().Count());
            var elms = body.Elements().ToArray();

            Assert.Equal(2, elms.Length);

            // First h1
            Assert.Equal("h1", elms[0].Name);
            Assert.Equal(1, elms[0].Nodes().Count());
            Assert.IsType <HText>(elms[0].FirstNode);
            Assert.Equal("Document", ((HText)elms[0].FirstNode).Value);

            // Second p
            Assert.Equal("p", elms[1].Name);
            Assert.Equal(1, elms[1].Nodes().Count());
            Assert.IsType <HText>(elms[1].FirstNode);
            Assert.Equal("Content & more.", ((HText)elms[1].FirstNode).Value);

            // Check errors
            Assert.Equal(1, hdoc.ParseErrors.Length);
            Assert.Equal("Attribute value expected.", hdoc.ParseErrors[0].Message);
        }