Bug Tracker

Ticket #3144 (closed bug: fixed)

Opened 6 years ago

Last modified 2 years ago

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:
Blocking: Blocked by:

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://pastie.org/228916

 http://www.nabble.com/.text()-method-quirks-p6933512s27240.html

Sorta-related: Bug #2425

Change History

comment:1 Changed 5 years ago by dmethvin

Closed duplicate #5429 with another example.

comment:2 Changed 4 years ago by ioquatix

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;
}

comment:3 Changed 4 years ago by dmethvin

  • Milestone changed from 1.3 to 1.4.3

comment:4 Changed 4 years ago by rwaldron

  • Owner set to dmethvin
  • Status changed from new to pending

Dave is this issue still outstanding?

comment:5 Changed 4 years ago by dmethvin

  • Status changed from pending to new

#4808 is a duplicate of this ticket.

comment:6 Changed 4 years ago by SlexAxton

  • Priority changed from minor to low
  • Status changed from new to pending
  • Milestone changed from 1.4.3 to 1.4.5

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.

comment:7 Changed 4 years ago by jitter

  • Keywords needsreview added
  • Status changed from pending to open
  • Version changed from 1.2.6 to 1.4.4
Last edited 4 years ago by jitter (previous) (diff)

comment:8 Changed 4 years ago by dmethvin

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.

comment:9 Changed 3 years ago by dmethvin

Confirmed in bug triage, but low priority.

comment:10 Changed 3 years ago by timmywil

  • Status changed from open to closed
  • Resolution set to fixed

Update Sizzle. Fixes #3144, #6863.

Changeset: 22fcc7744daded0dc0783d85df3bd88c6dbc4544

comment:11 Changed 3 years ago by timmywil

  • Keywords needsreview removed
  • Component changed from core to selector
  • Milestone changed from 1.next to 1.7

comment:12 Changed 3 years ago by dbjdbj

Looking into latest version:  http://code.jquery.com/jquery-git.js

What if function argument is null or undefined ?

comment:13 Changed 3 years ago by anonymous

I suppose "safe and slower" approach does not work? Perhaps:  http://jsbin.com/uqadup/43/

Note: See TracTickets for help on using tickets.