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