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 2007 John-Mark Bell <jmb@netsurf-browser.org>
  6.  * Copyright 2009 Bo Yang <struggleyb.nku@gmail.com>
  7.  */
  8.  
  9. #include <stdlib.h>
  10.  
  11. #include "core/cdatasection.h"
  12. #include "core/document.h"
  13. #include "core/text.h"
  14. #include "utils/utils.h"
  15.  
  16. /**
  17.  * A DOM CDATA section
  18.  */
  19. struct dom_cdata_section {
  20.         dom_text base;          /**< Base node */
  21. };
  22.  
  23. static struct dom_node_protect_vtable cdata_section_protect_vtable = {
  24.         DOM_CDATA_SECTION_PROTECT_VTABLE
  25. };
  26.  
  27. /**
  28.  * Create a CDATA section
  29.  *
  30.  * \param doc     The owning document
  31.  * \param name    The name of the node to create
  32.  * \param value   The text content of the node
  33.  * \param result  Pointer to location to receive created node
  34.  * \return DOM_NO_ERR                on success,
  35.  *         DOM_NO_MEM_ERR            on memory exhaustion.
  36.  *
  37.  * ::doc, ::name and ::value will have their reference counts increased.
  38.  *
  39.  * The returned node will already be referenced.
  40.  */
  41. dom_exception _dom_cdata_section_create(dom_document *doc,
  42.                 dom_string *name, dom_string *value,
  43.                 dom_cdata_section **result)
  44. {
  45.         dom_cdata_section *c;
  46.         dom_exception err;
  47.  
  48.         /* Allocate the comment node */
  49.         c = malloc(sizeof(dom_cdata_section));
  50.         if (c == NULL)
  51.                 return DOM_NO_MEM_ERR;
  52.        
  53.         /* Set up vtable */
  54.         ((dom_node_internal *) c)->base.vtable = &text_vtable;
  55.         ((dom_node_internal *) c)->vtable = &cdata_section_protect_vtable;
  56.  
  57.         /* And initialise the node */
  58.         err = _dom_cdata_section_initialise(&c->base, doc,
  59.                         DOM_CDATA_SECTION_NODE, name, value);
  60.         if (err != DOM_NO_ERR) {
  61.                 free(c);
  62.                 return err;
  63.         }
  64.  
  65.         *result = c;
  66.  
  67.         return DOM_NO_ERR;
  68. }
  69.  
  70. /**
  71.  * Destroy a CDATA section
  72.  *
  73.  * \param cdata  The cdata section to destroy
  74.  *
  75.  * The contents of ::cdata will be destroyed and ::cdata will be freed.
  76.  */
  77. void _dom_cdata_section_destroy(dom_cdata_section *cdata)
  78. {
  79.         /* Clean up base node contents */
  80.         _dom_cdata_section_finalise(&cdata->base);
  81.  
  82.         /* Destroy the node */
  83.         free(cdata);
  84. }
  85.  
  86. /*--------------------------------------------------------------------------*/
  87.  
  88. /* The protected virtual functions */
  89.  
  90. /* The virtual destroy function of this class */
  91. void __dom_cdata_section_destroy(dom_node_internal *node)
  92. {
  93.         _dom_cdata_section_destroy((dom_cdata_section *) node);
  94. }
  95.  
  96. /* The copy constructor of this class */
  97. dom_exception _dom_cdata_section_copy(dom_node_internal *old,
  98.                 dom_node_internal **copy)
  99. {
  100.         dom_cdata_section *new_cdata;
  101.         dom_exception err;
  102.  
  103.         new_cdata = malloc(sizeof(dom_cdata_section));
  104.         if (new_cdata == NULL)
  105.                 return DOM_NO_MEM_ERR;
  106.  
  107.         err = dom_text_copy_internal(old, new_cdata);
  108.         if (err != DOM_NO_ERR) {
  109.                 free(new_cdata);
  110.                 return err;
  111.         }
  112.  
  113.         *copy = (dom_node_internal *) new_cdata;
  114.  
  115.         return DOM_NO_ERR;     
  116. }
  117.  
  118.