We've been getting these requests in the past. Take a look at, for
example: <URL:
http://groups.google.com/group/prototype-core/browse_thread/thread/8ef26e7cedb43afc/47033b4bc8dc4c74#47033b4bc8dc4c74>

I still think that it's not a trivial solution (for the reasons
outlined in the post linked above) and so is best handled by a
standalone plugin. And using context-unaware `stripTags` on something
like HTML is usually asking for trouble :) (imagine what stripTags
would do to a string like this — "foo bar <script>function wrap(html)
{ return 'div' + html + '</div>'}</script> baz"; and then there are
other elements with CDATA content model, like STYLE)

--
kangax

On Apr 13, 8:20 am, "T.J. Crowder" <t...@crowdersoftware.com> wrote:
> On Apr 13, 10:39 am, Eric <lefauv...@gmail.com> wrote:
>
> > wouldn't it be wiser to check for the native method once and use it?
>
> Probably. I'd also check for innerText (in fact, I'd check for that
> first), since it's supported by IE, WebKit (so Chrome, Safari), and
> Opera; only Mozilla holds out. textContent is supported by all of them
> except IE. So:
>
> Element.addMethods((function() {
>
>     return {
>         /**
>          * Element.text() -> String
>          *
>          * Gets the text within the element, ignoring any tags
> (essentially the sum of all of the
>          * text nodes within).
>         **/
>         text: (function() {
>             var element, testvalue;
>
>             element = document.createElement("span");
>             element.innerHTML = testvalue = "foo";
>             if (text_fromInnerText(element) == testvalue) {
>                 return text_fromInnerText;
>             }
>             if (text_fromTextContent(element) == testvalue) {
>                 return text_fromTextContent;
>             }
>             return text_fromStripping;
>         })()
>     };
>
>     // Get the element's inner text via innerText if available (IE,
> WebKit, Opera, ...)
>     function text_fromInnerText(element) {
>         if (!(element = $(element))) return;
>         return element.innerText;
>     }
>
>     // Get the element's inner text via textContent if available
> (Gecko, WebKit, Opera, ...)
>     function text_fromTextContent(element) {
>         if (!(element = $(element))) return;
>         return element.textContent;
>     }
>
>     // Get the element's inner text by getting innerHTML and stripping
> tags (fallback)
>     function text_fromStripping(element) {
>         if (!(element = $(element))) return;
>         return element.innerHTML.stripTags();
>     }
>
> })());
>
> Do people think I should submit this to core? jQuery has an equivalent
> function, and I think I saw one in Closure as well. So it's not just
> the OP who wants to do this...
>
> -- T.J. :-)
>
> On Apr 13, 10:39 am, Eric <lefauv...@gmail.com> wrote:
>
> > Oooops, gmail sent the message before I finished... :o)
>
> > Here is the correct message (please ignore the previous one)
>
> > On Apr 12, 7:04 pm, "T.J. Crowder" <t...@crowdersoftware.com> wrote:
>
> > > Element.addMethods({
> > >     text: function(element) {
> > >         if (!(element = $(element))) return;
> > >         return element.innerHTML.stripTags();
> > >     }
> > > });
>
> > wouldn't it be wiser to check for the native method once and use it?
>
> > Something like (untested)
>
> > Element.addMethods({
> >     text: ($$('BODY').first().textContent===undefined)
> >             ? function(element) { if (!(element = $(element))) return;
> > return element.innerText; }
> >             : function(element) { if (!(element = $(element))) return;
> > return element.textContent; }
>
> > });
>
> > Eric
>
> > NB: I know, the testing condition is ugly... feel free to post a
> > better one :o)

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Reply via email to