Esempio n. 1
0
        private XamlFormatter.XmlElementFormatting GetElementFormatting(ElementNode elementNode)
        {
            XamlFormatter.XmlElementFormatting elementFormatting = new XamlFormatter.XmlElementFormatting();
            XmlElementReference elementReference = elementNode.SourceContextReference.SourceContext as XmlElementReference;

            if (elementReference != null && elementReference.TextRange != null)
            {
                IReadableTextBuffer textBuffer = (IReadableTextBuffer)TextBufferHelper.GetHostBuffer(elementReference.TextBuffer);
                elementFormatting.LeadingWhitespace          = XamlFormatter.GetLeadingWhitespace((XamlSourceContext)elementReference);
                elementFormatting.TrailingWhitespace         = XamlFormatter.GetTrailingWhitespace((XamlSourceContext)elementReference);
                elementFormatting.AttributeLeadingWhitespace = XamlFormatter.GetTrailingWhitespace(textBuffer, elementReference.TextRange.Offset);
                if (elementReference.StartTagRange != null)
                {
                    elementFormatting.AttributeTrailingWhitespace = XamlFormatter.GetLeadingWhitespace(textBuffer, elementReference.StartTagRange.Offset + elementReference.StartTagRange.Length - 1);
                    elementFormatting.ContentLeadingWhitespace    = XamlFormatter.GetTrailingWhitespace(textBuffer, elementReference.StartTagRange.Offset + elementReference.StartTagRange.Length);
                    int num = elementReference.TextRange.Offset + elementReference.TextRange.Length;
                    elementFormatting.ContentTrailingWhitespace = XamlFormatter.GetLeadingWhitespace(textBuffer, num - (elementNode.Name.FullName.Length + 3));
                }
                else
                {
                    elementFormatting.AttributeTrailingWhitespace = XamlFormatter.GetLeadingWhitespace(textBuffer, elementReference.TextRange.Offset + elementReference.TextRange.Length - 2);
                    elementFormatting.ContentLeadingWhitespace    = (string)null;
                    elementFormatting.ContentTrailingWhitespace   = (string)null;
                }
            }
            return(elementFormatting);
        }
Esempio n. 2
0
        public override INodeSourceContext Clone(bool keepOldRanges)
        {
            XmlElementReference elementReference = new XmlElementReference(this.Parent, new SourceContext(), new SourceContext(), false);

            this.CloneCopy((XamlSourceContext)elementReference, keepOldRanges);
            return((INodeSourceContext)elementReference);
        }
        public virtual XmlAttributeReference BuildXmlAttributeReference()
        {
            XmlAttributeReference attributeReference1 = this.SourceContext as XmlAttributeReference;
            XmlElementReference   parent = (XmlElementReference)null;

            if (this.documentNode != null)
            {
                for (DocumentNode documentNode = (DocumentNode)this.documentNode.Parent; documentNode != null && parent == null; documentNode = (DocumentNode)documentNode.Parent)
                {
                    parent = documentNode.SourceContext as XmlElementReference;
                }
            }
            XmlAttributeReference attributeReference2;

            if (attributeReference1 == null)
            {
                attributeReference2 = new XmlAttributeReference(parent);
            }
            else
            {
                attributeReference2        = (XmlAttributeReference)attributeReference1.Clone(false);
                attributeReference2.Parent = (XmlContainerReference)parent;
            }
            return(attributeReference2);
        }
Esempio n. 4
0
 bool IXmlNamespaceResolver.IsIgnorable(XmlNamespace xmlNamespace)
 {
     for (XmlElementReference elementReference = this; elementReference != null; elementReference = elementReference.Parent as XmlElementReference)
     {
         if (elementReference.ignorableNamespaces != null && elementReference.ignorableNamespaces.Contains(xmlNamespace))
         {
             return(true);
         }
     }
     return(false);
 }
Esempio n. 5
0
 XmlNamespace IXmlNamespaceResolver.GetXmlNamespace(XmlnsPrefix prefix, XmlNamespaceCanonicalization canonicalization)
 {
     for (XmlElementReference elementReference = this; elementReference != null; elementReference = elementReference.Parent as XmlElementReference)
     {
         foreach (XmlElementReference.Attribute attribute in elementReference.AttributesToPreserve)
         {
             if ((attribute.Type == XmlElementReference.AttributeType.Xmlns || attribute.Type == XmlElementReference.AttributeType.SerializerAddedXmlns) && string.Equals(string.IsNullOrEmpty(attribute.XmlAttribute.Prefix) ? attribute.XmlAttribute.Prefix : attribute.XmlAttribute.LocalName, prefix.Value, StringComparison.Ordinal))
             {
                 return(XmlNamespace.ToNamespace(attribute.XmlAttribute.Value.Value, canonicalization));
             }
         }
     }
     return((XmlNamespace)null);
 }
Esempio n. 6
0
        internal override void CloneCopy(XamlSourceContext other, bool keepOldRanges)
        {
            base.CloneCopy(other, keepOldRanges);
            XmlElementReference elementReference = (XmlElementReference)other;

            elementReference.comment = this.Comment == null ? (XmlContainerReference.ChildNode)null : new XmlContainerReference.ChildNode(this.Comment);
            elementReference.attributesToPreserve = this.attributesToPreserve == null ? (IList <XmlElementReference.Attribute>)null : (IList <XmlElementReference.Attribute>) new List <XmlElementReference.Attribute>((IEnumerable <XmlElementReference.Attribute>) this.attributesToPreserve);
            elementReference.ignorableNamespaces  = this.ignorableNamespaces == null ? (IList <XmlNamespace>)null : (IList <XmlNamespace>) new List <XmlNamespace>((IEnumerable <XmlNamespace>) this.ignorableNamespaces);
            elementReference.xmlSpace             = this.xmlSpace;
            elementReference.initialStartTagSpan  = this.initialStartTagSpan;
            if (!keepOldRanges)
            {
                return;
            }
            elementReference.startTagRange = this.startTagRange;
        }
Esempio n. 7
0
        private static void ClearSourceContextInitialRange(INodeSourceContext sourceContext)
        {
            XamlSourceContext xamlSourceContext = sourceContext as XamlSourceContext;

            if (xamlSourceContext == null)
            {
                return;
            }
            xamlSourceContext.InitialTextSpan = TextRange.Null;
            XmlElementReference elementReference = xamlSourceContext as XmlElementReference;

            if (elementReference == null)
            {
                return;
            }
            elementReference.InitialStartTagSpan = TextRange.Null;
        }
 public RemovedXmlElementReference(XmlElementReference originalReference, bool isClone)
     : base(originalReference.Parent, new SourceContext(), new SourceContext(), false)
 {
     originalReference.CloneCopy((XamlSourceContext)this, false);
     this.textBuffer = originalReference.TextBuffer.Freeze();
     if (this.Comment != null && this.Comment.SourceContext != null)
     {
         this.Comment.SourceContext.FreezeText(isClone);
     }
     if (originalReference.TextRange != null)
     {
         this.TextRange = this.textBuffer.FreezeRange(originalReference.TextRange);
     }
     if (originalReference.StartTagRange != null)
     {
         this.StartTagRange = this.textBuffer.FreezeRange(originalReference.StartTagRange);
     }
     this.isCloned = isClone;
 }
Esempio n. 9
0
        private static void UpdateSourceContextRangeFromInitialRange(IReadableSelectableTextBuffer textBuffer, INodeSourceContext context, int offset)
        {
            XamlSourceContext xamlSourceContext = context as XamlSourceContext;

            if (xamlSourceContext == null)
            {
                return;
            }
            if (!TextRange.IsNull(xamlSourceContext.InitialTextSpan))
            {
                ITextRange initialTextSpan = xamlSourceContext.InitialTextSpan;
                ITextRange textRange1      = (ITextRange) new TextRange(initialTextSpan.Offset + offset, initialTextSpan.Offset + initialTextSpan.Length + offset);
                xamlSourceContext.InitialTextSpan = textRange1;
                xamlSourceContext.SetTextRange(textBuffer, textRange1.Offset, textRange1.Length);
                XmlElementReference elementReference = xamlSourceContext as XmlElementReference;
                if (elementReference == null)
                {
                    return;
                }
                foreach (XmlElementReference.Attribute attribute in elementReference.AttributesToPreserve)
                {
                    XamlSerializerUtilities.UpdateSourceContextRangeFromInitialRange(textBuffer, (INodeSourceContext)attribute.SourceContext, offset);
                }
                if (TextRange.IsNull(elementReference.InitialStartTagSpan))
                {
                    return;
                }
                ITextRange initialStartTagSpan = elementReference.InitialStartTagSpan;
                ITextRange textRange2          = (ITextRange) new TextRange(initialStartTagSpan.Offset + offset, initialStartTagSpan.Offset + initialStartTagSpan.Length + offset);
                elementReference.InitialStartTagSpan = textRange2;
                elementReference.StartTagRange       = textBuffer.CreateRange(textRange2.Offset, textRange2.Length);
            }
            else
            {
                xamlSourceContext.ClearTextRange();
                XmlElementReference elementReference = xamlSourceContext as XmlElementReference;
                if (elementReference == null)
                {
                    return;
                }
                elementReference.StartTagRange = (ITextRange)null;
            }
        }
Esempio n. 10
0
        private static void UpdateNodeSourceContextWorker(IReadableSelectableTextBuffer textBuffer, DocumentNode node, int startTagLength, int elementLength)
        {
            XmlElementReference elementReference1 = (XmlElementReference)node.SourceContext;
            XmlElementReference elementReference2 = (XmlElementReference)elementReference1.Clone(false);

            if (startTagLength != elementLength)
            {
                elementReference2.StartTagRange = textBuffer.CreateRange(elementReference1.TextRange.Offset, startTagLength);
            }
            elementReference2.SetTextRange(textBuffer, elementReference1.TextRange.Offset, elementLength);
            if (node.DocumentRoot != null)
            {
                node.DocumentRoot.SetSourceContext(node, (INodeSourceContext)elementReference2);
            }
            else
            {
                node.SourceContext = (INodeSourceContext)elementReference2;
            }
        }
Esempio n. 11
0
        private void OffsetSourceContext(INodeSourceContext context, int offset)
        {
            XamlSourceContext xamlSourceContext = context as XamlSourceContext;

            if (xamlSourceContext == null || xamlSourceContext.TextRange == null)
            {
                return;
            }
            int start = xamlSourceContext.TextRange.Offset + offset;

            xamlSourceContext.InitialTextSpan = (ITextRange) new TextRange(start, start + xamlSourceContext.TextRange.Length);
            XmlElementReference elementReference = xamlSourceContext as XmlElementReference;

            if (elementReference == null || elementReference.StartTagRange == null)
            {
                return;
            }
            elementReference.InitialStartTagSpan = (ITextRange) new TextRange(start, elementReference.StartTagRange.Length);
        }
Esempio n. 12
0
        public XmlElementReference BuildXmlElementReference()
        {
            XmlElementReference elementReference1 = this.SourceContext as XmlElementReference;
            DocumentNode        documentNode      = (DocumentNode)this.documentNode.Parent;
            XmlElementReference elementReference2;

            for (elementReference2 = (XmlElementReference)null; documentNode != null && elementReference2 == null; documentNode = (DocumentNode)documentNode.Parent)
            {
                elementReference2 = documentNode.SourceContext as XmlElementReference;
            }
            XmlElementReference elementReference3;

            if (elementReference1 == null)
            {
                if (elementReference2 == null)
                {
                    XmlDocument xmlDocument = new XmlDocument();
                    XmlElement  xmlElement  = new XmlElement(xmlDocument);
                    xmlElement.Parent = (XmlNode)xmlDocument;
                    elementReference3 = new XmlElementReference((XmlContainerReference) new XmlDocumentReference((IReadableSelectableTextBuffer)null, xmlDocument, false), xmlElement, false);
                }
                else
                {
                    elementReference3 = new XmlElementReference((XmlContainerReference)elementReference2, (XmlElement)null, false);
                }
            }
            else
            {
                elementReference3 = (XmlElementReference)elementReference1.Clone(false);
                if (elementReference2 != null)
                {
                    elementReference3.Parent = (XmlContainerReference)elementReference2;
                }
            }
            return(elementReference3);
        }
Esempio n. 13
0
 public XmlAttributeReference(XmlElementReference parent, SourceContext context)
     : base((XmlContainerReference)parent, context)
 {
 }
Esempio n. 14
0
 public XmlAttributeReference(XmlElementReference parent)
     : base((XmlContainerReference)parent)
 {
 }
