Bug Tracker

Modify

Ticket #10075 (closed bug: invalid)

Opened 21 months ago

Last modified 20 months ago

$(" ... 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:
Blocking: Blocked by:

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

comment:1 Changed 21 months ago by dmethvin

  • Keywords needsdocs added
  • Status changed from new to closed
  • Resolution set to invalid

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 20 months ago by addyosmani

  • Keywords neededdocs added; needsdocs removed

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

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.