From dd872998de57a38b9cb114ade474b06a206f0fb6 Mon Sep 17 00:00:00 2001 From: tsuyoshiwada Date: Tue, 20 Jun 2017 17:47:52 +0900 Subject: [PATCH 1/4] Remove unnecessary whitespaces --- test/htmltojsx-test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/htmltojsx-test.js b/test/htmltojsx-test.js index 75d48d7..cf7fbc5 100644 --- a/test/htmltojsx-test.js +++ b/test/htmltojsx-test.js @@ -142,19 +142,19 @@ describe('htmltojsx', function() { expect(converter.convert('
Test
').trim()) .toBe('
Test
'); }); - + it('should convert uppercase vendor-prefix "style" attributes', function() { var converter = new HTMLtoJSX({ createClass: false }); expect(converter.convert('
Test
').trim()) .toBe('
Test
'); }); - + 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('
Test
').trim()) .toBe('
Test
'); }); - + it('should convert -ms- prefix "style" attributes', function() { var converter = new HTMLtoJSX({ createClass: false }); expect(converter.convert('
Test
').trim()) @@ -170,7 +170,7 @@ describe('htmltojsx', function() { it('should convert uppercase "style" attributes', function() { var converter = new HTMLtoJSX({ createClass: false }); expect(converter.convert('
Test
').trim()) - .toBe('
Test
'); + .toBe('
Test
'); }); it('should convert "class" attribute', function() { From 223737120d3a558332eaa1757b9d990cb3e8dbd3 Mon Sep 17 00:00:00 2001 From: tsuyoshiwada Date: Tue, 20 Jun 2017 17:49:23 +0900 Subject: [PATCH 2/4] Add test for convert of svg attributes --- test/htmltojsx-test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/htmltojsx-test.js b/test/htmltojsx-test.js index cf7fbc5..f1f6cbc 100644 --- a/test/htmltojsx-test.js +++ b/test/htmltojsx-test.js @@ -232,6 +232,12 @@ describe('htmltojsx', function() { expect(converter.convert('').trim()) .toBe(''); }); + + it('should convert SVG attributes', function() { + var converter = new HTMLtoJSX({ createClass: false }); + expect(converter.convert('').trim()) + .toBe(''); + }); }); describe('special tags', function() { From 1b1c8b1227a9c606757da4e4a0876cdf02a677de Mon Sep 17 00:00:00 2001 From: tsuyoshiwada Date: Tue, 20 Jun 2017 18:08:50 +0900 Subject: [PATCH 3/4] Refactor object iteration --- src/htmltojsx.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/htmltojsx.js b/src/htmltojsx.js index c48b778..d6c5559 100644 --- a/src/htmltojsx.js +++ b/src/htmltojsx.js @@ -37,6 +37,21 @@ var ELEMENT_ATTRIBUTE_MAPPING = { var HTMLDOMPropertyConfig = require('react-dom/lib/HTMLDOMPropertyConfig'); +/** + * 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]); + } + } +} + // 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) { @@ -562,12 +577,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) { + output.push(this.toJSXKey(key) + ': ' + this.toJSXValue(value)); + }, this); return output.join(', '); }, From 5f6bc07ff20cd77e57591d288145d2b98c0615b5 Mon Sep 17 00:00:00 2001 From: tsuyoshiwada Date: Tue, 20 Jun 2017 18:09:58 +0900 Subject: [PATCH 4/4] Add SVG attributes conversion --- src/htmltojsx.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/htmltojsx.js b/src/htmltojsx.js index d6c5559..d16b787 100644 --- a/src/htmltojsx.js +++ b/src/htmltojsx.js @@ -36,6 +36,7 @@ var ELEMENT_ATTRIBUTE_MAPPING = { }; var HTMLDOMPropertyConfig = require('react-dom/lib/HTMLDOMPropertyConfig'); +var SVGDOMPropertyConfig = require('react-dom/lib/SVGDOMPropertyConfig'); /** * Iterates over elements of object invokes iteratee for each element @@ -54,17 +55,18 @@ function eachObj(obj, iteratee, context) { // 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; - } - - var mapFrom = HTMLDOMPropertyConfig.DOMAttributeNames[propname] || propname.toLowerCase(); +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: