Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/browserElements/common/browserElements.ts
5232 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { CancellationToken } from '../../../base/common/cancellation.js';
7
import { createDecorator } from '../../instantiation/common/instantiation.js';
8
import { IRectangle } from '../../window/common/window.js';
9
10
export const INativeBrowserElementsService = createDecorator<INativeBrowserElementsService>('nativeBrowserElementsService');
11
12
export interface IElementData {
13
readonly outerHTML: string;
14
readonly computedStyle: string;
15
readonly bounds: IRectangle;
16
}
17
18
/**
19
* Locator for identifying a browser target/webview.
20
* Uses either the parent webview or browser view id to uniquely identify the target.
21
*/
22
export interface IBrowserTargetLocator {
23
/**
24
* Identifier of the parent webview hosting the target.
25
*
26
* Exactly one of {@link webviewId} or {@link browserViewId} should be provided.
27
* Use this when the target is rendered inside a webview.
28
*/
29
readonly webviewId?: string;
30
/**
31
* Identifier of the browser view hosting the target.
32
*
33
* Exactly one of {@link webviewId} or {@link browserViewId} should be provided.
34
* Use this when the target is rendered inside a browser view rather than a webview.
35
*/
36
readonly browserViewId?: string;
37
}
38
39
export interface INativeBrowserElementsService {
40
41
readonly _serviceBrand: undefined;
42
43
// Properties
44
readonly windowId: number;
45
46
getElementData(rect: IRectangle, token: CancellationToken, locator: IBrowserTargetLocator, cancellationId?: number): Promise<IElementData | undefined>;
47
48
startDebugSession(token: CancellationToken, locator: IBrowserTargetLocator, cancelAndDetachId?: number): Promise<void>;
49
}
50
51
/**
52
* Extract a display name from outer HTML (e.g., "div#myId.myClass1.myClass2")
53
*/
54
export function getDisplayNameFromOuterHTML(outerHTML: string): string {
55
const firstElementMatch = outerHTML.match(/^<([^ >]+)([^>]*?)>/);
56
if (!firstElementMatch) {
57
throw new Error('No outer element found');
58
}
59
60
const tagName = firstElementMatch[1];
61
const idMatch = firstElementMatch[2].match(/\s+id\s*=\s*["']([^"']+)["']/i);
62
const id = idMatch ? `#${idMatch[1]}` : '';
63
const classMatch = firstElementMatch[2].match(/\s+class\s*=\s*["']([^"']+)["']/i);
64
const className = classMatch ? `.${classMatch[1].replace(/\s+/g, '.')}` : '';
65
return `${tagName}${id}${className}`;
66
}
67
68