Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error: "Can't find variable: TextDecoder" #6011

Merged
merged 22 commits into from
Nov 4, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
2687820
Update amp-ad-network-doubleclick-impl.md
glevitzky Aug 31, 2016
8d8c6dd
Reverting doc changes
glevitzky Aug 31, 2016
91938ab
Merge branch 'master' of github.com:google/amphtml
Sep 6, 2016
cbc5e27
Merge branch 'master' of github.com:google/amphtml
glevitzky Sep 13, 2016
5476512
Merge remote-tracking branch 'upstream/master'
keithwrightbos Sep 14, 2016
f670b2c
Merge remote-tracking branch 'upstream/master'
keithwrightbos Sep 15, 2016
90057aa
Merge remote-tracking branch 'upstream/master'
keithwrightbos Sep 16, 2016
f6db020
Merge remote-tracking branch 'upstream/master'
keithwrightbos Sep 19, 2016
5977d63
Merge branch 'master' of github.com:google/amphtml
keithwrightbos Oct 20, 2016
23bd06f
Merge remote-tracking branch 'upstream/master'
keithwrightbos Oct 20, 2016
1b09313
Merge branch 'master' of github.com:google/amphtml
keithwrightbos Oct 28, 2016
5faa071
Initial changes, need to update tests
keithwrightbos Oct 28, 2016
4c09bce
Start fixing tests
keithwrightbos Oct 31, 2016
04b8d75
Fix unit test coverage
keithwrightbos Nov 2, 2016
bed279e
revert a4a examples
keithwrightbos Nov 2, 2016
1453ead
Merge branch 'master' of github.com:ampproject/amphtml into a4a_ampRu…
keithwrightbos Nov 2, 2016
5c47fe0
Include self (window) when verifying TextDecoder/TextEncoder existence
keithwrightbos Nov 3, 2016
c502d1a
Merge branch 'master' of github.com:ampproject/amphtml into textdecod…
keithwrightbos Nov 3, 2016
0b046b6
fix upstream conflict
keithwrightbos Nov 3, 2016
81bdaba
PR feedback
keithwrightbos Nov 4, 2016
8043704
Merge branch 'master' of github.com:ampproject/amphtml into textdecod…
keithwrightbos Nov 4, 2016
5c239c2
PR feedback
keithwrightbos Nov 4, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Initial changes, need to update tests
  • Loading branch information
keithwrightbos committed Oct 28, 2016
commit 5faa071d7f45ec8778f863a2462e34bfd23f448a
70 changes: 1 addition & 69 deletions examples/a4a.amp.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,79 +11,11 @@
</head>
<body>

<h1>A4A Examples</h1>

<h2>Fake ad nework</h2>
<amp-ad width="300" height="400"
type="fake"
data-use-a4a="true"
src="fake_amp.json">
</amp-ad>

<h2>Adsense</h2>
<amp-ad width=300 height=250
type="adsense"
data-ad-client="ca-pub-2005682797531342"
data-ad-slot="7046626912">
</amp-ad>

<h2>Adsense</h2>
<amp-ad width=300 height=300
type="adsense"
data-ad-client="ca-pub-8125901705757971"
data-ad-slot="7783467241">
data-ad-client="ca-pub-6743622525202572">
</amp-ad>

<h2>Doubleclick</h2>
<amp-ad width=320 height=50
type="doubleclick"
data-slot="/4119129/mobile_ad_banner"
>
</amp-ad>

<h2>Doubleclick with JSON based parameters</h2>
<amp-ad width=320 height=50
type="doubleclick"
data-slot="/4119129/mobile_ad_banner"
json='{"targeting":{"sport":["rugby","cricket"]},"categoryExclusions":["health"],"tagForChildDirectedTreatment":0}'
>
</amp-ad>

<h2>Doubleclick no ad with fallback</h2>
<amp-ad width=300 height=200
type="doubleclick"
data-slot="/4119129/doesnt-exist"
>
<div fallback>
<b>Thank you for trying AMP!</b>

<p>You got lucky! We have no ad to show to you!</p>
</div>
</amp-ad>

<h2>Doubleclick no ad with placeholder and fallback</h2>
<amp-ad width=300 height=200
type="doubleclick"
data-slot="/4119129/doesnt-exist"
>
<div placeholder>
<b>Placeholder here!!!</b>
</div>
<div fallback>
<b>Thank you for trying AMP!</b>

<p>You got lucky! We have no ad to show to you!</p>
</div>
</amp-ad>

