AngleSharp.Css
for CSS support)When attempting to navigate an anchor element using the DoClick
method (this might also apply to form submit buttons as well), which has a target referring to a named iframe, the document is not loaded in the iframe, but instead as a child context of the context of the document containing the link, opening the page in effectively a new window.
using AngleSharp; using AngleSharp.Html.Dom; using System.Diagnostics; var context = BrowsingContext.New(Configuration.Default.WithDefaultLoader(new() { IsResourceLoadingEnabled= true })); await context.OpenAsync(req => req.Content(""" <html> <body> <a id="frame" target="target" href="https://github.com/AngleSharp/AngleSharp/">AngleSharp</a> <iframe id="target" name="target"> </iframe> </body> </html> """)); var linkElement = context.Active.GetElementById("frame") as IHtmlAnchorElement; linkElement.DoClick(); Thread.Sleep(1000); var frameElement = context.Active.GetElementById("target") as IHtmlInlineFrameElement; Debug.Assert(frameElement.ContentDocument?.Title.StartsWith("GitHub - AngleSharp") ?? false, "frame document title does not start with AngleSharp"); var targetContext = context.FindChild("target"); Debug.Assert(targetContext.Active?.Title.StartsWith("GitHub - AngleSharp") ?? false, "child context title does not start with AngleSharp");Expected Behavior
The page with the url located in the anchor element should be loaded as the content document of the iframe.
Actual BehaviorThe page with the url located in the anchor element is loaded in a child context which is not the content document of the iframe.
Possible Solution / Known WorkaroundsOne possible work around would be to, instead of calling DoClick
, to instead check if the anchor tag has the target attribute set, and check if the document, or any documents of parent contexts, up to the root context, has any frames with the name identical to the target, and manually navigate that context to the url specific by the anchor tag, while also checking that no other subscribed event handler is cancelling the click.
A possible solution would be, when the frame is creating a new child context, if the frame has a name, it uses that name as the name of the child context, such that resolving targets pick the frame's context, rather than creating a new window. For this to work correctly, however, it may be necessary to check the parent browsing context for the associated frame browsing context, as in testing with FireFox, an anchor tag in one frame is able to target another frame in the parent page by name. I don't know how far up or down is necessary, as I've not tested beyond this.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4