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

Make sure event handlers are functions #126

Merged
merged 2 commits into from
Sep 16, 2021
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master/Unreleased

- Make sure event handlers, f.ex. "onclick", are functions [#126](https://github.com/aknuds1/html-to-react/pull/126) ([aknuds1](https://github.com/aknuds1))

## [v1.4.6](https://github.com/aknuds1/html-to-react/tree/v1.4.6)

- Handle boolean attributes differing from React's representation [\#132](https://github.com/aknuds1/html-to-react/pull/132) ([aknuds1](https://github.com/aknuds1))
Expand Down
3 changes: 3 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const camelCase = require('lodash.camelcase');
const toPairs = require('ramda/src/toPairs');
const reduce = require('ramda/src/reduce');
const startsWith = require('ramda/src/startsWith');
const React = require('react');
const includes = require('ramda/src/includes');
const camelCaseAttrMap = require('./camel-case-attribute-names');
Expand Down Expand Up @@ -74,6 +75,8 @@ function createElement(node, index, data, children) {
key = 'className';
} else if (key === 'for') {
key = 'htmlFor';
} else if (startsWith('on', key)) {
value = Function(value);
}

if (includes(key, booleanAttrs) && (value || '') === '') {
Expand Down
5 changes: 3 additions & 2 deletions test/html-to-react-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,12 @@ describe('Html2React', () => {
});

it('should handle onclick attributes', function () {
const htmlInput = '<button onclick="alert(\'hello!\')">Hello</button>';
const htmlInput = `<button onclick="alert('hello!')">Hello</button>`;

const reactElem = parser.parse(htmlInput);

assert.strictEqual(reactElem.props.onClick, 'alert(\'hello!\')');
assert.strictEqual(typeof reactElem.props.onClick, 'function');
assert.strictEqual(String(reactElem.props.onClick), String(Function(`alert('hello!')`)));
});

it('should handle inputs with empty value attribute', function () {
Expand Down