dom.ts 33.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

7
import * as platform from 'vs/base/common/platform';
J
Johannes Rieken 已提交
8 9 10
import { TPromise } from 'vs/base/common/winjs.base';
import { TimeoutTimer } from 'vs/base/common/async';
import { onUnexpectedError } from 'vs/base/common/errors';
11
import { Disposable, IDisposable, dispose } from 'vs/base/common/lifecycle';
A
Alex Dima 已提交
12
import * as browser from 'vs/base/browser/browser';
J
Johannes Rieken 已提交
13 14 15
import { IKeyboardEvent, StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { IMouseEvent, StandardMouseEvent } from 'vs/base/browser/mouseEvent';
import { CharCode } from 'vs/base/common/charCode';
M
Matt Bierner 已提交
16
import { Event, Emitter } from 'vs/base/common/event';
17
import { domEvent } from 'vs/base/browser/event';
E
Erich Gamma 已提交
18

B
Benjamin Pasero 已提交
19
export function clearNode(node: HTMLElement) {
E
Erich Gamma 已提交
20 21 22 23 24
	while (node.firstChild) {
		node.removeChild(node.firstChild);
	}
}

B
Benjamin Pasero 已提交
25
export function isInDOM(node: Node): boolean {
E
Erich Gamma 已提交
26 27 28 29 30 31 32 33 34
	while (node) {
		if (node === document.body) {
			return true;
		}
		node = node.parentNode;
	}
	return false;
}

A
Alex Dima 已提交
35 36 37
interface IDomClassList {
	hasClass(node: HTMLElement, className: string): boolean;
	addClass(node: HTMLElement, className: string): void;
38
	addClasses(node: HTMLElement, ...classNames: string[]): void;
A
Alex Dima 已提交
39
	removeClass(node: HTMLElement, className: string): void;
40
	removeClasses(node: HTMLElement, ...classNames: string[]): void;
A
Alex Dima 已提交
41 42 43 44
	toggleClass(node: HTMLElement, className: string, shouldHaveIt?: boolean): void;
}

const _manualClassList = new class implements IDomClassList {
E
Erich Gamma 已提交
45

46 47
	private _lastStart: number;
	private _lastEnd: number;
E
Erich Gamma 已提交
48

49
	private _findClassName(node: HTMLElement, className: string): void {
E
Erich Gamma 已提交
50

51 52 53 54 55
		let classes = node.className;
		if (!classes) {
			this._lastStart = -1;
			return;
		}
E
Erich Gamma 已提交
56

57
		className = className.trim();
E
Erich Gamma 已提交
58

59 60
		let classesLen = classes.length,
			classLen = className.length;
E
Erich Gamma 已提交
61

62 63
		if (classLen === 0) {
			this._lastStart = -1;
E
Erich Gamma 已提交
64 65 66
			return;
		}

67 68
		if (classesLen < classLen) {
			this._lastStart = -1;
E
Erich Gamma 已提交
69 70 71
			return;
		}

72 73 74
		if (classes === className) {
			this._lastStart = 0;
			this._lastEnd = classesLen;
E
Erich Gamma 已提交
75 76
			return;
		}
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

		let idx = -1,
			idxEnd: number;

		while ((idx = classes.indexOf(className, idx + 1)) >= 0) {

			idxEnd = idx + classLen;

			// a class that is followed by another class
			if ((idx === 0 || classes.charCodeAt(idx - 1) === CharCode.Space) && classes.charCodeAt(idxEnd) === CharCode.Space) {
				this._lastStart = idx;
				this._lastEnd = idxEnd + 1;
				return;
			}

			// last class
			if (idx > 0 && classes.charCodeAt(idx - 1) === CharCode.Space && idxEnd === classesLen) {
				this._lastStart = idx - 1;
				this._lastEnd = idxEnd;
				return;
			}

			// equal - duplicate of cmp above
			if (idx === 0 && idxEnd === classesLen) {
				this._lastStart = 0;
				this._lastEnd = idxEnd;
				return;
			}
		}

		this._lastStart = -1;
E
Erich Gamma 已提交
108 109
	}

110 111 112 113
	hasClass(node: HTMLElement, className: string): boolean {
		this._findClassName(node, className);
		return this._lastStart !== -1;
	}
E
Erich Gamma 已提交
114

115 116 117 118
	addClasses(node: HTMLElement, ...classNames: string[]): void {
		classNames.forEach(nameValue => nameValue.split(' ').forEach(name => this.addClass(node, name)));
	}

119 120 121 122 123 124 125 126 127 128
	addClass(node: HTMLElement, className: string): void {
		if (!node.className) { // doesn't have it for sure
			node.className = className;
		} else {
			this._findClassName(node, className); // see if it's already there
			if (this._lastStart === -1) {
				node.className = node.className + ' ' + className;
			}
		}
	}
E
Erich Gamma 已提交
129

130 131 132 133 134 135
	removeClass(node: HTMLElement, className: string): void {
		this._findClassName(node, className);
		if (this._lastStart === -1) {
			return; // Prevent styles invalidation if not necessary
		} else {
			node.className = node.className.substring(0, this._lastStart) + node.className.substring(this._lastEnd);
E
Erich Gamma 已提交
136 137 138
		}
	}

139 140 141 142
	removeClasses(node: HTMLElement, ...classNames: string[]): void {
		classNames.forEach(nameValue => nameValue.split(' ').forEach(name => this.removeClass(node, name)));
	}

143 144 145 146 147 148 149 150
	toggleClass(node: HTMLElement, className: string, shouldHaveIt?: boolean): void {
		this._findClassName(node, className);
		if (this._lastStart !== -1 && (shouldHaveIt === void 0 || !shouldHaveIt)) {
			this.removeClass(node, className);
		}
		if (this._lastStart === -1 && (shouldHaveIt === void 0 || shouldHaveIt)) {
			this.addClass(node, className);
		}
E
Erich Gamma 已提交
151
	}
152
};
E
Erich Gamma 已提交
153

A
Alex Dima 已提交
154
const _nativeClassList = new class implements IDomClassList {
155
	hasClass(node: HTMLElement, className: string): boolean {
J
Johannes Rieken 已提交
156
		return className && node.classList && node.classList.contains(className);
E
Erich Gamma 已提交
157
	}
158

159 160 161 162
	addClasses(node: HTMLElement, ...classNames: string[]): void {
		classNames.forEach(nameValue => nameValue.split(' ').forEach(name => this.addClass(node, name)));
	}

163
	addClass(node: HTMLElement, className: string): void {
J
Johannes Rieken 已提交
164 165 166
		if (className && node.classList) {
			node.classList.add(className);
		}
E
Erich Gamma 已提交
167
	}
168 169

	removeClass(node: HTMLElement, className: string): void {
J
Johannes Rieken 已提交
170 171 172
		if (className && node.classList) {
			node.classList.remove(className);
		}
173 174
	}

175 176 177 178
	removeClasses(node: HTMLElement, ...classNames: string[]): void {
		classNames.forEach(nameValue => nameValue.split(' ').forEach(name => this.removeClass(node, name)));
	}

179
	toggleClass(node: HTMLElement, className: string, shouldHaveIt?: boolean): void {
J
Johannes Rieken 已提交
180 181 182
		if (node.classList) {
			node.classList.toggle(className, shouldHaveIt);
		}
183 184 185 186 187
	}
};

// In IE11 there is only partial support for `classList` which makes us keep our
// custom implementation. Otherwise use the native implementation, see: http://caniuse.com/#search=classlist
A
Alex Dima 已提交
188
const _classList: IDomClassList = browser.isIE ? _manualClassList : _nativeClassList;
189 190
export const hasClass: (node: HTMLElement, className: string) => boolean = _classList.hasClass.bind(_classList);
export const addClass: (node: HTMLElement, className: string) => void = _classList.addClass.bind(_classList);
191
export const addClasses: (node: HTMLElement, ...classNames: string[]) => void = _classList.addClasses.bind(_classList);
192
export const removeClass: (node: HTMLElement, className: string) => void = _classList.removeClass.bind(_classList);
193
export const removeClasses: (node: HTMLElement, ...classNames: string[]) => void = _classList.removeClasses.bind(_classList);
194
export const toggleClass: (node: HTMLElement, className: string, shouldHaveIt?: boolean) => void = _classList.toggleClass.bind(_classList);
E
Erich Gamma 已提交
195

A
Alex Dima 已提交
196
class DomListener implements IDisposable {
E
Erich Gamma 已提交
197

A
Alex Dima 已提交
198 199 200 201
	private _handler: (e: any) => void;
	private _node: Element | Window | Document;
	private readonly _type: string;
	private readonly _useCapture: boolean;
E
Erich Gamma 已提交
202

203
	constructor(node: Element | Window | Document, type: string, handler: (e: any) => void, useCapture: boolean) {
204 205
		this._node = node;
		this._type = type;
A
Alex Dima 已提交
206
		this._handler = handler;
207
		this._useCapture = (useCapture || false);
208
		this._node.addEventListener(this._type, this._handler, this._useCapture);
E
Erich Gamma 已提交
209 210
	}

211
	public dispose(): void {
A
Alex Dima 已提交
212
		if (!this._handler) {
213 214 215 216
			// Already disposed
			return;
		}

A
Alex Dima 已提交
217
		this._node.removeEventListener(this._type, this._handler, this._useCapture);
218 219 220

		// Prevent leakers from holding on to the dom or handler func
		this._node = null;
A
Alex Dima 已提交
221
		this._handler = null;
222
	}
E
Erich Gamma 已提交
223 224
}

225 226
export function addDisposableListener(node: Element | Window | Document, type: string, handler: (event: any) => void, useCapture?: boolean): IDisposable {
	return new DomListener(node, type, handler, useCapture);
E
Erich Gamma 已提交
227 228 229
}

export interface IAddStandardDisposableListenerSignature {
B
Benjamin Pasero 已提交
230
	(node: HTMLElement, type: 'click', handler: (event: IMouseEvent) => void, useCapture?: boolean): IDisposable;
A
Alex Dima 已提交
231
	(node: HTMLElement, type: 'mousedown', handler: (event: IMouseEvent) => void, useCapture?: boolean): IDisposable;
B
Benjamin Pasero 已提交
232 233 234 235
	(node: HTMLElement, type: 'keydown', handler: (event: IKeyboardEvent) => void, useCapture?: boolean): IDisposable;
	(node: HTMLElement, type: 'keypress', handler: (event: IKeyboardEvent) => void, useCapture?: boolean): IDisposable;
	(node: HTMLElement, type: 'keyup', handler: (event: IKeyboardEvent) => void, useCapture?: boolean): IDisposable;
	(node: HTMLElement, type: string, handler: (event: any) => void, useCapture?: boolean): IDisposable;
E
Erich Gamma 已提交
236
}
B
Benjamin Pasero 已提交
237
function _wrapAsStandardMouseEvent(handler: (e: IMouseEvent) => void): (e: MouseEvent) => void {
238
	return function (e: MouseEvent) {
A
Cleanup  
Alex Dima 已提交
239
		return handler(new StandardMouseEvent(e));
E
Erich Gamma 已提交
240 241
	};
}
B
Benjamin Pasero 已提交
242
function _wrapAsStandardKeyboardEvent(handler: (e: IKeyboardEvent) => void): (e: KeyboardEvent) => void {
243
	return function (e: KeyboardEvent) {
A
Cleanup  
Alex Dima 已提交
244
		return handler(new StandardKeyboardEvent(e));
E
Erich Gamma 已提交
245 246
	};
}
B
Benjamin Pasero 已提交
247 248
export let addStandardDisposableListener: IAddStandardDisposableListenerSignature = function addStandardDisposableListener(node: HTMLElement, type: string, handler: (event: any) => void, useCapture?: boolean): IDisposable {
	let wrapHandler = handler;
E
Erich Gamma 已提交
249

A
Alex Dima 已提交
250
	if (type === 'click' || type === 'mousedown') {
E
Erich Gamma 已提交
251 252 253 254 255
		wrapHandler = _wrapAsStandardMouseEvent(handler);
	} else if (type === 'keydown' || type === 'keypress' || type === 'keyup') {
		wrapHandler = _wrapAsStandardKeyboardEvent(handler);
	}

256
	return addDisposableListener(node, type, wrapHandler, useCapture);
E
Erich Gamma 已提交
257 258
};

259 260
export function addDisposableNonBubblingMouseOutListener(node: Element, handler: (event: MouseEvent) => void): IDisposable {
	return addDisposableListener(node, 'mouseout', (e: MouseEvent) => {
E
Erich Gamma 已提交
261
		// Mouse out bubbles, so this is an attempt to ignore faux mouse outs coming from children elements
B
Benjamin Pasero 已提交
262
		let toElement = <Node>(e.relatedTarget || e.toElement);
E
Erich Gamma 已提交
263 264 265 266 267 268 269 270 271 272 273
		while (toElement && toElement !== node) {
			toElement = toElement.parentNode;
		}
		if (toElement === node) {
			return;
		}

		handler(e);
	});
}

A
Alex Dima 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
interface IRequestAnimationFrame {
	(callback: (time: number) => void): number;
}
let _animationFrame: IRequestAnimationFrame = null;
function doRequestAnimationFrame(callback: (time: number) => void): number {
	if (!_animationFrame) {
		const emulatedRequestAnimationFrame = (callback: (time: number) => void): number => {
			return setTimeout(() => callback(new Date().getTime()), 0);
		};
		_animationFrame = (
			self.requestAnimationFrame
			|| (<any>self).msRequestAnimationFrame
			|| (<any>self).webkitRequestAnimationFrame
			|| (<any>self).mozRequestAnimationFrame
			|| (<any>self).oRequestAnimationFrame
			|| emulatedRequestAnimationFrame
		);
	}
A
Alex Dima 已提交
292
	return _animationFrame.call(self, callback);
A
Alex Dima 已提交
293
}
E
Erich Gamma 已提交
294 295 296 297 298 299 300

/**
 * Schedule a callback to be run at the next animation frame.
 * This allows multiple parties to register callbacks that should run at the next animation frame.
 * If currently in an animation frame, `runner` will be executed immediately.
 * @return token that can be used to cancel the scheduled runner (only if `runner` was not executed immediately).
 */
B
Benjamin Pasero 已提交
301
export let runAtThisOrScheduleAtNextAnimationFrame: (runner: () => void, priority?: number) => IDisposable;
E
Erich Gamma 已提交
302 303 304 305 306 307
/**
 * Schedule a callback to be run at the next animation frame.
 * This allows multiple parties to register callbacks that should run at the next animation frame.
 * If currently in an animation frame, `runner` will be executed at the next animation frame.
 * @return token that can be used to cancel the scheduled runner.
 */
B
Benjamin Pasero 已提交
308
export let scheduleAtNextAnimationFrame: (runner: () => void, priority?: number) => IDisposable;
E
Erich Gamma 已提交
309

B
Benjamin Pasero 已提交
310
class AnimationFrameQueueItem implements IDisposable {
E
Erich Gamma 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

	private _runner: () => void;
	public priority: number;
	private _canceled: boolean;

	constructor(runner: () => void, priority: number) {
		this._runner = runner;
		this.priority = priority;
		this._canceled = false;
	}

	public dispose(): void {
		this._canceled = true;
	}

	public execute(): void {
		if (this._canceled) {
			return;
		}

		try {
			this._runner();
		} catch (e) {
B
Benjamin Pasero 已提交
334
			onUnexpectedError(e);
E
Erich Gamma 已提交
335 336 337 338 339 340 341 342 343
		}
	}

	// Sort by priority (largest to lowest)
	public static sort(a: AnimationFrameQueueItem, b: AnimationFrameQueueItem): number {
		return b.priority - a.priority;
	}
}

344
(function () {
E
Erich Gamma 已提交
345 346 347
	/**
	 * The runners scheduled at the next animation frame
	 */
B
Benjamin Pasero 已提交
348
	let NEXT_QUEUE: AnimationFrameQueueItem[] = [];
E
Erich Gamma 已提交
349 350 351
	/**
	 * The runners scheduled at the current animation frame
	 */
B
Benjamin Pasero 已提交
352
	let CURRENT_QUEUE: AnimationFrameQueueItem[] = null;
E
Erich Gamma 已提交
353 354 355
	/**
	 * A flag to keep track if the native requestAnimationFrame was already called
	 */
B
Benjamin Pasero 已提交
356
	let animFrameRequested = false;
E
Erich Gamma 已提交
357 358 359
	/**
	 * A flag to indicate if currently handling a native requestAnimationFrame callback
	 */
B
Benjamin Pasero 已提交
360
	let inAnimationFrameRunner = false;
E
Erich Gamma 已提交
361

B
Benjamin Pasero 已提交
362
	let animationFrameRunner = () => {
E
Erich Gamma 已提交
363 364 365 366 367 368 369 370
		animFrameRequested = false;

		CURRENT_QUEUE = NEXT_QUEUE;
		NEXT_QUEUE = [];

		inAnimationFrameRunner = true;
		while (CURRENT_QUEUE.length > 0) {
			CURRENT_QUEUE.sort(AnimationFrameQueueItem.sort);
B
Benjamin Pasero 已提交
371
			let top = CURRENT_QUEUE.shift();
E
Erich Gamma 已提交
372 373 374 375 376
			top.execute();
		}
		inAnimationFrameRunner = false;
	};

B
Benjamin Pasero 已提交
377 378
	scheduleAtNextAnimationFrame = (runner: () => void, priority: number = 0) => {
		let item = new AnimationFrameQueueItem(runner, priority);
E
Erich Gamma 已提交
379 380 381 382
		NEXT_QUEUE.push(item);

		if (!animFrameRequested) {
			animFrameRequested = true;
A
Alex Dima 已提交
383
			doRequestAnimationFrame(animationFrameRunner);
E
Erich Gamma 已提交
384 385 386 387 388
		}

		return item;
	};

B
Benjamin Pasero 已提交
389
	runAtThisOrScheduleAtNextAnimationFrame = (runner: () => void, priority?: number) => {
E
Erich Gamma 已提交
390
		if (inAnimationFrameRunner) {
B
Benjamin Pasero 已提交
391
			let item = new AnimationFrameQueueItem(runner, priority);
E
Erich Gamma 已提交
392 393 394 395 396 397 398 399
			CURRENT_QUEUE.push(item);
			return item;
		} else {
			return scheduleAtNextAnimationFrame(runner, priority);
		}
	};
})();

A
Alex Dima 已提交
400 401 402
/**
 * Add a throttled listener. `handler` is fired at most every 16ms or with the next animation frame (if browser supports it).
 */
403 404
export interface IEventMerger<R, E> {
	(lastEvent: R, currentEvent: E): R;
E
Erich Gamma 已提交
405 406
}

407 408 409 410 411
export interface DOMEvent {
	preventDefault(): void;
	stopPropagation(): void;
}

B
Benjamin Pasero 已提交
412
const MINIMUM_TIME_MS = 16;
413
const DEFAULT_EVENT_MERGER: IEventMerger<DOMEvent, DOMEvent> = function (lastEvent: DOMEvent, currentEvent: DOMEvent) {
E
Erich Gamma 已提交
414 415 416
	return currentEvent;
};

417
class TimeoutThrottledDomListener<R, E extends DOMEvent> extends Disposable {
E
Erich Gamma 已提交
418

419
	constructor(node: any, type: string, handler: (event: R) => void, eventMerger: IEventMerger<R, E> = <any>DEFAULT_EVENT_MERGER, minimumTimeMs: number = MINIMUM_TIME_MS) {
420
		super();
E
Erich Gamma 已提交
421

B
Benjamin Pasero 已提交
422
		let lastEvent: R = null;
423 424
		let lastHandlerTime = 0;
		let timeout = this._register(new TimeoutTimer());
E
Erich Gamma 已提交
425

426 427 428 429 430
		let invokeHandler = () => {
			lastHandlerTime = (new Date()).getTime();
			handler(lastEvent);
			lastEvent = null;
		};
E
Erich Gamma 已提交
431

432
		this._register(addDisposableListener(node, type, (e) => {
E
Erich Gamma 已提交
433

434 435 436 437 438 439 440 441 442 443 444
			lastEvent = eventMerger(lastEvent, e);
			let elapsedTime = (new Date()).getTime() - lastHandlerTime;

			if (elapsedTime >= minimumTimeMs) {
				timeout.cancel();
				invokeHandler();
			} else {
				timeout.setIfNotSet(invokeHandler, minimumTimeMs - elapsedTime);
			}
		}));
	}
E
Erich Gamma 已提交
445 446
}

447
export function addDisposableThrottledListener<R, E extends DOMEvent = DOMEvent>(node: any, type: string, handler: (event: R) => void, eventMerger?: IEventMerger<R, E>, minimumTimeMs?: number): IDisposable {
448
	return new TimeoutThrottledDomListener<R, E>(node, type, handler, eventMerger, minimumTimeMs);
E
Erich Gamma 已提交
449 450
}

B
Benjamin Pasero 已提交
451
export function getComputedStyle(el: HTMLElement): CSSStyleDeclaration {
E
Erich Gamma 已提交
452 453 454 455 456
	return document.defaultView.getComputedStyle(el, null);
}

// Adapted from WinJS
// Converts a CSS positioning string for the specified element to pixels.
457 458
const convertToPixels: (element: HTMLElement, value: string) => number = (function () {
	return function (element: HTMLElement, value: string): number {
E
Erich Gamma 已提交
459 460 461 462
		return parseFloat(value) || 0;
	};
})();

B
Benjamin Pasero 已提交
463 464 465
function getDimension(element: HTMLElement, cssPropertyName: string, jsPropertyName: string): number {
	let computedStyle: CSSStyleDeclaration = getComputedStyle(element);
	let value = '0';
E
Erich Gamma 已提交
466 467 468 469 470 471 472 473 474 475 476
	if (computedStyle) {
		if (computedStyle.getPropertyValue) {
			value = computedStyle.getPropertyValue(cssPropertyName);
		} else {
			// IE8
			value = (<any>computedStyle).getAttribute(jsPropertyName);
		}
	}
	return convertToPixels(element, value);
}

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
export function getClientArea(element: HTMLElement): Dimension {

	// Try with DOM clientWidth / clientHeight
	if (element !== document.body) {
		return new Dimension(element.clientWidth, element.clientHeight);
	}

	// Try innerWidth / innerHeight
	if (window.innerWidth && window.innerHeight) {
		return new Dimension(window.innerWidth, window.innerHeight);
	}

	// Try with document.body.clientWidth / document.body.clientHeigh
	if (document.body && document.body.clientWidth && document.body.clientWidth) {
		return new Dimension(document.body.clientWidth, document.body.clientHeight);
	}

	// Try with document.documentElement.clientWidth / document.documentElement.clientHeight
	if (document.documentElement && document.documentElement.clientWidth && document.documentElement.clientHeight) {
		return new Dimension(document.documentElement.clientWidth, document.documentElement.clientHeight);
	}

	throw new Error('Unable to figure out browser width and height');
}

B
Benjamin Pasero 已提交
502
const sizeUtils = {
E
Erich Gamma 已提交
503

504
	getBorderLeftWidth: function (element: HTMLElement): number {
E
Erich Gamma 已提交
505 506
		return getDimension(element, 'border-left-width', 'borderLeftWidth');
	},
J
Joao Moreno 已提交
507 508 509
	getBorderRightWidth: function (element: HTMLElement): number {
		return getDimension(element, 'border-right-width', 'borderRightWidth');
	},
510
	getBorderTopWidth: function (element: HTMLElement): number {
E
Erich Gamma 已提交
511 512
		return getDimension(element, 'border-top-width', 'borderTopWidth');
	},
513
	getBorderBottomWidth: function (element: HTMLElement): number {
E
Erich Gamma 已提交
514 515 516
		return getDimension(element, 'border-bottom-width', 'borderBottomWidth');
	},

J
Joao Moreno 已提交
517 518 519 520 521 522
	getPaddingLeft: function (element: HTMLElement): number {
		return getDimension(element, 'padding-left', 'paddingLeft');
	},
	getPaddingRight: function (element: HTMLElement): number {
		return getDimension(element, 'padding-right', 'paddingRight');
	},
523
	getPaddingTop: function (element: HTMLElement): number {
E
Erich Gamma 已提交
524 525
		return getDimension(element, 'padding-top', 'paddingTop');
	},
526
	getPaddingBottom: function (element: HTMLElement): number {
E
Erich Gamma 已提交
527 528 529
		return getDimension(element, 'padding-bottom', 'paddingBottom');
	},

530
	getMarginLeft: function (element: HTMLElement): number {
E
Erich Gamma 已提交
531 532
		return getDimension(element, 'margin-left', 'marginLeft');
	},
533
	getMarginTop: function (element: HTMLElement): number {
E
Erich Gamma 已提交
534 535
		return getDimension(element, 'margin-top', 'marginTop');
	},
536
	getMarginRight: function (element: HTMLElement): number {
E
Erich Gamma 已提交
537 538
		return getDimension(element, 'margin-right', 'marginRight');
	},
539
	getMarginBottom: function (element: HTMLElement): number {
E
Erich Gamma 已提交
540 541 542 543 544 545 546 547
		return getDimension(element, 'margin-bottom', 'marginBottom');
	},
	__commaSentinel: false
};

// ----------------------------------------------------------------------------------------
// Position & Dimension

548 549 550 551 552 553 554 555 556 557
export class Dimension {
	public width: number;
	public height: number;

	constructor(width: number, height: number) {
		this.width = width;
		this.height = height;
	}
}

B
Benjamin Pasero 已提交
558
export function getTopLeftOffset(element: HTMLElement): { left: number; top: number; } {
E
Erich Gamma 已提交
559 560 561
	// Adapted from WinJS.Utilities.getPosition
	// and added borders to the mix

B
Benjamin Pasero 已提交
562
	let offsetParent = element.offsetParent, top = element.offsetTop, left = element.offsetLeft;
E
Erich Gamma 已提交
563 564 565

	while ((element = <HTMLElement>element.parentNode) !== null && element !== document.body && element !== document.documentElement) {
		top -= element.scrollTop;
B
Benjamin Pasero 已提交
566
		let c = getComputedStyle(element);
E
Erich Gamma 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
		if (c) {
			left -= c.direction !== 'rtl' ? element.scrollLeft : -element.scrollLeft;
		}

		if (element === offsetParent) {
			left += sizeUtils.getBorderLeftWidth(element);
			top += sizeUtils.getBorderTopWidth(element);
			top += element.offsetTop;
			left += element.offsetLeft;
			offsetParent = element.offsetParent;
		}
	}

	return {
		left: left,
		top: top
	};
}

586
export interface IDomNodePagePosition {
B
Benjamin Pasero 已提交
587 588 589 590
	left: number;
	top: number;
	width: number;
	height: number;
E
Erich Gamma 已提交
591 592
}

593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
export function size(element: HTMLElement, width: number, height: number): void {
	if (typeof width === 'number') {
		element.style.width = `${width}px`;
	}

	if (typeof height === 'number') {
		element.style.height = `${height}px`;
	}
}

export function position(element: HTMLElement, top: number, right?: number, bottom?: number, left?: number, position: string = 'absolute'): void {
	if (typeof top === 'number') {
		element.style.top = `${top}px`;
	}

	if (typeof right === 'number') {
		element.style.right = `${right}px`;
	}

	if (typeof bottom === 'number') {
		element.style.bottom = `${bottom}px`;
	}

	if (typeof left === 'number') {
		element.style.left = `${left}px`;
	}

	element.style.position = position;
}

623 624 625 626 627
/**
 * Returns the position of a dom node relative to the entire page.
 */
export function getDomNodePagePosition(domNode: HTMLElement): IDomNodePagePosition {
	let bb = domNode.getBoundingClientRect();
E
Erich Gamma 已提交
628
	return {
629 630
		left: bb.left + StandardWindow.scrollX,
		top: bb.top + StandardWindow.scrollY,
631 632
		width: bb.width,
		height: bb.height
E
Erich Gamma 已提交
633 634 635
	};
}

636
export interface IStandardWindow {
637 638
	readonly scrollX: number;
	readonly scrollY: number;
639 640
}

641
export const StandardWindow: IStandardWindow = new class implements IStandardWindow {
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
	get scrollX(): number {
		if (typeof window.scrollX === 'number') {
			// modern browsers
			return window.scrollX;
		} else {
			return document.body.scrollLeft + document.documentElement.scrollLeft;
		}
	}

	get scrollY(): number {
		if (typeof window.scrollY === 'number') {
			// modern browsers
			return window.scrollY;
		} else {
			return document.body.scrollTop + document.documentElement.scrollTop;
		}
	}
};

E
Erich Gamma 已提交
661 662
// Adapted from WinJS
// Gets the width of the element, including margins.
B
Benjamin Pasero 已提交
663 664
export function getTotalWidth(element: HTMLElement): number {
	let margin = sizeUtils.getMarginLeft(element) + sizeUtils.getMarginRight(element);
E
Erich Gamma 已提交
665 666 667
	return element.offsetWidth + margin;
}

J
Joao Moreno 已提交
668 669 670 671 672 673
export function getContentWidth(element: HTMLElement): number {
	let border = sizeUtils.getBorderLeftWidth(element) + sizeUtils.getBorderRightWidth(element);
	let padding = sizeUtils.getPaddingLeft(element) + sizeUtils.getPaddingRight(element);
	return element.offsetWidth - border - padding;
}

674 675 676 677 678
export function getTotalScrollWidth(element: HTMLElement): number {
	let margin = sizeUtils.getMarginLeft(element) + sizeUtils.getMarginRight(element);
	return element.scrollWidth + margin;
}

E
Erich Gamma 已提交
679 680
// Adapted from WinJS
// Gets the height of the content of the specified element. The content height does not include borders or padding.
B
Benjamin Pasero 已提交
681 682 683
export function getContentHeight(element: HTMLElement): number {
	let border = sizeUtils.getBorderTopWidth(element) + sizeUtils.getBorderBottomWidth(element);
	let padding = sizeUtils.getPaddingTop(element) + sizeUtils.getPaddingBottom(element);
E
Erich Gamma 已提交
684 685 686 687 688
	return element.offsetHeight - border - padding;
}

// Adapted from WinJS
// Gets the height of the element, including its margins.
B
Benjamin Pasero 已提交
689 690
export function getTotalHeight(element: HTMLElement): number {
	let margin = sizeUtils.getMarginTop(element) + sizeUtils.getMarginBottom(element);
E
Erich Gamma 已提交
691 692 693 694
	return element.offsetHeight + margin;
}

// Gets the left coordinate of the specified element relative to the specified parent.
695
function getRelativeLeft(element: HTMLElement, parent: HTMLElement): number {
E
Erich Gamma 已提交
696 697 698 699
	if (element === null) {
		return 0;
	}

M
Maxime Quandalle 已提交
700 701 702
	let elementPosition = getTopLeftOffset(element);
	let parentPosition = getTopLeftOffset(parent);
	return elementPosition.left - parentPosition.left;
E
Erich Gamma 已提交
703 704
}

M
Maxime Quandalle 已提交
705 706
export function getLargestChildWidth(parent: HTMLElement, children: HTMLElement[]): number {
	let childWidths = children.map((child) => {
707
		return Math.max(getTotalScrollWidth(child), getTotalWidth(child)) + getRelativeLeft(child, parent) || 0;
M
Maxime Quandalle 已提交
708 709 710
	});
	let maxWidth = Math.max(...childWidths);
	return maxWidth;
E
Erich Gamma 已提交
711 712 713 714
}

// ----------------------------------------------------------------------------------------

B
Benjamin Pasero 已提交
715 716
export function isAncestor(testChild: Node, testAncestor: Node): boolean {
	while (testChild) {
E
Erich Gamma 已提交
717 718 719 720 721 722 723 724 725
		if (testChild === testAncestor) {
			return true;
		}
		testChild = testChild.parentNode;
	}

	return false;
}

B
Benjamin Pasero 已提交
726
export function findParentWithClass(node: HTMLElement, clazz: string, stopAtClazzOrNode?: string | HTMLElement): HTMLElement {
E
Erich Gamma 已提交
727 728 729 730 731
	while (node) {
		if (hasClass(node, clazz)) {
			return node;
		}

B
Benjamin Pasero 已提交
732 733 734 735 736 737 738 739 740 741
		if (stopAtClazzOrNode) {
			if (typeof stopAtClazzOrNode === 'string') {
				if (hasClass(node, stopAtClazzOrNode)) {
					return null;
				}
			} else {
				if (node === stopAtClazzOrNode) {
					return null;
				}
			}
E
Erich Gamma 已提交
742 743 744 745 746 747 748 749
		}

		node = <HTMLElement>node.parentNode;
	}

	return null;
}

750
export function createStyleSheet(container: HTMLElement = document.getElementsByTagName('head')[0]): HTMLStyleElement {
B
Benjamin Pasero 已提交
751
	let style = document.createElement('style');
E
Erich Gamma 已提交
752 753
	style.type = 'text/css';
	style.media = 'screen';
754
	container.appendChild(style);
E
Erich Gamma 已提交
755 756 757
	return style;
}

A
Alex Dima 已提交
758 759 760 761 762 763 764
let _sharedStyleSheet: HTMLStyleElement = null;
function getSharedStyleSheet(): HTMLStyleElement {
	if (!_sharedStyleSheet) {
		_sharedStyleSheet = createStyleSheet();
	}
	return _sharedStyleSheet;
}
E
Erich Gamma 已提交
765

B
Benjamin Pasero 已提交
766
function getDynamicStyleSheetRules(style: any) {
E
Erich Gamma 已提交
767 768 769 770 771 772 773 774 775 776 777
	if (style && style.sheet && style.sheet.rules) {
		// Chrome, IE
		return style.sheet.rules;
	}
	if (style && style.sheet && style.sheet.cssRules) {
		// FF
		return style.sheet.cssRules;
	}
	return [];
}

A
Alex Dima 已提交
778
export function createCSSRule(selector: string, cssText: string, style: HTMLStyleElement = getSharedStyleSheet()): void {
E
Erich Gamma 已提交
779 780 781 782
	if (!style || !cssText) {
		return;
	}

783
	(<CSSStyleSheet>style.sheet).insertRule(selector + '{' + cssText + '}', 0);
E
Erich Gamma 已提交
784 785
}

A
Alex Dima 已提交
786
export function removeCSSRulesContainingSelector(ruleName: string, style: HTMLStyleElement = getSharedStyleSheet()): void {
E
Erich Gamma 已提交
787 788 789 790
	if (!style) {
		return;
	}

B
Benjamin Pasero 已提交
791 792 793 794
	let rules = getDynamicStyleSheetRules(style);
	let toDelete: number[] = [];
	for (let i = 0; i < rules.length; i++) {
		let rule = rules[i];
795
		if (rule.selectorText.indexOf(ruleName) !== -1) {
E
Erich Gamma 已提交
796 797 798 799
			toDelete.push(i);
		}
	}

B
Benjamin Pasero 已提交
800
	for (let i = toDelete.length - 1; i >= 0; i--) {
A
Alex Dima 已提交
801
		(<any>style.sheet).deleteRule(toDelete[i]);
E
Erich Gamma 已提交
802 803 804
	}
}

B
Benjamin Pasero 已提交
805
export function isHTMLElement(o: any): o is HTMLElement {
806 807 808 809
	if (typeof HTMLElement === 'object') {
		return o instanceof HTMLElement;
	}
	return o && typeof o === 'object' && o.nodeType === 1 && typeof o.nodeName === 'string';
E
Erich Gamma 已提交
810 811
}

B
Benjamin Pasero 已提交
812
export const EventType = {
E
Erich Gamma 已提交
813 814
	// Mouse
	CLICK: 'click',
815
	AUXCLICK: 'auxclick', // >= Chrome 56
E
Erich Gamma 已提交
816 817 818 819 820 821
	DBLCLICK: 'dblclick',
	MOUSE_UP: 'mouseup',
	MOUSE_DOWN: 'mousedown',
	MOUSE_OVER: 'mouseover',
	MOUSE_MOVE: 'mousemove',
	MOUSE_OUT: 'mouseout',
822
	MOUSE_LEAVE: 'mouseleave',
E
Erich Gamma 已提交
823
	CONTEXT_MENU: 'contextmenu',
B
Benjamin Pasero 已提交
824
	WHEEL: 'wheel',
E
Erich Gamma 已提交
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
	// Keyboard
	KEY_DOWN: 'keydown',
	KEY_PRESS: 'keypress',
	KEY_UP: 'keyup',
	// HTML Document
	LOAD: 'load',
	UNLOAD: 'unload',
	ABORT: 'abort',
	ERROR: 'error',
	RESIZE: 'resize',
	SCROLL: 'scroll',
	// Form
	SELECT: 'select',
	CHANGE: 'change',
	SUBMIT: 'submit',
	RESET: 'reset',
	FOCUS: 'focus',
	BLUR: 'blur',
	INPUT: 'input',
	// Local Storage
	STORAGE: 'storage',
	// Drag
	DRAG_START: 'dragstart',
	DRAG: 'drag',
	DRAG_ENTER: 'dragenter',
	DRAG_LEAVE: 'dragleave',
	DRAG_OVER: 'dragover',
	DROP: 'drop',
	DRAG_END: 'dragend',
	// Animation
A
Alex Dima 已提交
855 856 857
	ANIMATION_START: browser.isWebKit ? 'webkitAnimationStart' : 'animationstart',
	ANIMATION_END: browser.isWebKit ? 'webkitAnimationEnd' : 'animationend',
	ANIMATION_ITERATION: browser.isWebKit ? 'webkitAnimationIteration' : 'animationiteration'
E
Erich Gamma 已提交
858 859
};

A
Alex Dima 已提交
860 861 862 863 864
export interface EventLike {
	preventDefault(): void;
	stopPropagation(): void;
}

B
Benjamin Pasero 已提交
865
export const EventHelper = {
866
	stop: function (e: EventLike, cancelBubble?: boolean) {
E
Erich Gamma 已提交
867 868 869 870 871 872 873 874 875 876 877 878
		if (e.preventDefault) {
			e.preventDefault();
		} else {
			// IE8
			(<any>e).returnValue = false;
		}

		if (cancelBubble) {
			if (e.stopPropagation) {
				e.stopPropagation();
			} else {
				// IE8
A
Alex Dima 已提交
879
				(<any>e).cancelBubble = true;
E
Erich Gamma 已提交
880 881 882 883 884 885
			}
		}
	}
};

export interface IFocusTracker {
886 887
	onDidFocus: Event<void>;
	onDidBlur: Event<void>;
B
Benjamin Pasero 已提交
888
	dispose(): void;
E
Erich Gamma 已提交
889 890
}

B
Benjamin Pasero 已提交
891 892 893
export function saveParentsScrollTop(node: Element): number[] {
	let r: number[] = [];
	for (let i = 0; node && node.nodeType === node.ELEMENT_NODE; i++) {
E
Erich Gamma 已提交
894 895 896 897 898 899
		r[i] = node.scrollTop;
		node = <Element>node.parentNode;
	}
	return r;
}

B
Benjamin Pasero 已提交
900 901
export function restoreParentsScrollTop(node: Element, state: number[]): void {
	for (let i = 0; node && node.nodeType === node.ELEMENT_NODE; i++) {
E
Erich Gamma 已提交
902 903 904 905 906 907 908
		if (node.scrollTop !== state[i]) {
			node.scrollTop = state[i];
		}
		node = <Element>node.parentNode;
	}
}

909
class FocusTracker implements IFocusTracker {
E
Erich Gamma 已提交
910

911 912
	private _onDidFocus = new Emitter<void>();
	readonly onDidFocus: Event<void> = this._onDidFocus.event;
E
Erich Gamma 已提交
913

914 915
	private _onDidBlur = new Emitter<void>();
	readonly onDidBlur: Event<void> = this._onDidBlur.event;
916

917 918 919
	private disposables: IDisposable[] = [];

	constructor(element: HTMLElement | Window) {
920 921 922
		let hasFocus = false;
		let loosingFocus = false;

923
		let onFocus = () => {
924 925 926
			loosingFocus = false;
			if (!hasFocus) {
				hasFocus = true;
927
				this._onDidFocus.fire();
E
Erich Gamma 已提交
928
			}
929
		};
E
Erich Gamma 已提交
930

931
		let onBlur = () => {
932 933 934 935 936 937
			if (hasFocus) {
				loosingFocus = true;
				window.setTimeout(() => {
					if (loosingFocus) {
						loosingFocus = false;
						hasFocus = false;
938
						this._onDidBlur.fire();
939 940 941 942
					}
				}, 0);
			}
		};
E
Erich Gamma 已提交
943

944 945
		domEvent(element, EventType.FOCUS, true)(onFocus, null, this.disposables);
		domEvent(element, EventType.BLUR, true)(onBlur, null, this.disposables);
946
	}
E
Erich Gamma 已提交
947

948 949 950 951
	dispose(): void {
		this.disposables = dispose(this.disposables);
		this._onDidFocus.dispose();
		this._onDidBlur.dispose();
952 953 954
	}
}

955
export function trackFocus(element: HTMLElement | Window): IFocusTracker {
956
	return new FocusTracker(element);
E
Erich Gamma 已提交
957 958
}

J
Joao Moreno 已提交
959 960 961
export function append<T extends Node>(parent: HTMLElement, ...children: T[]): T {
	children.forEach(child => parent.appendChild(child));
	return children[children.length - 1];
E
Erich Gamma 已提交
962 963
}

964 965 966 967 968
export function prepend<T extends Node>(parent: HTMLElement, child: T): T {
	parent.insertBefore(child, parent.firstChild);
	return child;
}

B
Benjamin Pasero 已提交
969
const SELECTOR_REGEX = /([\w\-]+)?(#([\w\-]+))?((.([\w\-]+))*)/;
E
Erich Gamma 已提交
970 971

// Similar to builder, but much more lightweight
972
export function $<T extends HTMLElement>(description: string, attrs?: { [key: string]: any; }, ...children: (Node | string)[]): T {
B
Benjamin Pasero 已提交
973
	let match = SELECTOR_REGEX.exec(description);
E
Erich Gamma 已提交
974 975 976 977 978

	if (!match) {
		throw new Error('Bad use of emmet');
	}

B
Benjamin Pasero 已提交
979
	let result = document.createElement(match[1] || 'div');
J
Joao Moreno 已提交
980

A
Alex Dima 已提交
981 982 983 984 985 986
	if (match[3]) {
		result.id = match[3];
	}
	if (match[4]) {
		result.className = match[4].replace(/\./g, ' ').trim();
	}
E
Erich Gamma 已提交
987

J
Joao Moreno 已提交
988 989
	Object.keys(attrs || {}).forEach(name => {
		if (/^on\w+$/.test(name)) {
A
Alex Dima 已提交
990
			(<any>result)[name] = attrs[name];
J
Joao Moreno 已提交
991 992 993 994 995 996
		} else if (name === 'selected') {
			const value = attrs[name];
			if (value) {
				result.setAttribute(name, 'true');
			}

J
Joao Moreno 已提交
997 998 999 1000
		} else {
			result.setAttribute(name, attrs[name]);
		}
	});
J
Joao Moreno 已提交
1001

J
Joao Moreno 已提交
1002 1003 1004 1005 1006 1007 1008 1009 1010
	children
		.filter(child => !!child)
		.forEach(child => {
			if (child instanceof Node) {
				result.appendChild(child);
			} else {
				result.appendChild(document.createTextNode(child as string));
			}
		});
J
Joao Moreno 已提交
1011

J
Joao Moreno 已提交
1012
	return result as T;
A
Alex Dima 已提交
1013
}
1014

1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
export function join(nodes: Node[], separator: Node | string): Node[] {
	const result: Node[] = [];

	nodes.forEach((node, index) => {
		if (index > 0) {
			if (separator instanceof Node) {
				result.push(separator.cloneNode());
			} else {
				result.push(document.createTextNode(separator));
			}
		}

		result.push(node);
	});

	return result;
}

J
Joao Moreno 已提交
1033
export function show(...elements: HTMLElement[]): void {
J
Johannes Rieken 已提交
1034
	for (let element of elements) {
S
Sandy Armstrong 已提交
1035
		element.style.display = '';
1036
		element.removeAttribute('aria-hidden');
J
Joao Moreno 已提交
1037
	}
1038 1039
}

J
Joao Moreno 已提交
1040
export function hide(...elements: HTMLElement[]): void {
J
Johannes Rieken 已提交
1041
	for (let element of elements) {
J
Joao Moreno 已提交
1042
		element.style.display = 'none';
1043
		element.setAttribute('aria-hidden', 'true');
J
Joao Moreno 已提交
1044
	}
1045
}
1046

B
Benjamin Pasero 已提交
1047
function findParentWithAttribute(node: Node, attribute: string): HTMLElement {
1048
	while (node) {
B
Benjamin Pasero 已提交
1049
		if (node instanceof HTMLElement && node.hasAttribute(attribute)) {
1050 1051 1052
			return node;
		}

B
Benjamin Pasero 已提交
1053
		node = node.parentNode;
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
	}

	return null;
}

export function removeTabIndexAndUpdateFocus(node: HTMLElement): void {
	if (!node || !node.hasAttribute('tabIndex')) {
		return;
	}

	// If we are the currently focused element and tabIndex is removed,
	// standard DOM behavior is to move focus to the <body> element. We
	// typically never want that, rather put focus to the closest element
	// in the hierarchy of the parent DOM nodes.
	if (document.activeElement === node) {
		let parentFocusable = findParentWithAttribute(node.parentElement, 'tabIndex');
		if (parentFocusable) {
			parentFocusable.focus();
		}
	}

B
fix npe  
Benjamin Pasero 已提交
1075
	node.removeAttribute('tabindex');
A
Alex Dima 已提交
1076
}
1077 1078 1079

export function getElementsByTagName(tag: string): HTMLElement[] {
	return Array.prototype.slice.call(document.getElementsByTagName(tag), 0);
J
Joao Moreno 已提交
1080 1081
}

1082
export function finalHandler<T extends DOMEvent>(fn: (event: T) => any): (event: T) => any {
J
Joao Moreno 已提交
1083 1084 1085 1086 1087
	return e => {
		e.preventDefault();
		e.stopPropagation();
		fn(e);
	};
1088 1089 1090 1091 1092 1093
}

export function domContentLoaded(): TPromise<any> {
	return new TPromise<any>((c, e) => {
		const readyState = document.readyState;
		if (readyState === 'complete' || (document && document.body !== null)) {
1094
			platform.setImmediate(c);
1095 1096 1097 1098
		} else {
			window.addEventListener('DOMContentLoaded', c, false);
		}
	});
1099
}
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112

/**
 * Find a value usable for a dom node size such that the likelihood that it would be
 * displayed with constant screen pixels size is as high as possible.
 *
 * e.g. We would desire for the cursors to be 2px (CSS px) wide. Under a devicePixelRatio
 * of 1.25, the cursor will be 2.5 screen pixels wide. Depending on how the dom node aligns/"snaps"
 * with the screen pixels, it will sometimes be rendered with 2 screen pixels, and sometimes with 3 screen pixels.
 */
export function computeScreenAwareSize(cssPx: number): number {
	const screenPx = window.devicePixelRatio * cssPx;
	return Math.max(1, Math.floor(screenPx)) / window.devicePixelRatio;
}
1113 1114 1115 1116 1117 1118 1119 1120 1121

/**
 * See https://github.com/Microsoft/monaco-editor/issues/601
 * To protect against malicious code in the linked site, particularly phishing attempts,
 * the window.opener should be set to null to prevent the linked site from having access
 * to change the location of the current page.
 * See https://mathiasbynens.github.io/rel-noopener/
 */
export function windowOpenNoOpener(url: string): void {
A
Alex Dima 已提交
1122
	if (platform.isNative || browser.isEdgeWebView) {
1123
		// In VSCode, window.open() always returns null...
A
Alex Dima 已提交
1124
		// The same is true for a WebView (see https://github.com/Microsoft/monaco-editor/issues/628)
1125 1126 1127 1128
		window.open(url);
	} else {
		let newTab = window.open();
		if (newTab) {
M
Matt Bierner 已提交
1129
			(newTab as any).opener = null;
1130 1131 1132
			newTab.location.href = url;
		}
	}
1133
}