<commit_before>/*
 Copyright (C) 2016-2017 <NAME>
 
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.
 
 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.
 
 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 Author: <EMAIL> (<NAME>)
*/

#include "modest/finder/pseudo_class.h"
#include "modest/finder/resource.h"

bool modest_finder_selector_sub_type_pseudo_class_function_undef(modest_finder_t* finder, myhtml_tree_node_t* base_node, mycss_selectors_entry_t* selector, mycss_selectors_specificity_t* spec)
{
    return false;
}

bool modest_finder_selector_sub_type_pseudo_class_function_current(modest_finder_t* finder, myhtml_tree_node_t* base_node, mycss_selectors_entry_t* selector, mycss_selectors_specificity_t* spec)
{
    mycss_selectors_list_t *list = selector->value;
    bool i_found;
    
    for(size_t i = 0; i < list->entries_list_length; i++)
    {
        i_found = false;
        modest_finder_node_combinator_undef(finder, base_node, NULL, list->entries_list[i].entry, spec, modest_finder_callback_found_with_bool, &i_found);
        
        if(i_found == true)
            return true;
    }
    
    return false;
}

/* after create render */
bool modest_finder_selector_sub_type_pseudo_class_function_dir(modest_finder_t* finder, myhtml_tree_node_t* base_node, mycss_selectors_entry_t* selector, mycss_selectors_specificity_t* spec)
{
    return false;
}

bool modest_finder_selector_sub_type_pseudo_class_function_drop(modest_finder_t* finder, myhtml_tree_node_t* base_node, mycss_selectors_entry_t* selector, mycss_selectors_specificity_t* spec)
{
    return false;
}

bool modest_finder_selector_sub_type_pseudo_class_function_has(modest_finder_t* finder, myhtml_tree_node_t* base_node, mycss_selectors_entry_t* selector, mycss_selectors_specificity_t* spec)
{
    mycss_selectors_list_t *list = selector->value;
    bool i_found;
    
    for(size_t i = 0; i < list->entries_list_length; i++)
    {
        i_found = false;
        
        mycss_selectors_entry_t *sel_entry = list->entries_list[i].entry;
        
        if(sel_entry->combinator == MyCSS_SELECTORS_COMBINATOR_UNDEF)
            modest_finder_node_combinator_descendant(finder, base_node, NULL, sel_entry, spec, modest_finder_callback_found_with_bool, &i_found);
        else
            modest_finder_static_selector_combinator_map[sel_entry->combinator](finder, base_node, NULL, sel_entry, spec, modest_finder_callback_found_with_bool, &i_found);
        
        if(i_found == true)
            return true;
    }
    
    return false;
}

bool modest_finder_selector_sub_type_pseudo_class_function_contains(modest_finder_t* finder, myhtml_tree_node_t* base_node, mycss_selectors_entry_t* selector, mycss_selectors_specificity_t* spec)
{
    if(base_node == NULL)
        return false;
    
    myhtml_tree_node_t *text_node = myhtml_node_child(base_node);
    if(text_node == NULL)
        return false;

    const char* text = myhtml_node_text(text_node, NULL);
    if(text == NULL)
        return false;

    mycss_selectors_list_t *list = (mycss_selectors_list_t*)selector->value;
    for(size_t i = 0; i < list->entries_list_length; i++) {
        char *data = NULL;
        data = mycore_malloc(0);
        if(data == NULL) {
            return false;
        }
        mycss_selectors_entry_t *sel_entry = list->entries_list[i].entry;
        if(sel_entry->key->data){
            const char *str = sel_entry->key->data;
            int length = strlen(str) + 1;
            data = mycore_realloc(data, length);
            if(data == NULL) {
                return false;
            }
            snprintf(&data[0], length, "%s", str);
        }

        mycss_selectors_entry_t *next = sel_entry->next;
        while(next) {
            if(next->key->data) {
                int prev = strlen(data);
                const char* whitespace = (prev > 0) ? " " : "";
                const char *str = next->key->data;
                int length = strlen(whitespace) + strlen(str) + 1;
                data = mycore_realloc(data, prev + length);
                if(data == NULL) {
                    return false;
                }
                snprintf(&data[prev], length, "%s%s", whitespace, str);
            }
            next = next->next;
        }

        if(strstr(text, data) != NULL) {
            mycore_free(data);
            return true;
        }
        else {
            mycore_free(data);
        }
    }
    
    return false;
}

bool modest_finder_selector_sub_type_pseudo_class_function_lang(modest_finder_t* finder, myhtml_tree_node_t* base_node, mycss_selectors_entry_t* selector, mycss_selectors_specificity_t* spec)
{
    return false;
}

