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