Skip to content

Commit

Permalink
Move event normalization to compat
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Mar 11, 2019
1 parent 1104fdb commit ee3b692
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
4 changes: 4 additions & 0 deletions compat/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ function applyEventNormalization({ type, props }) {
props.ondblclick = props[newProps.ondoubleclick];
delete props[newProps.ondoubleclick];
}
if (newProps.onbeforeinput) {
props.onbeforeinput = props[newProps.onbeforeinput];
delete props[newProps.onbeforeinput];
}
// for *textual inputs* (incl textarea), normalize `onChange` -> `onInput`:
if (newProps.onchange && (type==='textarea' || (type.toLowerCase()==='input' && !/^fil|che|rad/i.test(props.type)))) {
let normalized = newProps.oninput || 'oninput';
Expand Down
7 changes: 7 additions & 0 deletions compat/test/browser/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ describe('preact-compat', () => {
expectToBeNormalized(<input {...props} type="text" />, '<input type="text">');

});

it('should normalize beforeinput event listener', () => {
let spy = sinon.spy();
render(<input onBeforeInput={spy} />, scratch);
scratch.firstChild.dispatchEvent(new Event('beforeinput'));
expect(spy).to.be.calledOnce;
});
});

describe('Component', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/diff/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function setProperty(dom, name, value, oldValue, isSvg) {
else if (name[0]==='o' && name[1]==='n') {
let useCapture = name !== (name=name.replace(/Capture$/, ''));
let nameLower = name.toLowerCase();
name = (nameLower in dom || nameLower=='onbeforeinput' ? nameLower : name).substring(2);
name = (nameLower in dom ? nameLower : name).substring(2);

if (value) {
if (!oldValue) dom.addEventListener(name, eventProxy, useCapture);
Expand Down
7 changes: 0 additions & 7 deletions test/browser/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,6 @@ describe('render()', () => {
expect(scratch.firstChild.spellcheck).to.equal(false);
});

it('should attach beforeinput event listener', () => {
let spy = sinon.spy();
render(<input onBeforeInput={spy} />, scratch);
scratch.firstChild.dispatchEvent(new Event('beforeinput'));
expect(spy).to.be.calledOnce;
});

it('should apply string attributes', () => {
render(<div foo="bar" data-foo="databar" />, scratch);

Expand Down

0 comments on commit ee3b692

Please sign in to comment.