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/characterdata.h"
  12. #include "core/comment.h"
  13. #include "core/document.h"
  14.  
  15. #include "utils/utils.h"
  16.  
  17. /**
  18.  * A DOM Comment node
  19.  */
  20. struct dom_comment {
  21.         dom_characterdata base; /**< Base node */
  22. };
  23.  
  24. static struct dom_node_protect_vtable comment_protect_vtable = {
  25.         DOM_COMMENT_PROTECT_VTABLE
  26. };
  27.  
  28. /**
  29.  * Create a comment node
  30.  *
  31.  * \param doc     The owning document
  32.  * \param name    The name of the node to create
  33.  * \param value   The text content of the node
  34.  * \param result  Pointer to location to receive created node
  35.  * \return DOM_NO_ERR                on success,
  36.  *         DOM_NO_MEM_ERR            on memory exhaustion.
  37.  *
  38.  * ::doc, ::name and ::value will have their reference counts increased.
  39.  *
  40.  * The returned node will already be referenced.
  41.  */
  42. dom_exception _dom_comment_create(dom_document *doc,
  43.                 dom_string *name, dom_string *value,
  44.                 dom_comment **result)
  45. {
  46.         dom_comment *c;
  47.         dom_exception err;
  48.  
  49.         /* Allocate the comment node */
  50.         c = malloc(sizeof(dom_comment));
  51.         if (c == NULL)
  52.                 return DOM_NO_MEM_ERR;
  53.  
  54.         /* Set the virtual table */
  55.         ((dom_node_internal *) c)->base.vtable = &characterdata_vtable;
  56.         ((dom_node_internal *) c)->vtable = &comment_protect_vtable;
  57.  
  58.         /* And initialise the node */
  59.         err = _dom_characterdata_initialise(&c->base, doc, DOM_COMMENT_NODE,
  60.                         name, value);
  61.         if (err != DOM_NO_ERR) {
  62.                 free(c);
  63.                 return err;
  64.         }
  65.  
  66.         *result = c;
  67.  
  68.         return DOM_NO_ERR;
  69. }
  70.  
  71. /**
  72.  * Destroy a comment node
  73.  *
  74.  * \param comment  The node to destroy
  75.  *
  76.  * The contents of ::comment will be destroyed and ::comment will be freed
  77.  */
  78. void _dom_comment_destroy(dom_comment *comment)
  79. {
  80.         /* Finalise base class contents */
  81.         _dom_characterdata_finalise(&comment->base);
  82.  
  83.         /* Free node */
  84.         free(comment);
  85. }
  86.  
  87.  
  88. /*-----------------------------------------------------------------------*/
  89. /* The protected virtual functions */
  90.  
  91. /* The virtual destroy function */
  92. void __dom_comment_destroy(dom_node_internal *node)
  93. {
  94.         _dom_comment_destroy((dom_comment *) node);
  95. }
  96.  
  97. /* The copy constructor of this class */
  98. dom_exception _dom_comment_copy(dom_node_internal *old,
  99.                 dom_node_internal **copy)
  100. {
  101.         dom_comment *new_comment;
  102.         dom_exception err;
  103.  
  104.         new_comment = malloc(sizeof(dom_comment));
  105.         if (new_comment == NULL)
  106.                 return DOM_NO_MEM_ERR;
  107.  
  108.         err = dom_characterdata_copy_internal(old, new_comment);
  109.         if (err != DOM_NO_ERR) {
  110.                 free(new_comment);
  111.                 return err;
  112.         }
  113.  
  114.         *copy = (dom_node_internal *) new_comment;
  115.  
  116.         return DOM_NO_ERR;
  117. }
  118.  
  119.