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