public static string Execute(string html, IParserProvider parser, Uri page, CookieContainer cookies) { if (GhostConfiguration.ScriptEngine != null) { var config = new Configuration(); var internalparse = new DocumentBuilder(config); var document = internalparse.FromHtml(html); //var scriptInitialize = document.CreateElement("script"); //scriptInitialize.TextContent = string.Format(@"document.location.href='{0}';", page.OriginalString); //document.Head.Append(scriptInitialize); var scripts = document.Scripts.Where(x => x.Attributes.Any(i => i.Name == "src")).ToList(); var styles = document.QuerySelectorAll("link[href]").Cast<IElement>().ToList(); var iframes = document.QuerySelectorAll("iframe[src]").Cast<IElement>().ToList(); foreach (var script in scripts) ResolveUrls(page, script, "src"); foreach (var script in styles) ResolveUrls(page, script, "href"); foreach (var script in iframes) ResolveUrls(page, script, "src"); var htmlParsed = document.ToHtml(); return GhostConfiguration.ScriptEngine.Run(htmlParsed, cookies); } return html; }
/// <summary> /// Builds a new HTMLDocument with the given source code string. /// </summary> /// <param name="sourceCode">The string to use as source code.</param> /// <param name="options">[Optional] Options to use for the document generation.</param> /// <returns>The constructed HTML document.</returns> public static HTMLDocument Html(String sourceCode, DocumentOptions options = null) { var source = new SourceManager(sourceCode); var db = new DocumentBuilder(source, new HTMLDocument(), options ?? DocumentOptions.Default); return db.HtmlResult; }
/// <summary> /// Builds a new CSSStyleSheet with the given network stream. /// </summary> /// <param name="stream">The stream of chars to use as source code.</param> /// <param name="options">[Optional] Options to use for the document generation.</param> /// <returns>The constructed CSS stylesheet.</returns> public static CSSStyleSheet Css(Stream stream, DocumentOptions options = null) { var source = new SourceManager(stream); var db = new DocumentBuilder(source, new CSSStyleSheet(), options ?? DocumentOptions.Default); return db.CssResult; }
/// <summary> /// Builds a new CSSStyleSheet asynchronously by requesting the given URL. /// </summary> /// <param name="url">The URL which points to the address containing the source code.</param> /// <param name="options">[Optional] Options to use for the document generation.</param> /// <returns>The task which constructs the CSS stylesheet.</returns> public static async Task<CSSStyleSheet> CssAsync(Uri url, DocumentOptions options = null) { var stream = await Builder.GetFromUrl(url); var source = new SourceManager(stream); var db = new DocumentBuilder(source, new CSSStyleSheet { Href = url.OriginalString }, options ?? DocumentOptions.Default); await db.parser.ParseAsync(); return db.CssResult; }
/// <summary> /// Builds a list of nodes according with 8.4 Parsing HTML fragments. /// </summary> /// <param name="sourceCode">The string to use as source code.</param> /// <param name="context">[Optional] The context node to use.</param> /// <param name="options">[Optional] Options to use for the document generation.</param> /// <returns>A list of parsed nodes.</returns> public static NodeList HtmlFragment(String sourceCode, Node context = null, DocumentOptions options = null) { var source = new SourceManager(sourceCode); var doc = new HTMLDocument(); //Disable scripting for HTML fragments (security reasons) options = options ?? new DocumentOptions(scripting: false); var db = new DocumentBuilder(source, doc, options); if (context != null) { if (context.OwnerDocument != null && context.OwnerDocument.QuirksMode != QuirksMode.Off) doc.QuirksMode = context.OwnerDocument.QuirksMode; var parser = (HtmlParser)db.parser; parser.SwitchToFragment(context); return parser.Result.DocumentElement.ChildNodes; } return db.HtmlResult.ChildNodes; }
/// <summary> /// Builds a new HTMLDocument by asynchronously requesting the given URL. /// </summary> /// <param name="url">The URL which points to the address containing the source code.</param> /// <param name="options">[Optional] Options to use for the document generation.</param> /// <returns>The task that constructs the HTML document.</returns> public static async Task<HTMLDocument> HtmlAsync(Uri url, DocumentOptions options = null) { var stream = await Builder.GetFromUrl(url); var source = new SourceManager(stream); var db = new DocumentBuilder(source, new HTMLDocument { DocumentURI = url.OriginalString }, options ?? DocumentOptions.Default); await db.parser.ParseAsync(); return db.HtmlResult; }
public AngleSharpProvider() { var config = new Configuration(); _builder = new DocumentBuilder(config); }
/// <summary> /// Builds a new CSSStyleSheet with the given URL. /// </summary> /// <param name="url">The URL which points to the address containing the source code.</param> /// <returns>The constructed CSS stylesheet.</returns> public static CSSStyleSheet Css(Uri url) { var stream = Builder.Stream(url); var source = new SourceManager(stream); var db = new DocumentBuilder(source, new CSSStyleSheet()); return db.CssResult; }
/// <summary> /// Builds a new CSSStyleSheet with the given source code string. /// </summary> /// <param name="sourceCode">The string to use as source code.</param> /// <returns>The constructed CSS stylesheet.</returns> public static CSSStyleSheet Css(String sourceCode) { var source = new SourceManager(sourceCode); var db = new DocumentBuilder(source, new CSSStyleSheet()); return db.CssResult; }
/// <summary> /// Builds a list of nodes according with 8.4 Parsing HTML fragments. /// </summary> /// <param name="sourceCode">The string to use as source code.</param> /// <param name="context">The context node to use.</param> /// <returns>A list of parsed nodes.</returns> public static NodeList HtmlFragment(String sourceCode, Node context = null) { var source = new SourceManager(sourceCode); var doc = new HTMLDocument(); var db = new DocumentBuilder(source, doc); if (context != null) { if (context.OwnerDocument != null && context.OwnerDocument.QuirksMode != QuirksMode.Off) doc.QuirksMode = context.OwnerDocument.QuirksMode; // Note: For performance reasons, an implementation that does not report errors and that uses // the actual state machine described in this specification directly could use the // PLAINTEXT state instead of the RAWTEXT and script data states where those are mentioned // in the list above. Except for rules regarding parse errors, they are equivalent, since // there is no appropriate end tag token in the fragment case, yet they involve far // fewer state transitions. ((HtmlParser)db.parser).SwitchToFragment(context); return db.HtmlResult.DocumentElement.ChildNodes; } return db.HtmlResult.ChildNodes; }
/// <summary> /// Builds a new HTMLDocument with the given network stream. /// </summary> /// <param name="networkStream">The stream of chars to use as source code.</param> /// <returns>The constructed HTML document.</returns> public static HTMLDocument Html(Stream networkStream) { var source = new SourceManager(networkStream); var db = new DocumentBuilder(source, new HTMLDocument()); return db.HtmlResult; }
/// <summary> /// Builds a new HTMLDocument with the given URL. /// </summary> /// <param name="url">The URL which points to the address containing the source code.</param> /// <returns>The constructed HTML document.</returns> public static HTMLDocument Html(Uri url) { var stream = Builder.Stream(url); var source = new SourceManager(stream); var db = new DocumentBuilder(source, new HTMLDocument()); return db.HtmlResult; }
/// <summary> /// Builds a new HTMLDocument with the given source code string. /// </summary> /// <param name="sourceCode">The string to use as source code.</param> /// <returns>The constructed HTML document.</returns> public static HTMLDocument Html(String sourceCode) { var source = new SourceManager(sourceCode); var db = new DocumentBuilder(source, new HTMLDocument()); return db.HtmlResult; }
/// <summary> /// Builds a new CSSStyleSheet with the given network stream. /// </summary> /// <param name="networkStream">The stream of chars to use as source code.</param> /// <returns>The constructed CSS stylesheet.</returns> public static CSSStyleSheet Css(Stream networkStream) { var source = new SourceManager(networkStream); var db = new DocumentBuilder(source, new CSSStyleSheet()); return db.CssResult; }
/// <summary> /// Builds a new XMLDocument with the given (network) stream. /// </summary> /// <param name="stream">The stream of chars to use as source code.</param> /// <param name="options">[Optional] Options to use for the document generation.</param> /// <returns>The constructed XML document.</returns> public static XMLDocument Xml(Stream stream, DocumentOptions options = null) { var source = new SourceManager(stream); var db = new DocumentBuilder(source, new XMLDocument(), options ?? DocumentOptions.Default); return db.XmlResult; }