Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Prefix ids in style tags #86

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

<a name="0.8.3"></a>
## [0.8.3](https://github.com/sairion/svg-inline-loader/compare/v0.8.2...v0.8.3) (2018-10-19)


### Bug Fixes

* correctly close xml tags with namespaces ([c5b4421](https://github.com/sairion/svg-inline-loader/commit/c5b4421))



<a name="0.8.2"></a>
## [0.8.2](https://github.com/sairion/svg-inline-loader/compare/v0.8.1...v0.8.2) (2018-10-17)



<a name="0.8.1"></a>
## [0.8.1](https://github.com/sairion/svg-inline-loader/compare/v0.8.0...v0.8.1) (2018-10-16)


### Bug Fixes

* also add id prefixes for ids within style tags ([eae3faf](https://github.com/sairion/svg-inline-loader/commit/eae3faf))



<a name="0.8.0"></a>
# [0.8.0](https://github.com/sairion/svg-inline-loader/compare/0.6.1...v0.8.0) (2017-07-16)

Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ var regexSequences = [
[/<!--.*-->/gi, ""],

// SVG XML -> HTML5
[/\<([A-Za-z]+)([^\>]*)\/\>/g, "<$1$2></$1>"], // convert self-closing XML SVG nodes to explicitly closed HTML5 SVG nodes
[/\s+/g, " "], // replace whitespace sequences with a single space
[/\> \</g, "><"] // remove whitespace between tags
[/\<([A-Za-z0-9_.-]+)(:[A-Za-z0-9_.-]+)?([^\>]*)\/\>/g, "<$1$2$3></$1$2>"], // convert self-closing XML SVG nodes to explicitly closed HTML5 SVG nodes
[/\s+/g, " "], // replace whitespace sequences with a single space
[/\> \</g, "><"] // remove whitespace between tags
];

function getExtractedSVG(svgStr, query) {
Expand Down
17 changes: 13 additions & 4 deletions lib/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ function createClassPrefix(classPrefix) {
//Prefix classes when multiple classes are present
var classes = tag.attributes[classIdx][1];
var prefixedClassString = "";

classes = classes.replace(/[ ]+/,' ');
classes = classes.split(' ');
classes.forEach(function(classI){
prefixedClassString += classPrefix + classI + ' ';
});

tag.attributes[classIdx][1] = prefixedClassString;
}
}
Expand All @@ -131,14 +131,23 @@ function createClassPrefix(classPrefix) {
}

function createIdPrefix(idPrefix) {
var url_pattern = /^url\(#.+\)$/i;
var url_pattern = /url\(#(.+?)\)/ig;
var inStyleTag = false;
return function prefixIds(tag) {
var idIdx = getAttributeIndex(tag, 'id');
if (inStyleTag) {
tag.chars = tag.chars.replace(url_pattern, 'url(#'+idPrefix+'$1)');
inStyleTag = false;
return tag;
}
if (conditions.isStyleToken(tag)) {
inStyleTag = true;
return tag;
}
if (idIdx !== -1) {
// prefix id definitions
tag.attributes[idIdx][1] = idPrefix + tag.attributes[idIdx][1];
}

if (tag.tagName == 'use') {
// replace references via <use xlink:href='#foo'>
var hrefIdx = getAttributeIndex(tag, 'xlink:href');
Expand Down
Loading