Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.fsmpi.rwth-aachen.de!news-1.dfn.de!news.dfn.de!fu-berlin.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <10325533.pBRRTmW78i@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Sun, 23 Feb 2014 10:43:33 +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> <1780761.mlrEmVmIzg@PointedEars.de> MIME-Version: 1.0 Lines: 96 NNTP-Posting-Date: 23 Feb 2014 10:43:36 CET NNTP-Posting-Host: 21565d3f.newsspool1.arcor-online.net X-Trace: DXC=BKKK4Fo<]lROoR1nkgeX?EC@@0BeLlkVT3cf4DZm8W4\YJN8ZdD_1a;V85`RF76Fi192 X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:23215 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") { > 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(). > 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. > 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. > 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). > 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 (host) object. Therefore, I prefer approaches that test the properties of the target object instead. 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. MDN says (unverified): Node::textContent document.createTreeWalker()¹ ------------------------------------------------------------------ Chrome 1+ 1.0 Firefox (Gecko) (Yes) 2.0 (1.8.1) Internet Explorer 9 9.0 Opera (Yes) 9.0 Safari (Yes) 3.0 ------------------------------------------------------------------ ______ ¹ Basic support, respectively. -- PointedEars Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.