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

Select media through iframe boundary #14981

Merged
merged 5 commits into from May 4, 2018
Merged

Select media through iframe boundary #14981

merged 5 commits into from May 4, 2018

Conversation

calebcordry
Copy link
Member

In amp-story we want to make sure that any media existing inside of a friendly-iframe also gets added to themedia-pool and managed by the story runtime.

@calebcordry
Copy link
Member Author

@gmajoulet I tested this manually, but spent a long time trying to write a unit test with no success 😭. Let me know if you have any ideas.

@cmhokej
Copy link

cmhokej commented May 1, 2018

Ok

if (iframeDoc) {
mediaSet = Array.from(mediaSet);
const fieMedia = scopedQuerySelectorAll(iframeDoc, PAGE_MEDIA_SELECTOR);
fieMedia.forEach(el => mediaSet.push(el));
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: maybe using the ES6 syntax?

const fieMedia = scopedQuerySelectorAll(iframeDoc, PAGE_MEDIA_SELECTOR);
mediaArray = [...Array.from(mediaSet), ...fieMedia]

And since you're making it an array, we could replace the line below to mediaArray.map(...)

Copy link
Member Author

Choose a reason for hiding this comment

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

We aren’t currently allowing the spread operator 😕

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh... Maybe a good old mediaArray = Array.from(mediaSet).concat(fieMedia) then?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah this is also weird because you can't concat a nodeList. It just pushes the whole nodeList into the end of the array (I only know this because I tried it yesterday). I will change this around a bit to always return an array as suggested below anyway.

Copy link
Member Author

Choose a reason for hiding this comment

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

talked offline. Changed to concat for readability.

@@ -328,7 +336,16 @@ export class AmpStoryPage extends AMP.BaseElement {
* @private
*/
getAllMedia_() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this method should always return an array, and not sometimes an array, sometimes a NodeList

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we update getAllVideos_ too? It's used to know if there's a video loading, to maybe display the loading spinner indicator.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed to always return same type. Updated getAllVideos_ with similar logic.

@@ -324,21 +331,45 @@ export class AmpStoryPage extends AMP.BaseElement {

/**
* Gets all media elements on this page.
* @return {!NodeList<!Element>}
* @return {Array<!Element>}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I think you need the !?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it now always returns an array? Sometimes it is an empty array and sometimes its not.

Copy link
Member Author

Choose a reason for hiding this comment

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

Talked offline. @gmajoulet was correct, changed to !Array<?Element>

*/
const PAGE_MEDIA_SELECTOR = 'amp-audio, amp-video, amp-img, amp-anim';
const SELECTORS = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Selectors now that it's an enum

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

*/
getMediaBySelector_(selector) {
const iframe = this.element.querySelector('iframe');
const iframeDoc = iframe && iframe.contentDocument;
Copy link
Contributor

Choose a reason for hiding this comment

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

did you check if this line will generate console error in case of xorigin iframe?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes it returns null

@calebcordry calebcordry merged commit 560cfa0 into ampproject:master May 4, 2018
@calebcordry calebcordry deleted the iframe branch May 4, 2018 22:25
noranazmy pushed a commit to noranazmy/amphtml that referenced this pull request May 10, 2018
* change selector

* always return array

* refactor

* fix types

* fix types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants