string GetInnerText(XContainer node) { StringBuilder sb = null; foreach (XNode n in node.Nodes()) { GetInnerText(n, ref sb); } return(sb != null?sb.ToString() : String.Empty); }
/// <summary> /// Clone ctor /// </summary> internal XContainer(XContainer source) { content = source.content as string; if ((content == null) && (source.content is XNode)) { foreach (var node in source.Nodes()) { Add(node.Clone(), false); } } }
/// <summary> /// Clone ctor /// </summary> internal XContainer(XContainer source) { content = source.content as string; if ((content == null) && (source.content is XNode)) { foreach (var node in source.Nodes()) { Add(node.Clone(), false); } } }
private IEnumerable<ExpectedValue> ReplaceWithExpValues(XContainer elem, XNode toReplace, IEnumerable<object> replacement) { foreach (XNode n in elem.Nodes()) { if (n != toReplace) { yield return new ExpectedValue(true, n); } else { foreach (object o in replacement) { yield return new ExpectedValue(o is XNode && (o as XNode).Parent == null && (o as XNode).Document == null, o); } } } }
private IEnumerable<ExpectedValue> CalculateExpectedContent(XContainer orig, IEnumerable<object> newNodes, string stringOnlyContent) { if (stringOnlyContent == null) { foreach (object o in orig.Nodes()) { yield return new ExpectedValue(true, o); } } else { yield return new ExpectedValue(false, new XText(stringOnlyContent)); } foreach (object n in newNodes.Flatten()) { if (n is XAttribute) { continue; } yield return new ExpectedValue((n is XNode) && (n as XNode).Parent == null && (n as XNode).Document == null, n); } }
string GetInnerText (XContainer node) { StringBuilder sb = null; foreach (XNode n in node.Nodes ()) GetInnerText (n, ref sb); return sb != null ? sb.ToString () : String.Empty; }
private string TextValue(XContainer elem) { return elem.Nodes().OfType<XText>().Aggregate("", (s, text) => s + text); }
private void DoRemoveTest(XContainer elem, int position) { List<ExpectedValue> expectedData = elem.Nodes().Take(position).Concat(elem.Nodes().Skip(position + 1)).Select(n => new ExpectedValue(!(n is XText), n)).ProcessNodes().ToList(); XNode toRemove = elem.Nodes().ElementAt(position); toRemove.Remove(); TestLog.Compare(toRemove.Parent == null, "Parent of Removed"); TestLog.Compare(toRemove.Document == null, "Document of Removed"); TestLog.Compare(toRemove.NextNode == null, "NextNode"); TestLog.Compare(toRemove.PreviousNode == null, "PreviousNode"); if (toRemove is XContainer) { foreach (XNode child in (toRemove as XContainer).Nodes()) { TestLog.Compare(child.Document == null, "Document of child of Removed"); TestLog.Compare(child.Parent == toRemove, "Parent of child of Removed should be set"); } } // try Remove Removed node try { toRemove.Remove(); TestLog.Compare(false, "Exception expected [" + toRemove.NodeType + "] " + toRemove); } catch (InvalidOperationException) { } TestLog.Compare(expectedData.EqualAll(elem.Nodes(), XNode.EqualityComparer), "The rest of the tree - Nodes()"); }
private IEnumerable<TokenBase> ParseNodes(XContainer container, Stack<TextVisualProperties> propertiesStack, TokenIndex top, int bookLevel, int parentID = -1) { foreach (XNode node in container.Nodes()) { var text = node as XText; if ((text != null) && !string.IsNullOrEmpty(text.Value)) { foreach (TokenBase token in ParseText(text.Value, top)) { yield return token; } } var element = node as XElement; if(element == null) continue; TextVisualProperties properties = propertiesStack.Peek().Clone().Update(element, _styleSheet); string localName = element.Name.LocalName; int level = bookLevel; if (localName == "a") { ProcessLinks(properties, element); } ProcessAnchors(top, element); if (localName == "section") { yield return new NewPageToken(top.Index++); level++; } if (localName == "title") { ProcessTitleData(top, element, level); } if (localName == "image") { XAttribute hrefAttr = element.Attributes().FirstOrDefault(t => (t.Name.LocalName == "href")); string href = ((hrefAttr != null) ? hrefAttr.Value : string.Empty).TrimStart('#'); var pictureToken = new PictureToken(top.Index++, href); yield return pictureToken; } else { var tagOpen = new TagOpenToken(top.Index++, element, properties, parentID); yield return tagOpen; propertiesStack.Push(properties); foreach (TokenBase token in ParseNodes(element, propertiesStack, top, level, tagOpen.ID)) { yield return token; } propertiesStack.Pop(); yield return new TagCloseToken(top.Index++, parentID); } } }
private static void ReparentChildren( XContainer originalParent, XContainer newParent) { // re-parent all descendants from originalParent into newParent List<XNode> childNodes = new List<XNode>(); foreach (XNode d in originalParent.Nodes()) { childNodes.Add(d); } foreach (XNode d in childNodes) { d.Remove(); newParent.Add(d); } }
private static IEnumerable<XElement> SelectDirectDescendents( XContainer root, XName name) { List<XElement> selected = new List<XElement>(); foreach (XNode node in root.Nodes()) { if (node.NodeType == XmlNodeType.Element) { XElement e = node as XElement; if (e != null) { if (e.Name.Equals(name)) { selected.Add(e); } } } } return selected; }
private static void TransferChildren( XContainer originalParent, XContainer newParent) { // now move all descendants into this node List<XNode> childNodes = new List<XNode>(); foreach (XNode d in originalParent.Nodes()) { childNodes.Add(d); } foreach (XNode d in childNodes) { d.Remove(); newParent.Add(d); } }
private static void AddLeadingIndentation(XContainer container, string containerIndent, string oneIndentLevel) { var containerIsSelfClosed = !container.Nodes().Any(); var lastChildText = container.LastNode as XText; if (containerIsSelfClosed || lastChildText == null) { container.Add(new XText(containerIndent + oneIndentLevel)); } else { lastChildText.Value += oneIndentLevel; } }