Example #1
0
        public void ConnectMutationObserverAttributesTriggerManually()
        {
            var called = false;
            var attrName = "something";
            var attrValue = "test";

            var observer = new MutationObserver((mut, obs) =>
            {
                called = true;
                Assert.AreEqual(1, mut.Length);
                Assert.AreEqual(attrName, mut[0].AttributeName);
                Assert.IsNull(mut[0].PreviousValue);
            });

            var document = Html("");

            observer.Connect(document.Body, new MutationObserverInit
            {
                IsObservingAttributes = true
            });

            document.Body.SetAttribute(attrName, attrValue);
            observer.TriggerWith(observer.Flush().ToArray());
            Assert.IsTrue(called);
        }
Example #2
0
        public void ConnectMutationObserverChildNodesTriggerManually()
        {
            var called = false;

            var observer = new MutationObserver((mut, obs) =>
            {
                called = true;
                Assert.AreEqual(1, mut.Length);
                var record = mut[0];
                Assert.IsNotNull(record.Added);
                Assert.AreEqual(1, record.Added.Length);
            });

            var document = Html("");

            observer.Connect(document.Body, childList: true);
            document.Body.AppendChild(document.CreateElement("span"));
            observer.TriggerWith(observer.Flush().ToArray());
            Assert.IsTrue(called);
        }
Example #3
0
        public void ConnectMutationObserverMultipleAttributesDescendentTriggerManually()
        {
            var called1 = false;
            var called2 = false;
            var called3 = false;
            var attrName = "something";
            var attrValue = "test";

            var document = Html("");

            var observer1 = new MutationObserver((mut, obs) =>
            {
                called1 = true;
                Assert.AreEqual(1, mut.Length);
            });

            observer1.Connect(document.DocumentElement, new MutationObserverInit
            {
                IsObservingAttributes = true,
                IsObservingSubtree = true
            });

            var observer2 = new MutationObserver((mut, obs) =>
            {
                called2 = true;
                Assert.AreEqual(0, mut.Length);
            });

            observer2.Connect(document.DocumentElement, new MutationObserverInit
            {
                IsObservingAttributes = true,
                IsObservingSubtree = false
            });

            var observer3 = new MutationObserver((mut, obs) =>
            {
                called3 = true;
                Assert.AreEqual(1, mut.Length);
            });

            observer3.Connect(document.Body, new MutationObserverInit
            {
                IsObservingAttributes = true
            });

            document.Body.SetAttribute(attrName, attrValue);
            observer1.TriggerWith(observer1.Flush().ToArray());
            observer2.TriggerWith(observer2.Flush().ToArray());
            observer3.TriggerWith(observer3.Flush().ToArray());
            Assert.IsTrue(called1);
            Assert.IsTrue(called2);
            Assert.IsTrue(called3);
        }
Example #4
0
        public void ConnectMutationObserverTextNoDescendentsButCreatedTriggerManually()
        {
            var called = false;
            var text = "something";

            var observer = new MutationObserver((mut, obs) =>
            {
                called = true;
                Assert.AreEqual(1, mut.Length);
                Assert.AreEqual(1, mut[0].Added.Length);
                Assert.AreEqual(text, mut[0].Added[0].TextContent);
            });

            var document = Html("");

            observer.Connect(document.Body, new MutationObserverInit
            {
                IsObservingSubtree = false,
                IsObservingChildNodes = true
            });

            document.Body.TextContent = text;
            observer.TriggerWith(observer.Flush().ToArray());
            Assert.IsTrue(called);
        }
Example #5
0
        public void ConnectMutationObserverTextNoDescendentsTriggerManually()
        {
            var called = false;
            var text = "something";
            var replaced = "different";

            var observer = new MutationObserver((mut, obs) =>
            {
                called = true;
                Assert.AreEqual(0, mut.Length);
            });

            var document = Html("");

            observer.Connect(document.Body, new MutationObserverInit
            {
                IsObservingCharacterData = true,
                IsObservingSubtree = false
            });

            document.Body.TextContent = text;
            var textNode = document.Body.ChildNodes[0] as TextNode;
            textNode.Replace(text.Length, 0, replaced);
            observer.TriggerWith(observer.Flush().ToArray());
            Assert.IsTrue(called);
        }
Example #6
0
        public void ConnectMutationObserverTextWithDescendentsAndExaminingOldValueTriggerManually()
        {
            var called = false;
            var text = "something";
            var replaced = "different";

            var observer = new MutationObserver((mut, obs) =>
            {
                called = true;
                Assert.AreEqual(1, mut.Length);
                Assert.AreEqual(text, mut[0].PreviousValue);
                var tn = mut[0].Target as TextNode;
                Assert.IsNotNull(tn);
                Assert.AreEqual(text + replaced, tn.TextContent);
            });

            var document = Html("");

            observer.Connect(document.Body, new MutationObserverInit
            {
                IsObservingCharacterData = true,
                IsObservingSubtree = true,
                IsExaminingOldCharacterData = true
            });

            document.Body.TextContent = text;
            var textNode = document.Body.ChildNodes[0] as TextNode;
            textNode.Replace(text.Length, 0, replaced);
            observer.TriggerWith(observer.Flush().ToArray());
            Assert.IsTrue(called);
        }