Skip to content

Commit

Permalink
Amp-imgur : Implement imgur embed (ampproject#9405) (ampproject#9434)
Browse files Browse the repository at this point in the history
* Amp-imgur : Implement imgur embed (ampproject#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
techhtml authored and Aaron Turner committed Jun 6, 2017
1 parent b4f6729 commit cbe3a6d
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 0 deletions.
4 changes: 4 additions & 0 deletions build-system/tasks/check-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ function filterWhitelistedLinks(markdown) {
filteredMarkdown =
filteredMarkdown.replace(/https:\/\/cdn.ampproject.org(?!\/)/g, '');

// TODO(honeybadgerdontcare): Remove after PR #9434 is merged
filteredMarkdown =
filteredMarkdown.replace(/https:\/\/github.com\/ampproject\/amphtml\/blob\/master\/extensions\/amp-imgur\/0.1\/validator-amp-imgur.protoascii/g, '');

return filteredMarkdown;
}

Expand Down
15 changes: 15 additions & 0 deletions examples/amp-imgur.amp.html
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>
119 changes: 119 additions & 0 deletions extensions/amp-imgur/0.1/amp-imgur.js
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);
47 changes: 47 additions & 0 deletions extensions/amp-imgur/0.1/test/test-amp-imgur.js
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'));
});
});
});
48 changes: 48 additions & 0 deletions extensions/amp-imgur/0.1/validator-amp-imgur.protoascii
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
}
}
1 change: 1 addition & 0 deletions extensions/amp-imgur/OWNERS.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- techhtml
61 changes: 61 additions & 0 deletions extensions/amp-imgur/amp-imgur.md
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>&lt;script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-imgur-0.1.js">&lt;/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.
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ declareExtension('amp-hulu', '0.1', false);
declareExtension('amp-iframe', '0.1', false, 'NO_TYPE_CHECK');
declareExtension('amp-ima-video', '0.1', false);
declareExtension('amp-image-lightbox', '0.1', true);
declareExtension('amp-imgur', '0.1', false);
declareExtension('amp-instagram', '0.1', true);
declareExtension('amp-install-serviceworker', '0.1', false);
declareExtension('amp-izlesene', '0.1', false);
Expand Down

0 comments on commit cbe3a6d

Please sign in to comment.