AmendHub

Download:

jcs

/

detritus

/

amendments

/

62

http+html: Start on outputting fields


jcs made amendment 62 about 1 year ago
--- html.c Fri Dec 13 21:41:20 2024 +++ html.c Fri Dec 20 20:16:04 2024 @@ -228,8 +228,8 @@ html_is_block_tag(struct html_page *html, html_tag_typ } long -html_get_attribute_value(struct html_page *html, struct html_element *element, - char *name, char **ret) +html_get_attribute_value(struct html_page *html, + struct html_element *element, char *name, char **ret) { short n, namelen; @@ -301,7 +301,7 @@ html_render_current_node(struct html_page *html, bool html_tag_names[el->type])); } else { HTML_DEBUG(("[margin-top:%s\\r]", html_tag_names[el->type])); - html_output(html->cookie, html, "\r", 1); + html_output_margin(html->cookie, html); } html->last_margin_bottom = 0; } @@ -315,15 +315,7 @@ html_render_current_node(struct html_page *html, bool break; case HTML_TAG_INPUT: have_height = true; - - html_output(html->cookie, html, "[ input type=", 13); - - len = html_get_attribute_value(html, el, "type", &val); - if (val) - html_output(html->cookie, html, val, len); - else - html_output(html->cookie, html, "(none)", 6); - html_output(html->cookie, html, " ]", 2); + html_output_field(html->cookie, html, el); break; case HTML_TAG_IMG: have_height = true; @@ -460,7 +452,7 @@ html_render_current_node(struct html_page *html, bool /* unless the last element had a bottom margin */ if (!html->last_margin_bottom) { HTML_DEBUG(("[margin-bottom\\r]")); - html_output(html->cookie, html, "\r", 1); + html_output_margin(html->cookie, html); html->last_margin_bottom = el->margin_bottom; } } --- html.h Fri Dec 13 21:20:36 2024 +++ html.h Fri Dec 20 20:27:28 2024 @@ -20,23 +20,13 @@ #include <string.h> #include "stdint.h" -/* external functions the caller needs to provide */ -#if 0 -extern void panic(const char *format, ...); -extern void * xmalloc(size_t); -extern void xfree(void *ptrptr); -extern void * xmalloczero(size_t); -extern void * xrealloc(void *src, size_t size); -extern size_t strlcpy(char *dst, const char *src, size_t dsize); -extern size_t strlcat(char *dst, const char *src, size_t dsize); -extern short snprintf(char *s, size_t size, const char *fmt, ...); -extern short strcasecmp(const char *s1, const char *s2); -extern short strncasecmp(const char *s1, const char *s2, size_t n); -#else #include "util.h" -#endif + void html_output(void *cookie, struct html_page *html, char *str, size_t len); +void html_output_margin(void *cookie, struct html_page *html); +void html_output_field(void *cookie, struct html_page *html, + struct html_element *el); void html_debug(const char *fmt, ...); void html_have_title(void *cookie, struct html_page *html, char *str, size_t len); @@ -453,8 +443,7 @@ struct html_tag { html_namespace ns; char name[16]; short name_len; - /* TODO: make this dynamic so it's not so many KB on the stack */ - struct html_attr attrs[8]; + struct html_attr attrs[16]; short attrs_count; bool emitted; bool self_closing; @@ -480,6 +469,8 @@ struct html_element { short ol_count; short renders; + TEHandle input_te; + short refs; struct html_element *next_need_free; }; @@ -503,11 +494,11 @@ struct html_doctype { /* this must be first */ html_token_type _pad; - char name[32]; + char name[16]; short name_len; - char public_identifier[32]; + char public_identifier[16]; short public_identifier_len; - char system_identifier[32]; + char system_identifier[16]; short system_identifier_len; bool system_identifier_found; bool force_quirks; --- http.c Fri Dec 13 21:56:07 2024 +++ http.c Fri Dec 20 20:44:39 2024 @@ -36,11 +36,13 @@ struct http_page { bool http_accept_uri(struct URI *uri); bool http_request_init(page_handle pageh); bool http_process(page_handle pageh); +void http_update(page_handle pageh); void http_reset(page_handle pageh); void http_free(page_handle pageh); bool html_parse_page(page_handle pageh); static void print_plaintext(struct page *page); +void html_compute_style(struct page *page, struct html_page *html); struct page_handler http_handler = { http_accept_uri, @@ -49,8 +51,9 @@ struct page_handler http_handler = { page_consume_data, page_request_cleanup, http_process, + http_update, http_reset, - http_free, + http_free }; bool @@ -241,9 +244,8 @@ http_free(page_handle pageh) } void -html_output(void *cookie, struct html_page *html, char *str, size_t len) +html_compute_style(struct page *page, struct html_page *html) { - struct page *page = *((page_handle)cookie); short n, j; char *val; @@ -372,11 +374,93 @@ html_output(void *cookie, struct html_page *html, char if (page->cur_style.size < 9) page->cur_style.size = 9; +} + +void +html_output(void *cookie, struct html_page *html, char *str, size_t len) +{ + struct page *page = *((page_handle)cookie); + html_compute_style(page, html); + if (!TVAppend(page->browser->output_tv, &page->cur_style, str, len)) panic("out of memory in TVAppend"); html->last_output = str[len - 1]; +} + +void +html_output_margin(void *cookie, struct html_page *html) +{ + struct page *page = *((page_handle)cookie); + + html_compute_style(page, html); + page->cur_style.size = (page->cur_style.size * 75) / 100; + + if (!TVAppend(page->browser->output_tv, &page->cur_style, "\r", 1)) + panic("out of memory in TVAppend"); + + html->last_output = '\r'; +} + +void +html_output_field(void *cookie, struct html_page *html, + struct html_element *el) +{ + struct page *page = *((page_handle)cookie); + size_t len; + char *val, *filler; + short osize, isize; + BigRect brect; + Rect rect; + + len = html_get_attribute_value(html, el, "type", &val); + if (val && strcasecmp(val, "text") == 0) { + page->cur_style.tag = (unsigned long)el; + osize = page->cur_style.size; + + isize = 0; + if (html_get_attribute_value(html, el, "size", &val)) + isize = atoi(val); + if (isize < 3) + isize = 3; + + filler = xmalloczero(isize + 4); + if (filler == NULL) + return; + + page->cur_style.size = 12; + TVAppend(page->browser->output_tv, &page->cur_style, filler, + isize + 3); + xfree(&filler); + + browser_find_tag_rect(page->browser, page->cur_style.tag, &brect); + TVBigRectToLocalRect(page->browser->output_tv, &brect, &rect); + + el->input_te = TENew(&rect, &rect); + FrameRect(&rect); + isize = html_get_attribute_value(html, el, "value", &val); + if (isize) { + TESetText(val, isize, el->input_te); + } else + TESetText("hello", 5, el->input_te); + + page->cur_style.tag = 0; + page->cur_style.size = osize; + } else { + html_output(html->cookie, html, "[ input type=", 13); + + if (val) + html_output(html->cookie, html, val, len); + else + html_output(html->cookie, html, "(none)", 6); + html_output(html->cookie, html, " ]", 2); + } +} + +void +http_update(page_handle pageh) +{ } void