This is my fix for 1597. It appears to correctly handle the case where a topicref or any ancestor is resource-only. I tested with my client's failing real document.