示例#1
0
 public static void NodesBeforeSelfBeforeAndAfter()
 {
     XText aText = new XText("a"), bText = new XText("b");
     XElement a = new XElement("A", aText, bText);
     IEnumerable<XNode> nodes = bText.NodesBeforeSelf();
     Assert.Equal(1, nodes.Count());
     aText.Remove();
     Assert.Equal(0, nodes.Count());
 }
示例#2
0
        public static void NodesBeforeSelfBeforeAndAfter()
        {
            XText               aText = new XText("a"), bText = new XText("b");
            XElement            a     = new XElement("A", aText, bText);
            IEnumerable <XNode> nodes = bText.NodesBeforeSelf();

            Assert.Equal(1, nodes.Count());
            aText.Remove();
            Assert.Equal(0, nodes.Count());
        }
示例#3
0
 public static void AncestorsWithXNameBeforeAndAfter()
 {
     XText aText = new XText("a"), bText = new XText("b");
     XElement a = new XElement("A", aText), b = new XElement("B", bText);
     a.Add(b);
     IEnumerable<XElement> nodes = bText.Ancestors("B");
     Assert.Equal(1, nodes.Count());
     bText.Remove(); a.Add(bText);
     Assert.Equal(0, nodes.Count());
 }
示例#4
0
        public static void AncestorsWithXNameBeforeAndAfter()
        {
            XText    aText = new XText("a"), bText = new XText("b");
            XElement a = new XElement("A", aText), b = new XElement("B", bText);

            a.Add(b);
            IEnumerable <XElement> nodes = bText.Ancestors("B");

            Assert.Equal(1, nodes.Count());
            bText.Remove(); a.Add(bText);
            Assert.Equal(0, nodes.Count());
        }
示例#5
0
        public static void RemoveIndented(this XNode element)
        {
            XText  previousNode = element.PreviousNode as XText;
            XText  nextNode     = element.NextNode as XText;
            string str          = element.ComputeOneLevelOfIndentation();

            element.Remove();
            if ((nextNode != null) && nextNode.IsWhiteSpace())
            {
                nextNode.Remove();
            }
            if (!element.ElementsAfterSelf().Any <XElement>() && ((previousNode != null) && previousNode.IsWhiteSpace()))
            {
                previousNode.Value = previousNode.Value.Substring(0, previousNode.Value.Length - str.Length);
            }
        }
示例#6
0
        public static void RemoveIndented(this XNode element)
        {
            // NOTE: this method is tested by BindinRedirectManagerTest and SettingsTest
            XText  textBeforeOrNull = element.PreviousNode as XText;
            XText  textAfterOrNull  = element.NextNode as XText;
            string oneIndentLevel   = element.ComputeOneLevelOfIndentation();
            bool   isLastChild      = !element.ElementsAfterSelf().Any();

            element.Remove();

            if (textAfterOrNull != null && textAfterOrNull.IsWhiteSpace())
            {
                textAfterOrNull.Remove();
            }

            if (isLastChild && textBeforeOrNull != null && textBeforeOrNull.IsWhiteSpace())
            {
                textBeforeOrNull.Value = textBeforeOrNull.Value.Substring(0, textBeforeOrNull.Value.Length - oneIndentLevel.Length);
            }
        }
示例#7
0
        /// <summary>
        /// Convert the whitespace adjacent to a node.
        /// </summary>
        /// <param name="node">The node to convert.</param>
        /// <param name="level">The depth level of the node.</param>
        private void ConvertWhitespace(XNode node, int level)
        {
            // Fix the whitespace before this node.
            XText whitespace = node.PreviousNode as XText;

            if (null != whitespace)
            {
                if (XmlNodeType.CDATA == node.NodeType)
                {
                    if (this.OnError(ConverterTestType.WhitespacePrecedingCDATAWrong, node, "There should be no whitespace preceding a CDATA node."))
                    {
                        whitespace.Remove();
                    }
                }
                else
                {
                    if (!Converter.IsLegalWhitespace(this.IndentationAmount, level, whitespace.Value))
                    {
                        if (this.OnError(ConverterTestType.WhitespacePrecedingNodeWrong, node, "The whitespace preceding this node is incorrect."))
                        {
                            Converter.FixWhitespace(this.IndentationAmount, level, whitespace);
                        }
                    }
                }
            }

            // Fix the whitespace after CDATA nodes.
            XCData cdata = node as XCData;

            if (null != cdata)
            {
                whitespace = cdata.NextNode as XText;

                if (null != whitespace)
                {
                    if (this.OnError(ConverterTestType.WhitespaceFollowingCDATAWrong, node, "There should be no whitespace following a CDATA node."))
                    {
                        whitespace.Remove();
                    }
                }
            }
            else
            {
                // Fix the whitespace inside and after this node (except for Error which may contain just whitespace).
                XElement element = node as XElement;

                if (null != element && "Error" != element.Name.LocalName)
                {
                    if (!element.HasElements && !element.IsEmpty && String.IsNullOrEmpty(element.Value.Trim()))
                    {
                        if (this.OnError(ConverterTestType.NotEmptyElement, element, "This should be an empty element since it contains nothing but whitespace."))
                        {
                            element.RemoveNodes();
                        }
                    }

                    whitespace = node.NextNode as XText;

                    if (null != whitespace)
                    {
                        if (!Converter.IsLegalWhitespace(this.IndentationAmount, level - 1, whitespace.Value))
                        {
                            if (this.OnError(ConverterTestType.WhitespacePrecedingEndElementWrong, whitespace, "The whitespace preceding this end element is incorrect."))
                            {
                                Converter.FixWhitespace(this.IndentationAmount, level - 1, whitespace);
                            }
                        }
                    }
                }
            }
        }