Last Comment Bug 531818 - If an <a> tag surrounds an <img> then <a>.getBoundingClientRect() ignores <img> height
: If an <a> tag surrounds an <img> then <a>.getBoundingClientRect() ignores <im...
Status: RESOLVED INVALID
[firebug-p2]
:
Product: Core
Classification: Components
Component: DOM: CSS Object Model (show other bugs)
: unspecified
: x86 Windows XP
: -- normal (vote)
: ---
Assigned To: Nobody; OK to take it and work on it
:
: Andrew Overholt [:overholt]
Mentors:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2009-11-30 06:54 PST by Michael Ratcliffe [:miker] [:mratcliffe]
Modified: 2009-11-30 15:23 PST (History)
5 users (show)
See Also:
Crash Signature:
(edit)
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---


Attachments
Test Case (567 bytes, text/html)
2009-11-30 06:55 PST, Michael Ratcliffe [:miker] [:mratcliffe]
no flags Details

Description Michael Ratcliffe [:miker] [:mratcliffe] 2009-11-30 06:54:16 PST
User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)

If an <a> tag surrounds an <img> then <a>.getBoundingClientRect() ignores <img> height.


Reproducible: Always

Steps to Reproduce:
The following code illustrates the problem:
<html>
<head>
    <script>
        function doit() {
            var el = document.getElementById('testlink'),
                rect = el.getBoundingClientRect();
                
            alert(rect.width + ' x ' + rect.height + ' ... result should be 267 x 110');
        }
    </script>
</head>
<body onload="doit();">
    <a id="testlink" href="http://www.google.com">
        <img src="http://www.google.com/logos/logo.gif" style="width:276px;height:110px;border:0;" />
    </a>
</body>
</html>
Actual Results:  
The height of the <a> tag does not match the <img> tag

Expected Results:  
The height of the <a> tag should match the <img> tag

