File tree Expand file tree Collapse file tree 2 files changed +18
-9
lines changed
src/Html2OpenXml/Collections Expand file tree Collapse file tree 2 files changed +18
-9
lines changed Original file line number Diff line number Diff line change @@ -20,8 +20,7 @@ namespace HtmlToOpenXml;
20
20
/// </summary>
21
21
sealed class HtmlAttributeCollection
22
22
{
23
- private static readonly Regex stripStyleAttributesRegex = new ( @"(?<name>.+?):\s*(?<val>[^;]+);*\s*" ) ;
24
-
23
+ private static readonly Regex stripStyleAttributesRegex = new ( @"(?<name>[^;\s]+)\s?(&\#58;|:)\s?(?<val>[^;&]+)\s?(;|&\#59;)*" ) ;
25
24
private readonly Dictionary < string , string > attributes = [ ] ;
26
25
27
26
@@ -37,13 +36,7 @@ public static HtmlAttributeCollection ParseStyle(string? htmlTag)
37
36
38
37
// Encoded ':' and ';' characters are valid for browser but not handled by the regex (bug #13812 reported by robin391)
39
38
// ex= <span style="text-decoration:underline;color:red">
40
- MatchCollection matches = stripStyleAttributesRegex . Matches (
41
- #if NET5_0_OR_GREATER
42
- System . Web . HttpUtility . HtmlDecode ( htmlTag )
43
- #else
44
- HttpUtility . HtmlDecode ( htmlTag )
45
- #endif
46
- ) ;
39
+ MatchCollection matches = stripStyleAttributesRegex . Matches ( htmlTag ) ;
47
40
foreach ( Match m in matches )
48
41
collection . attributes [ m . Groups [ "name" ] . Value ] = m . Groups [ "val" ] . Value ;
49
42
Original file line number Diff line number Diff line change @@ -165,5 +165,21 @@ public void DuplicateStyle_ReturnsLatter()
165
165
var styleAttributes = HtmlAttributeCollection . ParseStyle ( "color:red;color:blue" ) ;
166
166
Assert . That ( styleAttributes [ "color" ] , Is . EqualTo ( "blue" ) ) ;
167
167
}
168
+
169
+ [ Test ( Description = "Encoded ':' and ';' characters are valid" ) ]
170
+ public void EncodedStyle_ShouldSucceed ( )
171
+ {
172
+ var styleAttributes = HtmlAttributeCollection . ParseStyle ( "text-decoration:underline;color:red" ) ;
173
+ Assert . That ( styleAttributes [ "text-decoration" ] , Is . EqualTo ( "underline" ) ) ;
174
+ Assert . That ( styleAttributes [ "color" ] , Is . EqualTo ( "red" ) ) ;
175
+ }
176
+
177
+ [ Test ( Description = "Key style with no value should be ignored" ) ]
178
+ public void EmptyStyle_ShouldBeIgnoredd ( )
179
+ {
180
+ var styleAttributes = HtmlAttributeCollection . ParseStyle ( "text-decoration;color:red" ) ;
181
+ Assert . That ( styleAttributes [ "text-decoration" ] , Is . Null ) ;
182
+ Assert . That ( styleAttributes [ "color" ] , Is . EqualTo ( "red" ) ) ;
183
+ }
168
184
}
169
185
}
You can’t perform that action at this time.
0 commit comments