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

🐛Do not select hidden elements in amp-selector #23735

Merged
merged 3 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
77 changes: 57 additions & 20 deletions extensions/amp-selector/0.1/amp-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,12 @@ export class AmpSelector extends AMP.BaseElement {
/**
* Handles keyboard events.
* @param {!Event} event
* @return {!Promise}
* @private
*/
keyDownHandler_(event) {
if (this.element.hasAttribute('disabled')) {
return;
return Promise.resolve();
}
const {key} = event;
switch (key) {
Expand All @@ -493,20 +494,22 @@ export class AmpSelector extends AMP.BaseElement {
case Keys.RIGHT_ARROW: /* fallthrough */
case Keys.DOWN_ARROW:
if (this.kbSelectMode_ != KEYBOARD_SELECT_MODES.NONE) {
this.navigationKeyDownHandler_(event);
return this.navigationKeyDownHandler_(event);
}
return;
return Promise.resolve();
case Keys.ENTER: /* fallthrough */
case Keys.SPACE:
this.selectionKeyDownHandler_(event);
return;
return Promise.resolve();
}
return Promise.resolve();
}

/**
* Handles keyboard navigation events. Should not be called if
* keyboard selection is disabled.
* @param {!Event} event
* @return {!Promise}
* @private
*/
navigationKeyDownHandler_(event) {
Expand Down Expand Up @@ -534,27 +537,37 @@ export class AmpSelector extends AMP.BaseElement {
}

event.preventDefault();

// Make currently selected option unfocusable
this.elements_[this.focusedIndex_].tabIndex = -1;

// Change the focus to the next element in the specified direction.
// The selection should loop around if the user attempts to go one
// past the beginning or end.
this.focusedIndex_ = (this.focusedIndex_ + dir) % this.elements_.length;
if (this.focusedIndex_ < 0) {
this.focusedIndex_ = this.focusedIndex_ + this.elements_.length;
}
return this.getElementsSizes_().then(sizes => {
const originalIndex = this.focusedIndex_;
do {
// Change the focus to the next element in the specified direction.
// The selection should loop around if the user attempts to go one
// past the beginning or end.
this.focusedIndex_ = (this.focusedIndex_ + dir) % this.elements_.length;
if (this.focusedIndex_ < 0) {
this.focusedIndex_ = this.focusedIndex_ + this.elements_.length;
}
} while (
isElementHidden(
this.elements_[this.focusedIndex_],
sizes[this.focusedIndex_]
) &&
this.focusedIndex_ != originalIndex
);

// Focus newly selected option
const newSelectedOption = this.elements_[this.focusedIndex_];
newSelectedOption.tabIndex = 0;
tryFocus(newSelectedOption);
// Focus newly selected option
const newSelectedOption = this.elements_[this.focusedIndex_];
newSelectedOption.tabIndex = 0;
tryFocus(newSelectedOption);

const focusedOption = this.elements_[this.focusedIndex_];
if (this.kbSelectMode_ == KEYBOARD_SELECT_MODES.SELECT) {
this.onOptionPicked_(focusedOption);
}
const focusedOption = this.elements_[this.focusedIndex_];
if (this.kbSelectMode_ == KEYBOARD_SELECT_MODES.SELECT) {
this.onOptionPicked_(focusedOption);
}
});
}

/**
Expand Down Expand Up @@ -633,6 +646,30 @@ export class AmpSelector extends AMP.BaseElement {
getSelectedElementsForTesting() {
return this.selectedElements_;
}

/**
* Cache the rects of each of the elements.
* @return {!Promise<!Array<!ClientRect>>}
* @private
*/
getElementsSizes_() {
return this.measureElement(() => {
return this.elements_.map(element =>
element./*OK*/ getBoundingClientRect()
);
});
}
}

/**
* Detect if an element is hidden.
* @param {!Element} element
* @param {!ClientRect} rect
* @return {boolean}
*/
function isElementHidden(element, rect) {
const {width, height} = rect;
return element.hidden || width == 0 || height == 0;
}

AMP.extension(TAG, '0.1', AMP => {
Expand Down
107 changes: 65 additions & 42 deletions extensions/amp-selector/0.1/test/test-amp-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describes.realWin(
const numberOfChildren = config.count || 3;
let selectedCount = config.selectedCount || 0;
let disabledCount = config.disabledCount || 0;

const rectArray = [];
for (let i = 0; i < numberOfChildren; i++) {
const child = win.document.createElement('div');
child.setAttribute('width', '10');
Expand All @@ -75,14 +75,17 @@ describes.realWin(
disabledCount--;
}
}

rectArray.push({width: 10, height: 10});
const childAttributes = options.optionAttributes || {};
Object.keys(childAttributes).forEach(key => {
child.setAttribute(key, childAttributes[key]);
});

ampSelector.appendChild(child);
}
sandbox
.stub(ampSelector.implementation_, 'getElementsSizes_')
.resolves(rectArray);
win.document.body.appendChild(ampSelector);
return ampSelector;
}
Expand All @@ -94,7 +97,7 @@ describes.realWin(
preventDefault: () => {},
target: opt_target,
};
impl.keyDownHandler_(event);
return impl.keyDownHandler_(event);
}

it('should build properly', function*() {
Expand Down Expand Up @@ -1144,14 +1147,18 @@ describes.realWin(
expect(ampSelector.children[0].tabIndex).to.equal(0);
expect(ampSelector.children[1].tabIndex).to.equal(-1);
expect(ampSelector.children[2].tabIndex).to.equal(-1);
keyPress(ampSelector, Keys.LEFT_ARROW);
expect(ampSelector.children[0].tabIndex).to.equal(-1);
expect(ampSelector.children[1].tabIndex).to.equal(-1);
expect(ampSelector.children[2].tabIndex).to.equal(0);
keyPress(ampSelector, Keys.RIGHT_ARROW);
expect(ampSelector.children[0].tabIndex).to.equal(0);
expect(ampSelector.children[1].tabIndex).to.equal(-1);
expect(ampSelector.children[2].tabIndex).to.equal(-1);
return keyPress(ampSelector, Keys.LEFT_ARROW)
.then(() => {
expect(ampSelector.children[0].tabIndex).to.equal(-1);
expect(ampSelector.children[1].tabIndex).to.equal(-1);
expect(ampSelector.children[2].tabIndex).to.equal(0);
return keyPress(ampSelector, Keys.RIGHT_ARROW);
})
.then(() => {
expect(ampSelector.children[0].tabIndex).to.equal(0);
expect(ampSelector.children[1].tabIndex).to.equal(-1);
expect(ampSelector.children[2].tabIndex).to.equal(-1);
});
}
);

Expand Down Expand Up @@ -1211,14 +1218,24 @@ describes.realWin(
expect(ampSelector.children[0].hasAttribute('selected')).to.be.false;
expect(ampSelector.children[1].hasAttribute('selected')).to.be.false;
expect(ampSelector.children[2].hasAttribute('selected')).to.be.false;
keyPress(ampSelector, Keys.DOWN_ARROW);
expect(ampSelector.children[0].hasAttribute('selected')).to.be.false;
expect(ampSelector.children[1].hasAttribute('selected')).to.be.true;
expect(ampSelector.children[2].hasAttribute('selected')).to.be.false;
keyPress(ampSelector, Keys.UP_ARROW);
expect(ampSelector.children[0].hasAttribute('selected')).to.be.true;
expect(ampSelector.children[1].hasAttribute('selected')).to.be.false;
expect(ampSelector.children[2].hasAttribute('selected')).to.be.false;
return keyPress(ampSelector, Keys.DOWN_ARROW)
.then(() => {
expect(ampSelector.children[0].hasAttribute('selected')).to.be
.false;
expect(ampSelector.children[1].hasAttribute('selected')).to.be
.true;
expect(ampSelector.children[2].hasAttribute('selected')).to.be
.false;
return keyPress(ampSelector, Keys.UP_ARROW);
})
.then(() => {
expect(ampSelector.children[0].hasAttribute('selected')).to.be
.true;
expect(ampSelector.children[1].hasAttribute('selected')).to.be
.false;
expect(ampSelector.children[2].hasAttribute('selected')).to.be
.false;
});
});
});

