Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <1780761.mlrEmVmIzg@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Sat, 22 Feb 2014 22:05:18 +0100 User-Agent: KNode/4.11.5 Content-Transfer-Encoding: 8Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. <5308ed02$0$9512$9b4e6d93@newsspool1.arcor-online.net> <1633562.pVBMJriu5e@PointedEars.de> MIME-Version: 1.0 Lines: 92 NNTP-Posting-Date: 22 Feb 2014 22:05:25 CET NNTP-Posting-Host: 8a4eaae0.newsspool1.arcor-online.net X-Trace: DXC=GQl;VYAe?TDX36K@\WTHGJic==]BZ:afN4Fo<]lROoRAnkgeX?EC@@@IRTKiQcV`1DDZm8W4\YJNLYUVHKH;GI^G>8ZdD_1a;VH^EgS@AXll8O X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:23208 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) -- PointedEars Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.