Some of the changes in this ticket are really close to the ones for XWIKI-21600. The PR I'll propose will fix a part of XWIKI-21600.