Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix/547-image-in-svg-fail #550

Closed
wants to merge 18 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed #547, add XLink namespace for SVG results.
jszuminski committed Jul 31, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit af24b2733660200f1bb9f87393be25926ed43d95
15 changes: 14 additions & 1 deletion lib/server/routes/export.js
Original file line number Diff line number Diff line change
@@ -23,7 +23,8 @@ import {
isObjectEmpty,
isPrivateRangeUrlFound,
optionsStringify,
measureTime
measureTime,
addXlinkNamespace
} from '../../utils.js';

import HttpError from '../../errors/HttpError.js';
@@ -249,6 +250,18 @@ const exportHandler = async (request, response, next) => {
doCallbacks(afterRequest, request, response, { id, body: info.result });

if (info.result) {
// This exception is a workaround for #547
// The plainly downloaded SVG is not properly formatted -
// it lacks the xmlns:xlink so images with "xlink:href" cannot be displayed
// and the entire SVG is deemed as incorrect (this may be a Highcharts
// problem as well as they should take care of this).
// A proper SVG has xlmns:xlink defined if they are used
// and Highcharts does not seem to have that for now.
// Once they do, we can get rid of this.
if (type === 'svg') {
info.result = addXlinkNamespace(info.result);
}

// If only base64 is required, return it
if (body.b64) {
// SVG Exception for the Highcharts 11.3.0 version
33 changes: 33 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -448,6 +448,39 @@ export const measureTime = () => {
return () => Number(process.hrtime.bigint() - start) / 1000000;
};

/**
* This method is used to add the xlink namespace to the SVG string.
* This may be a workaround, as Highcharts should take care of this.
* @param {string} svgString
* @returns {string}
*/
export const addXlinkNamespace = (svgString) => {
// Check if the xlink namespace is already present
const xlinkNamespace = 'xmlns:xlink="http://www.w3.org/1999/xlink"';
if (svgString.includes(xlinkNamespace)) {
// The namespace is already included, no need to add it
return svgString;
}

// Find the position of the opening <svg> tag
const svgTagEnd = svgString.indexOf('>');

// If <svg> is self-closing, find the position accordingly
const selfClosing = svgString[svgTagEnd - 1] === '/';

// Define the insertion point for the namespace attribute
const insertionPoint = selfClosing ? svgTagEnd - 1 : svgTagEnd;

// Insert the xlink namespace declaration
const modifiedSvgString =
svgString.slice(0, insertionPoint) +
' ' +
xlinkNamespace +
svgString.slice(insertionPoint);

return modifiedSvgString;
};

export default {
__dirname,
clearText,