Code

Resolve ID clashes when pasting (fixes bug 165936).
[inkscape.git] / src / id-clash.cpp
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()) {
129             const gchar *id = SP_OBJECT_ID(paint->value.href->getObject());
130             IdReference idref = { REF_STYLE, elem, SPIPaint_properties[i] };
131             (*refmap)[id].push_back(idref);
132         }
133     }
135     /* check for url(#...) references in 'filter' */
136     const SPIFilter *filter = &(style->filter);
137     if (filter->href) {
138         const gchar *id = SP_OBJECT_ID(filter->href->getObject());
139         IdReference idref = { REF_STYLE, elem, "filter" };
140         (*refmap)[id].push_back(idref);
141     }
143     /* check for other url(#...) references */
144     for (unsigned i = 0; i < NUM_OTHER_URL_PROPERTIES; ++i) {
145         const char *attr = other_url_properties[i];
146         const gchar *value = repr_elem->attribute(attr);
147         if (value) {
148             gchar *uri = extract_uri(value);
149             if (uri && uri[0] == '#') {
150                 IdReference idref = { REF_URL, elem, attr };
151                 (*refmap)[uri+1].push_back(idref);
152             }
153             g_free(uri);
154         }
155     }
156     
157     /* recurse */
158     for (SPObject *child = sp_object_first_child(elem);
159          child; child = SP_OBJECT_NEXT(child) )
160     {
161         find_references(child, refmap);
162     }
165 /**
166  *  Change any IDs that clash with IDs in the current document, and make
167  *  a list of those changes that will require fixing up references.
168  */
169 static void
170 change_clashing_ids(SPDocument *imported_doc, SPDocument *current_doc,
171                     SPObject *elem, const refmap_type *refmap,
172                     id_changelist_type *id_changes)
174     const gchar *id = SP_OBJECT_ID(elem);
176     if (id && current_doc->getObjectById(id)) {
177         // Choose a new ID.
178         // To try to preserve any meaningfulness that the original ID
179         // may have had, the new ID is the old ID followed by a hyphen
180         // and one or more digits.
181         std::string old_id(id);
182         std::string new_id(old_id + '-');
183         for (;;) {
184             new_id += "0123456789"[std::rand() % 10];
185             const char *str = new_id.c_str();
186             if (current_doc->getObjectById(str) == NULL &&
187                 imported_doc->getObjectById(str) == NULL) break;
188         }
189         // Change to the new ID
190         SP_OBJECT_REPR(elem)->setAttribute("id", new_id.c_str());
191         // Make a note of this change, if we need to fix up refs to it
192         if (refmap->find(old_id) != refmap->end())
193             id_changes->push_back(id_changeitem_type(elem, old_id));
194     }
196     /* recurse */
197     for (SPObject *child = sp_object_first_child(elem);
198          child; child = SP_OBJECT_NEXT(child) )
199     {
200         change_clashing_ids(imported_doc, current_doc, child, refmap, id_changes);
201     }
204 /**
205  *  Fix up references to changed IDs.
206  */
207 static void
208 fix_up_refs(const refmap_type *refmap, const id_changelist_type &id_changes)
210     id_changelist_type::const_iterator pp;
211     const id_changelist_type::const_iterator pp_end = id_changes.end();
212     for (pp = id_changes.begin(); pp != pp_end; ++pp) {
213         SPObject *obj = pp->first;
214         refmap_type::const_iterator pos = refmap->find(pp->second);
215         std::list<IdReference>::const_iterator it;
216         const std::list<IdReference>::const_iterator it_end = pos->second.end();
217         for (it = pos->second.begin(); it != it_end; ++it) {
218             if (it->type == REF_HREF) {
219                 gchar *new_uri = g_strdup_printf("#%s", SP_OBJECT_ID(obj));
220                 SP_OBJECT_REPR(it->elem)->setAttribute(it->attr, new_uri);
221                 g_free(new_uri);
222             }
223             else if (it->type == REF_STYLE) {
224                 sp_style_set_property_url(it->elem, it->attr, obj, false);
225             }
226             else if (it->type == REF_URL) {
227                 gchar *url = g_strdup_printf("url(#%s)", SP_OBJECT_ID(obj));
228                 SP_OBJECT_REPR(it->elem)->setAttribute(it->attr, url);
229                 g_free(url);
230             }
231             else if (it->type == REF_CLIPBOARD) {
232                 SPCSSAttr *style = sp_repr_css_attr(SP_OBJECT_REPR(it->elem), "style");
233                 gchar *url = g_strdup_printf("url(#%s)", SP_OBJECT_ID(obj));
234                 sp_repr_css_set_property(style, it->attr, url);
235                 g_free(url);
236                 gchar *style_string = sp_repr_css_write_string(style);
237                 SP_OBJECT_REPR(it->elem)->setAttribute("style", style_string);
238                 g_free(style_string);
239             }
240             else g_assert(0); // shouldn't happen
241         }
242     }
245 /**
246  *  This function resolves ID clashes between the document being imported
247  *  and the current open document: IDs in the imported document that would
248  *  clash with IDs in the existing document are changed, and references to
249  *  those IDs are updated accordingly.
250  */
251 void
252 prevent_id_clashes(SPDocument *imported_doc, SPDocument *current_doc)
254     refmap_type *refmap = new refmap_type;
255     id_changelist_type id_changes;
256     SPObject *imported_root = SP_DOCUMENT_ROOT(imported_doc);
257         
258     find_references(imported_root, refmap);
259     change_clashing_ids(imported_doc, current_doc, imported_root, refmap,
260                         &id_changes);
261     fix_up_refs(refmap, id_changes);
263     delete refmap;
266 /*
267   Local Variables:
268   mode:c++
269   c-file-style:"stroustrup"
270   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
271   indent-tabs-mode:nil
272   fill-column:99
273   End:
274 */
275 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :