fix(platform-browser): Update types for TypeScript nullability support

Closes #15898
This commit is contained in:
Miško Hevery
2017-03-24 09:59:41 -07:00
committed by Tobias Bosch
parent 01d93f3af8
commit 728c9d0632
52 changed files with 169 additions and 154 deletions

View File

@ -144,12 +144,12 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
return evt.defaultPrevented || evt.returnValue != null && !evt.returnValue;
}
getInnerHTML(el: HTMLElement): string { return el.innerHTML; }
getTemplateContent(el: Node): Node {
getTemplateContent(el: Node): Node|null {
return 'content' in el && el instanceof HTMLTemplateElement ? el.content : null;
}
getOuterHTML(el: HTMLElement): string { return el.outerHTML; }
nodeName(node: Node): string { return node.nodeName; }
nodeValue(node: Node): string { return node.nodeValue; }
nodeValue(node: Node): string|null { return node.nodeValue; }
type(node: HTMLInputElement): string { return node.type; }
content(node: Node): Node {
if (this.hasProperty(node, 'content')) {
@ -158,9 +158,9 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
return node;
}
}
firstChild(el: Node): Node { return el.firstChild; }
nextSibling(el: Node): Node { return el.nextSibling; }
parentElement(el: Node): Node { return el.parentNode; }
firstChild(el: Node): Node|null { return el.firstChild; }
nextSibling(el: Node): Node|null { return el.nextSibling; }
parentElement(el: Node): Node|null { return el.parentNode; }
childNodes(el: any): Node[] { return el.childNodes; }
childNodesAsList(el: Node): any[] {
const childNodes = el.childNodes;
@ -190,7 +190,7 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
}
insertAfter(parent: Node, ref: Node, node: any) { parent.insertBefore(node, ref.nextSibling); }
setInnerHTML(el: Element, value: string) { el.innerHTML = value; }
getText(el: Node): string { return el.textContent; }
getText(el: Node): string|null { return el.textContent; }
setText(el: Node, value: string) { el.textContent = value; }
getValue(el: any): string { return el.value; }
setValue(el: any, value: string) { el.value = value; }
@ -242,7 +242,7 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
element.style[stylename] = '';
}
getStyle(element: any, stylename: string): string { return element.style[stylename]; }
hasStyle(element: any, styleName: string, styleValue: string = null): boolean {
hasStyle(element: any, styleName: string, styleValue?: string|null): boolean {
const value = this.getStyle(element, styleName) || '';
return styleValue ? value == styleValue : value.length > 0;
}
@ -262,7 +262,7 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
hasAttributeNS(element: Element, ns: string, attribute: string): boolean {
return element.hasAttributeNS(ns, attribute);
}
getAttribute(element: Element, attribute: string): string {
getAttribute(element: Element, attribute: string): string|null {
return element.getAttribute(attribute);
}
getAttributeNS(element: Element, ns: string, name: string): string {
@ -335,7 +335,7 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
return _keyMap[key] || key;
}
getGlobalEventTarget(doc: Document, target: string): EventTarget {
getGlobalEventTarget(doc: Document, target: string): EventTarget|null {
if (target === 'window') {
return window;
}
@ -345,10 +345,11 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
if (target === 'body') {
return document.body;
}
return null;
}
getHistory(): History { return window.history; }
getLocation(): Location { return window.location; }
getBaseHref(doc: Document): string {
getBaseHref(doc: Document): string|null {
const href = getBaseElementHref();
return href == null ? null : relativePath(href);
}
@ -357,7 +358,7 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
setData(element: Element, name: string, value: string) {
this.setAttribute(element, 'data-' + name, value);
}
getData(element: Element, name: string): string {
getData(element: Element, name: string): string|null {
return this.getAttribute(element, 'data-' + name);
}
getComputedStyle(element: any): any { return getComputedStyle(element); }
@ -375,7 +376,7 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
supportsCookies(): boolean { return true; }
getCookie(name: string): string { return parseCookieValue(document.cookie, name); }
getCookie(name: string): string|null { return parseCookieValue(document.cookie, name); }
setCookie(name: string, value: string) {
// document.cookie is magical, assigning into it assigns/overrides one cookie value, but does
@ -384,10 +385,10 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
}
}
let baseElement: HTMLElement = null;
function getBaseElementHref(): string {
let baseElement: HTMLElement|null = null;
function getBaseElementHref(): string|null {
if (!baseElement) {
baseElement = document.querySelector('base');
baseElement = document.querySelector('base') !;
if (!baseElement) {
return null;
}
@ -406,7 +407,7 @@ function relativePath(url: any): string {
'/' + urlParsingNode.pathname;
}
export function parseCookieValue(cookieStr: string, name: string): string {
export function parseCookieValue(cookieStr: string, name: string): string|null {
name = encodeURIComponent(name);
for (const cookie of cookieStr.split(';')) {
const eqIndex = cookie.indexOf('=');
@ -423,7 +424,7 @@ export function setValueOnPath(global: any, path: string, value: any) {
const parts = path.split('.');
let obj: any = global;
while (parts.length > 1) {
const name = parts.shift();
const name = parts.shift() !;
if (obj.hasOwnProperty(name) && obj[name] != null) {
obj = obj[name];
} else {
@ -433,5 +434,5 @@ export function setValueOnPath(global: any, path: string, value: any) {
if (obj === undefined || obj === null) {
obj = {};
}
obj[parts.shift()] = value;
obj[parts.shift() !] = value;
}

View File

@ -17,8 +17,8 @@ import {DomAdapter} from '../dom/dom_adapter';
* can introduce XSS risks.
*/
export abstract class GenericBrowserDomAdapter extends DomAdapter {
private _animationPrefix: string = null;
private _transitionEnd: string = null;
private _animationPrefix: string|null = null;
private _transitionEnd: string|null = null;
constructor() {
super();
try {

View File

@ -40,7 +40,7 @@ export class BrowserPlatformLocation extends PlatformLocation {
get location(): Location { return this._location; }
getBaseHrefFromDOM(): string { return getDOM().getBaseHref(this._doc); }
getBaseHrefFromDOM(): string { return getDOM().getBaseHref(this._doc) !; }
onPopState(fn: LocationChangeListener): void {
getDOM().getGlobalEventTarget(this._doc, 'window').addEventListener('popstate', fn, false);

View File

@ -39,7 +39,7 @@ export class Meta {
private _dom: DomAdapter;
constructor(@Inject(DOCUMENT) private _doc: any) { this._dom = getDOM(); }
addTag(tag: MetaDefinition, forceCreation: boolean = false): HTMLMetaElement {
addTag(tag: MetaDefinition, forceCreation: boolean = false): HTMLMetaElement|null {
if (!tag) return null;
return this._getOrCreateElement(tag, forceCreation);
}
@ -54,7 +54,7 @@ export class Meta {
}, []);
}
getTag(attrSelector: string): HTMLMetaElement {
getTag(attrSelector: string): HTMLMetaElement|null {
if (!attrSelector) return null;
return this._dom.querySelector(this._doc, `meta[${attrSelector}]`);
}
@ -65,17 +65,17 @@ export class Meta {
return list ? [].slice.call(list) : [];
}
updateTag(tag: MetaDefinition, selector?: string): HTMLMetaElement {
updateTag(tag: MetaDefinition, selector?: string): HTMLMetaElement|null {
if (!tag) return null;
selector = selector || this._parseSelector(tag);
const meta: HTMLMetaElement = this.getTag(selector);
const meta: HTMLMetaElement = this.getTag(selector) !;
if (meta) {
return this._setMetaElementAttributes(tag, meta);
}
return this._getOrCreateElement(tag, true);
}
removeTag(attrSelector: string): void { this.removeTagElement(this.getTag(attrSelector)); }
removeTag(attrSelector: string): void { this.removeTagElement(this.getTag(attrSelector) !); }
removeTagElement(meta: HTMLMetaElement): void {
if (meta) {
@ -87,7 +87,7 @@ export class Meta {
HTMLMetaElement {
if (!forceCreation) {
const selector: string = this._parseSelector(meta);
const elem: HTMLMetaElement = this.getTag(selector);
const elem: HTMLMetaElement = this.getTag(selector) !;
// It's allowed to have multiple elements with the same name so it's not enough to
// just check that element with the same name already present on the page. We also need to
// check if element has tag attributes

View File

@ -49,7 +49,7 @@ export class BrowserGetTestability implements GetTestability {
}
findTestabilityInTree(registry: TestabilityRegistry, elem: any, findInAncestors: boolean):
Testability {
Testability|null {
if (elem == null) {
return null;
}