public CommentLineXml(XElement xml, CodeCommentOptions options) : base(null) { TagName = xml.Name.LocalName; // Tags that are forced to be their own line should never be self closing. This prevents // empty tags from getting collapsed. OpenTag = CodeCommentHelper.CreateXmlOpenTag(xml, options, false); Closetag = CodeCommentHelper.CreateXmlCloseTag(xml, options, false); Lines = new List <ICommentLine>(); _innerText = new StringBuilder(); ParseChildNodes(xml, options); CloseInnerText(); }
private void ParseChildNodes(XElement xml) { if (string.Equals(TagName, "code", StringComparison.OrdinalIgnoreCase)) { // Content of code element should be read literally and preserve whitespace. using (var reader = xml.CreateReader()) { reader.MoveToContent(); Content = reader.ReadInnerXml(); } } else { // Loop and parse all child nodes. var node = xml.FirstNode; while (node != null) { // If the node is a sub-element, it needs to be handled seperately. if (node.NodeType == XmlNodeType.Element) { var e = (XElement)node; if (ShouldBeNewLine(e)) { CloseInnerText(); Lines.Add(new CommentLineXml(e)); } else { // If the tag is not forced to be on it's own line, append it to the // current content as string. _innerText.Append(CodeCommentHelper.CreateXmlOpenTag(e)); if (!e.IsEmpty) { if (Settings.Default.Formatting_CommentXmlSpaceTags) { _innerText.Append(CodeCommentHelper.Spacer); } ParseChildNodes(e); _innerText.Append(CodeCommentHelper.CreateXmlCloseTag(e)); } } } else { // Always trim trailing var value = node.ToString().TrimEnd(CodeCommentHelper.Spacer); // If the parent is an element, trim the starting spaces. if (node.PreviousNode == null && node.Parent.NodeType == XmlNodeType.Element && !Settings.Default.Formatting_CommentXmlSpaceTags) { value = value.TrimStart(CodeCommentHelper.Spacer); } // If the previous node was an XML element, put a space before the text // unless the first character is interpunction. if (node.PreviousNode != null && node.PreviousNode.NodeType == XmlNodeType.Element) { if (!StartsWithInterpunction(value)) { _innerText.Append(CodeCommentHelper.Spacer); } } _innerText.Append(value); // Add spacing after (almost) each word. if (node.NextNode != null || node.Parent.NodeType != XmlNodeType.Element || Settings.Default.Formatting_CommentXmlSpaceTags) { _innerText.Append(CodeCommentHelper.Spacer); } } node = node.NextNode; } } }
private void ParseChildNodes(XElement xml, CodeCommentOptions options) { if (string.Equals(TagName, "code", StringComparison.OrdinalIgnoreCase)) { // Content of code element should be read literally and preserve whitespace. using (var reader = xml.CreateReader()) { reader.MoveToContent(); Content = reader.ReadInnerXml(); } } else { // Loop and parse all child nodes. var node = xml.FirstNode; while (node != null) { // If the node is a sub-element, it needs to be handled seperately. if (node.NodeType == XmlNodeType.Element) { var e = (XElement)node; // All root level elements and certain special sub elements always need to // be on their own line. if (e.Parent == null || e.Parent.Parent == null || NewLineElementNames.Contains(e.Name.LocalName, StringComparer.OrdinalIgnoreCase)) { CloseInnerText(); Lines.Add(new CommentLineXml(e, options)); } else { // If the tag is not forced to be on it's own line, append it to the // current content as string. _innerText.Append(CodeCommentHelper.CreateXmlOpenTag(e, options)); if (!e.IsEmpty) { if (options.XmlSpaceTagContent) { _innerText.Append(CodeCommentHelper.Spacer); } ParseChildNodes(e, options); _innerText.Append(CodeCommentHelper.CreateXmlCloseTag(e, options)); } _innerText.Append(CodeCommentHelper.Spacer); } } else { // Always trim trailing var value = node.ToString().TrimEnd(CodeCommentHelper.Spacer); // If the parent is an element, trim the starting spaces. if (node.PreviousNode == null && node.Parent.NodeType == XmlNodeType.Element && !options.XmlSpaceTagContent) { value = value.TrimStart(CodeCommentHelper.Spacer); } _innerText.Append(value); // Add spacing after (almost) each word. if (node.NextNode != null || node.Parent.NodeType != XmlNodeType.Element || options.XmlSpaceTagContent) { _innerText.Append(CodeCommentHelper.Spacer); } } node = node.NextNode; } } }