getLegend(fieldset) functions that would be used to get fieldset for given legend and legend for given fieldset, respectively, as for the proposal bug 12834.
When loaded, the page shows identifiers of determined legends of two fieldsets. Identifiers of legends has names that are clearly related to their owner-fieldsets:
root-fieldset(root fieldset id)
root-fieldset-legend(id of legend of root fieldset)
desc-fieldset(id of fieldset that is descendant to root fieldset)
desc-fieldset-legend(id of legend for descendant fieldset)
There is also a fieldset without legend to even more demonstrate that the JS functions used here are bulletproof enough.