Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix relative URLs of linked images
  • Loading branch information
aumouvantsillage committed Jun 20, 2015
1 parent ccbaf3c commit eaedfc2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
22 changes: 22 additions & 0 deletions js/Storage.js
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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;

Expand Down
2 changes: 0 additions & 2 deletions js/svg/SVGDocument.js
Expand Up @@ -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");

Expand Down

0 comments on commit eaedfc2

Please sign in to comment.