-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request svg#546 from gjjones/master
adds addAttributes plugin
- Loading branch information
Showing
5 changed files
with
95 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict'; | ||
|
||
exports.type = 'full'; | ||
|
||
exports.active = false; | ||
|
||
exports.description = 'adds attributes to an outer <svg> element'; | ||
|
||
var ENOCLS = 'Error in plugin "addAttributesToSVGElement": absent parameters.\n\ | ||
It should have a list of classes in "attributes" or one "attribute".\n\ | ||
Config example:\n\n\ | ||
\ | ||
plugins:\n\ | ||
- addAttributesToSVGElement:\n\ | ||
attribute: "mySvg"\n\n\ | ||
\ | ||
plugins:\n\ | ||
- addAttributesToSVGElement:\n\ | ||
attributes: ["mySvg", "size-big"]\n'; | ||
|
||
/** | ||
* Add attributes to an outer <svg> element. Example config: | ||
* | ||
* plugins: | ||
* - addAttributesToSVGElement: | ||
* attribute: 'data-icon' | ||
* | ||
* plugins: | ||
* - addAttributesToSVGElement: | ||
* attributes: ['data-icon', 'data-disabled'] | ||
* | ||
* @author April Arcus | ||
*/ | ||
exports.fn = function(data, params) { | ||
if (!params || !(Array.isArray(params.attributes) && params.attributes.some(String) || params.attribute)) { | ||
console.error(ENOCLS); | ||
return data; | ||
} | ||
|
||
var attributes = params.attributes || [ params.attribute ], | ||
svg = data.content[0]; | ||
|
||
if (svg.isElem('svg')) { | ||
attributes.forEach(function (attribute) { | ||
if (!svg.hasAttr(attribute)) { | ||
svg.addAttr({ | ||
name: attribute, | ||
prefix: '', | ||
local: attribute | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
return data; | ||
|
||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.