Add-ons

LookupNamespaceURI

Add-ons using the techniques described in this document are considered a legacy technology in Firefox. Don't use these techniques to develop new add-ons. Use WebExtensions instead. If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions.

Starting from Firefox 53, no new legacy add-ons will be accepted on addons.mozilla.org (AMO) for desktop Firefox and Firefox for Android.

Starting from Firefox 57, only extensions developed using WebExtensions APIs will be supported on Desktop Firefox and Firefox for Android.

Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to use WebExtensions APIs if they can. See the "Compatibility Milestones" document for more information.

A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.

Here is an implementation of Node.lookupNamespaceURI which should work cross-browser.

Note that all Gecko-based browsers (including Firefox) support Node.lookupNamespaceURI. This function is not necessary for Gecko-based browsers (though the function will quickly return the standard value for Mozilla browsers) when used to reflect on static documents. However, due to bug 312019, this method does not work with dynamically assigned namespaces (e.g., those set with Node.prefix).
/*globals Document, HTMLDocument */
(function () {
'use strict';
var Doc = typeof Document !== 'undefined' ? Document : HTMLDocument; // Support HTMLDocument if Document not present // If Document is present, is this method also present?
 
 addLookupNamespaceURI(Doc);
 addLookupNamespaceURI(Element);
 
function addLookupNamespaceURI (type) {
    if (!type.prototype.lookupNamespaceURI) {
        type.prototype.lookupNamespaceURI = lookupNamespaceURI;
    }
    function lookupNamespaceURI (prefix) {
        return lookupNamespaceURIHelper(this, prefix);
    }
    function lookupNamespaceURIHelper (node, prefix) { // adapted directly from http://www.w3.org/TR/DOM-Level-3-Core/namespaces-algorithms.html#lookupNamespaceURIAlgo
        var i, att,
            htmlMode = document.contentType, // Mozilla only
            xmlnsPattern = /^xmlns:(.*)$/;

        switch (node.nodeType) {
            case 1: // ELEMENT_NODE (could also just test for Node.ELEMENT_NODE, etc., if supported in all browsers)
                if (node.namespaceURI != null && node.prefix === prefix)  {
                    // Note: prefix could be "null" in the case we are looking for default namespace
                    return node.namespaceURI;
                }
                if (node.attributes.length) {
                    for (i = 0; i < node.attributes.length; i++) {
                        att = node.attributes[i];
                        if (xmlnsPattern.test(att.name) && xmlnsPattern.exec(att.name)[1] === prefix) {
                            if (att.value) {
                                return att.value;
                            }
                            return null; // unknown
                        }
                        if (att.name === 'xmlns' && prefix == null) {
                        // default namespace
                            if (att.value) {
                                return att.value;
                            }
                            return null; // unknown
                        }
                    }
                }
                if (node.parentNode && node.parentNode.nodeType !== 9) {
                // EntityReferences may have to be skipped to get to it
                    return lookupNamespaceURIHelper(node.parentNode, prefix);
                }
                return null;
            case 9: // DOCUMENT_NODE
                return lookupNamespaceURIHelper(node.documentElement, prefix);
            case 6: // ENTITY_NODE
            case 12: // NOTATION_NODE
            case 10: // DOCUMENT_TYPE_NODE
            case 11: // DOCUMENT_FRAGMENT_NODE
                return null; // unknown
            case 2: // ATTRIBUTE_NODE
                if (node.ownerElement) {
                    return lookupNamespaceURIHelper(node.ownerElement, prefix);
                }
                return null; // unknown
            default:
                // TEXT_NODE (3), CDATA_SECTION_NODE (4), ENTITY_REFERENCE_NODE (5),
                // PROCESSING_INSTRUCTION_NODE (7), COMMENT_NODE (8)
                if (node.parentNode) {
                // EntityReferences may have to be skipped to get to it
                    return lookupNamespaceURIHelper(node.parentNode, prefix);
                }
                return null; // unknown
        }
    };
}

}());

Document Tags and Contributors

 Contributors to this page: bunnybooboo, wbamberg, Brettz9
 Last updated by: bunnybooboo,