Skip to content

Commit 18ffa23

Browse files
committed
chore(polyfil): polish
1 parent 08fde0d commit 18ffa23

File tree

8 files changed

+258
-395
lines changed

8 files changed

+258
-395
lines changed

packages/canvas-polyfill/DOM/Document.ts

Lines changed: 9 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { HTMLCanvasElement } from './HTMLCanvasElement';
55
import { HTMLDivElement } from './HTMLDivElement';
66
import { Text } from './Text';
77
import { Canvas, FontFaceSet } from '@nativescript/canvas';
8-
import { ContentView, Frame, StackLayout, eachDescendant, knownFolders } from '@nativescript/core';
8+
import { ContentView, Frame, StackLayout, knownFolders } from '@nativescript/core';
99
import { Node } from './Node';
1010
import { Element, parseChildren } from './Element';
1111
import { SVGSVGElement } from './svg/SVGSVGElement';
@@ -15,96 +15,7 @@ import { HTMLCollection } from './HTMLCollection';
1515
import { HTMLHtmlElement } from './HTMLHtmlElement';
1616
import { HTMLHeadElement } from './HTMLHeadElement';
1717
import { HTMLBodyElement } from './HTMLBodyElement';
18-
19-
function getElementsByClassName(v, clsName) {
20-
var retVal = [];
21-
if (!v) {
22-
return retVal;
23-
}
24-
25-
if (v.classList.contains(clsName)) {
26-
retVal.push(v);
27-
}
28-
29-
const classNameCallback = function (child) {
30-
if (child.classList.contains(clsName)) {
31-
retVal.push(child);
32-
}
33-
34-
// Android patch for ListView
35-
if (child._realizedItems && child._realizedItems.size !== child._childrenCount) {
36-
for (var key in child._realizedItems) {
37-
if (child._realizedItems.hasOwnProperty(key)) {
38-
classNameCallback(child._realizedItems[key]);
39-
}
40-
}
41-
}
42-
43-
return true;
44-
};
45-
46-
eachDescendant(v, classNameCallback);
47-
48-
// Android patch for ListView
49-
if (v._realizedItems && v._realizedItems.size !== v._childrenCount) {
50-
for (var key in v._realizedItems) {
51-
if (v._realizedItems.hasOwnProperty(key)) {
52-
classNameCallback(v._realizedItems[key]);
53-
}
54-
}
55-
}
56-
57-
return retVal;
58-
}
59-
60-
function getElementsByTagName(v, tagName) {
61-
// TagName is case-Insensitive
62-
var tagNameLC = tagName.toLowerCase();
63-
64-
var retVal = [],
65-
allTags = false;
66-
if (!v) {
67-
return retVal;
68-
}
69-
70-
if (tagName === '*') {
71-
allTags = true;
72-
}
73-
74-
if ((v.typeName && v.typeName.toLowerCase() === tagNameLC) || allTags) {
75-
retVal.push(v);
76-
}
77-
78-
const tagNameCallback = function (child) {
79-
if ((child.typeName && child.typeName.toLowerCase() === tagNameLC) || allTags) {
80-
retVal.push(child);
81-
}
82-
83-
// Android patch for ListView
84-
if (child._realizedItems && child._realizedItems.size !== child._childrenCount) {
85-
for (var key in child._realizedItems) {
86-
if (child._realizedItems.hasOwnProperty(key)) {
87-
tagNameCallback(child._realizedItems[key]);
88-
}
89-
}
90-
}
91-
92-
return true;
93-
};
94-
95-
eachDescendant(v, tagNameCallback);
96-
97-
// Android patch for ListView
98-
if (v._realizedItems && v._realizedItems.size !== v._childrenCount) {
99-
for (var key in v._realizedItems) {
100-
if (v._realizedItems.hasOwnProperty(key)) {
101-
tagNameCallback(v._realizedItems[key]);
102-
}
103-
}
104-
}
105-
106-
return retVal;
107-
}
18+
import { domGetElementsByClassName, domGetElementsByTagName } from './domUtils';
10819

