]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/wp-embed.js
WordPress 4.5
[autoinstalls/wordpress.git] / wp-includes / js / wp-embed.js
1 /**
2  * WordPress inline HTML embed
3  *
4  * @since 4.4.0
5  *
6  * This file cannot have ampersands in it. This is to ensure
7  * it can be embedded in older versions of WordPress.
8  * See https://core.trac.wordpress.org/changeset/35708.
9  */
10 (function ( window, document ) {
11         'use strict';
12
13         var supportedBrowser = false,
14                 loaded = false;
15
16                 if ( document.querySelector ) {
17                         if ( window.addEventListener ) {
18                                 supportedBrowser = true;
19                         }
20                 }
21
22         window.wp = window.wp || {};
23
24         if ( !! window.wp.receiveEmbedMessage ) {
25                 return;
26         }
27
28         window.wp.receiveEmbedMessage = function( e ) {
29                 var data = e.data;
30                 if ( ! ( data.secret || data.message || data.value ) ) {
31                         return;
32                 }
33
34                 if ( /[^a-zA-Z0-9]/.test( data.secret ) ) {
35                         return;
36                 }
37
38                 var iframes = document.querySelectorAll( 'iframe[data-secret="' + data.secret + '"]' ),
39                         blockquotes = document.querySelectorAll( 'blockquote[data-secret="' + data.secret + '"]' ),
40                         i, source, height, sourceURL, targetURL;
41
42                 for ( i = 0; i < blockquotes.length; i++ ) {
43                         blockquotes[ i ].style.display = 'none';
44                 }
45
46                 for ( i = 0; i < iframes.length; i++ ) {
47                         source = iframes[ i ];
48
49                         if ( e.source !== source.contentWindow ) {
50                                 continue;
51                         }
52
53                         source.removeAttribute( 'style' );
54
55                         /* Resize the iframe on request. */
56                         if ( 'height' === data.message ) {
57                                 height = parseInt( data.value, 10 );
58                                 if ( height > 1000 ) {
59                                         height = 1000;
60                                 } else if ( ~~height < 200 ) {
61                                         height = 200;
62                                 }
63
64                                 source.height = height;
65                         }
66
67                         /* Link to a specific URL on request. */
68                         if ( 'link' === data.message ) {
69                                 sourceURL = document.createElement( 'a' );
70                                 targetURL = document.createElement( 'a' );
71
72                                 sourceURL.href = source.getAttribute( 'src' );
73                                 targetURL.href = data.value;
74
75                                 /* Only continue if link hostname matches iframe's hostname. */
76                                 if ( targetURL.host === sourceURL.host ) {
77                                         if ( document.activeElement === source ) {
78                                                 window.top.location.href = data.value;
79                                         }
80                                 }
81                         }
82                 }
83         };
84
85         function onLoad() {
86                 if ( loaded ) {
87                         return;
88                 }
89
90                 loaded = true;
91
92                 var isIE10 = -1 !== navigator.appVersion.indexOf( 'MSIE 10' ),
93                         isIE11 = !!navigator.userAgent.match( /Trident.*rv:11\./ ),
94                         iframes = document.querySelectorAll( 'iframe.wp-embedded-content' ),
95                         iframeClone, i, source, secret;
96
97                 for ( i = 0; i < iframes.length; i++ ) {
98                         source = iframes[ i ];
99
100                         if ( source.getAttribute( 'data-secret' ) ) {
101                                 continue;
102                         }
103
104                         /* Add secret to iframe */
105                         secret = Math.random().toString( 36 ).substr( 2, 10 );
106                         source.src += '#?secret=' + secret;
107                         source.setAttribute( 'data-secret', secret );
108
109                         /* Remove security attribute from iframes in IE10 and IE11. */
110                         if ( ( isIE10 || isIE11 ) ) {
111                                 iframeClone = source.cloneNode( true );
112                                 iframeClone.removeAttribute( 'security' );
113                                 source.parentNode.replaceChild( iframeClone, source );
114                         }
115                 }
116         }
117
118         if ( supportedBrowser ) {
119                 window.addEventListener( 'message', window.wp.receiveEmbedMessage, false );
120                 document.addEventListener( 'DOMContentLoaded', onLoad, false );
121                 window.addEventListener( 'load', onLoad, false );
122         }
123 })( window, document );