/// <summary>Find elements matching selector.</summary> /// <param name="query">CSS selector</param> /// <param name="roots">root elements to descend into</param> /// <returns>matching elements, empty if none</returns> public static Elements Select(String query, IEnumerable <iText.StyledXmlParser.Jsoup.Nodes.Element> roots) { Validate.NotEmpty(query); Validate.NotNull(roots); Evaluator evaluator = QueryParser.Parse(query); List <iText.StyledXmlParser.Jsoup.Nodes.Element> elements = new List <iText.StyledXmlParser.Jsoup.Nodes.Element >(); IdentityDictionary <iText.StyledXmlParser.Jsoup.Nodes.Element, bool?> seenElements = new IdentityDictionary <iText.StyledXmlParser.Jsoup.Nodes.Element, bool?>(); // dedupe elements by identity, not equality foreach (iText.StyledXmlParser.Jsoup.Nodes.Element root in roots) { Elements found = Select(evaluator, root); foreach (iText.StyledXmlParser.Jsoup.Nodes.Element el in found) { if (!seenElements.ContainsKey(el)) { elements.Add(el); seenElements.Put(el, true); } } } return(new Elements(elements)); }
private Tuple <Tree, Tree> CopyHelper(Tree node, Dictionary <string, Tree> newNamesToNodes) { Tree clone; Tree newFoot = null; if (node.IsLeaf()) { if (node == Foot) { // found the foot node; pass it up. clone = node.TreeFactory().NewTreeNode(node.Label(), new List <Tree>(0)); newFoot = clone; } else { clone = node.TreeFactory().NewLeaf(node.Label().LabelFactory().NewLabel(node.Label())); } } else { var newChildren = new List <Tree>(node.Children().Length); foreach (Tree child in node.Children()) { Tuple <Tree, Tree> newChild = CopyHelper(child, newNamesToNodes); newChildren.Add(newChild.Item1); if (newChild.Item2 != null) { newFoot = newChild.Item2; } } clone = node.TreeFactory().NewTreeNode(node.Label().LabelFactory().NewLabel(node.Label()), newChildren); } if (nodesToNames.ContainsKey(node)) { newNamesToNodes.Add(nodesToNames[node], clone); } return(new Tuple <Tree, Tree>(clone, newFoot)); }