From 4c60cafcc0fcda3ab6673374a7116b24f3659865 Mon Sep 17 00:00:00 2001 From: "Kirill Lipatov (Leency)" Date: Fri, 18 Dec 2020 01:47:36 +0000 Subject: [PATCH] WebView 3.11: refactoring tag parse git-svn-id: svn://kolibrios.org@8440 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/cmm/browser/TWB/TWB.c | 3 +- programs/cmm/browser/TWB/parse_tag.h | 121 ++++++++++++++------------- programs/cmm/browser/WebView.c | 5 +- programs/cmm/browser/const.h | 20 ++--- 4 files changed, 77 insertions(+), 72 deletions(-) diff --git a/programs/cmm/browser/TWB/TWB.c b/programs/cmm/browser/TWB/TWB.c index 392c30b9b4..77681baea7 100644 --- a/programs/cmm/browser/TWB/TWB.c +++ b/programs/cmm/browser/TWB/TWB.c @@ -247,7 +247,7 @@ void TWebBrowser::ParseHtml(dword _bufpointer, _bufsize){ goto _DEFAULT; } _TAG: - if (tag.parse_tag(#bufpos, bufpointer + bufsize)) { + if (tag.parse(#bufpos, bufpointer + bufsize)) { CheckForLineBreak(); Paint(); $push cur_encoding @@ -307,7 +307,6 @@ bool TWebBrowser::CheckForLineBreak() strcpy(#line, #new_line_text); NewLine(); - //while (CheckForLineBreak()==true) {}; return true; } //============================================================================================ diff --git a/programs/cmm/browser/TWB/parse_tag.h b/programs/cmm/browser/TWB/parse_tag.h index 6c733f53d7..f7ed198446 100644 --- a/programs/cmm/browser/TWB/parse_tag.h +++ b/programs/cmm/browser/TWB/parse_tag.h @@ -3,15 +3,14 @@ struct _tag { char name[32]; char prior[32]; - char params[6000]; bool opened; collection attributes; collection values; dword value; bool is(); - bool parse_tag(); + bool parse(); void debug_tag(); - bool get_next_param(); + dword get_next_param(); dword get_value_of(); } tag=0; @@ -24,17 +23,17 @@ bool _tag::is(dword _text) } } -bool _tag::parse_tag(dword _bufpos, bufend) +bool _tag::parse(dword _bufpos, bufend) { bool retok = true; dword bufpos = ESDWORD[_bufpos]; + dword params, paramsend; dword closepos; dword whitepos; if (name) strcpy(#prior, #name); else prior = '\0'; name = '\0'; - params = '\0'; attributes.drop(); values.drop(); @@ -47,6 +46,7 @@ bool _tag::parse_tag(dword _bufpos, bufend) bufpos++; } bufpos+=2; + retok = false; goto _RET; } @@ -59,39 +59,46 @@ bool _tag::parse_tag(dword _bufpos, bufend) closepos = strchr(bufpos, '>'); whitepos = strchrw(bufpos, bufend-bufpos); - if (whitepos > closepos) { + + if (debug_mode) { + if (!closepos) debugln("null closepos"); + if (!whitepos) debugln("null whitepos"); + } + + if (!whitepos) || (whitepos > closepos) { //no param strncpy(#name, bufpos, math.min(closepos - bufpos, sizeof(tag.name))); debug_tag(); - params = '\0'; bufpos = closepos; } else { //we have param strncpy(#name, bufpos, math.min(whitepos - bufpos, sizeof(tag.name))); - strncpy(#params, whitepos, math.min(closepos - whitepos, sizeof(tag.params))); debug_tag(); bufpos = closepos; - while (get_next_param()); + + params = malloc(closepos - whitepos + 1); + strncpy(params, whitepos, closepos - whitepos); + if (debug_mode) { debug("params: "); debugln(params+1); } + paramsend = params + closepos - whitepos; + while (paramsend = get_next_param(params, paramsend-1)); + free(params); } - if (!name) { + if (name) { + strlwr(#name); + // ignore text inside the next tags + if (is("script")) || (is("style")) || (is("binary")) || (is("select")) { + strcpy(#prior, #name); + sprintf(#name, "", #prior); + if (strstri(bufpos, #name)) bufpos = EAX-1; + retok = false; + } else { + if (name[strlen(#name)-1]=='/') name[strlen(#name)-1]=NULL; //for
+ } + } else { retok = false; - goto _RET; } - strlwr(#name); - - // ignore text inside the next tags - if (is("script")) || (is("style")) || (is("binary")) || (is("select")) { - strcpy(#prior, #name); - sprintf(#name, "", #prior); - if (strstri(bufpos, #name)) bufpos = EAX-1; - retok = false; - goto _RET; - } - - if (name[strlen(#name)-1]=='/') name[strlen(#name)-1]=NULL; //for
- _RET: ESDWORD[_bufpos] = bufpos; return retok; @@ -104,63 +111,58 @@ void _tag::debug_tag() if (!opened) debugch('/'); debug(#name); debugln(">"); - if (params) { - debug("params: "); - debugln(#params+1); - } } } -bool _tag::get_next_param() +dword _tag::get_next_param(dword ps, pe) { - byte quotes = NULL; - int i; + // "ps" - param start + // "pe" - param end + // "q" - quote char + char q = NULL; + dword fixeq; unsigned char val[6000]; unsigned char attr[6000]; - - if (!params) return false; - i = strlen(#params) - 1; - if (params[i] == '/') i--; - while (i>0) && (__isWhite(params[i])) i--; + if (ESBYTE[pe] == '/') pe--; + while (pe>ps) && (__isWhite(ESBYTE[pe])) pe--; - if (params[i] == '"') || (params[i] == '\'') + if (ESBYTE[pe] == '"') || (ESBYTE[pe] == '\'') { - //remove quotes - quotes = params[i]; - params[i] = '\0'; - i--; + //remove quote + q = ESBYTE[pe]; + ESBYTE[pe] = '\0'; + pe--; //find VAL start and copy - i = strrchr(#params, quotes); - strlcpy(#val, #params + i, sizeof(val)-1); - params[i] = '\0'; - i--; + pe = strrchr(ps, q) + ps; + strlcpy(#val, pe, sizeof(val)-1); + ESBYTE[pe] = '\0'; + pe--; //find ATTR end - while (i > 0) && (params[i] != '=') i--; - params[i+1] = '\0'; + while (pe > ps) && (ESBYTE[pe] != '=') pe--; + ESBYTE[pe+1] = '\0'; } else { //find VAL start and copy - while (i > 0) && (params[i] != '=') i--; - i++; - strlcpy(#val, #params + i, sizeof(val)-1); - + while (pe > ps) && (ESBYTE[pe] != '=') pe--; + pe++; + strlcpy(#val, pe, sizeof(val)-1); //already have ATTR end } //find ATTR start and copy - while (i>0) && (!__isWhite(params[i])) i--; - strlcpy(#attr, #params + i + 1, sizeof(attr)-1); - params[i] = '\0'; + while (pe>ps) && (!__isWhite(ESBYTE[pe])) pe--; + strlcpy(#attr, pe + 1, sizeof(attr)-1); + ESBYTE[pe] = '\0'; //fix case: src=./images/KolibriOS_logo2.jpg?sid=e8ece8b38b - i = strchr(#attr,'='); - if (!quotes) && (i) { - strlcpy(#val, i+1, sizeof(val)-1); - ESBYTE[i+1] = '\0'; + fixeq = strchr(#attr,'='); + if (!q) && (fixeq) { + strlcpy(#val, fixeq+1, sizeof(val)-1); + ESBYTE[fixeq+1] = '\0'; } strlwr(#attr); strrtrim(#val); @@ -174,7 +176,8 @@ bool _tag::get_next_param() debugch('\n'); } - return true; + if (pe==ps) return NULL; + return pe; } dword _tag::get_value_of(dword _attr_name) diff --git a/programs/cmm/browser/WebView.c b/programs/cmm/browser/WebView.c index 2ee9044352..f60484b1d8 100644 --- a/programs/cmm/browser/WebView.c +++ b/programs/cmm/browser/WebView.c @@ -41,7 +41,7 @@ // DATA // // // //===================================================// -char version[]="WebView 3.1"; +char version[]="WebView 3.11"; #define DEFAULT_URL URL_SERVICE_HOMEPAGE @@ -223,6 +223,7 @@ void main() notify("'Too many redirects.' -E"); StopLoading(); redirect_count = 0; + if (http_get_type==IMG) goto _IMG_RES; } } else { // Loading the page is complete, free resources @@ -235,6 +236,7 @@ void main() LoadInternalPage(cache.current_buf, cache.current_size); } else if (http_get_type==IMG) { + _IMG_RES: cache.add(cur_img_url, http.content_pointer, http.content_received, IMG); free(http.content_pointer); GetImg(false); @@ -934,6 +936,7 @@ dword GetImg(bool _new) for (i = 0; i < WB1.img_url.count; i++) { cur_img_url = WB1.img_url.get(i); + if (debug_mode) {debug("get img: ");debugln(cur_img_url);} if (cache.has(cur_img_url)==false) { prbar.max = WB1.img_url.count; prbar.value = i; diff --git a/programs/cmm/browser/const.h b/programs/cmm/browser/const.h index 953068b6a6..4f461ca4fd 100644 --- a/programs/cmm/browser/const.h +++ b/programs/cmm/browser/const.h @@ -7,12 +7,12 @@ char rmb_menu[] = "Посмотреть исходник|Ctrl+U Редактировать исходник"; char main_menu[] = -"Открыть файл...|Ctrl+O -Новое окно|Ctrl+N +"Открыть файл...|Ctrl+O +Новое окно|Ctrl+N - -История|Ctrl+H -Менеджер загрузок|Ctrl+J -Очистить кэш +История|Ctrl+H +Менеджер загрузок |Ctrl+J +Очистить кэш|Ctrl+F5 Обновить браузер"; char link_menu[] = "Открыть в новой вкладке @@ -39,12 +39,12 @@ char rmb_menu[] = "View source|Ctrl+U Edit source"; char main_menu[] = -"Open local file...|Ctrl+O -New window|Ctrl+N +"Open local file...|Ctrl+O +New window|Ctrl+N - -History|Ctrl+H -Download Manager|Ctrl+J -Clear cache +History|Ctrl+H +Download Manager |Ctrl+J +Clear cache|Ctrl+F5 Update browser"; char link_menu[] = "Open in new tab