-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Expand file tree
/
Copy pathbrowserElements.ts
More file actions
66 lines (55 loc) · 2.54 KB
/
browserElements.ts
File metadata and controls
66 lines (55 loc) · 2.54 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { CancellationToken } from '../../../base/common/cancellation.js';
import { createDecorator } from '../../instantiation/common/instantiation.js';
import { IRectangle } from '../../window/common/window.js';
export const INativeBrowserElementsService = createDecorator<INativeBrowserElementsService>('nativeBrowserElementsService');
export interface IElementData {
readonly outerHTML: string;
readonly computedStyle: string;
readonly bounds: IRectangle;
}
/**
* Locator for identifying a browser target/webview.
* Uses either the parent webview or browser view id to uniquely identify the target.
*/
export interface IBrowserTargetLocator {
/**
* Identifier of the parent webview hosting the target.
*
* Exactly one of {@link webviewId} or {@link browserViewId} should be provided.
* Use this when the target is rendered inside a webview.
*/
readonly webviewId?: string;
/**
* Identifier of the browser view hosting the target.
*
* Exactly one of {@link webviewId} or {@link browserViewId} should be provided.
* Use this when the target is rendered inside a browser view rather than a webview.
*/
readonly browserViewId?: string;
}
export interface INativeBrowserElementsService {
readonly _serviceBrand: undefined;
// Properties
readonly windowId: number;
getElementData(rect: IRectangle, token: CancellationToken, locator: IBrowserTargetLocator, cancellationId?: number): Promise<IElementData | undefined>;
startDebugSession(token: CancellationToken, locator: IBrowserTargetLocator, cancelAndDetachId?: number): Promise<void>;
}
/**
* Extract a display name from outer HTML (e.g., "div#myId.myClass1.myClass2")
*/
export function getDisplayNameFromOuterHTML(outerHTML: string): string {
const firstElementMatch = outerHTML.match(/^<([^ >]+)([^>]*?)>/);
if (!firstElementMatch) {
throw new Error('No outer element found');
}
const tagName = firstElementMatch[1];
const idMatch = firstElementMatch[2].match(/\s+id\s*=\s*["']([^"']+)["']/i);
const id = idMatch ? `#${idMatch[1]}` : '';
const classMatch = firstElementMatch[2].match(/\s+class\s*=\s*["']([^"']+)["']/i);
const className = classMatch ? `.${classMatch[1].replace(/\s+/g, '.')}` : '';
return `${tagName}${id}${className}`;
}