Skip to main content

Bug Tracker

Side navigation

#12523 closed bug (fixed)

Opened September 12, 2012 05:03PM UTC

Closed September 19, 2012 03:32PM UTC

JQuery renders line breaks as text nodes

Reported by: pbcomm Owned by: gibson042
Priority: blocker Milestone: 1.8.2
Component: selector Version: 1.8.1
Keywords: Cc:
Blocked by: Blocking:
Description

Line breaks are rendered as text nodes which also breaks closest selector:

$('<p id="test">test</p>' + "\\n" + '<p id="text">text</p>').length === 3 // should be 2

$('<p id="test">test</p>' + "\\n" + '<p id="text">text</p>').closest('#text') result in:

Uncaught TypeError: Object #<Text> has no method 'getAttribute' jquery-1.8.1.js:4206

Attachments (0)
Change History (9)

Changed September 12, 2012 05:54PM UTC by dmethvin comment:1

owner: → pbcomm
status: newpending

We're not creating those text nodes, the browser that is converting the text to HTML does that. Since we've been passing through text nodes created like that since jQuery 1.0 I don't think we'd want to change it.

$('<p id="test">test</p>' + "\\n" + '<p id="text">text</p>').closest('#text')

That doesn't make sense to do, so if it results in an error I am okay with it. Element selection won't return text nodes, only creating HTML from text. If you need to ensure that the top-level result of an HTML conversion only contains element nodes, you can use $(html).filter("*"). That is essentially what I think you're advocating anyway, so you'll only need to pay the performance price when you really need it. http://jsfiddle.net/dmethvin/VwFUk/

What is the situation where you're encountering this?

Changed September 12, 2012 06:12PM UTC by anonymous comment:2

The closest() selector does work in 1.7.2 and breaks in 1.8.1 because it selects the text node instead of the element with the id of the selection.

Changed September 12, 2012 06:18PM UTC by dmethvin comment:3

What I'm wondering is if there is reasonable code that might do this. That's why I asked about the situation where you're using this. Are you really creating HTML via $() and then using .closest() in your real code?

Changed September 12, 2012 06:22PM UTC by pbcomm comment:4

status: pendingnew

I am. I'm using jsRender to render the template into a jQuery object and then looking up the closest element to do further modifications before injecting all of it into the dom. jsRender templates come back formatted which creates multiple text nodes in between elements because of line breaks.

Changed September 14, 2012 12:42AM UTC by dmethvin comment:5

owner: pbcommdmethvin
status: newassigned

I'll take a look. It's strange that nodeType 11 is compared when we wouldn't expect anything but 1.

Changed September 14, 2012 01:00AM UTC by dmethvin comment:6

owner: dmethvingibson042

Changed September 14, 2012 01:31AM UTC by gibson042 comment:7

component: unfiledselector
milestone: None1.8.2
priority: undecidedblocker

Changed September 14, 2012 05:50AM UTC by gibson042 comment:8

Changed September 19, 2012 03:32PM UTC by Timmy Willison comment:9

resolution: → fixed
status: assignedclosed

Update Sizzle: Fixes #12303, #12474, #12523, #12526, #12541.

Changeset: f7980d4d68e76147e6a9ddd7b30d379ddce7f222