From 44bfb62537dcea90be029712bc62938148a854ae Mon Sep 17 00:00:00 2001 From: Florian Rappl Date: Thu, 19 Nov 2026 12:28:36 +0100 Subject: [PATCH 1/2] Fixes for improving Acid3 --- CHANGELOG.md | 9 ++ src/AngleSharp.Xml.Docs/package.json | 2 +- src/AngleSharp.Xml.Tests/Dom/Acid3Tests.cs | 62 +++++++++++ .../Dom/Internal/XmlDocument.cs | 17 +++ .../XmlConfigurationExtensions.cs | 102 ++++++++++++++++-- src/Directory.Build.props | 2 +- 6 files changed, 186 insertions(+), 8 deletions(-) create mode 100644 src/AngleSharp.Xml.Tests/Dom/Acid3Tests.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 346e068..e7becfd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +# 1.3.1 + +Released on tbd. + +- Fixed a navigated document that isn't well-formed failing the navigation (no document, no `load` event); it is shown as a `parsererror` element +- Fixed `document.title` of XHTML documents always being empty +- Fixed `createDocument` not being available to scripts (AngleSharp.Js) until a document was parsed +- Added running the scripts of the XHTML elements of a navigated XML document + # 1.3.0 Released on Sunday, November 1 2026. diff --git a/src/AngleSharp.Xml.Docs/package.json b/src/AngleSharp.Xml.Docs/package.json index 0844920..ff8e22d 100644 --- a/src/AngleSharp.Xml.Docs/package.json +++ b/src/AngleSharp.Xml.Docs/package.json @@ -1,6 +1,6 @@ { "name": "@anglesharp/xml", - "version": "1.3.0", + "version": "1.3.1", "preview": true, "description": "The doclet for the AngleSharp.Xml documentation.", "keywords": [ diff --git a/src/AngleSharp.Xml.Tests/Dom/Acid3Tests.cs b/src/AngleSharp.Xml.Tests/Dom/Acid3Tests.cs new file mode 100644 index 0000000..65e1d10 --- /dev/null +++ b/src/AngleSharp.Xml.Tests/Dom/Acid3Tests.cs @@ -0,0 +1,62 @@ +namespace AngleSharp.Xml.Tests.Dom +{ + using AngleSharp.Dom; + using AngleSharp.Io; + using AngleSharp.Xml.Parser; + using NUnit.Framework; + using System; + using System.Linq; + using System.Threading.Tasks; + + /// + /// Behavior the Acid3 test (acid3.acidtests.org) relies on. + /// + [TestFixture] + public class Acid3Tests + { + private const String XhtmlNamespace = "http://www.w3.org/1999/xhtml"; + + [Test] + public void TitleOfAnXhtmlDocumentUpdatesWithItsElement() + { + var document = BrowsingContext.New(Configuration.Default.WithXml()).OpenNewAsync().Result; + var doctype = document.Implementation.CreateDocumentType("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"); + var xhtml = document.Implementation.CreateDocument(XhtmlNamespace, "html", doctype); + xhtml.DocumentElement.AppendChild(xhtml.CreateElement(XhtmlNamespace, "head")); + xhtml.DocumentElement.AppendChild(xhtml.CreateElement(XhtmlNamespace, "body")); + var title = xhtml.CreateElement(XhtmlNamespace, "title"); + xhtml.DocumentElement.FirstChild.AppendChild(title); + + Assert.AreEqual(String.Empty, xhtml.Title); + + title.TextContent = " Sparrow "; + + Assert.AreEqual("Sparrow", xhtml.Title); + } + + [Test] + public async Task DocumentThatIsNotWellFormedIsShownAsTheError() + { + var context = BrowsingContext.New(Configuration.Default.WithXml()); + var document = await context.OpenAsync(res => res + .Address("http://localhost/xhtml.2") + .Content("

Parsing Test

