img and HtmlRenderer.GetAttributeAt

Dec 31, 2013 at 1:30 PM
Hi, I'm trying to use GetAttributeAt method to get the "alt" attribute of an img tag.
For testing purpose, I used a very simple peace of html:
<html><body><img src="human.png" alt="human"/></body></html>
The image is provided to the HtmlContainer via ImageLoad event.
I also set AvoidImagesLateLoading to True.

The problem is that GetAttributeAt never works.
the DomUtils.GetCssBox test else if (childBox.Bounds.Contains(location)) fails. It seems the actual size of the image is not recorded in the CSSBoxes tree.

Am I doing something wrong? Should I tell the container at one moment to store the actual size of the image?

And thanks for this awesome library!
Jan 6, 2014 at 11:20 AM
I fixed the problem by modifying DomUtils.GetCssBox.
It was also unable to work with td.

Here is the code in case anyone faces the same problem:
    /// <summary>
    /// Get css box under the given sub-tree at the given x,y location, get the inner most.<br/>
    /// the location must be in correct scroll offset.
    /// </summary>
    /// <param name="box">the box to start search from</param>
    /// <param name="location">the location to find the box by</param>
    /// <param name="visible">Optional: if to get only visible boxes (default - true)</param>
    /// <returns>css link box if exists or null</returns>
    public static CssBox GetCssBox(CssBox box, Point location, bool visible = true)
    {

      if (box != null)
      {
        if ((!visible || box.Visibility == CssConstants.Visible) && (box.Bounds.IsEmpty || box.Bounds.Contains(location)))
        {
          foreach (var childBox in box.Boxes)
          {
            var bounds = (childBox.IsImage ? childBox.FirstWord.Rectangle : childBox.Bounds);
            if (bounds.Contains(location) || (bounds.IsEmpty && childBox.Boxes.Count != 0))
            {
              return GetCssBox(childBox, location) ?? childBox;
            }
          }
        }
      }

      return null;
    }
Sorry if this isn't the perfect implementation ; I don't really know if that's the good way things should be done but at least it works better than the original.
Developer
Jan 6, 2014 at 2:28 PM
Looks like a bug, I will look into it, and your suggested fix, and add it to next minor release. Thx for investing the time.

May I ask the scenario you are using it for?
I wonder if there is a better way (regardless of the actual bug).
Jan 6, 2014 at 4:35 PM
Sure.
I use your HtmlContainer to display some small pieces of html in a winforms datagridview.
I have to display a tooltip whenever the mouse cursor hovers an image (img) or a piece of text (span) with an 'alt' or 'title' attribute, just like in a regular browser.
Hence my use of the GetAttributeAt method.
Developer
Jan 18, 2014 at 11:31 AM
Cool usage, fixed in 1.4.12.0