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