Beispiel #1
0
        public object splitText(int offset)
        {
            if (offset < 0 || offset > XmlText.Length)
            {
                return(false);
            }

            return(XmlText.SplitText(offset));
        }
Beispiel #2
0
        /// <summary>
        /// Replace words in a document.
        /// </summary>
        /// <param name="wordlist">List of words to run replacement function for.</param>
        /// <param name="handler">Word replacement delegate.</param>
        public static void ReplaceText(XDocWord[] wordlist, ReplacementHandler handler)
        {
            if (wordlist == null)
            {
                throw new ArgumentNullException("wordlist");
            }
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }

            // loop through words backwards
            for (int i = wordlist.Length - 1; i >= 0; --i)
            {
                XDocWord word = wordlist[i];
                if (word.IsText)
                {
                    XmlNode replacement = handler(word.Node.OwnerDocument, word);
                    if (replacement != null)
                    {
                        // split the node
                        XmlText node = (XmlText)word.Node;

                        // split off the part after the text
                        if (node.Value.Length > (word.Offset + word.Value.Length))
                        {
                            node.SplitText(word.Offset + word.Value.Length);
                        }

                        // split off the part before the text
                        if (word.Offset > 0)
                        {
                            node = node.SplitText(word.Offset);
                        }

                        // replace text with new result
                        node.ParentNode.InsertAfter(replacement, node);
                        node.ParentNode.RemoveChild(node);
                    }
                }
            }
        }
Beispiel #3
0
        public void SplitText()
        {
            document.LoadXml("<root>test text.</root>");
            document.NodeInserted += new XmlNodeChangedEventHandler(EventNodeInserted);
            document.NodeChanged  += new XmlNodeChangedEventHandler(EventNodeChanged);
            document.NodeRemoved  += new XmlNodeChangedEventHandler(EventNodeRemoved);
            XmlText t = document.DocumentElement.FirstChild as XmlText;

            t.SplitText(5);
            Assert.IsNotNull(t.NextSibling);
            Assert.AreEqual("test ", t.Value);
            Assert.AreEqual("text.", t.NextSibling.Value);
            Assert.IsTrue(changed);
            Assert.IsTrue(inserted);
            Assert.IsTrue(!removed);
        }
Beispiel #4
0
        public object splitText(int offset)
        {
            if (offset < 0 || offset > this.dataLengthImpl)
            {
                return(false);
            }

            if (IsAssociated)
            {
                return(XmlText.SplitText(offset));
            }
            else
            {
                return(CreateDOMText(dataImpl.Substring(offset)));
            }
        }
Beispiel #5
0
    // Test the splitting of text nodes.
    public void TestXmlTextSplitText()
    {
        // Perform simple splits at various points.
        XmlAttribute attr = doc.CreateAttribute("foo");
        XmlText      text = doc.CreateTextNode("hello and goodbye");

        RegisterWatchNeither(text);
        attr.AppendChild(text);
        XmlText split = text.SplitText(6);

        CheckForChangeNeither();
        Assert("SplitText (1)", text != split);
        AssertEquals("SplitText (2)", "hello ", text.Value);
        AssertEquals("SplitText (3)", "and goodbye", split.Value);

        attr = doc.CreateAttribute("foo");
        text = doc.CreateTextNode("hello and goodbye");
        attr.AppendChild(text);
        split = text.SplitText(-1);
        Assert("SplitText (4)", text != split);
        AssertEquals("SplitText (5)", "", text.Value);
        AssertEquals("SplitText (6)", "hello and goodbye", split.Value);

        attr = doc.CreateAttribute("foo");
        text = doc.CreateTextNode("hello and goodbye");
        attr.AppendChild(text);
        split = text.SplitText(100);
        Assert("SplitText (7)", text != split);
        AssertEquals("SplitText (8)", "hello and goodbye", text.Value);
        AssertEquals("SplitText (9)", "", split.Value);

        // Test event handling.
        attr = doc.CreateAttribute("foo");
        text = doc.CreateTextNode("hello and goodbye");
        attr.AppendChild(text);
        RegisterWatchBoth(text);
        text.SplitText(6);
        CheckForChangeBoth();

        attr = doc.CreateAttribute("foo");
        text = doc.CreateTextNode("hello and goodbye");
        attr.AppendChild(text);
        RegisterWatchBefore(text);
        text.SplitText(6);
        CheckForChangeBefore();

        attr = doc.CreateAttribute("foo");
        text = doc.CreateTextNode("hello and goodbye");
        attr.AppendChild(text);
        RegisterWatchAfter(text);
        text.SplitText(6);
        CheckForChangeAfter();
    }