From fcbce9c8d18e1d849777c067426906e1ba9c34ec Mon Sep 17 00:00:00 2001 From: Michael Robertson Date: Mon, 29 Jun 2020 16:05:00 -0400 Subject: [PATCH 1/3] Add title prop for setting SVG's title --- src/index.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/index.js b/src/index.js index 9bd2267..7e9e3a6 100644 --- a/src/index.js +++ b/src/index.js @@ -38,6 +38,9 @@ const InlineSvgComponent = { type: String, required: true, }, + title: { + type: String, + }, transformSource: { type: Function, default: (svg) => svg, @@ -122,6 +125,9 @@ const InlineSvgComponent = { let svgEl = result.getElementsByTagName('svg')[0]; if (svgEl) { svgEl = this.transformSource(svgEl); + if (this.title) { + this.setTitle(svgEl); + } resolve(svgEl); } else { reject(new Error('Loaded file is not valid SVG"')); @@ -138,6 +144,21 @@ const InlineSvgComponent = { request.send(); }); }, + + /** + * Create or edit the element of a SVG + * @param {Object} svg + */ + setTitle(svg) { + const titleTags = svg.getElementsByTagName('title'); + if (titleTags.length) { // overwrite existing title + titleTags[0].innerHTML = this.title; + } else { // create a title element if one doesn't already exist + const titleEl = document.createElementNS('http://www.w3.org/2000/svg', 'title'); + titleEl.innerHTML = this.title; + svg.appendChild(titleEl); + } + }, }, }; From 254819ba88962d590f2f51b8889fb4c131e69f3d Mon Sep 17 00:00:00 2001 From: Michael Robertson <mrobertson@cumulusnetworks.com> Date: Mon, 29 Jun 2020 16:09:59 -0400 Subject: [PATCH 2/3] Add title prop to docs --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 9816d25..47684ca 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ Loaded SVGs are cached so it will not make network request twice. - [Usage](#usage) - [props](#props) - [src](#--src) + - [title](#--title) - [transformSource](#--transformsource) - [SVG attributes](#svg-attributes) - [events](#events) @@ -110,6 +111,13 @@ Learn more: - https://vue-loader.vuejs.org/guide/asset-url.html#transform-rules - https://cli.vuejs.org/guide/html-and-static-assets.html#static-assets-handling +#### - `title` +Sets/overwrites the title of the SVG + +``` +<inline-svg :src="image.svg" :title="My Image"/> +``` + #### - `transformSource` Function to transform SVG source From 58e501c14439e734a97664823803fa67676b55b4 Mon Sep 17 00:00:00 2001 From: Michael Robertson <mrobertson@cumulusnetworks.com> Date: Tue, 14 Jul 2020 10:22:31 -0400 Subject: [PATCH 3/3] Set title for each use --- src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 7e9e3a6..9cf641d 100644 --- a/src/index.js +++ b/src/index.js @@ -89,6 +89,9 @@ const InlineSvgComponent = { for (let i = attrs.length - 1; i >= 0; i--) { this.svgAttrs[attrs[i].name] = attrs[i].value; } + if (this.title) { + this.setTitle(svg); + } // copy inner html this.svgContent = svg.innerHTML; // render svg element @@ -125,9 +128,6 @@ const InlineSvgComponent = { let svgEl = result.getElementsByTagName('svg')[0]; if (svgEl) { svgEl = this.transformSource(svgEl); - if (this.title) { - this.setTitle(svgEl); - } resolve(svgEl); } else { reject(new Error('Loaded file is not valid SVG"'));