Esempio n. 15
0
        private void WriteElement(string indent, ElementNode element)
        {
            ElementPersistenceSettings elementSettings = this.settings.GetElementSettings(typeof(object));

            XamlFormatter.XmlElementFormatting elementFormatting = this.GetElementFormatting(element);
            if (elementFormatting.LeadingWhitespace != null)
            {
                this.writer.SetWhitespace(elementFormatting.LeadingWhitespace);
                if (!this.writer.Started || this.writer.LineBreakCount > 0)
                {
                    if (element.IndentingBehavior == IndentingBehavior.FromContainer)
                    {
                        this.writer.SetIndent(indent);
                    }
                    else
                    {
                        indent = this.writer.Indent;
                    }
                }
            }
            else
            {
                this.writer.SetMinimumLineBreaks(elementSettings.LinesOutside);
                this.writer.SetIndent(indent);
            }
            XmlElementReference elementReference = (XmlElementReference)null;

            if (this.shouldUpdateSourceContext && element.SourceContextReference != null)
            {
                elementReference = element.SourceContextReference.BuildXmlElementReference();
                element.SourceContextReference.SourceContext = (XamlSourceContext)elementReference;
            }
            int start = this.writer.WriteBeginStartElement(element.Name.FullName);

            if (elementFormatting.AttributeLeadingWhitespace != null)
            {
                this.writer.SetWhitespace(elementFormatting.AttributeLeadingWhitespace);
            }
            string indent1 = indent + this.settings.IndentString;

            element.Attributes.EnsureOrdering();
            for (int index = 0; index < element.Attributes.Count; ++index)
            {
                this.WriteNode(indent1, element.Attributes[index]);
            }
            if (elementFormatting.AttributeTrailingWhitespace != null)
            {
                this.writer.SetWhitespace(elementFormatting.AttributeTrailingWhitespace);
            }
            int end;
            int length;

            if (element.Children.Count > 0)
            {
                bool flag = element.IsWhitespaceSignificant;
                for (int index = 0; index < element.Children.Count; ++index)
                {
                    switch (element.Children[index].NodeType)
                    {
                    case NodeType.Literal:
                    case NodeType.WhitespaceLiteral:
                        flag = true;
                        break;
                    }
                }
                this.writer.WriteEndStartElement();
                end = this.writer.Length;
                if (elementFormatting.ContentLeadingWhitespace != null)
                {
                    this.writer.SetWhitespace(elementFormatting.ContentLeadingWhitespace);
                }
                else
                {
                    this.writer.SetMinimumLineBreaks(elementSettings.LinesInside);
                }
                for (int index = 0; index < element.Children.Count; ++index)
                {
                    FormattedNode node = element.Children[index];
                    if (flag)
                    {
                        this.writer.SuppressWhitespace();
                    }
                    this.WriteNode(indent1, node);
                    if (flag)
                    {
                        this.writer.SuppressWhitespace();
                    }
                }
                if (elementFormatting.ContentTrailingWhitespace != null)
                {
                    this.writer.SetWhitespace(elementFormatting.ContentTrailingWhitespace);
                    if (element.IndentingBehavior == IndentingBehavior.FromContainer && this.writer.LineBreakCount > 0)
                    {
                        this.writer.SetIndent(indent);
                    }
                }
                else
                {
                    this.writer.SetMinimumLineBreaks(elementSettings.LinesInside);
                    this.writer.SetIndent(indent);
                }
                this.writer.WriteEndElement(true);
                length = this.writer.Length;
            }
            else
            {
                this.writer.WriteEndElement(false);
                end    = -1;
                length = this.writer.Length;
            }
            if (this.shouldUpdateSourceContext && elementReference != null)
            {
                elementReference.InitialTextSpan = (ITextRange) new TextRange(start, length);
                if (end != -1)
                {
                    elementReference.InitialStartTagSpan = (ITextRange) new TextRange(start, end);
                }
                element.SourceContextReference.SourceContext = (XamlSourceContext)elementReference;
            }
            if (elementFormatting.TrailingWhitespace != null)
            {
                this.writer.SetWhitespace(elementFormatting.TrailingWhitespace);
            }
            else
            {
                this.writer.SetMinimumLineBreaks(elementSettings.LinesOutside);
            }
        }