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


Groups > comp.lang.javascript > #23208

Re: Get inner text of HMTL element

Message-ID <1780761.mlrEmVmIzg@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2014-02-22 22:05 +0100
Subject Re: Get inner text of HMTL element
Newsgroups comp.lang.javascript
References <leambb$nfo$1@dont-email.me> <5308ed02$0$9512$9b4e6d93@newsspool1.arcor-online.net> <leav3t$aa4$5@dont-email.me> <1633562.pVBMJriu5e@PointedEars.de> <leb1q1$r9i$1@dont-email.me>

Show all headers | View raw


Cezary Tomczyk wrote:

> W dniu 2014-02-22 21:11, Thomas 'PointedEars' Lahn pisze:
>> Cezary Tomczyk wrote:
>>> On 2014-02-22 19:31, Martin Honnen wrote:
>>>> Cezary Tomczyk wrote:
>>>>> I would use now:
>>>>>
>>>>> var str = elm.textContent || elm.data || elm.innerText;
>>>>>
>>>>> Is it a good approach?
>>>>
>>>> No, with a browser implementing the W3C DOM Level 3 textContent
>>>> property and an empty element "elm" and no support for the other
>>>> properties you would get the value undefined, see
>>>> http://jsfiddle.net/55euf/, instead of the empty string.
>>>
>>> I've updated the code. How about that?
>>>
>>> http://jsfiddle.net/55euf/1/
>>
>> How about posting it?
> 
> No problem. Here we go:
> 
> function get_inner_text(elm) {
>      var txt = '';
>      if (elm && elm.childNodes && elm.childNodes.length > 0) {
>          txt = elm.data || elm.textContent || elm.innerText;
>      }
>      return txt;
> }

Better (but unnecessarily inefficient: consider the “firstChild” property), 
but see below.
 
>>> P.S. I should write first unit tests for it and probably this would
>>> catch scenario which you've described.
>>
>> You do not need a unit test for this, only deeper and clear thinking.
>> What is the result of
>>
>>    "" || void 0 || void 0
>>
>> ?
> 
> "" is converted to "false" boolean
> void 0 is evaluated to "undefined" and then to "false" boolean
> 
> Because all of them are converted to "false" boolean then "undefined" by
> default is returned.
> 
> How about that?

The final operand¹ is not converted, and no operand is actually converted 
(instead of just internally for the purpose of determining shortcut 
evaluation).  And nothing is *returned*.  Otherwise you have described it 
correctly.
 
> function get_inner_text(elm) {
>      var txt = (elm.data || elm.textContent || elm.innerText);
>      if (!txt) {
>          txt = '';
>      }
>      return txt;
> }

Much the same could be achieved with

  var text = elm.data || elm.textContent || elm.innerText || "";

and it would still be wrong.

For a start, one wonders how you got the idea that an *element* object had a 
(built-in, standards-compliant) “data” property to yield the text content of 
the represented element.

_______
¹ Colloquially speaking; actually, we are looking at only one “||” operation 
  whose first operand is the result of another one: “a || b || c” is parsed
  like “(a || b) || c” due to the production
  
  | LogicalORExpression(NoIn) :
  |   LogicalANDExpression(NoIn)
  |   LogicalORExpression || LogicalANDExpression(NoIn)

  <http://ecma-international.org/ecma-262/5.1/#sec-11.11>
-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

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