Skip to content

Commit

Permalink
Merge pull request jamietre#132 from codesmithtools/master
Browse files Browse the repository at this point in the history
Allow specifying !important on style unit values.
  • Loading branch information
jamietre committed May 22, 2015
2 parents b88e6ea + 32c616a commit 696ac05
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
10 changes: 10 additions & 0 deletions source/CsQuery.Tests/Core/Css/Css.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ public void RemoveStyle()
Assert.AreEqual("height: 10;", dom["div"][0]["style"]);
}

[Test, TestMethod]
public void SetHeightWithImportant() {
CQ dom = "<div style='height: 10; width: 10; display: none;'>";

Assert.AreEqual("height: 10; width: 10; display: none;", dom["div"][0]["style"]);

dom["div"].Css("height", "10px !important");
Assert.AreEqual("height: 10px !important; width: 10; display: none;", dom["div"][0]["style"]);
}

protected class StylesClass
{
public string width;
Expand Down
7 changes: 6 additions & 1 deletion source/CsQuery/Dom/Implementation/CSSStyleDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ public void SetStyle(string name, string value, bool strict)
}
break;
case CSSStyleType.Option:
if (!style.Options.Contains(value))
if (!style.Options.Contains(value.Replace("!important", String.Empty)))
{
throw new ArgumentException("The value '" + value + "' is not allowed for attribute '"
+ name + "'. Valid options are: " + OptionList(style));
Expand Down Expand Up @@ -807,6 +807,9 @@ protected string ValidateUnitString(string name,string value)
StringBuilder outVal = new StringBuilder();
//TODO: this is not comprehensive.
string type = name=="opacity" ? "" : "px";
bool important = value.Contains("!important");
if (important)
value = value.Replace("!important", String.Empty);

if (String.IsNullOrEmpty(value))
{
Expand Down Expand Up @@ -845,6 +848,8 @@ protected string ValidateUnitString(string name,string value)
throw new ArgumentException("No data provided for attribute, data: '" + value + "'");
}
outVal.Append(type);
if (important)
outVal.Append(" !important");
return outVal.ToString();
}

Expand Down

0 comments on commit 696ac05

Please sign in to comment.