<h2>Doubleclick with overriden size</h2>
<amp-ad width=420 height=100
data-override-width=320 data-override-height=50
type="doubleclick"
data-slot="/4119129/mobile_ad_banner"
class="red"
>
</amp-ad>

</body>
</html>
140 changes: 44 additions & 96 deletions extensions/amp-a4a/0.1/amp-a4a.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ const SHARED_IFRAME_PROPERTIES = {
export let AdResponseDef;

/** @typedef {{
cssUtf16CharOffsets: Array<number>,
bodyUtf16CharOffsets: !Array<number>,
bodyAttributes: ?string,
minifiedCreative: string,
customElementExtensions: Array<string>,
customStylesheets: Array<string>
}} */
Expand Down Expand Up @@ -707,11 +705,6 @@ export class AmpA4A extends AMP.BaseElement {
'frameborder': '0', 'allowfullscreen': '',
'allowtransparency': '', 'scrolling': 'no'}));
this.applyFillContent(iframe);

const cssBlock = this.formatCSSBlock_(creative, creativeMetaData);
const bodyBlock = this.formatBody_(creative, creativeMetaData);
const bodyAttrString = creativeMetaData.bodyAttributes ?
' ' + creativeMetaData.bodyAttributes : '';
const fontsArray = [];
if (creativeMetaData.customStylesheets) {
creativeMetaData.customStylesheets.forEach(s => {
Expand All @@ -721,20 +714,17 @@ export class AmpA4A extends AMP.BaseElement {
}
});
}
const modifiedCreative =
`<!doctype html><html ⚡4ads>
<head>
<style amp-custom>${cssBlock}</style>
</head>
<body ${bodyAttrString}>${bodyBlock}</body>
</html>`;
return installFriendlyIframeEmbed(
iframe, this.element, {
url: this.adUrl_,
html: modifiedCreative,
html: creativeMetaData.minifiedCreative,
extensionIds: creativeMetaData.customElementExtensions || [],
fonts: fontsArray,
}).then(friendlyIframeEmbed => {
// Ensure visibility hidden has been removed (set by boilerplate).
const frameDoc = friendlyIframeEmbed.iframe.contentDocument ||
friendlyIframeEmbed.win.document;
frameDoc.body.style.visibility = 'visible';
// Capture phase click handlers on the ad.
this.registerExpandUrlParams_(friendlyIframeEmbed.win);
// Bubble phase click handlers on the ad.
Expand All @@ -744,6 +734,7 @@ export class AmpA4A extends AMP.BaseElement {
return true;
});
} catch (e) {
dev().error('Error injecting creative in friendly frame', e);
// If we fail on any of the steps of Shadow DOM construction, just
// render in iframe.
// TODO: report!
Expand Down Expand Up @@ -854,94 +845,51 @@ export class AmpA4A extends AMP.BaseElement {
return null;
}
try {
return this.buildCreativeMetaData_(/** @type {!Object} */ (JSON.parse(
creative.slice(metadataStart + METADATA_STRING.length, metadataEnd))));
} catch (err) {
dev().warn('A4A', 'Invalid amp metadata: %s',
const metaDataObj = JSON.parse(
creative.slice(metadataStart + METADATA_STRING.length, metadataEnd));
return null;
}
}

/**
* @param {!Object} metaDataObj JSON extraced from creative
* @return {!CreativeMetaDataDef} if valid, null otherwise
* @private
*/
buildCreativeMetaData_(metaDataObj) {
const metaData = {};
metaData.bodyUtf16CharOffsets = metaDataObj['bodyUtf16CharOffsets'];
if (!isValidOffsetArray(metaData.bodyUtf16CharOffsets)) {
// Invalid/Missing body offsets array.
throw new Error('Invalid/missing body offsets');
}
if (metaDataObj['cssUtf16CharOffsets']) {
metaData.cssUtf16CharOffsets = metaDataObj['cssUtf16CharOffsets'];
if (!isValidOffsetArray(metaData.cssUtf16CharOffsets)) {
throw new Error('Invalid CSS offsets');
}
}
if (metaDataObj['bodyAttributes']) {
metaData.bodyAttributes = metaDataObj['bodyAttributes'];
if (typeof metaData.bodyAttributes !== 'string') {
throw new Error('Invalid body attributes');
const ampRuntimeUtf16CharOffsets =
metaDataObj['ampRuntimeUtf16CharOffsets'];
if (!isValidOffsetArray(ampRuntimeUtf16CharOffsets)) {
throw new Error('Invalid runtime offsets');
}
}
if (metaDataObj['customElementExtensions']) {
metaData.customElementExtensions = metaDataObj['customElementExtensions'];
if (!isArray(metaData.customElementExtensions)) {
throw new Error('Invalid extensions');
}
}
if (metaDataObj['customStylesheets']) {
// Expect array of objects with at least one key being 'href' whose value
// is URL.
metaData.customStylesheets = metaDataObj['customStylesheets'];
const errorMsg = 'Invalid custom stylesheets';
if (!isArray(metaData.customStylesheets)) {
throw new Error(errorMsg);
const metaData = {};
if (metaDataObj['customElementExtensions']) {
metaData.customElementExtensions =
metaDataObj['customElementExtensions'];
if (!isArray(metaData.customElementExtensions)) {
throw new Error('Invalid extensions');
}
}
metaData.customStylesheets.forEach(stylesheet => {
if (!isObject(stylesheet) || !stylesheet['href'] ||
typeof stylesheet['href'] !== 'string' ||
!/^https:\/\//i.test(stylesheet['href'])) {
if (metaDataObj['customStylesheets']) {
// Expect array of objects with at least one key being 'href' whose value
// is URL.
metaData.customStylesheets = metaDataObj['customStylesheets'];
const errorMsg = 'Invalid custom stylesheets';
if (!isArray(metaData.customStylesheets)) {
throw new Error(errorMsg);
}
});
metaData.customStylesheets.forEach(stylesheet => {
if (!isObject(stylesheet) || !stylesheet['href'] ||
typeof stylesheet['href'] !== 'string' ||
!/^https:\/\//i.test(stylesheet['href'])) {
throw new Error(errorMsg);
}
});
}
// TODO(keithwrightbos): OK to assume ampRuntimeUtf16CharOffsets is before
// metadata as its in the head?
metaData.minifiedCreative =
creative.slice(0, ampRuntimeUtf16CharOffsets[0]) +
creative.slice(ampRuntimeUtf16CharOffsets[1], metadataStart) +
creative.slice(metadataEnd, creative.length);
return metaData;
} catch (err) {
dev().warn('A4A', 'Invalid amp metadata: %s',
creative.slice(metadataStart + METADATA_STRING.length, metadataEnd));
return null;
}
return metaData;
}

/**
* Extracts the body portion of the creative, according to directions in the
* metaData, and formats it for insertion into Shadow DOM.
* @param {string} creative from which CSS is extracted
* @param {!CreativeMetaDataDef} metaData Metadata object extracted from the
* reserialized creative.
* @returns {string} Body of AMP creative, surrounded by {@code
* <amp-ad-body>} tags, and suitable for injection into Shadow DOM.
* @private
*/
formatBody_(creative, metaData) {
return creative.substring(metaData.bodyUtf16CharOffsets[0],
metaData.bodyUtf16CharOffsets[1]);
}

/**
* Note: destructively reverses the {@code offsets} list as a side effect.
* @param {string} creative from which CSS is extracted
* @param {!CreativeMetaDataDef} metaData from creative.
* @returns {string} CSS to be added to page.
*/
formatCSSBlock_(creative, metaData) {
if (!metaData.cssUtf16CharOffsets) {
return '';
}
return creative.substring(
metaData.cssUtf16CharOffsets[0],
metaData.cssUtf16CharOffsets[1]);
}

/**
* Registers a click handler for "A2A" (AMP-to-AMP navigation where the AMP
* viewer navigates to an AMP destination on our behalf.
Expand Down
9 changes: 4 additions & 5 deletions extensions/amp-a4a/0.1/test/testdata/regexps.original.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
limitations under the license.
-->
<!doctype html>
<html ⚡>
<html ⚡4ads>
<head>
<meta charset="utf-8">
<link rel="canonical" href="./regular-html-version.html" />
<meta name="viewport" content="width=device-width,minimum-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>
<style amp4ads-boilerplate>body{visibility:hidden}</style>
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-vine" src="https://cdn.ampproject.org/v0/amp-vine-latest.js"></script>
<script async custom-element="amp-audio" src="https://cdn.ampproject.org/v0/amp-audio-0.1.js"></script>
Expand All @@ -34,8 +33,8 @@
</head>
<body>

<amp-audio src="https://example.com/audio" layout="fixed" autoplay="">
<amp-audio src="https://example.com/audio" layout="fixed" autoplay="desktop">
<amp-audio src="https://example.com/audio" layout="fixed">
<amp-audio src="https://example.com/audio" layout="fixed">

</body>
</html>
Loading