Skip to content

Commit ea0d66a

Browse files
author
ArthurHub
committed
* add GetElementRectangle to get element rectangle by element id
1 parent 0915814 commit ea0d66a

1 file changed

Lines changed: 19 additions & 7 deletions

File tree

‎Source/HtmlRenderer/HtmlContainer.cs‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,21 @@ public string GetLinkAt(Point location)
460460
return link != null ? link.HrefLink : null;
461461
}
462462

463+
/// <summary>
464+
/// Get the rectangle of html element as calculated by html layout.<br/>
465+
/// Element if found by id (id attribute on the html element).<br/>
466+
/// Note: to get the screen rectangle you need to adjust by the hosting control.<br/>
467+
/// </summary>
468+
/// <param name="elementId">the id of the element to get its rectangle</param>
469+
/// <returns>the rectangle of the element or null if not found</returns>
470+
public RectangleF? GetElementRectangle(string elementId)
471+
{
472+
ArgChecker.AssertArgNotNullOrEmpty(elementId, "elementId");
473+
474+
var box = DomUtils.GetBoxById(_root, elementId);
475+
return box != null ? CommonUtils.GetFirstValueOrDefault(box.Rectangles, box.Bounds) : (RectangleF?)null;
476+
}
477+
463478
/// <summary>
464479
/// Measures the bounds of box and children, recursively.
465480
/// </summary>
@@ -788,22 +803,19 @@ internal void HandleLinkClicked(Control parent, MouseEventArgs e, CssBox link)
788803
throw new HtmlLinkClickedException("Error in link clicked intercept", ex);
789804
}
790805
if( args.Handled )
791-
{
792806
return;
793-
}
794807
}
795808

796809
if( !string.IsNullOrEmpty(link.HrefLink) )
797810
{
798-
if( link.HrefLink.StartsWith("#") )
811+
if( link.HrefLink.StartsWith("#") && link.HrefLink.Length > 1 )
799812
{
800813
if( ScrollChange != null )
801814
{
802-
var box = DomUtils.GetBoxById(_root, link.HrefLink.Substring(1));
803-
if( box != null )
815+
var rect = GetElementRectangle(link.HrefLink.Substring(1));
816+
if( rect.HasValue )
804817
{
805-
var rect = CommonUtils.GetFirstValueOrDefault(box.Rectangles, box.Bounds);
806-
ScrollChange(this, new HtmlScrollEventArgs(Point.Round(rect.Location)));
818+
ScrollChange(this, new HtmlScrollEventArgs(Point.Round(rect.Value.Location)));
807819
HandleMouseMove(parent, e);
808820
}
809821
}

0 commit comments

Comments
 (0)