This is related to a Firebug issue:
http://code.google.com/p/fbug/issues/detail?id=2534
Comment 1 Michael Ratcliffe [:miker] [:mratcliffe] 2009-11-30 06:55:18 PST
Created attachment 415125 [details]
Test Case
Comment 2 John J. Barton 2009-11-30 08:00:13 PST
Boris, I cc-ed you in case this bug can have impacts beyond Firebug.
Comment 3 Boris Zbarsky [:bz] (still a bit busy) 2009-11-30 08:03:22 PST
This is the right behavior.  getBoundingClientRect returns the size of the bounding client rect of the CSS boxes for the element.  It does NOT include the overflow.  Put a background on the <a> in this case (and make the image "visibility:hidden" or transparent or whatever) to see what the boxes for the <a> look like.
Comment 4 Boris Zbarsky [:bz] (still a bit busy) 2009-11-30 08:05:05 PST
Note that depending on how the highlighting is supposed to work in Firebug the Firebug bug cited in comment 0 may be invalid too.  I know what _I'd_ want out of highlighting in that case is to not include the full extent of the <img>, if I'm selecting the <a>.
Comment 5 John J. Barton 2009-11-30 08:14:38 PST
(In reply to comment #4)
> Note that depending on how the highlighting is supposed to work in Firebug the
> Firebug bug cited in comment 0 may be invalid too.  I know what _I'd_ want out
> of highlighting in that case is to not include the full extent of the <img>, if
> I'm selecting the <a>.

Maybe this is a difference between what Firebug users expect inspect to do and what getBoundingClientRect() provides.

I would expect that when I select the <a> in Firebug's HTML view, then Firebug will highlight the area of page where the <a> tag is active. I want that as a Firebug user to visually verify the maximum extent of my link. Then I can use the mouse on the page to look at details of the link extent.  The current result makes no sense and has no value to me: it only causes me to think Firebug or Firefox has a bug because the area highlighted for the <a> tag is too short or at least too low on the page.
Comment 6 Boris Zbarsky [:bz] (still a bit busy) 2009-11-30 08:44:11 PST
> then Firebug will highlight the area of page where the <a> tag is active.

This is, generally speaking, not a rectangle at all.  Heck, it's not in this testcase... or in this one:

  This is <a href="something">Some<br>
  text</a>

If the "where is the <a> active?" thing is really what you want, you probably want to take the union of all client rects (_not_ bounding client rects!) for all the DOM descendants of the <a>, not its bounding client rect at all.

As I said, what I generally want out of highlight functionality (and what DOM inspector provided) is some idea of what the layout boxes for the element look like.

Let's put this another way.  If <a> is replaced by a <span>, would you expect the highlight area to change?  If so, why?  If not, why does what you describe for <a> make sense for <span>?

In any case, none of that has to do with getBoundingClientRect directly...  Should probably move that to the Firebug bug.
Comment 7 Boris Zbarsky [:bz] (still a bit busy) 2009-11-30 08:45:36 PST
You might also want to look at bug 202942, fwiw.
Comment 8 John J. Barton 2009-11-30 09:02:23 PST
(In reply to comment #6)
...
> 
> As I said, what I generally want out of highlight functionality (and what DOM
> inspector provided) is some idea of what the layout boxes for the element look
> like.
> 
> Let's put this another way.  If <a> is replaced by a <span>, would you expect
> the highlight area to change? 

Yes.

> If so, why?  

Because I think of <a> as containing anything while I think of <span> as a text enclosing thing that an <img> would spill out of.  I guess this is also true for the other folks here.

> If not, why does what you describe
> for <a> make sense for <span>?

Sounds like the common sense ideas of <a> does not match its actual character as a span-like thing.

> 
> In any case, none of that has to do with getBoundingClientRect directly... 
> Should probably move that to the Firebug bug.

Evidently. Though we don't know what to do now, other than repeatedly explain this to users.
Comment 9 Boris Zbarsky [:bz] (still a bit busy) 2009-11-30 09:13:39 PST
(In reply to comment #8)
> Because I think of <a> as containing anything while I think of <span> as a text
> enclosing thing that an <img> would spill out of.

Ah, and from a DOM/CSS standpoint they're completely identical.

> I guess this is also true for the other folks here.

Which other folks?  It wasn't obvious to me that the bug report here was <a>-specific, nor that the Firebug bug was....

> Sounds like the common sense ideas of <a> does not match its actual character
> as a span-like thing.

For some values of "common sense", that's possible.

> Evidently. Though we don't know what to do now, other than repeatedly explain
> this to users.

If you really think the behavior for <a> should be completely different from everything else (which I would find confusing, myself, but maybe that's ok) then you could always special-case <a> and do something different for it, right?
Comment 10 John J. Barton 2009-11-30 09:20:28 PST
(In reply to comment #9)
> (In reply to comment #8)
> > Because I think of <a> as containing anything while I think of <span> as a text
> > enclosing thing that an <img> would spill out of.
> 
> Ah, and from a DOM/CSS standpoint they're completely identical.

Which is news to me...

> 
> > I guess this is also true for the other folks here.
> 
> Which other folks?  It wasn't obvious to me that the bug report here was
> <a>-specific, nor that the Firebug bug was....

Right, because you knew that <a> and <span> are similar. Other folks, including me, the reporter on the firebug bug, and Radcliffe thought this was <a>-specific because we did not expect that <a> and <span> where similar. We don't use <a> and <span> in similar ways. I bet this is a surprise to most web devs.

...
> > Evidently. Though we don't know what to do now, other than repeatedly explain
> > this to users.
> 
> If you really think the behavior for <a> should be completely different from
> everything else (which I would find confusing, myself, but maybe that's ok)
> then you could always special-case <a> and do something different for it,
> right?

As far as Firebug inspect goes, we cannot change it. It is doing the right thing here, even if we are surprised. If we want a "link area debugging tool", then we need to create one.
Comment 11 Robert O'Callahan (:roc) (email my personal email if necessary) 2009-11-30 13:22:00 PST
It seems to me that when you want to visualize the layout of an element, you should use getClientRects on the element. When you want to visualize event targeting, you should use something else --- I think it would be enough to collect up all the getClientRects of the element and all its descendants, include text node descendants. Text nodes don't support getClientRects, but you can create a Range covering the text node and call getClientRects on that. But if you want to include all the anonymous stuff, or be fast, we might need to create a new inspector API for this.

Maybe the thing to do would be to show the layout area and the event targeting area at the same time, e.g. boxes for the layout area and a translucent overlay for the event targeting area.
Comment 12 John J. Barton 2009-11-30 15:23:35 PST
(In reply to comment #11)
> It seems to me that when you want to visualize the layout of an element, you
> should use getClientRects on the element. 

Now here do you mean the union of getClientRects() or the result of getBoundingClientRects()? (we can cover this in the newsgroup see below).

> When you want to visualize event
> targeting, you should use something else --- I think it would be enough to
> collect up all the getClientRects of the element and all its descendants,
> include text node descendants. Text nodes don't support getClientRects, but you
> can create a Range covering the text node and call getClientRects on that. But
> if you want to include all the anonymous stuff, or be fast, we might need to
> create a new inspector API for this.

The Firebug inspect needs interactive speeds.

> 
> Maybe the thing to do would be to show the layout area and the event targeting
> area at the same time, e.g. boxes for the layout area and a translucent overlay
> for the event targeting area.

Yes, that sounds like it would be the right direction. 

I will ask about this on the layout newsgroup.

Note You need to log in before you can comment on or make changes to this bug.