From 4e9b84df0f10116c0e73f1aaf80f9327d2bbbd3d Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Wed, 18 Jul 2012 17:30:16 +0100 Subject: HTMLTextAreaElement: Make it work --- src/html/Makefile | 4 +- src/html/html_document.c | 6 + src/html/html_document_strings.h | 2 + src/html/html_text_area_element.c | 494 ++++++++++++++++++++++++++++++++++++++ src/html/html_text_area_element.h | 62 +++++ src/html/html_textarea_element.c | 7 - src/html/html_textarea_element.h | 7 - 7 files changed, 566 insertions(+), 16 deletions(-) create mode 100644 src/html/html_text_area_element.c create mode 100644 src/html/html_text_area_element.h delete mode 100644 src/html/html_textarea_element.c delete mode 100644 src/html/html_textarea_element.h (limited to 'src/html') diff --git a/src/html/Makefile b/src/html/Makefile index 4586239..9fdcea4 100644 --- a/src/html/Makefile +++ b/src/html/Makefile @@ -5,10 +5,10 @@ DIR_SOURCES := \ html_link_element.c html_title_element.c html_meta_element.c \ html_base_element.c html_isindex_element.c html_style_element.c \ html_body_element.c html_form_element.c html_select_element.c \ - html_button_element.c html_input_element.c + html_button_element.c html_input_element.c html_text_area_element.c UNINMPLEMENTED_SOURCES := html_optgroup_element.c \ - html_option_element.c html_textarea_element.c \ + html_option_element.c \ html_label_element.c html_fieldset_element.c \ html_legend_element.c html_ulist_element.c html_olist_element.c \ html_dlist_element.c html_directory_element.c html_menu_element.c \ diff --git a/src/html/html_document.c b/src/html/html_document.c index 739477f..ec88ebc 100644 --- a/src/html/html_document.c +++ b/src/html/html_document.c @@ -19,6 +19,7 @@ #include "html/html_form_element.h" #include "html/html_button_element.h" #include "html/html_input_element.h" +#include "html/html_text_area_element.h" #include "core/string.h" #include "utils/namespace.h" @@ -201,6 +202,11 @@ _dom_html_document_create_element_internal(dom_html_document *html, (dom_html_input_element **) result); } + if (dom_string_caseless_isequal(tag_name, html->memoised[hds_TEXTAREA])) { + return _dom_html_text_area_element_create(html, namespace, prefix, + (dom_html_text_area_element **) result); + } + return _dom_html_element_create(html, tag_name, namespace, prefix, result); } diff --git a/src/html/html_document_strings.h b/src/html/html_document_strings.h index 97a4336..f1ff04d 100644 --- a/src/html/html_document_strings.h +++ b/src/html/html_document_strings.h @@ -77,6 +77,8 @@ HTML_DOCUMENT_STRINGS_ACTION(tab_index,tabindex) /* HTML_DOCUMENT_STRINGS_ACTION1(type) */ HTML_DOCUMENT_STRINGS_ACTION(use_map,usemap) /* HTML_DOCUMENT_STRINGS_ACTION1(value) */ +/* HTMLTextAreaElement type */ +HTML_DOCUMENT_STRINGS_ACTION1(textarea) /* Names for elements which get specialised. */ HTML_DOCUMENT_STRINGS_ACTION1(HTML) HTML_DOCUMENT_STRINGS_ACTION1(HEAD) diff --git a/src/html/html_text_area_element.c b/src/html/html_text_area_element.c new file mode 100644 index 0000000..0234076 --- /dev/null +++ b/src/html/html_text_area_element.c @@ -0,0 +1,494 @@ +/* + * This file is part of libdom. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2012 Daniel Silverstone + */ + +#include +#include + +#include + +#include "html/html_document.h" +#include "html/html_text_area_element.h" + +#include "core/node.h" +#include "core/attr.h" +#include "utils/utils.h" + +static struct dom_element_protected_vtable _protect_vtable = { + { + DOM_NODE_PROTECT_VTABLE_HTML_TEXT_AREA_ELEMENT + }, + DOM_HTML_TEXT_AREA_ELEMENT_PROTECT_VTABLE +}; + +/** + * Create a dom_html_text_area_element object + * + * \param doc The document object + * \param ele The returned element object + * \return DOM_NO_ERR on success, appropriate dom_exception on failure. + */ +dom_exception _dom_html_text_area_element_create(struct dom_html_document *doc, + dom_string *namespace, dom_string *prefix, + struct dom_html_text_area_element **ele) +{ + struct dom_node_internal *node; + + *ele = malloc(sizeof(dom_html_text_area_element)); + if (*ele == NULL) + return DOM_NO_MEM_ERR; + + /* Set up vtables */ + node = (struct dom_node_internal *) *ele; + node->base.vtable = &_dom_html_element_vtable; + node->vtable = &_protect_vtable; + + return _dom_html_text_area_element_initialise(doc, namespace, prefix, *ele); +} + +/** + * Initialise a dom_html_text_area_element object + * + * \param doc The document object + * \param ele The dom_html_text_area_element object + * \return DOM_NO_ERR on success, appropriate dom_exception on failure. + */ +dom_exception _dom_html_text_area_element_initialise(struct dom_html_document *doc, + dom_string *namespace, dom_string *prefix, + struct dom_html_text_area_element *ele) +{ + ele->form = NULL; + ele->default_value = NULL; + ele->default_value_set = false; + ele->value = NULL; + ele->value_set = false; + + return _dom_html_element_initialise(doc, &ele->base, + doc->memoised[hds_TEXTAREA], + namespace, prefix); +} + +/** + * Finalise a dom_html_text_area_element object + * + * \param ele The dom_html_text_area_element object + */ +void _dom_html_text_area_element_finalise(struct dom_html_text_area_element *ele) +{ + if (ele->default_value != NULL) { + dom_string_unref(ele->default_value); + ele->default_value = NULL; + ele->default_value_set = false; + } + + if (ele->value != NULL) { + dom_string_unref(ele->value); + ele->value = NULL; + ele->value_set = false; + } + + if (ele->form != NULL) { + dom_node_unref(ele->form); + ele->form = NULL; + } + + _dom_html_element_finalise(&ele->base); +} + +/** + * Destroy a dom_html_text_area_element object + * + * \param ele The dom_html_text_area_element object + */ +void _dom_html_text_area_element_destroy(struct dom_html_text_area_element *ele) +{ + _dom_html_text_area_element_finalise(ele); + free(ele); +} + +/*-----------------------------------------------------------------------*/ +/* Public APIs */ + +/** + * Get the disabled property + * + * \param ele The dom_html_text_area_element object + * \param disabled The returned status + * \return DOM_NO_ERR on success, appropriate dom_exception on failure. + */ +dom_exception dom_html_text_area_element_get_disabled(dom_html_text_area_element *ele, + bool *disabled) +{ + return dom_html_element_get_bool_property(&ele->base, "disabled", + SLEN("disabled"), disabled); +} + +/** + * Set the disabled property + * + * \param ele The dom_html_text_area_element object + * \param disabled The status + * \return DOM_NO_ERR on success, appropriate dom_exception on failure. + */ +dom_exception dom_html_text_area_element_set_disabled(dom_html_text_area_element *ele, + bool disabled) +{ + return dom_html_element_set_bool_property(&ele->base, "disabled", + SLEN("disabled"), disabled); +} + +/** + * Get the readOnly property + * + * \param ele The dom_html_text_area_element object + * \param disabled The returned status + * \return DOM_NO_ERR on success, appropriate dom_exception on failure. + */ +dom_exception dom_html_text_area_element_get_read_only(dom_html_text_area_element *ele, + bool *read_only) +{ + return dom_html_element_get_bool_property(&ele->base, "readonly", + SLEN("readonly"), read_only); +} + +/** + * Set the readOnly property + * + * \param ele The dom_html_text_area_element object + * \param disabled The status + * \return DOM_NO_ERR on success, appropriate dom_exception on failure. + */ +dom_exception dom_html_text_area_element_set_read_only(dom_html_text_area_element *ele, + bool read_only) +{ + return dom_html_element_set_bool_property(&ele->base, "readonly", + SLEN("readonly"), read_only); +} + +/** + * Get the defaultValue property + * + * \param ele The dom_html_text_area_element object + * \param disabled The returned status + * \return DOM_NO_ERR on success, appropriate dom_exception on failure. + */ +dom_exception dom_html_text_area_element_get_default_value( + dom_html_text_area_element *ele, dom_string **default_value) +{ + dom_exception err; + + if (ele->default_value_set == false) { + err = dom_node_get_text_content((dom_node *)ele, + &ele->default_value); + if (err == DOM_NO_ERR) { + ele->default_value_set = true; + } + } + + *default_value = ele->default_value; + + if (*default_value != NULL) + dom_string_ref(*default_value); + + return DOM_NO_ERR; +} + +/** + * Set the defaultValue property + * + * \param ele The dom_html_text_area_element object + * \param disabled The status + * \return DOM_NO_ERR on success, appropriate dom_exception on failure. + */ +dom_exception dom_html_text_area_element_set_default_value( + dom_html_text_area_element *ele, dom_string *default_value) +{ + if (ele->default_value != NULL) + dom_string_unref(ele->default_value); + + ele->default_value = default_value; + ele->default_value_set = true; + + if (ele->default_value != NULL) + dom_string_ref(ele->default_value); + + return DOM_NO_ERR; +} + +/** + * Get the value property + * + * \param ele The dom_html_text_area_element object + * \param disabled The returned status + * \return DOM_NO_ERR on success, appropriate dom_exception on failure. + */ +dom_exception dom_html_text_area_element_get_value( + dom_html_text_area_element *ele, dom_string **value) +{ + dom_exception err; + + if (ele->value_set == false) { + err = dom_node_get_text_content((dom_node *)ele, + &ele->value); + if (err == DOM_NO_ERR) { + ele->default_value_set = true; + if (ele->default_value_set == false) { + ele->default_value_set = true; + ele->default_value = ele->value; + dom_string_ref(ele->default_value); + } + } + } + + *value = ele->value; + + if (*value != NULL) + dom_string_ref(*value); + + return DOM_NO_ERR; +} + +/** + * Set the value property + * + * \param ele The dom_html_text_area_element object + * \param disabled The status + * \return DOM_NO_ERR on success, appropriate dom_exception on failure. + */ +dom_exception dom_html_text_area_element_set_value( + dom_html_text_area_element *ele, dom_string *value) +{ + dom_exception err; + + if (ele->default_value_set == false) { + err = dom_node_get_text_content((dom_node *)ele, + &ele->default_value); + if (err == DOM_NO_ERR) { + ele->default_value_set = true; + } + } + + if (ele->value != NULL) + dom_string_unref(ele->value); + + ele->value = value; + ele->value_set = true; + + if (ele->value != NULL) + dom_string_ref(ele->value); + + return DOM_NO_ERR; +} + +/*------------------------------------------------------------------------*/ +/* The protected virtual functions */ + +/* The virtual function used to parse attribute value, see src/core/element.c + * for detail */ +dom_exception _dom_html_text_area_element_parse_attribute(dom_element *ele, + dom_string *name, dom_string *value, + dom_string **parsed) +{ + UNUSED(ele); + UNUSED(name); + + dom_string_ref(value); + *parsed = value; + + return DOM_NO_ERR; +} + +/* The virtual destroy function, see src/core/node.c for detail */ +void _dom_virtual_html_text_area_element_destroy(dom_node_internal *node) +{ + _dom_html_text_area_element_destroy((struct dom_html_text_area_element *) node); +} + +/* The virtual copy function, see src/core/node.c for detail */ +dom_exception _dom_html_text_area_element_copy(dom_node_internal *old, + dom_node_internal **copy) +{ + return _dom_html_element_copy(old, copy); +} + +/*-----------------------------------------------------------------------*/ +/* API functions */ + +#define SIMPLE_GET(attr) \ + dom_exception dom_html_text_area_element_get_##attr( \ + dom_html_text_area_element *element, \ + dom_string **attr) \ + { \ + dom_exception ret; \ + dom_string *_memo_##attr; \ + \ + _memo_##attr = \ + ((struct dom_html_document *) \ + ((struct dom_node_internal *)element)->owner)->\ + memoised[hds_##attr]; \ + \ + ret = dom_element_get_attribute(element, _memo_##attr, attr); \ + \ + return ret; \ + } +#define SIMPLE_SET(attr) \ +dom_exception dom_html_text_area_element_set_##attr( \ + dom_html_text_area_element *element, \ + dom_string *attr) \ + { \ + dom_exception ret; \ + dom_string *_memo_##attr; \ + \ + _memo_##attr = \ + ((struct dom_html_document *) \ + ((struct dom_node_internal *)element)->owner)->\ + memoised[hds_##attr]; \ + \ + ret = dom_element_set_attribute(element, _memo_##attr, attr); \ + \ + return ret; \ + } + +#define SIMPLE_GET_SET(attr) SIMPLE_GET(attr) SIMPLE_SET(attr) + +SIMPLE_GET_SET(access_key); +SIMPLE_GET_SET(name); + +dom_exception dom_html_text_area_element_get_type( + dom_html_text_area_element *text_area, dom_string **type) +{ + dom_html_document *html = (dom_html_document *) + ((dom_node_internal *)text_area)->owner; + + *type = html->memoised[hds_textarea]; + dom_string_ref(*type); + + return DOM_NO_ERR; +} + +dom_exception dom_html_text_area_element_get_tab_index( + dom_html_text_area_element *text_area, unsigned long *tab_index) +{ + return dom_html_element_get_long_property(&text_area->base, "tabindex", + SLEN("tabindex"), tab_index); +} + +dom_exception dom_html_text_area_element_set_tab_index( + dom_html_text_area_element *text_area, unsigned long tab_index) +{ + return dom_html_element_set_long_property(&text_area->base, "tabindex", + SLEN("tabindex"), tab_index); +} + +dom_exception dom_html_text_area_element_get_cols( + dom_html_text_area_element *text_area, unsigned long *cols) +{ + return dom_html_element_get_long_property(&text_area->base, "cols", + SLEN("cols"), cols); +} + +dom_exception dom_html_text_area_element_set_cols( + dom_html_text_area_element *text_area, unsigned long cols) +{ + return dom_html_element_set_long_property(&text_area->base, "cols", + SLEN("cols"), cols); +} + +dom_exception dom_html_text_area_element_get_rows( + dom_html_text_area_element *text_area, unsigned long *rows) +{ + return dom_html_element_get_long_property(&text_area->base, "rows", + SLEN("rows"), rows); +} + +dom_exception dom_html_text_area_element_set_rows( + dom_html_text_area_element *text_area, unsigned long rows) +{ + return dom_html_element_set_long_property(&text_area->base, "rows", + SLEN("rows"), rows); +} + +dom_exception dom_html_text_area_element_get_form( + dom_html_text_area_element *text_area, dom_html_form_element **form) +{ + *form = text_area->form; + + if (*form != NULL) + dom_node_ref(*form); + + return DOM_NO_ERR; +} + +dom_exception _dom_html_text_area_element_set_form( + dom_html_text_area_element *text_area, dom_html_form_element *form) +{ + if (text_area->form == form) + return DOM_NO_ERR; + + if (text_area->form != NULL) + dom_node_unref(text_area->form); + + text_area->form = form; + + if (text_area->form != NULL) + dom_node_ref(text_area->form); + + return DOM_NO_ERR; +} + +/** + * Blur this control + * + * \param ele The form object + * \return DOM_NO_ERR on success, appropriate dom_exception on failure. + */ +dom_exception dom_html_text_area_element_blur(dom_html_text_area_element *ele) +{ + struct dom_document *doc = dom_node_get_owner(ele); + bool success = false; + assert(doc != NULL); + + /** \todo Is this event (a) default (b) bubbling and (c) cancelable? */ + return _dom_dispatch_generic_event(doc, (dom_event_target *) ele, + (const uint8_t *) "blur", SLEN("blur"), true, + true, &success); +} + +/** + * Focus this control + * + * \param ele The form object + * \return DOM_NO_ERR on success, appropriate dom_exception on failure. + */ +dom_exception dom_html_text_area_element_focus(dom_html_text_area_element *ele) +{ + struct dom_document *doc = dom_node_get_owner(ele); + bool success = false; + assert(doc != NULL); + + /** \todo Is this event (a) default (b) bubbling and (c) cancelable? */ + return _dom_dispatch_generic_event(doc, (dom_event_target *) ele, + (const uint8_t *) "focus", SLEN("focus"), true, + true, &success); +} + +/** + * Select this control + * + * \param ele The form object + * \return DOM_NO_ERR on success, appropriate dom_exception on failure. + */ +dom_exception dom_html_text_area_element_select(dom_html_text_area_element *ele) +{ + struct dom_document *doc = dom_node_get_owner(ele); + bool success = false; + assert(doc != NULL); + + /** \todo Is this event (a) default (b) bubbling and (c) cancelable? */ + return _dom_dispatch_generic_event(doc, (dom_event_target *) ele, + (const uint8_t *) "select", SLEN("select"), true, + true, &success); +} diff --git a/src/html/html_text_area_element.h b/src/html/html_text_area_element.h new file mode 100644 index 0000000..a115e85 --- /dev/null +++ b/src/html/html_text_area_element.h @@ -0,0 +1,62 @@ +/* + * This file is part of libdom. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2012 Daniel Silverstone + */ + +#ifndef dom_internal_html_text_area_element_h_ +#define dom_internal_html_text_area_element_h_ + +#include + +#include "html/html_element.h" + +struct dom_html_text_area_element { + struct dom_html_element base; + /**< The base class */ + struct dom_html_form_element *form; + /**< The form associated with the text_area */ + dom_string *default_value; /**< Initial value */ + bool default_value_set; /**< Whether default_value has been set */ + dom_string *value; /**< Current value */ + bool value_set; /**< Whether value has been set */ +}; + +/* Create a dom_html_text_area_element object */ +dom_exception _dom_html_text_area_element_create(struct dom_html_document *doc, + dom_string *namespace, dom_string *prefix, + struct dom_html_text_area_element **ele); + +/* Initialise a dom_html_text_area_element object */ +dom_exception _dom_html_text_area_element_initialise(struct dom_html_document *doc, + dom_string *namespace, dom_string *prefix, + struct dom_html_text_area_element *ele); + +/* Finalise a dom_html_text_area_element object */ +void _dom_html_text_area_element_finalise(struct dom_html_text_area_element *ele); + +/* Destroy a dom_html_text_area_element object */ +void _dom_html_text_area_element_destroy(struct dom_html_text_area_element *ele); + +/* The protected virtual functions */ +dom_exception _dom_html_text_area_element_parse_attribute(dom_element *ele, + dom_string *name, dom_string *value, + dom_string **parsed); +void _dom_virtual_html_text_area_element_destroy(dom_node_internal *node); +dom_exception _dom_html_text_area_element_copy(dom_node_internal *old, + dom_node_internal **copy); + +#define DOM_HTML_TEXT_AREA_ELEMENT_PROTECT_VTABLE \ + _dom_html_text_area_element_parse_attribute + +#define DOM_NODE_PROTECT_VTABLE_HTML_TEXT_AREA_ELEMENT \ + _dom_virtual_html_text_area_element_destroy, \ + _dom_html_text_area_element_copy + +/* Internal function for bindings */ + +dom_exception _dom_html_text_area_element_set_form( + dom_html_text_area_element *text_area, dom_html_form_element *form); + +#endif diff --git a/src/html/html_textarea_element.c b/src/html/html_textarea_element.c deleted file mode 100644 index 2e182d5..0000000 --- a/src/html/html_textarea_element.c +++ /dev/null @@ -1,7 +0,0 @@ -/* - * This file is part of libdom. - * Licensed under the MIT License, - * http://www.opensource.org/licenses/mit-license.php - * Copyright 2009 Bo Yang - */ - diff --git a/src/html/html_textarea_element.h b/src/html/html_textarea_element.h deleted file mode 100644 index 2e182d5..0000000 --- a/src/html/html_textarea_element.h +++ /dev/null @@ -1,7 +0,0 @@ -/* - * This file is part of libdom. - * Licensed under the MIT License, - * http://www.opensource.org/licenses/mit-license.php - * Copyright 2009 Bo Yang - */ - -- cgit v1.2.3