From 5d1608a24535e4b4092710e1ad53bb65a2f40d71 Mon Sep 17 00:00:00 2001 From: pushkine Date: Mon, 1 Jun 2020 20:47:58 +0200 Subject: [PATCH] fix --- src/runtime/internal/Component.ts | 3 +-- src/runtime/internal/animations.ts | 3 +-- src/runtime/internal/dev.ts | 5 ++-- src/runtime/internal/dom.ts | 29 ++++++++++++++++++--- src/runtime/internal/environment.ts | 40 ++++++++++++----------------- src/runtime/internal/globals.ts | 7 +++++ src/runtime/internal/loop.ts | 3 ++- src/runtime/internal/scheduler.ts | 4 ++- src/runtime/internal/transitions.ts | 4 +-- src/runtime/internal/utils.ts | 3 +-- 10 files changed, 61 insertions(+), 40 deletions(-) create mode 100644 src/runtime/internal/globals.ts diff --git a/src/runtime/internal/Component.ts b/src/runtime/internal/Component.ts index 780e7e0eec..f3af2f33e4 100644 --- a/src/runtime/internal/Component.ts +++ b/src/runtime/internal/Component.ts @@ -1,9 +1,8 @@ import { add_render_callback, flush, schedule_update } from './scheduler'; import { current_component, set_current_component } from './lifecycle'; -import { blank_object, is_function, run, run_all } from './utils'; +import { blank_object, is_function, run, run_all, noop } from './utils'; import { children, detach } from './dom'; import { transition_in } from './transitions'; -import { noop } from './environment'; export interface Fragment { key: string|null; diff --git a/src/runtime/internal/animations.ts b/src/runtime/internal/animations.ts index d266488a8c..4d65480c7c 100644 --- a/src/runtime/internal/animations.ts +++ b/src/runtime/internal/animations.ts @@ -1,6 +1,5 @@ import { run_transition } from './transitions'; -import { noop } from './environment'; -import { methodify } from './utils'; +import { methodify, noop } from './utils'; import { CssTransitionConfig } from 'svelte/transition'; type AnimationFn = (node: Element, { from, to }: { from: DOMRect; to: DOMRect }, params: any) => CssTransitionConfig; diff --git a/src/runtime/internal/dev.ts b/src/runtime/internal/dev.ts index d38cb8d0a0..f1bdfc9ca2 100644 --- a/src/runtime/internal/dev.ts +++ b/src/runtime/internal/dev.ts @@ -1,6 +1,5 @@ import { custom_event, append, insert, detach, listen, attr } from './dom'; import { SvelteComponent } from './Component'; -import { has_Symbol } from './environment'; export function dispatch_dev(type: string, detail?: T) { document.dispatchEvent(custom_event(type, { version: '__VERSION__', ...detail })); @@ -83,7 +82,7 @@ export function set_data_dev(text, data) { export function validate_each_argument(arg) { if (typeof arg !== 'string' && !(arg && typeof arg === 'object' && 'length' in arg)) { let msg = '{#each} only iterates over array-like objects.'; - if (has_Symbol && arg && Symbol.iterator in arg) { + if (typeof Symbol === 'function' && arg && Symbol.iterator in arg) { msg += ' You can use a spread to convert this iterable into an array.'; } throw new Error(msg); @@ -141,4 +140,4 @@ export function loop_guard(timeout) { throw new Error(`Infinite loop detected`); } }; -} +} \ No newline at end of file diff --git a/src/runtime/internal/dom.ts b/src/runtime/internal/dom.ts index 7fdc26808b..07d0896a27 100644 --- a/src/runtime/internal/dom.ts +++ b/src/runtime/internal/dom.ts @@ -1,5 +1,4 @@ import { has_prop } from "./utils"; -import { is_cors } from "./environment"; export function append(target: Node, node: Node) { target.appendChild(node); @@ -235,6 +234,26 @@ export function select_multiple_value(select) { return [].map.call(select.querySelectorAll(':checked'), option => option.__value); } +// unfortunately this can't be a constant as that wouldn't be tree-shakeable +// so we cache the result instead +let crossorigin: boolean; + +export function is_crossorigin() { + if (crossorigin === undefined) { + crossorigin = false; + + try { + if (typeof window !== 'undefined' && window.parent) { + void window.parent.document; + } + } catch (error) { + crossorigin = true; + } + } + + return crossorigin; +} + export function add_resize_listener(node: HTMLElement, fn: () => void) { const computed_style = getComputedStyle(node); const z_index = (parseInt(computed_style.zIndex) || 0) - 1; @@ -251,9 +270,11 @@ export function add_resize_listener(node: HTMLElement, fn: () => void) { iframe.setAttribute('aria-hidden', 'true'); iframe.tabIndex = -1; + const crossorigin = is_crossorigin(); + let unsubscribe: () => void; - if (is_cors) { + if (crossorigin) { iframe.src = `data:text/html,`; unsubscribe = listen(window, 'message', (event: MessageEvent) => { if (event.source === iframe.contentWindow) fn(); @@ -268,7 +289,7 @@ export function add_resize_listener(node: HTMLElement, fn: () => void) { append(node, iframe); return () => { - if (is_cors) { + if (crossorigin) { unsubscribe(); } else if (unsubscribe && iframe.contentWindow) { unsubscribe(); @@ -333,4 +354,4 @@ export class HtmlTag { d() { this.n.forEach(detach); } -} +} \ No newline at end of file diff --git a/src/runtime/internal/environment.ts b/src/runtime/internal/environment.ts index 2dfe05eba5..a4de8f4912 100644 --- a/src/runtime/internal/environment.ts +++ b/src/runtime/internal/environment.ts @@ -1,23 +1,12 @@ -export function noop() {} -export const is_browser = typeof window !== 'undefined'; -export const is_iframe = is_browser && window.self !== window.top; -export const is_cors = - is_iframe && - /*#__PURE__*/ (() => { - try { - if (window.parent) void window.parent.document; - return false; - } catch (error) { - return true; - } - })(); -export const has_Symbol = typeof Symbol === 'function'; -/* eslint-disable no-var */ -declare var global: any; -export const globals = is_browser ? window : typeof globalThis !== 'undefined' ? globalThis : global; -export const resolved_promise = Promise.resolve(); -export let now = is_browser ? window.performance.now.bind(window.performance) : Date.now.bind(Date); -export let raf = is_browser ? requestAnimationFrame : noop; +import { noop } from './utils'; + +export const is_client = typeof window !== 'undefined'; + +export let now: () => number = is_client + ? () => window.performance.now() + : () => Date.now(); + +export let raf = is_client ? cb => requestAnimationFrame(cb) : noop; export let framerate = 1000 / 60; /*#__PURE__*/ raf((t1) => { raf((d) => { @@ -27,6 +16,11 @@ export let framerate = 1000 / 60; }); }); -/* tests only */ -export const set_now = (v) => void (now = v); -export const set_raf = (fn) => void (raf = fn); +// used internally for testing +export function set_now(fn) { + now = fn; +} + +export function set_raf(fn) { + raf = fn; +} \ No newline at end of file diff --git a/src/runtime/internal/globals.ts b/src/runtime/internal/globals.ts new file mode 100644 index 0000000000..ab75e38db6 --- /dev/null +++ b/src/runtime/internal/globals.ts @@ -0,0 +1,7 @@ +declare const global: any; + +export const globals = (typeof window !== 'undefined' + ? window + : typeof globalThis !== 'undefined' + ? globalThis + : global) as unknown as typeof globalThis; \ No newline at end of file diff --git a/src/runtime/internal/loop.ts b/src/runtime/internal/loop.ts index b4c1c895af..c63bdd4000 100644 --- a/src/runtime/internal/loop.ts +++ b/src/runtime/internal/loop.ts @@ -1,4 +1,5 @@ -import { now, raf, framerate, noop } from './environment'; +import { now, raf, framerate } from './environment'; +import { noop } from './utils'; type TaskCallback = (t: number) => boolean; type TaskCanceller = () => void; diff --git a/src/runtime/internal/scheduler.ts b/src/runtime/internal/scheduler.ts index 47c6edd08d..99b6dd940f 100644 --- a/src/runtime/internal/scheduler.ts +++ b/src/runtime/internal/scheduler.ts @@ -1,7 +1,9 @@ import { set_current_component } from './lifecycle'; -import { resolved_promise, now } from './environment'; +import { now } from './environment'; import { T$$ } from './Component'; +const resolved_promise = Promise.resolve(); + let update_scheduled = false; let is_flushing = false; diff --git a/src/runtime/internal/transitions.ts b/src/runtime/internal/transitions.ts index 4adcfd6915..088659858e 100644 --- a/src/runtime/internal/transitions.ts +++ b/src/runtime/internal/transitions.ts @@ -1,11 +1,11 @@ import { CssTransitionConfig } from '../transition'; import { Fragment } from './Component'; import { custom_event } from './dom'; -import { now, noop } from './environment'; +import { now } from './environment'; import { setFrameTimeout, setTweenTimeout } from './loop'; import { add_measure_callback } from './scheduler'; import { animate_css } from './style_manager'; -import { methodify } from './utils'; +import { methodify, noop } from './utils'; type TransitionFn = (node: HTMLElement, params: any) => CssTransitionConfig; export type StopResetReverseFn = (t?: number | -1) => StopResetReverseFn | void; diff --git a/src/runtime/internal/utils.ts b/src/runtime/internal/utils.ts index 5e06aa16bf..9f8addb65e 100644 --- a/src/runtime/internal/utils.ts +++ b/src/runtime/internal/utils.ts @@ -1,5 +1,4 @@ -import { noop } from "./environment"; - +export function noop() {} export const identity = x => x; export function assign(tar: T, src: S): T & S {