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
Conversation
@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. |
Ok |
if (iframeDoc) { | ||
mediaSet = Array.from(mediaSet); | ||
const fieMedia = scopedQuerySelectorAll(iframeDoc, PAGE_MEDIA_SELECTOR); | ||
fieMedia.forEach(el => mediaSet.push(el)); |
There was a problem hiding this comment.
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(...)
There was a problem hiding this comment.
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 😕
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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_() { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>} |
There was a problem hiding this comment.
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 !
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 = { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes it returns null
* change selector * always return array * refactor * fix types * fix types
In
amp-story
we want to make sure that any media existing inside of afriendly-iframe
also gets added to themedia-pool
and managed by the story runtime.