Several fixes

Comments: 

1. Nokogiri XML parser is a bad choise for HTML5 because of issues like \<br\> instead of \<br/\>. There one interesting test with erb. In this test XML parser become insane and critically brokes markup.
2. Two other bugs is more ordinary ones.
3. Write any suggestions about my code in this tread. This week I have some time to improve code quality.

About extracting conditions to method (previous pull request) - i'll do it. Don't merge this pull request before.