Bug Tracker

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#10075 closed bug (invalid)

$(" ... html ...") strips leading and trailing text nodes in HTML string

Reported by: phil127+jq@… Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.6.2
Keywords: neededdocs Cc:
Blocked by: Blocking:

Description

Hi,

I stumbled about this when trying to use $() with an HTML string that contains multiple top-level nodes and starts with a text node:

  • $("1<br/>2<br/>3")
    • Expected result: ["1", <br>, "2", <br>, "3"]
    • Observed result: [<br>, "2", <br>]

Additional observations:

The nodes are created as expected if the HTML string starts with "<" and ends with ">", even if the characters are converted to text nodes.

  • $("1<br/>2<br/>3 >") returns (incorrectly):
    • [<br>, "2", <br>, "3 &gt;"]
  • $("< 1<br/>2<br/>3") returns (incorrectly):
    • ["&lt; 1", <br>, "2", <br>]
  • $("< 1<br/>2<br/>3 >") returns (correctly):
    • ["&lt; 1", <br>, "2", <br>, "3 &gt;"]
  • $("1<br/>2<br/>3<!-- -->") returns (incorrectly):
    • [<br>, "2", <br>, "3", (comment)]
  • $("<!-- -->1<br/>2<br/>3") returns (incorrectly):
    • [(comment), "1", <br>, "2", <br>]
  • $("<!-- -->1<br/>2<br/>3<!-- -->") returns (correctly):
    • [(comment), "1", <br>, "2", <br>, "3", (comment)]

(The last is what I'm using as a workaround right now.)

Tested on:

  • Firefox 6.0
  • Google Chrome 13.0.782.112 m

OS:

  • Windows 7 x64 SP1

Regards, Philipp Serafin

Change History (2)

comment:1 Changed 7 years ago by dmethvin

Keywords: needsdocs added
Resolution: invalid
Status: newclosed

jQuery isn't intended to process text nodes as if they were DOM elements. Most methods save a few like .content() remove or ignore them if they are in a jQuery collection.

I've marked the ticket for a documentation review, but the behavior is correct.

comment:2 Changed 7 years ago by addyosmani

Keywords: neededdocs added; needsdocs removed

I've updated the docs for this here: http://api.jquery.com/jQuery/

Note: See TracTickets for help on using tickets.