Skip to content

Getting children of <object> tag with contents() not working correctly #4384

@patocallaghan

Description

@patocallaghan

Description

Version: jQuery 3.4.1

Since 0ba8e38 it appears that using contents() to get the children of an <object> is not working as expected.

Taking this example HTML

<object type="application/x-shockwave-flash" width="200" height="300" id="penguin">
    <param name="movie" value="flash/penguin.swf">
    <param name="quality" value="high">
    <img src="images/penguin.jpg" width="200" height="300" alt="Penguin">
</object>

I'd expect the following to happen

jQuery(objectHtml).contents().length // => 3

But what actually happens is:

jQuery(objectHtml).contents().length // => 0

This happens because on this line

jquery/src/traversing.js

Lines 148 to 149 in 110802c

if ( typeof elem.contentDocument !== "undefined" ) {
return elem.contentDocument;
it only checks if elem.contentDocument is not undefined. But in this scenario elem.contentDocument is null so the conditional is truthy and enters the if block and returns null for the contents.

If we change the conditional to be elem.contentDocument != null it works correctly.

Link to test case

https://codepen.io/patocallaghan/pen/bybaWv?editors=0011

I have a PR to fix it but I wanted to open an issue first.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions