Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to determine if a node belongs to a HTML vs. XHTML document in Fx 3.6+

7 views
Skip to first unread message

Steven Roussey

unread,
Nov 5, 2009, 5:21:27 PM11/5/09
to
In MDC:

namespaceURI :

The namespace URI of this node, or null if it is no namespace. In
Firefox 3.5 and earlier, HTML elements are in no namespace. In later
versions, HTML elements are in the http://www.w3.org/1999/xhtml
namespace in both HTML and XML trees. Requires Gecko 1.9.2.

I used this to determine if a node was part of a HTML vs. XHTML
document so we could figure out how to present nodes in Firebug (and
copy to output to text for Copy HTML). In Fx 3.6+, everything says it
is XHTML, even when clearly not.

So what other method can we use if not namespaceURI?

Boris Zbarsky

unread,
Nov 5, 2009, 5:34:08 PM11/5/09
to
On 11/5/09 5:21 PM, Steven Roussey wrote:
> I used this to determine if a node was part of a HTML vs. XHTML
> document so we could figure out how to present nodes in Firebug (and
> copy to output to text for Copy HTML). In Fx 3.6+, everything says it
> is XHTML, even when clearly not.
>
> So what other method can we use if not namespaceURI?

Hmm.... node.ownerDocument.contentType might sort of work as long as
you're not worried about synthetic documents (image, plaintext, etc).
If the type is text/html, it's HTML. If not, it's XHTML. Note that
your old approach fell down anyway if the node was created via
createElementNS....

That said, there are other behavior differences you should be able to
use here; e.g. element.nodeName is going to be lowercase in XHTML and
uppercase in HTML, modulo people messing with createElementNS in bogus
ways in HTML.

-Boris

Steven Roussey

unread,
Nov 5, 2009, 5:53:28 PM11/5/09
to

Thank you Boris, I'll look into those ideas. At one point I tried
using this:

function isXhtml(node)
{
var doctype = node.ownerDocument.documentElement.doctype;
return (doctype && doctype.indexOf('XHTML') != -1);
}

Can you lead me in the direction of why that doesn't work?

Boris Zbarsky

unread,
Nov 5, 2009, 6:44:03 PM11/5/09
to
On 11/5/09 5:53 PM, Steven Roussey wrote:
> var doctype = node.ownerDocument.documentElement.doctype;

I'd think this is always undefined, given that you're getting it off the
element, not the document, no?

> return (doctype&& doctype.indexOf('XHTML') != -1);

This would detect SVG as HTML, no?

-Boris

Steven Roussey

unread,
Nov 5, 2009, 7:48:10 PM11/5/09
to

Uppercase/lowercase it is...

Thank you!

Steven Roussey

unread,
Nov 5, 2009, 8:24:27 PM11/5/09
to
>
> Uppercase/lowercase it is...
>
> Thank you!

It does mean that https://developer.mozilla.org/En is html, not xhtml
when using this method, which I think is good since that is how the
browser sees it, correct? All the doctypes and stuff point to xhtml,
but it is served via the server as text/html.

Boris Zbarsky

unread,
Nov 5, 2009, 9:05:00 PM11/5/09
to
On 11/5/09 8:24 PM, Steven Roussey wrote:
> It does mean that https://developer.mozilla.org/En is html, not xhtml
> when using this method, which I think is good since that is how the
> browser sees it, correct? All the doctypes and stuff point to xhtml,
> but it is served via the server as text/html.

Yep. And it happens to not be well-formed XML, note.

-Boris

Steven Roussey

unread,
Nov 6, 2009, 2:54:39 AM11/6/09
to
On Nov 5, 6:05 pm, Boris Zbarsky <bzbar...@mit.edu> wrote:
> On 11/5/09 8:24 PM, Steven Roussey wrote:
>
> > It does mean thathttps://developer.mozilla.org/Enis html, not xhtml

> > when using this method, which I think is good since that is how the
> > browser sees it, correct? All the doctypes and stuff point to xhtml,
> > but it is served via the server as text/html.
>
> Yep.  And it happens to not be well-formed XML, note.
>
> -Boris

Ok, I was definitely getting it wrong before. Thanks again.

Jonas Sicking

unread,
Nov 12, 2009, 11:42:54 PM11/12/09
to

Out of curiosity, why do you care if the node belongs to a HTML or XHTML
document?

/ Jonas

Steven Roussey

unread,
Nov 13, 2009, 2:55:39 PM11/13/09
to

> Out of curiosity, why do you care if the node belongs to a HTML or XHTML
> document?

In firebug, people want to see a <br> in the HTML panel as a <br> not
a <br/>. Unless, of course, they are looking at xhtml. Same with
copying the HTML/XHTML as a string to the clipboard. That is 99%.

-steve--

Steven Roussey

unread,
Dec 16, 2009, 1:20:34 AM12/16/09
to

Bring this thread back to life for just a bit. What is the purpose of
a document.doctype then? Just letting us know what the user wrote,
regardless of its effect?

This page (https://developer.mozilla.org/En) has
document.doctype.publicId == "-//W3C//DTD XHTML 1.0 Transitional//EN".
But that is not the mode the browser is actually using as we discussed
before. I'd like to highlight when a user has defined a doctype and
the browser is ignoring it. Is there something like a doctype that the
browser is using internally that we can gain access to?

I'd like to show the doctype and put a red squiggly line under it if
it is not being used as expected. This sorta pulls to the idea of
presenting namespace info more easily (and displaying obvious errors
with the red underlines).

-steve--

0 new messages