Skip to content

Commit

Permalink
Add type checks for remaining non-access extensions.
Browse files Browse the repository at this point in the history
Part of #9876
  • Loading branch information
cramforce committed Jul 1, 2017
1 parent f66ae56 commit 8ae99c6
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 112 deletions.
5 changes: 4 additions & 1 deletion extensions/amp-jwplayer/0.1/amp-jwplayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ class AmpJWPlayer extends AMP.BaseElement {
this.iframe_ = null;
}

/** @override */
/**
* @param {boolean=} onLayout
* @override
*/
preconnectCallback(onLayout) {
// Host that serves player configuration and content redirects
this.preconnect.url('https://content.jwplatform.com', onLayout);
Expand Down
10 changes: 7 additions & 3 deletions extensions/amp-o2-player/0.1/amp-o2-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import {isLayoutSizeDefined} from '../../../src/layout';
import {user} from '../../../src/log';
import {dict} from '../../../src/utils/object';

class AmpO2Player extends AMP.BaseElement {

Expand All @@ -39,7 +40,10 @@ class AmpO2Player extends AMP.BaseElement {
this.src_ = '';
}

/** @override */
/**
* @param {boolean=} onLayout
* @override
*/
preconnectCallback(onLayout) {
this.preconnect.url(this.domain_, onLayout);
}
Expand Down Expand Up @@ -111,10 +115,10 @@ class AmpO2Player extends AMP.BaseElement {
/** @override */
pauseCallback() {
if (this.iframe_ && this.iframe_.contentWindow) {
this.iframe_.contentWindow./*OK*/postMessage(JSON.stringify({
this.iframe_.contentWindow./*OK*/postMessage(JSON.stringify(dict({
'method': 'pause',
'value': this.domain_,
}), '*');
})), '*');
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion extensions/amp-pinterest/0.1/amp-pinterest.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ import {PinWidget} from './pin-widget';
*/
class AmpPinterest extends AMP.BaseElement {

/** @override */
/**
* @param {boolean=} onLayout
* @override
*/
preconnectCallback(onLayout) {
// preconnect to widget APIpinMedia
this.preconnect.url('https://widgets.pinterest.com', onLayout);
Expand Down
3 changes: 2 additions & 1 deletion extensions/amp-pinterest/0.1/follow-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export class FollowButton {
'The data-label attribute is required for follow buttons');
this.element = rootElement;
this.label = rootElement.getAttribute('data-label');
this.href = assertHttpsUrl(rootElement.getAttribute('data-href'));
this.href = assertHttpsUrl(rootElement.getAttribute('data-href'),
rootElement);
}

/**
Expand Down
56 changes: 32 additions & 24 deletions extensions/amp-pinterest/0.1/pin-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export class PinWidget {
'The data-url attribute is required for Pin widgets');
this.element = rootElement;
this.xhr = xhrFor(rootElement.ownerDocument.defaultView);
this.pinId = '';
this.pinUrl = '';
this.width = '';
this.layout = '';
}

/**
Expand All @@ -62,22 +66,26 @@ export class PinWidget {
}
}

/** @return {!Promise<!JsonObject>} */
fetchPin() {
const baseUrl = 'https://widgets.pinterest.com/v3/pidgets/pins/info/?';
const query = `pin_ids=${this.pinId}&sub=www&base_scheme=https`;
return this.xhr.fetchJson(baseUrl + query, {
requireAmpResponseSourceOrigin: false,
}).then(res => res.json()).then(json => {
try {
return json.data[0];
return /** @type {JsonObject} */(json)['data'][0];
} catch (e) { return null; }
});
}

/**
* @param {!JsonObject} pin
*/
renderPin(pin) {
// start setting our class name
let className = '-amp-pinterest-embed-pin';
let imgUrl = assertHttpsUrl(pin.images['237x'].url);
let imgUrl = assertHttpsUrl(pin['images']['237x']['url'], this.element);

// large widgets may come later
if (this.width === 'medium' || this.width === 'large') {
Expand Down Expand Up @@ -105,7 +113,7 @@ export class PinWidget {
'src': imgUrl,
'className': '-amp-pinterest-embed-pin-image',
'data-pin-no-hover': true,
'data-pin-href': 'https://www.pinterest.com/pin/' + pin.id + '/',
'data-pin-href': 'https://www.pinterest.com/pin/' + pin['id'] + '/',
'data-pin-log': 'embed_pin_img',
}});
container.appendChild(img);
Expand All @@ -116,7 +124,7 @@ export class PinWidget {
' -amp-pinterest-embed-pin-repin',
'data-pin-log': 'embed_pin_repin',
'data-pin-pop': '1',
'data-pin-href': 'https://www.pinterest.com/pin/' + pin.id +
'data-pin-href': 'https://www.pinterest.com/pin/' + pin['id'] +
'/repin/x/?amp=1&guid=' + Util.guid,
}});
container.appendChild(repin);
Expand All @@ -127,61 +135,61 @@ export class PinWidget {
}});

// description
if (pin.description) {
if (pin['description']) {
const description = Util.make(this.element.ownerDocument, {'span': {
'className': '-amp-pinterest-embed-pin-text-block ' +
'-amp-pinterest-embed-pin-description',
'textContent': Util.filter(pin.description),
'textContent': Util.filter(pin['description']),
}});
text.appendChild(description);
}

// attribution
if (pin.attribution) {
if (pin['attribution']) {
const attribution = Util.make(this.element.ownerDocument, {'span': {
'className': '-amp-pinterest-embed-pin-text-block' +
' -amp-pinterest-embed-pin-attribution',
}});
attribution.appendChild(Util.make(this.element.ownerDocument, {'img': {
'className': '-amp-pinterest-embed-pin-text-icon-attrib',
'src': pin.attribution.provider_icon_url,
'src': pin['attribution']['provider_icon_url'],
}}));
attribution.appendChild(Util.make(this.element.ownerDocument, {'span': {
'textContent': ' by ',
}}));
attribution.appendChild(Util.make(this.element.ownerDocument, {'span': {
'data-pin-href': pin.attribution.url,
'textContent': Util.filter(pin.attribution.author_name),
'data-pin-href': pin['attribution']['url'],
'textContent': Util.filter(pin['attribution']['author_name']),
}}));
text.appendChild(attribution);
}

// likes and repins
if (pin.repin_count || pin.like_count) {
if (pin['repin_count'] || pin['like_count']) {
const stats = Util.make(this.element.ownerDocument, {'span': {
'className': '-amp-pinterest-embed-pin-text-block' +
' -amp-pinterest-embed-pin-stats',
}});
if (pin.repin_count) {
if (pin['repin_count']) {
const repinCount = Util.make(this.element.ownerDocument, {'span': {
'className': '-amp-pinterest-embed-pin-stats-repins',
'textContent': String(pin.repin_count),
'textContent': String(pin['repin_count']),
}});
stats.appendChild(repinCount);
}

if (pin.like_count) {
if (pin['like_count']) {
const likeCount = Util.make(this.element.ownerDocument, {'span': {
'className': '-amp-pinterest-embed-pin-stats-likes',
'textContent': String(pin.like_count),
'textContent': String(pin['like_count']),
}});
stats.appendChild(likeCount);
}
text.appendChild(stats);
}

// pinner
if (pin.pinner) {
if (pin['pinner']) {

const pinner = Util.make(this.element.ownerDocument, {'span': {
'className': '-amp-pinterest-embed-pin-text-block' +
Expand All @@ -191,24 +199,24 @@ export class PinWidget {
// avatar
pinner.appendChild(Util.make(this.element.ownerDocument, {'img': {
'className': '-amp-pinterest-embed-pin-pinner-avatar',
'alt': Util.filter(pin.pinner.full_name),
'title': Util.filter(pin.pinner.full_name),
'src': pin.pinner.image_small_url,
'data-pin-href': pin.pinner.profile_url,
'alt': Util.filter(pin['pinner']['full_name']),
'title': Util.filter(pin['pinner']['full_name']),
'src': pin['pinner']['image_small_url'],
'data-pin-href': pin['pinner']['profile_url'],
}}));

// name
pinner.appendChild(Util.make(this.element.ownerDocument, {'span': {
'className': '-amp-pinterest-embed-pin-pinner-name',
'textContent': Util.filter(pin.pinner.full_name),
'data-pin-href': pin.pinner.profile_url,
'textContent': Util.filter(pin['pinner']['full_name']),
'data-pin-href': pin['pinner']['profile_url'],
}}));

// board
pinner.appendChild(Util.make(this.element.ownerDocument, {'span': {
'className': '-amp-pinterest-embed-pin-board-name',
'textContent': Util.filter(pin.board.name),
'data-pin-href': 'https://www.pinterest.com/' + pin.board.url,
'textContent': Util.filter(pin['board']['name']),
'data-pin-href': 'https://www.pinterest.com/' + pin['board']['url'],
}}));

text.appendChild(pinner);
Expand Down
28 changes: 15 additions & 13 deletions extensions/amp-pinterest/0.1/pinit-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/
import {openWindowDialog} from '../../../src/dom';
import {user} from '../../../src/log';
import {dev, user} from '../../../src/log';
import {xhrFor} from '../../../src/services';

import {Util} from './util';
Expand Down Expand Up @@ -55,6 +55,9 @@ export class PinItButton {
this.round = rootElement.getAttribute('data-round');
this.tall = rootElement.getAttribute('data-tall');
this.description = rootElement.getAttribute('data-description');
this.media = null;
this.url = null;
this.href = null;
}

/**
Expand All @@ -63,14 +66,13 @@ export class PinItButton {
*/
handleClick(event) {
event.preventDefault();
openWindowDialog(window, this.href, '_pinit', POP);
openWindowDialog(window, dev().assertString(this.href), '_pinit', POP);
Util.log('&type=button_pinit');
}

/**
* Fetch the remote Pin count for the source URL
* @param {Event} evt: the HTML event object
* @returns {Promise}
* @return {Promise}
*/
fetchCount() {
const url = `https://widgets.pinterest.com/v1/urls/count.json?return_jsonp=false&url=${this.url}`;
Expand All @@ -81,8 +83,8 @@ export class PinItButton {

/**
* Pretty print the Pin count with english suffixes
* @param {number} count: the Pin count for the source URL
* @returns {string}
* @param {number|string} count: the Pin count for the source URL
* @return {string}
*/
formatPinCount(count) {
if (count > 999) {
Expand All @@ -96,14 +98,14 @@ export class PinItButton {
}
}
}
return count;
return String(count);
}

/**
* Render helper for the optional count bubble
* @param {string} count: the data-count attribute
* @param {string} heightClass: the height class to apply for spacing
* @returns {string}
* @return {Element}
*/
renderCount(count, heightClass) {
Util.log('&type=pidget&button_count=1');
Expand All @@ -115,8 +117,8 @@ export class PinItButton {

/**
* Render the follow button
* @param {number} count: optional Pin count for the source URL
* @returns {Element}
* @param {JsonObject} count: optional Pin count for the source URL
* @return {Element}
*/
renderTemplate(count) {
const CLASS = {
Expand All @@ -131,12 +133,12 @@ export class PinItButton {
'i-amphtml-fill-content',
];

let countBubble = '';
let countBubble = null;
if (!this.round) {
clazz.push(`-amp-pinterest${CLASS.lang}-${CLASS.color}${CLASS.height}`);
if (count) {
clazz.push(`-amp-pinterest-count-pad-${this.count}${CLASS.height}`);
countBubble = this.renderCount(count.count, CLASS.height);
countBubble = this.renderCount(count['count'], CLASS.height);
}
}

Expand All @@ -154,7 +156,7 @@ export class PinItButton {

/**
* Prepare the render data, create the node and add handlers
* @returns {!Promise}
* @return {!Promise}
*/
render() {
this.description = encodeURIComponent(this.description);
Expand Down
10 changes: 5 additions & 5 deletions extensions/amp-pinterest/0.1/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function log(queryParams) {
/**
* Strip data from string
* @param {string} str - the string to filter
* @returns {string}
* @return {string}
*/
function filter(str) {
let decoded, ret;
Expand All @@ -57,12 +57,12 @@ function filter(str) {

/**
* Create a DOM element with attributes
* @param {!Document} doc
* @param {Document} doc
* @param {Object} data - the string to filter
* @returns {DOMElement}
* @return {Element}
*/
function make(doc, data) {
let el = false, tag, attr;
let el = null, tag, attr;
for (tag in data) {
el = doc.createElement(tag);
for (attr in data[tag]) {
Expand All @@ -77,7 +77,7 @@ function make(doc, data) {

/**
* Set a DOM element attribute
* @param {DOMElement} data - the string to filter
* @param {Element} el - The element
* @param {string} attr - the attribute key
* @param {string} value - the attribute value
*/
Expand Down
Loading

0 comments on commit 8ae99c6

Please sign in to comment.