Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature for SVG attributes conversion #136

Merged
merged 4 commits into from Aug 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 28 additions & 14 deletions src/htmltojsx.js
Expand Up @@ -36,20 +36,37 @@ var ELEMENT_ATTRIBUTE_MAPPING = {
};

var HTMLDOMPropertyConfig = require('react-dom/lib/HTMLDOMPropertyConfig');
var SVGDOMPropertyConfig = require('react-dom/lib/SVGDOMPropertyConfig');

// Populate property map with ReactJS's attribute and property mappings
// TODO handle/use .Properties value eg: MUST_USE_PROPERTY is not HTML attr
for (var propname in HTMLDOMPropertyConfig.Properties) {
if (!HTMLDOMPropertyConfig.Properties.hasOwnProperty(propname)) {
continue;
/**
* Iterates over elements of object invokes iteratee for each element
*
* @param {object} obj Collection object
* @param {function} iteratee Callback function called in iterative processing
* @param {any} context This arg (aka Context)
*/
function eachObj(obj, iteratee, context) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
iteratee.call(context || obj, key, obj[key]);
}
}
}

var mapFrom = HTMLDOMPropertyConfig.DOMAttributeNames[propname] || propname.toLowerCase();
// Populate property map with ReactJS's attribute and property mappings
// TODO handle/use .Properties value eg: MUST_USE_PROPERTY is not HTML attr
function mappingAttributesFromReactConfig(config) {
eachObj(config.Properties, function(propname) {
var mapFrom = config.DOMAttributeNames[propname] || propname.toLowerCase();

if (!ATTRIBUTE_MAPPING[mapFrom])
ATTRIBUTE_MAPPING[mapFrom] = propname;
if (!ATTRIBUTE_MAPPING[mapFrom])
ATTRIBUTE_MAPPING[mapFrom] = propname;
});
}

mappingAttributesFromReactConfig(HTMLDOMPropertyConfig);
mappingAttributesFromReactConfig(SVGDOMPropertyConfig);

/**
* Repeats a string a certain number of times.
* Also: the future is bright and consists of native string repetition:
Expand Down Expand Up @@ -562,12 +579,9 @@ StyleParser.prototype = {
*/
toJSXString: function() {
var output = [];
for (var key in this.styles) {
if (!this.styles.hasOwnProperty(key)) {
continue;
}
output.push(this.toJSXKey(key) + ': ' + this.toJSXValue(this.styles[key]));
}
eachObj(this.styles, function(key, value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have refactored the iteration of the object using the newly created eachObj helper.
I'm sorry I made a confusing change... 馃槩

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's totally fine, don't worry @tsuyoshiwada 馃槃 I was just wondering about why we couldn't just continue to use a regular for loop here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! It was worried whether it became a meddle, but I am relieved 馃槂

output.push(this.toJSXKey(key) + ': ' + this.toJSXValue(value));
}, this);
return output.join(', ');
},

Expand Down
14 changes: 10 additions & 4 deletions test/htmltojsx-test.js
Expand Up @@ -142,19 +142,19 @@ describe('htmltojsx', function() {
expect(converter.convert('<div style="-moz-hyphens: auto; -webkit-hyphens: auto">Test</div>').trim())
.toBe('<div style={{MozHyphens: \'auto\', WebkitHyphens: \'auto\'}}>Test</div>');
});

it('should convert uppercase vendor-prefix "style" attributes', function() {
var converter = new HTMLtoJSX({ createClass: false });
expect(converter.convert('<div style="-MOZ-HYPHENS: auto; -WEBKIT-HYPHENS: auto">Test</div>').trim())
.toBe('<div style={{MozHyphens: \'auto\', WebkitHyphens: \'auto\'}}>Test</div>');
});

it('should convert "style" attributes with vendor prefix-like strings in the middle and mixed case', function() {
var converter = new HTMLtoJSX({ createClass: false });
expect(converter.convert('<div style="myclass-MOZ-HYPHENS: auto; myclass-WEBKIT-HYPHENS: auto">Test</div>').trim())
.toBe('<div style={{myclassMozHyphens: \'auto\', myclassWebkitHyphens: \'auto\'}}>Test</div>');
});

it('should convert -ms- prefix "style" attributes', function() {
var converter = new HTMLtoJSX({ createClass: false });
expect(converter.convert('<div style="-ms-hyphens: auto">Test</div>').trim())
Expand All @@ -170,7 +170,7 @@ describe('htmltojsx', function() {
it('should convert uppercase "style" attributes', function() {
var converter = new HTMLtoJSX({ createClass: false });
expect(converter.convert('<div style="TEXT-ALIGN: center">Test</div>').trim())
.toBe('<div style={{textAlign: \'center\'}}>Test</div>');
.toBe('<div style={{textAlign: \'center\'}}>Test</div>');
});

it('should convert "class" attribute', function() {
Expand Down Expand Up @@ -232,6 +232,12 @@ describe('htmltojsx', function() {
expect(converter.convert('<input type="checkbox" checked>').trim())
.toBe('<input type="checkbox" defaultChecked />');
});

it('should convert SVG attributes', function() {
var converter = new HTMLtoJSX({ createClass: false });
expect(converter.convert('<svg height="100" width="100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" fill-rule="evenodd"/></svg>').trim())
.toBe('<svg height={100} width={100}><circle cx={50} cy={50} r={40} stroke="black" strokeWidth={3} fill="red" fillRule="evenodd" /></svg>');
});
});

describe('special tags', function() {
Expand Down