NullReferenceException when parsing style tag

Apr 7, 2014 at 12:02 PM
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!
Developer
Apr 7, 2014 at 5:18 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.