public int AttributeInfoComparison(AttributeInfo x, AttributeInfo y) { if (x.OrderRule.Group != y.OrderRule.Group) { return x.OrderRule.Group.CompareTo(y.OrderRule.Group); } if (x.OrderRule.Priority != y.OrderRule.Priority) { return x.OrderRule.Priority.CompareTo(y.OrderRule.Priority); } return Options.OrderAttributesByName ? String.Compare(x.Name, y.Name, StringComparison.Ordinal) : 0; }
private void ProcessElement(XmlReader xmlReader, StringBuilder output) { string currentIndentString = GetIndentString(xmlReader.Depth); string elementName = xmlReader.Name; // Calculate how element should be indented if (!this.ElementProcessStatusStack.Peek().IsPreservingSpace) { // "Run" get special treatment to try to preserve spacing. Use xml:space='preserve' to make sure! if (elementName.Equals("Run")) { this.ElementProcessStatusStack.Peek().Parent.IsSignificantWhiteSpace = true; if ((output.Length == 0) || output.IsNewLine()) { output.Append(currentIndentString); } } else { this.ElementProcessStatusStack.Peek().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(xmlReader.Name); bool isEmptyElement = xmlReader.IsEmptyElement; bool hasPutEndingBracketOnNewLine = false; var list = new List<AttributeInfo>(xmlReader.AttributeCount); var firstLineList = new List<AttributeInfo>(xmlReader.AttributeCount); if (xmlReader.HasAttributes) { while (xmlReader.MoveToNextAttribute()) { string attributeName = xmlReader.Name; string attributeValue = xmlReader.Value; AttributeOrderRule orderRule = OrderRules.GetRuleFor(attributeName); var attribute = new AttributeInfo(attributeName, attributeValue, orderRule); list.Add(attribute); // Maintain separate list of first line attributes. if (this.Options.EnableAttributeReordering && this.IsFirstLineAttribute(attribute.Name)) { firstLineList.Add(attribute); } // Check for xml:space as defined in http://www.w3.org/TR/2008/REC-xml-20081126/#sec-white-space if (xmlReader.IsXmlSpaceAttribute()) { this.ElementProcessStatusStack.Peek().IsPreservingSpace = (xmlReader.Value == "preserve"); } } if (this.Options.EnableAttributeReordering) { list.Sort(AttributeInfoComparison); firstLineList.Sort(AttributeInfoComparison); } currentIndentString = this.GetIndentString(xmlReader.Depth); var noLineBreakInAttributes = (list.Count <= this.Options.AttributesTolerance) || this.IsNoLineBreakElement(elementName); var forceLineBreakInAttributes = false; // Root element? if (this.ElementProcessStatusStack.Count == 2) { switch (this.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(attrInfo.ToSingleLineString()); } this.ElementProcessStatusStack.Peek().IsMultlineStartTag = false; } // Need to break attributes else { IList<String> attributeLines = new List<String>(); var currentLineBuffer = new StringBuilder(); int attributeCountInCurrentLineBuffer = 0; AttributeInfo lastAttributeInfo = null; // Process first line attributes. string firstLine = String.Empty; foreach (var attrInfo in firstLineList) { firstLine = $"{firstLine} {attrInfo.ToSingleLineString()}"; } if (firstLine.Length > 0) { attributeLines.Add(firstLine); } foreach (AttributeInfo attrInfo in list) { // Skip attributes already added to first line. if (firstLineList.Contains(attrInfo)) { continue; } // Attributes with markup extension, always put on new line if (attrInfo.IsMarkupExtension && this.Options.FormatMarkupExtension) { string baseIndetationString; if (!this.Options.KeepFirstAttributeOnSameLine) { baseIndetationString = this.GetIndentString(xmlReader.Depth); } else { baseIndetationString = this.GetIndentString(xmlReader.Depth - 1) + String.Empty.PadLeft(elementName.Length + 2, ' '); } string pendingAppend; if (this.NoNewLineMarkupExtensionsList.Contains(attrInfo.MarkupExtension)) { pendingAppend = $" {attrInfo.ToSingleLineString()}"; } else { pendingAppend = attrInfo.ToMultiLineString(baseIndetationString); } if (currentLineBuffer.Length > 0) { attributeLines.Add(currentLineBuffer.ToString()); currentLineBuffer.Length = 0; attributeCountInCurrentLineBuffer = 0; } attributeLines.Add(pendingAppend); } else { string pendingAppend = attrInfo.ToSingleLineString(); 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.SeparateByGroups && (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++) { if ((i == 0) && (this.Options.KeepFirstAttributeOnSameLine || (firstLineList.Count > 0))) { output.Append(' ').Append(attributeLines[i].Trim()); // Align subsequent attributes with first attribute currentIndentString = GetIndentString(xmlReader.Depth - 1) + String.Empty.PadLeft(elementName.Length + 2, ' '); continue; } output.Append(Environment.NewLine).Append(currentIndentString).Append(attributeLines[i].Trim()); } this.ElementProcessStatusStack.Peek().IsMultlineStartTag = true; } // Determine if to put ending bracket on new line if (this.Options.PutEndingBracketOnNewLine && this.ElementProcessStatusStack.Peek().IsMultlineStartTag) { output.Append(Environment.NewLine).Append(currentIndentString); hasPutEndingBracketOnNewLine = true; } } if (isEmptyElement) { if (!hasPutEndingBracketOnNewLine && this.Options.SpaceBeforeClosingSlash) { output.Append(' '); } output.Append("/>"); this.ElementProcessStatusStack.Peek().IsSelfClosingElement = true; } else { output.Append(">"); } }