-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.c
More file actions
5983 lines (5190 loc) · 170 KB
/
Copy pathrender.c
File metadata and controls
5983 lines (5190 loc) · 170 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* @(#)highwire/render1.c - Updated with DOM Integration Phase 1
*
* Enhanced render.c with DOM tree building for CSS 2.1 support
* Part 1 of updated render.c - Header, includes, and core DOM functions
*/
#if defined (__PUREC__)
# include <tos.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <setjmp.h>
#include "token.h" /* must be included before gem/gemx.h */
#include <gemx.h>
#ifndef CLK_TCK
# define CLK_TCK CLOCKS_PER_SEC
#endif
#ifdef __PUREC__
# define PARSER struct s_parser * PARSER
#endif
#include "defs.h"
#include "style.h"
#include "Table.h"
#include "global.h"
#include "scanner.h"
#include "Loader.h"
#include "parser.h"
#include "Containr.h"
#include "Location.h"
#include "Logging.h"
#include "Form.h" /* Form functions and types */
#include "fontbase.h"
#include "http.h"
#include "cache.h"
#include "dom.h" /* DOM API functions and types */
/* parser function flags */
#define PF_NONE 0x0000
#define PF_START 0x0001 /* no slash in tag */
#define PF_PRE 0x0002 /* preformatted text */
#define PF_SPACE 0x0004 /* ignore spaces */
#define PF_FONT 0x0008 /* font has changed */
#define PF_ENCDNG 0x0010 /* charset encoding has changed */
#define PF_SCRIPT 0x0020 /* <noscrip> inside a script area */
static jmp_buf resume_jbuf; /* used for parser interuption */
/*============================================================================*/
/* DOM INTEGRATION ADDITIONS */
/*============================================================================*/
/* Add missing constants that were causing compilation errors */
#ifndef ALN_HIDDEN
#define ALN_HIDDEN 99
#endif
#ifndef CLR_LEFT
#define CLR_LEFT 1
#define CLR_RIGHT 2
#define CLR_BOTH 3
#endif
#ifndef COLOR_TRANSPARENT
#define COLOR_TRANSPARENT 0
#endif
#ifndef DSP_NONE
#define DSP_NONE 0
#define DSP_INLINE 1
#define DSP_BLOCK 2
#endif
#ifndef TD_NONE
#define TD_NONE 0x00
#define TD_UNDERLINE 0x01
#define TD_OVERLINE 0x02
#define TD_LINE_THROUGH 0x04
#endif
#ifndef FS_ITALIC
#define FS_ITALIC 1
#define FS_OBLIQUE 2
#endif
#ifndef FF_GENERIC
#define FF_GENERIC 1
#endif
#ifndef RESIZE_NONE
#define RESIZE_NONE 0
#endif
#ifndef OVERFLOW_HIDDEN
#define OVERFLOW_HIDDEN 1
#endif
/* DOM Context for parser */
typedef struct parse_dom_context {
DOM_DOCUMENT document; /* Current DOM document */
DOM_NODE current_element; /* Current element being parsed */
DOM_NODE html_element; /* <html> root element */
DOM_NODE head_element; /* <head> element */
DOM_NODE body_element; /* <body> element */
BOOL in_head; /* Are we parsing <head> content? */
} PARSE_DOM_CONTEXT;
/* Forward declarations for static render functions */
static UWORD render_STYLE_tag (struct s_parser *, const char **, UWORD);
static UWORD render_TABLE_tag (struct s_parser *, const char **, UWORD);
static UWORD render_TR_tag (struct s_parser *, const char **, UWORD);
static UWORD render_TD_tag (struct s_parser *, const char **, UWORD);
static UWORD render_TH_tag (struct s_parser *, const char **, UWORD);
static void reset_text_styles (PARSER);
static void box_anchor (PARSER, DOMBOX *, BOOL);
/* DOM-specific function declarations */
static PARSE_DOM_CONTEXT* init_dom_context(struct s_parser* parser);
static void cleanup_dom_context(PARSE_DOM_CONTEXT* dom_ctx);
static DOM_NODE create_dom_element_with_layout(PARSE_DOM_CONTEXT* dom_ctx, HTMLTAG tag, const char* tag_name, BOX_CLASS bc);
static void parser_process_tag_with_dom(struct s_parser* parser, PARSE_DOM_CONTEXT* dom_ctx, HTMLTAG tag, BOOL slash, const char** symbol);
/* Add missing function declarations that still exist */
WCHAR font_Space(FONT font);
WCHAR font_Nobrk(FONT font);
void word_set_color(TEXTBUFF current, WORD color);
void word_set_underline(TEXTBUFF current, BOOL underline);
void word_set_strike(TEXTBUFF current, BOOL strike);
void word_set_bold(TEXTBUFF current, BOOL bold);
void log_error(const char* message);
extern IMAGE image_from_dom_node (DOM_NODE node, DOMBOX * box, FRAME frame);
/*============================================================================*/
/* DOM CONTEXT INITIALIZATION */
/*============================================================================*/
/* Initialize DOM context for parsing */
static PARSE_DOM_CONTEXT* init_dom_context(struct s_parser* parser)
{
PARSE_DOM_CONTEXT* dom_ctx = malloc(sizeof(PARSE_DOM_CONTEXT));
if (!dom_ctx) return NULL;
memset(dom_ctx, 0, sizeof(PARSE_DOM_CONTEXT));
/* Create DOM document */
dom_ctx->document = dom_create_document(parser->Frame);
if (!dom_ctx->document) {
free(dom_ctx);
return NULL;
}
/* Initialize stylesheet if not already present */
if (!parser->Frame->Stylesheet && cfg_UseCSS) {
parser->Frame->Stylesheet = stylesheet_new();
dom_ctx->document->stylesheet = parser->Frame->Stylesheet;
}
/* Set current element to document root */
dom_ctx->current_element = dom_ctx->document->document_node;
dom_ctx->in_head = FALSE;
return dom_ctx;
}
/* Cleanup DOM context */
static void cleanup_dom_context(PARSE_DOM_CONTEXT* dom_ctx)
{
if (!dom_ctx) return;
/* DOM document will be cleaned up when frame is destroyed */
/* Just free the context structure */
free(dom_ctx);
}
/*============================================================================*/
/* DOM ELEMENT CREATION WITH LAYOUT INTEGRATION */
/*============================================================================*/
/* Create DOM element and associated layout box */
static DOM_NODE create_dom_element_with_layout(PARSE_DOM_CONTEXT* dom_ctx, HTMLTAG tag, const char* tag_name, BOX_CLASS bc)
{
if (!dom_ctx || !dom_ctx->document) return NULL;
/* Create DOM element */
DOM_NODE element = dom_create_element(dom_ctx->document, tag, tag_name);
if (!element) return NULL;
/* Append to current parent */
if (dom_ctx->current_element) {
dom_append_child(dom_ctx->current_element, element);
}
/* Create associated layout box */
DOMBOX* box = new_box(bc);
if (box) {
/* Link DOM node to layout box */
element->layout_box = box;
box->dom_node = element;
/* Set basic properties */
box->Html.TagName = tag;
/* Initialize CSS computed style */
if (dom_ctx->document->stylesheet) {
box->Style = computed_style_create(NULL);
if (box->Style) {
/* Apply default styles based on tag type - this function should exist in style.c */
/* For now, we'll set basic defaults inline */
switch (tag) {
case TAG_DIV:
box->Style->Display.Type = DSP_BLOCK;
break;
case TAG_SPAN:
box->Style->Display.Type = DSP_INLINE;
break;
case TAG_P:
box->Style->Display.Type = DSP_BLOCK;
break;
default:
box->Style->Display.Type = DSP_INLINE;
break;
}
}
}
}
return element;
}
/*============================================================================*/
/* ENHANCED TAG PROCESSING WITH DOM */
/*============================================================================*/
/* Process HTML tags with DOM tree building */
static void parser_process_tag_with_dom(struct s_parser* parser, PARSE_DOM_CONTEXT* dom_ctx, HTMLTAG tag, BOOL slash, const char** symbol)
{
TEXTBUFF current = &parser->Current;
if (slash) {
/* Closing tag */
switch (tag) {
case TAG_HTML:
if (dom_ctx->html_element) {
dom_ctx->current_element = dom_ctx->html_element->parent_node;
}
break;
case TAG_HEAD:
dom_ctx->in_head = FALSE;
if (dom_ctx->head_element) {
dom_ctx->current_element = dom_ctx->head_element->parent_node;
}
break;
case TAG_BODY:
if (dom_ctx->body_element) {
dom_ctx->current_element = dom_ctx->body_element->parent_node;
}
break;
default:
/* Move up the DOM tree */
if (dom_ctx->current_element && dom_ctx->current_element->parent_node) {
dom_ctx->current_element = dom_ctx->current_element->parent_node;
}
break;
}
} else {
/* Opening tag */
switch (tag) {
case TAG_HTML: {
DOM_NODE html = create_dom_element_with_layout(dom_ctx, tag, "html", BC_MAIN);
if (html) {
dom_ctx->html_element = html;
dom_ctx->current_element = html;
dom_ctx->document->document_element = html;
}
break;
}
case TAG_HEAD: {
DOM_NODE head = create_dom_element_with_layout(dom_ctx, tag, "head", BC_STRUCT);
if (head) {
dom_ctx->head_element = head;
dom_ctx->current_element = head;
dom_ctx->document->head = head;
dom_ctx->in_head = TRUE;
}
break;
}
case TAG_BODY: {
DOM_NODE body = create_dom_element_with_layout(dom_ctx, tag, "body", BC_MAIN);
if (body) {
dom_ctx->body_element = body;
dom_ctx->current_element = body;
dom_ctx->document->body = body;
dom_ctx->in_head = FALSE;
/* Apply CSS styles to body */
if (body->layout_box && dom_ctx->document->stylesheet) {
stylesheet_compute_style_for_box(dom_ctx->document->stylesheet, body->layout_box);
}
}
break;
}
case TAG_DIV: {
DOM_NODE div = create_dom_element_with_layout(dom_ctx, tag, "div", BC_GROUP);
if (div) {
dom_ctx->current_element = div;
/* Apply CSS styles */
if (div->layout_box && dom_ctx->document->stylesheet) {
stylesheet_compute_style_for_box(dom_ctx->document->stylesheet, div->layout_box);
}
}
break;
}
case TAG_P: {
DOM_NODE p = create_dom_element_with_layout(dom_ctx, tag, "p", BC_TXTPAR);
if (p) {
dom_ctx->current_element = p;
/* Apply default paragraph styles */
if (p->layout_box && p->layout_box->Style) {
p->layout_box->Style->Display.Type = DSP_BLOCK;
p->layout_box->Style->Margin.Top.Value = current->word->font->Size;
p->layout_box->Style->Margin.Bottom.Value = current->word->font->Size;
}
}
break;
}
case TAG_H1:
case TAG_H2:
case TAG_H3:
case TAG_H4:
case TAG_H5:
case TAG_H6: {
DOM_NODE heading = create_dom_element_with_layout(dom_ctx, tag, "h1", BC_TXTPAR);
if (heading) {
dom_ctx->current_element = heading;
/* Apply heading-specific styles */
if (heading->layout_box && heading->layout_box->Style) {
heading->layout_box->Style->Display.Type = DSP_BLOCK;
heading->layout_box->Style->FontWeight = 700; /* bold */
/* Set font size based on heading level */
WORD base_size = current->word->font->Size;
switch (tag) {
case TAG_H1: heading->layout_box->Style->FontSize.Value = base_size * 2; break;
case TAG_H2: heading->layout_box->Style->FontSize.Value = (base_size * 3) / 2; break;
case TAG_H3: heading->layout_box->Style->FontSize.Value = base_size; break;
case TAG_H4: heading->layout_box->Style->FontSize.Value = (base_size * 3) / 4; break;
case TAG_H5: heading->layout_box->Style->FontSize.Value = (base_size * 2) / 3; break;
case TAG_H6: heading->layout_box->Style->FontSize.Value = base_size / 2; break;
}
}
}
break;
}
default:
/* Handle other tags with generic processing */
DOM_NODE element = create_dom_element_with_layout(dom_ctx, tag, NULL, BC_GROUP);
if (element) {
dom_ctx->current_element = element;
}
break;
}
/* Process attributes for the current element */
if (dom_ctx->current_element) {
char attr_value[1000];
/* Handle id attribute */
if (get_value(parser, KEY_ID, attr_value, sizeof(attr_value))) {
dom_set_attribute(dom_ctx->current_element, "id", attr_value);
/* Also set in layout box for CSS processing */
if (dom_ctx->current_element->layout_box) {
box_anchor(parser, dom_ctx->current_element->layout_box, FALSE);
}
}
/* Handle class attribute */
if (get_value(parser, KEY_CLASS, attr_value, sizeof(attr_value))) {
dom_set_attribute(dom_ctx->current_element, "class", attr_value);
/* Also set in layout box for CSS processing */
if (dom_ctx->current_element->layout_box) {
dombox_setClass(dom_ctx->current_element->layout_box,
dom_ctx->document->stylesheet, attr_value, FALSE);
}
}
/* Handle style attribute */
if (get_value(parser, KEY_STYLE, attr_value, sizeof(attr_value))) {
dom_set_attribute(dom_ctx->current_element, "style", attr_value);
dom_ctx->current_element->inline_style = _dom_strdup(dom_ctx->document, attr_value);
/* Parse and apply inline styles - this function should exist in style.c */
if (dom_ctx->current_element->layout_box && dom_ctx->document->stylesheet) {
/* For now, we'll skip inline style parsing and let the main CSS system handle it */
stylesheet_compute_style_for_box(dom_ctx->document->stylesheet,
dom_ctx->current_element->layout_box);
}
}
}
}
/* Call original tag processing for backward compatibility */
parser_process_tag(parser, tag, slash, symbol);
}
/*----------------------------------------------------------------------------*/
static WORD
list_indent (WORD type)
{
switch (type)
{
case 0:
return (font_size * 2);
case 1:
return (font_size * 3);
case 2:
return (font_size * 4);
default:
return (font_size * 5);
}
}
/*
* ============================================================================
* Enhanced list_style_type with CSS 2.1 support
*/
static WORD
list_style_type (struct s_parser * parser, WORD dflt)
{
WORD bullet = -1;
char buf[40];
/* CSS list-style-type takes precedence */
if (parser->hasStyle && parser->Current.parentbox &&
parser->Current.parentbox->Style &&
parser->Current.parentbox->Style->List.StyleType != LT_NONE) {
return parser->Current.parentbox->Style->List.StyleType;
}
/* Parse list-style-type from inline style or CSS */
if (get_value (parser, KEY_STYLE, buf, sizeof(buf))) {
char * ptr = strstr(buf, "list-style-type");
if (ptr) {
ptr = strchr(ptr, ':');
if (ptr) {
ptr++;
while (isspace(*ptr)) ptr++;
/* Parse the value */
if (strnicmp(ptr, "disc", 4) == 0) bullet = LT_DISC;
else if (strnicmp(ptr, "circle", 6) == 0) bullet = LT_CIRCLE;
else if (strnicmp(ptr, "square", 6) == 0) bullet = LT_SQUARE;
else if (strnicmp(ptr, "decimal", 7) == 0) bullet = LT_DECIMAL;
else if (strnicmp(ptr, "lower-alpha", 11) == 0) bullet = LT_L_ALPHA;
else if (strnicmp(ptr, "upper-alpha", 11) == 0) bullet = LT_U_ALPHA;
else if (strnicmp(ptr, "lower-roman", 11) == 0) bullet = LT_L_ROMAN;
else if (strnicmp(ptr, "upper-roman", 11) == 0) bullet = LT_U_ROMAN;
else if (strnicmp(ptr, "none", 4) == 0) bullet = LT_NONE;
}
}
}
/* CSS list-style-type property via dedicated attribute (extension) */
if (bullet < 0 && get_value (parser, KEY_LIST_STYLE_TYPE, buf, sizeof(buf))) {
if (stricmp (buf, "disc") == 0) bullet = LT_DISC;
else if (stricmp (buf, "circle") == 0) bullet = LT_CIRCLE;
else if (stricmp (buf, "square") == 0) bullet = LT_SQUARE;
else if (stricmp (buf, "none") == 0) bullet = LT_NONE;
else if (stricmp (buf, "decimal") == 0) bullet = LT_DECIMAL;
else if (stricmp (buf, "decimal-leading-zero") == 0) bullet = LT_DECIMAL_ZERO;
else if (stricmp (buf, "lower-alpha") == 0) bullet = LT_L_ALPHA;
else if (stricmp (buf, "lower-latin") == 0) bullet = LT_L_ALPHA;
else if (stricmp (buf, "upper-alpha") == 0) bullet = LT_U_ALPHA;
else if (stricmp (buf, "upper-latin") == 0) bullet = LT_U_ALPHA;
else if (stricmp (buf, "lower-roman") == 0) bullet = LT_L_ROMAN;
else if (stricmp (buf, "upper-roman") == 0) bullet = LT_U_ROMAN;
else if (stricmp (buf, "lower-greek") == 0) bullet = LT_L_GREEK;
else if (stricmp (buf, "armenian") == 0) bullet = LT_ARMENIAN;
else if (stricmp (buf, "georgian") == 0) bullet = LT_GEORGIAN;
}
/* Legacy HTML type attribute fallback */
if (bullet < 0) switch (get_value_char (parser, KEY_TYPE)) {
case 'a': bullet = LT_L_ALPHA; break;
case 'A': bullet = LT_U_ALPHA; break;
case 'i': bullet = LT_L_ROMAN; break;
case 'I': bullet = LT_U_ROMAN; break;
case '1': bullet = LT_DECIMAL; break;
case 'D': bullet = LT_DISC; break;
case 'S': bullet = LT_SQUARE; break;
case 'C': bullet = LT_CIRCLE; break;
default: bullet = dflt;
}
return bullet;
}
/*
* ============================================================================
* Enhanced reset_text_styles with CSS integration
*/
static void
reset_text_styles (struct s_parser * parser)
{
TEXTBUFF current = &parser->Current;
if (current->parentbox) {
css_text_styles(parser, current->parentbox);
}
}
/*----------------------------------------------------------------------------*/
static void
box_anchor (struct s_parser * parser, DOMBOX * box, BOOL force)
{
TEXTBUFF current = &parser->Current;
char out[100];
/* Pass the stylesheet from the frame to the setter functions */
if (get_value (parser, KEY_ID, out, sizeof(out))
&& dombox_setId (box, parser->Frame->Stylesheet, out, force)) {
ANCHOR anchor = new_named_location (box->IdName, box);
if (anchor) {
anchor->offset.Y = 0;
*current->anchor = anchor;
current->anchor = &anchor->next_location;
}
}
if (get_value (parser, KEY_CLASS, out, sizeof(out))) {
dombox_setClass (box, parser->Frame->Stylesheet, out, force);
}
/* FontStk is obsolete - fonts now determined by computed styles */
}
/*============================================================================*/
/* ENHANCED MAIN PARSING FUNCTION WITH DOM */
/*============================================================================*/
/* Modified parse_html function with DOM tree building */
int parse_html_with_dom(void* arg, long invalidated)
{
PARSER parser = arg;
const char* symbol = parser->Loader->Data;
FRAME frame = parser->Frame;
TEXTBUFF current = &parser->Current;
WCHAR* watermark = parser->Watermark;
ENCODER_W encoder;
UWORD flags;
BOOL linetoolong = FALSE;
if (invalidated || !symbol) {
delete_parser(parser);
return FALSE;
}
if (parser->ResumeErr == 2/*EBUSY*/ || setjmp(resume_jbuf)) {
return -2; /* JOB_NOOP */
}
/* Initialize DOM context */
PARSE_DOM_CONTEXT* dom_ctx = init_dom_context(parser);
if (!dom_ctx) {
/* Fallback to original parsing if DOM initialization fails */
logprintf(LOG_RED, "DOM initialization failed, falling back to original parser\n");
return parse_html_original(arg, invalidated);
}
symbol = parser->ResumePtr;
if (parser->ResumeFnc) {
(*(UWORD (*)(PARSER, const char **, UWORD))parser->ResumeFnc)(parser, &symbol, PF_START);
parser_resumed(parser);
}
font_switch(current->word->font, NULL);
flags = PF_SPACE; /* skip leading spaces */
encoder = encoder_word(frame->Encoding, current->word->font->Base->Mapping);
/* Main parsing loop with DOM tree building */
while (*symbol != '\0') {
if (*symbol == '<') {
HTMLTAG tag;
BOOL slash = FALSE;
if (*(++symbol) == '/') {
slash = TRUE;
symbol++;
}
/* Parse tag */
tag = parse_tag(parser, &symbol);
/* Process with DOM integration */
parser_process_tag_with_dom(parser, dom_ctx, tag, slash, &symbol);
/* Handle encoding and font changes */
if (flags & PF_ENCDNG) {
encoder = encoder_word(frame->Encoding, current->word->font->Base->Mapping);
flags &= ~PF_ENCDNG;
}
if (flags & PF_FONT) {
flags &= ~PF_FONT;
/* Font changes are handled in the DOM processing */
encoder = encoder_word(frame->Encoding, current->word->font->Base->Mapping);
}
continue;
}
/* Handle text content */
switch (*symbol) {
case '&':
if (current->text >= watermark) {
goto line_too_long;
}
current->text = scan_namedchar(&symbol, current->text, TRUE,
current->word->font->Base->Mapping);
flags &= ~PF_SPACE;
/* Create text node in DOM if we have content */
if (dom_ctx->current_element && !dom_ctx->in_head) {
/* Text will be added to DOM when we finish processing the word */
}
break;
case ' ':
if (flags & PF_PRE) {
if (current->text >= watermark) {
goto line_too_long;
}
*(current->text++) = font_Space(current->word->font);
flags |= PF_SPACE;
}
while (isspace(*(++symbol)));
break;
default:
if (current->text < watermark) {
current->text = (*encoder)(&symbol, current->text);
flags &= ~PF_SPACE;
break;
}
line_too_long:
if (linetoolong == FALSE) {
errprintf("parse_html(): Line too long in '%s'!\n",
frame->Location->File);
linetoolong = TRUE;
}
flags &= ~PF_SPACE;
new_word(current, TRUE);
/* Add completed text to DOM */
if (dom_ctx->current_element && !dom_ctx->in_head && current->word) {
/* Extract text content from the word buffer */
size_t text_len = current->text - current->word->item;
if (text_len > 0) {
char* text_buffer = malloc(text_len + 1);
if (text_buffer) {
/* Convert WCHAR to char - simplified conversion */
size_t i;
for (i = 0; i < text_len && i < 255; i++) {
text_buffer[i] = (char)(current->word->item[i] & 0xFF);
}
text_buffer[i] = '\0';
DOM_NODE text_node = dom_create_text_node(dom_ctx->document, text_buffer);
if (text_node) {
dom_append_child(dom_ctx->current_element, text_node);
}
free(text_buffer);
}
}
}
break;
}
}
/* Final DOM tree processing */
if (dom_ctx->document && dom_ctx->document->document_element) {
/* Recompute all styles with full DOM context */
dom_recompute_styles(dom_ctx->document->document_element);
}
logprintf(LOG_BLUE, "DOM-enhanced parsing completed for '%s%s'\n",
location_Path(frame->Location, NULL), frame->Location->File);
cleanup_dom_context(dom_ctx);
delete_parser(parser);
return FALSE;
}
/* Keep original parse_html for compatibility, but use DOM-enhanced version */
int parse_html(void* arg, long invalidated)
{
return parse_html_with_dom(arg, invalidated);
}
/* Original parse_html function for fallback */
int parse_html_original(void* arg, long invalidated)
{
typedef UWORD (*RENDER)(PARSER, const char **, UWORD);
time_t start_clock = clock();
PARSER parser = arg;
const char * symbol = parser->Loader->Data;
FRAME frame = parser->Frame;
TEXTBUFF current = &parser->Current;
WCHAR * watermark = parser->Watermark;
ENCODER_W encoder;
UWORD flags;
BOOL linetoolong;
if (invalidated || !symbol) {
delete_parser (parser);
return FALSE;
}
if (parser->ResumeErr == 2/*EBUSY*/ || setjmp (resume_jbuf)) {
return -2; /* JOB_NOOP */
}
symbol = parser->ResumePtr;
if (parser->ResumeFnc) {
(*(RENDER)parser->ResumeFnc)(parser, &symbol, PF_START);
parser_resumed (parser);
}
font_switch (current->word->font, NULL);
flags = PF_SPACE; /* skip leading spaces */
linetoolong = FALSE; /* "line too long" error printed? */
encoder = encoder_word (frame->Encoding,
current->word->font->Base->Mapping);
while (*symbol != '\0') {
if (*symbol == '<') {
HTMLTAG tag;
BOOL slash = FALSE;
if (*(++symbol) == '/') {
slash = TRUE;
symbol++;
}
tag = parse_tag(parser, &symbol);
parser_process_tag(parser, tag, slash, &symbol);
if (flags & PF_ENCDNG) {
encoder = encoder_word (frame->Encoding,
current->word->font->Base->Mapping);
flags &= ~PF_ENCDNG;
}
if (flags & PF_FONT) {
flags &= ~PF_FONT;
encoder = encoder_word (frame->Encoding,
current->word->font->Base->Mapping);
}
continue;
}
switch (*symbol) {
case '&':
if (current->text >= watermark) {
goto line_too_long;
}
current->text = scan_namedchar (&symbol, current->text, TRUE,
current->word->font->Base->Mapping);
flags &= ~PF_SPACE;
break;
case ' ':
if (flags & PF_PRE) {
if (current->text >= watermark) {
goto line_too_long;
}
*(current->text++) = font_Space (current->word->font);
flags |= PF_SPACE;
}
while (isspace (*(++symbol)));
break;
default:
if (current->text < watermark) {
current->text = (*encoder)(&symbol, current->text);
flags &= ~PF_SPACE;
break;
}
line_too_long:
if (linetoolong == FALSE) {
errprintf("parse_html(): Line too long in '%s'!\n",
frame->Location->File);
linetoolong = TRUE;
}
flags &= ~PF_SPACE;
new_word (current, TRUE);
}
}
logprintf(LOG_BLUE, "%ld ms for '%s%s'\n",
(clock() - start_clock) * 1000 / CLK_TCK,
location_Path (frame->Location, NULL), frame->Location->File);
delete_parser (parser);
return FALSE;
}
/*==============================================================================
* Parse Plain Text
*
* this is the text parsing routine
* for use with non formatted text files
*
* Nothing exciting just maps lines to paragraphs
*/
int
parse_plain (void * arg, long invalidated)
{
time_t start_clock = clock();
PARSER parser = arg;
const char * symbol = parser->Loader->Data;
FRAME frame = parser->Frame;
TEXTBUFF current = &parser->Current;
WCHAR * watermark = parser->Watermark;
ENCODER_W encoder = encoder_word (frame->Encoding,
current->word->font->Base->Mapping);
WORD linefeed = current->word->word_height
+ current->word->word_tail_drop;
BOOL linetoolong = FALSE; /* "line too long" error printed? */
if (invalidated || !symbol) {
delete_parser (parser);
return FALSE;
}
while (*symbol != '\0') {
switch (*symbol) {
case '\r':
if (symbol[1] == '\n') symbol++;
/* no break, fall through */
case '\n':
if (!new_paragraph (current, linefeed, TRUE)) {
goto line_too_long;
}
symbol++;
break;
case ' ':
case '\t':
while (isspace (*(++symbol)));
if (current->text < watermark) {
*(current->text++) = font_Space (current->word->font);
break;
}
/* no break, fall through */
default:
if (current->text < watermark) {
current->text = (*encoder)(&symbol, current->text);
break;
}
line_too_long:
if (linetoolong == FALSE) {
errprintf("parse_plain(): Line too long in '%s'!\n",
frame->Location->File);
linetoolong = TRUE;
}
new_word (current, TRUE);
}
}
logprintf(LOG_BLUE, "%ld ms for '%s%s'\n",
(clock() - start_clock) * 1000 / CLK_TCK,
location_Path (frame->Location, NULL), frame->Location->File);
delete_parser (parser);
return FALSE;
}
/*
* ============================================================================
* Enhanced render_STYLE_tag with improved CSS 2.1 parsing
*/
static UWORD
render_STYLE_tag (struct s_parser * parser, const char ** text, UWORD flags)
{
if (flags & PF_START) {
char out[100];
const char * line = *text;
const char * end_style;
/* Check the type and media attributes of the <style> tag */
if ((!get_value (parser, KEY_TYPE, out, sizeof(out)) ||
mime_byString (out, NULL) == MIME_TXT_CSS) &&
(!get_value (parser, KEY_MEDIA, out, sizeof(out)) ||
(strnicmp (out, "all", 3) == 0) || (strnicmp (out, "screen", 6) == 0))) {
if (!cfg_UseCSS) {
/* If CSS is disabled, just find the closing tag and skip content */
end_style = strstr (line, "</style>");
if (!end_style) end_style = strstr (line, "</STYLE>");
if (end_style) {
*text = end_style;
} else {
*text = strchr (line, '\0'); /* Consume rest of file if no closing tag */
}
return flags;
}
/*
* Find the end of the style block *before* parsing.
* The parser expects a null-terminated string.
*/
end_style = strstr(line, "</style>");
if (!end_style) end_style = strstr(line, "</STYLE>");
if (!end_style) end_style = strchr(line, '\0'); /* Should not happen in valid HTML */
/*
* Create a temporary, null-terminated copy of the style content to
* pass to the CSS parser.
*/
size_t css_len = end_style - line;
char *css_text_block = malloc(css_len + 1);
if (css_text_block) {
memcpy(css_text_block, line, css_len);
css_text_block[css_len] = '\0';
/* Pass the content to the CSS engine */
if (parser->Frame->Stylesheet) {
stylesheet_add_rules_from_text(parser->Frame->Stylesheet, css_text_block);
}
free(css_text_block);
} else {
log_error("render_STYLE_tag: malloc failed for css block.");
}
/* Advance the main parser's pointer past the processed style block */
*text = end_style;
}
/* Code to find the closing </style> tag if not found above */
const char* final_end = strstr(*text, ">");
if (final_end) {
*text = final_end + 1;
}
}
return flags;
}
/*
* ============================================================================
* Enhanced render_LINK_tag with proper asynchronous CSS stylesheet loading
*/
static UWORD
render_LINK_tag (struct s_parser * parser, const char ** text, UWORD flags)
{
if (flags & PF_START) {
char rel[32];
char type[64];
char href[HW_PATH_MAX];
if (get_value(parser, KEY_REL, rel, sizeof(rel))) {
/* Check if it's a stylesheet and if CSS is enabled */
if (stricmp(rel, "StyleSheet") == 0 && cfg_UseCSS) {
/* Check media type */
if (!get_value (parser, KEY_MEDIA, type, sizeof(type)) ||
(strnicmp (type, "all", 3) == 0) ||
(strnicmp (type, "screen", 6) == 0)) {
/* Check mime type */
if ((!get_value(parser, KEY_TYPE, type, sizeof(type)) ||
mime_byString(type, NULL) == MIME_TXT_CSS)) {
/* Finally, get the href */
if(get_value(parser, KEY_HREF, href, sizeof(href))) {
LOCATION loc;
/*
* The parser is not re-entrant, so if we are already in a
* resume state (e.g. from a previous @import), we must
* not start another load.
*/
if (!parser->ResumeFnc) {
loc = new_location(href, parser->Frame->BaseHref);
if (loc) {
LOADER ldr = start_objc_load(parser->Target, NULL, loc,
resume_job, parser);