Skip to content

Commit

Permalink
Preserving xml prefixes in tags. Fixing 4404, 4406, 4407
Browse files Browse the repository at this point in the history
  • Loading branch information
napernik committed May 29, 2013
1 parent 0b8fcd8 commit 9707614
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,22 @@ private static XElement CopyWithoutNamespace(XElement source, XNamespace namespa
XName newName = sourceNs.Equals(namespaceToRemove) ? source.Name.LocalName : source.Name;
XElement copy = new XElement(newName);

if (!sourceNs.Equals(namespaceToRemove) && sourceNs != source.Parent.Name.Namespace && source.Attribute("xmlns") == null)
if (!sourceNs.Equals(namespaceToRemove)
&& sourceNs != source.Parent.Name.Namespace
&& source.Attribute("xmlns") == null
&& (sourceNs == Namespaces.Xhtml.NamespaceName || sourceNs == Namespaces.Svg.NamespaceName))
{
copy.Add(new XAttribute("xmlns", source.Name.Namespace));
}

copy.Add(source.Attributes().Where(a => a.Name.Namespace == namespaceToRemove).Select(a => new XAttribute(a.Name.LocalName, a.Value)));
copy.Add(source.Attributes().Where(a => a.Name.Namespace != namespaceToRemove && (a.IsNamespaceDeclaration == false || a.Value != sourceNs)).Select(a => new XAttribute(a.Name, a.Value)));
copy.Add(source.Attributes().Where(a => a.Name.Namespace == namespaceToRemove)
.Select(a => new XAttribute(a.Name.LocalName, a.Value)));

Func<XAttribute, bool> isNotHtmlRelatedNsDeclaration =
ns => !ns.IsNamespaceDeclaration || (ns.Value != Namespaces.Xhtml.NamespaceName && ns.Value != Namespaces.Svg.NamespaceName);

copy.Add(source.Attributes().Where(a => a.Name.Namespace != namespaceToRemove && isNotHtmlRelatedNsDeclaration(a))
.Select(a => new XAttribute(a.Name, a.Value)));

foreach (XNode child in source.Nodes())
{
Expand Down

0 comments on commit 9707614

Please sign in to comment.