]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/wp-embed.js
WordPress 4.4
[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.style.display = '';
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                 loaded = true;
90
91                 var isIE10 = -1 !== navigator.appVersion.indexOf( 'MSIE 10' ),
92                         isIE11 = !!navigator.userAgent.match( /Trident.*rv:11\./ ),
93                         iframes = document.querySelectorAll( 'iframe.wp-embedded-content' ),
94                         blockquotes = document.querySelectorAll( 'blockquote.wp-embedded-content' ),
95                         iframeClone, i, source, secret;
96
97                 for ( i = 0; i < blockquotes.length; i++ ) {
98                         blockquotes[ i ].style.display = 'none';
99                 }
100
101                 for ( i = 0; i < iframes.length; i++ ) {
102                         source = iframes[ i ];
103                         source.style.display = '';
104
105                         if ( source.getAttribute( 'data-secret' ) ) {
106                                 continue;
107                         }
108
109                         /* Add secret to iframe */
110                         secret = Math.random().toString( 36 ).substr( 2, 10 );
111                         source.src += '#?secret=' + secret;
112                         source.setAttribute( 'data-secret', secret );
113
114                         /* Remove security attribute from iframes in IE10 and IE11. */
115                         if ( ( isIE10 || isIE11 ) ) {
116                                 iframeClone = source.cloneNode( true );
117                                 iframeClone.removeAttribute( 'security' );
118                                 source.parentNode.replaceChild( iframeClone, source );
119                         }
120                 }
121         }
122
123         if ( supportedBrowser ) {
124                 window.addEventListener( 'message', window.wp.receiveEmbedMessage, false );
125                 document.addEventListener( 'DOMContentLoaded', onLoad, false );
126                 window.addEventListener( 'load', onLoad, false );
127         }
128 })( window, document );