Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #23217

Re: Get inner text of HMTL element

From Cezary Tomczyk <cezary.tomczyk@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Get inner text of HMTL element
Date 2014-02-23 14:50 +0100
Organization A noiseless patient Spider
Message-ID <lecuah$mib$9@dont-email.me> (permalink)
References (3 earlier) <1633562.pVBMJriu5e@PointedEars.de> <leb1q1$r9i$1@dont-email.me> <1780761.mlrEmVmIzg@PointedEars.de> <leb8sc$23n$5@dont-email.me> <10325533.pBRRTmW78i@PointedEars.de>

Show all headers | View raw


Thomas 'PointedEars' Lahn wrote:
> Cezary Tomczyk wrote:
>
>> [...]
>> After longer research, experimenting and reading
>> (http://www.kellegous.com/j/2013/02/27/innertext-vs-textcontent/) I
>> think I can build something like this:
>>
>> var get_inner_text;
>>
>> if (typeof document.getElementsByTagName('html')[0].textContent ===
>> 'string') {
>
> You should safely replace that with
>
>    if (typeof document.documentElement != "undefined"
>        && typeof document.documentElement.textContent == "string")
>    {

or

if (document.documentElement !== undefined && typeof 
document.documentElement.textContent === 'string') {

>>       get_inner_text = function(node) {
>>           return node.textContent;
>>       };
>> } else if (document.createTreeWalker) {
>
> Insufficient and error-prone feature test for a host object's method, see
> jsx.object.isHostMethod().

Yes, I know isHostMethod, but still I am feeling that it's not enough. 
Checking if host object is a function is one thing, but the second one 
if it's native or not. Once I had a scenario where trim() method was 
exists, but it was overwritten by some 3rd party library and this method 
wasn't working well. I've just realized it after some investigation :-(

However, last time I was thinking if it's strongly required every time 
for every scenario. As I remember:

1. Some host objects in IE8 are as an "object", not a "function".
2. There is type "unknown", but it's related to only one case.

That's why I've been thinking that explicit conversion in that case is 
relatively safe.

>>       get_inner_text = function(root) {
>>           var w = document.createTreeWalker(root, 4, null, false),
>>               result = '';
>>            while (w.nextNode()) {
>>                result += w.currentNode.data;
>
> See below.
>
>>            }
>>            return result;
>>       };
>>
>> } else {
>
> That the former two approaches fail does not mean that the third one must
> work.

Hm, should I test if "firstChild" and "nextSibling" is supported before? 
Or do something else?

>>       get_inner_text = function(node) {
>>           var txt = '';
>>
>>           node = node.firstChild;
>>           while (node) {
>>               if (node.nodeType === 3 || node.nodeType === 4) {
>
> I avoid strict comparison against property values of host objects.  There
> are too many unknowns.

I've never had problems with that, but I am not claim that there is no 
problems. However, if you have a time then provide maybe some valuable 
source where I can read about above problems. Otherwise, skip it, in 
case of having no time.

>>                   txt += node.data;
>>               } else if (node.nodeType === 1){
>>                   txt += get_inner_text(node);
>>               }
>
> You should join an Array of string values instead.  Also note that a stack
> overflow could prevent you from providing an adequate result (but the
> iterative approach is not as elegant and may fail as well, only later).

Let me try to rewrite it using Array.

>>               node = node.nextSibling;
>>           }
>>           return txt;
>>       };
>> }
>>
>> How about that?
>
> I do not think it is a good idea to infer from the existence of a property
> on one (host) object the existence of a property of the same name on another

Hm, is it then better to do in that way?

var get_inner_text = (function () {

     // here check what is available and return function

}());

> (host) object.  Therefore, I prefer approaches that test the properties of
> the target object instead.

By "test" you mean using isHostMethod, isHostObjectProperty and 
isRealObjectProperty?

> That aside, you should reconsider the actual probability that
> Node::textContent (DOM Level 3) would not be implemented but
> Document::createTreeWalker() (DOM Level 2 Traversal and Range) was.

Hm, maybe it is worth it to remove textContent and focus only on 
createTreeWalker or normal recursive iteration? Less code to maintenance.

[...]

-- 
Cezary Tomczyk
http://www.ctomczyk.pl/

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-22 18:21 +0100
  Re: Get inner text of HMTL element Martin Honnen <mahotrash@yahoo.de> - 2014-02-22 19:31 +0100
    Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-22 20:51 +0100
      Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-22 21:11 +0100
        Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-22 21:37 +0100
          Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-22 22:05 +0100
            Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-22 23:38 +0100
              Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-23 10:43 +0100
                Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-23 14:50 +0100
                Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-23 15:48 +0100
                Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-24 21:01 +0100
                Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-24 22:34 +0100
                Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-27 21:41 +0100
                Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-27 22:32 +0100
                Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-28 13:44 +0100
                Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-28 16:54 +0100
                Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-03-04 20:51 +0100
                Re: Get inner text of HMTL element ompalompasales2@gmail.com - 2014-03-04 12:59 -0800
  Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-22 20:59 +0100
    Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-22 22:23 +0100
      Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-22 23:32 +0100
  Re: Get inner text of HMTL element Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-02-22 21:27 -0300
    Re: Get inner text of HMTL element "se" <se@no-one.here> - 2014-02-23 05:35 +0100
      Re: Get inner text of HMTL element Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-02-23 15:29 -0300
        Re: Get inner text of HMTL element "se" <se@no-one.here> - 2014-02-23 21:01 +0100
    Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-23 20:02 +0100
      Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-23 21:55 +0100
        Re: Get inner text of HMTL element Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2014-02-24 08:37 +0100
          Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-24 12:30 +0100
            Re: Get inner text of HMTL element Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-02-24 15:09 -0300
              Re: Get inner text of HMTL element Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-24 19:43 +0100
                Re: Get inner text of HMTL element Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-02-24 18:12 -0300
                Re: Get inner text of HMTL element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-24 22:51 +0100
              Re: Get inner text of HMTL element Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-02-25 19:49 +0000
                Re: Get inner text of HMTL element Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-02-26 05:20 -0300
                Re: Get inner text of HMTL element "se" <se@no-one.here> - 2014-02-26 17:10 +0100
                OT - Re: Get inner text of HMTL element Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-02-26 15:48 -0300
                Re: OT - Re: Get inner text of HMTL element "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-02-26 22:43 +0100
                Re: OT - Re: Get inner text of HMTL element Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-02-27 15:00 +0000
                Re: OT - Re: Get inner text of HMTL element "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-02-28 10:50 +0100
                Re: OT - Re: Get inner text of HMTL element "se" <se@no-one.here> - 2014-02-27 10:57 +0100
                Re: Get inner text of HMTL element Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-02-27 15:46 +0000

csiph-web