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

Commit ada00d7

Browse files
nonmanifoldjoshwiens
authored andcommitted
feat: warnTags and warnTagAttrs
1 parent 2d28c42 commit ada00d7

File tree

8 files changed

+96
-10
lines changed

8 files changed

+96
-10
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ warning: this won't work unless you specify `removeTags: true`
3131

3232
default: `removingTags: ['title', 'desc', 'defs', 'style']`
3333

34+
#### `warnTags: [...string]`
35+
36+
warns about tags, ex: ['title', 'desc', 'defs', 'style']
37+
38+
default: `warnTags: []`
39+
3440
#### `removeSVGTagAttrs: boolean`
3541

3642
Removes `width` and `height` attributes from `<svg />`.
@@ -43,6 +49,11 @@ Removes attributes from inside the `<svg />`.
4349

4450
default: `removingTagAttrs: []`
4551

52+
#### `warnTagAttrs: [...string]`
53+
54+
Warns to console about attributes from inside the `<svg />`.
55+
56+
default: `warnTagAttrs: []`
4657
#### `classPrefix: boolean || string`
4758

4859
Adds a prefix to class names to avoid collision across svg files.

config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ module.exports = {
1010
removingTagAttrs: [],
1111
classPrefix: false,
1212
idPrefix: false,
13+
warnTags: [],
14+
warnTagAttrs: []
1315
};

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var regexSequences = [
1616
// SVG XML -> HTML5
1717
[/\<([A-Za-z]+)([^\>]*)\/\>/g, "<$1$2></$1>"], // convert self-closing XML SVG nodes to explicitly closed HTML5 SVG nodes
1818
[/\s+/g, " "], // replace whitespace sequences with a single space
19-
[/\> \</g, "><"], // remove whitespace between tags
19+
[/\> \</g, "><"] // remove whitespace between tags
2020
];
2121

2222
function getExtractedSVG(svgStr, query) {

karma.conf.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@ var webpackConf = {
66
loaders: [
77
{
88
test: /\.json$/,
9-
loaders: ['json'],
10-
},
9+
loaders: ['json']
10+
}
1111
]
1212
},
1313
entry: [
1414
'./index.js'
1515
],
1616
resolve: {
1717
extensions: ["", ".js", ".jsx"],
18-
},
18+
}
1919
};
2020

2121
module.exports = function(config) {
2222
var conf = {
2323
basePath: '.',
2424
frameworks: ['mocha'],
2525
files: [
26-
'./tests/**/*.js',
26+
'./tests/**/*.js'
2727
],
2828
exclude: [],
2929
preprocessors: {
30-
'./tests/**/*.js': ['webpack'],
30+
'./tests/**/*.js': ['webpack']
3131
},
3232
reporters: ['spec'],
3333
port: 9876,
@@ -39,7 +39,7 @@ module.exports = function(config) {
3939
'karma-spec-reporter',
4040
'karma-mocha',
4141
'karma-chrome-launcher',
42-
'karma-webpack',
42+
'karma-webpack'
4343
],
4444
webpack: webpackConf,
4545
autoWatch: true,

lib/conditions.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,18 @@ function createHasNoAttributes(attributes) {
2626
}
2727
}
2828

29+
function createHasAttributes(attributes) {
30+
return function hasAttributes(attributeToken) {
31+
return attributes.indexOf(attributeToken[0]) > -1;
32+
}
33+
}
34+
2935
module.exports = {
3036
isSVGToken: isSVGToken,
3137
isStyleToken: isStyleToken,
3238
isFilledObject: isFilledObject,
3339
hasNoWidthHeight: hasNoWidthHeight,
3440
createHasNoAttributes: createHasNoAttributes,
41+
createHasAttributes: createHasAttributes,
3542
isStartTag: isStartTag
3643
};

lib/transformer.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,30 @@ function createRemoveTagAttrs(removingTagAttrs) {
2020
return tag;
2121
};
2222
}
23-
23+
function createWarnTagAttrs(warnTagAttrs) {
24+
warnTagAttrs = warnTagAttrs || [];
25+
var hasNoAttributes = conditions.createHasAttributes(warnTagAttrs);
26+
return function warnTagAttrs(tag) {
27+
if (conditions.isStartTag(tag)) {
28+
var attrs=tag.attributes.filter(hasNoAttributes);
29+
if(attrs.length > 0) {
30+
var attrList=[];
31+
for(var i=0;i<attrs.length;i++){
32+
var attr=attrs[i];
33+
attrList.push(attr[0]);
34+
}
35+
console.warn('svg-inline-loader: tag ' + tag.tagName + ' has forbidden attrs: ' + attrList.join(', '));
36+
}
37+
}
38+
return tag;
39+
};
40+
}
2441
function isRemovingTag(removingTags, tag) {
2542
return removingTags.indexOf(tag.tagName) > -1;
2643
}
27-
44+
function isWarningTag(warningTags, tag) {
45+
return warningTags.indexOf(tag.tagName) > -1;
46+
}
2847
// FIXME: Due to limtation of parser, we need to implement our
2948
// very own little state machine to express tree structure
3049

