-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuiElementClassifier.js
More file actions
697 lines (615 loc) · 22.5 KB
/
Copy pathuiElementClassifier.js
File metadata and controls
697 lines (615 loc) · 22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/**
* Koda - Intelligent Browser Automation Library
* This project uses Koda by Trent Pierce
* https://github.com/TrentPierce/Koda
* Licensed under the Koda Non-Commercial License
*
* Copyright (c) 2026 Trent Pierce. All rights reserved.
* See LICENSE file for full terms.
*/
const EventEmitter = require('events');
/**
* UI element types
* @enum {string}
*/
const ElementType = {
BUTTON: 'button',
INPUT: 'input',
TEXTAREA: 'textarea',
SELECT: 'select',
CHECKBOX: 'checkbox',
RADIO: 'radio',
LINK: 'link',
IMAGE: 'image',
VIDEO: 'video',
HEADING: 'heading',
TEXT: 'text',
ICON: 'icon',
MENU: 'menu',
MODAL: 'modal',
CARD: 'card',
TABLE: 'table',
LIST: 'list',
NAVIGATION: 'navigation',
FORM: 'form',
UNKNOWN: 'unknown'
};
/**
* UI framework types
* @enum {string}
*/
const FrameworkType = {
REACT: 'react',
VUE: 'vue',
ANGULAR: 'angular',
BOOTSTRAP: 'bootstrap',
MATERIAL: 'material',
TAILWIND: 'tailwind',
CUSTOM: 'custom',
NONE: 'none'
};
/**
* Element interaction types
* @enum {string}
*/
const InteractionType = {
CLICKABLE: 'clickable',
EDITABLE: 'editable',
SELECTABLE: 'selectable',
HOVERABLE: 'hoverable',
DRAGGABLE: 'draggable',
SCROLLABLE: 'scrollable',
NONE: 'none'
};
/**
* UI Element Classifier for visual element recognition
* @class
* @extends EventEmitter
*/
class UIElementClassifier extends EventEmitter {
/**
* Create a new UIElementClassifier instance
* @param {Object} options - Configuration options
* @param {number} [options.minConfidence=0.5] - Minimum confidence threshold
* @param {boolean} [options.detectFrameworks=true] - Enable framework detection
* @param {boolean} [options.detectAccessibility=true] - Enable accessibility detection
*/
constructor(options = {}) {
super();
this.minConfidence = options.minConfidence || 0.5;
this.detectFrameworks = options.detectFrameworks !== undefined ? options.detectFrameworks : true;
this.detectAccessibility = options.detectAccessibility !== undefined ? options.detectAccessibility : true;
// Classification patterns
this.patterns = this.initializePatterns();
// Statistics
this.stats = {
totalClassifications: 0,
byType: {},
byFramework: {},
averageConfidence: 0
};
console.log('[UIElementClassifier] Initialized');
}
/**
* Initialize classification patterns
* @private
* @returns {Object} Pattern definitions
*/
initializePatterns() {
return {
button: {
tags: ['button', 'input[type="button"]', 'input[type="submit"]', 'a'],
classPatterns: /btn|button|cta|action|submit|primary|secondary/i,
rolePatterns: /button/i,
textPatterns: /^(submit|cancel|save|delete|ok|yes|no|confirm|send|add|create|edit|update|remove|search|login|sign|register|buy|checkout|continue|next|back|close|dismiss)$/i,
ariaPatterns: /button/i
},
input: {
tags: ['input', 'textarea'],
classPatterns: /input|field|form-control|textbox/i,
typePatterns: /text|email|password|number|tel|url|search/i,
rolePatterns: /textbox/i,
ariaPatterns: /textbox/i
},
checkbox: {
tags: ['input[type="checkbox"]'],
classPatterns: /checkbox|check|toggle/i,
rolePatterns: /checkbox/i,
ariaPatterns: /checkbox/i
},
radio: {
tags: ['input[type="radio"]'],
classPatterns: /radio|option/i,
rolePatterns: /radio/i,
ariaPatterns: /radio/i
},
select: {
tags: ['select'],
classPatterns: /select|dropdown|picker/i,
rolePatterns: /listbox|combobox/i,
ariaPatterns: /listbox|combobox/i
},
link: {
tags: ['a'],
classPatterns: /link|anchor|nav-link/i,
rolePatterns: /link/i,
ariaPatterns: /link/i
},
heading: {
tags: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
classPatterns: /heading|title|header/i,
rolePatterns: /heading/i,
ariaPatterns: /heading/i
},
icon: {
tags: ['i', 'svg', 'span'],
classPatterns: /icon|fa-|glyphicon|material-icons|icon-|svg-inline/i,
ariaPatterns: /icon|img/i
},
menu: {
tags: ['nav', 'ul', 'ol'],
classPatterns: /menu|nav|navigation|navbar|sidebar/i,
rolePatterns: /menu|navigation/i,
ariaPatterns: /menu|navigation/i
},
modal: {
tags: ['div', 'dialog'],
classPatterns: /modal|dialog|popup|overlay|lightbox/i,
rolePatterns: /dialog|alertdialog/i,
ariaPatterns: /dialog/i
},
card: {
tags: ['div', 'article'],
classPatterns: /card|panel|tile|box/i,
rolePatterns: /article|region/i,
ariaPatterns: /article/i
}
};
}
/**
* Classify a UI element
* @param {Object} element - Element to classify
* @param {string} element.tag - HTML tag name
* @param {string} [element.className] - CSS classes
* @param {string} [element.id] - Element ID
* @param {string} [element.type] - Input type
* @param {string} [element.role] - ARIA role
* @param {string} [element.text] - Element text content
* @param {Object} [element.bounds] - Element bounds {x, y, width, height}
* @param {Object} [element.styles] - Computed styles
* @param {Object} [element.attributes] - HTML attributes
* @returns {Object} Classification result
*/
classifyElement(element) {
console.log('[UIElementClassifier] Classifying element:', element.tag);
const startTime = Date.now();
try {
// Determine element type
const typeClassification = this.determineElementType(element);
// Determine interaction type
const interactionType = this.determineInteractionType(element, typeClassification.type);
// Detect framework patterns
const framework = this.detectFrameworks ? this.detectFramework(element) : null;
// Check accessibility
const accessibility = this.detectAccessibility ? this.checkAccessibility(element) : null;
// Calculate visual characteristics
const visual = this.analyzeVisualCharacteristics(element);
// Build classification result
const classification = {
type: typeClassification.type,
confidence: typeClassification.confidence,
interaction: interactionType,
framework: framework,
accessibility: accessibility,
visual: visual,
metadata: {
tag: element.tag,
hasText: !!element.text,
hasId: !!element.id,
hasClasses: !!element.className,
classifiedAt: Date.now()
}
};
// Update statistics
const duration = Date.now() - startTime;
this.updateStats(classification, duration);
console.log(`[UIElementClassifier] Classified as ${classification.type} with confidence ${classification.confidence.toFixed(3)}`);
this.emit('element:classified', { element, classification, duration });
return classification;
} catch (error) {
console.error('[UIElementClassifier] Classification failed:', error.message);
this.emit('classification:error', { element, error });
return {
type: ElementType.UNKNOWN,
confidence: 0,
error: error.message
};
}
}
/**
* Determine element type
* @private
* @param {Object} element - Element data
* @returns {Object} Type classification with confidence
*/
determineElementType(element) {
const scores = {};
// Check each pattern type
for (const [type, pattern] of Object.entries(this.patterns)) {
let score = 0;
let maxScore = 0;
// Tag matching (highest weight)
maxScore += 40;
if (pattern.tags) {
for (const tagPattern of pattern.tags) {
const cleanTag = tagPattern.replace(/\[.*\]/, '');
if (element.tag === cleanTag) {
score += 40;
// Check type attribute if specified
if (tagPattern.includes('[type=')) {
const typeMatch = tagPattern.match(/type="([^"]+)"/);
if (typeMatch && element.type === typeMatch[1]) {
score += 10;
maxScore += 10;
}
}
break;
}
}
}
// Class matching (medium weight)
maxScore += 25;
if (pattern.classPatterns && element.className) {
if (pattern.classPatterns.test(element.className)) {
score += 25;
}
}
// Role matching (medium weight)
maxScore += 20;
if (pattern.rolePatterns && element.role) {
if (pattern.rolePatterns.test(element.role)) {
score += 20;
}
}
// Text matching (lower weight)
maxScore += 10;
if (pattern.textPatterns && element.text) {
if (pattern.textPatterns.test(element.text)) {
score += 10;
}
}
// ARIA matching (lower weight)
maxScore += 5;
if (pattern.ariaPatterns && element.attributes) {
const ariaLabel = element.attributes['aria-label'] || '';
if (pattern.ariaPatterns.test(ariaLabel)) {
score += 5;
}
}
// Calculate confidence for this type
if (maxScore > 0) {
scores[type] = score / maxScore;
}
}
// Find best match
let bestType = ElementType.UNKNOWN;
let bestConfidence = 0;
for (const [type, confidence] of Object.entries(scores)) {
if (confidence > bestConfidence) {
bestConfidence = confidence;
bestType = type;
}
}
// Fallback to tag-based classification
if (bestConfidence < 0.3) {
const tagFallback = this.getTagFallbackType(element.tag);
if (tagFallback) {
return { type: tagFallback, confidence: 0.5 };
}
}
return {
type: bestType,
confidence: bestConfidence
};
}
/**
* Get fallback type based on tag
* @private
* @param {string} tag - HTML tag
* @returns {string|null} Element type
*/
getTagFallbackType(tag) {
const tagMap = {
'button': ElementType.BUTTON,
'input': ElementType.INPUT,
'textarea': ElementType.TEXTAREA,
'select': ElementType.SELECT,
'a': ElementType.LINK,
'img': ElementType.IMAGE,
'video': ElementType.VIDEO,
'h1': ElementType.HEADING,
'h2': ElementType.HEADING,
'h3': ElementType.HEADING,
'h4': ElementType.HEADING,
'h5': ElementType.HEADING,
'h6': ElementType.HEADING,
'nav': ElementType.NAVIGATION,
'ul': ElementType.LIST,
'ol': ElementType.LIST,
'table': ElementType.TABLE,
'form': ElementType.FORM,
'p': ElementType.TEXT,
'span': ElementType.TEXT,
'div': ElementType.UNKNOWN
};
return tagMap[tag.toLowerCase()] || null;
}
/**
* Determine interaction type
* @private
* @param {Object} element - Element data
* @param {string} elementType - Classified element type
* @returns {string} Interaction type
*/
determineInteractionType(element, elementType) {
// Based on element type
const typeInteractions = {
[ElementType.BUTTON]: InteractionType.CLICKABLE,
[ElementType.LINK]: InteractionType.CLICKABLE,
[ElementType.INPUT]: InteractionType.EDITABLE,
[ElementType.TEXTAREA]: InteractionType.EDITABLE,
[ElementType.SELECT]: InteractionType.SELECTABLE,
[ElementType.CHECKBOX]: InteractionType.CLICKABLE,
[ElementType.RADIO]: InteractionType.CLICKABLE,
[ElementType.MENU]: InteractionType.CLICKABLE
};
if (typeInteractions[elementType]) {
return typeInteractions[elementType];
}
// Check for click handlers
if (element.attributes && (
element.attributes.onclick ||
element.attributes['data-action'] ||
element.attributes['data-click']
)) {
return InteractionType.CLICKABLE;
}
// Check for draggable
if (element.attributes && element.attributes.draggable === 'true') {
return InteractionType.DRAGGABLE;
}
// Check styles for cursor pointer
if (element.styles && element.styles.cursor === 'pointer') {
return InteractionType.CLICKABLE;
}
return InteractionType.NONE;
}
/**
* Detect framework patterns
* @private
* @param {Object} element - Element data
* @returns {Object} Framework detection result
*/
detectFramework(element) {
const frameworks = {
react: /^(react|_react|__react)/i,
vue: /^(v-|vue-|data-v-)/i,
angular: /^(ng-|data-ng-|\[ng|_ng)/i,
bootstrap: /^(bs-|bootstrap)/i,
material: /^(mat-|md-|mdc-)/i,
tailwind: /^(tw-|tailwind)/i
};
const detected = [];
const className = element.className || '';
const attributes = element.attributes || {};
// Check class names
for (const [framework, pattern] of Object.entries(frameworks)) {
if (pattern.test(className)) {
detected.push(framework);
}
}
// Check attributes
for (const attr of Object.keys(attributes)) {
for (const [framework, pattern] of Object.entries(frameworks)) {
if (pattern.test(attr)) {
if (!detected.includes(framework)) {
detected.push(framework);
}
}
}
}
if (detected.length === 0) {
return { type: FrameworkType.NONE, detected: [] };
}
return {
type: detected[0],
detected: detected,
confidence: detected.length > 1 ? 0.8 : 0.9
};
}
/**
* Check accessibility compliance
* @private
* @param {Object} element - Element data
* @returns {Object} Accessibility analysis
*/
checkAccessibility(element) {
const issues = [];
const attributes = element.attributes || {};
// Check for ARIA attributes
const hasAriaLabel = !!attributes['aria-label'];
const hasAriaDescribedBy = !!attributes['aria-describedby'];
const hasRole = !!element.role;
// Check for alt text on images
if (element.tag === 'img' && !attributes.alt) {
issues.push('Missing alt text for image');
}
// Check for label on inputs
if (['input', 'textarea', 'select'].includes(element.tag)) {
const hasLabel = !!attributes['aria-labelledby'] || hasAriaLabel || !!attributes.id;
if (!hasLabel) {
issues.push('Input missing associated label');
}
}
// Check for interactive element accessibility
if ([ElementType.BUTTON, ElementType.LINK].includes(element.type)) {
if (!element.text && !hasAriaLabel) {
issues.push('Interactive element missing accessible name');
}
}
// Check for keyboard accessibility
const isFocusable = element.attributes && (
element.attributes.tabindex !== undefined ||
['a', 'button', 'input', 'select', 'textarea'].includes(element.tag)
);
return {
hasAriaLabel: hasAriaLabel,
hasAriaDescribedBy: hasAriaDescribedBy,
hasRole: hasRole,
isFocusable: isFocusable,
issues: issues,
score: Math.max(0, 1 - (issues.length * 0.25)),
compliant: issues.length === 0
};
}
/**
* Analyze visual characteristics
* @private
* @param {Object} element - Element data
* @returns {Object} Visual analysis
*/
analyzeVisualCharacteristics(element) {
const bounds = element.bounds || {};
const styles = element.styles || {};
// Size analysis
const width = bounds.width || 0;
const height = bounds.height || 0;
const area = width * height;
let sizeCategory = 'medium';
if (area < 1000) sizeCategory = 'small';
else if (area > 10000) sizeCategory = 'large';
// Position analysis
const position = {
x: bounds.x || 0,
y: bounds.y || 0,
isTopArea: (bounds.y || 0) < 200,
isBottomArea: (bounds.y || 0) > 800,
isLeftArea: (bounds.x || 0) < 200,
isRightArea: (bounds.x || 0) > 1000
};
// Visual prominence
let prominence = 0.5;
// Larger elements are more prominent
if (area > 10000) prominence += 0.2;
// Top position is more prominent
if (position.isTopArea) prominence += 0.15;
// Bright colors are more prominent (simplified)
if (styles.backgroundColor && !styles.backgroundColor.includes('rgb(255, 255, 255)')) {
prominence += 0.1;
}
prominence = Math.min(1.0, prominence);
return {
bounds: bounds,
size: {
width: width,
height: height,
area: area,
category: sizeCategory
},
position: position,
prominence: prominence,
aspectRatio: height > 0 ? (width / height).toFixed(2) : 0
};
}
/**
* Classify multiple elements in batch
* @param {Array<Object>} elements - Elements to classify
* @returns {Array<Object>} Classification results
*/
classifyBatch(elements) {
console.log(`[UIElementClassifier] Classifying batch of ${elements.length} elements`);
const results = elements.map(element => {
try {
return this.classifyElement(element);
} catch (error) {
console.error('[UIElementClassifier] Batch classification error:', error.message);
return {
type: ElementType.UNKNOWN,
confidence: 0,
error: error.message
};
}
});
return results;
}
/**
* Update statistics
* @private
* @param {Object} classification - Classification result
* @param {number} duration - Processing duration
*/
updateStats(classification, duration) {
this.stats.totalClassifications++;
// Count by type
const type = classification.type;
this.stats.byType[type] = (this.stats.byType[type] || 0) + 1;
// Count by framework
if (classification.framework && classification.framework.type !== FrameworkType.NONE) {
const framework = classification.framework.type;
this.stats.byFramework[framework] = (this.stats.byFramework[framework] || 0) + 1;
}
// Update average confidence
const totalConf = this.stats.averageConfidence * (this.stats.totalClassifications - 1);
this.stats.averageConfidence = (totalConf + classification.confidence) / this.stats.totalClassifications;
}
/**
* Get classifier statistics
* @returns {Object} Statistics
*/
getStats() {
return {
...this.stats,
averageConfidence: this.stats.averageConfidence.toFixed(3),
mostCommonType: this.getMostCommonType(),
mostCommonFramework: this.getMostCommonFramework()
};
}
/**
* Get most common element type
* @private
* @returns {string} Most common type
*/
getMostCommonType() {
let maxCount = 0;
let mostCommon = ElementType.UNKNOWN;
for (const [type, count] of Object.entries(this.stats.byType)) {
if (count > maxCount) {
maxCount = count;
mostCommon = type;
}
}
return mostCommon;
}
/**
* Get most common framework
* @private
* @returns {string} Most common framework
*/
getMostCommonFramework() {
let maxCount = 0;
let mostCommon = FrameworkType.NONE;
for (const [framework, count] of Object.entries(this.stats.byFramework)) {
if (count > maxCount) {
maxCount = count;
mostCommon = framework;
}
}
return mostCommon;
}
}
module.exports = {
UIElementClassifier,
ElementType,
FrameworkType,
InteractionType
};