public static IEnumerable <XElement> Ancestors(this XNode node, XName name, bool ignoreNamespace) { if (ignoreNamespace) { return(node.Ancestors().Where(e => e.Name.LocalName == name.LocalName)); } else { return(node.Ancestors(name)); } }
private static IEnumerable <XNode> ScanUp([NotNull] this XNode node) => node .NodesBeforeSelf().Prepend(node) .Concat( node .Ancestors() .SelectMany(n => n.NodesBeforeSelf().Prepend(n)));
private static string ComputeOneLevelOfIndentation(this XNode node) { int num = node.Ancestors().Count <XElement>(); XText previousNode = node.PreviousNode as XText; if ((num == 0) || ((previousNode == null) || !previousNode.IsWhiteSpace())) { return(" "); } string source = previousNode.Value.Trim(Environment.NewLine.ToCharArray()); return(new string((source.LastOrDefault <char>() == '\t') ? '\t' : ' ', Math.Max(1, source.Length / num))); }
/// <summary> /// Returns the parent statement (either expr_stmt, or decl_stmt) of the given node. /// </summary> /// <param name="node">The node to search from.</param> /// <returns>the parent element for <paramref name="node"/>. It will be either <see cref="SRC.ExpressionStatement"/> or <see cref="SRC.DeclarationStatement"/></returns> public static XElement ParentStatement(this XNode node) { if (null == node) { throw new ArgumentNullException("node"); } var ancestors = node.Ancestors().Where(a => a.Name.LocalName.EndsWith("_stmt", StringComparison.OrdinalIgnoreCase)); if (ancestors.Any()) { return(ancestors.First()); } return(null); }
public static CultureInfo SelectCulture(XNode node) { var lang = GetLanguage(node as XElement) ?? GetLanguage(node .Ancestors() .FirstOrDefault(elem => GetLanguage(elem) != null)); try { return(lang == null ? CultureInfo.InvariantCulture : new CultureInfo(lang)); } catch (CultureNotFoundException) { return(CultureInfo.InvariantCulture); } }
private static string ComputeOneLevelOfIndentation(this XNode node) { var depth = node.Ancestors().Count(); XText textBeforeOrNull = node.PreviousNode as XText; if (depth == 0 || textBeforeOrNull == null || !textBeforeOrNull.IsWhiteSpace()) { return(" "); } string indentString = textBeforeOrNull.Value.Trim(Environment.NewLine.ToCharArray()); char lastChar = indentString.LastOrDefault(); char indentChar = (lastChar == '\t' ? '\t' : ' '); int indentLevel = Math.Max(1, indentString.Length / depth); return(new string(indentChar, indentLevel)); }
/// <summary> /// Get the file path relative to <see cref="ProjectDirectory"/> for the unit containing the /// given node. /// </summary> /// <param name="node">The node.</param> /// <returns>The relative file path to that node.</returns> public string RelativePath(XNode node) { if (null == node) { throw new ArgumentNullException("node"); } var unit = node.Ancestors(SRC.Unit).First(); var path = GetPathForUnit(unit); int start = this.ProjectDirectory.Length; if (this.ProjectDirectory[start - 1] != Path.DirectorySeparatorChar) { start++; } path = path.Substring(start); return(path); }
internal static XNode FindTextNode(XNode node) { return(node.Ancestors() .Where(el => el.NodeType == System.Xml.XmlNodeType.Text) .FirstOrDefault()); }
public static IEnumerable <XElement> AncestorsCaseInsensitive(this XNode source, XName name) { return(source.Ancestors().FilterByNameCaseInsensitive(name)); }
/// <summary> /// Dispatches the event to this <see cref="XNode"/>. /// </summary> /// <param name="evt"></param> /// <returns></returns> public bool Dispatch(Event evt) { Contract.Requires <ArgumentNullException>(evt != null); trace.Information("EventDispatcher.DispatchEvent: {0} to {1}", evt.Type, node); if (evt.dispatch || evt.initialized == false || evt.type == null) { throw new DOMException(DOMException.INVALID_STATE_ERR); } evt.dispatch = true; evt.target = node; // path to root from root var path = node.Ancestors() .Cast <XNode>() .Append(node.Document) .ToLinkedList(); evt.eventPhase = EventPhase.Capturing; // capture phase moves from root to target foreach (var currentTarget in path.Backwards()) { if (evt.stopPropagation) { break; } InvokeListeners(currentTarget, evt); } // at-target phase evt.eventPhase = EventPhase.AtTarget; if (!evt.stopPropagation) { InvokeListeners(node, evt); } // bubbling phase if (evt.bubbles) { evt.eventPhase = EventPhase.Bubbling; // bubbling phase moves from target to root foreach (var currentTarget in path.Forwards()) { if (evt.stopPropagation) { break; } InvokeListeners(currentTarget, evt); } } evt.dispatch = false; evt.eventPhase = EventPhase.None; evt.currentTarget = null; if (evt.canceled) { return(false); } // handle default action foreach (var da in node.Interfaces <IEventDefaultAction>()) { if (da != null) { da.DefaultAction(evt); } } return(true); }