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"
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 */
53 LIST_HEAD(struct tag
);
56 unsigned char name
[1]; /* must be last of struct. --Zas */
65 struct link_state_info
{
67 unsigned char *target
;
69 struct form_control
*form
;
72 struct table_cache_entry_key
{
82 struct table_cache_entry
{
83 LIST_HEAD(struct table_cache_entry
);
85 struct table_cache_entry_key key
;
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
;
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
;
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)
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
);
152 realloc_line(struct html_context
*html_context
, struct document
*document
,
155 struct screen_char
*pos
, *end
;
159 if (!realloc_lines(document
, y
))
162 line
= &document
->data
[y
];
163 orig_length
= line
->length
;
165 if (length
< orig_length
)
168 if (!ALIGN_LINE(&line
->chars
, line
->length
, length
+ 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
];
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;
190 expand_lines(struct html_context
*html_context
, struct part
*part
,
191 int x
, int y
, int lines
, color_T bgcolor
)
195 assert(part
&& part
->document
);
196 if_assert_failed
return;
198 if (!use_document_bg_colors(&part
->document
->options
))
201 par_format
.bgcolor
= bgcolor
;
203 for (line
= 0; line
< lines
; line
++)
204 realloc_line(html_context
, part
->document
, Y(y
+ line
), X(x
));
208 realloc_spaces(struct part
*part
, int length
)
210 if (length
< part
->spaces_len
)
213 if (!ALIGN_SPACES(&part
->spaces
, part
->spaces_len
, length
))
216 if (!ALIGN_SPACES(&part
->char_width
, part
->spaces_len
, length
))
220 part
->spaces_len
= length
;
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
236 clear_hchars(struct html_context
*html_context
, int x
, int y
, int width
)
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)
252 assert(part
->document
->data
);
253 if_assert_failed
return;
256 end
= pos
+ width
- 1;
259 set_screen_char_color(end
, par_format
.bgcolor
, 0x0,
260 0, part
->document
->options
.color_mode
);
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)
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
);
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;
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);
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)
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
;
385 /* First possibly do the format change and then find out what coordinates
386 * to use since sub- or superscript might change them */
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
,
395 const int y
= part
->cy
;
398 const int utf8
= html_context
->options
->utf8
;
402 if_assert_failed
return len
;
404 assert(charslen
>= 0);
406 if (realloc_spaces(part
, x
+ charslen
))
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 */
427 unsigned char *end
= chars
+ charslen
;
430 if (part
->document
->buf_length
) {
431 /* previous char was broken in the middle */
432 int length
= utf8charlen(part
->document
->buf
);
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;
456 /* Still not full char */
457 LINE(y
).length
= orig_length
;
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
) {
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);
476 part
->char_width
[x
] = 0;
481 for (i
= 0; chars
< end
;i
++) {
482 part
->document
->buf
[i
] = *chars
++;
484 part
->document
->buf_length
= i
;
489 if (data
== UCS_NO_BREAK_SPACE
490 && html_context
->options
->wrap_nbsp
)
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
;
499 part
->char_width
[x
] = 0;
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
) {
512 part
->spaces
[x
] = html_context
->options
->wrap_nbsp
;
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
));
536 } else { /* part->document == NULL */
540 for (end
= chars
+ charslen
; chars
< end
; x
++) {
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) {
549 part
->char_width
[x
] = 0;
551 if (data
== UCS_NO_CHAR
) {
552 /* this is at the end only */
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 */
568 /* First possibly do the format change and then find out what coordinates
569 * to use since sub- or superscript might change them */
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
,
581 if_assert_failed
return;
583 if (realloc_spaces(part
, x
+ charslen
))
586 if (part
->document
) {
587 if (realloc_line(html_context
, part
->document
,
588 Y(y
), X(x
) + charslen
- 1) < 0)
591 for (; charslen
> 0; charslen
--, x
++, chars
++) {
592 if (*chars
== NBSP_CHAR
) {
594 part
->spaces
[x
] = html_context
->options
->wrap_nbsp
;
596 part
->spaces
[x
] = (*chars
== ' ');
597 schar
->data
= *chars
;
599 copy_screen_chars(&POS(x
, y
), schar
, 1);
602 for (; charslen
> 0; charslen
--, x
++, chars
++) {
603 part
->spaces
[x
] = (*chars
== ' ');
607 #endif /* CONFIG_UTF8 */
610 move_links(struct html_context
*html_context
, int xf
, int yf
, int xt
, int yt
)
614 int nlink
= renderer_context
.last_link_to_move
;
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
)))
628 for (; nlink
< part
->document
->nlinks
; nlink
++) {
629 struct link
*link
= &part
->document
->links
[nlink
];
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
)) {
666 if (link
->points
[i
].y
!= Y(yf
))
671 if (link
->points
[i
].x
< X(xf
))
675 link
->points
[i
].y
= Y(yt
);
676 link
->points
[i
].x
+= -xf
+ xt
;
678 int to_move
= link
->npoints
- (i
+ 1);
680 assert(to_move
>= 0);
683 memmove(&link
->points
[i
],
684 &link
->points
[i
+ 1],
686 sizeof(*link
->points
));
695 renderer_context
.last_link_to_move
= nlink
;
699 /* Don't move tags when removing links. */
703 tag
= renderer_context
.last_tag_to_move
;
705 while (list_has_next(part
->document
->tags
, tag
)) {
708 if (tag
->y
== Y(yf
)) {
710 if (tag
->x
>= X(xf
)) {
715 } else if (!matched
&& tag
->y
> Y(yf
)) {
716 /* Fix for bug 479 (part two) */
720 if (!matched
) renderer_context
.last_tag_to_move
= tag
;
725 copy_chars(struct html_context
*html_context
, int x
, int y
, int width
, struct screen_char
*d
)
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)
740 copy_screen_chars(&POS(x
, y
), d
, width
);
744 move_chars(struct html_context
*html_context
, int x
, int y
, int nx
, int ny
)
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
);
764 shift_chars(struct html_context
*html_context
, int y
, int shift
)
767 struct screen_char
*a
;
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;
780 a
= fmem_alloc(len
* sizeof(*a
));
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
);
789 move_links(html_context
, 0, y
, shift
, y
);
793 del_chars(struct html_context
*html_context
, int x
, int y
)
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
812 # define overlap_width(x) int_min((x).width, \
813 html_context->options->box.width - TABLE_LINE_PADDING)
815 #define overlap(x) int_max(overlap_width(x) - (x).rightmargin, 0)
818 split_line_at(struct html_context
*html_context
, int width
)
822 int new_width
= width
+ par_format
.rightmargin
;
824 assert(html_context
);
825 if_assert_failed
return 0;
827 part
= html_context
->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;
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
);
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
);
856 if (!(html_context
->options
->utf8
857 && width
< part
->spaces_len
858 && part
->char_width
[width
] == 2))
860 width
++; /* Since we were using (x + 1) only later... */
862 tmp
= part
->spaces_len
- width
;
864 /* 0 is possible and I'm paranoid ... --Zas */
865 memmove(part
->spaces
, part
->spaces
+ width
, tmp
);
867 memmove(part
->char_width
, part
->char_width
+ width
, tmp
);
872 if_assert_failed tmp
= 0;
873 memset(part
->spaces
+ tmp
, 0, width
);
875 memset(part
->char_width
+ tmp
, 0, width
);
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
);
884 memmove(part
->char_width
+ par_format
.leftmargin
, part
->char_width
, tmp
);
890 if (part
->cx
== width
) {
892 int_lower_bound(&part
->box
.height
, part
->cy
);
895 part
->cx
-= width
- par_format
.leftmargin
;
896 int_lower_bound(&part
->box
.height
, part
->cy
+ 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
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?). */
910 split_line(struct html_context
*html_context
)
915 assert(html_context
);
916 if_assert_failed
return 0;
918 part
= html_context
->part
;
921 if_assert_failed
return 0;
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
);
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
);
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. */
967 insert_spaces_in_link(struct part
*part
, int x
, int y
, int new_spaces
)
969 int i
= part
->document
->nlinks
;
975 struct link
*link
= &part
->document
->links
[i
];
976 int j
= link
->npoints
;
979 struct point
*point
= &link
->points
[j
];
981 if (point
->x
!= x
|| point
->y
!= y
)
984 if (!realloc_points(link
, link
->npoints
+ new_spaces
))
987 link
->npoints
+= new_spaces
;
988 point
= &link
->points
[link
->npoints
- 1];
990 while (new_spaces
--) {
1001 /* This function is very rare exemplary of clean and beautyful code here.
1002 * Please handle with care. --pasky */
1004 justify_line(struct html_context
*html_context
, int y
)
1007 struct screen_char
*line
; /* we save original line here */
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;
1024 if_assert_failed
return;
1026 line
= fmem_alloc(len
* sizeof(*line
));
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
));
1038 copy_screen_chars(line
, &POS(0, y
), len
);
1040 /* Skip leading spaces */
1045 while (line
[pos
].data
== ' ')
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;
1054 for (; pos
< len
; pos
++)
1055 if (line
[pos
].data
== ' ')
1056 space_list
[spaces
++] = pos
;
1058 space_list
[spaces
] = len
;
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) {
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
;
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
,
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
);
1111 align_line(struct html_context
*html_context
, int y
, int last
)
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;
1127 if (!len
|| par_format
.align
== ALIGN_LEFT
)
1130 if (par_format
.align
== ALIGN_JUSTIFY
) {
1132 justify_line(html_context
, y
);
1136 shift
= overlap(par_format
) - len
;
1137 if (par_format
.align
== ALIGN_CENTER
)
1140 shift_chars(html_context
, y
, shift
);
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_) \
1151 struct script_event_hook *evhook; \
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); \
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
);
1175 static struct link
*
1176 new_link(struct html_context
*html_context
, unsigned char *name
, int namelen
)
1178 struct document
*document
;
1183 assert(html_context
);
1184 if_assert_failed
return NULL
;
1186 part
= html_context
->part
;
1189 if_assert_failed
return NULL
;
1191 document
= part
->document
;
1194 if_assert_failed
return NULL
;
1196 link_number
= part
->link_num
;
1198 if (!ALIGN_LINK(&document
->links
, document
->nlinks
, document
->nlinks
+ 1))
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
);
1209 link
->target
= null_or_stracpy(format
.target
);
1210 link
->data
.name
= memacpy(name
, namelen
);
1211 /* if (strlen(url) > 4 && !strncasecmp(url, "MAP@", 4)) { */
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);
1221 link
->type
= LINK_HYPERTEXT
;
1222 link
->where
= null_or_stracpy(format
.link
);
1226 struct form_control
*fc
= format
.form
;
1233 link
->type
= LINK_FIELD
;
1236 link
->type
= LINK_AREA
;
1240 link
->type
= LINK_CHECKBOX
;
1243 link
->type
= LINK_SELECT
;
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. */
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;
1275 html_special_tag(struct document
*document
, unsigned char *t
, int x
, int y
)
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
);
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
;
1298 put_chars_conv(struct html_context
*html_context
,
1299 unsigned char *chars
, int charslen
)
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
);
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
);
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
;
1332 format
.link
= format
.target
= format
.image
= NULL
;
1336 ulongcat(s
, &slen
, part
->link_num
, sizeof(s
) - 3, 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
;
1356 #define assert_link_variable(old, new) \
1357 assertm(!(old), "Old link value [%s]. New value [%s]", old, new);
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
;
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
));
1385 process_link(struct html_context
*html_context
, enum link_state link_state
,
1386 unsigned char *chars
, int charslen
, int cells
)
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
;
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
);
1410 unsigned char *new_name
;
1412 new_name
= straconcat(name
, chars
, NULL
);
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.
1429 case LINK_STATE_NEW
:
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
] <= ' ')
1441 charslen
-= x_offset
;
1445 #endif /* CONFIG_UTF8 */
1448 link
= new_link(html_context
, chars
, charslen
);
1453 case LINK_STATE_NONE
:
1455 INTERNAL("bad link_state %i", (int) link_state
);
1459 /* Add new canvas positions to the link. */
1461 if (realloc_points(link
, link
->npoints
+ cells
))
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
);
1471 link
->npoints
+= cells
;
1473 for (; cells
> 0; cells
--, point
++, x
++)
1475 link
->npoints
+= charslen
;
1477 for (; charslen
> 0; charslen
--, point
++, x
++)
1478 #endif /* CONFIG_UTF8 */
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
;
1505 state
= LINK_STATE_NEW
;
1508 done_link_state_info();
1514 html_has_non_space_chars(unsigned char *chars
, int charslen
)
1518 while (pos
< charslen
)
1519 if (!isspace(chars
[pos
++]))
1526 put_chars(struct html_context
*html_context
, unsigned char *chars
, int charslen
)
1528 enum link_state link_state
;
1532 #endif /* CONFIG_UTF8 */
1534 assert(html_context
);
1535 if_assert_failed
return;
1537 part
= html_context
->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
1550 if (!html_is_preformatted()) {
1551 while (charslen
&& *chars
== ' ') {
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
) {
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
] <= ' ')
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
);
1593 #endif /* CONFIG_UTF8 */
1594 set_hline(html_context
, chars
, charslen
, link_state
);
1596 if (link_state
!= LINK_STATE_NONE
) {
1598 process_link(html_context
, link_state
, chars
, charslen
,
1601 process_link(html_context
, link_state
, chars
, charslen
);
1602 #endif /* CONFIG_UTF8 */
1606 if (renderer_context
.nowrap
1607 && part
->cx
+ cells
> overlap(par_format
))
1612 if (renderer_context
.nowrap
1613 && part
->cx
+ charslen
> overlap(par_format
))
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
);
1628 align_line(html_context
, part
->cy
- 1, 0);
1629 renderer_context
.nobreak
= !!(x
- 1);
1633 assert(charslen
> 0);
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()));
1650 line_break(struct html_context
*html_context
)
1655 assert(html_context
);
1656 if_assert_failed
return;
1658 part
= html_context
->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;
1672 if (!part
->document
|| !part
->document
->data
) goto end
;
1674 if (!realloc_lines(part
->document
, part
->box
.height
+ part
->cy
+ 1))
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
);
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
;
1689 tag
->y
= Y(part
->cy
+ 1);
1696 memset(part
->spaces
, 0, part
->spaces_len
);
1698 memset(part
->char_width
, 0, part
->spaces_len
);
1703 html_special_form(struct part
*part
, struct form
*form
)
1705 assert(part
&& form
);
1706 if_assert_failed
return;
1708 if (!part
->document
) {
1713 if (!list_empty(part
->document
->forms
)) {
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
)
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
) {
1735 /* The form start is inside an already added form, so
1736 * partition the space of the existing form and get
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
);
1745 /* If it is the first form make sure it eats the whole form
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
1756 add_to_list(part
->document
->forms
, form
);
1760 html_special_form_control(struct part
*part
, struct form_control
*fc
)
1765 if_assert_failed
return;
1767 if (!part
->document
) {
1768 done_form_control(fc
);
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
,
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
1793 add_to_list(part
->document
->forms
, form
);
1795 /* Attach this form control to the last form encountered. */
1796 form
= part
->document
->forms
.next
;
1798 add_to_list(form
->items
, fc
);
1801 /* Reparents form items based on position in the source. */
1803 check_html_form_hierarchy(struct part
*part
)
1805 struct document
*document
= part
->document
;
1806 INIT_LIST_HEAD(form_controls
);
1808 struct form_control
*fc
, *next
;
1810 if (list_empty(document
->forms
))
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
)
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
)
1839 move_to_top_of_list(form
->items
, fc
);
1844 assert(list_empty(form_controls
));
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
;
1856 for (y
= 0; y
< document
->height
; y
++) {
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;
1875 html_special(struct html_context
*html_context
, enum html_special_type c
, ...)
1879 struct document
*document
;
1880 void *ret_val
= NULL
;
1882 assert(html_context
);
1883 if_assert_failed
return NULL
;
1885 part
= html_context
->part
;
1888 if_assert_failed
return NULL
;
1890 document
= part
->document
;
1896 unsigned char *t
= va_arg(l
, unsigned char *);
1898 html_special_tag(document
, t
, X(part
->cx
), Y(part
->cy
));
1903 struct form
*form
= va_arg(l
, struct form
*);
1905 html_special_form(part
, form
);
1910 struct form_control
*fc
= va_arg(l
, struct form_control
*);
1912 html_special_form_control(part
, fc
);
1916 ret_val
= renderer_context
.convert_table
;
1919 ret_val
= (void *) (long) !!document
;
1921 case SP_CACHE_CONTROL
:
1923 struct cache_entry
*cached
= renderer_context
.cached
;
1925 cached
->cache_mode
= CACHE_MODE_NEVER
;
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
)
1937 timeval_from_seconds(&cached
->max_age
, expires
);
1943 struct frameset_param
*fsp
= va_arg(l
, struct frameset_param
*);
1944 struct frameset_desc
*frameset_desc
;
1946 if (!fsp
->parent
&& document
->frame_desc
)
1949 frameset_desc
= create_frameset(fsp
);
1950 if (!fsp
->parent
&& !document
->frame_desc
)
1951 document
->frame_desc
= frameset_desc
;
1953 ret_val
= frameset_desc
;
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
);
1966 renderer_context
.nowrap
= !!va_arg(l
, int);
1970 unsigned long seconds
= va_arg(l
, unsigned long);
1971 unsigned char *t
= va_arg(l
, unsigned char *);
1974 if (document
->refresh
)
1975 done_document_refresh(document
->refresh
);
1976 document
->refresh
= init_document_refresh(t
, seconds
);
1980 case SP_COLOR_LINK_LINES
:
1981 if (document
&& use_document_bg_colors(&document
->options
))
1982 color_link_lines(html_context
);
1987 struct uri
*uri
= va_arg(l
, struct uri
*);
1989 add_to_uri_list(&document
->css_imports
, uri
);
1994 #ifdef CONFIG_ECMASCRIPT
1996 struct uri
*uri
= va_arg(l
, struct uri
*);
1998 add_to_uri_list(&document
->ecmascript_imports
, uri
);
2010 free_table_cache(void)
2013 struct hash_item
*item
;
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;
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
,
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. */
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
));
2055 key
.margin
= margin
;
2058 key
.link_num
= link_num
;
2060 item
= get_hash_item(table_cache
,
2061 (unsigned char *) &key
,
2063 if (item
) { /* We found it in cache, so just copy and return. */
2064 part
= mem_alloc(sizeof(*part
));
2066 copy_struct(part
, &((struct table_cache_entry
*)
2067 item
->value
)->part
);
2073 assertm(y
>= 0, "format_html_part: y == %d", y
);
2074 if_assert_failed
return NULL
;
2077 struct node
*node
= mem_alloc(sizeof(*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
;
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
;
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
);
2124 mem_free_if(part
->char_width
);
2128 struct node
*node
= document
->nodes
.next
;
2130 node
->box
.height
= y
- node
->box
.y
+ part
->box
.height
;
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
2142 && table_cache_entries
< MAX_TABLE_CACHE_ENTRIES
) {
2143 /* Create a new entry. */
2144 /* Clear memory to prevent bad key comparaison due to alignment
2146 struct table_cache_entry
*tce
= mem_calloc(1, sizeof(*tce
));
2147 /* A goto is used here to prevent a test or code
2151 tce
->key
.start
= start
;
2153 tce
->key
.align
= align
;
2154 tce
->key
.margin
= margin
;
2155 tce
->key
.width
= width
;
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
)) {
2165 table_cache_entries
++;
2175 render_html_document(struct cache_entry
*cached
, struct document
*document
,
2176 struct string
*buffer
)
2178 struct html_context
*html_context
;
2180 unsigned char *start
;
2182 struct string title
;
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
,
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
,
2207 &document
->cp_status
,
2208 document
->options
.hard_assume
);
2210 html_context
->options
->utf8
= is_cp_utf8(document
->options
.cp
);
2211 #endif /* CONFIG_UTF8 */
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. */
2235 document
->width
= 0;
2236 for (i
= 0; i
< document
->height
; i
++)
2237 int_lower_bound(&document
->width
, document
->data
[i
].length
);
2241 document
->options
.needs_width
= 1;
2243 /* FIXME: This needs more tuning since if we are centering stuff it
2245 document
->options
.needs_width
=
2246 (document
->width
+ (document
->options
.margin
2247 >= document
->options
.width
));
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. */
2259 foreach (form
, document
->forms
) {
2263 if (list_empty(form
->items
))
2270 /* @part was residing in html_context so it has to stay alive until
2271 * done_html_parser(). */
2275 #if 0 /* debug purpose */
2277 FILE *f
= fopen("forms", "ab");
2278 struct form_control
*form
;
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");
2295 find_tag(struct document
*document
, unsigned char *name
, int namelen
)
2299 foreach (tag
, document
->tags
)
2300 if (!strlcasecmp(tag
->name
, -1, name
, namelen
))