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_text_area_element.h>
  12.  
  13. #include "html/html_document.h"
  14. #include "html/html_text_area_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_TEXT_AREA_ELEMENT
  23.         },
  24.         DOM_HTML_TEXT_AREA_ELEMENT_PROTECT_VTABLE
  25. };
  26.  
  27. /**
  28.  * Create a dom_html_text_area_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_text_area_element_create(struct dom_html_document *doc,
  35.                 dom_string *namespace, dom_string *prefix,
  36.                 struct dom_html_text_area_element **ele)
  37. {
  38.         struct dom_node_internal *node;
  39.  
  40.         *ele = malloc(sizeof(dom_html_text_area_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_text_area_element_initialise(doc, namespace, prefix, *ele);
  50. }
  51.  
  52. /**
  53.  * Initialise a dom_html_text_area_element object
  54.  *
  55.  * \param doc  The document object
  56.  * \param ele  The dom_html_text_area_element object
  57.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  58.  */
  59. dom_exception _dom_html_text_area_element_initialise(struct dom_html_document *doc,
  60.                 dom_string *namespace, dom_string *prefix,
  61.                 struct dom_html_text_area_element *ele)
  62. {
  63.         ele->form = NULL;
  64.         ele->default_value = NULL;
  65.         ele->default_value_set = false;
  66.         ele->value = NULL;
  67.         ele->value_set = false;
  68.  
  69.         return _dom_html_element_initialise(doc, &ele->base,
  70.                                             doc->memoised[hds_TEXTAREA],
  71.                                             namespace, prefix);
  72. }
  73.  
  74. /**
  75.  * Finalise a dom_html_text_area_element object
  76.  *
  77.  * \param ele  The dom_html_text_area_element object
  78.  */
  79. void _dom_html_text_area_element_finalise(struct dom_html_text_area_element *ele)
  80. {
  81.         if (ele->default_value != NULL) {
  82.                 dom_string_unref(ele->default_value);
  83.                 ele->default_value = NULL;
  84.                 ele->default_value_set = false;
  85.         }
  86.  
  87.         if (ele->value != NULL) {
  88.                 dom_string_unref(ele->value);
  89.                 ele->value = NULL;
  90.                 ele->value_set = false;
  91.         }
  92.  
  93.         _dom_html_element_finalise(&ele->base);
  94. }
  95.  
  96. /**
  97.  * Destroy a dom_html_text_area_element object
  98.  *
  99.  * \param ele  The dom_html_text_area_element object
  100.  */
  101. void _dom_html_text_area_element_destroy(struct dom_html_text_area_element *ele)
  102. {
  103.         _dom_html_text_area_element_finalise(ele);
  104.         free(ele);
  105. }
  106.  
  107. /*-----------------------------------------------------------------------*/
  108. /* Public APIs */
  109.  
  110. /**
  111.  * Get the disabled property
  112.  *
  113.  * \param ele       The dom_html_text_area_element object
  114.  * \param disabled  The returned status
  115.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  116.  */
  117. dom_exception dom_html_text_area_element_get_disabled(dom_html_text_area_element *ele,
  118.                 bool *disabled)
  119. {
  120.         return dom_html_element_get_bool_property(&ele->base, "disabled",
  121.                         SLEN("disabled"), disabled);
  122. }
  123.  
  124. /**
  125.  * Set the disabled property
  126.  *
  127.  * \param ele       The dom_html_text_area_element object
  128.  * \param disabled  The status
  129.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  130.  */
  131. dom_exception dom_html_text_area_element_set_disabled(dom_html_text_area_element *ele,
  132.                 bool disabled)
  133. {
  134.         return dom_html_element_set_bool_property(&ele->base, "disabled",
  135.                         SLEN("disabled"), disabled);
  136. }
  137.  
  138. /**
  139.  * Get the readOnly property
  140.  *
  141.  * \param ele       The dom_html_text_area_element object
  142.  * \param disabled  The returned status
  143.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  144.  */
  145. dom_exception dom_html_text_area_element_get_read_only(dom_html_text_area_element *ele,
  146.                 bool *read_only)
  147. {
  148.         return dom_html_element_get_bool_property(&ele->base, "readonly",
  149.                         SLEN("readonly"), read_only);
  150. }
  151.  
  152. /**
  153.  * Set the readOnly property
  154.  *
  155.  * \param ele       The dom_html_text_area_element object
  156.  * \param disabled  The status
  157.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  158.  */
  159. dom_exception dom_html_text_area_element_set_read_only(dom_html_text_area_element *ele,
  160.                 bool read_only)
  161. {
  162.         return dom_html_element_set_bool_property(&ele->base, "readonly",
  163.                         SLEN("readonly"), read_only);
  164. }
  165.  
  166. /**
  167.  * Get the defaultValue property
  168.  *
  169.  * \param ele       The dom_html_text_area_element object
  170.  * \param disabled  The returned status
  171.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  172.  */
  173. dom_exception dom_html_text_area_element_get_default_value(
  174.         dom_html_text_area_element *ele, dom_string **default_value)
  175. {
  176.         dom_exception err;
  177.  
  178.         if (ele->default_value_set == false) {
  179.                 err = dom_node_get_text_content((dom_node *)ele,
  180.                                                 &ele->default_value);
  181.                 if (err == DOM_NO_ERR) {
  182.                         ele->default_value_set = true;
  183.                 }
  184.         }
  185.  
  186.         *default_value = ele->default_value;
  187.  
  188.         if (*default_value != NULL)
  189.                 dom_string_ref(*default_value);
  190.  
  191.         return DOM_NO_ERR;
  192. }
  193.  
  194. /**
  195.  * Set the defaultValue property
  196.  *
  197.  * \param ele       The dom_html_text_area_element object
  198.  * \param disabled  The status
  199.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  200.  */
  201. dom_exception dom_html_text_area_element_set_default_value(
  202.         dom_html_text_area_element *ele, dom_string *default_value)
  203. {
  204.         if (ele->default_value != NULL)
  205.                 dom_string_unref(ele->default_value);
  206.  
  207.         ele->default_value = default_value;
  208.         ele->default_value_set = true;
  209.  
  210.         if (ele->default_value != NULL)
  211.                 dom_string_ref(ele->default_value);
  212.  
  213.         return DOM_NO_ERR;
  214. }
  215.  
  216. /**
  217.  * Get the value property
  218.  *
  219.  * \param ele       The dom_html_text_area_element object
  220.  * \param disabled  The returned status
  221.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  222.  */
  223. dom_exception dom_html_text_area_element_get_value(
  224.         dom_html_text_area_element *ele, dom_string **value)
  225. {
  226.         dom_exception err;
  227.  
  228.         if (ele->value_set == false) {
  229.                 err = dom_node_get_text_content((dom_node *)ele,
  230.                                                 &ele->value);
  231.                 if (err == DOM_NO_ERR) {
  232.                         ele->default_value_set = true;
  233.                         if (ele->default_value_set == false) {
  234.                                 ele->default_value_set = true;
  235.                                 ele->default_value = ele->value;
  236.                                 dom_string_ref(ele->default_value);
  237.                         }
  238.                 }
  239.         }
  240.  
  241.         *value = ele->value;
  242.  
  243.         if (*value != NULL)
  244.                 dom_string_ref(*value);
  245.  
  246.         return DOM_NO_ERR;
  247. }
  248.  
  249. /**
  250.  * Set the value property
  251.  *
  252.  * \param ele       The dom_html_text_area_element object
  253.  * \param disabled  The status
  254.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  255.  */
  256. dom_exception dom_html_text_area_element_set_value(
  257.         dom_html_text_area_element *ele, dom_string *value)
  258. {
  259.         dom_exception err;
  260.  
  261.         if (ele->default_value_set == false) {
  262.                 err = dom_node_get_text_content((dom_node *)ele,
  263.                                                 &ele->default_value);
  264.                 if (err == DOM_NO_ERR) {
  265.                         ele->default_value_set = true;
  266.                 }
  267.         }
  268.  
  269.         if (ele->value != NULL)
  270.                 dom_string_unref(ele->value);
  271.  
  272.         ele->value = value;
  273.         ele->value_set = true;
  274.  
  275.         if (ele->value != NULL)
  276.                 dom_string_ref(ele->value);
  277.  
  278.         return DOM_NO_ERR;
  279. }
  280.  
  281. /*------------------------------------------------------------------------*/
  282. /* The protected virtual functions */
  283.  
  284. /* The virtual function used to parse attribute value, see src/core/element.c
  285.  * for detail */
  286. dom_exception _dom_html_text_area_element_parse_attribute(dom_element *ele,
  287.                 dom_string *name, dom_string *value,
  288.                 dom_string **parsed)
  289. {
  290.         UNUSED(ele);
  291.         UNUSED(name);
  292.  
  293.         dom_string_ref(value);
  294.         *parsed = value;
  295.  
  296.         return DOM_NO_ERR;
  297. }
  298.  
  299. /* The virtual destroy function, see src/core/node.c for detail */
  300. void _dom_virtual_html_text_area_element_destroy(dom_node_internal *node)
  301. {
  302.         _dom_html_text_area_element_destroy((struct dom_html_text_area_element *) node);
  303. }
  304.  
  305. /* The virtual copy function, see src/core/node.c for detail */
  306. dom_exception _dom_html_text_area_element_copy(dom_node_internal *old,
  307.                 dom_node_internal **copy)
  308. {
  309.         return _dom_html_element_copy(old, copy);
  310. }
  311.  
  312. /*-----------------------------------------------------------------------*/
  313. /* API functions */
  314.  
  315. #define SIMPLE_GET(attr)                                                \
  316.         dom_exception dom_html_text_area_element_get_##attr(            \
  317.                 dom_html_text_area_element *element,                    \
  318.                 dom_string **attr)                                      \
  319.         {                                                               \
  320.                 dom_exception ret;                                      \
  321.                 dom_string *_memo_##attr;                               \
  322.                                                                         \
  323.                 _memo_##attr =                                          \
  324.                         ((struct dom_html_document *)                   \
  325.                          ((struct dom_node_internal *)element)->owner)->\
  326.                         memoised[hds_##attr];                           \
  327.                                                                         \
  328.                 ret = dom_element_get_attribute(element, _memo_##attr, attr); \
  329.                                                                         \
  330.                 return ret;                                             \
  331.         }
  332. #define SIMPLE_SET(attr)                                                \
  333. dom_exception dom_html_text_area_element_set_##attr(                    \
  334.                 dom_html_text_area_element *element,                    \
  335.                 dom_string *attr)                                       \
  336.         {                                                               \
  337.                 dom_exception ret;                                      \
  338.                 dom_string *_memo_##attr;                               \
  339.                                                                         \
  340.                 _memo_##attr =                                          \
  341.                         ((struct dom_html_document *)                   \
  342.                          ((struct dom_node_internal *)element)->owner)->\
  343.                         memoised[hds_##attr];                           \
  344.                                                                         \
  345.                 ret = dom_element_set_attribute(element, _memo_##attr, attr); \
  346.                                                                         \
  347.                 return ret;                                             \
  348.         }
  349.  
  350. #define SIMPLE_GET_SET(attr) SIMPLE_GET(attr) SIMPLE_SET(attr)
  351.  
  352. SIMPLE_GET_SET(access_key);
  353. SIMPLE_GET_SET(name);
  354.  
  355. dom_exception dom_html_text_area_element_get_type(
  356.         dom_html_text_area_element *text_area, dom_string **type)
  357. {
  358.         dom_html_document *html = (dom_html_document *)
  359.                 ((dom_node_internal *)text_area)->owner;
  360.  
  361.         *type = html->memoised[hds_textarea];
  362.         dom_string_ref(*type);
  363.  
  364.         return DOM_NO_ERR;
  365. }
  366.  
  367. dom_exception dom_html_text_area_element_get_tab_index(
  368.         dom_html_text_area_element *text_area, int32_t *tab_index)
  369. {
  370.         return dom_html_element_get_int32_t_property(&text_area->base, "tabindex",
  371.                         SLEN("tabindex"), tab_index);
  372. }
  373.  
  374. dom_exception dom_html_text_area_element_set_tab_index(
  375.         dom_html_text_area_element *text_area, uint32_t tab_index)
  376. {
  377.         return dom_html_element_set_int32_t_property(&text_area->base, "tabindex",
  378.                         SLEN("tabindex"), tab_index);
  379. }
  380.  
  381. dom_exception dom_html_text_area_element_get_cols(
  382.         dom_html_text_area_element *text_area, int32_t *cols)
  383. {
  384.         return dom_html_element_get_int32_t_property(&text_area->base, "cols",
  385.                         SLEN("cols"), cols);
  386. }
  387.  
  388. dom_exception dom_html_text_area_element_set_cols(
  389.         dom_html_text_area_element *text_area, uint32_t cols)
  390. {
  391.         return dom_html_element_set_int32_t_property(&text_area->base, "cols",
  392.                         SLEN("cols"), cols);
  393. }
  394.  
  395. dom_exception dom_html_text_area_element_get_rows(
  396.         dom_html_text_area_element *text_area, int32_t *rows)
  397. {
  398.         return dom_html_element_get_int32_t_property(&text_area->base, "rows",
  399.                         SLEN("rows"), rows);
  400. }
  401.  
  402. dom_exception dom_html_text_area_element_set_rows(
  403.         dom_html_text_area_element *text_area, uint32_t rows)
  404. {
  405.         return dom_html_element_set_int32_t_property(&text_area->base, "rows",
  406.                         SLEN("rows"), rows);
  407. }
  408.  
  409. dom_exception dom_html_text_area_element_get_form(
  410.         dom_html_text_area_element *text_area, dom_html_form_element **form)
  411. {
  412.         *form = text_area->form;
  413.  
  414.         if (*form != NULL)
  415.                 dom_node_ref(*form);
  416.  
  417.         return DOM_NO_ERR;
  418. }
  419.  
  420. dom_exception _dom_html_text_area_element_set_form(
  421.         dom_html_text_area_element *text_area, dom_html_form_element *form)
  422. {
  423.         text_area->form = form;
  424.  
  425.         return DOM_NO_ERR;
  426. }
  427.  
  428. /**
  429.  * Blur this control
  430.  *
  431.  * \param ele  The form object
  432.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  433.  */
  434. dom_exception dom_html_text_area_element_blur(dom_html_text_area_element *ele)
  435. {
  436.         struct dom_html_document *doc =
  437.                 (dom_html_document *) dom_node_get_owner(ele);
  438.         bool success = false;
  439.         assert(doc != NULL);
  440.  
  441.         /** \todo Is this event (a) default (b) bubbling and (c) cancelable? */
  442.         return _dom_dispatch_generic_event((dom_document *) doc,
  443.                                            (dom_event_target *) ele,
  444.                                            doc->memoised[hds_blur], true,
  445.                                            true, &success);
  446. }
  447.  
  448. /**
  449.  * Focus this control
  450.  *
  451.  * \param ele  The form object
  452.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  453.  */
  454. dom_exception dom_html_text_area_element_focus(dom_html_text_area_element *ele)
  455. {
  456.         struct dom_html_document *doc =
  457.                 (dom_html_document *) dom_node_get_owner(ele);
  458.         bool success = false;
  459.         assert(doc != NULL);
  460.  
  461.         /** \todo Is this event (a) default (b) bubbling and (c) cancelable? */
  462.         return _dom_dispatch_generic_event((dom_document *)doc,
  463.                                            (dom_event_target *) ele,
  464.                                            doc->memoised[hds_focus], true,
  465.                                            true, &success);
  466. }
  467.  
  468. /**
  469.  * Select this control
  470.  *
  471.  * \param ele  The form object
  472.  * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
  473.  */
  474. dom_exception dom_html_text_area_element_select(dom_html_text_area_element *ele)
  475. {
  476.         struct dom_html_document *doc =
  477.                 (dom_html_document *) dom_node_get_owner(ele);
  478.         bool success = false;
  479.         assert(doc != NULL);
  480.  
  481.         /** \todo Is this event (a) default (b) bubbling and (c) cancelable? */
  482.         return _dom_dispatch_generic_event((dom_document *)doc,
  483.                                            (dom_event_target *) ele,
  484.                                            doc->memoised[hds_select], true,
  485.                                            true, &success);
  486. }
  487.