(HtmlParser): rename ItemParsedListener to Listener

while here:
- add a new callback method: bool should_remove(Item &)
This commit is contained in:
Tomasz Sowa 2024-04-17 10:39:06 +02:00
parent f02dd1093a
commit f35e2122ed
Signed by: tomasz.sowa
GPG Key ID: 662CC1438638588B
2 changed files with 19 additions and 26 deletions

View File

@ -104,9 +104,9 @@ void HTMLParser::parse_html(const wchar_t * in, Space & space, bool compact_mode
} }
void HTMLParser::set_item_parsed_listener(ItemParsedListener * listener) void HTMLParser::set_item_parsed_listener(Listener * listener)
{ {
item_parsed_listener = listener; this->listener = listener;
} }
@ -393,7 +393,7 @@ void HTMLParser::SetSomeDefaults()
skip_commentaries = false; skip_commentaries = false;
skip_entities = false; skip_entities = false;
analyze_entities = false; analyze_entities = false;
item_parsed_listener = nullptr; listener = nullptr;
} }
@ -2121,11 +2121,7 @@ void HTMLParser::CheckClosingTags()
for(int z=(int)stack_len-2 ; z >= i ; --z) for(int z=(int)stack_len-2 ; z >= i ; --z)
{ {
if( RemoveIfNeeded(z) ) CallListener(z); // space from the item can be set as null here (when a should_remove() callback returned true)
{
RemoveLastSpace(z);
}
CheckWhiteCharsExceptions(pstack[z]); CheckWhiteCharsExceptions(pstack[z]);
if( !skip_tags && IsTagSafe(LastItem().name) && !IsNameEqual(no_filter_tag, LastItem().name) ) if( !skip_tags && IsTagSafe(LastItem().name) && !IsNameEqual(no_filter_tag, LastItem().name) )
@ -2343,23 +2339,20 @@ void HTMLParser::AddTextSpaceToSpaceTree(const Space & space)
} }
void HTMLParser::CallListener(size_t index)
bool HTMLParser::RemoveIfNeeded(size_t index)
{ {
if( item_parsed_listener ) if( listener )
{ {
if( !item_parsed_listener->item_parsed(pstack[index]) ) listener->item_parsed(pstack[index]);
if( listener->should_remove(pstack[index]) )
{ {
return true; RemoveLastSpace(index);
} }
} }
return false;
} }
void HTMLParser::ReadLoop() void HTMLParser::ReadLoop()
{ {
while( status == ok && ReadItem() ) while( status == ok && ReadItem() )
@ -2392,8 +2385,7 @@ void HTMLParser::ReadLoop()
{ {
if( stack_len > 0 ) if( stack_len > 0 )
{ {
if( RemoveIfNeeded(stack_len - 1) ) CallListener(stack_len - 1);
RemoveLastSpace(stack_len - 1);
} }
PopStack(); PopStack();

View File

@ -160,14 +160,15 @@ public:
}; };
class ItemParsedListener class Listener
{ {
public: public:
ItemParsedListener() {} Listener() {}
virtual bool item_parsed(const Item & item) { return true; } virtual void item_parsed(const Item & item) { }
virtual ~ItemParsedListener() {} virtual bool should_remove(const Item & item) { return false; }
virtual ~Listener() {}
}; };
@ -182,7 +183,7 @@ public:
HTMLParser & operator=(const HTMLParser & f); HTMLParser & operator=(const HTMLParser & f);
virtual ~HTMLParser(); virtual ~HTMLParser();
void set_item_parsed_listener(ItemParsedListener * listener); void set_item_parsed_listener(Listener * listener);
void parse_html(const wchar_t * in, Space & space, bool compact_mode = false); void parse_html(const wchar_t * in, Space & space, bool compact_mode = false);
@ -304,7 +305,7 @@ protected:
// html <nofilter> tag name // html <nofilter> tag name
std::wstring no_filter_tag; std::wstring no_filter_tag;
ItemParsedListener * item_parsed_listener; Listener * listener;
/* /*
true if the lastc was escaped (with a backslash) true if the lastc was escaped (with a backslash)
@ -445,7 +446,7 @@ protected:
void RemoveLastSpace(size_t index); void RemoveLastSpace(size_t index);
void AddTextSpaceToSpaceTree(const Space & space); void AddTextSpaceToSpaceTree(const Space & space);
bool RemoveIfNeeded(size_t index); void CallListener(size_t index);
bool check_escape_sequentions(); bool check_escape_sequentions();
void read_xml_entity(); void read_xml_entity();