| 1 |
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ |
| 2 |
/* gdome-xml-text.c |
| 3 |
* |
| 4 |
* Copyright (C) 1999 Raph Levien <[email protected]> |
| 5 |
* Copyright (C) 2000 Mathieu Lacage <[email protected]> |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2.1 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public |
| 18 |
* License along with this library; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
/* -------------------------------------------------------------- */ |
| 23 |
/* --------------------------- Text --------------------------- */ |
| 24 |
/* -------------------------------------------------------------- */ |
| 25 |
|
| 26 |
|
| 27 |
#include <string.h> |
| 28 |
#include "gdome.h" |
| 29 |
#include "gdome-events.h" |
| 30 |
#include <libxml/tree.h> |
| 31 |
#include <libxml/parser.h> |
| 32 |
#include <libxml/xmlmemory.h> |
| 33 |
#include "gdome-xml-util.h" |
| 34 |
#include "gdome-xml-xmlutil.h" |
| 35 |
#include "gdome-xml-str.h" |
| 36 |
#include "gdome-xml-node.h" |
| 37 |
#include "gdome-xml-cdata.h" |
| 38 |
#include "gdome-xml-document.h" |
| 39 |
#include "gdome-evt-event.h" |
| 40 |
#include "gdome-evt-mevent.h" |
| 41 |
#include "gdome-xml-text.h" |
| 42 |
|
| 43 |
|
| 44 |
const GdomeTextVtab gdome_xml_t_vtab = |
| 45 |
{ |
| 46 |
{ |
| 47 |
{ |
| 48 |
gdome_xml_n_ref, |
| 49 |
gdome_xml_n_unref, |
| 50 |
gdome_xml_t_query_interface, |
| 51 |
gdome_xml_n_nodeName, |
| 52 |
gdome_xml_n_nodeValue, |
| 53 |
gdome_xml_n_set_nodeValue, |
| 54 |
gdome_xml_n_nodeType, |
| 55 |
gdome_xml_n_parentNode, |
| 56 |
gdome_xml_n_childNodes, |
| 57 |
gdome_xml_n_firstChild, |
| 58 |
gdome_xml_n_lastChild, |
| 59 |
gdome_xml_n_previousSibling, |
| 60 |
gdome_xml_n_nextSibling, |
| 61 |
gdome_xml_n_attributes, |
| 62 |
gdome_xml_n_ownerDocument, |
| 63 |
gdome_xml_n_insertBefore, |
| 64 |
gdome_xml_n_replaceChild, |
| 65 |
gdome_xml_n_removeChild, |
| 66 |
gdome_xml_n_appendChild, |
| 67 |
gdome_xml_n_hasChildNodes, |
| 68 |
gdome_xml_n_cloneNode, |
| 69 |
gdome_xml_n_normalize, |
| 70 |
gdome_xml_n_isSupported, |
| 71 |
gdome_xml_n_namespaceURI, |
| 72 |
gdome_xml_n_prefix, |
| 73 |
gdome_xml_n_set_prefix, |
| 74 |
gdome_xml_n_localName, |
| 75 |
gdome_xml_n_hasAttributes, |
| 76 |
gdome_xml_n_addEventListener, |
| 77 |
gdome_xml_n_removeEventListener, |
| 78 |
gdome_xml_n_dispatchEvent, |
| 79 |
gdome_xml_n_subTreeDispatchEvent |
| 80 |
}, |
| 81 |
gdome_xml_cd_data, |
| 82 |
gdome_xml_cd_set_data, |
| 83 |
gdome_xml_cd_length, |
| 84 |
gdome_xml_cd_substringData, |
| 85 |
gdome_xml_cd_appendData, |
| 86 |
gdome_xml_cd_insertData, |
| 87 |
gdome_xml_cd_deleteData, |
| 88 |
gdome_xml_cd_replaceData |
| 89 |
}, |
| 90 |
gdome_xml_t_splitText |
| 91 |
}; |
| 92 |
|
| 93 |
/** |
| 94 |
* gdome_xml_t_query_interface: |
| 95 |
* @self: Node Object ref |
| 96 |
* @interface: interface needed |
| 97 |
* @exc: Exception Object ref |
| 98 |
* |
| 99 |
* Returns: a reference to this object that implements the @interface needed, |
| 100 |
* or %NULL if the @interface is not supported by this Object. |
| 101 |
*/ |
| 102 |
gpointer |
| 103 |
gdome_xml_t_query_interface (GdomeNode *self, const char *interface, GdomeException *exc) |
| 104 |
{ |
| 105 |
Gdome_xml_Text *priv = (Gdome_xml_Text *)self; |
| 106 |
|
| 107 |
g_return_val_if_fail (priv != NULL, NULL); |
| 108 |
g_return_val_if_fail (GDOME_XML_IS_T (priv), NULL); |
| 109 |
g_return_val_if_fail (interface != NULL, NULL); |
| 110 |
g_return_val_if_fail (exc != NULL, NULL); |
| 111 |
|
| 112 |
if (!strcmp (interface, "Node") || |
| 113 |
!strcmp (interface, "CharacterData") || |
| 114 |
!strcmp (interface, "Text") || |
| 115 |
!strcmp (interface, "EventTarget")) { |
| 116 |
priv->refcnt++; |
| 117 |
return self; |
| 118 |
} |
| 119 |
else |
| 120 |
return NULL; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* gdome_xml_t_splitText: |
| 125 |
* @self: The Text Object ref |
| 126 |
* @offset: The 16-bit unit offset at which to split, starting from 0 |
| 127 |
* @exc: The GdomeException Object ref |
| 128 |
* |
| 129 |
* Breaks this node into two nodes at the specified @offset, keeping both in |
| 130 |
* the tree as siblings. After being split, this node will contain all the |
| 131 |
* content up to the @offset point. A new node of the same type, which contains |
| 132 |
* all the content at and after the @offset point, is returned. If the original |
| 133 |
* node had a parent node, the new node is inserted as the next sibling of the |
| 134 |
* original node. When the @offset is equal to the length of this node, the new |
| 135 |
* node has no data. |
| 136 |
* |
| 137 |
* %GDOME_INDEX_SIZE_ERR: Raised if the specified @offset is greater than the |
| 138 |
* number of 16-bit units in data. |
| 139 |
* %GDOME_NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. |
| 140 |
* Returns: the new node, of the same type as this node. |
| 141 |
*/ |
| 142 |
GdomeText * |
| 143 |
gdome_xml_t_splitText (GdomeText *self, gulong offset, GdomeException *exc) |
| 144 |
{ |
| 145 |
Gdome_xml_Text *priv = (Gdome_xml_Text *)self; |
| 146 |
Gdome_xml_Text *new_priv = NULL; |
| 147 |
GdomeText *ret = NULL; |
| 148 |
GdomeDocument *owner; |
| 149 |
GdomeDOMString *new_str = NULL; |
| 150 |
gulong len; |
| 151 |
xmlNode *parent; |
| 152 |
|
| 153 |
g_return_val_if_fail (priv != NULL, NULL); |
| 154 |
g_return_val_if_fail (GDOME_XML_IS_T (priv), NULL); |
| 155 |
g_return_val_if_fail (exc != NULL, NULL); |
| 156 |
|
| 157 |
if (GDOME_ISREADONLY (priv)) { |
| 158 |
*exc = GDOME_NO_MODIFICATION_ALLOWED_ERR; |
| 159 |
return NULL; |
| 160 |
} |
| 161 |
|
| 162 |
len = gdome_xml_cd_length ((GdomeCharacterData *)self, exc); |
| 163 |
if (len < offset) { |
| 164 |
*exc = GDOME_INDEX_SIZE_ERR; |
| 165 |
return NULL; |
| 166 |
} |
| 167 |
|
| 168 |
/* create the new text node */ |
| 169 |
owner = gdome_xml_n_ownerDocument ((GdomeNode *)self, exc); |
| 170 |
if (len == offset) |
| 171 |
new_str = gdome_xml_str_mkref (""); |
| 172 |
else |
| 173 |
new_str = gdome_xml_cd_substringData ((GdomeCharacterData *)self, offset, len - offset, exc); |
| 174 |
ret = gdome_xml_doc_createTextNode (owner, new_str, exc); |
| 175 |
gdome_str_unref (new_str); |
| 176 |
gdome_doc_unref (owner, exc); |
| 177 |
new_priv = (Gdome_xml_Text *)ret; |
| 178 |
|
| 179 |
/* delete splitted data */ |
| 180 |
gdome_xml_cd_deleteData ((GdomeCharacterData *)self, offset, len - offset, exc); |
| 181 |
|
| 182 |
/* insert the new node in the right position */ |
| 183 |
parent = gdome_xmlGetParent (priv->n); |
| 184 |
if (parent != NULL) { |
| 185 |
GdomeMutationEvent *mev; |
| 186 |
GdomeNode *gparent; |
| 187 |
|
| 188 |
gdome_xmlSetParent (new_priv->n, parent); |
| 189 |
if (gdome_xmlGetLastChild (parent) == priv->n) |
| 190 |
gdome_xmlSetLastChild (parent, new_priv->n); |
| 191 |
gparent = gdome_xml_n_mkref (parent); |
| 192 |
|
| 193 |
if (gdome_xml_n_eventEnabledByCode ((GdomeNode *) self, DOM_NODE_INSERTED_EVENT_TYPE)) { |
| 194 |
/* Fire DOMNodeInserted */ |
| 195 |
mev = gdome_evt_mevnt_mkref (); |
| 196 |
gdome_xml_n_ref ((GdomeNode *)ret, exc); |
| 197 |
gdome_evt_mevnt_initMutationEventByCode (mev, DOM_NODE_INSERTED_EVENT_TYPE, |
| 198 |
TRUE, FALSE, gparent, |
| 199 |
NULL, NULL, NULL, 0, exc); |
| 200 |
gdome_xml_n_dispatchEvent ((GdomeNode *)ret, (GdomeEvent *)mev, exc); |
| 201 |
gdome_xml_n_unref ((GdomeNode *)ret, exc); |
| 202 |
gdome_evt_mevnt_unref ((GdomeEvent *)mev, exc); |
| 203 |
} |
| 204 |
|
| 205 |
if (gdome_xml_n_eventEnabledByCode ((GdomeNode *) self, DOM_SUBTREE_MODIFIED_EVENT_TYPE)) { |
| 206 |
/* Fire DOMSubtreeModified */ |
| 207 |
mev = gdome_evt_mevnt_mkref (); |
| 208 |
gdome_evt_mevnt_initMutationEventByCode (mev, DOM_SUBTREE_MODIFIED_EVENT_TYPE, |
| 209 |
TRUE, FALSE, NULL, |
| 210 |
NULL, NULL, NULL, 0, exc); |
| 211 |
gdome_xml_n_dispatchEvent (gparent, (GdomeEvent *)mev, exc); |
| 212 |
gdome_evt_mevnt_unref ((GdomeEvent *)mev, exc); |
| 213 |
gdome_xml_n_unref (gparent, exc); |
| 214 |
} |
| 215 |
} |
| 216 |
gdome_xmlSetNext (new_priv->n, gdome_xmlGetNext (priv->n)); |
| 217 |
gdome_xmlSetPrev (new_priv->n, priv->n); |
| 218 |
gdome_xmlSetPrev (gdome_xmlGetNext (priv->n), new_priv->n); |
| 219 |
gdome_xmlSetNext (priv->n, new_priv->n); |
| 220 |
|
| 221 |
return ret; |
| 222 |
} |