* Find question element within a container
(container, inputElement)
| 258 | * Find question element within a container |
| 259 | */ |
| 260 | function findQuestionElementInContainer(container, inputElement) { |
| 261 | // Look for semantic elements that typically contain questions |
| 262 | const selectors = [ |
| 263 | 'label:not(:has(input))', // Labels that don't wrap inputs |
| 264 | '.question', '.label', '.prompt', |
| 265 | '[class*="label"]:not(input)', |
| 266 | 'h3', 'h4', 'h5', 'p', 'span', 'div' |
| 267 | ]; |
| 268 | |
| 269 | for (const selector of selectors) { |
| 270 | try { |
| 271 | const el = container.querySelector(selector); |
| 272 | if (el && !el.contains(inputElement)) { |
| 273 | const text = el.textContent?.trim(); |
| 274 | if (text && text.length > 10 && text.length < 500) { |
| 275 | return el; |
| 276 | } |
| 277 | } |
| 278 | } catch (e) { /* Skip invalid selectors */ } |
| 279 | } |
| 280 | |
| 281 | return null; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * DOM traversal fallback - look for text in ancestors/siblings |