<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.kde.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.6"
          urlbase="https://bugs.kde.org/"
          
          maintainer="sysadmin@kde.org"
>

    <bug>
          <bug_id>134771</bug_id>
          
          <creation_ts>2006-09-28 02:51:51 +0000</creation_ts>
          <short_desc>instanceof ecma operator does not work on DOM objects</short_desc>
          <delta_ts>2006-10-14 20:00:26 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>2</classification_id>
          <classification>Applications</classification>
          <product>konqueror</product>
          <component>khtml</component>
          <version>unspecified</version>
          <rep_platform>Compiled Sources</rep_platform>
          <op_sys>Linux</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>NOR</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Germain Garand">germain</reporter>
          <assigned_to name="Konqueror Bugs">konqueror-bugs-null</assigned_to>
          <cc>maksim</cc>
          
          <cf_commitlink></cf_commitlink>
          <cf_versionfixedin></cf_versionfixedin>
          <cf_sentryurl></cf_sentryurl>
          <votes>0</votes>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>473359</commentid>
    <comment_count>0</comment_count>
    <who name="Germain Garand">germain</who>
    <bug_when>2006-09-28 02:51:51 +0000</bug_when>
    <thetext>Version:            (using KDE Devel)
Installed from:    Compiled sources

In Mozilla and Opera browsers, instanceof works on DOM objects, so eg.

  (document.getElementsByTagName(&quot;body&quot;)[0] instanceof HTMLElement)
or
  (document instanceof HTMLDocument)

would evaluate to true.

As long as we did not expose (pseudo-)constructor functions it was not a big deal that we didn&apos;t follow this, because script would test e.g. window.HTMLDocument before using instanceof.

Now that we expose the constructor, it seems we must also honour instanceof queries, or we are going to have regressions...

An example of this is:
  http://hop.inria.fr =&gt; &quot;Demos&quot; tab

a sophisticated ajax demo that used to work but now fails because of the above.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>473361</commentid>
    <comment_count>1</comment_count>
      <attachid>17948</attachid>
    <who name="Germain Garand">germain</who>
    <bug_when>2006-09-28 03:01:46 +0000</bug_when>
    <thetext>Created attachment 17948
patch for review

I just lift implementations of hasInstance/implementsHasInstance
from KJS&apos;s InternalFunctionImp, as it seems a perfect fit...</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>473363</commentid>
    <comment_count>2</comment_count>
    <who name="Maksim Orlovich">maksim</who>
    <bug_when>2006-09-28 03:15:58 +0000</bug_when>
    <thetext>I suppose that&apos;s the best way of doing it, thanks for catching it...
</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>473364</commentid>
    <comment_count>3</comment_count>
    <who name="Germain Garand">germain</who>
    <bug_when>2006-09-28 03:30:42 +0000</bug_when>
    <thetext>&gt; I suppose that&apos;s the best way of doing it, thanks for catching it...

glad to hear that as it was just voodoo programming :)

mmh now there is something I don&apos;t get at all,
  document instanceof HTMLDocument
is now true, whereas
  document instanceof Document
isn&apos;t...

At first sight, it doesn&apos;t seem to be a flaw in the implementsHasInstance logic as eg.
  document.getElementsByTagName(&quot;body&quot;)[0] instanceof Element

is OK, and it seems to properly walk the inheritance chain?

I feebly tried to follow the macros implementing the pseudo constructors, but all I got is a severe headache... 



