Skip to main content

Bug Tracker

Side navigation

#2690 closed bug (duplicate)

Opened April 15, 2008 10:40AM UTC

Closed December 01, 2009 09:56PM UTC

Last modified March 15, 2012 09:10AM UTC

$.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
Blocked by: Blocking:
Description
$.trim(option.text()) // - doesn't work

i use replace function instead

option.text().replace(/(^[\\s\\xA0]+|[\\s\\xA0]+$)/g, '')
Attachments (1)
  • UnitTestTrim.htm (2.4 KB) - added by doug July 03, 2008 04:59PM UTC.

    Test case

Change History (11)

Changed April 16, 2008 04:36AM UTC by davidserduke comment:1

description: $.trim(option.text()) // - doesn't work \ i use replace function instead \ option.text().replace(/(^[\\s\\xA0]+|[\\s\\xA0]+$)/g, ''){{{ \ $.trim(option.text()) // - doesn't work \ }}} \ i use replace function instead \ {{{ \ option.text().replace(/(^[\\s\\xA0]+|[\\s\\xA0]+$)/g, '') \ }}}

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

Changed May 16, 2008 03:23AM UTC by flesler comment:2

need: ReviewTest Case

As david said, could you provide a test case ?

Changed July 03, 2008 12:35PM UTC by doug comment:3

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

Changed July 03, 2008 12:41PM UTC by doug comment:4

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,"");
};

Changed July 03, 2008 03:05PM UTC by flesler comment:5

milestone: 1.2.41.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.

Changed July 03, 2008 04:50PM UTC by doug comment:6

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]"

Changed July 03, 2008 04:58PM UTC by doug comment:7

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 November 04, 2008 03:14AM UTC by flesler comment:8

cc: → dimik, flesler.doug

Indeed, \\xa0 isn't trimmed, as well as a pseudo linebreak.

Now, the question is, in what situation are you generating such characters ?

Changed July 25, 2009 08:03PM UTC by aakoch comment:9

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

Changed October 14, 2009 12:53AM UTC by jmdixon comment:10

Another test case for IE7/8:

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

Changed December 01, 2009 09:56PM UTC by john comment:11

milestone: 1.31.4
resolution: → duplicate
status: newclosed
version: 1.2.31.3.2

Duplicate of #4980.