10920
export class Document extends Node {
11021
readonly body: Element = null;
@@ -239,10 +150,10 @@ export class Document extends Node {
239150
const topmost = Frame.topmost();
240151
if (topmost) {
241152
let nativeElement;
242-
if (topmost.currentPage && topmost.currentPage.modal) {
243-
nativeElement = topmost.getViewById(id);
244-
} else {
153+
if (topmost.currentPage?.modal) {
245154
nativeElement = topmost.currentPage.modal.getViewById(id);
155+
} else {
156+
nativeElement = topmost.currentPage?.getViewById(id) ?? topmost.getViewById(id);
246157
}
247158

248159
if (nativeElement) {
@@ -269,15 +180,8 @@ export class Document extends Node {
269180
}
270181
const topmost = Frame.topmost();
271182
if (topmost) {
272-
let view;
273-
if (topmost.currentPage && topmost.currentPage.modal) {
274-
view = topmost;
275-
} else {
276-
view = topmost.currentPage.modal;
277-
}
278-
const elements = getElementsByTagName(view, tagname);
279-
280-
return elements;
183+
const view = topmost.currentPage?.modal ?? topmost.currentPage ?? topmost;
184+
return domGetElementsByTagName(view, tagname);
281185
}
282186
return [];
283187
}
@@ -288,15 +192,8 @@ export class Document extends Node {
288192
}
289193
const topmost = Frame.topmost();
290194
if (topmost) {
291-
let view;
292-
if (topmost.currentPage && topmost.currentPage.modal) {
293-
view = topmost;
294-
} else {
295-
view = topmost.currentPage.modal;
296-
}
297-
const elements = getElementsByClassName(view, className);
298-
299-
return elements;
195+
const view = topmost.currentPage?.modal ?? topmost.currentPage ?? topmost;
196+
return domGetElementsByClassName(view, className);
300197
}
301198
return [];
302199
}

packages/canvas-polyfill/DOM/Element.ts

Lines changed: 9 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Node } from './Node';
2-
import { eachDescendant, Frame, StackLayout, View, ViewBase } from '@nativescript/core';
2+
import { Frame, StackLayout, View, ViewBase } from '@nativescript/core';
33
import setValue from 'set-value';
44
import querySelector from 'query-selector';
55
import { HTMLCollection } from './HTMLCollection';
66
import { Canvas } from '@nativescript/canvas';
7+
import { domGetElementsByClassName, domGetElementsByTagName } from './domUtils';
78
declare const NSCanvas;
89

910
export class DOMRectReadOnly {
@@ -119,96 +120,6 @@ export function parseChildren(element, children) {
119120
}
120121
}
121122

122-
function getElementsByClassName(v, clsName) {
123-
var retVal = [];
124-
if (!v) {
125-
return retVal;
126-
}
127-
128-
if (v.classList.contains(clsName)) {
129-
retVal.push(v);
130-
}
131-
132-
const classNameCallback = function (child) {
133-
if (child.classList.contains(clsName)) {
134-
retVal.push(child);
135-
}
136-
137-
// Android patch for ListView
138-
if (child._realizedItems && child._realizedItems.size !== child._childrenCount) {
139-
for (var key in child._realizedItems) {
140-
if (child._realizedItems.hasOwnProperty(key)) {
141-
classNameCallback(child._realizedItems[key]);
142-
}
143-
}
144-
}
145-
146-
return true;
147-
};
148-
149-
eachDescendant(v, classNameCallback);
150-
151-
// Android patch for ListView
152-
if (v._realizedItems && v._realizedItems.size !== v._childrenCount) {
153-
for (var key in v._realizedItems) {
154-
if (v._realizedItems.hasOwnProperty(key)) {
155-
classNameCallback(v._realizedItems[key]);
156-
}
157-
}
158-
}
159-
160-
return retVal;
161-
}
162-
163-
function getElementsByTagName(v, tagName) {
164-
// TagName is case-Insensitive
165-
var tagNameLC = tagName.toLowerCase();
166-
167-
var retVal = [],
168-
allTags = false;
169-
if (!v) {
170-
return retVal;
171-
}
172-
173-
if (tagName === '*') {
174-
allTags = true;
175-
}
176-
177-
if ((v.typeName && v.typeName.toLowerCase() === tagNameLC) || allTags) {
178-
retVal.push(v);
179-
}
180-
181-
const tagNameCallback = function (child) {
182-
if ((child.typeName && child.typeName.toLowerCase() === tagNameLC) || allTags) {
183-
retVal.push(child);
184-
}
185-
186-
// Android patch for ListView
187-
if (child._realizedItems && child._realizedItems.size !== child._childrenCount) {
188-
for (var key in child._realizedItems) {
189-
if (child._realizedItems.hasOwnProperty(key)) {
190-
tagNameCallback(child._realizedItems[key]);
191-
}
192-
}
193-
}
194-
195-
return true;
196-
};
197-
198-
eachDescendant(v, tagNameCallback);
199-
200-
// Android patch for ListView
201-
if (v._realizedItems && v._realizedItems.size !== v._childrenCount) {
202-
for (var key in v._realizedItems) {
203-
if (v._realizedItems.hasOwnProperty(key)) {
204-
tagNameCallback(v._realizedItems[key]);
205-
}
206-
}
207-
}
208-
209-
return retVal;
210-
}
211-
212123
type NativeElement = View & { __domElement?: Element; isConnected?: boolean };
213124
export class Element extends Node {
214125
private _classList = new Set();
@@ -340,10 +251,10 @@ export class Element extends Node {
340251
const topmost = Frame.topmost();
341252
if (topmost) {
342253
let nativeElement;
343-
if (topmost.currentPage && topmost.currentPage.modal) {
344-
nativeElement = topmost.getViewById(id);
345-
} else {
254+
if (topmost.currentPage?.modal) {
346255
nativeElement = topmost.currentPage.modal.getViewById(id);
256+
} else {
257+
nativeElement = topmost.currentPage?.getViewById(id) ?? topmost.getViewById(id);
347258
}
348259

349260
if (nativeElement) {
@@ -370,15 +281,8 @@ export class Element extends Node {
370281
}
371282
const topmost = Frame.topmost();
372283
if (topmost) {
373-
let view;
374-
if (topmost.currentPage && topmost.currentPage.modal) {
375-
view = topmost;
376-
} else {
377-
view = topmost.currentPage.modal;
378-
}
379-
const elements = getElementsByTagName(view, tagname);
380-
381-
return elements;
284+
const view = topmost.currentPage?.modal ?? topmost.currentPage ?? topmost;
285+
return domGetElementsByTagName(view, tagname);
382286
}
383287
return [];
384288
}
@@ -393,15 +297,8 @@ export class Element extends Node {
393297
}
394298
const topmost = Frame.topmost();
395299
if (topmost) {
396-
let view;
397-
if (topmost.currentPage && topmost.currentPage.modal) {
398-
view = topmost;
399-
} else {
400-
view = topmost.currentPage.modal;
401-
}
402-
const elements = getElementsByClassName(view, className);
403-
404-
return elements;
300+
const view = topmost.currentPage?.modal ?? topmost.currentPage ?? topmost;
301+
return domGetElementsByClassName(view, className);
405302
}
406303
return [];
407304
}

0 commit comments

Comments
 (0)