Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.3.2

Released on Monday, November 23 2026.

- Fixed a document created without navigating (e.g. by `DOMParser` or `XMLHttpRequest`) replacing the page and running its scripts

# 1.3.1

Released on Sunday, November 22 2026.
Expand Down
2 changes: 1 addition & 1 deletion src/AngleSharp.Xml.Docs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@anglesharp/xml",
"version": "1.3.1",
"version": "1.3.2",
"preview": true,
"description": "The doclet for the AngleSharp.Xml documentation.",
"keywords": [
Expand Down
12 changes: 0 additions & 12 deletions src/AngleSharp.Xml.Tests/Dom/Acid3Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,5 @@ await context.OpenAsync(res => res
{
}
}

[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);
}
}
}
25 changes: 25 additions & 0 deletions src/AngleSharp.Xml.Tests/Dom/XmlDocumentFactories.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
namespace AngleSharp.Xml.Tests.Dom
{
using AngleSharp.Dom;
using AngleSharp.Io;
using AngleSharp.Xml.Dom;
using NUnit.Framework;
using System;
using System.Threading;
using System.Threading.Tasks;

[TestFixture]
public class XmlDocumentFactories
{
[Test]
public async Task DocumentThatIsJustDataDoesNotReplaceThePage()
{
var navigationDisabled = typeof(CreateDocumentOptions).GetProperty("IsNavigationDisabled");

if (navigationDisabled is null)
{
Assert.Ignore("The option is part of AngleSharp 1.10.");
}

var context = BrowsingContext.New(Configuration.Default.WithXml());
var page = await context.OpenAsync(res => res.Content("<!doctype html><title>page</title>").Address("http://localhost/"));
var response = VirtualResponse.Create(res => res.Content("<root/>").Address("http://localhost/data.xml").Header("Content-Type", "application/xml"));
var options = new CreateDocumentOptions(response);
navigationDisabled.SetValue(options, true);

var document = await context.GetService<IDocumentFactory>().CreateAsync(context, options, CancellationToken.None);

Assert.AreEqual("root", document.DocumentElement.LocalName);
Assert.AreSame(page, context.Active);
}

[Test]
public void DeclarationMetadataIsExposed()
{
Expand Down
38 changes: 21 additions & 17 deletions src/AngleSharp.Xml/XmlConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,9 @@ 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<XmlLibrary>().Any())
{
configuration = configuration.With(XmlLibrary.Instance);
}

return configuration.WithOnly<IXmlParser>(ctx => new XmlParser(ctx));
}

/// <summary>
/// Marks a configuration as using this library.
/// </summary>
private sealed class XmlLibrary
{
public static readonly XmlLibrary Instance = new XmlLibrary();
}

private static Task<IDocument> LoadXmlAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancellationToken)
{
var document = new XmlDocument(context, options.Source);
Expand All @@ -72,13 +57,32 @@ private static async Task<IDocument> LoadAsync(IBrowsingContext context, Documen
{
var parser = context.GetService<IXmlParser>() ?? throw new InvalidOperationException("The IXmlParser service has been removed. Cannot continue.");
document.Setup(options.Response, options.ContentType, options.ImportAncestor);
context.NavigateTo(document);

// a document that is just data (e.g. of DOMParser or XMLHttpRequest) neither replaces the page, nor runs anything
if (!IsSet(options, "IsNavigationDisabled"))
{
context.NavigateTo(document);
}

await parser.ParseDocumentAsync(document, cancellationToken).ConfigureAwait(false);
await RunScriptsAsync(context, document, cancellationToken).ConfigureAwait(false);

if (!IsSet(options, "IsNavigationDisabled") && !IsSet(options, "IsScriptingDisabled"))
{
await RunScriptsAsync(context, document, cancellationToken).ConfigureAwait(false);
}

return document;
}

/// <summary>
/// Reads an option of AngleSharp 1.10 (or later), which is not set with an earlier version.
/// </summary>
private static Boolean IsSet(CreateDocumentOptions options, String name)
{
var property = typeof(CreateDocumentOptions).GetProperty(name);
return property?.GetValue(options) is true;
}

/// <summary>
/// Runs the (inline) scripts of the XHTML elements of a document that was navigated to,
/// in tree order once it has been parsed.
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<Description>Adds a powerful XML and DTD parser to AngleSharp.</Description>
<Product>AngleSharp.Xml</Product>
<Version>1.3.1</Version>
<Version>1.3.2</Version>
<LangVersion>latest</LangVersion>
<SignAssembly>true</SignAssembly>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
Loading