/// <summary> /// Creates a new HTML element. /// </summary> /// <param name="tagName">The tag of the HTML element.</param> /// <returns>The newly created HTML element.</returns> public HtmlElement CreateElement(string tagName) { HtmlElement element = HtmlElementFactory.Create(tagName); element.MetaId = Guid.NewGuid().ToString(); element.Document = this; AddMetaId(element, $"document.createElement('{element.TagName}')"); return(element); }
/// <summary> /// Gets an HTML element from a js expression. /// </summary> /// <param name="expression">The expression.</param> /// <returns>The HTML element.</returns> internal HtmlElement GetElementByExpression(string expression) { bool exists = JavaScript.Execute <bool>($"{expression} instanceof HTMLElement"); if (!exists) { throw new ArgumentException("Given expression does not evaluate to an object.", nameof(expression)); } string metaId = JavaScript.Execute <string>($"metaIdTableReverse.get({expression})"); if (!string.IsNullOrEmpty(metaId)) { return(Elements[metaId]); } string tag = JavaScript.Execute <string>($"{expression}.tagName"); HtmlElement element = HtmlElementFactory.Create(tag); element.Document = this; element.MetaId = Guid.NewGuid().ToString(); AddMetaId(element, expression); return(element); }
/// <summary> /// Gets a collection of HTML elements with a given type. /// </summary> /// <typeparam name="T">The type of HTML elements to get.</typeparam> /// <returns>A collection of HTML elements.</returns> public HtmlCollection <T> GetElementsByTagName <T>() where T : HtmlElement => CreateCollection <T>($"document.getElementsByTagName('{HtmlElementFactory.GetTag<T>()}')");