public void TestNormalize(string sourceText, string expected)
        {
            var indentService = new IndentService(true, 4);
            var result        = indentService.Normalize(sourceText);

            Assert.That(result, Is.EqualTo(expected));
        }
        /// <summary>
        /// Handles markup extension value in style as:
        /// XyzAttribute="{XyzMarkup value1,
        ///                          value2,
        ///                          key1=value1,
        ///                          key2=value2}"
        /// </summary>
        /// <param name="attrInfo"></param>
        /// <param name="baseIndentationString"></param>
        /// <returns></returns>
        public string ToMultiLineString(AttributeInfo attrInfo, string baseIndentationString)
        {
            #region Parameter Checks

            if (!attrInfo.IsMarkupExtension)
            {
                throw new ArgumentException("AttributeInfo shall have a markup extension value.",
                                            MethodBase.GetCurrentMethod().GetParameters()[0].Name);
            }

            #endregion Parameter Checks

            if (attrInfo.IsMarkupExtension)
            {
                string currentIndentationString = baseIndentationString + string.Empty.PadLeft(attrInfo.Name.Length + 2, ' ');
                var    lines = _formatter.Format(attrInfo.MarkupExtension);

                var buffer = new StringBuilder();
                buffer.AppendFormat("{0}=\"{1}", attrInfo.Name, lines.First());
                foreach (var line in lines.Skip(1))
                {
                    buffer.AppendLine();
                    buffer.Append(_indentService.Normalize(currentIndentationString + line));
                }

                buffer.Append('"');
                return(buffer.ToString());
            }
            return($"{attrInfo.Name}=\"{attrInfo.Value}\"");
        }
Esempio n. 3
0
        public void TestNormalize(string sourceText, string expected)
        {
            var indentService = new IndentService(new StylerOptions()
            {
                IndentWithTabs            = true,
                IndentSize                = 4,
                AttributeIndentationStyle = AttributeIndentationStyle.Mixed
            });

            var result = indentService.Normalize(sourceText);

            Assert.That(result, Is.EqualTo(expected));
        }
Esempio n. 4
0
        private void ProcessAttributes(XmlReader xmlReader, StringBuilder output, ElementProcessContext elementProcessContext, bool isNoLineBreakElement, string attributeIndentationString)
        {
            var list = new List <AttributeInfo>(xmlReader.AttributeCount);

            while (xmlReader.MoveToNextAttribute())
            {
                list.Add(_attributeInfoFactory.Create(xmlReader));

                // Check for xml:space as defined in http://www.w3.org/TR/2008/REC-xml-20081126/#sec-white-space
                if (xmlReader.IsXmlSpaceAttribute())
                {
                    elementProcessContext.Current.IsPreservingSpace = (xmlReader.Value == "preserve");
                }
            }

            if (_options.EnableAttributeReordering)
            {
                list.Sort(AttributeInfoComparison);
            }

            var noLineBreakInAttributes    = (list.Count <= _options.AttributesTolerance) || isNoLineBreakElement;
            var forceLineBreakInAttributes = false;

            // Root element?
            if (elementProcessContext.Count == 2)
            {
                switch (_options.RootElementLineBreakRule)
                {
                case LineBreakRule.Default:
                    break;

                case LineBreakRule.Always:
                    noLineBreakInAttributes    = false;
                    forceLineBreakInAttributes = true;
                    break;

                case LineBreakRule.Never:
                    noLineBreakInAttributes = true;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            // No need to break attributes
            if (noLineBreakInAttributes)
            {
                foreach (var attrInfo in list)
                {
                    output
                    .Append(' ')
                    .Append(_attributeInfoFormatter.ToSingleLineString(attrInfo));
                }

                elementProcessContext.Current.IsMultlineStartTag = false;
            }

            // Need to break attributes
            else
            {
                var attributeLines    = new List <string>();
                var currentLineBuffer = new StringBuilder();
                int attributeCountInCurrentLineBuffer = 0;

                AttributeInfo lastAttributeInfo = null;
                foreach (AttributeInfo attrInfo in list)
                {
                    // Attributes with markup extension, always put on new line
                    if (attrInfo.IsMarkupExtension && _options.FormatMarkupExtension)
                    {
                        if (currentLineBuffer.Length > 0)
                        {
                            attributeLines.Add(currentLineBuffer.ToString());
                            currentLineBuffer.Length          = 0;
                            attributeCountInCurrentLineBuffer = 0;
                        }

                        attributeLines.Add(_attributeInfoFormatter.ToMultiLineString(attrInfo, attributeIndentationString));
                    }
                    else
                    {
                        string pendingAppend = _attributeInfoFormatter.ToSingleLineString(attrInfo);

                        bool isAttributeCharLengthExceeded =
                            (attributeCountInCurrentLineBuffer > 0 && _options.MaxAttributeCharatersPerLine > 0
                             &&
                             currentLineBuffer.Length + pendingAppend.Length > _options.MaxAttributeCharatersPerLine);

                        bool isAttributeCountExceeded =
                            (_options.MaxAttributesPerLine > 0 &&
                             attributeCountInCurrentLineBuffer + 1 > _options.MaxAttributesPerLine);

                        bool isAttributeRuleGroupChanged = _options.PutAttributeOrderRuleGroupsOnSeparateLines &&
                                                           lastAttributeInfo != null &&
                                                           lastAttributeInfo.OrderRule.Group != attrInfo.OrderRule.Group;

                        if (currentLineBuffer.Length > 0 && (forceLineBreakInAttributes || isAttributeCharLengthExceeded || isAttributeCountExceeded || isAttributeRuleGroupChanged))
                        {
                            attributeLines.Add(currentLineBuffer.ToString());
                            currentLineBuffer.Length          = 0;
                            attributeCountInCurrentLineBuffer = 0;
                        }

                        currentLineBuffer.AppendFormat("{0} ", pendingAppend);
                        attributeCountInCurrentLineBuffer++;
                    }

                    lastAttributeInfo = attrInfo;
                }

                if (currentLineBuffer.Length > 0)
                {
                    attributeLines.Add(currentLineBuffer.ToString());
                }

                for (int i = 0; i < attributeLines.Count; i++)
                {
                    // Put first attribute line on same line as element?
                    if (0 == i && _options.KeepFirstAttributeOnSameLine)
                    {
                        output
                        .Append(' ')
                        .Append(attributeLines[i].Trim());
                    }
                    else
                    {
                        output
                        .Append(Environment.NewLine)
                        .Append(_indentService.Normalize(attributeIndentationString + attributeLines[i].Trim()));
                    }
                }

                elementProcessContext.Current.IsMultlineStartTag = true;
            }
        }