Site Navigation

Wednesday, October 17, 2007

bug 256 - DOM nodeType constants are not in IE and Opera

Issue: #256
Affects: IE5, IE5.5 IE6, IE7, IE8, Opera 8.2, Opera 9.2
Fixed in: Opera 9.50 alpha 1 build 9500
MSIE Feedback ID: 339307
Status: Microsoft has confirmed this will NOT be fixed in IE8 RTM

The ECMAScript specification for the DOM provides a set list of element types or nodeTypes... Text Elements, Node Elements, Comments, etc. They are numbered integers, with a Constant defined for easy to read comparison purposes. The only problem is, that they are not Constants at all, in IE or Opera.

NodeTypes:

interface Node {
// NodeType
const unsigned short ELEMENT_NODE = 1;
const unsigned short ATTRIBUTE_NODE = 2;
const unsigned short TEXT_NODE = 3;
const unsigned short CDATA_SECTION_NODE = 4;
const unsigned short ENTITY_REFERENCE_NODE = 5;
const unsigned short ENTITY_NODE = 6;
const unsigned short PROCESSING_INSTRUCTION_NODE = 7;
const unsigned short COMMENT_NODE = 8;
const unsigned short DOCUMENT_NODE = 9;
const unsigned short DOCUMENT_TYPE_NODE = 10;
const unsigned short DOCUMENT_FRAGMENT_NODE = 11;
const unsigned short NOTATION_NODE = 12;
//...
}


Example:

<script type="text/javascript">
var obj = document.getElementById('someID');
var textContent [];
for(var i=0;i<obj.childNodes.length;i++){
if(obj.childNodes.item(i).nodeType == TEXT_NODE){
textContent.push( obj.childNodes.item(i) );
}
}
alert('The inner text without HTML tags is: ' + textContent.join(', '));
</script>


In a spec compliant browser, the code should be able to test against the TEXT_NODE constant.


Known Workarounds: One. It is extra processing, but each element type can be defined as globals at the begining of every page.

Example Workaround Code:

<script type="text/javascript">
(function(){
var NodeTypes = ['ELEMENT', 'ATTRIBUTE', 'TEXT', 'CDATA_SECTION',
'ENTITY_REFERENCE', 'ENTITY', 'PROCESSING_INSTRUCTION',
'COMMENT', 'DOCUMENT', 'DOCUMENT_TYPE',
'DOCUMENT_FRAGMENT', 'NOTATION'];
for(var i=0i<NodeTypes.length;i++){
window[NodeTypes[i] + '_NODE'] = (i + 1);
}
})();
</script>



Related Issues: None.