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

Add document.location (WIP) #1075

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
80 changes: 80 additions & 0 deletions src/main-thread/commands/location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright 2019 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 { CommandExecutorInterface } from './interface';
import {
TransferrableMutationType,
LocationMutationIndex
} from '../../transfer/TransferrableMutation';
import { TransferrableKeys } from '../../transfer/TransferrableKeys';
import { MessageType, LocationToWorker, GetOrSet } from '../../transfer/Messages';
import { Location } from '../../worker-thread/dom/Location';

export const LocationProcessor: CommandExecutorInterface = (strings, nodeContext, workerContext, objectContext, config) => {
const allowedExecution = config.executorsAllowed.includes(TransferrableMutationType.LOCATION);

const get = (): void => {
if (config.sanitizer) {
config.sanitizer.getLocation().then((value: Location) => {
const message: LocationToWorker = {
[TransferrableKeys.type]: MessageType.LOCATION,
[TransferrableKeys.location]: value,
};
workerContext.messageToWorker(message);
});
} else {
console.error(`LOCATION: Sanitizer not found`);
}
};

const set = (location: string): void => {
if (config.sanitizer) {
config.sanitizer.setLocation(location);
} else {
window.location.href = location;
}
};

return {
execute(mutations: Uint16Array, startPosition: number, allowedMutation: boolean): number {
if (allowedExecution && allowedMutation) {
const operation = mutations[startPosition + LocationMutationIndex.Operation];
const locationIndex = mutations[startPosition + LocationMutationIndex.Location];

const location = strings.get(locationIndex);

if (operation === GetOrSet.GET) {
get();
} else if (operation === GetOrSet.SET) {
set(location);
}
}

return startPosition + LocationMutationIndex.End;
},
print(mutations: Uint16Array, startPosition: number): {} {
const operation = mutations[startPosition + LocationMutationIndex.Operation];
const location = mutations[startPosition + LocationMutationIndex.Location];

return {
type: 'LOCATION',
operation,
location,
allowedExecution,
};
},
};
};
15 changes: 15 additions & 0 deletions src/main-thread/main-thread.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import { Location } from '../worker-thread/dom/Location';

/**
* Allows clients to apply restrictions on DOM and storage changes.
*/
Expand Down Expand Up @@ -58,6 +60,19 @@ declare interface Sanitizer {
* @return True if storage change was applied.
*/
setStorage(location: number, key: string | null, value: string | null): boolean;

/**
* Retrieves the current location.
* @return current location
*/
getLocation(): Promise<Location>;

/**
* Requests a change in location.
* @param location href
* @return True if location change was applied.
*/
setLocation(location: string): boolean;
}

type StorageValue = { [key: string]: string };
Expand Down
2 changes: 2 additions & 0 deletions src/test/DocumentCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import { DOMTokenList } from '../worker-thread/dom/DOMTokenList';
import { HTMLDataListElement } from '../worker-thread/dom/HTMLDataListElement';
import { Element } from '../worker-thread/dom/Element';
import { rafPolyfill, cafPolyfill } from '../worker-thread/AnimationFrame';
import { Location } from '../worker-thread/dom/Location';

