public void MutationObserverShouldHaveConstructor()
        {
            var name = "MutationObserver";
            var members = new[] { "observe", "disconnect", "takeRecords" };
            var binding = GeneralExtensions.GetBindings(new Dictionary<String, List<Type>>
            {
                { name, ListOf(typeof(MutationObserver)) }
            }).First() as BindingClass;
            Assert.AreEqual(name, binding.Name);
            Assert.AreEqual(1, binding.Constructors.Count());
            Assert.AreEqual(0, binding.Deleters.Count());
            Assert.AreEqual(members.Length, binding.Members.Count());
            
            for (int i = 0; i < members.Length; i++)
			{
                Assert.IsTrue(binding.Members.Any(m => m.Key == members[i]));
			}
        }
        public void IElementShouldContainMembersFromParentNode()
        {
            var name = "Element";
            var members = new[] 
            { 
                "childElementCount", "children", "firstElementChild", "lastElementChild", 
                "append", "prepend", "querySelector", "querySelectorAll" 
            };
            var binding = GeneralExtensions.GetBindings(new Dictionary<String, List<Type>>
            {
                { name, ListOf(typeof(IElement)) }
            }).First() as BindingClass;
            Assert.AreEqual(name, binding.Name);
            Assert.AreEqual(0, binding.Constructors.Count());
            Assert.AreEqual(0, binding.Deleters.Count());
            Assert.AreEqual(0, binding.Getters.Count());
            Assert.AreEqual(0, binding.Setters.Count());

            for (int i = 0; i < members.Length; i++)
            {
                Assert.IsTrue(binding.Members.Any(m => m.Key == members[i]));
            }
        }
Beispiel #3
0
        public void HtmlLiveCollectionCompleteDOMRebuildWithInnerHtml()
        {
            var document = Html("<p><p><p><p><p>");

            var body = document.Body;
            Assert.IsNotNull(body);
            Assert.AreEqual(5, body.ChildNodes.Length);

            var live = body.Children;
            Assert.AreEqual(5, live.Length);

            foreach (var child in live)
            {
                Assert.AreEqual("p", child.GetTagName());
                Assert.AreEqual(0, child.ChildNodes.Length);
                Assert.AreEqual(0, child.Attributes.Count());
                Assert.AreEqual(NodeType.Element, child.NodeType);
                Assert.AreEqual("", child.TextContent);
            }

            body.InnerHtml = "<p>First<p>Second<p>Third";
            Assert.AreEqual(3, body.ChildNodes.Length);
            Assert.AreEqual(3, live.Length);

            var i = 0;
            var str = new[] { "First", "Second", "Third" };

            foreach (var child in live)
            {
                Assert.AreEqual("p", child.GetTagName());
                Assert.AreEqual(1, child.ChildNodes.Length);
                Assert.AreEqual(0, child.Attributes.Count());
                Assert.AreEqual(NodeType.Element, child.NodeType);
                Assert.AreEqual(str[i++], child.TextContent);
            }
        }
Beispiel #4
0
        public void RemoveEventForNotAllowedUrlAtStyle()
        {
            var allowedTags = new[] { "a" };
            var allowedAttributes = new[] { "style" };
            var allowedStyles = new[] { "background" };
            RemoveReason? actual = null;

            var s = new HtmlSanitizer(allowedTags: allowedTags, allowedAttributes: allowedAttributes, allowedCssProperties: allowedStyles);
            s.RemovingStyle += (sender, args) =>
            {
                actual = args.Reason;
            };

            s.Sanitize("<a style=\"background:url(javascript:alert('xss'))\">just any content</a>");

            Assert.That(actual, Is.EqualTo(RemoveReason.NotAllowedUrlValue));
        }
Beispiel #5
0
        public void RemoveEventForNotAllowedUrlAtUriAttribute()
        {
            var allowedTags = new[] { "a" };
            var allowedAttributes = new[] { "href" };
            var uriAttributes = new[] { "href" };
            RemoveReason? actual = null;

            var s = new HtmlSanitizer(allowedTags: allowedTags, allowedAttributes: allowedAttributes, uriAttributes: uriAttributes);
            s.RemovingAttribute += (sender, args) =>
            {
                actual = args.Reason;
            };

            s.Sanitize("<a href=\"javascript:(alert('xss'))\">just any content</a>");

            Assert.That(actual, Is.EqualTo(RemoveReason.NotAllowedUrlValue));
        }
