Side navigation
#3144 closed bug (fixed)
Opened July 08, 2008 02:55PM UTC
Closed September 28, 2011 08:47PM UTC
Last modified March 08, 2012 03:27PM UTC
Inconsistent cross-browser results from .text() method
Reported by: | dmethvin | Owned by: | dmethvin |
---|---|---|---|
Priority: | low | Milestone: | 1.7 |
Component: | selector | Version: | 1.4.4 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
The .text() method returns different whitespace on different browsers. In the case of IE6/7, it sometimes returns no whitespace at all between adjacent text nodes, even though the corresponding .innerText result has white space.
This behavior has been in the .text() method for quite a while, so I don't think a lot of people have found it a critical problem. I would say that IE's lack of whitespace between text nodes is a bug.
The other whitespace differences might deserve some thought as well; for example, should IE's CR-LF sequences be changed to LF for consistency with the other browsers? Regardless, there will still be whitespace differences across browsers that jQuery can't control. For example, IE sometimes removes the trailing newline from text.
http://groups.google.com/group/jquery-en/browse_frm/thread/6f03f58ac517aea9/5865c907af2d0324
http://www.nabble.com/.text()-method-quirks-p6933512s27240.html
Sorta-related: Bug #2425
Attachments (0)
Change History (13)
Changed October 31, 2009 12:24AM UTC by comment:1
Changed February 14, 2010 09:30AM UTC by comment:2
I have fixed this bug. Here is the patch:
jQuery.getText = function ( elems ) { var ret = "", elem; for ( var i = 0; elems[i]; i++ ) { elem = elems[i]; // Get the text from text nodes and CDATA nodes if ( elem.nodeType === 3 || elem.nodeType === 4 ) { ret += elem.nodeValue; // Use textContent || innerText for elements } else if ( elem.nodeType === 1 ) { if ( typeof(elem.textContent) === 'string' ) ret += elem.textContent; else if ( typeof(elem.innerText) === 'string' ) ret += elem.innerText; else ret += getText( elem.childNodes ); // Traverse everything else, except comment nodes } else if ( elem.nodeType !== 8 ) { ret += getText( elem.childNodes ); } } return ret; }
Changed June 18, 2010 02:32AM UTC by comment:3
milestone: | 1.3 → 1.4.3 |
---|
Changed November 01, 2010 08:53PM UTC by comment:4
owner: | → dmethvin |
---|---|
status: | new → pending |
Dave is this issue still outstanding?
Changed November 14, 2010 03:12PM UTC by comment:5
status: | pending → new |
---|
#4808 is a duplicate of this ticket.
Changed November 15, 2010 12:33AM UTC by comment:6
milestone: | 1.4.3 → 1.4.5 |
---|---|
priority: | minor → low |
status: | new → pending |
I think the dupe triggered a reopen on this one. Would still love someone to verify that this is an issue in the latest jQuery.
Changed November 17, 2010 08:11PM UTC by comment:7
_comment0: | → 1290024725378930 |
---|---|
keywords: | → needsreview |
status: | pending → open |
version: | 1.2.6 → 1.4.4 |
Changed December 24, 2010 02:25AM UTC by comment:8
I played with the test case a bit more, and included the getText from ioquatix:
http://jsfiddle.net/dmethvin/tptT3/
It does look like the new getText provides better results for IE.
Changed July 11, 2011 06:05PM UTC by comment:9
Confirmed in bug triage, but low priority.
Changed September 28, 2011 08:47PM UTC by comment:10
Changed September 28, 2011 09:01PM UTC by comment:11
component: | core → selector |
---|---|
keywords: | needsreview |
milestone: | 1.next → 1.7 |
Changed October 17, 2011 10:24PM UTC by comment:12
Looking into latest version: http://code.jquery.com/jquery-git.js
What if function argument is null or undefined ?
Changed October 17, 2011 10:27PM UTC by comment:13
I suppose "safe and slower" approach does not work?
Perhaps: http://jsbin.com/uqadup/43/
Closed duplicate #5429 with another example.