diff --git a/CHANGELOG.md b/CHANGELOG.md index 346e068..131b178 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +# 1.3.1 + +Released on tbd. + +- 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..76c7819 --- /dev/null +++ b/src/AngleSharp.Xml.Tests/Dom/Acid3Tests.cs @@ -0,0 +1,68 @@ +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 DocumentThatIsNotWellFormedFailsToOpen() + { + // showing the error instead is up to the application (IDocumentErrorHandler) + var context = BrowsingContext.New(Configuration.Default.WithXml()); + + 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] + 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..8e1e1da 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,86 @@ 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); + + await parser.ParseDocumentAsync(document, cancellationToken).ConfigureAwait(false); + await RunScriptsAsync(context, document, cancellationToken).ConfigureAwait(false); + return document; + } + + /// + /// 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); + } + } } } } 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