Beispiel #6
0
        public void RemoveEventForNotAllowedValueAtAttribute()
        {
            var allowedTags = new[] { "a" };
            var allowedAttributes = new[] { "id" };
            RemoveReason? actual = null;

            var s = new HtmlSanitizer(allowedTags: allowedTags, allowedAttributes: allowedAttributes);
            s.RemovingAttribute += (sender, args) =>
            {
                actual = args.Reason;
            };

            s.Sanitize("<a id=\"anyId&{\">just any content</a>");

            Assert.That(actual, Is.EqualTo(RemoveReason.NotAllowedValue));
        }
Beispiel #7
0
        public void RemoveEventForNotAllowedStyle()
        {
            var allowedTags = new[] { "a" };
            var allowedAttributes = new[] { "style" };
            var allowedStyles = new[] { "margin" };
            RemoveReason? actual = null;

            var s = new HtmlSanitizer(allowedTags: allowedTags, allowedAttributes: allowedAttributes, allowedCssProperties: allowedStyles);
            s.RemovingStyle += (sender, args) =>
            {
                actual = args.Reason;
            };

            s.Sanitize("<a style=\"padding:5px\">just any content</a>");

            Assert.That(actual, Is.EqualTo(RemoveReason.NotAllowedStyle));
        }
Beispiel #8
0
        public void RemoveEventForNotAllowedTag()
        {
            var allowedTags = new[] {"a"};
            RemoveReason? actual = null;

            var s = new HtmlSanitizer(allowedTags);
            s.RemovingTag += (sender, args) =>
            {
                actual = args.Reason;
            };

            s.Sanitize("<span>just any content</span>");

            Assert.That(actual, Is.EqualTo(RemoveReason.NotAllowedTag));
        }
