Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #23228
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail |
|---|---|
| Content-Type | text/plain; charset="UTF-8" |
| Message-ID | <2932926.BXQvjz4BgK@PointedEars.de> (permalink) |
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Reply-To | Thomas 'PointedEars' Lahn <cljs@PointedEars.de> |
| Organization | PointedEars Software (PES) |
| Date | Sun, 23 Feb 2014 15:48:22 +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+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v |
| Face | iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== |
| 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> <1780761.mlrEmVmIzg@PointedEars.de> <leb8sc$23n$5@dont-email.me> <10325533.pBRRTmW78i@PointedEars.de> <lecuah$mib$9@dont-email.me> |
| MIME-Version | 1.0 |
| Lines | 212 |
| NNTP-Posting-Date | 23 Feb 2014 15:48:30 CET |
| NNTP-Posting-Host | 9ebb286e.newsspool1.arcor-online.net |
| X-Trace | DXC=2W7J[beNPmUHigV@eW57PQic==]BZ:af^4Fo<]lROoRQnkgeX?EC@@PKV1GJ:PY58QDZm8W4\YJN\YUVHKH;GI^W>8ZdD_1a;VX_?OaBZ_iSoP |
| X-Complaints-To | usenet-abuse@arcor.de |
| Xref | csiph.com comp.lang.javascript:23228 |
Show key headers only | View raw
Cezary Tomczyk wrote:
> 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') {
I wrote “safely” for a reason. However,
if (typeof document.documentElement != "undefined"
&& document.documentElement !== null
&& typeof document.documentElement.textContent == "string")
{
should be even safer.
Since this is a common task, it is useful to define a general method that
takes an object reference and a list of property names as arguments, whereas
the property names specify the “path” of a property such that the property’s
value can be obtained by repeated property access along that list:
/**
* Determines if a value refers to an object.
*
* <p>Returns <code>true</code> if the value is a reference
* to an object; <code>false</code> otherwise.</p>
*
* <p>An value "is an object" if it is a function or
* <code>typeof "object"</code> but not <code>null</code>.
*
* @return {boolean}
*/
function _isObject (a)
{
var t = typeof a;
return t == "function" || t == "object" && a !== null;
}
return {
/**
* Returns a feature of an object
*
* @param {Object} obj
* Object which provides the feature
* @params {string}
* Property names on the feature path, including the property
* for the feature itself. For example, use
* <code>jsx.object.getFeature("foo", "bar", "baz")</code> for
* safe access to <code>foo.bar.baz</code>.
* @return {any}
* <code>undefined</code> if <var>obj</var> does not have such
* a feature. Note that features whose value can be
* <code>undefined</code> cannot be detected with this method.
*/
getFeature: function (obj) {
for (var i = 1, len = arguments.length; i < len; i++)
{
var arg = arguments[i];
if (_isObject(obj) && typeof obj[arg] != "undefined" && obj[arg])
{
obj = obj[arg];
}
else
{
return void 0;
}
}
return obj;
}
};
See also: <http://PointedEars.de/wsvn/JSX/trunk/object.js>
A refined method would support specification of the property path as an
Array of string values, and primitive false-values. I am working on it.
>>> 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.
Your logic is flawed. It had been unrecoverable exceptions being thrown
upon type-converting tests of host object's methods in MSHTML that prompted
my/the creation of isMethod(), later isHostMethod(), in the first place.
>>> get_inner_text = function(root) {
>>> var w = document.createTreeWalker(root, 4, null, false),
>>> result = '';
>>> while (w.nextNode()) {
>>> result += w.currentNode.data;
>>> }
>>> 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?
If you are to support “innerText” as well, *that* should be handled by the
last branch. Anyhow, there is no guarantee that if some arbitrary element
object has a “firstChild” or “nextSibling” property, the *target* element
object also has it.
Do you see now why it is a bad idea to infer from one property to another,
even if the property names are the same and only the objects differ?
>>> 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.
You are still operating in “Show me where it fails” mode in place of logic.
>>> 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
>
> }());
No, same problem: The feature test does not happen on the target object
because that object is not reliably accessible yet (if it even exists).
>> (host) object. Therefore, I prefer approaches that test the properties
>> of the target object instead.
>
> By "test" you mean using isHostMethod, isHostObjectProperty and
> isRealObjectProperty?
Yes, but not in the DM way, because that is error-prone even though it might
be faster.
>> 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.
And much less efficient with regard to runtime and memory. You should as
yourself instead whether there is any actual *need* for tree-walking at this
point.
--
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
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