Skip to content

Xml prefixed attributes do not appropriately find namespace #20

Open
@jbrayfaithlife

Description

@jbrayfaithlife

Bug Report

During Xml parsing, attributes with an xml prefix ought to be associated with the xml namespace, even if such a namespace is not explicitly declared. According to https://www.w3.org/TR/REC-xml-names/#ns-decl :

The prefix xml is by definition bound to the namespace name http://www.w3.org/XML/1998/namespace. It MAY, but need not, be declared, and MUST NOT be bound to any other namespace name. Other prefixes MUST NOT be bound to this namespace name, and it MUST NOT be declared as the default namespace.

Prerequisites

  • [/] Can you reproduce the problem in a MWE?
  • [/] Are you running the latest version of AngleSharp?
  • [/] Did you check the FAQs to see if that helps you?
  • [/] Are you reporting to the correct repository? (there are multiple AngleSharp libraries, e.g., AngleSharp.Css for CSS support)
  • [/] Did you perform a search in the issues?

For more information, see the CONTRIBUTING guide.

Description

Xml prefixed attributes ought to be associated with the Xml namespace even if it has not been explicitly declared.

Steps to Reproduce

var xmlParser = new XmlParser();
var doc = xmlParser.ParseDocument("<xml xml:lang=\"en\">Test</xml>");
using (var stringWriter = new StringWriter()){
	doc.ToHtml(stringWriter, new XhtmlMarkupFormatter());
	stringWriter.ToString().Dump();
}

Expected behavior: Output should be <xml xml:lang=\"en\">Test</xml>

Actual behavior: Output is <xml lang="en">Test</xml>

Compare with the output to the following linqpad script

var xmlParser = new XmlParser();
var doc = xmlParser.ParseDocument("<xml xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xml:lang=\"en\">Test</xml>");
using (var stringWriter = new StringWriter()){
	doc.ToHtml(stringWriter, new XhtmlMarkupFormatter());
	stringWriter.ToString().Dump();
}
	

Output: <xml xmlns:="http://www.w3.org/XML/1998/namespace" xml:lang="en">Test</xml>

Environment details: Win 10; .NET 6.0.15

Possible Solution

In the XmlDomBuilder we need to replace this code:
with this:

if (prefix.Is(NamespaceNames.XmlPrefix))
{
    ns = NamespaceNames.XmlUri;
}
else if (!prefix.Is(NamespaceNames.XmlNsPrefix))
{
    ns = CurrentNode.LookupNamespaceUri(prefix);
}

A PR can be made to this effect with a test by which I confirmed the bug and solution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions