Sometimes $parent->parentNode is null and fails.