From abe349be3445b77230c37fcf65f5438c45249a55 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Sat, 2 Oct 2021 21:01:09 +0200 Subject: [PATCH] small refactoring in HTMLParser --- src/html/htmlparser.cpp | 93 ++++++++++++----------------------------- 1 file changed, 26 insertions(+), 67 deletions(-) diff --git a/src/html/htmlparser.cpp b/src/html/htmlparser.cpp index 90a32f1..e5d3b66 100644 --- a/src/html/htmlparser.cpp +++ b/src/html/htmlparser.cpp @@ -1862,55 +1862,6 @@ void HTMLParser::CheckDifferentContentExceptions(Item & item) -void HTMLParser::AddForgottenTags() -{ -int i; - - if( stack_len < 3 ) - { - PopStack(); - return; - } - - // we have forgotten to close some tags - - // looking whether there is a matching opening tag - for(i=int(stack_len)-3 ; i>=0 ; --i) - if( IsNameEqual(pstack[i].name, pstack[stack_len-1].name) ) - break; - - if( i < 0 ) - { - // oops, there is no such a tag - // we don't print the closing and the missing opening tag - PopStack(); - return; - } - - for(int z=(int)stack_len-2 ; z>=i ; --z) - { - CheckWhiteCharsExceptions(pstack[z]); - - if( !skip_tags && pstack[z].new_line ) - { - if( current_white_char_mode() == WHITE_MODE_TREE ) - { - Put(10); - PutTabs(pstack[z].tree_index); - } - } - - PutClosingTag(pstack[z]); - pstack[z].Clear(); - } - - //last_new_line = pstack[stack_len-1].new_line; - - // invalidate tags - stack_len = i; -} - - void HTMLParser::CheckStackPrintRest() { while( stack_len-- > 0 ) @@ -1935,44 +1886,52 @@ void HTMLParser::CheckStackPrintRest() void HTMLParser::CheckClosingTags() { + int i; + if( stack_len == 0 ) return; // on the stack we have only opening tags // but only the last tag is a closing tag - if( stack_len == 1 ) + if( stack_len < 3 ) { - // there is only last closing tag - // we dont print it PopStack(); return; } - // there are more than one tag - if( (pstack[stack_len-1].is_commentary && pstack[stack_len-2].is_commentary) || IsNameEqual(pstack[stack_len-1].name, pstack[stack_len-2].name) ) - { - CheckWhiteCharsExceptions(pstack[stack_len-1]); + // looking whether there is a matching opening tag + for(i=int(stack_len)-2 ; i >= 0 ; --i) + if( (pstack[i].is_commentary && pstack[stack_len-1].is_commentary) || IsNameEqual(pstack[i].name, pstack[stack_len-1].name) ) + break; - // last closing tag is from the previous one - if( !skip_tags && pstack[stack_len-2].new_line ) + if( i < 0 ) + { + // oops, there is no such an opening tag on the stack + // we don't print the closing and the missing opening tag + PopStack(); + return; + } + + for(int z=(int)stack_len-2 ; z >= i ; --z) + { + CheckWhiteCharsExceptions(pstack[z]); + + if( !skip_tags && pstack[z].new_line ) { if( current_white_char_mode() == WHITE_MODE_TREE ) { Put(10); - PutTabs(pstack[stack_len-2].tree_index); + PutTabs(pstack[z].tree_index); } } - PutClosingTag(pstack[stack_len-1]); - //last_new_line = pstack[stack_len-1].new_line; - PopStack(); - PopStack(); - } - else - { - AddForgottenTags(); + PutClosingTag(pstack[z]); + pstack[z].Clear(); } + + // invalidate items on the stack + stack_len = i; }