Skip to content

Commit 29ff429

Browse files
committed
chore(polyfill): clean up
1 parent 518e8d0 commit 29ff429

File tree

5 files changed

+80
-93
lines changed

5 files changed

+80
-93
lines changed

packages/canvas-polyfill/DOM/Document.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import {Element} from "./Element";
2-
import {HTMLVideoElement} from "./HTMLVideoElement";
3-
import {HTMLImageElement} from "./HTMLImageElement";
4-
import {HTMLCanvasElement} from "./HTMLCanvasElement";
5-
import {Text} from "./Text";
6-
import {Canvas} from "@nativescript/canvas";
7-
import {Frame} from '@nativescript/core';
1+
import { Element } from './Element';
2+
import { HTMLVideoElement } from './HTMLVideoElement';
3+
import { HTMLImageElement } from './HTMLImageElement';
4+
import { HTMLCanvasElement } from './HTMLCanvasElement';
5+
import { Text } from './Text';
6+
import { Canvas } from '@nativescript/canvas';
7+
import { Frame } from '@nativescript/core';
88

99
export class Document extends Element {
1010
private body: any;
@@ -14,25 +14,23 @@ export class Document extends Element {
1414
private defaultView: any;
1515

1616
constructor() {
17-
super("#document");
18-
this.body = new Element("BODY");
19-
this.documentElement = new Element("HTML");
20-
this.readyState = "complete";
21-
this.head = new Element("HEAD");
17+
super('#document');
18+
this.body = new Element('BODY');
19+
this.documentElement = new Element('HTML');
20+
this.readyState = 'complete';
21+
this.head = new Element('HEAD');
2222
this.defaultView = (global as any).window;
2323
}
2424

2525
createElement(tagName) {
26-
switch ((tagName || "").toLowerCase()) {
27-
case "video":
26+
switch ((tagName || '').toLowerCase()) {
27+
case 'video':
2828
return new HTMLVideoElement();
29-
case "img":
29+
case 'img':
3030
return new HTMLImageElement();
31-
case "canvas":
32-
const canvas = new HTMLCanvasElement();
33-
canvas._canvas = Canvas.createCustomView();
34-
return canvas;
35-
case "iframe":
31+
case 'canvas':
32+
return new HTMLCanvasElement();
33+
case 'iframe':
3634
// Return nothing to keep firebase working.
3735
return null;
3836
default:
@@ -71,15 +69,15 @@ export class Document extends Element {
7169
if (topmost) {
7270
const nativeElement = topmost.getViewById(id);
7371
if (nativeElement) {
74-
const element = new Element("div");
72+
const element = new Element('div');
7573
element.nativeElement = nativeElement;
7674
return element;
7775
}
7876
}
79-
return new Element("div");
77+
return new Element('div');
8078
}
8179

82-
querySelector(selector){
80+
querySelector(selector) {
8381
return new Element(selector);
8482
}
85-
}
83+
}
Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,21 @@
1-
import { Node } from "./Node";
2-
import { Canvas } from '@nativescript/canvas';
1+
import { Node } from './Node';
32

43
export class Element extends Node {
54
private doc: any;
65
private _classList: any;
7-
private _width: number;
8-
private _height: number;
96
namespaceURI: any;
107
nativeElement: any;
11-
12-
constructor(tagName, canvas = undefined) {
8+
private _width: number;
9+
private _height: number;
10+
constructor(tagName) {
1311
super(tagName.toUpperCase());
1412

1513
this.doc = {
1614
body: {
17-
innerHTML: "",
15+
innerHTML: '',
1816
},
1917
};
2018
this._classList = new Set();
21-
if (tagName.toLowerCase() === 'canvas') {
22-
if (canvas instanceof Canvas) {
23-
this._canvas = canvas;
24-
} else {
25-
this._canvas = Canvas.createCustomView();
26-
}
27-
}
2819
}
2920

3021
get classList() {
@@ -35,17 +26,13 @@ export class Element extends Node {
3526
return this.nodeName;
3627
}
3728

38-
setAttribute() {
39-
}
29+
setAttribute() {}
4030

41-
removeAttribute() {
42-
}
31+
removeAttribute() {}
4332

44-
setAttributeNS() {
45-
}
33+
setAttributeNS() {}
4634

47-
removeAttributeNS() {
48-
}
35+
removeAttributeNS() {}
4936

5037
get clientWidth() {
5138
return this.innerWidth;
@@ -71,48 +58,23 @@ export class Element extends Node {
7158
return this.height;
7259
}
7360

74-
set width(value) {
75-
this._width = value as any;
76-
if (this._canvas) {
77-
this._canvas.width = value;
78-
}
79-
}
80-
8161
get width() {
82-
if (this._canvas) {
83-
return this._canvas.width;
84-
}
8562
return this._width;
8663
}
8764

88-
set height(value) {
89-
this._height = value as any;
90-
if (this._canvas) {
91-
this._canvas.height = value;
92-
}
65+
set width(value: number) {
66+
this._width = value;
9367
}
9468

9569
get height() {
96-
if (this._canvas) {
97-
return this._canvas.height;
98-
}
9970
return this._height;
10071
}
10172

102-
103-
toDataURL(type, encoderOptions) {
104-
if (!this._canvas) {
105-
return "";
106-
}
107-
return this._canvas.toDataURL(type, encoderOptions);
108-
}
109-
110-
111-
getContext(contextType, contextOptions, context) {
112-
return this._canvas.getContext(contextType, contextOptions);
73+
set height(value: number) {
74+
this._height = value;
11375
}
11476

11577
get ontouchstart() {
11678
return {};
11779
}
118-
}
80+
}
Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,42 @@
1-
import {Element} from "./Element";
1+
import { Canvas } from '@nativescript/canvas';
2+
import { Element } from './Element';
23

34
export class HTMLCanvasElement extends Element {
4-
constructor() {
5-
super("canvas");
6-
}
5+
constructor() {
6+
super('canvas');
7+
let canvas = undefined;
8+
if (arguments.length > 1) {
9+
canvas = arguments[1];
10+
}
11+
12+
if (canvas instanceof Canvas) {
13+
this._canvas = canvas;
14+
} else {
15+
this._canvas = Canvas.createCustomView();
16+
}
17+
}
18+
19+
set width(value) {
20+
this._canvas.width = value;
21+
}
22+
23+
get width() {
24+
return this._canvas.width;
25+
}
26+
27+
set height(value) {
28+
this._canvas.height = value;
29+
}
30+
31+
get height() {
32+
return this._canvas.height;
33+
}
34+
35+
toDataURL(type, encoderOptions) {
36+
return this._canvas.toDataURL(type, encoderOptions);
37+
}
38+
39+
getContext(contextType, contextOptions) {
40+
return this._canvas.getContext(contextType, contextOptions);
41+
}
742
}

packages/canvas-polyfill/DOM/HTMLImageElement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class HTMLImageElement extends Element {
7676
}
7777

7878
constructor(props?) {
79-
super('image');
79+
super('img');
8080
this._asset = new ImageAsset();
8181
this.__id = getUUID();
8282
this._onload = () => { };

packages/canvas-polyfill/DOM/HTMLVideoElement.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class HTMLVideoElement extends Element {
1515
this._video.removeEventListener(type, listener, useCapture);
1616
}
1717

18-
requestVideoFrameCallback(callback: Function){
18+
requestVideoFrameCallback(callback: Function) {
1919
this._video.requestVideoFrameCallback(callback);
2020
}
2121

@@ -66,23 +66,15 @@ export class HTMLVideoElement extends Element {
6666
}
6767

6868
get width() {
69-
if (this._video) {
70-
return this._video.width;
71-
}
72-
return this._video;
69+
return this._video.width as any;
7370
}
7471

7572
set height(value) {
76-
if (this._video) {
77-
this._video.height = value as any;
78-
}
73+
this._video.height = value as any;
7974
}
8075

8176
get height() {
82-
if (this._video) {
83-
return this._video.height;
84-
}
85-
return this._video;
77+
return this._video.height as any;
8678
}
8779

8880
play() {

0 commit comments

Comments
 (0)