Beispiel #9
0
        public void ChildrenOfTableAndVariousSections()
        {
            var document = Html("");
            var table = document.CreateElement("table");
            var orphan1 = table.AppendChild(document.CreateElement("tr")) as IHtmlTableRowElement;
            orphan1.Id = "orphan1";
            var foot1 = table.AppendChild(document.CreateElement("tfoot"));
            var orphan2 = table.AppendChild(document.CreateElement("tr")) as IHtmlTableRowElement;
            orphan2.Id = "orphan2";
            var foot2 = table.AppendChild(document.CreateElement("tfoot"));
            var orphan3 = table.AppendChild(document.CreateElement("tr")) as IHtmlTableRowElement;
            orphan3.Id = "orphan3";
            var body1 = table.AppendChild(document.CreateElement("tbody"));
            var orphan4 = table.AppendChild(document.CreateElement("tr")) as IHtmlTableRowElement;
            orphan4.Id = "orphan4";
            var body2 = table.AppendChild(document.CreateElement("tbody"));
            var orphan5 = table.AppendChild(document.CreateElement("tr")) as IHtmlTableRowElement;
            orphan5.Id = "orphan5";
            var head1 = table.AppendChild(document.CreateElement("thead"));
            var orphan6 = table.AppendChild(document.CreateElement("tr")) as IHtmlTableRowElement;
            orphan6.Id = "orphan6";
            var head2 = table.AppendChild(document.CreateElement("thead"));
            var orphan7 = table.AppendChild(document.CreateElement("tr")) as IHtmlTableRowElement;
            orphan7.Id = "orphan7";
            var foot1row1 = foot1.AppendChild(document.CreateElement("tr")) as IHtmlTableRowElement;
            foot1row1.Id = "foot1row1";
            var foot1row2 = foot1.AppendChild(document.CreateElement("tr")) as IHtmlTableRowElement;
            foot1row2.Id = "foot1row2";
            var foot2row1 = foot2.AppendChild(document.CreateElement("tr")) as IHtmlTableRowElement;
            foot2row1.Id = "foot2row1";
            var foot2row2 = foot2.AppendChild(document.CreateElement("tr")) as IHtmlTableRowElement;
            foot2row2.Id = "foot2row2";
            var body1row1 = body1.AppendChild(document.CreateElement("tr")) as IHtmlTableRowElement;
            body1row1.Id = "body1row1";
            var body1row2 = body1.AppendChild(document.CreateElement("tr")) as IHtmlTableRowElement;
            body1row2.Id = "body1row2";
            var body2row1 = body2.AppendChild(document.CreateElement("tr")) as IHtmlTableRowElement;
            body2row1.Id = "body2row1";
            var body2row2 = body2.AppendChild(document.CreateElement("tr")) as IHtmlTableRowElement;
            body2row2.Id = "body2row2";
            var head1row1 = head1.AppendChild(document.CreateElement("tr")) as IHtmlTableRowElement;
            head1row1.Id = "head1row1";
            var head1row2 = head1.AppendChild(document.CreateElement("tr")) as IHtmlTableRowElement;
            head1row2.Id = "head1row2";
            var head2row1 = head2.AppendChild(document.CreateElement("tr")) as IHtmlTableRowElement;
            head2row1.Id = "head2row1";
            var head2row2 = head2.AppendChild(document.CreateElement("tr")) as IHtmlTableRowElement;
            head2row2.Id = "head2row2";

            // These elements should not end up in any collection.
            table.AppendChild(document.CreateElement("div"))
                 .AppendChild(document.CreateElement("tr"));
            foot1.AppendChild(document.CreateElement("div"))
                 .AppendChild(document.CreateElement("tr"));
            body1.AppendChild(document.CreateElement("div"))
                 .AppendChild(document.CreateElement("tr"));
            head1.AppendChild(document.CreateElement("div"))
                 .AppendChild(document.CreateElement("tr"));
            table.AppendChild(document.CreateElement("http://example.com/test", "tr"));
            foot1.AppendChild(document.CreateElement("http://example.com/test", "tr"));
            body1.AppendChild(document.CreateElement("http://example.com/test", "tr"));
            head1.AppendChild(document.CreateElement("http://example.com/test", "tr"));

            var rows = (table as IHtmlTableElement).Rows;
            Assert.IsNotNull(rows);

            CollectionAssert.AreEqual(new IHtmlTableRowElement[] {
                // thead
                head1row1,
                head1row2,
                head2row1,
                head2row2,
                // tbody + table
                orphan1,
                orphan2,
                orphan3,
                body1row1,
                body1row2,
                orphan4,
                body2row1,
                body2row2,
                orphan5,
                orphan6,
                orphan7,
                // tfoot
                foot1row1,
                foot1row2,
                foot2row1,
                foot2row2
            }, rows.ToArray());

            var ids = new[] {
                "orphan1",
                "orphan2",
                "orphan3",
                "orphan4",
                "orphan5",
                "orphan6",
                "orphan7",
                "foot1row1",
                "foot1row2",
                "foot2row1",
                "foot2row2",
                "body1row1",
                "body1row2",
                "body2row1",
                "body2row2",
                "head1row1",
                "head1row2",
                "head2row1",
                "head2row2"
            };

            foreach (var id in ids)
                Assert.AreEqual(id, rows[id].Id);

            while (table.FirstChild != null)
                table.RemoveChild(table.FirstChild);

            foreach (var id in ids)
                Assert.IsNull(rows[id]);
        }
        public void NodeFilterShouldHaveCorrectNameAndLocations()
        {
            var name = "NodeFilter";
            var fields = new[] 
            { 
                "SHOW_ELEMENT", "SHOW_ATTRIBUTE", "SHOW_TEXT", "SHOW_CDATA_SECTION", 
                "SHOW_ENTITY_REFERENCE", "SHOW_ENTITY", "SHOW_PROCESSING_INSTRUCTION", "SHOW_COMMENT" ,
                "SHOW_DOCUMENT", "SHOW_DOCUMENT_TYPE", "SHOW_DOCUMENT_FRAGMENT", "SHOW_NOTATION", "SHOW_ALL"
            };
            var binding = GeneralExtensions.GetBindings(new Dictionary<String, List<Type>>
            {
                { name, ListOf(typeof(FilterSettings)) }
            }).First();
            var bindingFields = binding.GetAll<BindingField>();
            Assert.AreEqual(name, binding.Name);
            Assert.AreEqual(fields.Length, bindingFields.Count());
            Assert.AreEqual(fields.Length, binding.GetMembers().Count());

            for (int i = 0; i < fields.Length; i++)
            {
                Assert.IsTrue(bindingFields.Any(m => m.Key == fields[i]));
            }
        }