</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>473365</commentid>
    <comment_count>4</comment_count>
    <who name="Maksim Orlovich">maksim</who>
    <bug_when>2006-09-28 03:48:22 +0000</bug_when>
    <thetext>Ugh, the problem is that when we create a prototype with a parent, it doesn&apos;t actually setup a prototype link but delegates manually :-(

The element case works because we only have a single prototype for the entire hierarchy.
</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>473366</commentid>
    <comment_count>5</comment_count>
    <who name="Maksim Orlovich">maksim</who>
    <bug_when>2006-09-28 04:01:59 +0000</bug_when>
    <thetext>.. So basically, to fix this, we would need yet another prototype macro. What ugliness :-(. Might try tomorrow evening... 
</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>474114</commentid>
    <comment_count>6</comment_count>
    <who name="Germain Garand">germain</who>
    <bug_when>2006-10-01 15:41:53 +0000</bug_when>
    <thetext>SVN commit 591021 by ggarand:

commiting partial fix for #134771 to get a working instanceof on most DOM objects

the rest of it is completely above my head and in the benevolent hands of Mighty Maksim...

CCBUG: 134771


 M  +22 -0     kjs_binding.cpp  
 M  +2 -1      kjs_binding.h  


--- branches/KDE/3.5/kdelibs/khtml/ecma/kjs_binding.cpp #591020:591021
@@ -92,6 +92,28 @@
   return &quot;[object &quot; + className() + &quot;]&quot;;
 }
 
+Boolean DOMObject::hasInstance(ExecState *exec, const Value &amp;value)
+{
+  if (value.type() != ObjectType)
+    return Boolean(false);
+
+  Value prot = get(exec,prototypePropertyName);
+  if (prot.type() != ObjectType &amp;&amp; prot.type() != NullType) {
+    Object err = Error::create(exec, TypeError, &quot;Invalid prototype encountered &quot;
+                               &quot;in instanceof operation.&quot;);
+    exec-&gt;setException(err);
+    return Boolean(false);
+  }
+
+  Object v = Object(static_cast&lt;ObjectImp*&gt;(value.imp()));
+  while ((v = Object::dynamicCast(v.prototype())).imp()) {
+    if (v.imp() == prot.imp())
+      return Boolean(true);
+  }
+  return Boolean(false);
+}
+
+
 Value DOMFunction::get(ExecState *exec, const Identifier &amp;propertyName) const
 {
   try {
--- branches/KDE/3.5/kdelibs/khtml/ecma/kjs_binding.h #591020:591021
@@ -53,7 +53,8 @@
     virtual Value get(ExecState *exec, const Identifier &amp;propertyName) const;
     virtual Value tryGet(ExecState *exec, const Identifier &amp;propertyName) const
       { return ObjectImp::get(exec, propertyName); }
-
+    virtual bool implementsHasInstance() const { return true; }
+    virtual Boolean hasInstance(ExecState *exec, const Value &amp;value);
     virtual void put(ExecState *exec, const Identifier &amp;propertyName,
                      const Value &amp;value, int attr = None);
     virtual void tryPut(ExecState *exec, const Identifier &amp;propertyName,
</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>476852</commentid>
    <comment_count>7</comment_count>
    <who name="Maksim Orlovich">maksim</who>
    <bug_when>2006-10-14 19:58:25 +0000</bug_when>
    <thetext>SVN commit 595530 by orlovich:

Backport newer prototype macros, that delegate using the prototype system and not directly.
FIxes #134771, and make this closer to trunk
BUG:134771


 M  +4 -4      domparser.cpp  
 M  +82 -17    kjs_binding.h  
 M  +27 -23    kjs_css.cpp  
 M  +26 -25    kjs_dom.cpp  
 M  +9 -5      kjs_dom.h  
 M  +23 -19    kjs_events.cpp  
 M  +11 -9     kjs_html.cpp  
 M  +9 -6      kjs_range.cpp  
 M  +14 -12    kjs_traversal.cpp  
 M  +2 -1      kjs_window.cpp  
 M  +5 -4      xmlhttprequest.cpp  
 M  +4 -4      xmlserializer.cpp  
</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>476853</commentid>
    <comment_count>8</comment_count>
    <who name="Maksim Orlovich">maksim</who>
    <bug_when>2006-10-14 20:00:26 +0000</bug_when>
    <thetext>SVN commit 595531 by orlovich:

Regression test for #134771, and also shows some of the limitations of our implementation..
CCBUG:134771


 A             baseline/dom/instanceof_pseudoctor.html-dom  
 AM            baseline/dom/instanceof_pseudoctor.html-dump.png  
 A             baseline/dom/instanceof_pseudoctor.html-render  
 A             tests/dom/instanceof_pseudoctor.html  


** trunk/tests/khtmltests/regression/baseline/dom/instanceof_pseudoctor.html-dump.png #property svn:mime-type
   + image/png
</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>17948</attachid>
            <date>2006-09-28 03:01:46 +0000</date>
            <delta_ts>2006-09-28 03:01:46 +0000</delta_ts>
            <desc>patch for review</desc>
            <filename>instanceof.diff</filename>
            <type>text/plain</type>
            <size>1760</size>
            <attacher name="Germain Garand">germain</attacher>
            
              <data encoding="base64">SW5kZXg6IGVjbWEva2pzX2JpbmRpbmcuaAo9PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09
PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09Ci0tLSBlY21hL2tqc19iaW5kaW5n
LmgJKHJldmlzaW9uIDU4ODMxNCkKKysrIGVjbWEva2pzX2JpbmRpbmcuaAkod29ya2luZyBjb3B5
KQpAQCAtNTMsNyArNTMsOCBAQAogICAgIHZpcnR1YWwgVmFsdWUgZ2V0KEV4ZWNTdGF0ZSAqZXhl
YywgY29uc3QgSWRlbnRpZmllciAmcHJvcGVydHlOYW1lKSBjb25zdDsKICAgICB2aXJ0dWFsIFZh
bHVlIHRyeUdldChFeGVjU3RhdGUgKmV4ZWMsIGNvbnN0IElkZW50aWZpZXIgJnByb3BlcnR5TmFt
ZSkgY29uc3QKICAgICAgIHsgcmV0dXJuIE9iamVjdEltcDo6Z2V0KGV4ZWMsIHByb3BlcnR5TmFt
ZSk7IH0KLQorICAgIHZpcnR1YWwgYm9vbCBpbXBsZW1lbnRzSGFzSW5zdGFuY2UoKSBjb25zdCB7
IHJldHVybiB0cnVlOyB9CisgICAgdmlydHVhbCBCb29sZWFuIGhhc0luc3RhbmNlKEV4ZWNTdGF0
ZSAqZXhlYywgY29uc3QgVmFsdWUgJnZhbHVlKTsKICAgICB2aXJ0dWFsIHZvaWQgcHV0KEV4ZWNT
dGF0ZSAqZXhlYywgY29uc3QgSWRlbnRpZmllciAmcHJvcGVydHlOYW1lLAogICAgICAgICAgICAg
ICAgICAgICAgY29uc3QgVmFsdWUgJnZhbHVlLCBpbnQgYXR0ciA9IE5vbmUpOwogICAgIHZpcnR1
YWwgdm9pZCB0cnlQdXQoRXhlY1N0YXRlICpleGVjLCBjb25zdCBJZGVudGlmaWVyICZwcm9wZXJ0
eU5hbWUsCkluZGV4OiBlY21hL2tqc19iaW5kaW5nLmNwcAo9PT09PT09PT09PT09PT09PT09PT09
PT09PT09PT09PT09PT09PT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9CisgIHJldHVybiBCb29sZWFu
KGZhbHNlKTsKK30KKworCiBWYWx1ZSBET01GdW5jdGlvbjo6Z2V0KEV4ZWNTdGF0ZSAqZXhlYywg
Y29uc3QgSWRlbnRpZmllciAmcHJvcGVydHlOYW1lKSBjb25zdAogewogICB0cnkgewo=
</data>

          </attachment>
      

    </bug>

</bugzilla>