(An earlier copy of this is stuck in the moderator queue and can be deleted;
there's more detail in this version.)

I've noticed several minor differences with the .text() method across
browsers. For example, given this (note the space around two):

<div id="test">
  <div >one</div>
  <div> two </div>
</div>

function check(m,s) {
  if ( s == null ) return;
  var r = "'";
  for ( var i=0; i < s.length; i++ ) {
    var c = s.charCodeAt(i);
    r += c > 0x20? s.charAt(i) : "{"+c+"}";
  }
  alert(m+": "+r+"' "+s.length);
}               
var t = $("#test");
check(".text()",t.text());
check(".innerText", t[0].innerText);
check(".textContent", t[0].textContent);

Here are the results of .text versus the native text methods on the DOM
element:

IE6/IE7:
.text()         'onetwo{32}' 7
.innerText      'one{13}{10}two{32}' 9

Firefox 1.5:
.text()         '{10}{32}{32}one{10}{32}{32}{32}two{32}{10}' 15
.textContent    '{10}{32}{32}one{10}{32}{32}{32}two{32}{10}' 15

Opera 9.02
.text()         '{10}{32}{32}one{10}{32}{32}{32}two{32}{10}' 15
.innerText      '{10}{32}{32}one{10}{32}{32}{32}two{32}{10}' 15
.textContent    '{10}{32}{32}one{10}{32}{32}{32}two{32}{10}' 15

Are most people using .text() in situations where these differences don't
matter? The current IE behavior with .text() (no space between elements) can
be annoying. In some situations I have seen Opera act more like IE and leave
no space between text. I don't have a Mac to check Safari, does it use {13}
instead of {10} perhaps?

A quick test showed it's about 60% faster in IE and 25% faster in
Firefox/Opera to use the builtin .innerText/.textContent properties for HTML
documents; I guess the tree crawl needs to stay for XML? I think it would be
worthwhile to trim leading/trailing whitespace, but am inclined to leave the
imbedded spaces alone. Does anyone depend on the current behavior?



_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to