Permalink
Browse files

- fixing formatting of prefixes without explicit namespaces (sometime…

…s found in non-XHTML markup)
  • Loading branch information...
1 parent 3431872 commit 46a0129104ac31291467f657292aab43f8883d8d @mckamey mckamey committed Aug 27, 2010
@@ -234,6 +234,25 @@ public void Format_NestedDefaultNamespaces_ReturnsMarkup()
Assert.Equal(expected, actual);
}
+ [Fact]
+ [Trait(TraitName, TraitValue)]
+ public void Format_PrefixWithoutNamespace_ReturnsPrefix()
+ {
+ var input = new[]
+ {
+ MarkupGrammar.TokenElementBegin(new DataName("div")),
+ MarkupGrammar.TokenAttribute(new DataName("visible", "jbst", null)),
+ MarkupGrammar.TokenPrimitive(false),
+ MarkupGrammar.TokenElementEnd,
+ };
+ const string expected = @"<div jbst:visible=""false""></div>";
+
+ var formatter = new HtmlFormatter(new DataWriterSettings());
+ var actual = formatter.Format(input);
+
+ Assert.Equal(expected, actual);
+ }
+
#endregion Namespace Tests
#region Simple Attribute Tests
@@ -241,16 +241,18 @@ public void Format(IEnumerable<Token<MarkupTokenType>> tokens, TextWriter writer
DataName attrName = token.Name;
string prefix = this.ScopeChain.EnsurePrefix(attrName.Prefix, attrName.NamespaceUri);
- if (prefix != attrName.Prefix)
+ if (prefix != null)
{
- attrName = new DataName(attrName.LocalName, prefix, attrName.NamespaceUri, true);
- }
-
- if (prefix != null &&
- !this.ScopeChain.ContainsNamespace(attrName.NamespaceUri) &&
- (!String.IsNullOrEmpty(attrName.NamespaceUri) || this.ScopeChain.ContainsPrefix(String.Empty)))
- {
- scope[prefix] = attrName.NamespaceUri;
+ if (prefix != attrName.Prefix)
+ {
+ attrName = new DataName(attrName.LocalName, prefix, attrName.NamespaceUri, true);
+ }
+
+ if (!this.ScopeChain.ContainsNamespace(attrName.NamespaceUri) &&
+ (!String.IsNullOrEmpty(attrName.NamespaceUri) || this.ScopeChain.ContainsPrefix(String.Empty)))
+ {
+ scope[prefix] = attrName.NamespaceUri;
+ }
}
stream.Pop();
@@ -326,7 +328,7 @@ public void Format(IEnumerable<Token<MarkupTokenType>> tokens, TextWriter writer
return;
}
- string tagPrefix = this.ScopeChain.EnsurePrefix(tagName.Prefix, tagName.NamespaceUri);
+ string tagPrefix = this.ScopeChain.EnsurePrefix(tagName.Prefix, tagName.NamespaceUri) ?? tagName.Prefix;
// "<"
writer.Write(MarkupGrammar.OperatorElementBegin);
@@ -363,7 +365,7 @@ public void Format(IEnumerable<Token<MarkupTokenType>> tokens, TextWriter writer
// "The attribute value in a default namespace declaration MAY be empty.
// This has the same effect, within the scope of the declaration, of there being no default namespace."
// http://www.w3.org/TR/xml-names/#defaulting
- string attrPrefix = this.ScopeChain.EnsurePrefix(attribute.Key.Prefix, attribute.Key.NamespaceUri);
+ string attrPrefix = this.ScopeChain.EnsurePrefix(attribute.Key.Prefix, attribute.Key.NamespaceUri) ?? attribute.Key.Prefix;
this.WriteAttribute(writer, attrPrefix, attribute.Key.LocalName, attribute.Value);
}
@@ -471,6 +471,12 @@ public void Clear()
#region Utility Methods
+ /// <summary>
+ /// Looks up the prefix for the given namespace
+ /// </summary>
+ /// <param name="preferredPrefix"></param>
+ /// <param name="namespaceUri"></param>
+ /// <returns>null if namespace is empty and no default prefix found</returns>
public string EnsurePrefix(string preferredPrefix, string namespaceUri)
{
string storedPrefix = this.GetPrefix(namespaceUri, false);

0 comments on commit 46a0129

Please sign in to comment.