Quantcast
Channel: HTML Renderer
Viewing all articles
Browse latest Browse all 693

Closed Unassigned: NullReferenceException when parsing style tag [9031]

$
0
0
Hello,

There is NullReferenceException when HTML text contains structure like this:
```
<v:shape style="color: Red" />
```

Stack Trace:
```
System.NullReferenceException: Object reference not set to an instance of an object.

at HtmlRenderer.Parse.DomParser.AssignCssBlock(CssBox box, CssBlock block) in HtmlRenderer 1.4.13.0\Source\HtmlRenderer\Parse\DomParser.cs:line 297

at HtmlRenderer.Parse.DomParser.CascadeStyles(CssBox box, HtmlContainer htmlContainer, CssData& cssData, Boolean& cssDataChanged) in HtmlRenderer 1.4.13.0\Source\HtmlRenderer\Parse\DomParser.cs:line 105

at HtmlRenderer.Parse.DomParser.CascadeStyles(CssBox box, HtmlContainer htmlContainer, CssData& cssData, Boolean& cssDataChanged) in HtmlRenderer 1.4.13.0\Source\HtmlRenderer\Parse\DomParser.cs:line 140

at HtmlRenderer.Parse.DomParser.GenerateCssTree(String html, HtmlContainer htmlContainer, CssData& cssData) in HtmlRenderer 1.4.13.0\Source\HtmlRenderer\Parse\DomParser.cs:line 43

at HtmlRenderer.HtmlContainer.SetHtml(String htmlSource, CssData baseCssData) in HtmlRenderer 1.4.13.0\Source\HtmlRenderer\HtmlContainer.cs:line 419
```

To fix that I added null check to CascadeStyles method of DomParser.

Existing code:
```
// Check for the style="" attribute
if (box.HtmlTag.HasAttribute("style"))
{
var block = CssParser.ParseCssBlock(box.HtmlTag.Name, box.HtmlTag.TryGetAttribute("style"));
AssignCssBlock(box, block);
}
```

Replaced to:
```
// Check for the style="" attribute
if (box.HtmlTag.HasAttribute("style"))
{
var block = CssParser.ParseCssBlock(box.HtmlTag.Name, box.HtmlTag.TryGetAttribute("style"));
if (block != null)
{
AssignCssBlock(box, block);
}
}
```

Because ParseCssBlock is able to return null, I added also null check to WriteHtmlTag method of DomUtils class.

Existing code:
```
else if (styleGen == HtmlGenerationStyle.Inline && att.Key == HtmlConstants.Style)
{
// if inline style add the styles to the collection
var block = CssParser.ParseCssBlock(box.HtmlTag.Name, box.HtmlTag.TryGetAttribute("style"));
foreach (var prop in block.Properties)
tagStyles[prop.Key] = prop.Value;
}
```

Replaced to:
```
else if (styleGen == HtmlGenerationStyle.Inline && att.Key == HtmlConstants.Style)
{
// if inline style add the styles to the collection
var block = CssParser.ParseCssBlock(box.HtmlTag.Name, box.HtmlTag.TryGetAttribute("style"));

if (block != null)
{
foreach (var prop in block.Properties)
tagStyles[prop.Key] = prop.Value;
}
}
```

It's just a quick fix.

It will be perfect if developer have a look into this bug =)

Thank you!
Comments: 1.4.14.0

Viewing all articles
Browse latest Browse all 693


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>