@@ -46,6 +65,16 @@ function createRemoveTags(removingTags) {
4665
};
4766
}
4867

68+
function createWarnTags(warningTags) {
69+
warningTags = warningTags || [];
70+
71+
return function warnTags(tag) {
72+
if (conditions.isStartTag(tag) && isWarningTag(warningTags, tag)) {
73+
console.warn('svg-inline-loader: forbidden tag ' + tag.tagName);
74+
}
75+
return tag;
76+
};
77+
}
4978
function getAttributeIndex (tag, attr) {
5079
if( tag.attributes !== undefined && tag.attributes.length > 0 ) {
5180
for(var i = 0; i < tag.attributes.length; i++) {
@@ -142,7 +171,9 @@ function runTransform(tokens, configOverride) {
142171
if (config.classPrefix !== false) transformations.push(createClassPrefix(config.classPrefix));
143172
if (config.idPrefix !== false) transformations.push(createIdPrefix(config.idPrefix));
144173
if (config.removeSVGTagAttrs === true) transformations.push(removeSVGTagAttrs);
174+
if (config.warnTags.length > 0) transformations.push(createWarnTags(config.warnTags));
145175
if (config.removeTags === true) transformations.push(createRemoveTags(config.removingTags));
176+
if (config.warnTagAttrs.length > 0) transformations.push(createWarnTagAttrs(config.warnTagAttrs));
146177
if (config.removingTagAttrs.length > 0) transformations.push(createRemoveTagAttrs(config.removingTagAttrs));
147178

148179
transformations.forEach(function (transformation) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
},
3030
"devDependencies": {
3131
"chai": "^3.0.0",
32+
"chai-spies": "^0.7.1",
3233
"json-loader": "^0.5.4",
3334
"karma": "^1.0.0",
3435
"karma-chrome-launcher": "^1.0.1",

tests/svg-inline-loader.test.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ var simpleHTMLTokenizer = require('simple-html-tokenizer');
22
var tokenize = simpleHTMLTokenizer.tokenize;
33

44
var SVGInlineLoader = require('../index');
5-
var assert = require('chai').assert;
5+
var chai = require('chai');
6+
var assert = chai.assert;
7+
var expect = chai.expect;
8+
var spies = require('chai-spies');
9+
chai.use(spies);
10+
var createSpy = chai.spy;
611
var _ = require('lodash');
712

813
var svgWithRect = require('raw!./fixtures/xml-rect.svg');
@@ -117,5 +122,34 @@ describe('getExtractedSVG()', function(){
117122
}
118123
});
119124
});
125+
it('should be able to warn about tagsAttrs to be removed listed in `warnTagAttrs` option via console.log', function () {
126+
var svg = require('raw!./fixtures/with-ids.svg');
127+
var tobeWarned = ['id'];
128+
var oldConsoleWarn = console.warn;
129+
var warnings=[];
130+
console.warn=createSpy(function (str) {
131+
warnings.push(str);
132+
});
133+
var processedSVG = SVGInlineLoader.getExtractedSVG(svg, { warnTagAttrs: tobeWarned });
134+
var reTokenizedSVG = tokenize(processedSVG);
135+
expect(console.warn).to.have.been.called.with('svg-inline-loader: tag path has forbidden attrs: id');
136+
console.warn = oldConsoleWarn; // reset console back
137+
});
120138

139+
it('should be able to specify tags to be warned about by `warnTags` option', function () {
140+
var svg = require('raw!./fixtures/removing-tags.svg');
141+
var tobeWarnedAbout = ['title', 'desc', 'defs', 'style', 'image'];
142+
var oldConsoleWarn = console.warn;
143+
var warnings=[];
144+
console.warn=createSpy(function (str) {
145+
warnings.push(str);
146+
});
147+
var processedStyleInsertedSVG = SVGInlineLoader.getExtractedSVG(svg, { warnTags: tobeWarnedAbout });
148+
var reTokenizedStyleInsertedSVG = tokenize(processedStyleInsertedSVG);
149+
150+
expect(console.warn).to.have.been.called();
151+
expect(console.warn).to.have.been.called.min(3);
152+
expect(console.warn).to.have.been.called.with('svg-inline-loader: forbidden tag style');
153+
console.warn = oldConsoleWarn; // reset console back
154+
});
121155
});

0 commit comments

Comments
 (0)