* 检测字段所属分组
(element)
| 633 | * 检测字段所属分组 |
| 634 | */ |
| 635 | function detectFieldGroup(element) { |
| 636 | // 1. 检查 fieldset |
| 637 | const fieldset = element.closest('fieldset'); |
| 638 | if (fieldset) { |
| 639 | const legend = fieldset.querySelector('legend'); |
| 640 | if (legend) { |
| 641 | return legend.innerText.trim(); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | // 2. 检查带有标题的父容器 |
| 646 | let current = element.parentElement; |
| 647 | let depth = 0; |
| 648 | while (current && depth < 5) { |
| 649 | // 检查是否有分组标题 |
| 650 | const heading = current.querySelector(':scope > h1, :scope > h2, :scope > h3, :scope > h4, :scope > h5, :scope > h6'); |
| 651 | if (heading) { |
| 652 | return heading.innerText.trim(); |
| 653 | } |
| 654 | |
| 655 | // 检查常见分组类名 |
| 656 | const className = (current.className || '').toLowerCase(); |
| 657 | if (className.includes('group') || className.includes('section') || className.includes('block') || className.includes('panel')) { |
| 658 | // 尝试从第一个标题或 label 获取分组名 |
| 659 | const firstHeading = current.querySelector('h1, h2, h3, h4, h5, h6, .title, .heading'); |
| 660 | if (firstHeading) { |
| 661 | return firstHeading.innerText.trim(); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | current = current.parentElement; |
| 666 | depth++; |
| 667 | } |
| 668 | |
| 669 | return ''; |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * 检测相邻字段关系 |