tags:

views:

42

answers:

2

I started working on an experimental project tonight. I've realized that I need to determine if a group of selected nodes are self closing or not.

For example, suppose I query the dom and get this collection of nodes:

<br/><br/><p></p><div></div></br/>

Is there a property on the elements that can determine which are which?

Moreover, rather than filter on specific html elements (oh, if this were the only limitations), suppose that I am parsing an XML document that can contain arbitrarily named tags.

A: 

The only thing I can think of is for each of those nodes doing a tostring on them and checking that the last two characters are /> but as far as I know theres no predefined method of finding out whether a node is self-closed or not

I may be wrong about the tostring but there is a way to just get the entire node as text - i know theres a jQuery function for that but I cant remember how to do it in pure JS.

Darko Z
Such a lovely idea. I just tried it, and sadly the various browsers I tested always parse the nodes to their DOM types, whether the elements are written correctly or not.
Geuis
Bugger... will think about it and let you know if I come up with anything...
Darko Z
Try getting the innerHTML from the parent of the node? it means you have to isolate the node you're looking at but it may give you the HTML as is and not parsed already?
Darko Z
+1  A: 

XML does not differentiate self closing tags from empty tags, so <p /> and <p></p> are identical, as far as XML is concerned.

Some XML parsers will parse all such structures to be <p /> some will parse them all to <p></p> and some will just leave them as they are.

I would say there is no foolproof way to do this - you will have to specifically test on your browser of choice, see what exactly is returned and if you can work with that (searching for /> for example).

Oded