Code

compile depending on WITH_LIBWPG
[inkscape.git] / src / uri-references.cpp
index 6b2a8401fdeb9d78799af866b5423fbad5e6ed5e..7fc1f483801e9713edd791366fd9d75d4f37ab93 100644 (file)
 #include "sp-object.h"
 #include "uri.h"
 #include "uri-references.h"
+#include "extract-uri.h"
 
 #include <sigc++/functors/mem_fun.h>
 
-static gchar *uri_to_id(SPDocument *document, const gchar *uri);
-
 namespace Inkscape {
 
 URIReference::URIReference(SPObject *owner)
-: _owner(owner), _obj(NULL), _uri(NULL)
+       : _owner(owner), _owner_document(NULL), _obj(NULL), _uri(NULL)
 {
        g_assert(_owner != NULL);
        /* FIXME !!! attach to owner's destroy signal to clean up in case */
 }
 
+URIReference::URIReference(SPDocument *owner_document)
+       : _owner(NULL), _owner_document(owner_document), _obj(NULL), _uri(NULL)
+{
+       g_assert(_owner_document != NULL);
+}
+
 URIReference::~URIReference() {
        detach();
 }
 
 void URIReference::attach(const URI &uri) throw(BadURIException)
 {
-       SPDocument *document = SP_OBJECT_DOCUMENT(_owner);
+       SPDocument *document;
+  if (_owner) {
+    document = SP_OBJECT_DOCUMENT(_owner);
+       } else if (_owner_document) {
+    document = _owner_document;
+       } else {
+    g_assert_not_reached();
+       }
        gchar const *fragment = uri.getFragment();
        if ( !uri.isRelative() || uri.getQuery() || !fragment ) {
                throw UnsupportedURIException();
@@ -120,48 +132,29 @@ void URIReference::_release(SPObject *obj) {
 
 } /* namespace Inkscape */
 
-static gchar *
-uri_to_id(SPDocument *document, const gchar *uri)
+SPObject* sp_css_uri_reference_resolve( SPDocument *document, const gchar *uri )
 {
-       const gchar *e;
-       gchar *id;
-       gint len;
-
-       g_return_val_if_fail (document != NULL, NULL);
+    SPObject* ref = 0;
 
-       if (!uri) return NULL;
-       /* fixme: xpointer, everything */
-       if (strncmp (uri, "url(#", 5)) return NULL;
+    if ( document && uri && ( strncmp(uri, "url(", 4) == 0 )) {
+        gchar *trimmed = extract_uri( uri );
+        if ( trimmed ) {
+            ref = sp_uri_reference_resolve( document, trimmed );
+            g_free( trimmed );
+        }
+    }
 
-       e = uri + 5;
-       while (*e) {
-               if (*e == ')') break;
-               if (!isalnum (*e) && (*e != '_') && (*e != '-') && (*e != ':') && (*e != '.')) return NULL;
-               e += 1;
-               if (!*e) return NULL;
-       }
-
-       len = e - uri - 5;
-       if (len < 1) return NULL;
-
-       id = (gchar*)g_new(gchar, len + 1);
-       memcpy (id, uri + 5, len);
-       id[len] = '\0';
-
-       return id;
+    return ref;
 }
 
 SPObject *
 sp_uri_reference_resolve (SPDocument *document, const gchar *uri)
 {
-       gchar *id;
+    SPObject* ref = 0;
 
-       id = uri_to_id(document, uri);
-       if (!id) return NULL;
+    if ( uri && (*uri == '#') ) {
+        ref = document->getObjectById( uri + 1 );
+    }
 
-       SPObject *ref;
-       ref = document->getObjectById(id);
-       g_free(id);
-       return ref;
+    return ref;
 }
-