Skip to content

Commit

Permalink
Format all plugins with prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed Mar 9, 2021
1 parent 1be0d0d commit 00ec0f7
Show file tree
Hide file tree
Showing 41 changed files with 2,078 additions and 2,017 deletions.
65 changes: 32 additions & 33 deletions plugins/addAttributesToSVGElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,47 +43,46 @@ plugins: [
}
}
]
`
`;

/**
* Add attributes to an outer <svg> element. Example config:
*
* @author April Arcus
*/
exports.fn = function(data, params) {
if (!params || !(Array.isArray(params.attributes) || params.attribute)) {
console.error(ENOCLS);
return data;
}
exports.fn = function (data, params) {
if (!params || !(Array.isArray(params.attributes) || params.attribute)) {
console.error(ENOCLS);
return data;
}

var attributes = params.attributes || [ params.attribute ],
svg = data.content[0];
var attributes = params.attributes || [params.attribute],
svg = data.content[0];

if (svg.isElem('svg')) {
attributes.forEach(function (attribute) {
if (typeof attribute === 'string') {
if (!svg.hasAttr(attribute)) {
svg.addAttr({
name: attribute,
prefix: '',
local: attribute
});
}
} else if (typeof attribute === 'object') {
Object.keys(attribute).forEach(function (key) {
if (!svg.hasAttr(key)) {
svg.addAttr({
name: key,
value: attribute[key],
prefix: '',
local: key
});
}
});
}
if (svg.isElem('svg')) {
attributes.forEach(function (attribute) {
if (typeof attribute === 'string') {
if (!svg.hasAttr(attribute)) {
svg.addAttr({
name: attribute,
prefix: '',
local: attribute,
});
}
} else if (typeof attribute === 'object') {
Object.keys(attribute).forEach(function (key) {
if (!svg.hasAttr(key)) {
svg.addAttr({
name: key,
value: attribute[key],
prefix: '',
local: key,
});
}
});
}

return data;
}
});
}

return data;
};
29 changes: 17 additions & 12 deletions plugins/addClassesToSVGElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,24 @@ plugins:
*
* @author April Arcus
*/
exports.fn = function(data, params) {
if (!params || !(Array.isArray(params.classNames) && params.classNames.some(String) || params.className)) {
console.error(ENOCLS);
return data;
}

var classNames = params.classNames || [ params.className ],
svg = data.content[0];
exports.fn = function (data, params) {
if (
!params ||
!(
(Array.isArray(params.classNames) && params.classNames.some(String)) ||
params.className
)
) {
console.error(ENOCLS);
return data;
}

if (svg.isElem('svg')) {
svg.class.add.apply(svg.class, classNames);
}
var classNames = params.classNames || [params.className],
svg = data.content[0];

return data;
if (svg.isElem('svg')) {
svg.class.add.apply(svg.class, classNames);
}

return data;
};
66 changes: 32 additions & 34 deletions plugins/cleanupAttrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ exports.type = 'perItem';

exports.active = true;

exports.description = 'cleanups attributes from newlines, trailing and repeating spaces';
exports.description =
'cleanups attributes from newlines, trailing and repeating spaces';

exports.params = {
newlines: true,
trim: true,
spaces: true
newlines: true,
trim: true,
spaces: true,
};

var regNewlinesNeedSpace = /(\S)\r?\n(\S)/g,
regNewlines = /\r?\n/g,
regSpaces = /\s{2,}/g;
regNewlines = /\r?\n/g,
regSpaces = /\s{2,}/g;

/**
* Cleanup attributes values from newlines, trailing and repeating spaces.
Expand All @@ -25,32 +26,29 @@ var regNewlinesNeedSpace = /(\S)\r?\n(\S)/g,
*
* @author Kir Belevich
*/
exports.fn = function(item, params) {

if (item.isElem()) {

item.eachAttr(function(attr) {

if (params.newlines) {
// new line which requires a space instead of themselve
attr.value = attr.value.replace(regNewlinesNeedSpace, function(match, p1, p2) {
return p1 + ' ' + p2;
});

// simple new line
attr.value = attr.value.replace(regNewlines, '');
}

if (params.trim) {
attr.value = attr.value.trim();
}

if (params.spaces) {
attr.value = attr.value.replace(regSpaces, ' ');
}

});

}

exports.fn = function (item, params) {
if (item.isElem()) {
item.eachAttr(function (attr) {
if (params.newlines) {
// new line which requires a space instead of themselve
attr.value = attr.value.replace(
regNewlinesNeedSpace,
function (match, p1, p2) {
return p1 + ' ' + p2;
}
);

// simple new line
attr.value = attr.value.replace(regNewlines, '');
}

if (params.trim) {
attr.value = attr.value.trim();
}

if (params.spaces) {
attr.value = attr.value.replace(regSpaces, ' ');
}
});
}
};
100 changes: 51 additions & 49 deletions plugins/cleanupEnableBackground.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ exports.type = 'full';

exports.active = true;

exports.description = 'remove or cleanup enable-background attribute when possible';
exports.description =
'remove or cleanup enable-background attribute when possible';

/**
* Remove or cleanup enable-background attr which coincides with a width/height box.
Expand All @@ -21,64 +22,65 @@ exports.description = 'remove or cleanup enable-background attribute when possib
*
* @author Kir Belevich
*/
exports.fn = function(data) {
exports.fn = function (data) {
var regEnableBackground = /^new\s0\s0\s([-+]?\d*\.?\d+([eE][-+]?\d+)?)\s([-+]?\d*\.?\d+([eE][-+]?\d+)?)$/,
hasFilter = false,
elems = ['svg', 'mask', 'pattern'];

var regEnableBackground = /^new\s0\s0\s([-+]?\d*\.?\d+([eE][-+]?\d+)?)\s([-+]?\d*\.?\d+([eE][-+]?\d+)?)$/,
hasFilter = false,
elems = ['svg', 'mask', 'pattern'];
function checkEnableBackground(item) {
if (
item.isElem(elems) &&
item.hasAttr('enable-background') &&
item.hasAttr('width') &&
item.hasAttr('height')
) {
var match = item
.attr('enable-background')
.value.match(regEnableBackground);

function checkEnableBackground(item) {
if (match) {
if (
item.isElem(elems) &&
item.hasAttr('enable-background') &&
item.hasAttr('width') &&
item.hasAttr('height')
item.attr('width').value === match[1] &&
item.attr('height').value === match[3]
) {

var match = item.attr('enable-background').value.match(regEnableBackground);

if (match) {
if (
item.attr('width').value === match[1] &&
item.attr('height').value === match[3]
) {
if (item.isElem('svg')) {
item.removeAttr('enable-background');
} else {
item.attr('enable-background').value = 'new';
}
}
}

if (item.isElem('svg')) {
item.removeAttr('enable-background');
} else {
item.attr('enable-background').value = 'new';
}
}
}
}
}

function checkForFilter(item) {
if (item.isElem('filter')) {
hasFilter = true;
}
function checkForFilter(item) {
if (item.isElem('filter')) {
hasFilter = true;
}
}

function monkeys(items, fn) {
items.content.forEach(function(item) {
fn(item);
function monkeys(items, fn) {
items.content.forEach(function (item) {
fn(item);

if (item.content) {
monkeys(item, fn);
}
});
return items;
}

var firstStep = monkeys(data, function(item) {
checkEnableBackground(item);
if (!hasFilter) {
checkForFilter(item);
}
if (item.content) {
monkeys(item, fn);
}
});
return items;
}

return hasFilter ? firstStep : monkeys(firstStep, function(item) {
//we don't need 'enable-background' if we have no filters
item.removeAttr('enable-background');
});
var firstStep = monkeys(data, function (item) {
checkEnableBackground(item);
if (!hasFilter) {
checkForFilter(item);
}
});

return hasFilter
? firstStep
: monkeys(firstStep, function (item) {
//we don't need 'enable-background' if we have no filters
item.removeAttr('enable-background');
});
};
Loading

0 comments on commit 00ec0f7

Please sign in to comment.