Skip to content
This repository was archived by the owner on Sep 25, 2021. It is now read-only.

Commit 833c374

Browse files
committedOct 2, 2015
Handle style tags and style properties correctly
1 parent 920f585 commit 833c374

File tree

6 files changed

+103
-17
lines changed

6 files changed

+103
-17
lines changed
 

‎package.json

+13-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "svg-react-loader",
33
"fullname": "SVG to React Loader",
44
"description": "A Webpack Loader to turn SVGs into React Components",
5-
"version": "0.1.5",
5+
"version": "0.2.0",
66
"keywords": [
77
"webpack",
88
"loader",
@@ -18,17 +18,18 @@
1818
"loader-utils": "0.x"
1919
},
2020
"devDependencies": {
21-
"babel-loader": "5.x",
22-
"babel-core": "5.x",
23-
"react": ">=0.12",
24-
"webpack": "1.x",
25-
"mocha": "2.x",
26-
"should": "6.x",
27-
"del": "*",
28-
"gulp": "3.x",
29-
"gulp-util": "*",
30-
"gulp-ejs": "*",
31-
"gulp-concat": "*"
21+
"babel-loader": "5.x",
22+
"babel-core": "5.x",
23+
"react": ">=0.12",
24+
"webpack": "1.x",
25+
"webpack-dev-server": "1.x",
26+
"mocha": "2.x",
27+
"should": "6.x",
28+
"del": "*",
29+
"gulp": "3.x",
30+
"gulp-util": "*",
31+
"gulp-ejs": "*",
32+
"gulp-concat": "*"
3233
},
3334
"peerDependencies": {
3435
"babel-core": "5.x",

‎test/build.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var React = require('react');
2-
var MashUp = require('./svg/mashup.svg');
2+
var Styles = require('./svg/styles.svg');
33

4-
React.render(<MashUp />, document.getElementById('content'));
4+
React.render(<Styles />, document.getElementById('content'));
55

‎test/svg/styles.svg

+27
Loading

‎test/test-test.js

+27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*globals describe, it*/
2+
var react = require('react');
23
var loader = require('svg-react-loader');
34
var babel = require('babel-core');
45
var fs = require('fs');
@@ -53,4 +54,30 @@ describe('something', function () {
5354
// resourceQuery: '?tag=foo&attrs={foo: \'bar\'}'
5455
});
5556
});
57+
58+
it.only('should handle styles', function (done) {
59+
var filename = './svg/styles.svg';
60+
61+
invoke(read(filename), {
62+
callback: function (error, result) {
63+
if (error) {
64+
throw error;
65+
}
66+
67+
var src = babel.transform(result).code;
68+
console.log(src);
69+
fs.writeFileSync(__dirname + '/temp', src);
70+
var el = react.createElement(require(__dirname + '/temp'));
71+
var html = react.renderToStaticMarkup(el);
72+
73+
// var el = react.createElement('style');
74+
// var html = react.renderToStaticMarkup(el);
75+
76+
console.log(html);
77+
fs.unlink(__dirname + '/temp');
78+
done();
79+
},
80+
resourcePath: filename
81+
});
82+
});
5683
});

‎utility/sanitize.js

+30-3
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,39 @@ var XML_TEXT_NODE_KEY = '_';
77
var NS_SEPARATOR = ':';
88
var DATA_ATTR_KEY = 'data-svgreactloader';
99
var XML_NAMESPACE_KEY = 'xmlns';
10+
var TEXT_REGEX = /(["'])/g;
1011

1112
var XML_NAMESPACES = {
1213
svg: 'http://www.w3.org/2000/svg',
1314
xlink: 'http://www.w3.org/1999/xlink'
1415
};
1516

17+
var STYLE_ATTR_KEY = 'style';
18+
1619
var RESERVED_KEYS = {
1720
'class': 'className',
1821
'for': 'htmlFor'
1922
};
2023

24+
/**
25+
* @param {Object[]} nodes
26+
*/
27+
function sanitizeStyleNodes (nodes) {
28+
nodes.
29+
forEach(function (node, idx, context) {
30+
var isText = typeof node === 'string';
31+
var src = isText ? node : node[XML_TEXT_NODE_KEY];
32+
var text = '{`' + src.replace(TEXT_REGEX, "\\$1") + '`}';
33+
34+
if (isText) {
35+
context[idx] = text;
36+
}
37+
else {
38+
node[XML_TEXT_NODE_KEY] = text;
39+
}
40+
});
41+
}
42+
2143
/**
2244
* Remove any non-jsx xml attributes from the given node and any of its child
2345
* nodes. Return the original node sanitized.
@@ -42,7 +64,7 @@ module.exports = function sanitize (xmlNode, namespaces) {
4264
var hasSep = !!~i;
4365
var ns = hasSep && key.slice(0, i);
4466
var attr = hasSep ? key.slice(i + 1) : key;
45-
var value = xmlNode.$[key];
67+
var value = xmlNode[XML_ATTR_KEY][key];
4668
var nsKey = hasSep ? ns : attr;
4769

4870
if (nsKey === XML_NAMESPACE_KEY && !hasSep) {
@@ -54,7 +76,7 @@ module.exports = function sanitize (xmlNode, namespaces) {
5476

5577
nsKey = nsKey === XML_NAMESPACE_KEY ? 'xml' : nsKey;
5678

57-
if (ns && attr) {
79+
if (ns && attr || attr === STYLE_ATTR_KEY) {
5880
acc[DATA_ATTR_KEY] = acc[DATA_ATTR_KEY] || [];
5981
acc[DATA_ATTR_KEY].push([namespaces[nsKey], attr, value]);
6082
}
@@ -69,7 +91,12 @@ module.exports = function sanitize (xmlNode, namespaces) {
6991

7092
keys(omit(xmlNode, [XML_ATTR_KEY, XML_TEXT_NODE_KEY])).
7193
forEach(function (key) {
72-
sanitize(xmlNode[key], namespaces);
94+
if (key === 'style') {
95+
sanitizeStyleNodes(xmlNode[key]);
96+
}
97+
else {
98+
sanitize(xmlNode[key], namespaces);
99+
}
73100
});
74101

75102
if (xmlNode[XML_ATTR_KEY] && xmlNode[XML_ATTR_KEY][DATA_ATTR_KEY]) {

‎webpack.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ module.exports = {
1010
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel' },
1111
{ test: /\.svg$/, loader: 'babel!svg-react' }
1212
]
13+
},
14+
15+
devServer: {
16+
contentBase: './'
1317
}
1418
};

0 commit comments

Comments
 (0)
Failed to load comments.