Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6465 (new enhancement)

Opened 3 years ago

Last modified 1 year ago

[PATCH] crossbrowser innerText implementation

Reported by: Tobie Assigned to: sam
Priority: low Milestone: 2.x
Component: Prototype Version:
Severity: trivial Keywords: innerText textContent
Cc:

Description

A cross-browser way to get the textual content of an element.

Deals with Opera and Safari inconsistencies (Opera doesn't strip extra whitespace, line breaks or content of the <script> tag, Safari returns an empty string on hidden elements).

Provides a workaround for FF which is missing that method.

Attachments

dom.diff (0.6 kB) - added by Tobie on 10/21/06 19:59:47.
the js
dom.html.diff (1.5 kB) - added by Tobie on 10/21/06 20:00:33.
the tests
crossbrowser_innertext.diff (2.4 kB) - added by Caged on 01/08/07 06:45:12.
Preserves innerText as a property in Firefox

Change History

10/21/06 19:59:47 changed by Tobie

  • attachment dom.diff added.

the js

10/21/06 20:00:33 changed by Tobie

  • attachment dom.html.diff added.

the tests

(follow-up: ↓ 2 ) 10/22/06 15:14:27 changed by mislav

What about Firefox textContent property?

Nice addition (certainly useful) but somehow I don't feel the name is right. Why not call it getContent() or getTextContent()?

(in reply to: ↑ 1 ; follow-up: ↓ 3 ) 10/22/06 17:39:13 changed by Tobie

Replying to mislav:

What about Firefox textContent property?

Firefox's textContentunfortunately does not strip the content of <script> tags, e.g.:

<div id="myDiv">Some text <script>var test = 'hello world';</script></div>

$('myDiv'). textContent // returns: "Some text var test = \'hello world\';"

Nice addition (certainly useful) but somehow I don't feel the name is right. Why not call it getContent() or getTextContent()?

I agree, the name is too long AND doesn't really feel right (too much of an IE flavor, right? ;-) ) . I would have decided on getText() if it wasn't such a reference to i18n.

What about something like textOnly() or onlyText() or strippedText() ?

I'm really not sure what's better...

(in reply to: ↑ 2 ; follow-up: ↓ 4 ) 10/22/06 18:05:27 changed by mislav

Replying to Tobie:

What about something like textOnly() or onlyText() or strippedText() ?

Argh! Awful. Especially onlyText :-/

We are talking about content of the element. Inline scripts are not content and HTML tags are not content (that's what innerHTML is for). So my choice would be getContent

(in reply to: ↑ 3 ) 10/22/06 18:17:47 changed by Tobie

Replying to mislav:

Argh! Awful. Especially onlyText :-/

lol

So my choice would be getContent

Do you think it is descriptive enough?

12/12/06 09:40:08 changed by Tobie

Could we try to decide on a name for this method? For the moment, we have: getInnerText(), getText(), getContent(), getTextContent(), which one do you prefer?

12/12/06 10:04:03 changed by Tobie

  • keywords set to innerText textContent.
  • summary changed from [PATCH] `getInnerText()` method for `Element` to [PATCH] crossbrowser innerText implementation.

01/08/07 06:45:12 changed by Caged

  • attachment crossbrowser_innertext.diff added.

Preserves innerText as a property in Firefox

(follow-up: ↓ 8 ) 01/08/07 06:48:02 changed by Caged

I run into this issue today, and saw this ticket. I've attached a patch that preserves innerText as a property, and thus there doesn't need to be a method proxy.

This doesn't fix the opera issue though, as it will still include contents of a script.

(in reply to: ↑ 7 ) 02/20/07 20:56:23 changed by mjb

Not yet addressed in the patches, if the idea is to mimic IE: 1. In IE, innerText apparently replaces <br> with \r\n. 2. innerText is read/write, not read-only.

03/03/08 23:57:47 changed by stephenr

So um, is this (or something along these lines) going to make it into prototype any time soon?