Skip to content

Commit

Permalink
Merge pull request #578 from simonihmig/support-shadowdom
Browse files Browse the repository at this point in the history
Support inputs in shadow DOM in disableOnInputFields mode
  • Loading branch information
lukemelia authored Jan 22, 2022
2 parents 6393250 + a4a26e7 commit 047c125
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 7 deletions.
9 changes: 7 additions & 2 deletions addon-test-support/key-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import validMouseButtons from 'ember-keyboard/fixtures/mouse-buttons-array';
import getCmdKey from 'ember-keyboard/utils/get-cmd-key';
import { triggerEvent } from '@ember/test-helpers';

export function keyEvent(keyCombo, type, element = document) {
export function keyEvent(
keyCombo,
type,
element = document,
eventOptions = {}
) {
let keyComboParts = (keyCombo || '').split('+');

let eventProps = keyComboParts.reduce((eventProps, keyComboPart) => {
Expand All @@ -30,5 +35,5 @@ export function keyEvent(keyCombo, type, element = document) {
return eventProps;
}, {});

return triggerEvent(element, type, eventProps);
return triggerEvent(element, type, { ...eventOptions, ...eventProps });
}
4 changes: 2 additions & 2 deletions addon-test-support/test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export function keyDown(keyCombo) {
return keyEvent(keyCombo, 'keydown');
}

export function keyDownWithElement(keyCombo, element) {
return keyEvent(keyCombo, 'keydown', element);
export function keyDownWithElement(keyCombo, element, eventOptions) {
return keyEvent(keyCombo, 'keydown', element, eventOptions);
}

export function keyUp(keyCombo) {
Expand Down
6 changes: 3 additions & 3 deletions addon/services/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ export default class KeyboardService extends Service {
@action
_respond(event) {
if (this._disableOnInput && event.target) {
const tag = event.target.tagName;
const target = event.composedPath()[0] ?? event.target;
const tag = target.tagName;
const isContentEditable =
event.target.getAttribute &&
event.target.getAttribute('contenteditable') != null;
target.getAttribute && target.getAttribute('contenteditable') != null;
if (isContentEditable || tag === 'TEXTAREA' || tag === 'INPUT') {
return;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/acceptance/disable-on-input-fields-config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ module('Acceptance | disableOnInputFields config', function (hooks) {
assert.deepEqual(getValues(), [0, 0, 0], 'responders do not respond');
});

test('test event does not propagate on input field in open shadow DOM', async function (assert) {
assert.expect(1);

await visit('/test-scenario');

const input = this.element
.querySelector('[data-test-shadow-dom]')
.shadowRoot.querySelector('input');

// trigger a "composed" event to make sure the event triggered within the shadow dom is able to traverse the shadow root boundary.
// this is only needed for the synthetic event we create here in tests, "real" events do this by default.
await keyDownWithElement('ArrowRight', input, { composed: true });
assert.deepEqual(getValues(), [0, 0, 0], 'responders do not respond');
});

test('test standard functionality', async function (assert) {
assert.expect(8);

Expand Down
1 change: 1 addition & 0 deletions tests/dummy/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Application from '@ember/application';
import Resolver from 'ember-resolver';
import loadInitializers from 'ember-load-initializers';
import config from 'dummy/config/environment';
import './custom-elements/input-in-open-shadow';

export default class App extends Application {
modulePrefix = config.modulePrefix;
Expand Down
11 changes: 11 additions & 0 deletions tests/dummy/app/custom-elements/input-in-open-shadow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
if (typeof FastBoot === 'undefined') {
class InputInOpenShadow extends HTMLElement {
async connectedCallback() {
const shadowRoot = this.attachShadow({ mode: 'open' });
const input = document.createElement('input');
shadowRoot.appendChild(input);
}
}

customElements.define('input-in-open-shadow', InputInOpenShadow);
}
2 changes: 2 additions & 0 deletions tests/dummy/app/templates/test-scenario/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@

<label for="data-test-input-field">input field</label>
<input id="data-test-input-field" data-test-input-field/>

<input-in-open-shadow data-test-shadow-dom/>

0 comments on commit 047c125

Please sign in to comment.