Avoid infinite loop when gathering "g" element styles

Same way of how fdo#74743 is fixed 3b7472b284131c09d91b69f26d5d26d54648f939

Change-Id: If6cc8eb6ff89b0081f27ff75a9b60a3e81cff1bd
Reviewed-on: https://gerrit.libreoffice.org/10326
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Tested-by: Tomaž Vajngerl <quikee@gmail.com>