forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfieldhtml.cpp
More file actions
2292 lines (2034 loc) · 72.8 KB
/
fieldhtml.cpp
File metadata and controls
2292 lines (2034 loc) · 72.8 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
/* Copyright (C) 2003-2013 Runtime Revolution Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "prefix.h"
#include "core.h"
#include "globdefs.h"
#include "filedefs.h"
#include "objdefs.h"
#include "parsedef.h"
#include "field.h"
#include "paragraf.h"
#include "unicode.h"
#include "text.h"
#include "osspec.h"
#include "execpt.h"
#include "mcstring.h"
#include "textbuffer.h"
#include "uidc.h"
#include "globals.h"
////////////////////////////////////////////////////////////////////////////////
typedef uint32_t export_html_tag_type_t;
enum
{
kExportHtmlTagLink,
kExportHtmlTag_Lower = kExportHtmlTagLink,
kExportHtmlTagSpan,
kExportHtmlTagItalic,
kExportHtmlTagBold,
kExportHtmlTagStrike,
kExportHtmlTagUnderline,
kExportHtmlTagSuperscript,
kExportHtmlTagSubscript,
kExportHtmlTagCondensed,
kExportHtmlTagExpanded,
kExportHtmlTagThreeDBox,
kExportHtmlTagBox,
kExportHtmlTagFont,
kExportHtmlTag_Upper,
};
// MW-2012-09-19: [[ Bug 10228 ]] Enum specifying type of escaping to do when
// emitting chars.
enum export_html_escape_type_t
{
kExportHtmlEscapeTypeNone,
kExportHtmlEscapeTypeAttribute,
kExportHtmlEscapeTypeTag,
};
struct export_html_tag_t
{
bool present;
union
{
struct
{
bool has_color;
bool has_bg_color;
MCNameRef name;
int32_t size;
uint32_t color;
uint32_t bg_color;
} font;
uint32_t shift;
MCNameRef metadata;
struct
{
MCNameRef target;
bool is_href;
} link;
};
};
typedef export_html_tag_t export_html_tag_list_t[kExportHtmlTag_Upper];
struct export_html_t
{
text_buffer_t buffer;
bool effective;
uint32_t list_depth;
uint8_t list_styles[16];
export_html_tag_list_t tags;
export_html_tag_type_t tag_stack[10];
uint32_t tag_depth;
};
////////////////////////////////////////////////////////////////////////////////
static const char *s_export_html_tag_strings[] =
{
"a",
"span",
"i",
"b",
"strike",
"u",
"sup",
"sub",
"condensed",
"expanded",
"threedbox",
"box",
"font"
};
static const char *s_export_html_list_types[] =
{
"",
"disc",
"circle",
"square",
"1",
"a",
"A",
"i",
"I",
nil
};
// This is the list of HTML entities for ISO8859-1 (unicode) codepoints in the range
// 0x00A0 to 0x00FF inclusive.
static const char *s_export_html_native_entities[] =
{
"nbsp", "iexcl", "cent", "pound", "curren", "yen", "brvbar", "sect",
"uml", "copy", "ordf", "laquo", "not", "shy", "reg", "macr",
"deg", "plusmn", "sup2", "sup3", "acute", "micro", "para", "middot",
"cedil", "sup1", "ordm", "raquo", "frac14", "frac12", "frac34", "iquest",
"Agrave", "Aacute", "Acirc", "Atilde", "Auml", "Aring", "AElig", "Ccedil",
"Egrave", "Eacute", "Ecirc", "Euml", "Igrave", "Iacute", "Icirc", "Iuml",
"ETH", "Ntilde", "Ograve", "Oacute", "Ocirc", "Otilde", "Ouml", "times",
"Oslash", "Ugrave", "Uacute", "Ucirc", "Uuml", "Yacute", "THORN", "szlig",
"agrave", "aacute", "acirc", "atilde", "auml", "aring", "aelig", "ccedil",
"egrave", "eacute", "ecirc", "euml", "igrave", "iacute", "icirc", "iuml",
"eth", "ntilde", "ograve", "oacute", "ocirc", "otilde", "ouml", "divide",
"oslash", "ugrave", "uacute", "ucirc", "uuml", "yacute", "thorn", "yuml",
};
static struct { const char *entity; uint32_t codepoint; } s_export_html_unicode_entities[] =
{
{ "OElig", 0x0152 }, { "oelig", 0x0153 }, { "Scaron", 0x0160 },
{ "scaron", 0x0161 }, { "Yuml", 0x0178 }, { "fnof", 0x0192 }, { "circ", 0x02C6 },
{ "tilde", 0x02DC }, { "Alpha", 0x0391 }, { "Beta", 0x0392 }, { "Gamma", 0x0393 },
{ "Delta", 0x0394 }, { "Epsilon", 0x0395 }, { "Zeta", 0x0396 }, { "Eta", 0x0397 },
{ "Theta", 0x0398 }, { "Iota", 0x0399 }, { "Kappa", 0x039A }, { "Lambda", 0x039B },
{ "Mu", 0x039C }, { "Nu", 0x039D }, { "Xi", 0x039E }, { "Omicron", 0x039F },
{ "Pi", 0x03A0 }, { "Rho", 0x03A1 }, { "Sigma", 0x03A3 }, { "Tau", 0x03A4 },
{ "Upsilon", 0x03A5 }, { "Phi", 0x03A6 }, { "Chi", 0x03A7 }, { "Psi", 0x03A8 },
{ "Omega", 0x03A9 }, { "alpha", 0x03B1 }, { "beta", 0x03B2 }, { "gamma", 0x03B3 },
{ "delta", 0x03B4 }, { "epsilon", 0x03B5 }, { "zeta", 0x03B6 }, { "eta", 0x03B7 },
{ "theta", 0x03B8 }, { "iota", 0x03B9 }, { "kappa", 0x03BA }, { "lambda", 0x03BB },
{ "mu", 0x03BC }, { "nu", 0x03BD }, { "xi", 0x03BE }, { "omicron", 0x03BF },
{ "pi", 0x03C0 }, { "rho", 0x03C1 }, { "sigmaf", 0x03C2 }, { "sigma", 0x03C3 },
{ "tau", 0x03C4 }, { "upsilon", 0x03C5 }, { "phi", 0x03C6 }, { "chi", 0x03C7 },
{ "psi", 0x03C8 }, { "omega", 0x03C9 }, { "thetasym", 0x03D1 }, { "upsih", 0x03D2 },
{ "piv", 0x03D6 }, { "ensp", 0x2002 }, { "emsp", 0x2003 }, { "thinsp", 0x2009 },
{ "zwnj", 0x200C }, { "zwj", 0x200D }, { "lrm", 0x200E }, { "rlm", 0x200F },
{ "ndash", 0x2013 }, { "mdash", 0x2014 }, { "lsquo", 0x2018 }, { "rsquo", 0x2019 },
{ "sbquo", 0x201A }, { "ldquo", 0x201C }, { "rdquo", 0x201D }, { "bdquo", 0x201E },
{ "dagger", 0x2020 }, { "Dagger", 0x2021 }, { "bull", 0x2022 }, { "hellip", 0x2026 },
{ "permil", 0x2030 }, { "prime", 0x2032 }, { "Prime", 0x2033 }, { "lsaquo", 0x2039 },
{ "rsaquo", 0x203A }, { "oline", 0x203E }, { "frasl", 0x2044 }, { "euro", 0x20AC },
{ "image", 0x2111 }, { "weierp", 0x2118 }, { "real", 0x211C }, { "trade", 0x2122 },
{ "alefsym", 0x2135 }, { "larr", 0x2190 }, { "uarr", 0x2191 }, { "rarr", 0x2192 },
{ "darr", 0x2193 }, { "harr", 0x2194 }, { "crarr", 0x21B5 }, { "lArr", 0x21D0 },
{ "uArr", 0x21D1 }, { "rArr", 0x21D2 }, { "dArr", 0x21D3 }, { "hArr", 0x21D4 },
{ "forall", 0x2200 }, { "part", 0x2202 }, { "exist", 0x2203 }, { "empty", 0x2205 },
{ "nabla", 0x2207 }, { "isin", 0x2208 }, { "notin", 0x2209 }, { "ni", 0x220B },
{ "prod", 0x220F }, { "sum", 0x2211 }, { "minus", 0x2212 }, { "lowast", 0x2217 },
{ "radic", 0x221A }, { "prop", 0x221D }, { "infin", 0x221E }, { "ang", 0x2220 },
{ "and", 0x2227 }, { "or", 0x2228 }, { "cap", 0x2229 }, { "cup", 0x222A },
{ "int", 0x222B }, { "there4", 0x2234 }, { "sim", 0x223C }, { "cong", 0x2245 },
{ "asymp", 0x2248 }, { "ne", 0x2260 }, { "equiv", 0x2261 }, { "le", 0x2264 },
{ "ge", 0x2265 }, { "sub", 0x2282 }, { "sup", 0x2283 }, { "nsub", 0x2284 },
{ "sube", 0x2286 }, { "supe", 0x2287 }, { "oplus", 0x2295 }, { "otimes", 0x2297 },
{ "perp", 0x22A5 }, { "sdot", 0x22C5 }, { "lceil", 0x2308 }, { "rceil", 0x2309 },
{ "lfloor", 0x230A }, { "rfloor", 0x230B }, { "lang", 0x2329 }, { "rang", 0x232A },
{ "loz", 0x25CA }, { "spades", 0x2660 }, { "clubs", 0x2663 }, { "hearts", 0x2665 },
{ "diams", 0x2666 },
};
////////////////////////////////////////////////////////////////////////////////
static bool export_html_lookup_entity(uint32_t p_codepoint, char *r_buffer)
{
uint4 t_low, t_high;
t_low = 0;
t_high = sizeof(s_export_html_unicode_entities) / sizeof(s_export_html_unicode_entities[0]);
while(t_low < t_high)
{
uint4 t_mid;
t_mid = t_low + (t_high - t_low) / 2;
uint4 t_codepoint;
t_codepoint = s_export_html_unicode_entities[t_mid] . codepoint;
if (p_codepoint < t_codepoint)
t_high = t_mid;
else if (p_codepoint > t_codepoint)
t_low = t_mid + 1;
else
{
sprintf(r_buffer, "&%s;", s_export_html_unicode_entities[t_mid] . entity);
return true;
}
}
return false;
}
static void export_html_emit_char(char *p_output, uint32_t p_char, export_html_escape_type_t p_escape)
{
if (p_char < 0x00A0)
{
// MW-2012-09-19: [[ Bug 10228 ]] Make sure we only escape chars based on context.
// Also, no longer escape apostrophe at all, as its not needed.
if (p_char == 0x0022 && p_escape == kExportHtmlEscapeTypeAttribute)
strcpy(p_output, """);
else if (p_char == 0x0026 && p_escape == kExportHtmlEscapeTypeTag)
strcpy(p_output, "&");
else if (p_char == 0x003C && p_escape == kExportHtmlEscapeTypeTag)
strcpy(p_output, "<");
else if (p_char == 0x003E && p_escape == kExportHtmlEscapeTypeTag)
strcpy(p_output, ">");
else if (p_char >= 0x20 && p_char < 0x7f)
p_output[0] = p_char, p_output[1] = '\0';
else
sprintf(p_output, "&#%d;", p_char);
}
else if (p_char < 0x0100)
sprintf(p_output, "&%s;", s_export_html_native_entities[p_char - 0x00A0]);
else if (p_char < 0x0152 || p_char > 0x2666 || !export_html_lookup_entity(p_char, p_output))
sprintf(p_output, "&#%d;", p_char);
}
static void export_html_emit_unicode_text(text_buffer_t& buffer, const uint16_t *p_chars, uint32_t p_char_count, export_html_escape_type_t p_escape)
{
for(uint32_t i = 0; i < p_char_count; i++)
{
char t_output[16];
export_html_emit_char(t_output, p_chars[i], p_escape);
buffer . appendcstring(t_output);
}
}
static void export_html_emit_text(text_buffer_t& buffer, const void *p_bytes, uint32_t p_byte_count, bool p_is_unicode, export_html_escape_type_t p_escape)
{
if (p_is_unicode)
export_html_emit_unicode_text(buffer, (const uint16_t *)p_bytes, p_byte_count / 2, p_escape);
else
{
uint16_t *t_chars;
uint32_t t_char_count;
t_chars = new uint16_t[p_byte_count * 2];
t_char_count = p_byte_count;
MCS_multibytetounicode((const char *)p_bytes, p_byte_count, (char *)t_chars, t_char_count * 2, t_char_count, LCH_ROMAN);
t_char_count /= 2;
export_html_emit_unicode_text(buffer, t_chars, t_char_count, p_escape);
delete t_chars;
}
}
static void export_html_emit_cstring(text_buffer_t& buffer, const char *p_cstring, export_html_escape_type_t p_escape)
{
export_html_emit_text(buffer, p_cstring, strlen(p_cstring), false, p_escape);
}
static bool export_html_tag_equal(const export_html_tag_t& left, const export_html_tag_t& right)
{
return memcmp(&left, &right, sizeof(export_html_tag_t)) == 0;
}
static void export_html_compute_tags(const MCFieldCharacterStyle& p_style, bool p_effective, export_html_tag_list_t r_tags)
{
if (p_effective || p_style . has_text_style)
{
if ((p_style . text_style & (FA_ITALIC | FA_OBLIQUE)) != 0)
r_tags[kExportHtmlTagItalic] . present = true;
if ((p_style . text_style & FA_WEIGHT) == (FA_BOLD & FA_WEIGHT))
r_tags[kExportHtmlTagBold] . present = true;
if ((p_style . text_style & FA_STRIKEOUT) != 0)
r_tags[kExportHtmlTagStrike] . present = true;
if ((p_style . text_style & FA_UNDERLINE) != 0)
r_tags[kExportHtmlTagUnderline] . present = true;
if ((p_style . text_style & FA_BOX) != 0)
r_tags[kExportHtmlTagBox] . present = true;
if ((p_style . text_style & FA_3D_BOX) != 0)
r_tags[kExportHtmlTagThreeDBox] . present = true;
// MW-2012-11-13: [[ Bug ]] Condensed/Expanded don't appear in html.
if ((p_style . text_style & FA_EXPAND) == (FA_EXPANDED & FA_EXPAND))
r_tags[kExportHtmlTagExpanded] . present = true;
if ((p_style . text_style & FA_EXPAND) == (FA_CONDENSED & FA_EXPAND))
r_tags[kExportHtmlTagCondensed] . present = true;
}
if (p_effective || p_style . has_text_shift)
{
export_html_tag_type_t t_tag;
if (p_style . text_shift < 0)
t_tag = kExportHtmlTagSuperscript;
else
t_tag = kExportHtmlTagSubscript;
r_tags[t_tag] . present = true;
r_tags[t_tag] . shift = p_style . text_shift;
}
if (p_effective || p_style . has_text_color)
{
r_tags[kExportHtmlTagFont] . present = true;
r_tags[kExportHtmlTagFont] . font . has_color = true;
r_tags[kExportHtmlTagFont] . font . color = p_style . text_color;
}
if (p_style . has_background_color)
{
r_tags[kExportHtmlTagFont] . present = true;
r_tags[kExportHtmlTagFont] . font . has_bg_color = true;
r_tags[kExportHtmlTagFont] . font . bg_color = p_style . background_color;
}
if (p_effective || p_style . has_text_font)
{
r_tags[kExportHtmlTagFont] . present = true;
r_tags[kExportHtmlTagFont] . font . name = p_style . text_font;
}
if (p_effective || p_style . has_text_size)
{
r_tags[kExportHtmlTagFont] . present = true;
r_tags[kExportHtmlTagFont] . font . size = p_style . text_size;
}
// MW-2012-03-16: [[ Bug ]] Make sure we emit <a></a> if we have link textStyle but
// no linkText.
if (p_style . has_link_text || (p_style . has_text_style && (p_style . text_style & FA_LINK) != 0))
{
r_tags[kExportHtmlTagLink] . present = true;
r_tags[kExportHtmlTagLink] . link . target = p_style . has_link_text ? p_style . link_text : nil;
r_tags[kExportHtmlTagLink] . link . is_href = p_style . has_text_style && (p_style . text_style & FA_LINK) != 0;
}
if (p_style . has_metadata)
{
r_tags[kExportHtmlTagSpan] . present = true;
r_tags[kExportHtmlTagSpan] . metadata = p_style . metadata;
}
}
// IM-2013-11-21: [[ Bug 11475 ]] Ensure html hex colors contain only rgb hex values in the right order
static const char *export_html_hexcolor(uint32_t p_pixel)
{
static char s_color[8];
uint8_t r, g, b, a;
MCGPixelUnpackNative(p_pixel, r, g, b, a);
sprintf(s_color, "#%02.2X%02.2X%02.2X", r, g, b);
return s_color;
}
static void export_html_add_tag(export_html_t& ctxt, export_html_tag_type_t p_tag, export_html_tag_t p_value)
{
if (!p_value . present)
return;
ctxt . tags[p_tag] = p_value;
ctxt . tag_stack[ctxt . tag_depth++] = p_tag;
switch(p_tag)
{
case kExportHtmlTagSuperscript:
case kExportHtmlTagSubscript:
ctxt . buffer . appendtextf("<%s shift=\"%d\">", s_export_html_tag_strings[p_tag], p_value . shift);
break;
case kExportHtmlTagFont:
ctxt . buffer . appendcstring("<font");
if (p_value . font . name != nil)
ctxt . buffer . appendtextf(" face=\"%s\"", MCNameGetCString(p_value . font . name));
if (p_value . font . size != 0)
ctxt . buffer . appendtextf(" size=\"%d\"", p_value . font . size);
if (p_value . font . has_color)
ctxt . buffer . appendtextf(" color=\"%s\"", export_html_hexcolor(p_value . font . color));
if (p_value . font . has_bg_color)
ctxt . buffer . appendtextf(" bgcolor=\"%s\"", export_html_hexcolor(p_value . font . bg_color));
ctxt . buffer . appendcstring(">");
break;
case kExportHtmlTagItalic:
case kExportHtmlTagBold:
case kExportHtmlTagStrike:
case kExportHtmlTagUnderline:
case kExportHtmlTagCondensed:
case kExportHtmlTagExpanded:
case kExportHtmlTagThreeDBox:
case kExportHtmlTagBox:
ctxt . buffer . appendtextf("<%s>", s_export_html_tag_strings[p_tag]);
break;
case kExportHtmlTagLink:
{
// MW-2012-03-16: [[ Bug ]] If the linkText is nil, then just output a <a> tag.
if (p_value . link . target != nil)
{
ctxt . buffer . appendtextf("<a %s=\"", p_value . link . is_href ? "href" : "name");
// MW-2012-09-19: [[ Bug 10228 ]] Make sure we generate the string in quote context.
export_html_emit_cstring(ctxt . buffer, MCNameGetCString(p_value . link . target), kExportHtmlEscapeTypeAttribute);
ctxt . buffer . appendcstring("\">");
}
else
ctxt . buffer . appendcstring("<a>");
}
break;
case kExportHtmlTagSpan:
ctxt . buffer . appendcstring("<span metadata=\"");
// MW-2012-09-19: [[ Bug 10228 ]] Make sure we generate the string in quote context.
export_html_emit_cstring(ctxt . buffer, MCNameGetCString(p_value . metadata), kExportHtmlEscapeTypeAttribute);
ctxt . buffer . appendcstring("\">");
break;
default:
break;
}
}
static void export_html_remove_tag(export_html_t& ctxt, export_html_tag_type_t p_tag, bool p_all_above)
{
if (!ctxt . tags[p_tag] . present)
return;
while(ctxt . tag_depth > 0)
{
ctxt . tag_depth -= 1;
memset(&ctxt . tags[ctxt . tag_stack[ctxt . tag_depth]], 0, sizeof(export_html_tag_t));
ctxt . buffer . appendtextf("</%s>", s_export_html_tag_strings[ctxt . tag_stack[ctxt . tag_depth]]);
if (ctxt . tag_stack[ctxt . tag_depth] == p_tag &&
(!p_all_above || ctxt . tag_stack[ctxt . tag_depth] > p_tag))
break;
}
}
static void export_html_remove_all_tags(export_html_t& ctxt, bool p_keep_link)
{
while(ctxt . tag_depth > 0)
{
ctxt . tag_depth -= 1;
memset(&ctxt . tags[ctxt . tag_stack[ctxt . tag_depth]], 0, sizeof(export_html_tag_t));
ctxt . buffer . appendtextf("</%s>", s_export_html_tag_strings[ctxt . tag_stack[ctxt . tag_depth]]);
if (ctxt . tag_stack[ctxt . tag_depth] == kExportHtmlTagLink && p_keep_link)
break;
}
}
static void export_html_end_lists(export_html_t& ctxt, uint32_t p_new_style, uint32_t p_new_depth)
{
if (ctxt . list_depth > 0 && !(p_new_depth == ctxt . list_depth && p_new_style == kMCParagraphListStyleSkip))
ctxt . buffer . appendcstring("</li>\n");
while(p_new_depth < ctxt . list_depth)
{
ctxt . list_depth -= 1;
ctxt . buffer . appendcstring(ctxt . list_styles[ctxt . list_depth] < kMCParagraphListStyleNumeric ? "</ul>" : "</ol>");
}
}
static void export_html_begin_lists(export_html_t& ctxt, uint32_t p_new_style, uint32_t p_new_depth, uint32_t p_index)
{
if (p_new_depth == 0)
return;
if (ctxt . list_depth == p_new_depth && p_new_style != kMCParagraphListStyleSkip && ctxt . list_styles[ctxt . list_depth - 1] != p_new_style)
{
ctxt . list_depth -= 1;
ctxt . buffer . appendcstring(ctxt . list_styles[ctxt . list_depth] < kMCParagraphListStyleNumeric ? "</ul>" : "</ol>");
}
while(p_new_depth > ctxt . list_depth)
{
ctxt . list_styles[ctxt . list_depth] = p_new_style;
ctxt . buffer . appendtextf(ctxt . list_styles[ctxt . list_depth] < kMCParagraphListStyleNumeric ? "<ul type=\"%s\">\n" : "<ol type=\"%s\">\n", s_export_html_list_types[p_new_style]);
ctxt . list_depth += 1;
}
if (ctxt . list_depth != p_new_depth || p_new_style != kMCParagraphListStyleSkip)
{
if (p_index == 0)
ctxt . buffer . appendcstring("<li>\n");
else
ctxt . buffer . appendtextf("<li value=%d>", p_index);
}
}
static bool export_html_emit_paragraphs(void *p_context, MCFieldExportEventType p_event_type, const MCFieldExportEventData& p_event_data)
{
export_html_t& ctxt = *(export_html_t *)p_context;
if (p_event_type == kMCFieldExportEventBeginParagraph)
{
uint32_t t_new_list_style, t_new_list_depth, t_new_list_index;
if (p_event_data . has_paragraph_style && p_event_data . paragraph_style . has_list_style)
{
t_new_list_style = p_event_data . paragraph_style . list_style;
t_new_list_depth = p_event_data . paragraph_style . list_depth + 1;
t_new_list_index = p_event_data . paragraph_style . has_list_index ? p_event_data . paragraph_style . list_index : 0;
}
else
t_new_list_style = kMCParagraphListStyleNone, t_new_list_depth = 0, t_new_list_index = 0;
export_html_end_lists(ctxt, t_new_list_style, t_new_list_depth);
export_html_begin_lists(ctxt, t_new_list_style, t_new_list_depth, t_new_list_index);
if (ctxt . effective || p_event_data . has_paragraph_style)
{
const MCFieldParagraphStyle& t_style = p_event_data . paragraph_style;
ctxt . buffer . appendcstring("<p");
if (t_style . has_metadata)
{
ctxt . buffer . appendcstring(" metadata=\"");
export_html_emit_cstring(ctxt . buffer, MCNameGetCString(t_style . metadata), kExportHtmlEscapeTypeAttribute);
ctxt . buffer . appendcstring("\"");
}
if (ctxt . effective || t_style . has_text_align)
ctxt . buffer . appendtextf(" align=\"%s\"", MCtextalignstrings[t_style . text_align]);
if (!t_style . has_list_indent && (t_style . has_first_indent || ctxt . effective))
ctxt . buffer . appendtextf(" firstindent=\"%d\"", t_style . first_indent);
else if (t_style . has_list_indent)
ctxt . buffer . appendtextf(" listindent=\"%d\"", t_style . list_indent);
if (ctxt . effective || t_style . has_left_indent)
ctxt . buffer . appendtextf(" leftindent=\"%d\"", t_style . left_indent);
if (ctxt . effective || t_style . has_right_indent)
ctxt . buffer . appendtextf(" rightindent=\"%d\"", t_style . right_indent);
if (ctxt . effective || t_style . has_space_above)
ctxt . buffer . appendtextf(" spaceabove=\"%d\"", t_style . space_above);
if (ctxt . effective || t_style . has_space_below)
ctxt . buffer . appendtextf(" spacebelow=\"%d\"", t_style . space_below);
if (ctxt . effective || t_style . has_tabs)
{
ctxt . buffer . appendcstring(" tabstops=\"");
for(uint32_t i = 0; i < t_style . tab_count; i++)
ctxt . buffer . appendtextf(i == 0 ? "%d" : ",%d", t_style . tabs[i]);
ctxt . buffer . appendcstring("\"");
}
if (t_style . has_background_color)
ctxt . buffer . appendtextf(" bgcolor=\"%s\"", export_html_hexcolor(t_style . background_color));
if (t_style . has_border_width || ctxt . effective)
ctxt . buffer . appendtextf(" borderwidth=\"%d\"", t_style . border_width);
if (t_style . has_border_color || ctxt . effective)
ctxt . buffer . appendtextf(" bordercolor=\"%s\"", export_html_hexcolor(t_style . border_color));
if (t_style . has_padding || ctxt . effective)
ctxt . buffer . appendtextf(" padding=\"%d\"", t_style . padding);
if (t_style . has_hgrid || ctxt . effective)
ctxt . buffer . appendcstring(t_style . hgrid ? " hgrid" : " nohgrid");
if (t_style . has_vgrid || ctxt . effective)
ctxt . buffer . appendcstring(t_style . vgrid ? " vgrid" : " novgrid");
if (t_style . has_dont_wrap || ctxt . effective)
ctxt . buffer . appendcstring(t_style . dont_wrap ? " nowrap" : " wrap");
if (t_style . hidden)
ctxt . buffer . appendcstring(" hidden");
ctxt . buffer . appendcstring(">");
}
else
ctxt . buffer . appendcstring("<p>");
}
else if (p_event_type == kMCFieldExportEventEndParagraph)
{
export_html_remove_all_tags(ctxt, false);
// MW-2012-09-18: [[ Bug 10216 ]] If we are the last paragraph, but have non-zero listDepth then
// still emit a newline.
ctxt . buffer . appendcstring(p_event_data . is_last_paragraph && ctxt . list_depth == 0 ? "</p>" : "</p>\n");
if (p_event_data . is_last_paragraph)
export_html_end_lists(ctxt, kMCParagraphListStyleNone, 0);
}
else if (p_event_type == kMCFieldExportEventUnicodeRun || p_event_type == kMCFieldExportEventNativeRun)
{
export_html_tag_list_t t_new_tags;
memset(t_new_tags, 0, sizeof(export_html_tag_list_t));
if (ctxt . effective || p_event_data . has_character_style)
export_html_compute_tags(p_event_data . character_style, ctxt . effective, t_new_tags);
for(uint32_t i = kExportHtmlTag_Lower; i < kExportHtmlTag_Upper; i++)
if (!export_html_tag_equal(t_new_tags[i], ctxt . tags[i]))
export_html_remove_tag(ctxt, i, i <= kExportHtmlTagSpan);
for(uint32_t i = kExportHtmlTag_Lower; i < kExportHtmlTag_Upper; i++)
if (!export_html_tag_equal(t_new_tags[i], ctxt . tags[i]))
export_html_add_tag(ctxt, i, t_new_tags[i]);
if (p_event_data . character_style . has_image_source)
{
ctxt . buffer . appendcstring("<img src=\"");
// MW-2012-09-19: [[ Bug 10228 ]] Make sure we generate the string in quote context.
export_html_emit_cstring(ctxt . buffer, MCNameGetCString(p_event_data . character_style . image_source), kExportHtmlEscapeTypeAttribute);
ctxt . buffer . appendcstring("\" char=\"");
// MW-2012-09-19: [[ Bug 10228 ]] Make sure we generate the string in quote context.
export_html_emit_text(ctxt . buffer, p_event_data . bytes, p_event_data . byte_count, p_event_type == kMCFieldExportEventUnicodeRun, kExportHtmlEscapeTypeAttribute);
ctxt . buffer . appendcstring("\">");
}
else
{
// MW-2012-09-19: [[ Bug 10228 ]] Make sure we generate the string in tag context.
export_html_emit_text(ctxt . buffer, p_event_data . bytes, p_event_data . byte_count, p_event_type == kMCFieldExportEventUnicodeRun, kExportHtmlEscapeTypeTag);
}
}
return true;
}
void MCField::exportashtmltext(MCExecPoint& ep, MCParagraph *p_paragraphs, int32_t p_start_index, int32_t p_finish_index, bool p_effective)
{
export_html_t ctxt;
memset(&ctxt, 0, sizeof(export_html_t));
ctxt . effective = p_effective;
ctxt . list_depth = 0;
// Compute the flags to pass to export.
uint32_t t_flags;
t_flags = kMCFieldExportParagraphs | kMCFieldExportRuns | kMCFieldExportParagraphStyles | kMCFieldExportCharacterStyles;
if (p_effective)
t_flags |= kMCFieldExportFlattenStyles;
// Now execute the second pass which generates all the paragraph content.
doexport(t_flags, p_paragraphs, p_start_index, p_finish_index, export_html_emit_paragraphs, &ctxt);
// Return the buffer in the ep.
ctxt . buffer . givetoep(ep);
}
void MCField::exportashtmltext(uint32_t p_part_id, MCExecPoint& ep, int32_t p_start_index, int32_t p_finish_index, bool p_effective)
{
exportashtmltext(ep, resolveparagraphs(p_part_id), p_start_index, p_finish_index, p_effective);
}
////////////////////////////////////////////////////////////////////////////////
enum import_html_tag_type_t
{
kImportHtmlTagNone,
kImportHtmlTagP,
kImportHtmlTagLi,
kImportHtmlTagH1,
kImportHtmlTagH2,
kImportHtmlTagH3,
kImportHtmlTagH4,
kImportHtmlTagH5,
kImportHtmlTagH6,
kImportHtmlTagBlockquote,
kImportHtmlTagDt,
kImportHtmlTagDd,
kImportHtmlTagBr,
kImportHtmlTagHr,
kImportHtmlTagOl,
kImportHtmlTagUl,
kImportHtmlTagPre,
kImportHtmlTag_FirstStyleTag,
kImportHtmlTagAnchor,
kImportHtmlTagImage,
kImportHtmlTagFont,
kImportHtmlTagBold,
kImportHtmlTagItalic,
kImportHtmlTagUnderline,
kImportHtmlTagStrike,
kImportHtmlTagCondensed,
kImportHtmlTagExpanded,
kImportHtmlTagBox,
kImportHtmlTagThreeDBox,
kImportHtmlTagSub,
kImportHtmlTagSup,
// MW-2012-08-31: [[ Bug 10343 ]] Implement support for importing 'span' tags.
kImportHtmlTagSpan,
kImportHtmlTagTitle,
kImportHtmlTagHead,
kImportHtmlTagMeta,
kImportHtmlTagHtml,
kImportHtmlTagBody,
kImportHtmlTagAddress,
kImportHtmlTagBig,
kImportHtmlTagCaption,
kImportHtmlTagCite,
kImportHtmlTagCode,
kImportHtmlTagDef,
kImportHtmlTagEm,
kImportHtmlTagSmall,
kImportHtmlTagStrong,
kImportHtmlTagVar,
kImportHtmlTagTable,
kImportHtmlTagTd,
kImportHtmlTagTh,
kImportHtmlTagTr,
kImportHtmlTagTt,
};
static struct { const char *tag; import_html_tag_type_t type; } s_import_html_tag_strings[] =
{
{ "p", kImportHtmlTagP },
{ "li", kImportHtmlTagLi },
{ "h1", kImportHtmlTagH1 },
{ "h2", kImportHtmlTagH2 },
{ "h3", kImportHtmlTagH3 },
{ "h4", kImportHtmlTagH4 },
{ "h5", kImportHtmlTagH5 },
{ "h6", kImportHtmlTagH6 },
{ "blockquote", kImportHtmlTagBlockquote },
{ "dt", kImportHtmlTagDt },
{ "dd", kImportHtmlTagDd },
{ "br", kImportHtmlTagBr },
{ "hr", kImportHtmlTagHr },
{ "ol", kImportHtmlTagOl },
{ "ul", kImportHtmlTagUl },
{ "pre", kImportHtmlTagPre },
{ "a", kImportHtmlTagAnchor },
{ "img", kImportHtmlTagImage },
{ "font", kImportHtmlTagFont },
{ "b", kImportHtmlTagBold },
{ "i", kImportHtmlTagItalic },
{ "u", kImportHtmlTagUnderline },
{ "strike", kImportHtmlTagStrike },
{ "condensed", kImportHtmlTagCondensed },
{ "expanded", kImportHtmlTagExpanded },
{ "box", kImportHtmlTagBox },
{ "threedbox", kImportHtmlTagThreeDBox },
{ "sub", kImportHtmlTagSub },
{ "sup", kImportHtmlTagSup },
// MW-2012-08-31: [[ Bug 10343 ]] Implement support for importing 'span' tags.
{ "span", kImportHtmlTagSpan },
{ "title", kImportHtmlTagTitle },
{ "head", kImportHtmlTagHead },
{ "meta", kImportHtmlTagMeta },
{ "html", kImportHtmlTagHtml },
{ "body", kImportHtmlTagBody },
{ "address", kImportHtmlTagAddress },
{ "big", kImportHtmlTagBig },
{ "caption", kImportHtmlTagCaption },
{ "cite", kImportHtmlTagCite },
{ "code", kImportHtmlTagCode },
{ "def", kImportHtmlTagDef },
{ "em", kImportHtmlTagEm },
{ "small", kImportHtmlTagSmall },
{ "strong", kImportHtmlTagStrong },
{ "var", kImportHtmlTagVar },
{ "table", kImportHtmlTagTable },
{ "td", kImportHtmlTagTd },
{ "th", kImportHtmlTagTh },
{ "tr", kImportHtmlTagTr },
{ "tt", kImportHtmlTagTt },
};
enum import_html_attr_type_t
{
kImportHtmlAttrShift,
kImportHtmlAttrFace,
kImportHtmlAttrSize,
kImportHtmlAttrColor,
kImportHtmlAttrBgColor,
kImportHtmlAttrHref,
kImportHtmlAttrName,
kImportHtmlAttrMetadata,
kImportHtmlAttrAlign,
kImportHtmlAttrFirstIndent,
kImportHtmlAttrListIndent,
kImportHtmlAttrLeftIndent,
kImportHtmlAttrRightIndent,
kImportHtmlAttrSpaceAbove,
kImportHtmlAttrSpaceBelow,
kImportHtmlAttrTabStops,
kImportHtmlAttrBorderWidth,
kImportHtmlAttrBorderColor,
kImportHtmlAttrPadding,
kImportHtmlAttrHGrid,
kImportHtmlAttrNoHGrid,
kImportHtmlAttrVGrid,
kImportHtmlAttrNoVGrid,
kImportHtmlAttrWrap,
kImportHtmlAttrNoWrap,
kImportHtmlAttrHidden,
kImportHtmlAttrSrc,
kImportHtmlAttrChar,
kImportHtmlAttrType,
kImportHtmlAttrValue,
kImportHtmlAttrStart,
kImportHtmlAttrCharset,
kImportHtmlAttrContent,
};
static struct { const char *attr; import_html_attr_type_t type; } s_import_html_attr_strings[] =
{
{ "shift", kImportHtmlAttrShift },
{ "face", kImportHtmlAttrFace },
{ "size", kImportHtmlAttrSize },
{ "color", kImportHtmlAttrColor },
{ "bgcolor", kImportHtmlAttrBgColor },
{ "href", kImportHtmlAttrHref },
{ "name", kImportHtmlAttrName },
{ "metadata", kImportHtmlAttrMetadata },
{ "align", kImportHtmlAttrAlign },
{ "firstindent", kImportHtmlAttrFirstIndent },
{ "leftindent", kImportHtmlAttrLeftIndent },
{ "rightindent", kImportHtmlAttrRightIndent },
{ "spaceabove", kImportHtmlAttrSpaceAbove},
{ "spacebelow", kImportHtmlAttrSpaceBelow },
{ "tabstops", kImportHtmlAttrTabStops },
{ "borderwidth", kImportHtmlAttrBorderWidth },
{ "bordercolor", kImportHtmlAttrBorderColor },
{ "padding", kImportHtmlAttrPadding },
{ "hgrid", kImportHtmlAttrHGrid },
{ "nohgrid", kImportHtmlAttrNoHGrid },
{ "vgrid", kImportHtmlAttrVGrid },
{ "novgrid", kImportHtmlAttrNoVGrid },
{ "wrap", kImportHtmlAttrWrap },
{ "nowrap", kImportHtmlAttrNoWrap },
{ "hidden", kImportHtmlAttrHidden },
{ "src", kImportHtmlAttrSrc },
{ "char", kImportHtmlAttrChar },
{ "type", kImportHtmlAttrType },
{ "charset", kImportHtmlAttrCharset },
{ "content", kImportHtmlAttrContent },
{ "value", kImportHtmlAttrValue },
{ "start", kImportHtmlAttrStart },
};
static struct { const char *entity; uint32_t codepoint; } s_import_html_entities[] =
{
{ "AElig", 0x00C6 }, { "Aacute", 0x00C1 }, { "Acirc", 0x00C2 }, { "Agrave", 0x00C0 },
{ "Alpha", 0x0391 }, { "Aring", 0x00C5 }, { "Atilde", 0x00C3 }, { "Auml", 0x00C4 },
{ "Beta", 0x0392 }, { "Ccedil", 0x00C7 }, { "Chi", 0x03A7 }, { "Dagger", 0x2021 },
{ "Delta", 0x0394 }, { "ETH", 0x00D0 }, { "Eacute", 0x00C9 }, { "Ecirc", 0x00CA },
{ "Egrave", 0x00C8 }, { "Epsilon", 0x0395 }, { "Eta", 0x0397 }, { "Euml", 0x00CB },
{ "Gamma", 0x0393 }, { "Iacute", 0x00CD }, { "Icirc", 0x00CE }, { "Igrave", 0x00CC },
{ "Iota", 0x0399 }, { "Iuml", 0x00CF }, { "Kappa", 0x039A }, { "Lambda", 0x039B },
{ "Mu", 0x039C }, { "Ntilde", 0x00D1 }, { "Nu", 0x039D }, { "OElig", 0x0152 },
{ "Oacute", 0x00D3 }, { "Ocirc", 0x00D4 }, { "Ograve", 0x00D2 }, { "Omega", 0x03A9 },
{ "Omicron", 0x039F }, { "Oslash", 0x00D8 }, { "Otilde", 0x00D5 }, { "Ouml", 0x00D6 },
{ "Phi", 0x03A6 }, { "Pi", 0x03A0 }, { "Prime", 0x2033 }, { "Psi", 0x03A8 },
{ "Rho", 0x03A1 }, { "Scaron", 0x0160 }, { "Sigma", 0x03A3 }, { "THORN", 0x00DE },
{ "Tau", 0x03A4 }, { "Theta", 0x0398 }, { "Uacute", 0x00DA }, { "Ucirc", 0x00DB },
{ "Ugrave", 0x00D9 }, { "Upsilon", 0x03A5 }, { "Uuml", 0x00DC }, { "Xi", 0x039E },
{ "Yacute", 0x00DD }, { "Yuml", 0x0178 }, { "Zeta", 0x0396 }, { "aacute", 0x00E1 },
{ "acirc", 0x00E2 }, { "acute", 0x00B4 }, { "aelig", 0x00E6 }, { "agrave", 0x00E0 },
{ "alefsym", 0x2135 }, { "alpha", 0x03B1 }, { "amp", 0x0026 }, { "and", 0x2227 },
{ "ang", 0x2220 }, { "apos", 0x0027 }, { "aring", 0x00E5 }, { "asymp", 0x2248 },
{ "atilde", 0x00E3 }, { "auml", 0x00E4 }, { "bdquo", 0x201E }, { "beta", 0x03B2 },
{ "brvbar", 0x00A6 }, { "bull", 0x2022 }, { "cap", 0x2229 }, { "ccedil", 0x00E7 },
{ "cedil", 0x00B8 }, { "cent", 0x00A2 }, { "chi", 0x03C7 }, { "circ", 0x02C6 },
{ "clubs", 0x2663 }, { "cong", 0x2245 }, { "copy", 0x00A9 }, { "crarr", 0x21B5 },
{ "cup", 0x222A }, { "curren", 0x00A4 }, { "dArr", 0x21D3 }, { "dagger", 0x2020 },
{ "darr", 0x2193 }, { "deg", 0x00B0 }, { "delta", 0x03B4 }, { "diams", 0x2666 },
{ "divide", 0x00F7 }, { "eacute", 0x00E9 }, { "ecirc", 0x00EA }, { "egrave", 0x00E8 },
{ "empty", 0x2205 }, { "emsp", 0x2003 }, { "ensp", 0x2002 }, { "epsilon", 0x03B5 },
{ "equiv", 0x2261 }, { "eta", 0x03B7 }, { "eth", 0x00F0 }, { "euml", 0x00EB },
{ "euro", 0x20AC }, { "exist", 0x2203 }, { "fnof", 0x0192 }, { "forall", 0x2200 },
{ "frac12", 0x00BD }, { "frac14", 0x00BC }, { "frac34", 0x00BE }, { "frasl", 0x2044 },
{ "gamma", 0x03B3 }, { "ge", 0x2265 }, { "gt", 0x003E }, { "hArr", 0x21D4 },
{ "harr", 0x2194 }, { "hearts", 0x2665 }, { "hellip", 0x2026 }, { "iacute", 0x00ED },
{ "icirc", 0x00EE }, { "iexcl", 0x00A1 }, { "igrave", 0x00EC }, { "image", 0x2111 },
{ "infin", 0x221E }, { "int", 0x222B }, { "iota", 0x03B9 }, { "iquest", 0x00BF },
{ "isin", 0x2208 }, { "iuml", 0x00EF }, { "kappa", 0x03BA }, { "lArr", 0x21D0 },
{ "lambda", 0x03BB }, { "lang", 0x2329 }, { "laquo", 0x00AB }, { "larr", 0x2190 },
{ "lceil", 0x2308 }, { "ldquo", 0x201C }, { "le", 0x2264 }, { "lfloor", 0x230A },
{ "lowast", 0x2217 }, { "loz", 0x25CA }, { "lrm", 0x200E }, { "lsaquo", 0x2039 },
{ "lsquo", 0x2018 }, { "lt", 0x003C }, { "macr", 0x00AF }, { "mdash", 0x2014 },
{ "micro", 0x00B5 }, { "middot", 0x00B7 }, { "minus", 0x2212 }, { "mu", 0x03BC },
{ "nabla", 0x2207 }, { "nbsp", 0x00A0 }, { "ndash", 0x2013 }, { "ne", 0x2260 },
{ "ni", 0x220B }, { "not", 0x00AC }, { "notin", 0x2209 }, { "nsub", 0x2284 },
{ "ntilde", 0x00F1 }, { "nu", 0x03BD }, { "oacute", 0x00F3 }, { "ocirc", 0x00F4 },
{ "oelig", 0x0153 }, { "ograve", 0x00F2 }, { "oline", 0x203E }, { "omega", 0x03C9 },
{ "omicron", 0x03BF }, { "oplus", 0x2295 }, { "or", 0x2228 }, { "ordf", 0x00AA },
{ "ordm", 0x00BA }, { "oslash", 0x00F8 }, { "otilde", 0x00F5 }, { "otimes", 0x2297 },
{ "ouml", 0x00F6 }, { "para", 0x00B6 }, { "part", 0x2202 }, { "permil", 0x2030 },
{ "perp", 0x22A5 }, { "phi", 0x03C6 }, { "pi", 0x03C0 }, { "piv", 0x03D6 },
{ "plusmn", 0x00B1 }, { "pound", 0x00A3 }, { "prime", 0x2032 }, { "prod", 0x220F },
{ "prop", 0x221D }, { "psi", 0x03C8 }, { "quot", 0x0022 }, { "rArr", 0x21D2 },
{ "radic", 0x221A }, { "rang", 0x232A }, { "raquo", 0x00BB }, { "rarr", 0x2192 },
{ "rceil", 0x2309 }, { "rdquo", 0x201D }, { "real", 0x211C }, { "reg", 0x00AE },
{ "rfloor", 0x230B }, { "rho", 0x03C1 }, { "rlm", 0x200F }, { "rsaquo", 0x203A },
{ "rsquo", 0x2019 }, { "sbquo", 0x201A }, { "scaron", 0x0161 }, { "sdot", 0x22C5 },
{ "sect", 0x00A7 }, { "shy", 0x00AD }, { "sigma", 0x03C3 }, { "sigmaf", 0x03C2 },
{ "sim", 0x223C }, { "spades", 0x2660 }, { "sub", 0x2282 }, { "sube", 0x2286 },
{ "sum", 0x2211 }, { "sup", 0x2283 }, { "sup1", 0x00B9 }, { "sup2", 0x00B2 },
{ "sup3", 0x00B3 }, { "supe", 0x2287 }, { "szlig", 0x00DF }, { "tau", 0x03C4 },
{ "there4", 0x2234 }, { "theta", 0x03B8 }, { "thetasym", 0x03D1 }, { "thinsp", 0x2009 },
{ "thorn", 0x00FE }, { "tilde", 0x02DC }, { "times", 0x00D7 }, { "trade", 0x2122 },
{ "uArr", 0x21D1 }, { "uacute", 0x00FA }, { "uarr", 0x2191 }, { "ucirc", 0x00FB },
{ "ugrave", 0x00F9 }, { "uml", 0x00A8 }, { "upsih", 0x03D2 }, { "upsilon", 0x03C5 },
{ "uuml", 0x00FC }, { "weierp", 0x2118 }, { "xi", 0x03BE }, { "yacute", 0x00FD },
{ "yen", 0x00A5 }, { "yuml", 0x00FF }, { "zeta", 0x03B6 }, { "zwj", 0x200D },
{ "zwnj", 0x200C },
};
struct import_html_attr_t
{
import_html_attr_type_t type;
char *value;
};
struct import_html_tag_t
{
import_html_tag_type_t type;
bool is_terminator;
import_html_attr_t *attrs;
uint32_t attr_count;
};
struct import_html_stack_entry_t
{
import_html_tag_type_t type;
MCFieldCharacterStyle style;
};
struct import_html_t
{
// The target string.
MCField *field;
// The paragraph list we are building.
MCParagraph *paragraphs;
// The last run style.
MCFieldCharacterStyle last_used_style;
// The current style stack.
import_html_stack_entry_t *styles;
uint32_t style_index;
uint32_t style_capacity;
// If true, then we need a new paragraph before appending text.
bool need_paragraph;
bool preformatted;
uint32_t list_depth;
bool list_in_li;
uint32_t list_index;
uint32_t list_paragraph_count;
uint32_t list_starts[16];
uint8_t list_styles[16];
import_html_tag_type_t list_tags[16];
// The currently accumulated characters.
bool is_unicode;
uint8_t *bytes;
uint32_t byte_count;
uint32_t byte_capacity;
// If true, then the input html must be considered to be encoded in utf-8.
bool is_utf8;
};
static void import_html_copy_style(const MCFieldCharacterStyle& p_src, MCFieldCharacterStyle& r_dst)
{
r_dst = p_src;
if (p_src . has_link_text)
MCNameClone(p_src . link_text, r_dst . link_text);
if (p_src . has_image_source)
MCNameClone(p_src . image_source, r_dst . image_source);
if (p_src . has_metadata)
MCNameClone(p_src . metadata, r_dst . metadata);
if (p_src . has_text_font)
MCNameClone(p_src . text_font, r_dst . text_font);
}
static void import_html_free_style(MCFieldCharacterStyle& p_style)
{
MCNameDelete(p_style . link_text);
MCNameDelete(p_style . image_source);
MCNameDelete(p_style . metadata);
MCNameDelete(p_style . text_font);
}
static bool import_html_equal_style(const MCFieldCharacterStyle& left, const MCFieldCharacterStyle& right)
{
// MW-2012-09-19: [[ Bug 10399 ]] If either char style has an imageSource, then they
// are different.
return left . image_source == nil && right . image_source == nil && memcmp(&left, &right, sizeof(MCFieldCharacterStyle)) == 0;