") + .Header(HeaderNames.ContentType, MimeTypeNames.Xml)); + + Assert.AreEqual("parsererror", document.DocumentElement.LocalName); + Assert.AreEqual(0, document.QuerySelectorAll("p").Length); + } + + [Test] + public void ConfigurationNamesTheLibrarySoItsDomExtensionsAreFound() + { + var config = Configuration.Default.WithXml().WithXml(); + var libraries = config.Services + .Where(m => m is not Delegate) + .Select(m => m.GetType().Assembly) + .Count(m => m == typeof(XmlParser).Assembly); + + Assert.AreEqual(1, libraries); + } + } +} diff --git a/src/AngleSharp.Xml/Dom/Internal/XmlDocument.cs b/src/AngleSharp.Xml/Dom/Internal/XmlDocument.cs index 223728f..95c7ae7 100644 --- a/src/AngleSharp.Xml/Dom/Internal/XmlDocument.cs +++ b/src/AngleSharp.Xml/Dom/Internal/XmlDocument.cs @@ -1,9 +1,11 @@ namespace AngleSharp.Xml.Dom { using AngleSharp.Dom; + using AngleSharp.Html.Dom; using AngleSharp.Io; using AngleSharp.Text; using System; + using System.Linq; /// /// Represents a document node that contains only XML nodes. @@ -72,8 +74,23 @@ public override Node Clone(Document owner, Boolean deep) #region Helpers + /// + /// The title of an XHTML document is its first title element (in the HTML namespace). + /// + protected override String GetTitle() + { + var title = DocumentElement?.Descendants().FirstOrDefault(); + return title?.TextContent.CollapseAndStrip() ?? base.GetTitle(); + } + protected override void SetTitle(String value) { + var title = DocumentElement?.Descendants().FirstOrDefault(); + + if (title is not null) + { + title.TextContent = value ?? String.Empty; + } } internal void SetValidity(Boolean isValid) diff --git a/src/AngleSharp.Xml/XmlConfigurationExtensions.cs b/src/AngleSharp.Xml/XmlConfigurationExtensions.cs index 7fa840d..a666565 100644 --- a/src/AngleSharp.Xml/XmlConfigurationExtensions.cs +++ b/src/AngleSharp.Xml/XmlConfigurationExtensions.cs @@ -1,8 +1,12 @@ namespace AngleSharp { + using AngleSharp.Browser; using AngleSharp.Dom; + using AngleSharp.Html.Dom; using AngleSharp.Io; + using AngleSharp.Scripting; using AngleSharp.Svg.Dom; + using AngleSharp.Text; using AngleSharp.Xml.Dom; using AngleSharp.Xml.Parser; using System; @@ -34,25 +38,111 @@ public static IConfiguration WithXml(this IConfiguration configuration) documentFactory.Register(MimeTypeNames.Svg, LoadSvgAsync); } + // AngleSharp.Js exposes the DOM extensions of the libraries it finds services of, but + // only sees the parser once it was created (e.g. createDocument would be missing) + if (!configuration.Services.OfType().Any()) + { + configuration = configuration.With(XmlLibrary.Instance); + } + return configuration.WithOnly(ctx => new XmlParser(ctx)); } + /// + /// Marks a configuration as using this library. + /// + private sealed class XmlLibrary + { + public static readonly XmlLibrary Instance = new XmlLibrary(); + } + private static Task LoadXmlAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancellationToken) { - var parser = context.GetService() ?? throw new InvalidOperationException("The IXmlParser service has been removed. Cannot continue."); var document = new XmlDocument(context, options.Source); - document.Setup(options.Response, options.ContentType, options.ImportAncestor); - context.NavigateTo(document); - return parser.ParseDocumentAsync(document, cancellationToken); + return LoadAsync(context, document, options, cancellationToken); } private static Task LoadSvgAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancellationToken) { - var parser = context.GetService() ?? throw new InvalidOperationException("The IXmlParser service has been removed. Cannot continue."); var document = new SvgDocument(context, options.Source); + return LoadAsync(context, document, options, cancellationToken); + } + + private static async Task LoadAsync(IBrowsingContext context, Document document, CreateDocumentOptions options, CancellationToken cancellationToken) + { + var parser = context.GetService() ?? throw new InvalidOperationException("The IXmlParser service has been removed. Cannot continue."); document.Setup(options.Response, options.ContentType, options.ImportAncestor); context.NavigateTo(document); - return parser.ParseDocumentAsync(document, cancellationToken); + + try + { + await parser.ParseDocumentAsync(document, cancellationToken).ConfigureAwait(false); + } + catch (XmlParseException ex) when (!cancellationToken.IsCancellationRequested) + { + // like a browser, a document that isn't well-formed is shown as the error (and + // its scripts don't run), but it is a document still, which loads + ShowParseError(document, ex); + return document; + } + + await RunScriptsAsync(context, document, cancellationToken).ConfigureAwait(false); + return document; } + + private static void ShowParseError(Document document, XmlParseException error) + { + while (document.HasChildNodes) + { + document.RemoveChild(((INode)document).FirstChild); + } + + var root = document.CreateElement(ParserErrorNamespace, "parsererror"); + root.TextContent = error.Message; + document.AppendChild(root); + } + + /// + /// Runs the (inline) scripts of the XHTML elements of a document that was navigated to, + /// in tree order once it has been parsed. + /// + private static async Task RunScriptsAsync(IBrowsingContext context, Document document, CancellationToken cancellationToken) + { + if (!context.IsScripting()) + { + return; + } + + var loop = context.GetService(); + // the parser creates its own elements, so they are found by their name + var scripts = document.Descendants() + .Where(m => m.LocalName.Is("script") && m.NamespaceUri.Is(NamespaceNames.HtmlUri) && !m.HasAttribute("src")) + .ToArray(); + + foreach (var script in scripts) + { + var type = script.GetAttribute("type"); + var service = context.GetScripting(String.IsNullOrEmpty(type) ? MimeTypeNames.DefaultJavaScript : type); + + if (service is null || script.Parent is null) + { + continue; + } + + var response = VirtualResponse.Create(r => r.Address(document.DocumentUri).Content(script.TextContent ?? String.Empty)); + + try + { + await service.EvaluateScriptAsync(response, new ScriptOptions(document, loop) { Element = script as IHtmlScriptElement }, cancellationToken).ConfigureAwait(false); + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + // a failing script doesn't stop the others + System.Diagnostics.Debug.WriteLine(ex); + } + } + } + + private static readonly String ParserErrorNamespace = "http://www.mozilla.org/newlayout/xml/parsererror.xml"; } } diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 96fd140..3827f2d 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ Adds a powerful XML and DTD parser to AngleSharp. AngleSharp.Xml - 1.3.0 + 1.3.1 latest true true From a90798e4be417352d2dc5057bac9e171c3edc66d Mon Sep 17 00:00:00 2001 From: Florian Rappl Date: Fri, 20 Nov 2026 23:41:12 +0100 Subject: [PATCH 2/2] Reverted breaking behavior change --- CHANGELOG.md | 1 - src/AngleSharp.Xml.Tests/Dom/Acid3Tests.cs | 20 +++++++++----- .../XmlConfigurationExtensions.cs | 27 +------------------ 3 files changed, 14 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7becfd..131b178 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,6 @@ Released on tbd. -- Fixed a navigated document that isn't well-formed failing the navigation (no document, no `load` event); it is shown as a `parsererror` element - Fixed `document.title` of XHTML documents always being empty - Fixed `createDocument` not being available to scripts (AngleSharp.Js) until a document was parsed - Added running the scripts of the XHTML elements of a navigated XML document diff --git a/src/AngleSharp.Xml.Tests/Dom/Acid3Tests.cs b/src/AngleSharp.Xml.Tests/Dom/Acid3Tests.cs index 65e1d10..76c7819 100644 --- a/src/AngleSharp.Xml.Tests/Dom/Acid3Tests.cs +++ b/src/AngleSharp.Xml.Tests/Dom/Acid3Tests.cs @@ -35,16 +35,22 @@ public void TitleOfAnXhtmlDocumentUpdatesWithItsElement() } [Test] - public async Task DocumentThatIsNotWellFormedIsShownAsTheError() + public async Task DocumentThatIsNotWellFormedFailsToOpen() { + // showing the error instead is up to the application (IDocumentErrorHandler) var context = BrowsingContext.New(Configuration.Default.WithXml()); - var document = await context.OpenAsync(res => res - .Address("http://localhost/xhtml.2") - .Content("

