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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert “value” to “defaultValue” for editable fields #15

Merged
merged 1 commit into from
Jan 8, 2015
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/htmltojsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ var NODE_TYPE = {
TEXT: 3,
COMMENT: 8
};

var ATTRIBUTE_MAPPING = {
'for': 'htmlFor',
'class': 'className'
};

var ELEMENT_ATTRIBUTE_MAPPING = {
'input': {
'checked': 'defaultChecked',
'value': 'defaultValue'
}
};

/**
* Repeats a string a certain number of times.
* Also: the future is bright and consists of native string repetition:
Expand Down Expand Up @@ -391,7 +399,12 @@ HTMLtoJSX.prototype = {
case 'style':
return this._getStyleAttribute(attribute.value);
default:
var name = ATTRIBUTE_MAPPING[attribute.name] || attribute.name;
var tagName = node.tagName.toLowerCase();
var name =
(ELEMENT_ATTRIBUTE_MAPPING[tagName] &&
ELEMENT_ATTRIBUTE_MAPPING[tagName][attribute.name]) ||
ATTRIBUTE_MAPPING[attribute.name] ||
attribute.name;
var result = name;

// Numeric values should be output as {123} not "123"
Expand Down
18 changes: 18 additions & 0 deletions test/htmltojsx-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ describe('htmltojsx', function() {
].join('\n'));
});

it('should set <input> "value" to "defaultValue" to allow input editing', function() {
var converter = new HTMLtoJSX({ createClass: false });
expect(converter.convert('<input value="Darth Vader">').trim())
.toBe('<input defaultValue="Darth Vader" />');
});

it('should not set "value" to "defaultValue" for non-<input> elements', function() {
var converter = new HTMLtoJSX({ createClass: false });
expect(converter.convert('<select><option value="Hans"></select>').trim())
.toBe('<select><option value="Hans" /></select>');
});

it('should set <input> "checked" to "defaultChecked" to allow box checking', function() {
var converter = new HTMLtoJSX({ createClass: false });
expect(converter.convert('<input type="checkbox" checked>').trim())
.toBe('<input type="checkbox" defaultChecked />');
});

describe('Attribute transformations', function() {
it('should convert basic "style" attributes', function() {
var converter = new HTMLtoJSX({ createClass: false });
Expand Down