Skip to content

isXMLDoc() - Uncaught TypeError: Cannot read property 'namespaceURI' of undefined #4782

Closed
jquery/sizzle
#474

Description

@borzaka

Description

isXMLDoc() throws an error if undefined given, starting from version 3.4.0

Test script:

jQuery.isXMLDoc(undefined)

jQuery 3.3.1 result:

false

jQuery 3.5.1 (currently the latest, but starting from 3.4.0) result:

Uncaught TypeError: Cannot read property 'namespaceURI' of undefined

Techinfo

Before the Sizzle update from 2.3.3 to 2.3.4 (see related commit below), isXML() function looked liked this:

isXML = Sizzle.isXML = function( elem ) {
	// documentElement is verified for cases where it doesn't yet exist
	// (such as loading iframes in IE - #4833)
	var documentElement = elem && (elem.ownerDocument || elem).documentElement;
	return documentElement ? documentElement.nodeName !== "HTML" : false;
};

This doesn't throw an error, when the elem is undefined, because it checks it.

In the update from 3.4.0, it looks like this:

isXML = Sizzle.isXML = function( elem ) {
	var namespace = elem.namespaceURI,
		docElem = ( elem.ownerDocument || elem ).documentElement;

	// Support: IE <=8
	// Assume HTML when documentElement doesn't yet exist, such as inside loading iframes
	// https://bugs.jquery.com/ticket/4833
	return !rhtml.test( namespace || docElem && docElem.nodeName || "HTML" );
};

This throws an error when the elem is undefined:
Uncaught TypeError: Cannot read property 'namespaceURI' of undefined

Please, if you can, bring back the previous functionality: undefined is not an XML node, so it should return a false boolean instead of a TypeError.

Thanks in advance!

Additional info

Related commit: d940bc0 (from line 562)
Related blog-post: https://blog.jquery.com/2019/04/10/jquery-3-4-0-released/

Activity

  1. dmethvin commented on Aug 29, 2020

    @dmethvin
    Member

    Can you post a test case showing when this happens? Also can you use the latest jQuery 3 in your test case?

    Be aware that version 2.x is no longer supported. If there is a bug to fix we'd only be fixing it in jQuery 3.x, or 4.x if it requires a breaking change.

  2. borzaka commented on Aug 30, 2020

    @borzaka
    Author

    I'am afraid if I present my use case, the attention will move away from my finding. Which is:
    jQuery 3.3.1 returns a different outcome for the same function and argument then 3.5.1

    jQuery 3.3.1 returns: false
    jQuery 3.5.1 returns: Uncaught TypeError: Cannot read property 'namespaceURI' of undefined
    For this line of code: jQuery.isXMLDoc(undefined)

    jQuery 3.5.1 doesn't check the argument, 3.3.1 does. undefined is not an XML node, so in my interpretation it should return false, as version 3.3.1 does.

    I'am talking about jQuery 3.x, not 2.x, only 2.x I mentioned is Sizzle update from 2.3.3 to 2.3.4, where this isXML() function is changed and causing this new TypeError since 3.4.0. Linked under Additional info in my original post.

  3. mgol commented on Aug 30, 2020

    @mgol
    Member

    As indicated in the signature of that method on its API page, only arguments of Element type are allowed as input to that method. Behavior for all other inputs is undefined as jQuery generally doesn't validate whether input is of a correct type.

    If you want to make sure you only use allowed types, using TypeScript with jQuery typings is a good idea.

    I'll let it open for discussion for now but generally we've been leaning towards not handling undefined inputs in APIs where it's not expected.

  4. added
    Discuss in MeetingReserved for Issues and PRs that anyone would like to discuss in the weekly meeting.
    on Aug 30, 2020
  5. timmywil commented on Aug 31, 2020

    @timmywil
    Member

    We're deliberating whether we want to add back the falsey check.

  6. timmywil commented on Sep 14, 2020

    @timmywil
    Member

    I think we're willing to add back the falsey check, but we'd all feel better if there was a valid use case presented.

    the attention will move away from my finding

    Just the opposite btw. It will grant some validity to your finding.

  7. added and removed
    Discuss in MeetingReserved for Issues and PRs that anyone would like to discuss in the weekly meeting.
    on Sep 21, 2020
  8. borzaka commented on Nov 6, 2020

    @borzaka
    Author

    Sorry for the late reply. Here is a part of that code working with ajax responses.

    mainComplete = function(xhr, status) {
        var result
        var resultText
    
        if ($.isXMLDoc(xhr.responseXML) && xhr.responseXML.documentElement.nodeName === 'result') {
            result = processCoreResponse(xhr.responseXML)
        }
    
        resultText = result ? (result.content[''] || '') : xhr.responseText
    
        ...
    }
    

    The $.isXMLDoc(xhr.responseXML) is failing because of the lack of the falsey check as before.

    Could you reopen this issue please?

  9. mgol commented on Nov 6, 2020

    @mgol
    Member

    Thanks for the use case, reopening.

  10. reopened this on Nov 6, 2020
  11. added
    Discuss in MeetingReserved for Issues and PRs that anyone would like to discuss in the weekly meeting.
    and removed on Nov 6, 2020
  12. 22 remaining items

  13. added a commit that references this issue on Feb 17, 2021
    e15238e
  14. added a commit that references this issue on Feb 17, 2021
    1654874
  15. mgol commented on Feb 18, 2021

    @mgol
    Member

    Fixed on 3.x-stable (jQuery 3.x) by #4846.
    Previously, it was only fixed on main (jQuery 4.0) by #4814.

    Closing.

  16. locked as resolved and limited conversation to collaborators on Sep 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions