views:

356

answers:

3

Hey guys

I have the following is really weird. Bassically when I view the source of the page everything looks fine but the page looks all wrong. So I decided to take a look at the source using firebug and firebug shows a very different story. But if I refresh the page the page looks fine and the source and firebug match up.

See below for what the source is but what firefox displays and firebug shows:

View source shows this:

<div class="mainpanel">  
    <a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Little-Rock"> 
        <div class="thumbphotoimage"><table cellpadding="0" cellspacing="0"><tr><td><img src="/Assets/Photos/Portfolio/BB001D_0.jpg" alt="Little Rock" /></td></tr></table></div>
        <div class="thumbphototitle">Little Rock</div> 
    </a>  
    <a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Split-Rock"> 
        <div class="thumbphotoimage"><table cellpadding="0" cellspacing="0"><tr><td><img src="/Assets/Photos/Portfolio/BB002D_0.jpg" alt="Split Rock" /></td></tr></table></div>
        <div class="thumbphototitle">Split Rock</div>
    </a>  
    <a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Rock-Pointer"> 
        <div class="thumbphotoimage"><table cellpadding="0" cellspacing="0"><tr><td><img src="/Assets/Photos/Portfolio/BB003D_0.jpg" alt="Rock Pointer" /></td></tr></table></div>
        <div class="thumbphototitle">Rock Pointer</div>
    </a>   
</div> 

But firebug shows this and it renders on the screen as if its like this:

<div class="mainpanel">  
    <a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Little-Rock"> 
        <div class="thumbphotoimage"><table cellpadding="0" cellspacing="0"><tr><td><img src="/Assets/Photos/Portfolio/BB001D_0.jpg" alt="Little Rock" /></td></tr></table></div>
        <div class="thumbphototitle">Little Rock</div> 
    </a>  

    <a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Split-Rock"></a> 
    <div class="thumbphotoimage"><table cellpadding="0" cellspacing="0"><tr><td><img src="/Assets/Photos/Portfolio/BB002D_0.jpg" alt="Split Rock" /></td></tr></table></div>
    <a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Split-Rock"> </a> 
    <div class="thumbphototitle">
        <a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Split-Rock">Split Rock</a>
    </div> 
    <a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Split-Rock"> </a> 

    <a class="thumbphoto" onclick="window.location=this.href;return false;" href="/Photograph/Narooma/Rock-Pointer"> 
        <div class="thumbphotoimage"><table cellpadding="0" cellspacing="0"><tr><td><img src="/Assets/Photos/Portfolio/BB003D_0.jpg" alt="Rock Pointer" /></td></tr></table></div>
        <div class="thumbphototitle">Rock Pointer</div>
    </a>   
</div>

The offending html is the middle a tag which goes crazy...

Any ideas.

Cheers Anthony

+3  A: 

You should not wrap block elements/tags (like <div>) in inline tags (like <a>). That's asking for trouble.

Max Shawabkeh
see the above comment and I know you "shouldn't" do this but it doesn't exsplain why the render randomly gets out of wack.
vdhant
That's exactly what explains why it 'randomly gets out of wack'.
Daniel
I'm sorry but I would exspect it to be consitent with what it renders. How can it descide to render it one way one minute and then render it another sometime later... what type of code would they have that would render it two different ways given the same input?
vdhant
+3  A: 

That's because your HTML is invalid. Inline elements can only contain other inline elements and cannot contain block elements.

Browsers encountering HTML which breaks this rule is allowed to do anything at all in order to parse the page (including not displaying the page) and apparently firefox's interpretation of anything-at-all is not the same as yours.

Note that you can convert inline elements like <span> to a block element by setting it's display css property. But I'm not entirely sure if that is legal for an element with additional semantics such as an <a> tag. Of course, you could convert those divs to inline elements.

slebetman
Ok i get what you are saying but it doesn't exsplain why it isn't consistent... why does it do it to this one and not the others???
vdhant
For that, you'll need to ask Firefox devs. Browser bug handling is a black art.
slebetman
Lol seems like it... this is the strangest thing I have come across.
vdhant
+7  A: 

Like others said, this happens because your markup is invalid. Going a bit deeper, the problem is that when the parser received <a><div> in its input, it may mean two things:

  1. You forgot to close the anchor tag, in which case this should become <a></a><div>... in the DOM, or
  2. The anchor wraps the div, in which case the DOM should be <a><div></div></a>.

The correct decision can be made only when more (potentially much more) characters are known; the parsing, as you could have noticed, happens incrementally -- i.e. you can see parts of the page before it's fully downloaded.

Unfortunately, the Mozilla's HTML parser (as of Firefox 3.6 and earlier) is non-deterministic in this case -- the resulting DOM depends on the portions your HTML is split into, while going over network.

There's a Mozilla bug about a problem that looks very similar to yours.

I'm sorry for you, and I don't know how to implement (nor have any desire to try implementing ;) the solution to your original problem, but perhaps a hack involving setting innerHTML (to avoid parser non-determinism) is in order?

BTW, it would be interesting to check how the HTML5 parsing algorithm says your markup should be treated, since that's what will eventually be implemented in the browsers.

Nickolay
+1: That bug looks to be exactly the problem here.
Max Shawabkeh
Thanks for the info. This is the answer I was looking for and seems to address the issues that I have mentioned. I didn't exspect people to fix the implementation just to help me understand what is going on. From some of the feedback here I have been able to adjust the html and replace the divs with spans and this has fixed the issue for the time being. I agree, how html 5 deals with this will be interesting and is something that I will have to keep a close eye on. But by then hopefully there will be a better way all together to solve the core alignment issue.
vdhant
@vdhant: HTML 5 parsing is closer than you may think, see http://ejohn.org/blog/html-5-parsing/ - it links to the specification you can check and to the information how to test it in Firefox.
Nickolay