rebase-hrefs.cpp 13.1 KB
Newer Older
1 2
#include "xml/rebase-hrefs.h"
#include "dir-util.h"
3
#include "../document.h"  /* Unfortunately there's a separate xml/document.h. */
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include "io/sys.h"
#include "sp-object.h"
#include "streq.h"
#include "util/share.h"
#include "xml/attribute-record.h"
#include "xml/node.h"
#include <glib/gmem.h>
#include <glib/gurifuncs.h>
#include <glib/gutils.h>
using Inkscape::XML::AttributeRecord;


/**
 * \pre href.
 */
static bool
href_needs_rebasing(char const *const href)
{
    g_return_val_if_fail(href, false);

24
    if (!*href || *href == '#') {
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
        return false;
        /* False (no change) is the right behaviour even when the base URI differs from the
         * document URI: RFC 3986 defines empty string relative URL as referring to the containing
         * document, rather than referring to the base URI. */
    }

    /* Don't change data or http hrefs. */
    {
        char *const scheme = g_uri_parse_scheme(href);
        if (scheme) {
            /* Assume it shouldn't be changed.  This is probably wrong if the scheme is `file'
             * (or if the scheme of the new base is non-file, though I believe that never
             * happens at the time of writing), but that's rare, and we won't try too hard to
             * handle this now: wait until after the freeze, then add liburiparser (or similar)
             * as a dependency and do it properly.  For now we'll just try to be simple (while
             * at least still correctly handling data hrefs). */
            free(scheme);
            return false;
        }
    }

    /* If absolute then keep it as is.
     *
     * Even in the following borderline cases:
     *
     *   - We keep it absolute even if it is in new_base (directly or indirectly).
     *
     *   - We assume that if xlink:href is absolute then we honour it in preference to
     *     sodipodi:absref even if sodipodi:absref points to an existing file while xlink:href
     *     doesn't.  This is because we aren't aware of any bugs in xlink:href handling when
     *     it's absolute, so we assume that it's the best value to use even in this case.)
     */
    if (g_path_is_absolute(href)) {
        /* No strong preference on what we do for sodipodi:absref.  Once we're
         * confident of our handling of xlink:href and xlink:base, we should clear it.
         * Though for the moment we do the simple thing: neither clear nor set it. */
        return false;
    }

    return true;
}

static gchar *
calc_abs_href(gchar const *const abs_base_dir, gchar const *const href,
              gchar const *const sp_absref)
{
    gchar *ret = g_build_filename(abs_base_dir, href, NULL);

    if ( sp_absref
         && !Inkscape::IO::file_test(ret,       G_FILE_TEST_EXISTS)
         &&  Inkscape::IO::file_test(sp_absref, G_FILE_TEST_EXISTS) )
    {
        /* sodipodi:absref points to an existing file while xlink:href doesn't.
         * This could mean that xlink:href is wrong, or it could mean that the user
         * intends to supply the missing file later.
         *
         * Given that we aren't sure what the right behaviour is, and given that a
         * wrong xlink:href value may mean a bug (as has occurred in the past), we
         * write a message to stderr. */
        g_warning("xlink:href points to non-existent file, so using sodipodi:absref instead");

        /* Currently, we choose to use sodipodi:absref in this situation (because we
         * aren't yet confident in xlink:href interpretation); though note that
         * honouring a foreign attribute in preference to standard SVG xlink:href and
         * xlink:base means that we're not a conformant SVG user agent, so eventually
         * we hope to have enough confidence in our xlink:href and xlink:base handling
         * to be able to disregard sodipodi:absref.
         *
         * effic: Once we no longer consult sodipodi:absref, we can do
         * `if (base unchanged) { return; }' at the start of rebase_hrefs.
         */
        g_free(ret);
        ret = g_strdup(sp_absref);
    }

    return ret;
}

/**
 * Change relative xlink:href attributes to be relative to \a new_abs_base instead of old_abs_base.
 *
 * Note that old_abs_base and new_abs_base must each be non-NULL, absolute directory paths.
 */
Inkscape::Util::List<AttributeRecord const>
Inkscape::XML::rebase_href_attrs(gchar const *const old_abs_base,
                                 gchar const *const new_abs_base,
                                 Inkscape::Util::List<AttributeRecord const> attributes)
{
    using Inkscape::Util::List;
    using Inkscape::Util::cons;
    using Inkscape::Util::ptr_shared;
    using Inkscape::Util::share_string;

    if (old_abs_base == new_abs_base) {
        return attributes;
    }

    GQuark const href_key = g_quark_from_static_string("xlink:href");
    GQuark const absref_key = g_quark_from_static_string("sodipodi:absref");

    /* First search attributes for xlink:href and sodipodi:absref, putting the rest in ret.
     *
     * However, if we find that xlink:href doesn't need rebasing, then return immediately
     * with no change to attributes. */
    ptr_shared<char> old_href;
    ptr_shared<char> sp_absref;
    List<AttributeRecord const> ret;
    {
        for (List<AttributeRecord const> ai(attributes); ai; ++ai) {
            if (ai->key == href_key) {
                old_href = ai->value;
                if (!href_needs_rebasing(old_href)) {
                    return attributes;
                }
            } else if (ai->key == absref_key) {
                sp_absref = ai->value;
            } else {
                ret = cons(AttributeRecord(ai->key, ai->value), ret);
            }
        }
    }

    if (!old_href) {
        return attributes;
        /* We could instead return ret in this case, i.e. ensure that sodipodi:absref is cleared if
         * no xlink:href attribute.  However, retaining it might be more cautious.
         *
         * (For the usual case of not present, attributes and ret will be the same except
         * reversed.) */
    }

    gchar *const abs_href(calc_abs_href(old_abs_base, old_href, sp_absref));
    gchar const *const new_href = sp_relative_path_from_path(abs_href, new_abs_base);
    ret = cons(AttributeRecord(href_key, share_string(new_href)), ret);
    if (sp_absref) {
        /* We assume that if there wasn't previously a sodipodi:absref attribute
         * then we shouldn't create one. */
        ret = cons(AttributeRecord(absref_key, ( streq(abs_href, sp_absref)
                                                 ? sp_absref
                                                 : share_string(abs_href) )),
                   ret);
    }
    g_free(abs_href);
    return ret;
}

gchar *
Inkscape::XML::calc_abs_doc_base(gchar const *const doc_base)
{
    /* Note that we don't currently try to handle the case of doc_base containing
     * `..' or `.' path components.  This non-handling means that sometimes
     * sp_relative_path_from_path will needlessly give an absolute path.
     *
     * It's probably not worth trying to address this until we're using proper
     * relative URL/IRI href processing (with liburiparser).
     *
     * (Note that one possibile difficulty with `..' is symlinks.) */

    if (!doc_base) {
        return g_get_current_dir();
    } else if (g_path_is_absolute(doc_base)) {
        return g_strdup(doc_base);
    } else {
        gchar *const cwd = g_get_current_dir();
        gchar *const ret = g_build_filename(cwd, doc_base, NULL);
        g_free(cwd);
        return ret;
    }
}

/**
 * Change relative hrefs in doc to be relative to \a new_base instead of doc.base.
 *
 * (NULL doc base or new_base is interpreted as current working directory.)
 *
 * \param spns True iff doc should contain sodipodi:absref attributes.
 */
