From eaedfc2eed2a186d0dd8bd51ce49233cdcb52408 Mon Sep 17 00:00:00 2001 From: senshu Date: Sat, 20 Jun 2015 22:22:28 +0200 Subject: [PATCH] Fix relative URLs of linked images --- js/Storage.js | 22 ++++++++++++++++++++++ js/svg/SVGDocument.js | 2 -- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/js/Storage.js b/js/Storage.js index 814c72b0..22d90466 100644 --- a/js/Storage.js +++ b/js/Storage.js @@ -9,6 +9,7 @@ import {EventEmitter} from "events"; import nunjucks from "nunjucks"; import Jed from "jed"; import {upgrade} from "./upgrade"; +import {toArray} from "./utils"; export var Storage = Object.create(EventEmitter.prototype); @@ -69,6 +70,7 @@ Storage.onBackendLoad = function (backend, fileDescriptor, data, err) { this.reloading = fileDescriptor === this.svgFileDescriptor; this.document.import(data); if (this.document.isValidSVG) { + this.resolveRelativeURLs(location); this.controller.setSVGDocument(this.document); this.svgFileDescriptor = fileDescriptor; this.openJSONFile(name.replace(/\.svg$/, ".sozi.json"), location); @@ -87,6 +89,26 @@ Storage.onBackendLoad = function (backend, fileDescriptor, data, err) { } }; +/* + * Fix the href attribute of linked images when the given URL is relative. + * + * In linked images, the href attribute can be either an absolute URL + * or a path relative to the location of the SVG file. + * But in the presentation editor, URLs are relative to the location of + * the index.html file of the application. + * For this reason, we modify image URLs by prefixing all relative URLs + * with the actual location of the SVG file. + */ +Storage.resolveRelativeURLs = function (location) { + var images = toArray(this.document.root.getElementsByTagName("image")); + images.forEach(img => { + var href = img.getAttribute("xlink:href"); + if (!/^[a-z]+:|^[/#]/.test(href)) { + img.setAttribute("xlink:href", `${location}/${href}`); + } + }); +}; + Storage.onBackendChange = function (fileDescriptor) { var _ = this.gettext; diff --git a/js/svg/SVGDocument.js b/js/svg/SVGDocument.js index 40c378b3..895029a7 100644 --- a/js/svg/SVGDocument.js +++ b/js/svg/SVGDocument.js @@ -108,8 +108,6 @@ export var SVGDocument = { link.addEventListener("mousedown", evt => evt.stopPropagation(), false); }); - // TODO Transform xlink:href attributes to replace relative URLs with absolute URLs - // Wrap isolated elements into groups var svgWrapper = document.createElementNS(SVG_NS, "g");