Push() public method

public Push ( ElementProcessStatus elementProcessStatus ) : void
elementProcessStatus XamlStyler.Core.Parser.ElementProcessStatus
return void
Esempio n. 1
0
        public void Process(XmlReader xmlReader, StringBuilder output, ElementProcessContext elementProcessContext)
        {
            elementProcessContext.UpdateParentElementProcessStatus(ContentTypeEnum.MIXED);

            var elementName = xmlReader.Name;

            elementProcessContext.Push(
                new ElementProcessStatus
            {
                Parent             = elementProcessContext.Current,
                Name               = elementName,
                ContentType        = ContentTypeEnum.NONE,
                IsMultlineStartTag = false,
                IsPreservingSpace  = elementProcessContext.Current.IsPreservingSpace
            }
                );

            var currentIndentString       = _indentService.GetIndentString(xmlReader.Depth);
            var attributeIndetationString = GetAttributeIndetationString(xmlReader);

            // Calculate how element should be indented
            if (!elementProcessContext.Current.IsPreservingSpace)
            {
                // "Run" get special treatment to try to preserve spacing. Use xml:space='preserve' to make sure!
                if (elementName.Equals("Run"))
                {
                    elementProcessContext.Current.Parent.IsSignificantWhiteSpace = true;
                    if (output.Length == 0 || output.IsNewLine())
                    {
                        output.Append(currentIndentString);
                    }
                }
                else
                {
                    elementProcessContext.Current.Parent.IsSignificantWhiteSpace = false;
                    if (output.Length == 0 || output.IsNewLine())
                    {
                        output.Append(currentIndentString);
                    }
                    else
                    {
                        output
                        .Append(Environment.NewLine)
                        .Append(currentIndentString);
                    }
                }
            }

            // Output the element itself
            output
            .Append('<')
            .Append(elementName);

            bool isEmptyElement = xmlReader.IsEmptyElement;

            if (xmlReader.HasAttributes)
            {
                bool isNoLineBreakElement = IsNoLineBreakElement(elementName);
                ProcessAttributes(xmlReader, output, elementProcessContext, isNoLineBreakElement, attributeIndetationString);
            }

            // Determine if to put ending bracket on new line
            bool putEndingBracketOnNewLine = (_options.PutEndingBracketOnNewLine && elementProcessContext.Current.IsMultlineStartTag);

            if (putEndingBracketOnNewLine)
            {
                // Indent ending bracket just like an attribute
                output
                .Append(Environment.NewLine)
                .Append(attributeIndetationString);
            }

            if (isEmptyElement)
            {
                if (putEndingBracketOnNewLine == false && _options.SpaceBeforeClosingSlash)
                {
                    output.Append(' ');
                }
                output.Append("/>");

                // Self closing element. Remember to pop element context.
                elementProcessContext.Pop();
            }
            else
            {
                output.Append(">");
            }
        }
        public void Process(XmlReader xmlReader, StringBuilder output, ElementProcessContext elementProcessContext)
        {
            elementProcessContext.UpdateParentElementProcessStatus(ContentTypeEnum.MIXED);

            var elementName = xmlReader.Name;
            elementProcessContext.Push(
                new ElementProcessStatus
                {
                    Parent = elementProcessContext.Current,
                    Name = elementName,
                    ContentType = ContentTypeEnum.NONE,
                    IsMultlineStartTag = false,
                    IsPreservingSpace = elementProcessContext.Current.IsPreservingSpace
                }
                );

            var currentIndentString = _indentService.GetIndentString(xmlReader.Depth);
            var attributeIndetationString = GetAttributeIndetationString(xmlReader);

            // Calculate how element should be indented
            if (!elementProcessContext.Current.IsPreservingSpace)
            {
                // "Run" get special treatment to try to preserve spacing. Use xml:space='preserve' to make sure!
                if (elementName.Equals("Run"))
                {
                    elementProcessContext.Current.Parent.IsSignificantWhiteSpace = true;
                    if (output.Length == 0 || output.IsNewLine())
                    {
                        output.Append(currentIndentString);
                    }
                }
                else
                {
                    elementProcessContext.Current.Parent.IsSignificantWhiteSpace = false;
                    if (output.Length == 0 || output.IsNewLine())
                    {
                        output.Append(currentIndentString);
                    }
                    else
                    {
                        output
                            .Append(Environment.NewLine)
                            .Append(currentIndentString);
                    }
                }
            }

            // Output the element itself
            output
                .Append('<')
                .Append(elementName);

            bool isEmptyElement = xmlReader.IsEmptyElement;

            if (xmlReader.HasAttributes)
            {
                bool isNoLineBreakElement = IsNoLineBreakElement(elementName);
                ProcessAttributes(xmlReader, output, elementProcessContext, isNoLineBreakElement, attributeIndetationString);
            }

            // Determine if to put ending bracket on new line
            bool putEndingBracketOnNewLine = (_options.PutEndingBracketOnNewLine && elementProcessContext.Current.IsMultlineStartTag);
            if(putEndingBracketOnNewLine)
            {
                // Indent ending bracket just like an attribute
                output
                    .Append(Environment.NewLine)
                    .Append(attributeIndetationString);
            }

            if (isEmptyElement)
            {
                if (putEndingBracketOnNewLine == false && _options.SpaceBeforeClosingSlash)
                {
                    output.Append(' ');
                }
                output.Append("/>");

                // Self closing element. Remember to pop element context.
                elementProcessContext.Pop();
            }
            else
            {
                output.Append(">");
            }
        }