Bug Tracker

Ticket #2690 (closed bug: duplicate)

Opened 7 years ago

Last modified 3 years ago

$.trim() doesn't work in IE7

Reported by: dimik Owned by:
Priority: major Milestone: 1.4
Component: core Version: 1.3.2
Keywords: Cc: dimik, flesler.doug
Blocking: Blocked by:

Description (last modified by davidserduke) (diff)

$.trim(option.text()) // - doesn't work

i use replace function instead

option.text().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '')

Attachments

UnitTestTrim.htm Download (2.4 KB) - added by doug 6 years ago.
Test case

Change History

comment:1 Changed 7 years ago by davidserduke

  • Description modified (diff)

What was the value of option.text() ? I haven't had a problem with trim().

comment:2 Changed 6 years ago by flesler

  • need changed from Review to Test Case

As david said, could you provide a test case ?

comment:3 Changed 6 years ago by doug

Unfortunately, IE not only left out the non-breaking space from \s, but all the other Unicode space separators.

Solution

$.CharacterClass = {}; IE (as of IE 7) omits non-breaking space and other Unicode space separators in "\s". $.CharacterClass.s = "[\t\x0b\f \xa0\u1680\u180e\u2000-\u200a\u202f\u205f\3000\n\r\u2028\u2029]"; /*

ECMA-262 3rd Edition - December 1999

15.10.2.12 CharacterClassEscape

The production CharacterClassEscape
s evaluates by returning the set of characters containing the characters that are on the right-hand side of the WhiteSpace (7.2) or LineTerminator (7.3) productions.

7.2 White Space \u0009 Tab <TAB> \u000B Vertical Tab <VT> \u000C Form Feed <FF> \u0020 Space <SP> \u00A0 No-break space <NBSP> Other category “Zs” Any other Unicode “space separator”

7.3 Line Terminator \u000A Line Feed <LF> \u000D Carriage Return <CR> \u2028 Line separator <LS> \u2029 Paragraph separator <PS>

Unicode Regular Expressions  http://unicode.org/reports/tr18/ Unicode Regular Expression Guidelines  http://unicode.org/reports/tr18/tr18-6d2.html UNICODE CHARACTER DATABASE  http://www.unicode.org/Public/UNIDATA/UCD.html Revision 5.1.0 Date 2008-03-25  http://www.unicode.org/Public/UNIDATA/PropList.txt # PropList-5.1.0.txt # Date: 2008-03-20, 17:55:27 GMT [MD] 0020 ; White_Space # Zs SPACE 00A0 ; White_Space # Zs NO-BREAK SPACE 1680 ; White_Space # Zs OGHAM SPACE MARK 180E ; White_Space # Zs MONGOLIAN VOWEL SEPARATOR 2000..200A ; White_Space # Zs [11] EN QUAD..HAIR SPACE 202F ; White_Space # Zs NARROW NO-BREAK SPACE 205F ; White_Space # Zs MEDIUM MATHEMATICAL SPACE 3000 ; White_Space # Zs IDEOGRAPHIC SPACE

Reference:  http://en.wikipedia.org/wiki/Space_(punctuation)#Table_of_spaces

*/

$.RegExp = {}; $.RegExp.ltrim = new RegExp("" + $.CharacterClass.s + "+"); $.RegExp.rtrim = new RegExp($.CharacterClass.s + "+$"); $.trim = function( text ) {

return (text+"").replace($.RegExp.ltrim,"").replace($.RegExp.rtrim,"");

};

The test case is to trim a string with all the white space separators in it using IE 7.

IMPORTANT NOTE: the '(text+"")' is to convert the argument to a string. The jQuery 1.2.6 method of '(text
"")' throws an exception with the argument is passed but not a string, for example, an integer.

For such a simple, apparently trivial function, trim() sure does evoke a lot of discussion. Cite:  http://groups.google.com/group/jquery-dev/browse_thread/thread/cb76879a29a7b5d9?hl=en and my earlier comment at  http://groups.google.com/group/jquery-dev/browse_thread/thread/f3799d916dfdd872/c4cc1c9e5c0a237d

comment:4 Changed 6 years ago by doug

Sorry, got bit by WikiFormatting. Here's the code again.

$.RegExp = {};
$.RegExp.CharacterClass = {};
		
