Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * This file is part of libdom.
  3.  * Licensed under the MIT License,
  4.  *                http://www.opensource.org/licenses/mit-license.php
  5.  * Copyright 2012 Daniel Silverstone <dsilvers@netsurf-browser.org>
  6.  */
  7.  
  8. #include <assert.h>
  9. #include <stdlib.h>
  10.  
  11. #include <dom/html/html_input_element.h>
  12.  
  13. #include "html/html_document.h"
  14. #include "html/html_input_element.h"
  15.  
  16. #include "core/node.h"
  17. #include "core/attr.h"
  18. #include "utils/utils.h"
  19.  
  20. static struct dom_element_protected_vtable _protect_vtable = {
  21.         {
  22.                 DOM_NODE_PROTECT_VTABLE_HTML_INPUT_ELEMENT
  23.         },
  24.         DOM_HTML_INPUT_ELEMENT_PROTECT_VTABLE
  25. };
  26.  
  27. /**
  28.  * Create a dom_html_input_element object
  29.  *
  30.  * \param doc  The document object
  31.  * \param ele  The returned element object
  32.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  33.  */
  34. dom_exception _dom_html_input_element_create(struct dom_html_document *doc,
  35.                 dom_string *namespace, dom_string *prefix,
  36.                 struct dom_html_input_element **ele)
  37. {
  38.         struct dom_node_internal *node;
  39.  
  40.         *ele = malloc(sizeof(dom_html_input_element));
  41.         if (*ele == NULL)
  42.                 return DOM_NO_MEM_ERR;
  43.  
  44.         /* Set up vtables */
  45.         node = (struct dom_node_internal *) *ele;
  46.         node->base.vtable = &_dom_html_element_vtable;
  47.         node->vtable = &_protect_vtable;
  48.  
  49.         return _dom_html_input_element_initialise(doc, namespace, prefix, *ele);
  50. }
  51.  
  52. /**
  53.  * Initialise a dom_html_input_element object
  54.  *
  55.  * \param doc  The document object
  56.  * \param ele  The dom_html_input_element object
  57.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  58.  */
  59. dom_exception _dom_html_input_element_initialise(struct dom_html_document *doc,
  60.                 dom_string *namespace, dom_string *prefix,
  61.                 struct dom_html_input_element *ele)
  62. {
  63.         ele->form = NULL;
  64.         ele->default_checked = false;
  65.         ele->default_checked_set = false;
  66.         ele->default_value = NULL;
  67.         ele->default_value_set = false;
  68.  
  69.         return _dom_html_element_initialise(doc, &ele->base,
  70.                                             doc->memoised[hds_INPUT],
  71.                                             namespace, prefix);
  72. }
  73.  
  74. /**
  75.  * Finalise a dom_html_input_element object
  76.  *
  77.  * \param ele  The dom_html_input_element object
  78.  */
  79. void _dom_html_input_element_finalise(struct dom_html_input_element *ele)
  80. {
  81.         if (ele->default_value != NULL) {
  82.                 dom_string_unref(ele->default_value);
  83.                 ele->default_value = NULL;
  84.         }
  85.  
  86.         _dom_html_element_finalise(&ele->base);
  87. }
  88.  
  89. /**
  90.  * Destroy a dom_html_input_element object
  91.  *
  92.  * \param ele  The dom_html_input_element object
  93.  */
  94. void _dom_html_input_element_destroy(struct dom_html_input_element *ele)
  95. {
  96.         _dom_html_input_element_finalise(ele);
  97.         free(ele);
  98. }
  99.  
  100. /*-----------------------------------------------------------------------*/
  101. /* Public APIs */
  102.  
  103. /**
  104.  * Get the disabled property
  105.  *
  106.  * \param ele       The dom_html_input_element object
  107.  * \param disabled  The returned status
  108.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  109.  */
  110. dom_exception dom_html_input_element_get_disabled(dom_html_input_element *ele,
  111.                 bool *disabled)
  112. {
  113.         return dom_html_element_get_bool_property(&ele->base, "disabled",
  114.                         SLEN("disabled"), disabled);
  115. }
  116.  
  117. /**
  118.  * Set the disabled property
  119.  *
  120.  * \param ele       The dom_html_input_element object
  121.  * \param disabled  The status
  122.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  123.  */
  124. dom_exception dom_html_input_element_set_disabled(dom_html_input_element *ele,
  125.                 bool disabled)
  126. {
  127.         return dom_html_element_set_bool_property(&ele->base, "disabled",
  128.                         SLEN("disabled"), disabled);
  129. }
  130.  
  131. /**
  132.  * Get the readOnly property
  133.  *
  134.  * \param ele       The dom_html_input_element object
  135.  * \param disabled  The returned status
  136.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  137.  */
  138. dom_exception dom_html_input_element_get_read_only(dom_html_input_element *ele,
  139.                 bool *read_only)
  140. {
  141.         return dom_html_element_get_bool_property(&ele->base, "readonly",
  142.                         SLEN("readonly"), read_only);
  143. }
  144.  
  145. /**
  146.  * Set the readOnly property
  147.  *
  148.  * \param ele       The dom_html_input_element object
  149.  * \param disabled  The status
  150.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  151.  */
  152. dom_exception dom_html_input_element_set_read_only(dom_html_input_element *ele,
  153.                 bool read_only)
  154. {
  155.         return dom_html_element_set_bool_property(&ele->base, "readonly",
  156.                         SLEN("readonly"), read_only);
  157. }
  158.  
  159. /**
  160.  * Get the checked property
  161.  *
  162.  * \param ele       The dom_html_input_element object
  163.  * \param disabled  The returned status
  164.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  165.  */
  166. dom_exception dom_html_input_element_get_checked(dom_html_input_element *ele,
  167.                 bool *checked)
  168. {
  169.         return dom_html_element_get_bool_property(&ele->base, "checked",
  170.                         SLEN("checked"), checked);
  171. }
  172.  
  173. /**
  174.  * Set the checked property
  175.  *
  176.  * \param ele       The dom_html_input_element object
  177.  * \param disabled  The status
  178.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  179.  */
  180. dom_exception dom_html_input_element_set_checked(dom_html_input_element *ele,
  181.                 bool checked)
  182. {
  183.         return dom_html_element_set_bool_property(&ele->base, "checked",
  184.                         SLEN("checked"), checked);
  185. }
  186.  
  187. /**
  188.  * Get the defaultValue property
  189.  *
  190.  * \param ele       The dom_html_input_element object
  191.  * \param disabled  The returned status
  192.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  193.  */
  194. dom_exception dom_html_input_element_get_default_value(
  195.         dom_html_input_element *ele, dom_string **default_value)
  196. {
  197.         *default_value = ele->default_value;
  198.  
  199.         if (*default_value != NULL)
  200.                 dom_string_ref(*default_value);
  201.  
  202.         return DOM_NO_ERR;
  203. }
  204.  
  205. /**
  206.  * Set the defaultValue property
  207.  *
  208.  * \param ele       The dom_html_input_element object
  209.  * \param disabled  The status
  210.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  211.  */
  212. dom_exception dom_html_input_element_set_default_value(
  213.         dom_html_input_element *ele, dom_string *default_value)
  214. {
  215.         if (ele->default_value != NULL)
  216.                 dom_string_unref(ele->default_value);
  217.  
  218.         ele->default_value = default_value;
  219.         ele->default_value_set = true;
  220.  
  221.         if (ele->default_value != NULL)
  222.                 dom_string_ref(ele->default_value);
  223.  
  224.         return DOM_NO_ERR;
  225. }
  226.  
  227. /**
  228.  * Get the defaultChecked property
  229.  *
  230.  * \param ele       The dom_html_input_element object
  231.  * \param disabled  The returned status
  232.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  233.  */
  234. dom_exception dom_html_input_element_get_default_checked(
  235.         dom_html_input_element *ele, bool *default_checked)
  236. {
  237.         *default_checked = ele->default_checked;
  238.  
  239.         return DOM_NO_ERR;
  240. }
  241.  
  242. /**
  243.  * Set the defaultChecked property
  244.  *
  245.  * \param ele       The dom_html_input_element object
  246.  * \param disabled  The status
  247.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  248.  */
  249. dom_exception dom_html_input_element_set_default_checked(
  250.         dom_html_input_element *ele, bool default_checked)
  251. {
  252.         ele->default_checked = default_checked;
  253.         ele->default_checked_set = true;
  254.  
  255.         return DOM_NO_ERR;
  256. }
  257.  
  258. /*------------------------------------------------------------------------*/
  259. /* The protected virtual functions */
  260.  
  261. /* The virtual function used to parse attribute value, see src/core/element.c
  262.  * for detail */
  263. dom_exception _dom_html_input_element_parse_attribute(dom_element *ele,
  264.                 dom_string *name, dom_string *value,
  265.                 dom_string **parsed)
  266. {
  267.         dom_html_input_element *input = (dom_html_input_element *)ele;
  268.         dom_html_document *html = (dom_html_document *)(ele->base.owner);
  269.  
  270.         /** \todo Find some way to do the equiv for default_checked to be
  271.          * false instead of true.  Some end-tag hook in the binding perhaps?
  272.          */
  273.         if (dom_string_caseless_isequal(name, html->memoised[hds_checked])) {
  274.                 if (input->default_checked_set == false) {
  275.                         input->default_checked = true;
  276.                         input->default_checked_set = true;
  277.                 }
  278.         }
  279.  
  280.         if (dom_string_caseless_isequal(name, html->memoised[hds_value])) {
  281.                 if (input->default_value_set == false) {
  282.                         input->default_value = value;
  283.                         dom_string_ref(value);
  284.                         input->default_value_set = true;
  285.                 }
  286.         }
  287.  
  288.         dom_string_ref(value);
  289.         *parsed = value;
  290.  
  291.         return DOM_NO_ERR;
  292. }
  293.  
  294. /* The virtual destroy function, see src/core/node.c for detail */
  295. void _dom_virtual_html_input_element_destroy(dom_node_internal *node)
  296. {
  297.         _dom_html_input_element_destroy((struct dom_html_input_element *) node);
  298. }
  299.  
  300. /* The virtual copy function, see src/core/node.c for detail */
  301. dom_exception _dom_html_input_element_copy(dom_node_internal *old,
  302.                 dom_node_internal **copy)
  303. {
  304.         return _dom_html_element_copy(old, copy);
  305. }
  306.  
  307. /*-----------------------------------------------------------------------*/
  308. /* API functions */
  309.  
  310. #define SIMPLE_GET(attr)                                                \
  311.         dom_exception dom_html_input_element_get_##attr(                \
  312.                 dom_html_input_element *element,                        \
  313.                 dom_string **attr)                                      \
  314.         {                                                               \
  315.                 dom_exception ret;                                      \
  316.                 dom_string *_memo_##attr;                               \
  317.                                                                         \
  318.                 _memo_##attr =                                          \
  319.                         ((struct dom_html_document *)                   \
  320.                          ((struct dom_node_internal *)element)->owner)->\
  321.                         memoised[hds_##attr];                           \
  322.                                                                         \
  323.                 ret = dom_element_get_attribute(element, _memo_##attr, attr); \
  324.                                                                         \
  325.                 return ret;                                             \
  326.         }
  327. #define SIMPLE_SET(attr)                                                \
  328. dom_exception dom_html_input_element_set_##attr(                        \
  329.                 dom_html_input_element *element,                        \
  330.                 dom_string *attr)                                       \
  331.         {                                                               \
  332.                 dom_exception ret;                                      \
  333.                 dom_string *_memo_##attr;                               \
  334.                                                                         \
  335.                 _memo_##attr =                                          \
  336.                         ((struct dom_html_document *)                   \
  337.                          ((struct dom_node_internal *)element)->owner)->\
  338.                         memoised[hds_##attr];                           \
  339.                                                                         \
  340.                 ret = dom_element_set_attribute(element, _memo_##attr, attr); \
  341.                                                                         \
  342.                 return ret;                                             \
  343.         }
  344.  
  345. #define SIMPLE_GET_SET(attr) SIMPLE_GET(attr) SIMPLE_SET(attr)
  346.  
  347. SIMPLE_GET_SET(accept);
  348. SIMPLE_GET_SET(access_key);
  349. SIMPLE_GET_SET(align);
  350. SIMPLE_GET_SET(alt);
  351. SIMPLE_GET_SET(name);
  352. SIMPLE_GET_SET(size);
  353. SIMPLE_GET_SET(src);
  354. SIMPLE_GET(type);
  355. SIMPLE_GET_SET(use_map);
  356. SIMPLE_GET_SET(value);
  357.  
  358. dom_exception dom_html_input_element_get_tab_index(
  359.         dom_html_input_element *input, int32_t *tab_index)
  360. {
  361.         return dom_html_element_get_int32_t_property(&input->base, "tabindex",
  362.                         SLEN("tabindex"), tab_index);
  363. }
  364.  
  365. dom_exception dom_html_input_element_set_tab_index(
  366.         dom_html_input_element *input, uint32_t tab_index)
  367. {
  368.         return dom_html_element_set_int32_t_property(&input->base, "tabindex",
  369.                         SLEN("tabindex"), tab_index);
  370. }
  371.  
  372. dom_exception dom_html_input_element_get_max_length(
  373.         dom_html_input_element *input, int32_t *max_length)
  374. {
  375.         return dom_html_element_get_int32_t_property(&input->base, "maxlength",
  376.                         SLEN("maxlength"), max_length);
  377. }
  378.  
  379. dom_exception dom_html_input_element_set_max_length(
  380.         dom_html_input_element *input, uint32_t max_length)
  381. {
  382.         return dom_html_element_set_int32_t_property(&input->base, "maxlength",
  383.                         SLEN("maxlength"), max_length);
  384. }
  385.  
  386. dom_exception dom_html_input_element_get_form(
  387.         dom_html_input_element *input, dom_html_form_element **form)
  388. {
  389.         *form = input->form;
  390.  
  391.         if (*form != NULL)
  392.                 dom_node_ref(*form);
  393.  
  394.         return DOM_NO_ERR;
  395. }
  396.  
  397. dom_exception _dom_html_input_element_set_form(
  398.         dom_html_input_element *input, dom_html_form_element *form)
  399. {
  400.         input->form = form;
  401.  
  402.         return DOM_NO_ERR;
  403. }
  404.  
  405. /**
  406.  * Blur this control
  407.  *
  408.  * \param ele  The form object
  409.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  410.  */
  411. dom_exception dom_html_input_element_blur(dom_html_input_element *ele)
  412. {
  413.         struct dom_html_document *doc =
  414.                 (dom_html_document *) dom_node_get_owner(ele);
  415.         bool success = false;
  416.         assert(doc != NULL);
  417.  
  418.         /** \todo Is this event (a) default (b) bubbling and (c) cancelable? */
  419.         return _dom_dispatch_generic_event((dom_document *) doc,
  420.                                            (dom_event_target *) ele,
  421.                                            doc->memoised[hds_blur], true,
  422.                                            true, &success);
  423. }
  424.  
  425. /**
  426.  * Focus this control
  427.  *
  428.  * \param ele  The form object
  429.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  430.  */
  431. dom_exception dom_html_input_element_focus(dom_html_input_element *ele)
  432. {
  433.         struct dom_html_document *doc =
  434.                 (dom_html_document *) dom_node_get_owner(ele);
  435.         bool success = false;
  436.         assert(doc != NULL);
  437.  
  438.         /** \todo Is this event (a) default (b) bubbling and (c) cancelable? */
  439.         return _dom_dispatch_generic_event((dom_document *)doc,
  440.                                            (dom_event_target *) ele,
  441.                                            doc->memoised[hds_focus], true,
  442.                                            true, &success);
  443. }
  444.  
  445. /**
  446.  * Select this control
  447.  *
  448.  * \param ele  The form object
  449.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  450.  */
  451. dom_exception dom_html_input_element_select(dom_html_input_element *ele)
  452. {
  453.         struct dom_html_document *doc =
  454.                 (dom_html_document *) dom_node_get_owner(ele);
  455.         bool success = false;
  456.         assert(doc != NULL);
  457.  
  458.         /** \todo Is this event (a) default (b) bubbling and (c) cancelable? */
  459.         return _dom_dispatch_generic_event((dom_document *)doc,
  460.                                            (dom_event_target *) ele,
  461.                                            doc->memoised[hds_select], true,
  462.                                            true, &success);
  463. }
  464.  
  465. /**
  466.  * Click this control
  467.  *
  468.  * \param ele  The form object
  469.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  470.  */
  471. dom_exception dom_html_input_element_click(dom_html_input_element *ele)
  472. {
  473.         struct dom_html_document *doc =
  474.                 (dom_html_document *) dom_node_get_owner(ele);
  475.         bool success = false;
  476.         assert(doc != NULL);
  477.  
  478.         /** \todo Is this is meant to check/uncheck boxes, radios etc */
  479.         /** \todo Is this event (a) default (b) bubbling and (c) cancelable? */
  480.         return _dom_dispatch_generic_event((dom_document *)doc,
  481.                                            (dom_event_target *) ele,
  482.                                            doc->memoised[hds_click], true,
  483.                                            true, &success);
  484. }
  485.  
  486.