bool modest_finder_selector_sub_type_pseudo_class_function_matches(modest_finder_t* finder, myhtml_tree_node_t* base_node, mycss_selectors_entry_t* selector, mycss_selectors_specificity_t* spec)
{<commit_msg>Removed memory leak in case mycore_realloc failes<commit_after>/*
 Copyright (C) 2016-2017 <NAME>
 
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.
 
 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.
 
 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 Author: <EMAIL> (<NAME>)
*/

#include "modest/finder/pseudo_class.h"
#include "modest/finder/resource.h"

bool modest_finder_selector_sub_type_pseudo_class_function_undef(modest_finder_t* finder, myhtml_tree_node_t* base_node, mycss_selectors_entry_t* selector, mycss_selectors_specificity_t* spec)
{
    return false;
}

bool modest_finder_selector_sub_type_pseudo_class_function_current(modest_finder_t* finder, myhtml_tree_node_t* base_node, mycss_selectors_entry_t* selector, mycss_selectors_specificity_t* spec)
{
    mycss_selectors_list_t *list = selector->value;
    bool i_found;
    
    for(size_t i = 0; i < list->entries_list_length; i++)
    {
        i_found = false;
        modest_finder_node_combinator_undef(finder, base_node, NULL, list->entries_list[i].entry, spec, modest_finder_callback_found_with_bool, &i_found);
        
        if(i_found == true)
            return true;
    }
    
    return false;
}

/* after create render */
bool modest_finder_selector_sub_type_pseudo_class_function_dir(modest_finder_t* finder, myhtml_tree_node_t* base_node, mycss_selectors_entry_t* selector, mycss_selectors_specificity_t* spec)
{
    return false;
}

bool modest_finder_selector_sub_type_pseudo_class_function_drop(modest_finder_t* finder, myhtml_tree_node_t* base_node, mycss_selectors_entry_t* selector, mycss_selectors_specificity_t* spec)
{
    return false;
}

bool modest_finder_selector_sub_type_pseudo_class_function_has(modest_finder_t* finder, myhtml_tree_node_t* base_node, mycss_selectors_entry_t* selector, mycss_selectors_specificity_t* spec)
{
    mycss_selectors_list_t *list = selector->value;
    bool i_found;
    
    for(size_t i = 0; i < list->entries_list_length; i++)
    {
        i_found = false;
        
        mycss_selectors_entry_t *sel_entry = list->entries_list[i].entry;
        
        if(sel_entry->combinator == MyCSS_SELECTORS_COMBINATOR_UNDEF)
            modest_finder_node_combinator_descendant(finder, base_node, NULL, sel_entry, spec, modest_finder_callback_found_with_bool, &i_found);
        else
            modest_finder_static_selector_combinator_map[sel_entry->combinator](finder, base_node, NULL, sel_entry, spec, modest_finder_callback_found_with_bool, &i_found);
        
        if(i_found == true)
            return true;
    }
    
    return false;
}

bool modest_finder_selector_sub_type_pseudo_class_function_contains(modest_finder_t* finder, myhtml_tree_node_t* base_node, mycss_selectors_entry_t* selector, mycss_selectors_specificity_t* spec)
{
    if(base_node == NULL)
        return false;
    
    myhtml_tree_node_t *text_node = myhtml_node_child(base_node);
    if(text_node == NULL)
        return false;

    const char* text = myhtml_node_text(text_node, NULL);
    if(text == NULL)
        return false;

    mycss_selectors_list_t *list = (mycss_selectors_list_t*)selector->value;
    for(size_t i = 0; i < list->entries_list_length; i++) {
        char *data = NULL;
        data = mycore_malloc(0);
        if(data == NULL) {
            return false;
        }
        
        mycss_selectors_entry_t *sel_entry = list->entries_list[i].entry;
        if(sel_entry->key->data){
            const char *str = sel_entry->key->data;
            int length = strlen(str) + 1;
            char *new_data = mycore_realloc(data, length);
            if(new_data == NULL) {
                mycore_free(data);
                return false;
            }
            snprintf(&new_data[0], length, "%s", str);
            data = new_data;
        }

        mycss_selectors_entry_t *next = sel_entry->next;
        while(next) {
            if(next->key->data) {
                int prev = strlen(data);
                const char *whitespace = (prev > 0) ? " " : "";
                const char *str = next->key->data;
                int length = strlen(whitespace) + strlen(str) + 1;
                char *new_data = mycore_realloc(data, prev + length);
                if(new_data == NULL) {
                    mycore_free(data);
                    return false;
                }
                snprintf(&new_data[prev], length, "%s%s", whitespace, str);
                data = new_data;
            }
            next = next->next;
        }

        if(strstr(text, data) != NULL) {
            mycore_free(data);
            return true;
        }
        mycore_free(data);
    }
    
    return false;
}

bool modest_finder_selector_sub_type_pseudo_class_function_lang(modest_finder_t* finder, myhtml_tree_node_t* base_node, mycss_selectors_entry_t* selector, mycss_selectors_specificity_t* spec)
{
    return false;
}

bool modest_finder_selector_sub_type_pseudo_class_function_matches(modest_finder_t* finder, myhtml_tree_node_t* base_node, mycss_selectors_entry_t* selector, mycss_selectors_specificity_t* spec)
{
{'fraction_non_alphanumeric': 0.08429602888086643, 'fraction_numerical': 0.005234657039711191, 'mean_word_length': 3.2213333333333334, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '652589', 'n_tokens_mistral': 3622, 'n_tokens_neox': 3504, 'n_words': 781}