Parsing Test

") - .Header(HeaderNames.ContentType, MimeTypeNames.Xml)); - Assert.AreEqual("parsererror", document.DocumentElement.LocalName); - Assert.AreEqual(0, document.QuerySelectorAll("p").Length); + try + { + await context.OpenAsync(res => res + .Address("http://localhost/xhtml.2") + .Content("

Parsing Test

") + .Header(HeaderNames.ContentType, MimeTypeNames.Xml)); + Assert.Fail("The parse error should propagate."); + } + catch (XmlParseException) + { + } } [Test] diff --git a/src/AngleSharp.Xml/XmlConfigurationExtensions.cs b/src/AngleSharp.Xml/XmlConfigurationExtensions.cs index a666565..8e1e1da 100644 --- a/src/AngleSharp.Xml/XmlConfigurationExtensions.cs +++ b/src/AngleSharp.Xml/XmlConfigurationExtensions.cs @@ -74,34 +74,11 @@ private static async Task LoadAsync(IBrowsingContext context, Documen document.Setup(options.Response, options.ContentType, options.ImportAncestor); context.NavigateTo(document); - try - { - await parser.ParseDocumentAsync(document, cancellationToken).ConfigureAwait(false); - } - catch (XmlParseException ex) when (!cancellationToken.IsCancellationRequested) - { - // like a browser, a document that isn't well-formed is shown as the error (and - // its scripts don't run), but it is a document still, which loads - ShowParseError(document, ex); - return document; - } - + await parser.ParseDocumentAsync(document, cancellationToken).ConfigureAwait(false); await RunScriptsAsync(context, document, cancellationToken).ConfigureAwait(false); return document; } - private static void ShowParseError(Document document, XmlParseException error) - { - while (document.HasChildNodes) - { - document.RemoveChild(((INode)document).FirstChild); - } - - var root = document.CreateElement(ParserErrorNamespace, "parsererror"); - root.TextContent = error.Message; - document.AppendChild(root); - } - /// /// Runs the (inline) scripts of the XHTML elements of a document that was navigated to, /// in tree order once it has been parsed. @@ -142,7 +119,5 @@ private static async Task RunScriptsAsync(IBrowsingContext context, Document doc } } } - - private static readonly String ParserErrorNamespace = "http://www.mozilla.org/newlayout/xml/parsererror.xml"; } }