Skip to content

Commit

Permalink
added support for empty attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ventaur committed Oct 12, 2012
1 parent aa988f7 commit 6ea8b04
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
32 changes: 22 additions & 10 deletions src/HtmlTags.Testing/HtmlTagTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,16 +321,6 @@ public void set_an_attribute_to_null_should_remove_the_attribute()
tag.HasAttr("name").ShouldBeFalse();
}

[Test]
public void set_an_attribute_to_empty_string_should_remove_the_attribute()
{
var tag = new HtmlTag("div");
tag.Attr("name", "bill");
tag.HasAttr("name").ShouldBeTrue();
tag.Attr("name", "");
tag.HasAttr("name").ShouldBeFalse();
}

[Test]
public void set_the_class_attribute_to_null_should_remove_all_classes()
{
Expand Down Expand Up @@ -376,6 +366,28 @@ public void retrieve_a_non_existing_attr_should_return_an_empty_string()
new HtmlTag("div").Attr("new").ShouldEqual(string.Empty);
}

[Test]
public void set_an_attribute_to_empty_string_should_not_remove_the_attribute() {
var tag = new HtmlTag("div");
tag.Attr("name", "bill");
tag.HasAttr("name").ShouldBeTrue();
tag.Attr("name", string.Empty);
tag.HasAttr("name").ShouldBeTrue();
}

[Test]
public void retrieve_an_empty_attr_should_return_an_empty_string() {
var tag = new HtmlTag("option");
tag.Attr("value", string.Empty);
tag.Attr("value").ShouldEqual(string.Empty);
}

[Test]
public void render_empty_attr() {
var tag = new HtmlTag("option").Attr("value", string.Empty);
tag.ToString().ShouldEqual("<option value=\"\"></option>");
}

//
// CSS classes
//
Expand Down
8 changes: 6 additions & 2 deletions src/HtmlTags/HtmlTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,14 +539,18 @@ public HtmlTag UnencodedAttr(string attribute, object value)

private HtmlTag buildAttr(string attribute, object value, bool encode = true)
{
if (value == null || value.Equals(string.Empty))
if (value == null)
{
return RemoveAttr(attribute);
}
if (value.Equals(string.Empty) && (isCssClassAttr(attribute) || isCssStyleAttr(attribute) || isMetadataAttr(attribute)))
{
return RemoveAttr(attribute);
}
if (isCssClassAttr(attribute))
{
AddClass(value.ToString());
}
}
else
{
_htmlAttributes[attribute] = new HtmlAttribute(value.ToString(), encode);
Expand Down

0 comments on commit 6ea8b04

Please sign in to comment.