Skip to content

Commit

Permalink
Use is attribute to ignore attribute whitelist. Fixes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
Vivek Patel committed Jan 27, 2016
1 parent 1a0ed71 commit f0ab93b
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 72 deletions.
4 changes: 1 addition & 3 deletions lib/Oy.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ exports['default'] = {
},

renderTemplate: function renderTemplate(options, generateCustomTemplate) {
var rawHTML = generateCustomTemplate ? generateCustomTemplate(options) : _utilsHTML42['default'].generateDefaultTemplate(options);

return _utilsHTML42['default'].replaceWhitelistedAttributes(rawHTML);
return generateCustomTemplate ? generateCustomTemplate(options) : _utilsHTML42['default'].generateDefaultTemplate(options);
}
};
module.exports = exports['default'];
10 changes: 10 additions & 0 deletions lib/__tests__/Oy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ describe('Oy', function () {
expect(_Oy2['default'].renderTemplate).toBeDefined();
});

it('should render data-background in the body', function () {
var rawHTML = _Oy2['default'].renderTemplate({
title: 'Foo bar',
bodyContent: '<h1>Foobar data-background</h1>',
previewText: 'Baz qux'
});

expect(rawHTML.indexOf('<h1>Foobar data-background</h1>') !== -1).toEqual(true);
});

it('should use provided base template generator by default', function () {
var shouldThrow = function shouldThrow() {
_Oy2['default'].renderTemplate({
Expand Down
5 changes: 3 additions & 2 deletions lib/components/OyImg.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ exports['default'] = _react2['default'].createClass({

render: function render() {
return _react2['default'].createElement('img', _extends({}, this.props, {
'data-border': this.props.border,
'data-align': this.props.align }));
is: true,
border: this.props.border,
align: this.props.align }));
}
});
module.exports = exports['default'];
9 changes: 5 additions & 4 deletions lib/components/OyTD.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ exports['default'] = _react2['default'].createClass({
return _react2['default'].createElement(
'td',
_extends({}, this.props, {
'data-align': this.props.align,
'data-valign': this.props.vAlign,
'data-background': this.props.background,
'data-bgcolor': this.props.bgColor }),
is: true,
align: this.props.align,
valign: this.props.vAlign,
background: this.props.background,
bgcolor: this.props.bgColor }),
this.props.children
);
}
Expand Down
7 changes: 4 additions & 3 deletions lib/components/OyTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ exports['default'] = _react2['default'].createClass({
return _react2['default'].createElement(
'table',
_extends({}, this.props, {
'data-align': this.props.align,
'data-bgcolor': this.props.bgColor,
'data-valign': this.props.vAlign }),
is: true,
align: this.props.align,
bgcolor: this.props.bgColor,
valign: this.props.vAlign }),
this.props.children
);
}
Expand Down
20 changes: 0 additions & 20 deletions lib/utils/HTML4.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,7 @@
Object.defineProperty(exports, '__esModule', {
value: true
});
var attributeWhitelist = [{
regex: /data-align/g,
replacement: 'align'
}, {
regex: /data-valign/g,
replacement: 'valign'
}, {
regex: /data-background/g,
replacement: 'background'
}, {
regex: /data-bgcolor/g,
replacement: 'bgcolor'
}];

exports['default'] = {
replaceWhitelistedAttributes: function replaceWhitelistedAttributes(html) {
return attributeWhitelist.reduce(function (previousHTML, attribute) {
return previousHTML.replace(attribute.regex, attribute.replacement);
}, html);
},

generateDefaultTemplate: function generateDefaultTemplate(_ref) {
var title = _ref.title;
var bodyContent = _ref.bodyContent;
Expand Down
8 changes: 3 additions & 5 deletions src/Oy.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ export default {
},

renderTemplate: (options, generateCustomTemplate) => {
const rawHTML = (
generateCustomTemplate ? generateCustomTemplate(options) : HTML4.generateDefaultTemplate(options)
);

return HTML4.replaceWhitelistedAttributes(rawHTML);
return generateCustomTemplate ? (
generateCustomTemplate(options)
) : HTML4.generateDefaultTemplate(options);
}
};
11 changes: 11 additions & 0 deletions src/__tests__/Oy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ describe('Oy', function() {
expect(Oy.renderTemplate).toBeDefined();
});

it('should render data-background in the body', function() {
const rawHTML = Oy.renderTemplate({
title: 'Foo bar',
bodyContent: '<h1>Foobar data-background</h1>',
previewText: 'Baz qux',
});

expect(rawHTML.indexOf('<h1>Foobar data-background</h1>') !== -1)
.toEqual(true);
});

it('should use provided base template generator by default', function() {
const shouldThrow = () => {
Oy.renderTemplate({
Expand Down
5 changes: 3 additions & 2 deletions src/components/OyImg.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ export default React.createClass({
render: function() {
return (
<img {...this.props}
data-border={this.props.border}
data-align={this.props.align} />
is
border={this.props.border}
align={this.props.align} />
);
}
});
9 changes: 5 additions & 4 deletions src/components/OyTD.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ export default React.createClass({
render: function() {
return (
<td {...this.props}
data-align={this.props.align}
data-valign={this.props.vAlign}
data-background={this.props.background}
data-bgcolor={this.props.bgColor}>
is
align={this.props.align}
valign={this.props.vAlign}
background={this.props.background}
bgcolor={this.props.bgColor}>
{this.props.children}
</td>
);
Expand Down
7 changes: 4 additions & 3 deletions src/components/OyTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ export default React.createClass({
render: function() {
return (
<table {...this.props}
data-align={this.props.align}
data-bgcolor={this.props.bgColor}
data-valign={this.props.vAlign}>
is
align={this.props.align}
bgcolor={this.props.bgColor}
valign={this.props.vAlign}>
{this.props.children}
</table>
);
Expand Down
26 changes: 0 additions & 26 deletions src/utils/HTML4.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,4 @@
const attributeWhitelist = [
{
regex: /data-align/g,
replacement: 'align'
},
{
regex: /data-valign/g,
replacement: 'valign'
},
{
regex: /data-background/g,
replacement: 'background'
},
{
regex: /data-bgcolor/g,
replacement: 'bgcolor'
}
];


export default {
replaceWhitelistedAttributes: (html) => {
return attributeWhitelist.reduce((previousHTML, attribute) => {
return previousHTML.replace(attribute.regex, attribute.replacement)
}, html);
},

generateDefaultTemplate: ({title, bodyContent, previewText, headCSS=''}) => {
if (!title) {
throw new Error('`title` is a required option for `renderTemplate`');
Expand Down

0 comments on commit f0ab93b

Please sign in to comment.