IsDefaultNamespace

The following is a snippet to get isDefaultNamespace() supported across other browsers.

Note that all Gecko-based browsers (including Firefox) support Node.isDefaultNamespace. This function is not necessary for Gecko-based browsers (though the function will quickly return the standard value for Mozilla browsers).
// http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-isDefaultNamespace
// http://www.w3.org/TR/DOM-Level-3-Core/namespaces-algorithms.html#isDefaultNamespaceAlgo

function isDefaultNamespace(node, namespaceURI) {
    if (node.isDefaultNamespace) {
        return node.isDefaultNamespace(namespaceURI);
    }
    switch (node.nodeType) {
        case 1: //  ELEMENT_NODE
            if (!node.prefix) {
                return (node.namespaceURI === namespaceURI);
            }
            if (node.attributes.length) {
                for (var i=0; i < node.attributes.length; i++) {
                    var att = node.attributes[i];
                    if (att.localName === 'xmlns') {
                        return att.value === namespaceURI;
                    }
                }
            }
            if (node.parentNode) {
            // EntityReferences may have to be skipped to get to it
                return isDefaultNamespace(node.parentNode, namespaceURI);
            }
            else {
                return false; // unknown;
            }
        case 9: // DOCUMENT_NODE
            return isDefaultNamespace(node.documentElement, namespaceURI);
        case 6: // ENTITY_NODE
        case 12: // NOTATION_NODE
        case 10: // DOCUMENT_TYPE_NODE
        case 11: // DOCUMENT_FRAGMENT_NODE
            return false; // unknown
        case 2: // ATTRIBUTE_NODE:
            if (node.ownerElement ) {
                return isDefaultNamespace(node.ownerElement , namespaceURI);
            }
            else {
                return false; // unknown
            }
        default:
            if (node.parentNode) {
            // EntityReferences may have to be skipped to get to it
                return isDefaultNamespace(node.parentNode, namespaceURI);
            }
            else {
                return false; // unknown
            }
    }
}

 

Document Tags and Contributors

Last updated by: wbamberg,
Hide Sidebar