Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespace declaration processed first. Issue 22 #23

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions src/AngleSharp.Xml.Tests/Parser/XmlParsing.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using System.Linq;
using System.Threading.Tasks;
using AngleSharp.Dom;

namespace AngleSharp.Xml.Tests.Parser
{
using AngleSharp.Xml.Parser;
Expand Down Expand Up @@ -147,6 +151,15 @@ public void ParseInvalidXmlShouldNotThrowWhenSuppressingErrors_Issue14()
});
}

[Test]
public async Task NamespaceDeclarationsInAttributesShouldNotCareAboutOrdering()
{
var document = @"<xml p6:type=""noteref"" xmlns:p6=""http://www.idpf.org/2007/ops"" >1</xml>"
.ToXmlDocument();
var root = document.DocumentElement;
Assert.AreEqual("http://www.idpf.org/2007/ops",
root.Attributes.First(att => att.LocalName == "type").NamespaceUri);

[Test]
public async Task XmlPrefixedAttributesShouldLocateXmlNamespaceWithoutDeclaration()
{
Expand Down
20 changes: 18 additions & 2 deletions src/AngleSharp.Xml/Parser/XmlDomBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Linq;

namespace AngleSharp.Xml.Parser
{
using AngleSharp.Dom;
Expand Down Expand Up @@ -274,9 +276,23 @@ private void InBody(XmlToken token)
var element = CreateElement(tagToken.Name, tagToken.IsSelfClosing);
CurrentNode.AppendChild(element);

for (var i = 0; i < tagToken.Attributes.Count; i++)
var namespaceDeclarations = tagToken.Attributes
.Where(attr => attr.Key.StartsWith("xmlns"))
.ToList();
var otherAttributes = tagToken.Attributes
.Where(attr => !attr.Key.StartsWith("xmlns"))
.ToList();

for (var i = 0; i < namespaceDeclarations.Count; i++)
{
var attr = namespaceDeclarations[i];
var item = CreateAttribute(attr.Key, attr.Value.Trim());
element.AddAttribute(item);
}

for (var i = 0; i < otherAttributes.Count; i++)
{
var attr = tagToken.Attributes[i];
var attr = otherAttributes[i];
var item = CreateAttribute(attr.Key, attr.Value.Trim());
element.AddAttribute(item);
}
Expand Down
Loading