Skip to content

Commit

Permalink
fixed #948 - adds prefixes to each class individually
Browse files Browse the repository at this point in the history
  • Loading branch information
vzaidman committed Apr 24, 2018
1 parent a63f7a7 commit 0654fb5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
30 changes: 20 additions & 10 deletions plugins/prefixIds.js
Expand Up @@ -61,13 +61,13 @@ var prefixId = function(val) {

// attr.value helper methods

// prefixes a normal attribute value
var addPrefixToAttr = function(attr) {
// prefixes a class attribute value
var addPrefixToClassAttr = function(attr) {
if (!attrNotEmpty(attr)) {
return;
}

attr.value = addPrefix(attr.value);
attr.value = attr.value.split(/\s+/).map(addPrefix).join(' ');
};

// prefixes an ID attribute value
Expand All @@ -76,6 +76,15 @@ var addPrefixToIdAttr = function(attr) {
return;
}

attr.value = addPrefix(attr.value);
};

// prefixes a href attribute value
var addPrefixToHrefAttr = function(attr) {
if (!attrNotEmpty(attr)) {
return;
}

var idPrefixed = prefixId(attr.value);
if (!idPrefixed) {
return;
Expand Down Expand Up @@ -165,9 +174,8 @@ exports.fn = function(node, opts, extra) {
csstree.walk(cssAst, function(node) {

// #ID, .class
if ((node.type === 'IdSelector' ||
node.type === 'ClassSelector') &&
node.name) {
if ((node.type === 'ClassSelector' || node.type === 'IdSelector')
&& node.name) {
node.name = addPrefix(node.name);
return;
}
Expand Down Expand Up @@ -197,14 +205,16 @@ exports.fn = function(node, opts, extra) {
}

// ID
addPrefixToAttr(node.attrs.id);
addPrefixToIdAttr(node.attrs.id);

// Class
addPrefixToAttr(node.attrs.class);
addPrefixToClassAttr(node.attrs.class);

// href
addPrefixToIdAttr(node.attrs.href);
addPrefixToHrefAttr(node.attrs.href);

// (xlink:)href (deprecated, must be still supported)
addPrefixToIdAttr(node.attrs['xlink:href']);
addPrefixToHrefAttr(node.attrs['xlink:href']);

// referenceable properties
for (var referencesProp of referencesProps) {
Expand Down
4 changes: 2 additions & 2 deletions test/plugins/prefixIds.01.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions test/plugins/prefixIds.06.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0654fb5

Please sign in to comment.