1 #define __ID_CLASH_C__
2 /** \file
3 * Routines for resolving ID clashes when importing or pasting.
4 *
5 * Authors:
6 * Stephen Silver <sasilver@users.sourceforge.net>
7 *
8 * Copyright (C) 2008 authors
9 *
10 * Released under GNU GPL, read the file 'COPYING' for more information
11 */
13 #include <cstdlib>
14 #include <cstring>
15 #include <list>
16 #include <map>
17 #include <string>
18 #include <utility>
20 #include "extract-uri.h"
21 #include "id-clash.h"
22 #include "sp-object.h"
23 #include "style.h"
24 #include "xml/node.h"
25 #include "xml/repr.h"
27 typedef enum { REF_HREF, REF_STYLE, REF_URL, REF_CLIPBOARD } ID_REF_TYPE;
29 struct IdReference {
30 ID_REF_TYPE type;
31 SPObject *elem;
32 const char *attr; // property or href-like attribute
33 };
35 typedef std::map<std::string, std::list<IdReference> > refmap_type;
37 typedef std::pair<SPObject*, std::string> id_changeitem_type;
38 typedef std::list<id_changeitem_type> id_changelist_type;
40 const char *href_like_attributes[] = {
41 "inkscape:href",
42 "inkscape:path-effect",
43 "inkscape:perspectiveID",
44 "inkscape:tiled-clone-of",
45 "xlink:href",
46 };
47 #define NUM_HREF_LIKE_ATTRIBUTES (sizeof(href_like_attributes) / sizeof(*href_like_attributes))
49 const SPIPaint SPStyle::* SPIPaint_members[] = {
50 &SPStyle::color,
51 &SPStyle::fill,
52 &SPStyle::stroke,
53 };
54 const char* SPIPaint_properties[] = {
55 "color",
56 "fill",
57 "stroke",
58 };
59 #define NUM_SPIPAINT_PROPERTIES (sizeof(SPIPaint_properties) / sizeof(*SPIPaint_properties))
61 const char* other_url_properties[] = {
62 "clip-path",
63 "color-profile",
64 "cursor",
65 "marker-end",
66 "marker-mid",
67 "marker-start",
68 "mask",
69 };
70 #define NUM_OTHER_URL_PROPERTIES (sizeof(other_url_properties) / sizeof(*other_url_properties))
72 const char* clipboard_properties[] = {
73 "color",
74 "fill",
75 "filter",
76 "stroke",
77 };
78 #define NUM_CLIPBOARD_PROPERTIES (sizeof(clipboard_properties) / sizeof(*clipboard_properties))
80 /**
81 * Build a table of places where IDs are referenced, for a given element.
82 * FIXME: There are some types of references not yet dealt with here
83 * (e.g., ID selectors in CSS stylesheets, and references in scripts).
84 */
85 static void
86 find_references(SPObject *elem, refmap_type *refmap)
87 {
88 Inkscape::XML::Node *repr_elem = SP_OBJECT_REPR(elem);
89 if (repr_elem->type() != Inkscape::XML::ELEMENT_NODE) return;
91 /* check for references in inkscape:clipboard elements */
92 if (!std::strcmp(repr_elem->name(), "inkscape:clipboard")) {
93 SPCSSAttr *css = sp_repr_css_attr(repr_elem, "style");
94 if (css) {
95 for (unsigned i = 0; i < NUM_CLIPBOARD_PROPERTIES; ++i) {
96 const char *attr = clipboard_properties[i];
97 const gchar *value = sp_repr_css_property(css, attr, NULL);
98 if (value) {
99 gchar *uri = extract_uri(value);
100 if (uri && uri[0] == '#') {
101 IdReference idref = { REF_CLIPBOARD, elem, attr };
102 (*refmap)[uri+1].push_back(idref);
103 }
104 g_free(uri);
105 }
106 }
107 }
108 return; // nothing more to do for inkscape:clipboard elements
109 }
111 /* check for xlink:href="#..." and similar */
112 for (unsigned i = 0; i < NUM_HREF_LIKE_ATTRIBUTES; ++i) {
113 const char *attr = href_like_attributes[i];
114 const gchar *val = repr_elem->attribute(attr);
115 if (val && val[0] == '#') {
116 std::string id(val+1);
117 IdReference idref = { REF_HREF, elem, attr };
118 (*refmap)[id].push_back(idref);
119 }
120 }
122 SPStyle *style = SP_OBJECT_STYLE(elem);
124 /* check for url(#...) references in 'fill' or 'stroke' */
125 for (unsigned i = 0; i < NUM_SPIPAINT_PROPERTIES; ++i) {
126 const SPIPaint SPStyle::*prop = SPIPaint_members[i];
127 const SPIPaint *paint = &(style->*prop);
128 if (paint->isPaintserver() && paint->value.href) {
129 const SPObject *obj = paint->value.href->getObject();
130 if (obj) {
131 const gchar *id = SP_OBJECT_ID(obj);
132 IdReference idref = { REF_STYLE, elem, SPIPaint_properties[i] };
133 (*refmap)[id].push_back(idref);
134 }
135 }
136 }
138 /* check for url(#...) references in 'filter' */
139 const SPIFilter *filter = &(style->filter);
140 if (filter->href) {
141 const SPObject *obj = filter->href->getObject();
142 if (obj) {
143 const gchar *id = SP_OBJECT_ID(obj);
144 IdReference idref = { REF_STYLE, elem, "filter" };
145 (*refmap)[id].push_back(idref);
146 }
147 }
149 /* check for other url(#...) references */
150 for (unsigned i = 0; i < NUM_OTHER_URL_PROPERTIES; ++i) {
151 const char *attr = other_url_properties[i];
152 const gchar *value = repr_elem->attribute(attr);
153 if (value) {
154 gchar *uri = extract_uri(value);
155 if (uri && uri[0] == '#') {
156 IdReference idref = { REF_URL, elem, attr };
157 (*refmap)[uri+1].push_back(idref);
158 }
159 g_free(uri);
160 }
161 }
163 /* recurse */
164 for (SPObject *child = sp_object_first_child(elem);
165 child; child = SP_OBJECT_NEXT(child) )
166 {
167 find_references(child, refmap);
168 }
169 }
171 /**
172 * Change any IDs that clash with IDs in the current document, and make
173 * a list of those changes that will require fixing up references.
174 */
175 static void
176 change_clashing_ids(SPDocument *imported_doc, SPDocument *current_doc,
177 SPObject *elem, const refmap_type *refmap,
178 id_changelist_type *id_changes)
179 {
180 const gchar *id = SP_OBJECT_ID(elem);
182 if (id && current_doc->getObjectById(id)) {
183 // Choose a new ID.
184 // To try to preserve any meaningfulness that the original ID
185 // may have had, the new ID is the old ID followed by a hyphen
186 // and one or more digits.
187 std::string old_id(id);
188 std::string new_id(old_id + '-');
189 for (;;) {
190 new_id += "0123456789"[std::rand() % 10];
191 const char *str = new_id.c_str();
192 if (current_doc->getObjectById(str) == NULL &&
193 imported_doc->getObjectById(str) == NULL) break;
194 }
195 // Change to the new ID
196 SP_OBJECT_REPR(elem)->setAttribute("id", new_id.c_str());
197 // Make a note of this change, if we need to fix up refs to it
198 if (refmap->find(old_id) != refmap->end())
199 id_changes->push_back(id_changeitem_type(elem, old_id));
200 }
202 /* recurse */
203 for (SPObject *child = sp_object_first_child(elem);
204 child; child = SP_OBJECT_NEXT(child) )
205 {
206 change_clashing_ids(imported_doc, current_doc, child, refmap, id_changes);
207 }
208 }
210 /**
211 * Fix up references to changed IDs.
212 */
213 static void
214 fix_up_refs(const refmap_type *refmap, const id_changelist_type &id_changes)
215 {
216 id_changelist_type::const_iterator pp;
217 const id_changelist_type::const_iterator pp_end = id_changes.end();
218 for (pp = id_changes.begin(); pp != pp_end; ++pp) {
219 SPObject *obj = pp->first;
220 refmap_type::const_iterator pos = refmap->find(pp->second);
221 std::list<IdReference>::const_iterator it;
222 const std::list<IdReference>::const_iterator it_end = pos->second.end();
223 for (it = pos->second.begin(); it != it_end; ++it) {
224 if (it->type == REF_HREF) {
225 gchar *new_uri = g_strdup_printf("#%s", SP_OBJECT_ID(obj));
226 SP_OBJECT_REPR(it->elem)->setAttribute(it->attr, new_uri);
227 g_free(new_uri);
228 }
229 else if (it->type == REF_STYLE) {
230 sp_style_set_property_url(it->elem, it->attr, obj, false);
231 }
232 else if (it->type == REF_URL) {
233 gchar *url = g_strdup_printf("url(#%s)", SP_OBJECT_ID(obj));
234 SP_OBJECT_REPR(it->elem)->setAttribute(it->attr, url);
235 g_free(url);
236 }
237 else if (it->type == REF_CLIPBOARD) {
238 SPCSSAttr *style = sp_repr_css_attr(SP_OBJECT_REPR(it->elem), "style");
239 gchar *url = g_strdup_printf("url(#%s)", SP_OBJECT_ID(obj));
240 sp_repr_css_set_property(style, it->attr, url);
241 g_free(url);
242 gchar *style_string = sp_repr_css_write_string(style);
243 SP_OBJECT_REPR(it->elem)->setAttribute("style", style_string);
244 g_free(style_string);
245 }
246 else g_assert(0); // shouldn't happen
247 }
248 }
249 }
251 /**
252 * This function resolves ID clashes between the document being imported
253 * and the current open document: IDs in the imported document that would
254 * clash with IDs in the existing document are changed, and references to
255 * those IDs are updated accordingly.
256 */
257 void
258 prevent_id_clashes(SPDocument *imported_doc, SPDocument *current_doc)
259 {
260 refmap_type *refmap = new refmap_type;
261 id_changelist_type id_changes;
262 SPObject *imported_root = SP_DOCUMENT_ROOT(imported_doc);
264 find_references(imported_root, refmap);
265 change_clashing_ids(imported_doc, current_doc, imported_root, refmap,
266 &id_changes);
267 fix_up_refs(refmap, id_changes);
269 delete refmap;
270 }
272 /*
273 Local Variables:
274 mode:c++
275 c-file-style:"stroustrup"
276 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
277 indent-tabs-mode:nil
278 fill-column:99
279 End:
280 */
281 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :