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

Cleanup ampdoc-fie in AmpDoc service #30511

Merged
merged 5 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 2 additions & 11 deletions extensions/amp-a4a/0.1/test/test-amp-a4a.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ import {
protectFunctionWrapper,
} from '../amp-a4a';
import {AmpAdXOriginIframeHandler} from '../../../../extensions/amp-ad/0.1/amp-ad-xorigin-iframe-handler';
import {
AmpDoc,
installDocService,
updateFieModeForTesting,
} from '../../../../src/service/ampdoc-impl';
import {AmpDoc, installDocService} from '../../../../src/service/ampdoc-impl';
import {CONSENT_POLICY_STATE} from '../../../../src/consent-state';
import {Extensions} from '../../../../src/service/extensions-impl';
import {FetchMock, networkFailure} from './fetch-mock';
Expand All @@ -62,7 +58,6 @@ import {
import {layoutRectLtwh} from '../../../../src/layout-rect';
import {resetScheduledElementForTesting} from '../../../../src/service/custom-element-registry';
import {data as testFragments} from './testdata/test_fragments';
import {toggleAmpdocFieForTesting} from '../../../../src/ampdoc-fie';
import {data as validCSSAmp} from './testdata/valid_css_at_rules_amp.reserialized';

// eslint-disable-next-line no-undef
Expand Down Expand Up @@ -2301,11 +2296,7 @@ describe('amp-a4a', () => {
a4a.adUrl_ = 'https://nowhere.org';
});

it('should render correctly in ampdoc-fie mode', async () => {
const parentWin = a4aElement.ownerDocument.defaultView;
const ampdocService = Services.ampdocServiceFor(parentWin);
toggleAmpdocFieForTesting(parentWin, true);
updateFieModeForTesting(ampdocService, true);
it('should render correctly', async () => {
await a4a.renderAmpCreative_(metaData);
// Verify iframe presence.
expect(a4aElement.children.length).to.equal(1);
Expand Down
8 changes: 2 additions & 6 deletions extensions/amp-a4a/0.1/test/test-name-frame-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describes.realWin('NameFrameRenderer', realWinConfig, (env) => {
additionalContextMetadata: {},
};

containerElement = document.createElement('div');
containerElement = env.win.document.createElement('div');
containerElement.setAttribute('height', 50);
containerElement.setAttribute('width', 320);
containerElement.getPageLayoutBox = () => ({
Expand All @@ -55,15 +55,11 @@ describes.realWin('NameFrameRenderer', realWinConfig, (env) => {
height: 0,
});
containerElement.getIntersectionChangeEntry = () => ({});
document.body.appendChild(containerElement);
env.win.document.body.appendChild(containerElement);

new NameFrameRenderer().render(context, containerElement, creativeData);
});

afterEach(() => {
document.body.removeChild(containerElement);
});

it('should append iframe child', () => {
const iframe = containerElement.querySelector('iframe');
expect(iframe).to.be.ok;
Expand Down
4 changes: 2 additions & 2 deletions extensions/amp-analytics/0.1/test/test-variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ import {
linkerReaderServiceFor,
} from '../linker-reader';

const fakeElement = document.documentElement;

describes.fakeWin('amp-analytics.VariableService', {amp: true}, (env) => {
let fakeElement;
let variables;

beforeEach(() => {
fakeElement = env.win.document.documentElement;
installLinkerReaderService(env.win);
variables = new VariableService(env.ampdoc);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ import {
extractTitle,
} from '../utils';

describes.realWin('amp-apester-media-utils', {}, (unused) => {
describes.realWin('amp-apester-media-utils', {amp: true}, (env) => {
let window, document;

beforeEach(() => {
window = env.win;
document = window.document;
document.body.textContent = '';
document.head.textContent = '';
});
Expand Down
47 changes: 0 additions & 47 deletions src/ampdoc-fie.js

This file was deleted.

9 changes: 5 additions & 4 deletions src/impression.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import {Deferred} from './utils/promise';
import {Services} from './services';
import {WindowInterface} from './window-interface';
import {
addParamsToUrl,
isProxyOrigin,
Expand Down Expand Up @@ -204,11 +205,11 @@ function handleClickUrl(win) {
return Promise.resolve();
}

if (win.location.hash) {
if (WindowInterface.getLocation(win).hash) {
// This is typically done using replaceState inside the viewer.
// If for some reason it failed, get rid of the fragment here to
// avoid duplicate tracking.
win.location.hash = '';
WindowInterface.getLocation(win).hash = '';
}

// TODO(@zhouyx) need test with a real response.
Expand Down Expand Up @@ -278,7 +279,7 @@ function applyResponse(win, response) {
}

const viewer = Services.viewerForDoc(win.document.documentElement);
const currentHref = win.location.href;
const currentHref = WindowInterface.getLocation(win).href;
const url = parseUrlDeprecated(adLocation);
const params = parseQueryString(url.search);
const newHref = addParamsToUrl(currentHref, params);
Expand Down Expand Up @@ -310,7 +311,7 @@ export function shouldAppendExtraParams(ampdoc) {
*/
export function getExtraParamsUrl(win, target) {
// Get an array with extra params that needs to append.
const url = parseUrlDeprecated(win.location.href);
const url = parseUrlDeprecated(WindowInterface.getLocation(win).href);
const params = parseQueryString(url.search);
const appendParams = [];
for (let i = 0; i < DEFAULT_APPEND_URL_PARAM.length; i++) {
Expand Down
102 changes: 18 additions & 84 deletions src/service/ampdoc-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ import {Deferred} from '../utils/promise';
import {Observable} from '../observable';
import {Signals} from '../utils/signals';
import {VisibilityState} from '../visibility-state';
import {WindowInterface} from '../window-interface';
import {
addDocumentVisibilityChangeListener,
getDocumentVisibilityState,
removeDocumentVisibilityChangeListener,
} from '../utils/document-visibility';
import {dev, devAssert} from '../log';
import {getParentWindowFrameElement, registerServiceBuilder} from '../service';
import {getShadowRootNode} from '../shadow-embed';
import {isDocumentReady, whenDocumentReady} from '../document-ready';
import {isInAmpdocFieExperiment} from '../ampdoc-fie';
import {iterateCursor, rootNodeFor, waitForBodyOpenPromise} from '../dom';
import {map} from '../utils/object';
import {parseQueryString} from '../url';
Expand Down Expand Up @@ -86,12 +85,6 @@ export class AmpDocService {
});
win.document[AMPDOC_PROP] = this.singleDoc_;
}

/** @private {boolean} */
this.ampdocFieExperimentOn_ = isInAmpdocFieExperiment(win);

/** @private {boolean} */
this.mightHaveShadowRoots_ = !isSingleDoc;
}

/**
Expand Down Expand Up @@ -133,93 +126,45 @@ export class AmpDocService {

/**
* Returns the instance of the ampdoc (`AmpDoc`) that contains the specified
* node. If the runtime is in the single-doc mode, the one global `AmpDoc`
* instance is returned, unless specfically looking for a closer `AmpDoc`.
* Otherwise, this method locates the `AmpDoc` that contains the specified
* node and, if necessary, initializes it.
*
* TODO(#22733): rewrite docs once the ampdoc-fie is launched.
* node.
*
* @param {!Node} node
* @return {?AmpDoc}
*/
getAmpDocIfAvailable(node) {
if (this.ampdocFieExperimentOn_) {
let n = node;
while (n) {
// A custom element may already have the reference. If we are looking
// for the closest AmpDoc, the element might have a reference to the
// global AmpDoc, which we do not want. This occurs when using
// <amp-next-page>.

const cachedAmpDoc = this.getCustomElementAmpDocReference_(node);
if (cachedAmpDoc) {
return cachedAmpDoc;
}

// Root note: it's either a document, or a shadow document.
const rootNode = rootNodeFor(n);
if (!rootNode) {
break;
}
const ampdoc = rootNode[AMPDOC_PROP];
if (ampdoc) {
return ampdoc;
}

// Try to iterate to the host of the current root node.
// First try the shadow root's host.
if (rootNode.host) {
n = rootNode.host;
} else {
// Then, traverse the boundary of a friendly iframe.
n = getParentWindowFrameElement(rootNode, this.win);
}
}

return null;
}

// Otherwise discover and possibly create the ampdoc.
let n = node;
while (n) {
// A custom element may already have the reference. If we are looking
// for the closest AmpDoc, the element might have a reference to the
// global AmpDoc, which we do not want. This occurs when using
// <amp-next-page>.

const cachedAmpDoc = this.getCustomElementAmpDocReference_(node);
if (cachedAmpDoc) {
return cachedAmpDoc;
}

// Traverse the boundary of a friendly iframe.
const frameElement = getParentWindowFrameElement(n, this.win);
if (frameElement) {
n = frameElement;
continue;
}

if (!this.mightHaveShadowRoots_) {
// Root note: it's either a document, or a shadow document.
const rootNode = rootNodeFor(n);
if (!rootNode) {
break;
}

// Shadow doc.
const shadowRoot =
n.nodeType == /* DOCUMENT */ 9 ? n : getShadowRootNode(n);
if (!shadowRoot) {
break;
}

const ampdoc = shadowRoot[AMPDOC_PROP];
const ampdoc = rootNode[AMPDOC_PROP];
if (ampdoc) {
return ampdoc;
}
n = shadowRoot.host;

// Try to iterate to the host of the current root node.
// First try the shadow root's host.
if (rootNode.host) {
n = rootNode.host;
} else {
// Then, traverse the boundary of a friendly iframe.
n = getParentWindowFrameElement(rootNode, this.win);
}
}

// If we were looking for the closest AmpDoc, then fall back to the single
// doc if there is no other AmpDoc that is closer.
return this.singleDoc_;
calebcordry marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

/**
Expand Down Expand Up @@ -258,7 +203,6 @@ export class AmpDocService {
* @restricted
*/
installShadowDoc(url, shadowRoot, opt_options) {
this.mightHaveShadowRoots_ = true;
devAssert(
!shadowRoot[AMPDOC_PROP],
'The shadow root already contains ampdoc'
Expand Down Expand Up @@ -802,7 +746,7 @@ export class AmpDocSingle extends AmpDoc {

/** @override */
getUrl() {
return this.win.location.href;
return WindowInterface.getLocation(this.win).href;
}

/** @override */
Expand Down Expand Up @@ -1088,13 +1032,3 @@ export function installDocService(win, isSingleDoc, opt_initParams) {
return new AmpDocService(win, isSingleDoc, opt_initParams);
});
}

/**
* @param {AmpDocService} ampdocService
* @param {boolean} value
* @visibleForTesting
*/
export function updateFieModeForTesting(ampdocService, value) {
// TODO(#22733): remove this method once ampdoc-fie is launched.
ampdocService.ampdocFieExperimentOn_ = value;
}
Loading