Skip to content

Commit

Permalink
Merge 8bd1241 into a63f7a7
Browse files Browse the repository at this point in the history
  • Loading branch information
vzaidman committed Apr 24, 2018
2 parents a63f7a7 + 8bd1241 commit 541b218
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
37 changes: 26 additions & 11 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 @@ -164,14 +173,18 @@ exports.fn = function(node, opts, extra) {
var idPrefixed = '';
csstree.walk(cssAst, function(node) {

// #ID, .class
if ((node.type === 'IdSelector' ||
node.type === 'ClassSelector') &&
node.name) {
// #ID,
if (node.type === 'IdSelector' && node.name) {
node.name = addPrefix(node.name);
return;
}

// .class
if (node.type === 'ClassSelector' && node.name) {
node.name = node.name.split(/\s+/).map(addPrefix).join(' ');
return;
}

// url(...) in value
if (node.type === 'Url' &&
node.value.value && node.value.value.length > 0) {
Expand All @@ -197,14 +210,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.
28 changes: 28 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 541b218

Please sign in to comment.