-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Amp-imgur : Implement imgur embed (#9405) * amp-imgur test code modified * amp-imgur validation code changed * fix amp-imgur lint check * Remove unused Layour variables * Test code modified * Ingegration amp-imgur extension * [amp-imgur] add resize logic * Remove 3p iframes and add resize message listener * Fix some code with reviews * change some syntax * change validation rules * add find event source * [amp-imgur] remove unnecessary code and add object check * remove spec_name from validation code * Remove unnecessary code and add owners file * Change required tag name to in validation file * data-imgurid -> data-imgur-id * Add whitelist for amp-imgur * remove unused AmpImgur import in test file * remove unused preconnect callback
- Loading branch information
1 parent
54442e1
commit 482270b
Showing
8 changed files
with
296 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!doctype html> | ||
<html ⚡> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>amp-imgur example</title> | ||
<link rel="canonical" href="amps.html"> | ||
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1"> | ||
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript> | ||
<script async custom-element="amp-imgur" src="https://cdn.ampproject.org/v0/amp-imgur-0.1.js"></script> | ||
<script async src="https://cdn.ampproject.org/v0.js"></script> | ||
</head> | ||
<body> | ||
<amp-imgur data-imgur-id="2CnX7" layout="responsive" width="1" height="1"></amp-imgur> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* Copyright 2017 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @fileoverview Embeds a imgur | ||
* Example: | ||
* <code> | ||
* <amp-imgur | ||
* layout="reponsive" | ||
* width="540" | ||
* height="663" | ||
* data-imgur-id="f462IUj"> | ||
* </amp-imgur> | ||
* </code> | ||
*/ | ||
|
||
import {user} from '../../../src/log'; | ||
import {isLayoutSizeDefined} from '../../../src/layout'; | ||
import {removeElement} from '../../../src/dom'; | ||
import {tryParseJson} from '../../../src/json'; | ||
import {isObject} from '../../../src/types'; | ||
import {listen} from '../../../src/event-helper'; | ||
import {startsWith} from '../../../src/string'; | ||
|
||
export class AmpImgur extends AMP.BaseElement { | ||
|
||
/** @param {!AmpElement} element */ | ||
constructor(element) { | ||
super(element); | ||
|
||
/** @private {?Element} */ | ||
this.iframe_ = null; | ||
|
||
/** @private {?Function} */ | ||
this.unlistenMessage_ = null; | ||
|
||
/** @private {?string} */ | ||
this.imgurid_ = ''; | ||
} | ||
|
||
/** @override */ | ||
isLayoutSupported(layout) { | ||
return isLayoutSizeDefined(layout); | ||
} | ||
|
||
/** @override */ | ||
buildCallback() { | ||
this.imgurid_ = user().assert( | ||
this.element.getAttribute('data-imgur-id'), | ||
'The data-imgur-id attribute is required for <amp-imgur> %s', | ||
this.element); | ||
} | ||
|
||
/** @override */ | ||
layoutCallback() { | ||
const iframe = this.element.ownerDocument.createElement('iframe'); | ||
this.iframe_ = iframe; | ||
|
||
this.unlistenMessage_ = listen( | ||
this.win, | ||
'message', | ||
this.hadleImgurMessages_.bind(this) | ||
); | ||
|
||
iframe.setAttribute('scrolling', 'no'); | ||
iframe.setAttribute('frameborder', '0'); | ||
iframe.setAttribute('allowfullscreen', 'true'); | ||
|
||
iframe.src = 'https://imgur.com/a/' + | ||
encodeURIComponent(this.imgurid_) + '/embed?pub=true'; | ||
this.applyFillContent(iframe); | ||
this.element.appendChild(iframe); | ||
return this.loadPromise(iframe); | ||
} | ||
|
||
/** @private */ | ||
hadleImgurMessages_(event) { | ||
if (event.origin != 'https://imgur.com' || | ||
event.source != this.iframe_.contentWindow) { | ||
return; | ||
} | ||
if (!event.data || !(isObject(event.data)) || startsWith(event.data, '{')) { | ||
return; | ||
} | ||
const data = isObject(event.data) ? event.data : tryParseJson(event.data); | ||
if (data.message == 'resize_imgur') { | ||
const height = data.height; | ||
this.attemptChangeHeight(height).catch(() => {}); | ||
} | ||
} | ||
|
||
/** @override */ | ||
unlayoutCallback() { | ||
if (this.iframe_) { | ||
removeElement(this.iframe_); | ||
this.iframe_ = null; | ||
} | ||
if (this.unlistenMessage_) { | ||
this.unlistenMessage_(); | ||
} | ||
return true; // Call layoutCallback again. | ||
} | ||
|
||
} | ||
|
||
AMP.registerElement('amp-imgur', AmpImgur); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright 2017 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { | ||
createIframePromise, | ||
doNotLoadExternalResourcesInTest, | ||
} from '../../../../testing/iframe'; | ||
import '../amp-imgur'; | ||
|
||
describe('amp-imgur', () => { | ||
|
||
function getIns(imgurId) { | ||
return createIframePromise().then(iframe => { | ||
doNotLoadExternalResourcesInTest(iframe.win); | ||
const ins = iframe.doc.createElement('amp-imgur'); | ||
ins.setAttribute('data-imgur-id', imgurId); | ||
ins.setAttribute('width', '1'); | ||
ins.setAttribute('height', '1'); | ||
ins.setAttribute('layout', 'responsive'); | ||
return iframe.addElement(ins); | ||
}); | ||
} | ||
|
||
function testIframe(iframe) { | ||
expect(iframe).to.not.be.null; | ||
expect(iframe.src).to.equal('https://imgur.com/a/2CnX7/embed?pub=true'); | ||
expect(iframe.className).to.match(/i-amphtml-fill-content/); | ||
} | ||
|
||
it('renders', () => { | ||
return getIns('2CnX7').then(ins => { | ||
testIframe(ins.querySelector('iframe')); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# | ||
# Copyright 2017 The AMP HTML Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS-IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the license. | ||
# | ||
|
||
tags: { # amp-imgur | ||
html_format: AMP | ||
tag_name: "SCRIPT" | ||
satisfies: "amp-imgur extension .js script" | ||
requires: "amp-imgur" | ||
extension_spec: { | ||
name: "amp-imgur" | ||
allowed_versions: "0.1" | ||
allowed_versions: "latest" | ||
} | ||
attr_lists: "common-extension-attrs" | ||
} | ||
tags: { # <amp-imgur> | ||
html_format: AMP | ||
tag_name: "AMP-IMGUR" | ||
satisfies: "amp-imgur" | ||
requires: "amp-imgur extension .js script" | ||
attrs: { | ||
name: "data-imgur-id" | ||
mandatory: true | ||
} | ||
attr_lists: "extended-amp-global" | ||
spec_url: "https://www.ampproject.org/docs/reference/components/amp-imgur" | ||
amp_layout: { | ||
supported_layouts: FILL | ||
supported_layouts: FIXED | ||
supported_layouts: FIXED_HEIGHT | ||
supported_layouts: FLEX_ITEM | ||
supported_layouts: NODISPLAY | ||
supported_layouts: RESPONSIVE | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- techhtml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<!-- | ||
Copyright 2017 The AMP HTML Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS-IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
|
||
# <a name="`amp-imgur`"></a> `amp-imgur` | ||
|
||
<table> | ||
<tr> | ||
<td width="40%"><strong>Description</strong></td> | ||
<td>Displays a <a href="http://imgur.com">imgur</a>.</td> | ||
</tr> | ||
<tr> | ||
<td width="40%"><strong>Availability</strong></td> | ||
<td>in development</td> | ||
</tr> | ||
<tr> | ||
<td width="40%"><strong>Required Script</strong></td> | ||
<td><code><script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-imgur-0.1.js"></script></code></td> | ||
</tr> | ||
<tr> | ||
<td class="col-fourty"><strong><a href="https://www.ampproject.org/docs/guides/responsive/control_layout.html">Supported Layouts</a></strong></td> | ||
<td>fill, fixed, fixed-height, flex-item, nodisplay, responsive</td> | ||
</tr> | ||
<tr> | ||
<td width="40%"><strong>Examples</strong></td> | ||
<td>None</td> | ||
</tr> | ||
</table> | ||
|
||
## Behavior | ||
|
||
This extension creates an iframe and displays the imgur post. | ||
|
||
## Attributes | ||
|
||
**data-imgur-id** (required) | ||
The ID of the imgur to embed. | ||
|
||
**layout** (required) | ||
Currently only supports `responsive`. | ||
|
||
**width** (required) | ||
The width of the imgur. | ||
|
||
**height** (required) | ||
The width of the imgur. | ||
|
||
## Validation | ||
See [amp-imgur rules](https://github.com/ampproject/amphtml/blob/master/extensions/amp-imgur/0.1/validator-amp-imgur.protoascii) in the AMP validator specification. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
482270b
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.
Awesome! Thank you.