Implemented modest_finder_selector_sub_type_pseudo_class_function_contains(); Test pseudo_class_contains succeeds

This commit is contained in:
frank 2018-03-24 21:31:05 +01:00
parent 58836153d9
commit bec0bc87c6
12 changed files with 177 additions and 58 deletions

View File

@ -23,11 +23,11 @@
#pragma once
static const char mycss_selectors_resource_matcher_names_map[][3] = {
"=", "~=", "|=", "^=", "$=", "*="
"=", "~=", "|=", "^=", "$=", "*=", "'", "\""
};
static const char mycss_selectors_resource_combinator_names_map[][3] = {
"", " ", ">", "+", "~", "||"
"", " ", ">", "+", "~", "||", "'", "\""
};
#endif /* MyHTML_MyCSS_SELECTORS_MYOSI_RESOURCE_H */

View File

@ -163,8 +163,8 @@ mystatus_t modest_finder_by_selectors_list(modest_finder_t* finder, myhtml_tree_
}
// FRANK
printf("\nmodest_finder_by_selectors_list()\n");
printf("\tselector_list->entries_list_length = %d\n", (int)selector_list->entries_list_length);
// printf("\nmodest_finder_by_selectors_list()\n");
// printf("\tselector_list->entries_list_length = %d\n", (int)selector_list->entries_list_length);
for(size_t i = 0; i < selector_list->entries_list_length; i++) {
mycss_selectors_specificity_t spec = selector_list->entries_list[i].specificity;
@ -187,9 +187,9 @@ myhtml_tree_node_t * modest_finder_node_combinator_begin(modest_finder_t* finder
myhtml_tree_node_t *node = base_node;
// FRANK
printf("\nmodest_finder_node_combinator_begin()\n");
printf("\t%s\n", (node)?"has node":"no node");
printf("\tselector->type = %d\n", (int)selector->type);
// printf("\nmodest_finder_node_combinator_begin()\n");
// printf("\t%s\n", (node)?"has node":"no node");
// printf("\tselector->type = %d\n", (int)selector->type);
while(node) {
if(node->tag_id != MyHTML_TAG__TEXT && node->tag_id != MyHTML_TAG__COMMENT &&
@ -249,8 +249,8 @@ myhtml_tree_node_t * modest_finder_node_combinator_undef(modest_finder_t* finder
return NULL;
// FRANK
printf("\nmodest_finder_node_combinator_undef()\n");
printf("\tselector->type = %d\n", (int)selector->type);
// printf("\nmodest_finder_node_combinator_undef()\n");
// printf("\tselector->type = %d\n", (int)selector->type);
mycss_selectors_specificity_t match_spec = *spec;

View File

@ -77,6 +77,67 @@ bool modest_finder_selector_sub_type_pseudo_class_function_has(modest_finder_t*
return false;
}
// concat str1 and str2
char *concat_string(const char *str1, const char *str2)
{
char *finalString = NULL;
size_t n = 0;
if(str1) n += strlen(str1);
if(str2) n += strlen(str2);
if((str1 || str2) && (finalString = malloc(n + 1)) != NULL)
{
*finalString = '\0';
if(str1) strcpy(finalString, str1);
if(str2) strcat(finalString, str2);
}
return finalString;
}
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)
{
// FRANK
// printf("\nmodest_finder_selector_sub_type_pseudo_class_function_contains()\n");
if(base_node){
const char* text = NULL;
myhtml_tree_node_t *text_node = myhtml_node_child(base_node);
if(text_node) {
text = myhtml_node_text(text_node, NULL);
if(text){
// printf("\ttext = %s\n", text);
char *data = NULL;
mycss_selectors_list_t *list = selector->value;
for(size_t i = 0; i < list->entries_list_length; i++) {
bool i_found = false;
mycss_selectors_entry_t *sel_entry = list->entries_list[i].entry;
if(sel_entry->key->data){
data = concat_string(data, sel_entry->key->data);
}
mycss_selectors_entry_t *next = sel_entry->next;
while(next && !i_found){
if(next->key->data){
data = concat_string(data, " ");
data = concat_string(data, next->key->data);
}
next = next->next;
}
}
// printf("\tdata = %s\n", data);
if(strstr(text, data) != NULL) {
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)
{
// FRANK
@ -136,6 +197,7 @@ bool modest_finder_selector_sub_type_pseudo_class_function_contains(modest_finde
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)
{

View File

@ -113,8 +113,8 @@ bool modest_finder_selector_type_attribute(modest_finder_t* finder, myhtml_tree_
bool modest_finder_selector_type_pseudo_class_function(modest_finder_t* finder, myhtml_tree_node_t* node, mycss_selectors_entry_t* selector, mycss_selectors_specificity_t* spec)
{
// FRANK
printf("\nmodest_finder_selector_type_pseudo_class_function()\n");
printf("\tselector->sub_type = %d\n", (int)selector->sub_type);
// printf("\nmodest_finder_selector_type_pseudo_class_function()\n");
// printf("\tselector->sub_type = %d\n", (int)selector->sub_type);
if(selector->sub_type < MyCSS_SELECTORS_SUB_TYPE_PSEUDO_CLASS_FUNCTION_LAST_ENTRY)
return modest_finder_static_selector_sub_type_pseudo_class_function_map[selector->sub_type](finder, node, selector, spec);

View File

@ -118,7 +118,7 @@ void mycss_selectors_function_begin_contains(mycss_entry_t* entry, mycss_selecto
// FRANK
// TODO: implement
printf("\nmycss_selectors_function_begin_contains()\n");
// printf("\nmycss_selectors_function_begin_contains()\n");
// printf("\tselector->sub_type = %d\n", (int)selector->sub_type);
mycss_selectors_t *selectors = entry->selectors;

View File

@ -234,32 +234,32 @@ bool mycss_selectors_function_parser_has(mycss_entry_t* entry, mycss_token_t* to
// printf("\nmycss_selectors_function_parser_contains_find_bad_selector()\n");
// }
bool mycss_selectors_function_parser_contains(mycss_entry_t* entry, mycss_token_t* token, bool last_response)
{
// TODO: implement
// FRANK
printf("\nmycss_selectors_function_parser_contains()\n");
// printf("\ttoken->type = %d\n\tentry->parser_ending_token = %d\n\tlast_response = %d\n", (int)token->type, entry->parser_ending_token, (int)last_response);
// bool mycss_selectors_function_parser_contains(mycss_entry_t* entry, mycss_token_t* token, bool last_response)
// {
// // TODO: implement
// // FRANK
// printf("\nmycss_selectors_function_parser_contains()\n");
// // printf("\ttoken->type = %d\n\tentry->parser_ending_token = %d\n\tlast_response = %d\n", (int)token->type, entry->parser_ending_token, (int)last_response);
mycss_selectors_t *selectors = entry->selectors;
mycss_selectors_entry_t *selector = selectors->entry_last;
printf("\t%s\n", (selector->value) ? "selector->value exists" : "selector->value does not exist");
// mycss_selectors_t *selectors = entry->selectors;
// mycss_selectors_entry_t *selector = selectors->entry_last;
// printf("\t%s\n", (selector->value) ? "selector->value exists" : "selector->value does not exist");
if(token){
// if(token){
// printf("%.*s", (int)size, buffer);
printf("\ttoken from %d -> %d, type = %d\n", (int)token->begin, (int)token->length, (int)token->type);
printf("\tdata = \"%.*s\"\n", (int)token->length, (char*)token->data);
// // printf("%.*s", (int)size, buffer);
// printf("\ttoken from %d -> %d, type = %d\n", (int)token->begin, (int)token->length, (int)token->type);
// printf("\tdata = \"%.*s\"\n", (int)token->length, (char*)token->data);
if(token->type == entry->parser_ending_token) {
printf("\ttype is entry->parser_ending_token\n");
mycss_entry_parser_list_pop(entry);
return false;
}
}
// if(token->type == entry->parser_ending_token) {
// printf("\ttype is entry->parser_ending_token\n");
// mycss_entry_parser_list_pop(entry);
// return false;
// }
// }
return true;
}
// return true;
// }
///////////////////////////////////////////////////////////
////// NTH OF SELECTORS

View File

@ -144,7 +144,7 @@ mycss_selectors_list_t * mycss_selectors_parse_by_function(mycss_selectors_t* se
// FRANK
// printf("\nmycss_selectors_parse_by_function()\n");
// printf("\tstatus = %d %s\n", (int)status, ((int)status == MyHTML_STATUS_OK)?"MyHTML_STATUS_OK":"not OK");
// printf("\t%s\n", ((int)status == MyHTML_STATUS_OK)?"MyHTML_STATUS_OK":"not OK");
if(out_status)
*out_status = status;

View File

@ -23,11 +23,11 @@
#pragma once
static const char mycss_selectors_resource_matcher_names_map[][3] = {
"=", "~=", "|=", "^=", "$=", "*="
"=", "~=", "|=", "^=", "$=", "*=", "'", "\""
};
static const char mycss_selectors_resource_combinator_names_map[][3] = {
"", " ", ">", "+", "~", "||"
"", " ", ">", "+", "~", "||", "'", "\""
};
#endif /* MyHTML_MyCSS_SELECTORS_MYOSI_RESOURCE_H */

View File

@ -21,6 +21,70 @@
#include "mycss/selectors/serialization.h"
#include "mycss/selectors/myosi_resource.h"
// void mycss_selectors_serialization_chain_contains(mycss_selectors_t* selectors, mycss_selectors_entry_t* selector,
// mycore_callback_serialize_f callback, void* context)
// {
// while(selector) {
// if(selector->combinator == MyCSS_SELECTORS_COMBINATOR_DESCENDANT)
// callback(" ", 1, context);
// else if(selector->combinator == MyCSS_SELECTORS_COMBINATOR_UNDEF) {
// /* "" */
// }
// else {
// callback(" ", 1, context);
// const char *comb_name = mycss_selectors_resource_combinator_names_map[ selector->combinator ];
// callback(comb_name, strlen(comb_name), context);
// callback(" ", 1, context);
// }
// mycss_selectors_serialization_selector(selectors, selector, callback, context);
// selector = selector->next;
// }
// }
// bool mycss_selectors_serialization_contains(mycss_selectors_t* selectors, mycss_selectors_list_t *selectors_list,
// mycore_callback_serialize_f callback, void* context)
// {
// // TODO: implement
// // FRANK
// // printf("\nmycss_selectors_serialization_contains()\n");
// while(selectors_list) {
// // FRANK
// // printf("\nmycss_selectors_serialization_list()\n");
// // printf("\tselectors_list->entries_list_length = %d\n", (int)selectors_list->entries_list_length);
// for(size_t i = 0; i < selectors_list->entries_list_length; i++)
// {
// mycss_selectors_entries_list_t *entries = &selectors_list->entries_list[i];
// mycss_selectors_serialization_chain_contains(selectors, entries->entry, callback, context);
// if((i + 1) != selectors_list->entries_list_length)
// callback(", ", 2, context);
// }
// // if(selectors_list->declaration_entry) {
// // callback(" {", 2, context);
// // mycss_declaration_serialization_entries(selectors->ref_entry, selectors_list->declaration_entry, callback, context);
// // callback("}", 1, context);
// // }
// // if(selectors_list->flags == MyCSS_SELECTORS_FLAGS_SELECTOR_BAD) {
// // callback("^BAD_SELECTOR_LIST", 18, context);
// // }
// if(selectors_list->next)
// callback("\n", 1, context);
// selectors_list = selectors_list->next;
// }
// return true;
// }
void mycss_selectors_serialization_chain(mycss_selectors_t* selectors, mycss_selectors_entry_t* selector,
mycore_callback_serialize_f callback, void* context)
{
@ -45,17 +109,6 @@ void mycss_selectors_serialization_chain(mycss_selectors_t* selectors, mycss_sel
}
}
bool mycss_selectors_serialization_contains(mycss_selectors_t* selectors, void *value,
mycore_callback_serialize_f callback, void* context)
{
// TODO: implement
// FRANK
printf("\nmycss_selectors_serialization_contains()\n");
return true;
}
bool mycss_selectors_serialization_list(mycss_selectors_t* selectors, mycss_selectors_list_t* selectors_list,
mycore_callback_serialize_f callback, void* context)
{

View File

@ -257,17 +257,19 @@ void * mycss_selectors_value_pseudo_class_function_contains_destroy(mycss_entry_
{
// FRANK
// TODO: implement
printf("\nmycss_selectors_value_pseudo_class_function_contains_destroy()\n");
// printf("\nmycss_selectors_value_pseudo_class_function_contains_destroy()\n");
if(value == NULL)
return NULL;
return mycss_selectors_list_destroy(entry->selectors, value, self_destroy);
if(self_destroy) {
mchar_async_free(entry->mchar, entry->mchar_value_node_id, value);
return NULL;
}
// if(value == NULL)
// return NULL;
return value;
// if(self_destroy) {
// mchar_async_free(entry->mchar, entry->mchar_value_node_id, value);
// return NULL;
// }
// return value;
}
void * mycss_selectors_value_pseudo_class_function_lang_destroy(mycss_entry_t* entry, void* value, bool self_destroy)

BIN
test/mycss/pseudo_class_contains Executable file

Binary file not shown.

View File

@ -89,7 +89,6 @@ mycss_selectors_list_t * prepare_selector(mycss_entry_t *css_entry, const char*
// printf("\nprepare_selector()\n");
// printf("\t%s\n", (list != NULL)?"has list":"no list");
// printf("\tlist->flags = %d\n", (int)list->flags);
/* check parsing errors */
if(list == NULL || (list->flags & MyCSS_SELECTORS_FLAGS_SELECTOR_BAD)) {
@ -125,9 +124,12 @@ void print_found_result(myhtml_tree_t* html_tree, myhtml_collection_t *collectio
int main(int argc, const char * argv[])
{
const char *html = "<div>Another<p>Hello World</p><p>Something Else</p></div>";
const char *html = "<div>Another<p id=\"hello world\">Hello World</p><p id=\"world\">World</p></div>";
// const char *selector = ":has(p)";
const char *selector = "p:contains(Hello World)";
// const char *selector = ":contains(Hello World)";
const char *selector = ":contains(World)";
// const char *selector = "p:contains('Hello World')"; // Bad Selector
// const char *selector = "p:contains(\"Hello World\")"; // Bad Selector
/* init MyHTML and parse HTML */
myhtml_tree_t *html_tree = parse_html(html, strlen(html));