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

Allow for functional links in amp-accordion headers #9335

Merged
merged 21 commits into from
May 26, 2017
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 17 additions & 4 deletions extensions/amp-accordion/0.1/amp-accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ class AmpAccordion extends AMP.BaseElement {
header.setAttribute('tabindex', 0);
}
this.headers_.push(header);
header.addEventListener('click',
this.onHeaderPicked_.bind(this));
header.addEventListener('click', this.clickHandler_.bind(this));
header.addEventListener('keydown', this.keyDownHandler_.bind(this));
});
});
Expand Down Expand Up @@ -177,8 +176,22 @@ class AmpAccordion extends AMP.BaseElement {
}

/**
* Handles key presses on an accordion expand/collapse its content or
* move focus to previous/next header.
* Handles clicks on an accordion header to expand/collapse its content.
*/
clickHandler_(event) {
// Need to support clicks on any children of the header except
// for on links, which should not have their default behavior
// overidden.
const target = dev().assertElement(event.target);
if (target.tagName != 'A') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we support anything else here besides anchor tags?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this may not work for image links (<a href><amp-img>...). I think we need to check all the ancestors of target up to the header element. ancestorElements(child, predicate) should help find it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

// Don't use clicks on links in header to expand/collapse.
this.onHeaderPicked_(event);
}
}

/**
* Handles key presses on an accordion header to expand/collapse its content
* or move focus to previous/next header.
* @param {!Event} event keydown event.
*/
keyDownHandler_(event) {
Expand Down
21 changes: 19 additions & 2 deletions extensions/amp-accordion/0.1/test/test-amp-accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describes.sandboxed('amp-accordion', {}, () => {
obj.ampAccordion.implementation_.onHeaderPicked_(clickEvent);
expect(header.parentNode.hasAttribute('expanded')).to.be.true;
expect(header.getAttribute('aria-expanded')).to.equal('true');
expect(clickEvent.preventDefault.called).to.be.true;
expect(clickEvent.preventDefault).to.have.been.called;
});
});

Expand All @@ -99,7 +99,24 @@ describes.sandboxed('amp-accordion', {}, () => {
obj.ampAccordion.implementation_.onHeaderPicked_(clickEvent);
expect(headerElements[1].parentNode.hasAttribute('expanded')).to.be.false;
expect(headerElements[1].getAttribute('aria-expanded')).to.equal('false');
expect(clickEvent.preventDefault.called).to.be.true;
expect(clickEvent.preventDefault).to.have.been.called;
});
});

it('should allow for clickable links in header', () => {
return getAmpAccordion().then(obj => {
const iframe = obj.iframe;
const headerElements = iframe.doc.querySelectorAll(
'section > *:first-child');
const a = iframe.doc.createElement('a');
headerElements[0].appendChild(a);
const aClickEvent = {
target: a,
currentTarget: headerElements[0],
preventDefault: sandbox.spy(),
};
obj.ampAccordion.implementation_.clickHandler_(aClickEvent);
expect(aClickEvent.preventDefault).to.not.have.been.called;
});
});

Expand Down
4 changes: 2 additions & 2 deletions test/manual/amp-accordion.amp.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ <h2 tabindex="0">Section 1</h2>
</div>
</section>
<section expanded>
<h2 tabindex="0">Section 2</h2>
<h2 tabindex="0">Section 2. Move focus here and press enter/space to open.</h2>
<div>
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus
</div>
</section>
<section>
<h2 tabindex="0">Section 3</h2>
<h2 tabindex="0">Section 3. <a href="https://google.com" target="_blank">Clicking this link should not expand/collapse this section.</a></h2>
<div>
<amp-img id="img1" srcset="https://lh4.googleusercontent.com/kDqWeHQ0Zt-UFeHAlc2ydE9AwK7W-kbMXBqyuNvPNy0mNSGAP7FOVB3_1iAOdbQSsbZByErbehvvSKtnTb1L5GoYreijfKwgYwpP1eLCHyl9Am7BSpwRuBABOAO12PtyPBTirdAadnxOmfq9dH_rsLSTaYGmNz1D5QIwXeWd8UeDNUQ3f-cMgvkq4ePqmKoe9t5ySqqLMZs-v3wTi2pd4jV_CurzcMB76k_b4lyD5w77NUowkSaQfscYVQpkDjo6OgG2nBiKJ2TITWVDu2rvt6NuEoOD9xaHgXuv81OnOjXCokxZd5K6TZiAH-Qm1jTKGMANklXiXt6hbwrN3QPA1mq2FardxGNLj1_oqOpXaqfUuj8LvejFRY6zJMGq0r6S_TEtPvbyulIg4PkKPIaVzi5nVdGrAWWoesWh-ORqmxZ4FIhdbd_Igsdh5AcMETBcZz3l5-IX0hmnyUeT5IOPSGw-p3Esgp_abwWB9-kElEiiHPD4QuQ_swsRu0NSFwfRi_QefnJQJ5UATng6iVP3K0g7uumHcwLtFId0vCeHp4A=w1024-h768-no" width=512 height=344></amp-img>
</div>
Expand Down