提交 d9990fd5 编写于 作者: A Alex Dima

Remove unnecessary browserService and mockDom

上级 954f86dc
......@@ -68,22 +68,6 @@ export const canUseTranslate3d = !isIE9 && !isFirefox;
export const enableEmptySelectionClipboard = isWebKit;
let _disablePushState = false;
/**
* Returns if the browser supports the history.pushState function or not.
*/
export function canPushState() {
return (!_disablePushState && globals.history && globals.history.pushState);
}
/**
* Helpful when we detect that pushing state does not work for some reason (e.g. FF prevents pushState for security reasons in some cases)
*/
export function disablePushState() {
_disablePushState = true;
}
/**
* Returns if the browser supports CSS 3 animations.
*/
......@@ -116,10 +100,6 @@ export function hasCSSAnimationSupport() {
return this._hasCSSAnimationSupport;
}
export function isInWebWorker(): boolean {
return !globals.document && typeof ((<any>globals).importScripts) !== 'undefined';
}
export function supportsExecCommand(command: string): boolean {
return (
(isIE11orEarlier || Platform.isNative)
......
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import {isInWebWorker} from 'vs/base/browser/browser';
export interface IBrowserServiceData {
document: Document;
window: Window;
isHTMLElement: (o: any) => boolean;
}
export interface IBrowserService extends IBrowserServiceData {
/**
* Mock the DOM with dummy objects
*/
mock(source: IBrowserServiceData): void;
/**
* Restore the normal DOM
*/
restore(): void;
}
export function regularIsHTMLElement(o: any): boolean {
if (typeof HTMLElement === 'object') {
return o instanceof HTMLElement;
}
return o && typeof o === 'object' && o.nodeType === 1 && typeof o.nodeName === 'string';
}
class BrowserService implements IBrowserService {
public document: Document;
public window: Window;
public isHTMLElement: (o: any) => boolean;
constructor() {
this.restore();
}
public mock(source: IBrowserServiceData): void {
this.document = source.document;
this.window = source.window;
this.isHTMLElement = source.isHTMLElement;
}
public restore(): void {
this.isHTMLElement = regularIsHTMLElement;
if (isInWebWorker()) {
this.document = null;
this.window = null;
} else {
this.document = window.document;
this.window = window;
}
}
}
const browserService = new BrowserService();
export function getService(): IBrowserService {
return browserService;
}
\ No newline at end of file
......@@ -11,7 +11,6 @@ import {IDisposable, dispose} from 'vs/base/common/lifecycle';
import strings = require('vs/base/common/strings');
import assert = require('vs/base/common/assert');
import DOM = require('vs/base/browser/dom');
import BrowserService = require('vs/base/browser/browserService');
/**
* Welcome to the monaco builder. The recommended way to use it is:
......@@ -52,7 +51,7 @@ export interface QuickBuilder {
export function withElementById(id: string, offdom?: boolean): Builder {
assert.ok(types.isString(id), 'Expected String as parameter');
let element = BrowserService.getService().document.getElementById(id);
let element = document.getElementById(id);
if (element) {
return new Builder(element, offdom);
}
......@@ -136,7 +135,6 @@ export class Builder implements IDisposable {
private createdElements: HTMLElement[];
private toUnbind: { [type: string]: IDisposable[]; };
private captureToUnbind: { [type: string]: IDisposable[]; };
private browserService: BrowserService.IBrowserService;
constructor(element?: HTMLElement, offdom?: boolean) {
this.offdom = offdom;
......@@ -148,7 +146,6 @@ export class Builder implements IDisposable {
this.toUnbind = {};
this.captureToUnbind = {};
this.browserService = BrowserService.getService();
}
/**
......@@ -476,7 +473,7 @@ export class Builder implements IDisposable {
private doElement(name: string, attributesOrFn?: any, fn?: (builder: Builder) => void): Builder {
// Create Element
let element = this.browserService.document.createElement(name);
let element = document.createElement(name);
this.currentElement = element;
// Off-DOM: Remember in array of created elements
......@@ -521,7 +518,7 @@ export class Builder implements IDisposable {
* Returns true if the current element of this builder is the active element.
*/
public hasFocus(): boolean {
let activeElement: Element = this.browserService.document.activeElement;
let activeElement: Element = document.activeElement;
return (activeElement === this.currentElement);
}
......@@ -1535,7 +1532,7 @@ export class Builder implements IDisposable {
else {
// if there are elements inside this node, append the string as a new text node
// to avoid wiping out the innerHTML and replacing it with only text content
this.currentElement.appendChild(this.browserService.document.createTextNode(text));
this.currentElement.appendChild(document.createTextNode(text));
}
} else {
this.currentElement.textContent = text;
......@@ -1848,24 +1845,24 @@ export class Builder implements IDisposable {
public getClientArea(): Dimension {
// 0.) Try with DOM getDomNodePosition
if (this.currentElement !== this.browserService.document.body) {
if (this.currentElement !== document.body) {
let dimensions = DOM.getDomNodePosition(this.currentElement);
return new Dimension(dimensions.width, dimensions.height);
}
// 1.) Try innerWidth / innerHeight
if (this.browserService.window.innerWidth && this.browserService.window.innerHeight) {
return new Dimension(this.browserService.window.innerWidth, this.browserService.window.innerHeight);
if (window.innerWidth && window.innerHeight) {
return new Dimension(window.innerWidth, window.innerHeight);
}
// 2.) Try with document.body.clientWidth / document.body.clientHeigh
if (this.browserService.document.body && this.browserService.document.body.clientWidth && this.browserService.document.body.clientWidth) {
return new Dimension(this.browserService.document.body.clientWidth, this.browserService.document.body.clientHeight);
if (document.body && document.body.clientWidth && document.body.clientWidth) {
return new Dimension(document.body.clientWidth, document.body.clientHeight);
}
// 3.) Try with document.documentElement.clientWidth / document.documentElement.clientHeight
if (this.browserService.document.documentElement && this.browserService.document.documentElement.clientWidth && this.browserService.document.documentElement.clientHeight) {
return new Dimension(this.browserService.document.documentElement.clientWidth, this.browserService.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');
......@@ -2154,7 +2151,7 @@ export let $: QuickBuilder = function(arg?: any): Builder {
// Use the argument as HTML code
if (arg[0] === '<') {
let element: Node;
let container = BrowserService.getService().document.createElement('div');
let container = document.createElement('div');
container.innerHTML = strings.format.apply(strings, arguments);
if (container.children.length === 0) {
......
......@@ -10,7 +10,6 @@ import {EventEmitter} from 'vs/base/common/eventEmitter';
import {Disposable, IDisposable} from 'vs/base/common/lifecycle';
import {isObject} from 'vs/base/common/types';
import {isChrome, isWebKit} from 'vs/base/browser/browser';
import {getService} from 'vs/base/browser/browserService';
import {IKeyboardEvent, StandardKeyboardEvent} from 'vs/base/browser/keyboardEvent';
import {IMouseEvent, StandardMouseEvent} from 'vs/base/browser/mouseEvent';
......@@ -764,7 +763,10 @@ export function removeCSSRulesWithPrefix(ruleName: string, style = sharedStyle):
}
export function isHTMLElement(o: any): o is HTMLElement {
return getService().isHTMLElement(o);
if (typeof HTMLElement === 'object') {
return o instanceof HTMLElement;
}
return o && typeof o === 'object' && o.nodeType === 1 && typeof o.nodeName === 'string';
}
export const EventType = {
......
......@@ -7,7 +7,6 @@
import {TimeoutTimer} from 'vs/base/common/async';
import {EventEmitter} from 'vs/base/common/eventEmitter';
import {Disposable, IDisposable} from 'vs/base/common/lifecycle';
import {getService} from 'vs/base/browser/browserService';
import * as dom from 'vs/base/browser/dom';
export enum UserStatus {
......@@ -34,8 +33,8 @@ export class IdleMonitor extends Disposable {
this._idleTime = idleTime;
this._eventEmitter = this._register(new EventEmitter());
this._register(dom.addDisposableListener(getService().document, 'mousemove', () => this._onUserActive()));
this._register(dom.addDisposableListener(getService().document, 'keydown', () => this._onUserActive()));
this._register(dom.addDisposableListener(document, 'mousemove', () => this._onUserActive()));
this._register(dom.addDisposableListener(document, 'keydown', () => this._onUserActive()));
this._onUserActive();
}
......
......@@ -9,7 +9,6 @@ import 'vs/css!./inputBox';
import nls = require('vs/nls');
import * as Bal from 'vs/base/browser/browser';
import * as dom from 'vs/base/browser/dom';
import * as browser from 'vs/base/browser/browserService';
import {IHTMLContentElement} from 'vs/base/common/htmlContent';
import {renderHtml} from 'vs/base/browser/htmlContentRenderer';
import aria = require('vs/base/browser/ui/aria/aria');
......@@ -204,7 +203,7 @@ export class InputBox extends Widget {
}
public hasFocus(): boolean {
return browser.getService().document.activeElement === this.input;
return document.activeElement === this.input;
}
public select(range: IRange = null): void {
......
......@@ -19,35 +19,29 @@ suite('Browsers', () => {
var isChrome = browser.isChrome;
var isSafari = browser.isSafari;
var canPushState = browser.canPushState();
var hasCSSAnimations = browser.hasCSSAnimationSupport();
var browserCount = 0;
if (isOpera) {
browserCount++;
assert(canPushState);
}
if (isIE11orEarlier) {
browserCount++;
}
if (isFirefox) {
browserCount++;
assert(canPushState);
assert(hasCSSAnimations);
}
if (isWebKit) {
browserCount++;
assert(canPushState);
assert(hasCSSAnimations);
}
if (isChrome) {
browserCount++;
assert(canPushState);
assert(hasCSSAnimations);
}
if (isSafari) {
browserCount++;
assert(canPushState);
assert(hasCSSAnimations);
}
});
......
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as assert from 'assert';
import BrowserService = require('vs/base/browser/browserService');
import { MockBrowserServiceData } from 'vs/base/test/browser/mockBrowserService';
suite('BrowserService', () => {
test('Mocking of Window', () => {
try {
var service = BrowserService.getService();
service.mock(new MockBrowserServiceData());
var w = <any> service.window;
w.testValue = 42;
service.restore();
w = <any> service.window;
assert.strictEqual(w.testValue, undefined);
}
finally {
if(service) {
service.restore();
}
}
});
});
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { MockDocument, MockWindow, MockElement } from 'vs/base/test/browser/mockDom';
import browserService = require('vs/base/browser/browserService');
function mockedIsHTMLElement(o:any): boolean {
if (typeof HTMLElement === 'object') {
return o instanceof HTMLElement || o instanceof MockElement;
}
return o && typeof o === 'object' && o.nodeType === 1 && typeof o.nodeName === 'string';
}
export class MockBrowserServiceData implements browserService.IBrowserServiceData {
public document:Document;
public window:Window;
public isHTMLElement: (o:any)=>boolean;
constructor() {
this.document = <any> new MockDocument();
this.window = <any> new MockWindow();
this.isHTMLElement = mockedIsHTMLElement;
}
}
\ No newline at end of file
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as assert from 'assert';
import MockDom = require('vs/base/test/browser/mockDom');
suite('MockDom', () => {
test('Create Element', () => {
var doc = new MockDom.MockDocument();
var div = doc.createElement('div');
div.textContent = 'test';
assert.strictEqual(div.textContent, 'test');
assert.strictEqual(div.tagName, 'div');
});
test('Events', () => {
var doc = new MockDom.MockDocument();
var div = doc.createElement('div');
var fakeEvent = <Event>{ type: 'test'};
var count = 0;
var callback = function(event:any) {
assert.strictEqual(event, fakeEvent);
count++;
};
div.addEventListener('test', callback);
div.dispatchEvent(fakeEvent);
assert.strictEqual(count, 1);
div.removeEventListener('test', callback);
div.dispatchEvent(fakeEvent);
assert.strictEqual(count, 1);
});
test('Create elements via innerHTML', () => {
var doc = new MockDom.MockDocument();
var div = doc.createElement('div');
div.innerHTML = '<div id="testId"><span class="testClass">test</span></div>';
assert.strictEqual(div.children.length, 1);
assert.strictEqual(div.childNodes[0], div.children[0]);
assert.strictEqual(div.children[0], div.firstElementChild);
assert.strictEqual(div.firstElementChild, div.firstChild);
var child = <HTMLElement> div.children[0];
assert.strictEqual(child.id, 'testId');
assert.strictEqual(child.getAttribute('id'), 'testId');
assert.strictEqual(child.childElementCount, 1);
var grandchild = <HTMLElement> child.firstElementChild;
assert.strictEqual(grandchild.tagName, 'span');
assert.strictEqual(grandchild.className, 'testClass');
assert.strictEqual(grandchild.textContent, 'test');
});
test('Convert elements to innerHTML', () => {
var doc = new MockDom.MockDocument();
var div = doc.createElement('div');
var child = doc.createElement('div');
child.id = 'testId';
var grandchild = doc.createElement('span');
grandchild.className = 'testClass';
grandchild.textContent = 'test';
child.appendChild(grandchild);
div.appendChild(child);
assert.strictEqual(div.innerHTML, '<div id="testId"><span class="testClass">test</span></div>');
assert.strictEqual(div.outerHTML, '<div><div id="testId"><span class="testClass">test</span></div></div>');
});
});
\ No newline at end of file
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册