( elementId: string | null, selector: string, files: Record<string, string>, )
| 105 | * Find which source file contains an element by its ID. |
| 106 | */ |
| 107 | export function resolveSourceFile( |
| 108 | elementId: string | null, |
| 109 | selector: string, |
| 110 | files: Record<string, string>, |
| 111 | ): string | null { |
| 112 | if (!elementId && !selector) return null; |
| 113 | |
| 114 | // Strategy 1: Search by id attribute |
| 115 | if (elementId) { |
| 116 | for (const [path, content] of Object.entries(files)) { |
| 117 | if (content.includes(`id="${elementId}"`) || content.includes(`id='${elementId}'`)) { |
| 118 | return path; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // Strategy 2: Search by data-composition-id from the selector |
| 124 | const compIdMatch = selector.match(/data-composition-id="([^"]+)"/); |
| 125 | if (compIdMatch) { |
| 126 | const compId = compIdMatch[1]; |
| 127 | for (const [path, content] of Object.entries(files)) { |
| 128 | if (content.includes(`data-composition-id="${compId}"`)) { |
| 129 | return path; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Strategy 3: Search by class from the selector |
| 135 | const classMatch = selector.match(/^\.([a-zA-Z0-9_-]+)/); |
| 136 | if (classMatch) { |
| 137 | const cls = classMatch[1]; |
| 138 | for (const [path, content] of Object.entries(files)) { |
| 139 | if ( |
| 140 | content.includes(`class="${cls}"`) || |
| 141 | content.includes(`class="${cls} `) || |
| 142 | content.includes(` ${cls}"`) |
| 143 | ) { |
| 144 | return path; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Fallback: index.html |
| 150 | if ("index.html" in files) return "index.html"; |
| 151 | return null; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Apply a style property change to an element's inline style in the HTML source. |
no test coverage detected