Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /* Binding to generate interfaces for the DOM IDL
  2.  *
  3.  * Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>
  4.  *
  5.  * This file is part of NetSurf, http://www.netsurf-browser.org/
  6.  *
  7.  * Released under the terms of the MIT License,
  8.  *         http://www.opensource.org/licenses/mit-license
  9.  */
  10.  
  11. webidlfile "dom.idl";
  12.  
  13. preamble %{
  14. #include "comment.h"
  15. #include "text.h"
  16. #include "htmlelement.h"
  17. %}
  18.  
  19.  
  20. prologue %{
  21. /* CAUTION this expects all javascript Node objects private pointers
  22.  * to have private->node in the same place.
  23.  */
  24. static struct dom_node *jsnode_to_domnode(JSContext *cx, JSObject *jsnode)
  25. {
  26.         struct jsclass_private *jsnode_private;
  27.  
  28.         if (jsnode == NULL) {
  29.                 return NULL;
  30.         }
  31.  
  32.         /* element */
  33.         jsnode_private = JS_GetInstancePrivate(cx,
  34.                                                jsnode,
  35.                                                &JSClass_HTMLElement,
  36.                                                NULL);
  37.         if (jsnode_private != NULL) {
  38.                 return (struct dom_node *)jsnode_private->node;
  39.         }
  40.  
  41.         /* text */
  42.         jsnode_private = JS_GetInstancePrivate(cx,
  43.                                                jsnode,
  44.                                                &JSClass_Text,
  45.                                                NULL);
  46.         if (jsnode_private != NULL) {
  47.                 return (struct dom_node *)jsnode_private->node;
  48.         }
  49.  
  50.         /* comment */
  51.         jsnode_private = JS_GetInstancePrivate(cx,
  52.                                                jsnode,
  53.                                                &JSClass_Comment,
  54.                                                NULL);
  55.         if (jsnode_private != NULL) {
  56.                 return (struct dom_node *)jsnode_private->node;
  57.         }
  58.  
  59.         return NULL;
  60. }
  61.  
  62. %}
  63.  
  64. /* interface Node members */
  65.  
  66. getter nodeType %{
  67.         dom_exception exc;
  68.         dom_node_type node_type;
  69.  
  70.         exc = dom_node_get_node_type(private->node, &node_type);
  71.         if (exc != DOM_NO_ERR) {
  72.                 return JS_FALSE;
  73.         }
  74.         jsret = node_type;
  75. %}
  76.  
  77.  
  78. getter nodeName %{
  79.         dom_exception exc;
  80.         dom_string *name;
  81.  
  82.         exc = dom_node_get_node_name(private->node, &name);
  83.         if (exc != DOM_NO_ERR) {
  84.                 return JS_FALSE;
  85.         }
  86.  
  87.         if (name != NULL) {
  88.                 jsret = JS_NewStringCopyN(cx,
  89.                                           dom_string_data(name),
  90.                                           dom_string_length(name));
  91.                 dom_string_unref(name);
  92.         }
  93. %}
  94.  
  95. getter nodeValue %{
  96.         dom_exception exc;
  97.         dom_string *value;
  98.  
  99.         exc = dom_node_get_node_value(private->node, &value);
  100.         if (exc != DOM_NO_ERR) {
  101.                 return JS_FALSE;
  102.         }
  103.  
  104.         if (value != NULL) {
  105.                 jsret = JS_NewStringCopyN(cx,
  106.                                           dom_string_data(value),
  107.                                           dom_string_length(value));
  108.                 dom_string_unref(value);
  109.         }
  110. %}
  111.  
  112. getter textContent %{
  113.         dom_exception exc;
  114.         dom_string *content;
  115.  
  116.         exc = dom_node_get_text_content(private->node, &content);
  117.         if (exc != DOM_NO_ERR) {
  118.                 return JS_FALSE;
  119.         }
  120.  
  121.         if (content != NULL) {
  122.                 jsret = JS_NewStringCopyN(cx, dom_string_data(content), dom_string_length(content));
  123.                 dom_string_unref(content);
  124.  
  125.         }
  126. %}
  127.  
  128. /* interface Node { Node appendChild(Node node); } */
  129. operation appendChild %{
  130.         struct dom_node *domnode; /* dom node from js input node */
  131.         struct dom_node *result = NULL;
  132.         dom_exception exc;
  133.         dom_node_type node_type;
  134.  
  135.         domnode = jsnode_to_domnode(cx, node);
  136.         if (domnode == NULL) {
  137.                 /* should cause Error: NOT_FOUND_ERR: DOM Exception 8 */
  138.                 JSLOG("Error: NOT_FOUND_ERR: DOM Exception 8");
  139.                 return JS_FALSE;
  140.         }
  141.  
  142.         JSLOG("appending js node %p (dom %p)", node, domnode);
  143.  
  144.         /* append the found element */
  145.         exc = dom_node_append_child(private->node, domnode, &result);
  146.         if (exc != DOM_NO_ERR) {
  147.                 JSLOG("Error: DOM Exception (libdom append child)");
  148.                 return JS_FALSE;
  149.         }
  150.  
  151.         if (result != NULL) {
  152.                 exc = dom_node_get_node_type(result, &node_type);
  153.                 if (exc != DOM_NO_ERR) {
  154.                         return JS_FALSE;
  155.                 }
  156.                 switch (node_type) {
  157.                 case DOM_ELEMENT_NODE:
  158.                         jsret = jsapi_new_HTMLElement(cx, NULL, NULL, (dom_element *)result, private->htmlc);
  159.                         break;
  160.  
  161.                 case DOM_TEXT_NODE:
  162.                         jsret = jsapi_new_Text(cx, NULL, NULL, (dom_text *)result, private->htmlc);
  163.                         break;
  164.  
  165.                 default:
  166.                         JSLOG("Unsupported result node type %d", node_type);
  167.                 }
  168.  
  169.         } else {
  170.                 JSLOG("No result");
  171.         }
  172. %}
  173.