// IE (as of IE 7) omits non-breaking space and other Unicode space separators in "\s".
$.CharacterClass.s = "[\t\x0b\f \xa0\u1680\u180e\u2000-\u200a\u202f\u205f\3000\n\r\u2028\u2029]";
/*    
	ECMA-262 3rd Edition - December 1999

	15.10.2.12 CharacterClassEscape
	The production CharacterClassEscape :: s evaluates by returning the set of characters containing the
	characters that are on the right-hand side of the WhiteSpace (7.2) or LineTerminator (7.3)
	productions.

	7.2 White Space
	\u0009 Tab <TAB>
	\u000B Vertical Tab <VT>
	\u000C Form Feed <FF>
	\u0020 Space <SP>
	\u00A0 No-break space <NBSP>
	Other category “Zs” Any other Unicode
	“space separator”

	7.3 Line Terminator
	\u000A Line Feed <LF>
	\u000D Carriage Return <CR>
	\u2028 Line separator <LS>
	\u2029 Paragraph separator <PS>

	Unicode Regular Expressions http://unicode.org/reports/tr18/
	Unicode Regular Expression Guidelines http://unicode.org/reports/tr18/tr18-6d2.html
	UNICODE CHARACTER DATABASE http://www.unicode.org/Public/UNIDATA/UCD.html
	Revision 5.1.0 
	Date 2008-03-25 
	http://www.unicode.org/Public/UNIDATA/PropList.txt
	# PropList-5.1.0.txt
	# Date: 2008-03-20, 17:55:27 GMT [MD]
	0020          ; White_Space # Zs       SPACE
	00A0          ; White_Space # Zs       NO-BREAK SPACE
	1680          ; White_Space # Zs       OGHAM SPACE MARK
	180E          ; White_Space # Zs       MONGOLIAN VOWEL SEPARATOR
	2000..200A    ; White_Space # Zs  [11] EN QUAD..HAIR SPACE
	202F          ; White_Space # Zs       NARROW NO-BREAK SPACE
	205F          ; White_Space # Zs       MEDIUM MATHEMATICAL SPACE
	3000          ; White_Space # Zs       IDEOGRAPHIC SPACE

	Reference: http://en.wikipedia.org/wiki/Space_(punctuation)#Table_of_spaces
*/
$.RegExp.ltrim = new RegExp("^" + $.RegExp.CharacterClass.s + "+");
$.RegExp.rtrim = new RegExp($.RegExp.CharacterClass.s + "+$");

$.trim = function( text )
{
    return (text+"").replace($.RegExp.ltrim,"").replace($.RegExp.rtrim,"");
};

comment:5 Changed 6 years ago by flesler

  • Milestone changed from 1.2.4 to 1.3

trim() worked fine for me on IE7. It trimmed every unicode char I added Can you please provide a real test case to reproduce the error ? An html file with the minimum html/js necessary.

IMPORTANT NOTE: the '(text+"")' is to convert the argument to a string. The jQuery 1.2.6 method >of '(text "")' throws an exception with the argument is passed but not a string, for example, an integer.

trim() only works on strings, that is documented in the docs.

comment:6 Changed 6 years ago by doug

Found a typo. As is, it trims "0" characters.

"u" is needed in \3000. That is \u3000.

Fix: "[\t\x0b\f \xa0\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\n\r\u2028\u2029]"

comment:7 Changed 6 years ago by doug

The test page is attached. This is the test string:

var strHelloWorld = " \t\n  \xa0hello world \t\n  \xa0";

Tested on IE 7.0.5730.11 update versions: 0; 3283; on Windows XP SP 3

Changed 6 years ago by doug

Test case

comment:8 Changed 6 years ago by flesler

  • Cc dimik, flesler.doug added

Indeed, \xa0 isn't trimmed, as well as a pseudo linebreak. Now, the question is, in what situation are you generating such characters ?

comment:9 Changed 5 years ago by aakoch

I got such a character returned when calling

$("iframe").contents().find("#id").text()

Here's my post about it:  http://www.adamkoch.com/2009/07/25/white-space-and-character-160/ And my test page:  http://www.adamkoch.com/char160test.html

comment:10 Changed 5 years ago by jmdixon

Another test case for IE7/8:

   $.trim($('<span>&nbsp;test</span>').text()) // returns " test"

comment:11 Changed 5 years ago by john

  • Status changed from new to closed
  • Version changed from 1.2.3 to 1.3.2
  • Resolution set to duplicate
  • Milestone changed from 1.3 to 1.4

Duplicate of #4980.

Note: See TracTickets for help on using tickets.