202
void Inkscape::XML::rebase_hrefs(SPDocument *const doc, gchar const *const new_base, bool const spns)
203
{
204
    if (!doc->getBase()) {
Dmitry Kirsanov's avatar
Dmitry Kirsanov committed
205
        return;
206
    }
Dmitry Kirsanov's avatar
Dmitry Kirsanov committed
207

208
    gchar *const old_abs_base = calc_abs_doc_base(doc->getBase());
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    gchar *const new_abs_base = calc_abs_doc_base(new_base);

    /* TODO: Should handle not just image but also:
     *
     *    a, altGlyph, animElementAttrs, animate, animateColor, animateMotion, animateTransform,
     *    animation, audio, color-profile, cursor, definition-src, discard, feImage, filter,
     *    font-face-uri, foreignObject, glyphRef, handler, linearGradient, mpath, pattern,
     *    prefetch, radialGradient, script, set, textPath, tref, use, video
     *
     * (taken from the union of the xlink:href elements listed at
     * http://www.w3.org/TR/SVG11/attindex.html and
     * http://www.w3.org/TR/SVGMobile12/attributeTable.html).
     *
     * Also possibly some other attributes of type <URI> or <IRI> or list-thereof, or types like
     * <paint> that can include an IRI/URI, and stylesheets and style attributes.  (xlink:base is a
     * special case.  xlink:role and xlink:arcrole can be assumed to be already absolute, based on
     * http://www.w3.org/TR/SVG11/struct.html#xlinkRefAttrs .)
     *
     * Note that it may not useful to set sodipodi:absref for anything other than image.
     *
     * Note also that Inkscape only supports fragment hrefs (href="#pattern257") for many of these
     * cases. */
231
    GSList const *images = doc->getResourceList("image");
232
    for (GSList const *l = images; l != NULL; l = l->next) {
233
        Inkscape::XML::Node *ir = static_cast<SPObject *>(l->data)->getRepr();
234

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
        gchar * uri = g_strdup(ir->attribute("xlink:href"));
        if (!uri) {
            continue;
        }
        if (!strncmp(uri, "file://", 7)) {
            uri = g_strdup(g_filename_from_uri(ir->attribute("xlink:href"), NULL, NULL)); 
        }
        // The following two cases are for absolute hrefs that can be converted to relative.
        // Imported images, first time rebased, need an old base.
        gchar * href = uri;
        if (g_path_is_absolute(href)) {
            href = (gchar *) sp_relative_path_from_path(uri, old_abs_base);
        }
        // Files moved from a absolute path need a new one.
        if (g_path_is_absolute(href)) {
            href = (gchar *) sp_relative_path_from_path(uri, new_abs_base);
        }
        // Other bitmaps are either really absolute, or already relative.

#ifdef WIN32
        /* Windows relative path needs their native separators before we
         * compare it to native baserefs. */
        if (!g_path_is_absolute(href)) {
            g_strdelimit(href, "/", '\\');
        }
#endif

262 263 264 265 266 267
        /* TODO: Most of this function currently treats href as if it were a simple filename
         * (e.g. passing it to g_path_is_absolute, g_build_filename or IO::file_test, or avoiding
         * changing non-file hrefs), which breaks if href starts with a scheme or if href contains
         * any escaping. */

        if (!href || !href_needs_rebasing(href)) {
268
            g_free(uri);
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
            continue;
        }

        gchar *const abs_href(calc_abs_href(old_abs_base, href, ir->attribute("sodipodi:absref")));

        /* todo: One difficult case once we support writing to non-file locations is where
         * existing hrefs in the document point to local files.  In this case, we should
         * probably copy those referenced files to the new location at the same time.  It's
         * less clear what to do when copying from one non-file location to another.  We may
         * need to ask the user in some way (even if it's as a checkbox), but we'd like to
         * bother the user as little as possible yet also want to warn the user about the case
         * of file hrefs. */

        gchar const *const new_href = sp_relative_path_from_path(abs_href, new_abs_base);
        ir->setAttribute("sodipodi:absref", ( spns
                                              ? abs_href
                                              : NULL ));
286 287 288 289 290 291 292 293 294 295 296 297
        if (!g_path_is_absolute(new_href)) {
#ifdef WIN32
            /* Native Windows path separators are replaced with / so that the href
             * also works on Gnu/Linux and OSX */
            ir->setAttribute("xlink:href", g_strdelimit((gchar *) new_href, "\\", '/'));
#else
            ir->setAttribute("xlink:href", new_href);
#endif
        } else {
            ir->setAttribute("xlink:href", g_filename_to_uri((gchar *) new_href, NULL, NULL));
        }

298 299 300 301 302 303 304
        /* impl: I assume that if !spns then any existing sodipodi:absref is about to get
         * cleared (or is already cleared) anyway, in which case it doesn't matter whether we
         * clear or leave any existing sodipodi:absref value.  If that assumption turns out to
         * be wrong, then leaving it means risking leaving the wrong value (if xlink:href
         * referred to a different file than sodipodi:absref) while clearing it means risking
         * losing information. */

305 306
        g_free(uri);
        // (No need to free href, it's guaranteed to point into uri.)
307
        g_free(abs_href);
308
        // (No need to free new_href, it's guaranteed to point into abs_href.)
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    }

    g_free(new_abs_base);
    g_free(old_abs_base);
}


/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vi: set autoindent shiftwidth=4 tabstop=8 filetype=cpp expandtab softtabstop=4 encoding=utf-8 textwidth=99 :