Fixes #542 and #543 Issues with nesting CDATA tags

Applies changes from #547 to 1.8.