Object.defineProperty(global, 'ServiceWorkerContainer', {
configurable: true,
Expand Down Expand Up @@ -130,6 +131,7 @@ const GlobalScope: GlobalScope = {
MutationObserver,
requestAnimationFrame: rafPolyfill,
cancelAnimationFrame: cafPolyfill,
location: Location,
};

/**
Expand Down
10 changes: 8 additions & 2 deletions src/transfer/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { HydrateableNode, TransferredNode } from './TransferrableNodes';
import { TransferrableBoundingClientRect } from './TransferrableBoundClientRect';
import { Phase } from './Phase';
import { StorageLocation } from './TransferrableStorage';
import { Location } from '../worker-thread/dom/Location';

export const enum MessageType {
// INIT = 0,
Expand All @@ -36,6 +37,7 @@ export const enum MessageType {
IMAGE_BITMAP_INSTANCE = 10,
GET_STORAGE = 11,
FUNCTION = 12,
LOCATION = 13,
// NAVIGATION_PUSH_STATE = 8,
// NAVIGATION_REPLACE_STATE = 9,
// NAVIGATION_POP_STATE = 10,
Expand Down Expand Up @@ -90,7 +92,10 @@ export interface StorageValueToWorker {
[TransferrableKeys.storageLocation]: StorageLocation;
[TransferrableKeys.value]: { [key: string]: string };
}

export interface LocationToWorker {
[TransferrableKeys.type]: MessageType.LOCATION;
[TransferrableKeys.location]: Location;
}
export interface FunctionCallToWorker {
[TransferrableKeys.type]: MessageType.FUNCTION;
[TransferrableKeys.index]: number;
Expand All @@ -106,7 +111,8 @@ export type MessageToWorker =
| OffscreenCanvasToWorker
| ImageBitmapToWorker
| StorageValueToWorker
| FunctionCallToWorker;
| FunctionCallToWorker
| LocationToWorker;

/**
* Can parameterize a method invocation message as a getter or setter.
Expand Down
3 changes: 2 additions & 1 deletion src/transfer/TransferrableKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const enum TransferrableKeys {
propertyEventHandlers = 76,
functionIdentifier = 77,
functionArguments = 78,
location = 79,
// This must always be the last numerically ordered Key, for testing purposes.
END = 78,
END = 79,
}
17 changes: 17 additions & 0 deletions src/transfer/TransferrableMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const enum TransferrableMutationType {
STORAGE = 12,
FUNCTION_CALL = 13,
SCROLL_INTO_VIEW = 14,
LOCATION = 15,
}

/**
Expand Down Expand Up @@ -67,6 +68,7 @@ export const DefaultAllowedMutations = [
TransferrableMutationType.STORAGE,
TransferrableMutationType.FUNCTION_CALL,
TransferrableMutationType.SCROLL_INTO_VIEW,
TransferrableMutationType.LOCATION,
];

export const ReadableMutationType: { [key: number]: string } = {
Expand All @@ -85,6 +87,7 @@ export const ReadableMutationType: { [key: number]: string } = {
12: 'STORAGE',
13: 'FUNCTION_INVOCATION',
14: 'SCROLL_INTO_VIEW',
15: 'LOCATION',
};

/**
Expand Down Expand Up @@ -286,3 +289,17 @@ export const enum ScrollIntoViewMutationIndex {
Target = 1,
End = 2,
}

/**
* Location Mutation
* [
* TransferrableMutationType.LOCATION,
* GetOrSet,
* Location
* ]
*/
export const enum LocationMutationIndex {
Operation = 1,
Location = 2,
End = 3,
}
35 changes: 35 additions & 0 deletions src/worker-thread/LocationPropagation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright 2018 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 { MessageToWorker, MessageType, LocationToWorker } from '../transfer/Messages';
import { TransferrableKeys } from '../transfer/TransferrableKeys';
import { WorkerDOMGlobalScope } from './WorkerDOMGlobalScope';

export function propagate(global: WorkerDOMGlobalScope): void {
const document = global.document;
if (!document.addGlobalEventListener) {
return;
}
document.addGlobalEventListener('message', ({ data }: { data: MessageToWorker }) => {
if (data[TransferrableKeys.type] !== MessageType.LOCATION) {
return;
}
const location = (data as LocationToWorker)[TransferrableKeys.location];
if (location) {
document.location = location;
}
});
}
2 changes: 2 additions & 0 deletions src/worker-thread/WorkerDOMGlobalScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { DocumentFragment } from './dom/DocumentFragment';
import { DOMTokenList } from './dom/DOMTokenList';
import { Element } from './dom/Element';
import { DocumentStub } from './dom/DocumentLite';
import { Location } from './dom/Location';

/**
* Should only contain properties that exist on Window.
Expand Down Expand Up @@ -114,6 +115,7 @@ export interface GlobalScope {
ImageBitmap?: typeof ImageBitmap;
requestAnimationFrame: typeof requestAnimationFrame;
cancelAnimationFrame: typeof cancelAnimationFrame;
location: typeof Location;
}

export interface WorkerDOMGlobalScope extends GlobalScope {
Expand Down
4 changes: 3 additions & 1 deletion src/worker-thread/dom/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { Text } from './Text';
import { Comment } from './Comment';
import { toLower } from '../../utils';
import { DocumentFragment } from './DocumentFragment';
import { Location } from "./Location";
import { PostMessage } from '../worker-thread';
import { NodeType, HTML_NAMESPACE, HydrateableNode } from '../../transfer/TransferrableNodes';
import { Phase } from '../../transfer/Phase';
Expand All @@ -68,6 +69,7 @@ export class Document extends Element {
public defaultView: WorkerDOMGlobalScope;
public documentElement: Document;
public body: Element;
public location: Location;

// Internal variables.
public postMessage: PostMessage;
Expand All @@ -80,11 +82,11 @@ export class Document extends Element {
// Element uppercases its nodeName, but Document doesn't.
this.nodeName = DOCUMENT_NAME;
this.documentElement = this; // TODO(choumx): Should be the <html> element.

this.defaultView = Object.assign(global, {
document: this,
addEventListener: this.addEventListener.bind(this),
removeEventListener: this.removeEventListener.bind(this),
location: this.location,
});
}

Expand Down
Loading