Expand Down Expand Up @@ -1306,29 +1323,35 @@ describes.realWin(
expect(ampSelector.children[2].tabIndex).to.equal(-1);

// Note that the newly added third child is ignored.
keyPress(ampSelector, Keys.LEFT_ARROW);
expect(ampSelector.children[0].tabIndex).to.equal(-1);
expect(ampSelector.children[1].tabIndex).to.equal(0);
expect(ampSelector.children[2].tabIndex).to.equal(-1);

const e = new CustomEvent(AmpEvents.DOM_UPDATE, {bubbles: true});
newChild.dispatchEvent(e);

// `newChild` should be focused since it has the 'selected' attribute.
expect(ampSelector.children[0].tabIndex).to.equal(-1);
expect(ampSelector.children[1].tabIndex).to.equal(-1);
expect(ampSelector.children[2].tabIndex).to.equal(0);

// Tabbing between children now works for `newChild`.
keyPress(ampSelector, Keys.LEFT_ARROW);
expect(ampSelector.children[0].tabIndex).to.equal(-1);
expect(ampSelector.children[1].tabIndex).to.equal(0);
expect(ampSelector.children[2].tabIndex).to.equal(-1);

keyPress(ampSelector, Keys.RIGHT_ARROW);
expect(ampSelector.children[0].tabIndex).to.equal(-1);
expect(ampSelector.children[1].tabIndex).to.equal(-1);
expect(ampSelector.children[2].tabIndex).to.equal(0);
keyPress(ampSelector, Keys.LEFT_ARROW)
.then(() => {
expect(ampSelector.children[0].tabIndex).to.equal(-1);
expect(ampSelector.children[1].tabIndex).to.equal(0);
expect(ampSelector.children[2].tabIndex).to.equal(-1);

const e = new CustomEvent(AmpEvents.DOM_UPDATE, {bubbles: true});
newChild.dispatchEvent(e);

// `newChild` should be focused since it has the 'selected' attribute.
expect(ampSelector.children[0].tabIndex).to.equal(-1);
expect(ampSelector.children[1].tabIndex).to.equal(-1);
expect(ampSelector.children[2].tabIndex).to.equal(0);

// Tabbing between children now works for `newChild`.
return keyPress(ampSelector, Keys.LEFT_ARROW);
})
.then(() => {
expect(ampSelector.children[0].tabIndex).to.equal(-1);
expect(ampSelector.children[1].tabIndex).to.equal(0);
expect(ampSelector.children[2].tabIndex).to.equal(-1);

return keyPress(ampSelector, Keys.RIGHT_ARROW);
})
.then(() => {
expect(ampSelector.children[0].tabIndex).to.equal(-1);
expect(ampSelector.children[1].tabIndex).to.equal(-1);
expect(ampSelector.children[2].tabIndex).to.equal(0);
});
});
});
});
Expand Down