Fix the memleak. Part II of the 927 bugfix.
[elinks.git] / src / document / html / renderer.c
blob8430c96204229f154988a6e14bb74d6ebc658c1d
1 /* HTML renderer */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <ctype.h>
8 #include <stdarg.h>
9 #include <string.h>
11 #include "elinks.h"
13 #include "cache/cache.h"
14 #include "config/options.h"
15 #include "document/docdata.h"
16 #include "document/document.h"
17 #include "document/html/frames.h"
18 #include "document/html/parser.h"
19 #include "document/html/parser/parse.h"
20 #include "document/html/renderer.h"
21 #include "document/html/tables.h"
22 #include "document/options.h"
23 #include "document/refresh.h"
24 #include "document/renderer.h"
25 #include "intl/charsets.h"
26 #include "osdep/types.h"
27 #include "protocol/uri.h"
28 #include "session/session.h"
29 #include "terminal/color.h"
30 #include "terminal/draw.h"
31 #include "util/color.h"
32 #include "util/conv.h"
33 #include "util/error.h"
34 #include "util/hash.h"
35 #include "util/lists.h"
36 #include "util/memory.h"
37 #include "util/string.h"
38 #include "util/time.h"
39 #include "viewer/text/form.h"
40 #include "viewer/text/view.h"
41 #include "viewer/text/vs.h"
43 /* Unsafe macros */
44 #include "document/html/internal.h"
46 /* Types and structs */
48 /* Tags are used for ``id''s or anchors in the document referenced by the
49 * fragment part of the URI. */
50 /* FIXME: This and find_tag() should be part of the general infrastructure
51 * in document/document.*. --pasky */
52 struct tag {
53 LIST_HEAD(struct tag);
55 int x, y;
56 unsigned char name[1]; /* must be last of struct. --Zas */
59 enum link_state {
60 LINK_STATE_NONE,
61 LINK_STATE_NEW,
62 LINK_STATE_SAME,
65 struct link_state_info {
66 unsigned char *link;
67 unsigned char *target;
68 unsigned char *image;
69 struct form_control *form;
72 struct table_cache_entry_key {
73 unsigned char *start;
74 unsigned char *end;
75 int align;
76 int margin;
77 int width;
78 int x;
79 int link_num;
82 struct table_cache_entry {
83 LIST_HEAD(struct table_cache_entry);
85 struct table_cache_entry_key key;
86 struct part part;
89 /* Max. entries in table cache used for nested tables. */
90 #define MAX_TABLE_CACHE_ENTRIES 16384
92 /* Global variables */
93 static int table_cache_entries;
94 static struct hash *table_cache;
97 struct renderer_context {
98 int last_link_to_move;
99 struct tag *last_tag_to_move;
100 /* All tags between document->tags and this tag (inclusive) should
101 * be aligned to the next line break, unless some real content follows
102 * the tag. Therefore, this virtual tags list accumulates new tags as
103 * they arrive and empties when some real content is written; if a line
104 * break is inserted in the meanwhile, the tags follow it (ie. imagine
105 * <a name="x"> <p>, then the "x" tag follows the line breaks inserted
106 * by the <p> tag). */
107 struct tag *last_tag_for_newline;
109 struct link_state_info link_state_info;
111 struct conv_table *convert_table;
113 /* Used for setting cache info from HTTP-EQUIV meta tags. */
114 struct cache_entry *cached;
116 int g_ctrl_num;
117 int subscript; /* Count stacked subscripts */
118 int supscript; /* Count stacked supscripts */
120 unsigned int empty_format:1;
121 unsigned int nobreak:1;
122 unsigned int nosearchable:1;
123 unsigned int nowrap:1; /* Activated/deactivated by SP_NOWRAP. */
126 static struct renderer_context renderer_context;
129 /* Prototypes */
130 static void line_break(struct html_context *);
131 static void put_chars(struct html_context *, unsigned char *, int);
133 #define X(x_) (part->box.x + (x_))
134 #define Y(y_) (part->box.y + (y_))
136 #define SPACES_GRANULARITY 0x7F
138 #define ALIGN_SPACES(x, o, n) mem_align_alloc(x, o, n, SPACES_GRANULARITY)
140 static inline void
141 set_screen_char_color(struct screen_char *schar,
142 color_T bgcolor, color_T fgcolor,
143 enum color_flags color_flags,
144 enum color_mode color_mode)
146 struct color_pair colors = INIT_COLOR_PAIR(bgcolor, fgcolor);
148 set_term_color(schar, &colors, color_flags, color_mode);
151 static int
152 realloc_line(struct html_context *html_context, struct document *document,
153 int y, int length)
155 struct screen_char *pos, *end;
156 struct line *line;
157 int orig_length;
159 if (!realloc_lines(document, y))
160 return -1;
162 line = &document->data[y];
163 orig_length = line->length;
165 if (length < orig_length)
166 return orig_length;
168 if (!ALIGN_LINE(&line->chars, line->length, length + 1))
169 return -1;
171 /* We cannot rely on the aligned allocation to clear the members for us
172 * since for line splitting we simply trim the length. Question is if
173 * it is better to to clear the line after the splitting or here. */
174 end = &line->chars[length];
175 end->data = ' ';
176 end->attr = 0;
177 set_screen_char_color(end, par_format.bgcolor, 0x0,
178 0, document->options.color_mode);
180 for (pos = &line->chars[line->length]; pos < end; pos++) {
181 copy_screen_chars(pos, end, 1);
184 line->length = length + 1;
186 return orig_length;
189 void
190 expand_lines(struct html_context *html_context, struct part *part,
191 int x, int y, int lines, color_T bgcolor)
193 int line;
195 assert(part && part->document);
196 if_assert_failed return;
198 if (!use_document_bg_colors(&part->document->options))
199 return;
201 par_format.bgcolor = bgcolor;
203 for (line = 0; line < lines; line++)
204 realloc_line(html_context, part->document, Y(y + line), X(x));
207 static inline int
208 realloc_spaces(struct part *part, int length)
210 if (length < part->spaces_len)
211 return 0;
213 if (!ALIGN_SPACES(&part->spaces, part->spaces_len, length))
214 return -1;
215 #ifdef CONFIG_UTF8
216 if (!ALIGN_SPACES(&part->char_width, part->spaces_len, length))
217 return -1;
218 #endif
220 part->spaces_len = length;
222 return 0;
226 #define LINE(y_) part->document->data[Y(y_)]
227 #define POS(x_, y_) LINE(y_).chars[X(x_)]
228 #define LEN(y_) int_max(LINE(y_).length - part->box.x, 0)
231 /* When we clear chars we want to preserve and use the background colors
232 * already in place else we could end up ``staining'' the background especial
233 * when drawing table cells. So make the cleared chars share the colors in
234 * place. */
235 static inline void
236 clear_hchars(struct html_context *html_context, int x, int y, int width)
238 struct part *part;
239 struct screen_char *pos, *end;
241 assert(html_context);
242 if_assert_failed return;
244 part = html_context->part;
246 assert(part && part->document && width > 0);
247 if_assert_failed return;
249 if (realloc_line(html_context, part->document, Y(y), X(x) + width - 1) < 0)
250 return;
252 assert(part->document->data);
253 if_assert_failed return;
255 pos = &POS(x, y);
256 end = pos + width - 1;
257 end->data = ' ';
258 end->attr = 0;
259 set_screen_char_color(end, par_format.bgcolor, 0x0,
260 0, part->document->options.color_mode);
262 while (pos < end)
263 copy_screen_chars(pos++, end, 1);
266 /* TODO: Merge parts with get_format_screen_char(). --jonas */
267 /* Allocates the required chars on the given line and returns the char at
268 * position (x, y) ready to be used as a template char. */
269 static inline struct screen_char *
270 get_frame_char(struct html_context *html_context, struct part *part,
271 int x, int y, unsigned char data,
272 color_T bgcolor, color_T fgcolor)
274 struct screen_char *template;
276 assert(html_context);
277 if_assert_failed return NULL;
279 assert(part && part->document && x >= 0 && y >= 0);
280 if_assert_failed return NULL;
282 if (realloc_line(html_context, part->document, Y(y), X(x)) < 0)
283 return NULL;
285 assert(part->document->data);
286 if_assert_failed return NULL;
288 template = &POS(x, y);
289 template->data = data;
290 template->attr = SCREEN_ATTR_FRAME;
291 set_screen_char_color(template, bgcolor, fgcolor,
292 part->document->options.color_flags,
293 part->document->options.color_mode);
295 return template;
298 void
299 draw_frame_hchars(struct part *part, int x, int y, int width,
300 unsigned char data, color_T bgcolor, color_T fgcolor,
301 struct html_context *html_context)
303 struct screen_char *template;
305 assert(width > 0);
306 if_assert_failed return;
308 template = get_frame_char(html_context, part, x + width - 1, y, data, bgcolor, fgcolor);
309 if (!template) return;
311 /* The template char is the last we need to draw so only decrease @width. */
312 for (width -= 1; width; width--, x++) {
313 copy_screen_chars(&POS(x, y), template, 1);
317 void
318 draw_frame_vchars(struct part *part, int x, int y, int height,
319 unsigned char data, color_T bgcolor, color_T fgcolor,
320 struct html_context *html_context)
322 struct screen_char *template = get_frame_char(html_context, part, x, y,
323 data, bgcolor, fgcolor);
325 if (!template) return;
327 /* The template char is the first vertical char to be drawn. So
328 * copy it to the rest. */
329 for (height -= 1, y += 1; height; height--, y++) {
330 if (realloc_line(html_context, part->document, Y(y), X(x)) < 0)
331 return;
333 copy_screen_chars(&POS(x, y), template, 1);
337 static inline struct screen_char *
338 get_format_screen_char(struct html_context *html_context,
339 enum link_state link_state)
341 static struct text_attrib_style ta_cache = { -1, 0x0, 0x0 };
342 static struct screen_char schar_cache;
344 if (memcmp(&ta_cache, &format.style, sizeof(ta_cache))) {
345 copy_struct(&ta_cache, &format.style);
347 schar_cache.attr = 0;
348 if (format.style.attr) {
349 if (format.style.attr & AT_UNDERLINE) {
350 schar_cache.attr |= SCREEN_ATTR_UNDERLINE;
353 if (format.style.attr & AT_BOLD) {
354 schar_cache.attr |= SCREEN_ATTR_BOLD;
357 if (format.style.attr & AT_ITALIC) {
358 schar_cache.attr |= SCREEN_ATTR_ITALIC;
361 if (format.style.attr & AT_GRAPHICS) {
362 schar_cache.attr |= SCREEN_ATTR_FRAME;
366 if (link_state != LINK_STATE_NONE
367 && html_context->options->underline_links) {
368 schar_cache.attr |= SCREEN_ATTR_UNDERLINE;
371 set_screen_char_color(&schar_cache, format.style.bg, format.style.fg,
372 html_context->options->color_flags,
373 html_context->options->color_mode);
376 if (!!(schar_cache.attr & SCREEN_ATTR_UNSEARCHABLE)
377 ^ !!renderer_context.nosearchable) {
378 schar_cache.attr ^= SCREEN_ATTR_UNSEARCHABLE;
381 return &schar_cache;
384 #ifdef CONFIG_UTF8
385 /* First possibly do the format change and then find out what coordinates
386 * to use since sub- or superscript might change them */
387 static inline int
388 set_hline(struct html_context *html_context, unsigned char *chars, int charslen,
389 enum link_state link_state)
391 struct part *const part = html_context->part;
392 struct screen_char *const schar = get_format_screen_char(html_context,
393 link_state);
394 int x = part->cx;
395 const int y = part->cy;
396 const int x2 = x;
397 int len = charslen;
398 const int utf8 = html_context->options->utf8;
399 int orig_length;
401 assert(part);
402 if_assert_failed return len;
404 assert(charslen >= 0);
406 if (realloc_spaces(part, x + charslen))
407 return 0;
409 if (part->document) {
410 /* Reallocate LINE(y).chars[] to large enough. The
411 * last parameter of realloc_line is the index of the
412 * last element to which we may want to write,
413 * i.e. one less than the required size of the array.
414 * Compute the required size by assuming that each
415 * byte of input will need at most one character cell.
416 * (All double-cell characters take up at least two
417 * bytes in UTF-8, and there are no triple-cell or
418 * wider characters.) However, if there already is an
419 * incomplete character in part->document->buf, then
420 * the first byte of input can result in a double-cell
421 * character, so we must reserve one extra element. */
422 orig_length = realloc_line(html_context, part->document,
423 Y(y), X(x) + charslen);
424 if (orig_length < 0) /* error */
425 return 0;
426 if (utf8) {
427 unsigned char *end = chars + charslen;
428 unicode_val_T data;
430 if (part->document->buf_length) {
431 /* previous char was broken in the middle */
432 int length = utf8charlen(part->document->buf);
433 unsigned char i;
434 unsigned char *buf_ptr = part->document->buf;
436 for (i = part->document->buf_length; i < length && chars < end;) {
437 part->document->buf[i++] = *chars++;
439 part->document->buf_length = i;
440 part->document->buf[i] = '\0';
441 data = utf8_to_unicode(&buf_ptr, buf_ptr + i);
442 if (data != UCS_NO_CHAR) {
443 /* FIXME: If there was invalid
444 * UTF-8 in the buffer,
445 * @utf8_to_unicode may have left
446 * some bytes unused. Those
447 * bytes should be pulled back
448 * into @chars, rather than
449 * discarded. This is not
450 * trivial to implement because
451 * each byte may have arrived in
452 * a separate call. */
453 part->document->buf_length = 0;
454 goto good_char;
455 } else {
456 /* Still not full char */
457 LINE(y).length = orig_length;
458 return 0;
462 for (; chars < end; x++) {
463 /* ELinks does not use NBSP_CHAR in UTF-8. */
465 data = utf8_to_unicode(&chars, end);
466 if (data == UCS_NO_CHAR) {
467 part->spaces[x] = 0;
468 if (charslen == 1) {
469 /* HR */
470 unsigned char attr = schar->attr;
472 schar->data = *chars++;
473 schar->attr = SCREEN_ATTR_FRAME;
474 copy_screen_chars(&POS(x, y), schar, 1);
475 schar->attr = attr;
476 part->char_width[x] = 0;
477 continue;
478 } else {
479 unsigned char i;
481 for (i = 0; chars < end;i++) {
482 part->document->buf[i] = *chars++;
484 part->document->buf_length = i;
485 break;
487 } else {
488 good_char:
489 if (data == UCS_NO_BREAK_SPACE
490 && html_context->options->wrap_nbsp)
491 data = UCS_SPACE;
492 part->spaces[x] = (data == UCS_SPACE);
493 if (unicode_to_cell(data) == 2) {
494 schar->data = (unicode_val_T)data;
495 part->char_width[x] = 2;
496 copy_screen_chars(&POS(x++, y), schar, 1);
497 schar->data = UCS_NO_CHAR;
498 part->spaces[x] = 0;
499 part->char_width[x] = 0;
500 } else {
501 part->char_width[x] = unicode_to_cell(data);
502 schar->data = (unicode_val_T)data;
505 copy_screen_chars(&POS(x, y), schar, 1);
507 } else { /* not UTF-8 */
508 for (; charslen > 0; charslen--, x++, chars++) {
509 part->char_width[x] = 1;
510 if (*chars == NBSP_CHAR) {
511 schar->data = ' ';
512 part->spaces[x] = html_context->options->wrap_nbsp;
513 } else {
514 part->spaces[x] = (*chars == ' ');
515 schar->data = *chars;
517 copy_screen_chars(&POS(x, y), schar, 1);
519 } /* end of UTF-8 check */
521 /* Assert that we haven't written past the end of the
522 * LINE(y).chars array. @x here is one greater than
523 * the last one used in POS(x, y). Instead of this,
524 * we could assert(X(x) < LINE(y).length) immediately
525 * before each @copy_screen_chars call above, but
526 * those are in an inner loop that should be fast. */
527 assert(X(x) <= LINE(y).length);
528 /* Some part of the code is apparently using LINE(y).length
529 * for line-wrapping decisions. It may currently be too
530 * large because it was allocated above based on @charslen
531 * which is the number of bytes, not the number of cells.
532 * Change the length to the correct size, but don't let it
533 * get smaller than it was on entry to this function. */
534 LINE(y).length = int_max(orig_length, X(x));
535 len = x - x2;
536 } else { /* part->document == NULL */
537 if (utf8) {
538 unsigned char *end;
540 for (end = chars + charslen; chars < end; x++) {
541 unicode_val_T data;
543 part->spaces[x] = (*chars == ' ');
544 data = utf8_to_unicode(&chars, end);
545 part->char_width[x] = unicode_to_cell(data);
546 if (part->char_width[x] == 2) {
547 x++;
548 part->spaces[x] = 0;
549 part->char_width[x] = 0;
551 if (data == UCS_NO_CHAR) {
552 /* this is at the end only */
553 return x - x2;
556 len = x - x2;
557 } else { /* not UTF-8 */
558 for (; charslen > 0; charslen--, x++, chars++) {
559 part->spaces[x] = (*chars == ' ');
560 part->char_width[x] = 1;
563 } /* end of part->document check */
564 return len;
566 #else
568 /* First possibly do the format change and then find out what coordinates
569 * to use since sub- or superscript might change them */
570 static inline void
571 set_hline(struct html_context *html_context, unsigned char *chars, int charslen,
572 enum link_state link_state)
574 struct part *part = html_context->part;
575 struct screen_char *schar = get_format_screen_char(html_context,
576 link_state);
577 int x = part->cx;
578 int y = part->cy;
580 assert(part);
581 if_assert_failed return;
583 if (realloc_spaces(part, x + charslen))
584 return;
586 if (part->document) {
587 if (realloc_line(html_context, part->document,
588 Y(y), X(x) + charslen - 1) < 0)
589 return;
591 for (; charslen > 0; charslen--, x++, chars++) {
592 if (*chars == NBSP_CHAR) {
593 schar->data = ' ';
594 part->spaces[x] = html_context->options->wrap_nbsp;
595 } else {
596 part->spaces[x] = (*chars == ' ');
597 schar->data = *chars;
599 copy_screen_chars(&POS(x, y), schar, 1);
601 } else {
602 for (; charslen > 0; charslen--, x++, chars++) {
603 part->spaces[x] = (*chars == ' ');
607 #endif /* CONFIG_UTF8 */
609 static void
610 move_links(struct html_context *html_context, int xf, int yf, int xt, int yt)
612 struct part *part;
613 struct tag *tag;
614 int nlink = renderer_context.last_link_to_move;
615 int matched = 0;
617 assert(html_context);
618 if_assert_failed return;
620 part = html_context->part;
622 assert(part && part->document);
623 if_assert_failed return;
625 if (!realloc_lines(part->document, Y(yt)))
626 return;
628 for (; nlink < part->document->nlinks; nlink++) {
629 struct link *link = &part->document->links[nlink];
630 int i;
632 for (i = 0; i < link->npoints; i++) {
633 /* Fix for bug 479 (part one) */
634 /* The scenario that triggered it:
636 * Imagine a centered element containing a really long
637 * word (over half of the screen width long) followed
638 * by a few links with no spaces between them where all
639 * the link text combined with the really long word
640 * will force the line to be wrapped. When rendering
641 * the line first words (including link text words) are
642 * put on one line. Then wrapping is performed moving
643 * all links from current line to the one below. Then
644 * the current line (now only containing the really
645 * long word) is centered. This will trigger a call to
646 * move_links() which will increment.
648 * Without the fix below the centering of the current
649 * line will increment last_link_to_move to that of the
650 * last link which means centering of the next line
651 * with all the links will only move the last link
652 * leaving all the other links' points dangling and
653 * causing buggy link highlighting.
655 * Even links like textareas will be correctly handled
656 * because @last_link_to_move is a way to optimize how
657 * many links move_links() will have to iterate and
658 * this little fix will only decrease the effect of the
659 * optimization by always ensuring it is never
660 * incremented too far. */
661 if (!matched && link->points[i].y > Y(yf)) {
662 matched = 1;
663 continue;
666 if (link->points[i].y != Y(yf))
667 continue;
669 matched = 1;
671 if (link->points[i].x < X(xf))
672 continue;
674 if (yt >= 0) {
675 link->points[i].y = Y(yt);
676 link->points[i].x += -xf + xt;
677 } else {
678 int to_move = link->npoints - (i + 1);
680 assert(to_move >= 0);
682 if (to_move > 0) {
683 memmove(&link->points[i],
684 &link->points[i + 1],
685 to_move *
686 sizeof(*link->points));
687 i--;
690 link->npoints--;
694 if (!matched) {
695 renderer_context.last_link_to_move = nlink;
699 /* Don't move tags when removing links. */
700 if (yt < 0) return;
702 matched = 0;
703 tag = renderer_context.last_tag_to_move;
705 while (list_has_next(part->document->tags, tag)) {
706 tag = tag->next;
708 if (tag->y == Y(yf)) {
709 matched = 1;
710 if (tag->x >= X(xf)) {
711 tag->y = Y(yt);
712 tag->x += -xf + xt;
715 } else if (!matched && tag->y > Y(yf)) {
716 /* Fix for bug 479 (part two) */
717 matched = 1;
720 if (!matched) renderer_context.last_tag_to_move = tag;
724 static inline void
725 copy_chars(struct html_context *html_context, int x, int y, int width, struct screen_char *d)
727 struct part *part;
729 assert(html_context);
730 if_assert_failed return;
732 part = html_context->part;
734 assert(width > 0 && part && part->document && part->document->data);
735 if_assert_failed return;
737 if (realloc_line(html_context, part->document, Y(y), X(x) + width - 1) < 0)
738 return;
740 copy_screen_chars(&POS(x, y), d, width);
743 static inline void
744 move_chars(struct html_context *html_context, int x, int y, int nx, int ny)
746 struct part *part;
748 assert(html_context);
749 if_assert_failed return;
751 part = html_context->part;
753 assert(part && part->document && part->document->data);
754 if_assert_failed return;
756 if (LEN(y) - x <= 0) return;
757 copy_chars(html_context, nx, ny, LEN(y) - x, &POS(x, y));
759 LINE(y).length = X(x);
760 move_links(html_context, x, y, nx, ny);
763 static inline void
764 shift_chars(struct html_context *html_context, int y, int shift)
766 struct part *part;
767 struct screen_char *a;
768 int len;
770 assert(html_context);
771 if_assert_failed return;
773 part = html_context->part;
775 assert(part && part->document && part->document->data);
776 if_assert_failed return;
778 len = LEN(y);
780 a = fmem_alloc(len * sizeof(*a));
781 if (!a) return;
783 copy_screen_chars(a, &POS(0, y), len);
785 clear_hchars(html_context, 0, y, shift);
786 copy_chars(html_context, shift, y, len, a);
787 fmem_free(a);
789 move_links(html_context, 0, y, shift, y);
792 static inline void
793 del_chars(struct html_context *html_context, int x, int y)
795 struct part *part;
797 assert(html_context);
798 if_assert_failed return;
800 part = html_context->part;
802 assert(part && part->document && part->document->data);
803 if_assert_failed return;
805 LINE(y).length = X(x);
806 move_links(html_context, x, y, -1, -1);
809 #if TABLE_LINE_PADDING < 0
810 # define overlap_width(x) (x).width
811 #else
812 # define overlap_width(x) int_min((x).width, \
813 html_context->options->box.width - TABLE_LINE_PADDING)
814 #endif
815 #define overlap(x) int_max(overlap_width(x) - (x).rightmargin, 0)
817 static int inline
818 split_line_at(struct html_context *html_context, int width)
820 struct part *part;
821 int tmp;
822 int new_width = width + par_format.rightmargin;
824 assert(html_context);
825 if_assert_failed return 0;
827 part = html_context->part;
829 assert(part);
830 if_assert_failed return 0;
832 /* Make sure that we count the right margin to the total
833 * actual box width. */
834 int_lower_bound(&part->box.width, new_width);
836 if (part->document) {
837 assert(part->document->data);
838 if_assert_failed return 0;
839 #ifdef CONFIG_UTF8
840 if (html_context->options->utf8
841 && width < part->spaces_len && part->char_width[width] == 2) {
842 move_chars(html_context, width, part->cy, par_format.leftmargin, part->cy + 1);
843 del_chars(html_context, width, part->cy);
844 } else
845 #endif
847 assertm(POS(width, part->cy).data == ' ',
848 "bad split: %c", POS(width, part->cy).data);
849 move_chars(html_context, width + 1, part->cy, par_format.leftmargin, part->cy + 1);
850 del_chars(html_context, width, part->cy);
855 #ifdef CONFIG_UTF8
856 if (!(html_context->options->utf8
857 && width < part->spaces_len
858 && part->char_width[width] == 2))
859 #endif
860 width++; /* Since we were using (x + 1) only later... */
862 tmp = part->spaces_len - width;
863 if (tmp > 0) {
864 /* 0 is possible and I'm paranoid ... --Zas */
865 memmove(part->spaces, part->spaces + width, tmp);
866 #ifdef CONFIG_UTF8
867 memmove(part->char_width, part->char_width + width, tmp);
868 #endif
871 assert(tmp >= 0);
872 if_assert_failed tmp = 0;
873 memset(part->spaces + tmp, 0, width);
874 #ifdef CONFIG_UTF8
875 memset(part->char_width + tmp, 0, width);
876 #endif
878 if (par_format.leftmargin > 0) {
879 tmp = part->spaces_len - par_format.leftmargin;
880 assertm(tmp > 0, "part->spaces_len - par_format.leftmargin == %d", tmp);
881 /* So tmp is zero, memmove() should survive that. Don't recover. */
882 memmove(part->spaces + par_format.leftmargin, part->spaces, tmp);
883 #ifdef CONFIG_UTF8
884 memmove(part->char_width + par_format.leftmargin, part->char_width, tmp);
885 #endif
888 part->cy++;
890 if (part->cx == width) {
891 part->cx = -1;
892 int_lower_bound(&part->box.height, part->cy);
893 return 2;
894 } else {
895 part->cx -= width - par_format.leftmargin;
896 int_lower_bound(&part->box.height, part->cy + 1);
897 return 1;
901 /* Here, we scan the line for a possible place where we could split it into two
902 * (breaking it, because it is too long), if it is overlapping from the maximal
903 * box width. */
904 /* Returns 0 if there was found no spot suitable for breaking the line.
905 * 1 if the line was split into two.
906 * 2 if the (second) splitted line is blank (that is useful to determine
907 * ie. if the next line_break() should really break the line; we don't
908 * want to see any blank lines to pop up, do we?). */
909 static int
910 split_line(struct html_context *html_context)
912 struct part *part;
913 int x;
915 assert(html_context);
916 if_assert_failed return 0;
918 part = html_context->part;
920 assert(part);
921 if_assert_failed return 0;
923 #ifdef CONFIG_UTF8
924 if (html_context->options->utf8) {
925 for (x = overlap(par_format); x >= par_format.leftmargin; x--) {
927 if (x < part->spaces_len && (part->spaces[x]
928 || (part->char_width[x] == 2
929 /* Ugly hack. If we haven't place for
930 * double-width characters we print two
931 * double-width characters. */
932 && x != par_format.leftmargin)))
933 return split_line_at(html_context, x);
936 for (x = par_format.leftmargin; x < part->cx ; x++) {
937 if (x < part->spaces_len && (part->spaces[x]
938 || (part->char_width[x] == 2
939 /* We want to break line after _second_
940 * double-width character. */
941 && x > par_format.leftmargin)))
942 return split_line_at(html_context, x);
944 } else
945 #endif
947 for (x = overlap(par_format); x >= par_format.leftmargin; x--)
948 if (x < part->spaces_len && part->spaces[x])
949 return split_line_at(html_context, x);
951 for (x = par_format.leftmargin; x < part->cx ; x++)
952 if (x < part->spaces_len && part->spaces[x])
953 return split_line_at(html_context, x);
956 /* Make sure that we count the right margin to the total
957 * actual box width. */
958 int_lower_bound(&part->box.width, part->cx + par_format.rightmargin);
960 return 0;
963 /* Insert @new_spaces spaces before the coordinates @x and @y,
964 * adding those spaces to whatever link is at those coordinates. */
965 /* TODO: Integrate with move_links. */
966 static void
967 insert_spaces_in_link(struct part *part, int x, int y, int new_spaces)
969 int i = part->document->nlinks;
971 x = X(x);
972 y = Y(y);
974 while (i--) {
975 struct link *link = &part->document->links[i];
976 int j = link->npoints;
978 while (j-- > 1) {
979 struct point *point = &link->points[j];
981 if (point->x != x || point->y != y)
982 continue;
984 if (!realloc_points(link, link->npoints + new_spaces))
985 return;
987 link->npoints += new_spaces;
988 point = &link->points[link->npoints - 1];
990 while (new_spaces--) {
991 point->x = --x;
992 point->y = y;
993 point--;
996 return;
1001 /* This function is very rare exemplary of clean and beautyful code here.
1002 * Please handle with care. --pasky */
1003 static void
1004 justify_line(struct html_context *html_context, int y)
1006 struct part *part;
1007 struct screen_char *line; /* we save original line here */
1008 int len;
1009 int pos;
1010 int *space_list;
1011 int spaces;
1012 int diff;
1014 assert(html_context);
1015 if_assert_failed return;
1017 part = html_context->part;
1019 assert(part && part->document && part->document->data);
1020 if_assert_failed return;
1022 len = LEN(y);
1023 assert(len > 0);
1024 if_assert_failed return;
1026 line = fmem_alloc(len * sizeof(*line));
1027 if (!line) return;
1029 /* It may sometimes happen that the line is only one char long and that
1030 * char is space - then we're going to write to both [0] and [1], but
1031 * we allocated only one field. Thus, we've to do (len + 1). --pasky */
1032 space_list = fmem_alloc((len + 1) * sizeof(*space_list));
1033 if (!space_list) {
1034 fmem_free(line);
1035 return;
1038 copy_screen_chars(line, &POS(0, y), len);
1040 /* Skip leading spaces */
1042 spaces = 0;
1043 pos = 0;
1045 while (line[pos].data == ' ')
1046 pos++;
1048 /* Yes, this can be negative, we know. But we add one to it always
1049 * anyway, so it's ok. */
1050 space_list[spaces++] = pos - 1;
1052 /* Count spaces */
1054 for (; pos < len; pos++)
1055 if (line[pos].data == ' ')
1056 space_list[spaces++] = pos;
1058 space_list[spaces] = len;
1060 /* Realign line */
1062 /* Diff is the difference between the width of the paragraph
1063 * and the current length of the line. */
1064 diff = overlap(par_format) - len;
1066 /* We check diff > 0 because diff can be negative (i.e., we have
1067 * an unbroken line of length > overlap(par_format))
1068 * even when spaces > 1 if the line has only non-breaking spaces. */
1069 if (spaces > 1 && diff > 0) {
1070 int prev_end = 0;
1071 int word;
1073 clear_hchars(html_context, 0, y, overlap(par_format));
1075 for (word = 0; word < spaces; word++) {
1076 /* We have to increase line length by 'diff' num. of
1077 * characters, so we move 'word'th word 'word_shift'
1078 * characters right. */
1079 int word_start = space_list[word] + 1;
1080 int word_len = space_list[word + 1] - word_start;
1081 int word_shift;
1082 int new_start;
1083 int new_spaces;
1085 assert(word_len >= 0);
1086 if_assert_failed continue;
1087 if (!word_len) continue;
1089 word_shift = (word * diff) / (spaces - 1);
1090 new_start = word_start + word_shift;
1092 copy_chars(html_context, new_start, y, word_len,
1093 &line[word_start]);
1095 new_spaces = new_start - prev_end - 1;
1096 if (word && new_spaces) {
1097 move_links(html_context, prev_end + 1, y, new_start, y);
1098 insert_spaces_in_link(part,
1099 new_start, y, new_spaces);
1102 prev_end = new_start + word_len;
1106 fmem_free(space_list);
1107 fmem_free(line);
1110 static void
1111 align_line(struct html_context *html_context, int y, int last)
1113 struct part *part;
1114 int shift;
1115 int len;
1117 assert(html_context);
1118 if_assert_failed return;
1120 part = html_context->part;
1122 assert(part && part->document && part->document->data);
1123 if_assert_failed return;
1125 len = LEN(y);
1127 if (!len || par_format.align == ALIGN_LEFT)
1128 return;
1130 if (par_format.align == ALIGN_JUSTIFY) {
1131 if (!last)
1132 justify_line(html_context, y);
1133 return;
1136 shift = overlap(par_format) - len;
1137 if (par_format.align == ALIGN_CENTER)
1138 shift /= 2;
1139 if (shift > 0)
1140 shift_chars(html_context, y, shift);
1143 static inline void
1144 init_link_event_hooks(struct html_context *html_context, struct link *link)
1146 link->event_hooks = mem_calloc(1, sizeof(*link->event_hooks));
1147 if (!link->event_hooks) return;
1149 #define add_evhook(list_, type_, src_) \
1150 do { \
1151 struct script_event_hook *evhook; \
1153 if (!src_) break; \
1155 evhook = mem_calloc(1, sizeof(*evhook)); \
1156 if (!evhook) break; \
1158 evhook->type = type_; \
1159 evhook->src = stracpy(src_); \
1160 add_to_list(*(list_), evhook); \
1161 } while (0)
1163 init_list(*link->event_hooks);
1164 add_evhook(link->event_hooks, SEVHOOK_ONCLICK, format.onclick);
1165 add_evhook(link->event_hooks, SEVHOOK_ONDBLCLICK, format.ondblclick);
1166 add_evhook(link->event_hooks, SEVHOOK_ONMOUSEOVER, format.onmouseover);
1167 add_evhook(link->event_hooks, SEVHOOK_ONHOVER, format.onhover);
1168 add_evhook(link->event_hooks, SEVHOOK_ONFOCUS, format.onfocus);
1169 add_evhook(link->event_hooks, SEVHOOK_ONMOUSEOUT, format.onmouseout);
1170 add_evhook(link->event_hooks, SEVHOOK_ONBLUR, format.onblur);
1172 #undef add_evhook
1175 static struct link *
1176 new_link(struct html_context *html_context, unsigned char *name, int namelen)
1178 struct document *document;
1179 struct part *part;
1180 int link_number;
1181 struct link *link;
1183 assert(html_context);
1184 if_assert_failed return NULL;
1186 part = html_context->part;
1188 assert(part);
1189 if_assert_failed return NULL;
1191 document = part->document;
1193 assert(document);
1194 if_assert_failed return NULL;
1196 link_number = part->link_num;
1198 if (!ALIGN_LINK(&document->links, document->nlinks, document->nlinks + 1))
1199 return NULL;
1201 link = &document->links[document->nlinks++];
1202 link->number = link_number - 1;
1203 if (document->options.use_tabindex) link->number += format.tabindex;
1204 link->accesskey = format.accesskey;
1205 link->title = null_or_stracpy(format.title);
1206 link->where_img = null_or_stracpy(format.image);
1208 if (!format.form) {
1209 link->target = null_or_stracpy(format.target);
1210 link->data.name = memacpy(name, namelen);
1211 /* if (strlen(url) > 4 && !strncasecmp(url, "MAP@", 4)) { */
1212 if (format.link
1213 && ((format.link[0]|32) == 'm')
1214 && ((format.link[1]|32) == 'a')
1215 && ((format.link[2]|32) == 'p')
1216 && (format.link[3] == '@')
1217 && format.link[4]) {
1218 link->type = LINK_MAP;
1219 link->where = stracpy(format.link + 4);
1220 } else {
1221 link->type = LINK_HYPERTEXT;
1222 link->where = null_or_stracpy(format.link);
1225 } else {
1226 struct form_control *fc = format.form;
1227 struct form *form;
1229 switch (fc->type) {
1230 case FC_TEXT:
1231 case FC_PASSWORD:
1232 case FC_FILE:
1233 link->type = LINK_FIELD;
1234 break;
1235 case FC_TEXTAREA:
1236 link->type = LINK_AREA;
1237 break;
1238 case FC_CHECKBOX:
1239 case FC_RADIO:
1240 link->type = LINK_CHECKBOX;
1241 break;
1242 case FC_SELECT:
1243 link->type = LINK_SELECT;
1244 break;
1245 case FC_SUBMIT:
1246 case FC_IMAGE:
1247 case FC_RESET:
1248 case FC_BUTTON:
1249 case FC_HIDDEN:
1250 link->type = LINK_BUTTON;
1252 link->data.form_control = fc;
1253 /* At this point, format.form might already be set but
1254 * the form_control not registered through SP_CONTROL
1255 * yet, therefore without fc->form set. It is always
1256 * after the "good" last form was already processed,
1257 * though, so we can safely just take that. */
1258 form = fc->form;
1259 if (!form && !list_empty(document->forms))
1260 form = document->forms.next;
1261 link->target = null_or_stracpy(form ? form->target : NULL);
1264 link->color.background = format.style.bg;
1265 link->color.foreground = link_is_textinput(link)
1266 ? format.style.fg : format.clink;
1268 init_link_event_hooks(html_context, link);
1270 document->links_sorted = 0;
1271 return link;
1274 static void
1275 html_special_tag(struct document *document, unsigned char *t, int x, int y)
1277 struct tag *tag;
1278 int tag_len;
1280 assert(document);
1281 if_assert_failed return;
1283 tag_len = strlen(t);
1284 /* One byte is reserved for name in struct tag. */
1285 tag = mem_alloc(sizeof(*tag) + tag_len);
1286 if (!tag) return;
1288 tag->x = x;
1289 tag->y = y;
1290 memcpy(tag->name, t, tag_len + 1);
1291 add_to_list(document->tags, tag);
1292 if (renderer_context.last_tag_for_newline == (struct tag *) &document->tags)
1293 renderer_context.last_tag_for_newline = tag;
1297 static void
1298 put_chars_conv(struct html_context *html_context,
1299 unsigned char *chars, int charslen)
1301 struct part *part;
1303 assert(html_context);
1304 if_assert_failed return;
1306 part = html_context->part;
1308 assert(part && chars && charslen);
1309 if_assert_failed return;
1311 if (format.style.attr & AT_GRAPHICS) {
1312 put_chars(html_context, chars, charslen);
1313 return;
1316 convert_string(renderer_context.convert_table, chars, charslen,
1317 html_context->options->cp,
1318 CSM_DEFAULT, NULL, (void (*)(void *, unsigned char *, int)) put_chars, html_context);
1321 static inline void
1322 put_link_number(struct html_context *html_context)
1324 struct part *part = html_context->part;
1325 unsigned char s[64];
1326 unsigned char *fl = format.link;
1327 unsigned char *ft = format.target;
1328 unsigned char *fi = format.image;
1329 struct form_control *ff = format.form;
1330 int slen = 0;
1332 format.link = format.target = format.image = NULL;
1333 format.form = NULL;
1335 s[slen++] = '[';
1336 ulongcat(s, &slen, part->link_num, sizeof(s) - 3, 0);
1337 s[slen++] = ']';
1338 s[slen] = '\0';
1340 renderer_context.nosearchable = 1;
1341 put_chars(html_context, s, slen);
1342 renderer_context.nosearchable = 0;
1344 if (ff && ff->type == FC_TEXTAREA) line_break(html_context);
1346 /* We might have ended up on a new line after the line breaking
1347 * or putting the link number chars. */
1348 if (part->cx == -1) part->cx = par_format.leftmargin;
1350 format.link = fl;
1351 format.target = ft;
1352 format.image = fi;
1353 format.form = ff;
1356 #define assert_link_variable(old, new) \
1357 assertm(!(old), "Old link value [%s]. New value [%s]", old, new);
1359 static inline void
1360 init_link_state_info(unsigned char *link, unsigned char *target,
1361 unsigned char *image, struct form_control *form)
1363 assert_link_variable(renderer_context.link_state_info.image, image);
1364 assert_link_variable(renderer_context.link_state_info.target, target);
1365 assert_link_variable(renderer_context.link_state_info.link, link);
1367 renderer_context.link_state_info.link = null_or_stracpy(link);
1368 renderer_context.link_state_info.target = null_or_stracpy(target);
1369 renderer_context.link_state_info.image = null_or_stracpy(image);
1370 renderer_context.link_state_info.form = form;
1373 static inline void
1374 done_link_state_info(void)
1376 mem_free_if(renderer_context.link_state_info.link);
1377 mem_free_if(renderer_context.link_state_info.target);
1378 mem_free_if(renderer_context.link_state_info.image);
1379 memset(&renderer_context.link_state_info, 0,
1380 sizeof(renderer_context.link_state_info));
1383 #ifdef CONFIG_UTF8
1384 static inline void
1385 process_link(struct html_context *html_context, enum link_state link_state,
1386 unsigned char *chars, int charslen, int cells)
1387 #else
1388 static inline void
1389 process_link(struct html_context *html_context, enum link_state link_state,
1390 unsigned char *chars, int charslen)
1391 #endif /* CONFIG_UTF8 */
1393 struct part *part = html_context->part;
1394 struct link *link;
1395 int x_offset = 0;
1397 switch (link_state) {
1398 case LINK_STATE_SAME: {
1399 unsigned char *name;
1401 if (!part->document) return;
1403 assertm(part->document->nlinks > 0, "no link");
1404 if_assert_failed return;
1406 link = &part->document->links[part->document->nlinks - 1];
1408 name = get_link_name(link);
1409 if (name) {
1410 unsigned char *new_name;
1412 new_name = straconcat(name, chars, NULL);
1413 if (new_name) {
1414 mem_free(name);
1415 link->data.name = new_name;
1419 /* FIXME: Concatenating two adjectent <a> elements to a single
1420 * link is broken since we lose the event handlers for the
1421 * second one. OTOH simply appending them here won't fly since
1422 * we may get here multiple times for even a single link. We
1423 * will probably need some SP_ for creating a new link or so.
1424 * --pasky */
1426 break;
1429 case LINK_STATE_NEW:
1430 part->link_num++;
1432 init_link_state_info(format.link, format.target,
1433 format.image, format.form);
1434 if (!part->document) return;
1436 /* Trim leading space from the link text */
1437 while (x_offset < charslen && chars[x_offset] <= ' ')
1438 x_offset++;
1440 if (x_offset) {
1441 charslen -= x_offset;
1442 chars += x_offset;
1443 #ifdef CONFIG_UTF8
1444 cells -= x_offset;
1445 #endif /* CONFIG_UTF8 */
1448 link = new_link(html_context, chars, charslen);
1449 if (!link) return;
1451 break;
1453 case LINK_STATE_NONE:
1454 default:
1455 INTERNAL("bad link_state %i", (int) link_state);
1456 return;
1459 /* Add new canvas positions to the link. */
1460 #ifdef CONFIG_UTF8
1461 if (realloc_points(link, link->npoints + cells))
1462 #else
1463 if (realloc_points(link, link->npoints + charslen))
1464 #endif /* CONFIG_UTF8 */
1466 struct point *point = &link->points[link->npoints];
1467 int x = X(part->cx) + x_offset;
1468 int y = Y(part->cy);
1470 #ifdef CONFIG_UTF8
1471 link->npoints += cells;
1473 for (; cells > 0; cells--, point++, x++)
1474 #else
1475 link->npoints += charslen;
1477 for (; charslen > 0; charslen--, point++, x++)
1478 #endif /* CONFIG_UTF8 */
1480 point->x = x;
1481 point->y = y;
1486 static inline enum link_state
1487 get_link_state(struct html_context *html_context)
1489 enum link_state state;
1491 if (!(format.link || format.image || format.form)) {
1492 state = LINK_STATE_NONE;
1494 } else if ((renderer_context.link_state_info.link
1495 || renderer_context.link_state_info.image
1496 || renderer_context.link_state_info.form)
1497 && !xstrcmp(format.link, renderer_context.link_state_info.link)
1498 && !xstrcmp(format.target, renderer_context.link_state_info.target)
1499 && !xstrcmp(format.image, renderer_context.link_state_info.image)
1500 && format.form == renderer_context.link_state_info.form) {
1502 return LINK_STATE_SAME;
1504 } else {
1505 state = LINK_STATE_NEW;
1508 done_link_state_info();
1510 return state;
1513 static inline int
1514 html_has_non_space_chars(unsigned char *chars, int charslen)
1516 int pos = 0;
1518 while (pos < charslen)
1519 if (!isspace(chars[pos++]))
1520 return 1;
1522 return 0;
1525 static void
1526 put_chars(struct html_context *html_context, unsigned char *chars, int charslen)
1528 enum link_state link_state;
1529 struct part *part;
1530 #ifdef CONFIG_UTF8
1531 int cells;
1532 #endif /* CONFIG_UTF8 */
1534 assert(html_context);
1535 if_assert_failed return;
1537 part = html_context->part;
1539 assert(part);
1540 if_assert_failed return;
1542 assert(chars && charslen);
1543 if_assert_failed return;
1545 /* If we are not handling verbatim aligning and we are at the begining
1546 * of a line trim whitespace. */
1547 if (part->cx == -1) {
1548 /* If we are not handling verbatim aligning trim leading
1549 * whitespaces. */
1550 if (!html_is_preformatted()) {
1551 while (charslen && *chars == ' ') {
1552 chars++;
1553 charslen--;
1556 if (charslen < 1) return;
1559 part->cx = par_format.leftmargin;
1562 /* For preformatted html always update 'the last tag' so we never end
1563 * up moving tags to the wrong line (Fixes bug 324). For all other html
1564 * it is moved only when the line being rendered carry some real
1565 * non-whitespace content. */
1566 if (html_is_preformatted()
1567 || html_has_non_space_chars(chars, charslen)) {
1568 renderer_context.last_tag_for_newline = (struct tag *) &part->document->tags;
1571 int_lower_bound(&part->box.height, part->cy + 1);
1573 link_state = get_link_state(html_context);
1575 if (link_state == LINK_STATE_NEW) {
1576 int x_offset = 0;
1578 /* Don't add inaccessible links. It seems to be caused
1579 * by the parser putting a space char after stuff like
1580 * <img>-tags or comments wrapped in <a>-tags. See bug
1581 * 30 for test case. */
1582 while (x_offset < charslen && chars[x_offset] <= ' ')
1583 x_offset++;
1585 /* For pure spaces reset the link state */
1586 if (x_offset == charslen)
1587 link_state = LINK_STATE_NONE;
1588 else if (html_context->options->links_numbering)
1589 put_link_number(html_context);
1591 #ifdef CONFIG_UTF8
1592 cells =
1593 #endif /* CONFIG_UTF8 */
1594 set_hline(html_context, chars, charslen, link_state);
1596 if (link_state != LINK_STATE_NONE) {
1597 #ifdef CONFIG_UTF8
1598 process_link(html_context, link_state, chars, charslen,
1599 cells);
1600 #else
1601 process_link(html_context, link_state, chars, charslen);
1602 #endif /* CONFIG_UTF8 */
1605 #ifdef CONFIG_UTF8
1606 if (renderer_context.nowrap
1607 && part->cx + cells > overlap(par_format))
1608 return;
1610 part->cx += cells;
1611 #else
1612 if (renderer_context.nowrap
1613 && part->cx + charslen > overlap(par_format))
1614 return;
1616 part->cx += charslen;
1617 #endif /* CONFIG_UTF8 */
1619 renderer_context.nobreak = 0;
1621 if (!(html_context->options->wrap || html_is_preformatted())) {
1622 while (part->cx > overlap(par_format)
1623 && part->cx > par_format.leftmargin) {
1624 int x = split_line(html_context);
1626 if (!x) break;
1627 if (part->document)
1628 align_line(html_context, part->cy - 1, 0);
1629 renderer_context.nobreak = !!(x - 1);
1633 assert(charslen > 0);
1634 #ifdef CONFIG_UTF8
1635 part->xa += cells;
1636 #else
1637 part->xa += charslen;
1638 #endif /* CONFIG_UTF8 */
1639 int_lower_bound(&part->max_width, part->xa
1640 + par_format.leftmargin + par_format.rightmargin
1641 - (chars[charslen - 1] == ' '
1642 && !html_is_preformatted()));
1643 return;
1647 #undef overlap
1649 static void
1650 line_break(struct html_context *html_context)
1652 struct part *part;
1653 struct tag *tag;
1655 assert(html_context);
1656 if_assert_failed return;
1658 part = html_context->part;
1660 assert(part);
1661 if_assert_failed return;
1663 int_lower_bound(&part->box.width, part->cx + par_format.rightmargin);
1665 if (renderer_context.nobreak) {
1666 renderer_context.nobreak = 0;
1667 part->cx = -1;
1668 part->xa = 0;
1669 return;
1672 if (!part->document || !part->document->data) goto end;
1674 if (!realloc_lines(part->document, part->box.height + part->cy + 1))
1675 return;
1677 if (part->cx > par_format.leftmargin && LEN(part->cy) > part->cx - 1
1678 && POS(part->cx - 1, part->cy).data == ' ') {
1679 del_chars(html_context, part->cx - 1, part->cy);
1680 part->cx--;
1683 if (part->cx > 0) align_line(html_context, part->cy, 1);
1685 for (tag = renderer_context.last_tag_for_newline;
1686 tag && tag != (struct tag *) &part->document->tags;
1687 tag = tag->prev) {
1688 tag->x = X(0);
1689 tag->y = Y(part->cy + 1);
1692 end:
1693 part->cy++;
1694 part->cx = -1;
1695 part->xa = 0;
1696 memset(part->spaces, 0, part->spaces_len);
1697 #ifdef CONFIG_UTF8
1698 memset(part->char_width, 0, part->spaces_len);
1699 #endif
1702 static void
1703 html_special_form(struct part *part, struct form *form)
1705 assert(part && form);
1706 if_assert_failed return;
1708 if (!part->document) {
1709 done_form(form);
1710 return;
1713 if (!list_empty(part->document->forms)) {
1714 struct form *nform;
1716 /* Make sure the new form ``claims'' its slice of the form range
1717 * maintained in the form_num and form_end variables. */
1718 foreach (nform, part->document->forms) {
1719 if (form->form_num < nform->form_num
1720 || nform->form_end < form->form_num)
1721 continue;
1723 /* First check if the form has identical form numbers.
1724 * That should only be the case when the form being
1725 * added is in fact the same form in which case it
1726 * should be dropped. The fact that this can happen
1727 * suggests that the table renderering can be confused.
1728 * See bug 647 for a test case. */
1729 if (nform->form_num == form->form_num
1730 && nform->form_end == form->form_end) {
1731 done_form(form);
1732 return;
1735 /* The form start is inside an already added form, so
1736 * partition the space of the existing form and get
1737 * |old|new|. */
1738 nform->form_end = form->form_num - 1;
1739 assertm(nform->form_num <= nform->form_end,
1740 "[%d:%d] [%d:%d]", nform->form_num, nform->form_end,
1741 form->form_num, form->form_end);
1742 break;
1744 } else {
1745 /* If it is the first form make sure it eats the whole form
1746 * range. */
1747 #if 0
1748 /* Disabled because in tables the parse order may lead to a
1749 * later form being parsed before a preceeding one causing the
1750 * wrong order if we set it to zero. Let's hope it doesn't break
1751 * anything else. */
1752 form->form_num = 0;
1753 #endif
1756 add_to_list(part->document->forms, form);
1759 static void
1760 html_special_form_control(struct part *part, struct form_control *fc)
1762 struct form *form;
1764 assert(part && fc);
1765 if_assert_failed return;
1767 if (!part->document) {
1768 done_form_control(fc);
1769 mem_free(fc);
1770 return;
1773 fc->g_ctrl_num = renderer_context.g_ctrl_num++;
1775 /* We don't want to recode hidden fields. */
1776 if (fc->type == FC_TEXT || fc->type == FC_PASSWORD ||
1777 fc->type == FC_TEXTAREA) {
1778 unsigned char *dv = convert_string(renderer_context.convert_table,
1779 fc->default_value,
1780 strlen(fc->default_value),
1781 part->document->options.cp,
1782 CSM_QUERY, NULL, NULL, NULL);
1784 if (dv) mem_free_set(&fc->default_value, dv);
1787 if (list_empty(part->document->forms)) {
1788 /* No forms encountered yet, that means a homeless form
1789 * control. Generate a dummy form for those Flying
1790 * Dutchmans. */
1791 form = init_form();
1792 form->form_num = 0;
1793 add_to_list(part->document->forms, form);
1795 /* Attach this form control to the last form encountered. */
1796 form = part->document->forms.next;
1797 fc->form = form;
1798 add_to_list(form->items, fc);
1801 /* Reparents form items based on position in the source. */
1802 void
1803 check_html_form_hierarchy(struct part *part)
1805 struct document *document = part->document;
1806 INIT_LIST_HEAD(form_controls);
1807 struct form *form;
1808 struct form_control *fc, *next;
1810 if (list_empty(document->forms))
1811 return;
1813 /* Take out all badly placed form items. */
1815 foreach (form, document->forms) {
1817 assertm(form->form_num <= form->form_end,
1818 "%p [%d : %d]", form, form->form_num, form->form_end);
1820 foreachsafe (fc, next, form->items) {
1821 if (form->form_num <= fc->position
1822 && fc->position <= form->form_end)
1823 continue;
1825 move_to_top_of_list(form_controls, fc);
1829 /* Re-insert the form items the correct places. */
1831 foreachsafe (fc, next, form_controls) {
1833 foreach (form, document->forms) {
1834 if (fc->position < form->form_num
1835 || form->form_end < fc->position)
1836 continue;
1838 fc->form = form;
1839 move_to_top_of_list(form->items, fc);
1840 break;
1844 assert(list_empty(form_controls));
1847 static inline void
1848 color_link_lines(struct html_context *html_context)
1850 struct document *document = html_context->part->document;
1851 struct color_pair colors = INIT_COLOR_PAIR(par_format.bgcolor, 0x0);
1852 enum color_mode color_mode = document->options.color_mode;
1853 enum color_flags color_flags = document->options.color_flags;
1854 int y;
1856 for (y = 0; y < document->height; y++) {
1857 int x;
1859 for (x = 0; x < document->data[y].length; x++) {
1860 struct screen_char *schar = &document->data[y].chars[x];
1862 set_term_color(schar, &colors, color_flags, color_mode);
1864 /* XXX: Entering hack zone! Change to clink color after
1865 * link text has been recolored. */
1866 if (schar->data == ':' && colors.foreground == 0x0)
1867 colors.foreground = format.clink;
1870 colors.foreground = 0x0;
1874 static void *
1875 html_special(struct html_context *html_context, enum html_special_type c, ...)
1877 va_list l;
1878 struct part *part;
1879 struct document *document;
1880 void *ret_val = NULL;
1882 assert(html_context);
1883 if_assert_failed return NULL;
1885 part = html_context->part;
1887 assert(part);
1888 if_assert_failed return NULL;
1890 document = part->document;
1892 va_start(l, c);
1893 switch (c) {
1894 case SP_TAG:
1895 if (document) {
1896 unsigned char *t = va_arg(l, unsigned char *);
1898 html_special_tag(document, t, X(part->cx), Y(part->cy));
1900 break;
1901 case SP_FORM:
1903 struct form *form = va_arg(l, struct form *);
1905 html_special_form(part, form);
1906 break;
1908 case SP_CONTROL:
1910 struct form_control *fc = va_arg(l, struct form_control *);
1912 html_special_form_control(part, fc);
1913 break;
1915 case SP_TABLE:
1916 ret_val = renderer_context.convert_table;
1917 break;
1918 case SP_USED:
1919 ret_val = (void *) (long) !!document;
1920 break;
1921 case SP_CACHE_CONTROL:
1923 struct cache_entry *cached = renderer_context.cached;
1925 cached->cache_mode = CACHE_MODE_NEVER;
1926 cached->expire = 0;
1927 break;
1929 case SP_CACHE_EXPIRES:
1931 time_t expires = va_arg(l, time_t);
1932 struct cache_entry *cached = renderer_context.cached;
1934 if (!expires || cached->cache_mode == CACHE_MODE_NEVER)
1935 break;
1937 timeval_from_seconds(&cached->max_age, expires);
1938 cached->expire = 1;
1939 break;
1941 case SP_FRAMESET:
1943 struct frameset_param *fsp = va_arg(l, struct frameset_param *);
1944 struct frameset_desc *frameset_desc;
1946 if (!fsp->parent && document->frame_desc)
1947 break;
1949 frameset_desc = create_frameset(fsp);
1950 if (!fsp->parent && !document->frame_desc)
1951 document->frame_desc = frameset_desc;
1953 ret_val = frameset_desc;
1954 break;
1956 case SP_FRAME:
1958 struct frameset_desc *parent = va_arg(l, struct frameset_desc *);
1959 unsigned char *name = va_arg(l, unsigned char *);
1960 unsigned char *url = va_arg(l, unsigned char *);
1962 add_frameset_entry(parent, NULL, name, url);
1963 break;
1965 case SP_NOWRAP:
1966 renderer_context.nowrap = !!va_arg(l, int);
1967 break;
1968 case SP_REFRESH:
1970 unsigned long seconds = va_arg(l, unsigned long);
1971 unsigned char *t = va_arg(l, unsigned char *);
1973 if (document) {
1974 if (document->refresh)
1975 done_document_refresh(document->refresh);
1976 document->refresh = init_document_refresh(t, seconds);
1978 break;
1980 case SP_COLOR_LINK_LINES:
1981 if (document && use_document_bg_colors(&document->options))
1982 color_link_lines(html_context);
1983 break;
1984 case SP_STYLESHEET:
1985 #ifdef CONFIG_CSS
1986 if (document) {
1987 struct uri *uri = va_arg(l, struct uri *);
1989 add_to_uri_list(&document->css_imports, uri);
1991 #endif
1992 break;
1993 case SP_SCRIPT:
1994 #ifdef CONFIG_ECMASCRIPT
1995 if (document) {
1996 struct uri *uri = va_arg(l, struct uri *);
1998 add_to_uri_list(&document->ecmascript_imports, uri);
2000 #endif
2001 break;
2004 va_end(l);
2006 return ret_val;
2009 void
2010 free_table_cache(void)
2012 if (table_cache) {
2013 struct hash_item *item;
2014 int i;
2016 /* We do not free key here. */
2017 foreach_hash_item (item, *table_cache, i) {
2018 mem_free_if(item->value);
2021 free_hash(&table_cache);
2022 table_cache_entries = 0;
2026 struct part *
2027 format_html_part(struct html_context *html_context,
2028 unsigned char *start, unsigned char *end,
2029 int align, int margin, int width, struct document *document,
2030 int x, int y, unsigned char *head,
2031 int link_num)
2033 struct part *part;
2034 struct html_element *html_state;
2035 int llm = renderer_context.last_link_to_move;
2036 struct tag *ltm = renderer_context.last_tag_to_move;
2037 int ef = renderer_context.empty_format;
2038 int lm = html_context->margin;
2040 /* Hash creation if needed. */
2041 if (!table_cache) {
2042 table_cache = init_hash8();
2043 } else if (!document) {
2044 /* Search for cached entry. */
2045 struct table_cache_entry_key key;
2046 struct hash_item *item;
2048 /* Clear key to prevent potential alignment problem
2049 * when keys are compared. */
2050 memset(&key, 0, sizeof(key));
2052 key.start = start;
2053 key.end = end;
2054 key.align = align;
2055 key.margin = margin;
2056 key.width = width;
2057 key.x = x;
2058 key.link_num = link_num;
2060 item = get_hash_item(table_cache,
2061 (unsigned char *) &key,
2062 sizeof(key));
2063 if (item) { /* We found it in cache, so just copy and return. */
2064 part = mem_alloc(sizeof(*part));
2065 if (part) {
2066 copy_struct(part, &((struct table_cache_entry *)
2067 item->value)->part);
2068 return part;
2073 assertm(y >= 0, "format_html_part: y == %d", y);
2074 if_assert_failed return NULL;
2076 if (document) {
2077 struct node *node = mem_alloc(sizeof(*node));
2079 if (node) {
2080 int node_width = !html_context->table_level ? INT_MAX : width;
2082 set_box(&node->box, x, y, node_width, 1);
2083 add_to_list(document->nodes, node);
2086 renderer_context.last_link_to_move = document->nlinks;
2087 renderer_context.last_tag_to_move = (struct tag *) &document->tags;
2088 renderer_context.last_tag_for_newline = (struct tag *) &document->tags;
2089 } else {
2090 renderer_context.last_link_to_move = 0;
2091 renderer_context.last_tag_to_move = (struct tag *) NULL;
2092 renderer_context.last_tag_for_newline = (struct tag *) NULL;
2095 html_context->margin = margin;
2096 renderer_context.empty_format = !document;
2098 done_link_state_info();
2099 renderer_context.nobreak = 1;
2101 part = mem_calloc(1, sizeof(*part));
2102 if (!part) goto ret;
2104 part->document = document;
2105 part->box.x = x;
2106 part->box.y = y;
2107 part->cx = -1;
2108 part->cy = 0;
2109 part->link_num = link_num;
2111 html_state = init_html_parser_state(html_context, ELEMENT_IMMORTAL, align, margin, width);
2113 parse_html(start, end, part, head, html_context);
2115 done_html_parser_state(html_context, html_state);
2117 int_lower_bound(&part->max_width, part->box.width);
2119 renderer_context.nobreak = 0;
2121 done_link_state_info();
2122 mem_free_if(part->spaces);
2123 #ifdef CONFIG_UTF8
2124 mem_free_if(part->char_width);
2125 #endif
2127 if (document) {
2128 struct node *node = document->nodes.next;
2130 node->box.height = y - node->box.y + part->box.height;
2133 ret:
2134 renderer_context.last_link_to_move = llm;
2135 renderer_context.last_tag_to_move = ltm;
2136 renderer_context.empty_format = ef;
2138 html_context->margin = lm;
2140 if (html_context->table_level > 1 && !document
2141 && table_cache
2142 && table_cache_entries < MAX_TABLE_CACHE_ENTRIES) {
2143 /* Create a new entry. */
2144 /* Clear memory to prevent bad key comparaison due to alignment
2145 * of key fields. */
2146 struct table_cache_entry *tce = mem_calloc(1, sizeof(*tce));
2147 /* A goto is used here to prevent a test or code
2148 * redundancy. */
2149 if (!tce) goto end;
2151 tce->key.start = start;
2152 tce->key.end = end;
2153 tce->key.align = align;
2154 tce->key.margin = margin;
2155 tce->key.width = width;
2156 tce->key.x = x;
2157 tce->key.link_num = link_num;
2158 copy_struct(&tce->part, part);
2160 if (!add_hash_item(table_cache,
2161 (unsigned char *) &tce->key,
2162 sizeof(tce->key), tce)) {
2163 mem_free(tce);
2164 } else {
2165 table_cache_entries++;
2169 end:
2171 return part;
2174 void
2175 render_html_document(struct cache_entry *cached, struct document *document,
2176 struct string *buffer)
2178 struct html_context *html_context;
2179 struct part *part;
2180 unsigned char *start;
2181 unsigned char *end;
2182 struct string title;
2183 struct string head;
2185 assert(cached && document);
2186 if_assert_failed return;
2188 if (!init_string(&head)) return;
2190 if (cached->head) add_to_string(&head, cached->head);
2192 start = buffer->source;
2193 end = buffer->source + buffer->length;
2195 html_context = init_html_parser(cached->uri, &document->options,
2196 start, end, &head, &title,
2197 put_chars_conv, line_break,
2198 html_special);
2199 if (!html_context) return;
2201 renderer_context.g_ctrl_num = 0;
2202 renderer_context.cached = cached;
2203 renderer_context.convert_table = get_convert_table(head.source,
2204 document->options.cp,
2205 document->options.assume_cp,
2206 &document->cp,
2207 &document->cp_status,
2208 document->options.hard_assume);
2209 #ifdef CONFIG_UTF8
2210 html_context->options->utf8 = is_cp_utf8(document->options.cp);
2211 #endif /* CONFIG_UTF8 */
2213 if (title.length) {
2214 document->title = convert_string(renderer_context.convert_table,
2215 title.source, title.length,
2216 document->options.cp,
2217 CSM_DEFAULT, NULL, NULL, NULL);
2219 done_string(&title);
2221 part = format_html_part(html_context, start, end, par_format.align,
2222 par_format.leftmargin,
2223 document->options.box.width, document,
2224 0, 0, head.source, 1);
2226 /* Drop empty allocated lines at end of document if any
2227 * and adjust document height. */
2228 while (document->height && !document->data[document->height - 1].length)
2229 mem_free_if(document->data[--document->height].chars);
2231 /* Calculate document width. */
2233 int i;
2235 document->width = 0;
2236 for (i = 0; i < document->height; i++)
2237 int_lower_bound(&document->width, document->data[i].length);
2240 #if 1
2241 document->options.needs_width = 1;
2242 #else
2243 /* FIXME: This needs more tuning since if we are centering stuff it
2244 * does not work. */
2245 document->options.needs_width =
2246 (document->width + (document->options.margin
2247 >= document->options.width));
2248 #endif
2250 document->bgcolor = par_format.bgcolor;
2252 done_html_parser(html_context);
2254 /* Drop forms which has been serving as a placeholder for form items
2255 * added in the wrong order due to the ordering of table rendering. */
2257 struct form *form;
2259 foreach (form, document->forms) {
2260 if (form->form_num)
2261 continue;
2263 if (list_empty(form->items))
2264 done_form(form);
2266 break;
2270 /* @part was residing in html_context so it has to stay alive until
2271 * done_html_parser(). */
2272 done_string(&head);
2273 mem_free_if(part);
2275 #if 0 /* debug purpose */
2277 FILE *f = fopen("forms", "ab");
2278 struct form_control *form;
2279 unsigned char *qq;
2280 fprintf(f,"FORM:\n");
2281 foreach (form, document->forms) {
2282 fprintf(f, "g=%d f=%d c=%d t:%d\n",
2283 form->g_ctrl_num, form->form_num,
2284 form->ctrl_num, form->type);
2286 fprintf(f,"fragment: \n");
2287 for (qq = start; qq < end; qq++) fprintf(f, "%c", *qq);
2288 fprintf(f,"----------\n\n");
2289 fclose(f);
2291 #endif
2295 find_tag(struct document *document, unsigned char *name, int namelen)
2297 struct tag *tag;
2299 foreach (tag, document->tags)
2300 if (!strlcasecmp(tag->name, -1, name, namelen))
2301 return tag->y;
2303 return -1;