WebView 3.11: refactoring tag parse

git-svn-id: svn://kolibrios.org@8440 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Kirill Lipatov (Leency) 2020-12-18 01:47:36 +00:00
parent 64a81df7e7
commit 4c60cafcc0
4 changed files with 77 additions and 72 deletions

View File

@ -247,7 +247,7 @@ void TWebBrowser::ParseHtml(dword _bufpointer, _bufsize){
goto _DEFAULT; goto _DEFAULT;
} }
_TAG: _TAG:
if (tag.parse_tag(#bufpos, bufpointer + bufsize)) { if (tag.parse(#bufpos, bufpointer + bufsize)) {
CheckForLineBreak(); CheckForLineBreak();
Paint(); Paint();
$push cur_encoding $push cur_encoding
@ -307,7 +307,6 @@ bool TWebBrowser::CheckForLineBreak()
strcpy(#line, #new_line_text); strcpy(#line, #new_line_text);
NewLine(); NewLine();
//while (CheckForLineBreak()==true) {};
return true; return true;
} }
//============================================================================================ //============================================================================================

View File

@ -3,15 +3,14 @@ struct _tag
{ {
char name[32]; char name[32];
char prior[32]; char prior[32];
char params[6000];
bool opened; bool opened;
collection attributes; collection attributes;
collection values; collection values;
dword value; dword value;
bool is(); bool is();
bool parse_tag(); bool parse();
void debug_tag(); void debug_tag();
bool get_next_param(); dword get_next_param();
dword get_value_of(); dword get_value_of();
} tag=0; } 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; bool retok = true;
dword bufpos = ESDWORD[_bufpos]; dword bufpos = ESDWORD[_bufpos];
dword params, paramsend;
dword closepos; dword closepos;
dword whitepos; dword whitepos;
if (name) strcpy(#prior, #name); else prior = '\0'; if (name) strcpy(#prior, #name); else prior = '\0';
name = '\0'; name = '\0';
params = '\0';
attributes.drop(); attributes.drop();
values.drop(); values.drop();
@ -47,6 +46,7 @@ bool _tag::parse_tag(dword _bufpos, bufend)
bufpos++; bufpos++;
} }
bufpos+=2; bufpos+=2;
retok = false;
goto _RET; goto _RET;
} }
@ -59,39 +59,46 @@ bool _tag::parse_tag(dword _bufpos, bufend)
closepos = strchr(bufpos, '>'); closepos = strchr(bufpos, '>');
whitepos = strchrw(bufpos, bufend-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 //no param
strncpy(#name, bufpos, math.min(closepos - bufpos, sizeof(tag.name))); strncpy(#name, bufpos, math.min(closepos - bufpos, sizeof(tag.name)));
debug_tag(); debug_tag();
params = '\0';
bufpos = closepos; bufpos = closepos;
} else { } else {
//we have param //we have param
strncpy(#name, bufpos, math.min(whitepos - bufpos, sizeof(tag.name))); strncpy(#name, bufpos, math.min(whitepos - bufpos, sizeof(tag.name)));
strncpy(#params, whitepos, math.min(closepos - whitepos, sizeof(tag.params)));
debug_tag(); debug_tag();
bufpos = closepos; 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, "</%s>", #prior);
if (strstri(bufpos, #name)) bufpos = EAX-1;
retok = false;
} else {
if (name[strlen(#name)-1]=='/') name[strlen(#name)-1]=NULL; //for <br/>
}
} else {
retok = false; 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, "</%s>", #prior);
if (strstri(bufpos, #name)) bufpos = EAX-1;
retok = false;
goto _RET;
}
if (name[strlen(#name)-1]=='/') name[strlen(#name)-1]=NULL; //for <br/>
_RET: _RET:
ESDWORD[_bufpos] = bufpos; ESDWORD[_bufpos] = bufpos;
return retok; return retok;
@ -104,63 +111,58 @@ void _tag::debug_tag()
if (!opened) debugch('/'); if (!opened) debugch('/');
debug(#name); debug(#name);
debugln(">"); debugln(">");
if (params) {
debug("params: ");
debugln(#params+1);
}
} }
} }
bool _tag::get_next_param() dword _tag::get_next_param(dword ps, pe)
{ {
byte quotes = NULL; // "ps" - param start
int i; // "pe" - param end
// "q" - quote char
char q = NULL;
dword fixeq;
unsigned char val[6000]; unsigned char val[6000];
unsigned char attr[6000]; unsigned char attr[6000];
if (!params) return false;
i = strlen(#params) - 1; if (ESBYTE[pe] == '/') pe--;
if (params[i] == '/') i--; while (pe>ps) && (__isWhite(ESBYTE[pe])) pe--;
while (i>0) && (__isWhite(params[i])) i--;
if (params[i] == '"') || (params[i] == '\'') if (ESBYTE[pe] == '"') || (ESBYTE[pe] == '\'')
{ {
//remove quotes //remove quote
quotes = params[i]; q = ESBYTE[pe];
params[i] = '\0'; ESBYTE[pe] = '\0';
i--; pe--;
//find VAL start and copy //find VAL start and copy
i = strrchr(#params, quotes); pe = strrchr(ps, q) + ps;
strlcpy(#val, #params + i, sizeof(val)-1); strlcpy(#val, pe, sizeof(val)-1);
params[i] = '\0'; ESBYTE[pe] = '\0';
i--; pe--;
//find ATTR end //find ATTR end
while (i > 0) && (params[i] != '=') i--; while (pe > ps) && (ESBYTE[pe] != '=') pe--;
params[i+1] = '\0'; ESBYTE[pe+1] = '\0';
} }
else else
{ {
//find VAL start and copy //find VAL start and copy
while (i > 0) && (params[i] != '=') i--; while (pe > ps) && (ESBYTE[pe] != '=') pe--;
i++; pe++;
strlcpy(#val, #params + i, sizeof(val)-1); strlcpy(#val, pe, sizeof(val)-1);
//already have ATTR end //already have ATTR end
} }
//find ATTR start and copy //find ATTR start and copy
while (i>0) && (!__isWhite(params[i])) i--; while (pe>ps) && (!__isWhite(ESBYTE[pe])) pe--;
strlcpy(#attr, #params + i + 1, sizeof(attr)-1); strlcpy(#attr, pe + 1, sizeof(attr)-1);
params[i] = '\0'; ESBYTE[pe] = '\0';
//fix case: src=./images/KolibriOS_logo2.jpg?sid=e8ece8b38b //fix case: src=./images/KolibriOS_logo2.jpg?sid=e8ece8b38b
i = strchr(#attr,'='); fixeq = strchr(#attr,'=');
if (!quotes) && (i) { if (!q) && (fixeq) {
strlcpy(#val, i+1, sizeof(val)-1); strlcpy(#val, fixeq+1, sizeof(val)-1);
ESBYTE[i+1] = '\0'; ESBYTE[fixeq+1] = '\0';
} }
strlwr(#attr); strlwr(#attr);
strrtrim(#val); strrtrim(#val);
@ -174,7 +176,8 @@ bool _tag::get_next_param()
debugch('\n'); debugch('\n');
} }
return true; if (pe==ps) return NULL;
return pe;
} }
dword _tag::get_value_of(dword _attr_name) dword _tag::get_value_of(dword _attr_name)

View File

@ -41,7 +41,7 @@
// DATA // // DATA //
// // // //
//===================================================// //===================================================//
char version[]="WebView 3.1"; char version[]="WebView 3.11";
#define DEFAULT_URL URL_SERVICE_HOMEPAGE #define DEFAULT_URL URL_SERVICE_HOMEPAGE
@ -223,6 +223,7 @@ void main()
notify("'Too many redirects.' -E"); notify("'Too many redirects.' -E");
StopLoading(); StopLoading();
redirect_count = 0; redirect_count = 0;
if (http_get_type==IMG) goto _IMG_RES;
} }
} else { } else {
// Loading the page is complete, free resources // Loading the page is complete, free resources
@ -235,6 +236,7 @@ void main()
LoadInternalPage(cache.current_buf, cache.current_size); LoadInternalPage(cache.current_buf, cache.current_size);
} }
else if (http_get_type==IMG) { else if (http_get_type==IMG) {
_IMG_RES:
cache.add(cur_img_url, http.content_pointer, http.content_received, IMG); cache.add(cur_img_url, http.content_pointer, http.content_received, IMG);
free(http.content_pointer); free(http.content_pointer);
GetImg(false); GetImg(false);
@ -934,6 +936,7 @@ dword GetImg(bool _new)
for (i = 0; i < WB1.img_url.count; i++) for (i = 0; i < WB1.img_url.count; i++)
{ {
cur_img_url = WB1.img_url.get(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) { if (cache.has(cur_img_url)==false) {
prbar.max = WB1.img_url.count; prbar.max = WB1.img_url.count;
prbar.value = i; prbar.value = i;

View File

@ -7,12 +7,12 @@ char rmb_menu[] =
"<EFBFBD>®á¬®âà¥âì ¨á室­¨ª|Ctrl+U "<EFBFBD>®á¬®âà¥âì ¨á室­¨ª|Ctrl+U
<EFBFBD>¥¤ ªâ¨à®¢ âì ¨á室­¨ª"; <EFBFBD>¥¤ ªâ¨à®¢ âì ¨á室­¨ª";
char main_menu[] = char main_menu[] =
"Žâªàëâì ä ©«...|Ctrl+O "Žâªàëâì ä ©«...|Ctrl+O
<EFBFBD>®¢®¥ ®ª­®|Ctrl+N <EFBFBD>®¢®¥ ®ª­®|Ctrl+N
- -
ˆáâ®à¨ï|Ctrl+H ˆáâ®à¨ï|Ctrl+H
Œ¥­¥¤¦¥à § £à㧮ª|Ctrl+J Œ¥­¥¤¦¥à § £à㧮ª |Ctrl+J
Žç¨áâ¨âì ªíè Žç¨áâ¨âì ªíè|Ctrl+F5
Ž¡­®¢¨âì ¡à ã§¥à"; Ž¡­®¢¨âì ¡à ã§¥à";
char link_menu[] = char link_menu[] =
"Žâªàëâì ¢ ­®¢®© ¢ª« ¤ª¥ "Žâªàëâì ¢ ­®¢®© ¢ª« ¤ª¥
@ -39,12 +39,12 @@ char rmb_menu[] =
"View source|Ctrl+U "View source|Ctrl+U
Edit source"; Edit source";
char main_menu[] = char main_menu[] =
"Open local file...|Ctrl+O "Open local file...|Ctrl+O
New window|Ctrl+N New window|Ctrl+N
- -
History|Ctrl+H History|Ctrl+H
Download Manager|Ctrl+J Download Manager |Ctrl+J
Clear cache Clear cache|Ctrl+F5
Update browser"; Update browser";
char link_menu[] = char link_menu[] =
"Open in new tab "Open in new tab