-
Notifications
You must be signed in to change notification settings - Fork 429
/
Copy pathparser.c
6450 lines (5522 loc) · 200 KB
/
parser.c
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
/* parser.c -- HTML Parser
(c) 1998-2007 (W3C) MIT, ERCIM, Keio University
See tidy.h for the copyright notice.
*/
#include "tidy-int.h"
#include "lexer.h"
#include "parser.h"
#include "message.h"
#include "clean.h"
#include "tags.h"
#include "tmbstr.h"
#include "sprtf.h"
/****************************************************************************//*
** MARK: - Configuration Options
***************************************************************************/
/**
* Issue #72 - Need to know to avoid error-reporting - no warning only if
* --show-body-only yes.
* Issue #132 - Likewise avoid warning if showing body only.
*/
#define showingBodyOnly(doc) (cfgAutoBool(doc,TidyBodyOnly) == TidyYesState) ? yes : no
/****************************************************************************//*
** MARK: - Forward Declarations
***************************************************************************/
static Node* ParseXMLElement(TidyDocImpl* doc, Node *element, GetTokenMode mode);
/****************************************************************************//*
** MARK: - Node Operations
***************************************************************************/
/**
* Generalised search for duplicate elements.
* Issue #166 - repeated <main> element.
*/
static Bool findNodeWithId( Node *node, TidyTagId tid )
{
Node *content;
while (node)
{
if (TagIsId(node,tid))
return yes;
/*\
* Issue #459 - Under certain circumstances, with many node this use of
* 'for (content = node->content; content; content = content->content)'
* would produce a **forever** circle, or at least a very extended loop...
* It is sufficient to test the content, if it exists,
* to quickly iterate all nodes. Now all nodes are tested only once.
\*/
content = node->content;
if (content)
{
if ( findNodeWithId(content,tid) )
return yes;
}
node = node->next;
}
return no;
}
/**
* Perform a global search for an element.
* Issue #166 - repeated <main> element
*/
static Bool findNodeById( TidyDocImpl* doc, TidyTagId tid )
{
Node *node = (doc ? doc->root.content : NULL);
return findNodeWithId( node,tid );
}
/**
* Inserts node into element at an appropriate location based
* on the type of node being inserted.
*/
static Bool InsertMisc(Node *element, Node *node)
{
if (node->type == CommentTag ||
node->type == ProcInsTag ||
node->type == CDATATag ||
node->type == SectionTag ||
node->type == AspTag ||
node->type == JsteTag ||
node->type == PhpTag )
{
TY_(InsertNodeAtEnd)(element, node);
return yes;
}
if ( node->type == XmlDecl )
{
Node* root = element;
while ( root && root->parent )
root = root->parent;
if ( root && !(root->content && root->content->type == XmlDecl))
{
TY_(InsertNodeAtStart)( root, node );
return yes;
}
}
/* Declared empty tags seem to be slipping through
** the cracks. This is an experiment to figure out
** a decent place to pick them up.
*/
if ( node->tag &&
TY_(nodeIsElement)(node) &&
TY_(nodeCMIsEmpty)(node) && TagId(node) == TidyTag_UNKNOWN &&
(node->tag->versions & VERS_PROPRIETARY) != 0 )
{
TY_(InsertNodeAtEnd)(element, node);
return yes;
}
return no;
}
/**
* Insert "node" into markup tree in place of "element"
* which is moved to become the child of the node
*/
static void InsertNodeAsParent(Node *element, Node *node)
{
node->content = element;
node->last = element;
node->parent = element->parent;
element->parent = node;
if (node->parent->content == element)
node->parent->content = node;
if (node->parent->last == element)
node->parent->last = node;
node->prev = element->prev;
element->prev = NULL;
if (node->prev)
node->prev->next = node;
node->next = element->next;
element->next = NULL;
if (node->next)
node->next->prev = node;
}
/**
* Unexpected content in table row is moved to just before the table in
* in accordance with Netscape and IE. This code assumes that node hasn't
* been inserted into the row.
*/
static void MoveBeforeTable( TidyDocImpl* ARG_UNUSED(doc), Node *row,
Node *node )
{
Node *table;
/* first find the table element */
for (table = row->parent; table; table = table->parent)
{
if ( nodeIsTABLE(table) )
{
TY_(InsertNodeBeforeElement)( table, node );
return;
}
}
/* No table element */
TY_(InsertNodeBeforeElement)( row->parent, node );
}
/**
* Moves given node to end of body element.
*/
static void MoveNodeToBody( TidyDocImpl* doc, Node* node )
{
Node* body = TY_(FindBody)( doc );
if ( body )
{
TY_(RemoveNode)( node );
TY_(InsertNodeAtEnd)( body, node );
}
}
/**
* Move node to the head, where element is used as starting
* point in hunt for head. Normally called during parsing.
*/
static void MoveToHead( TidyDocImpl* doc, Node *element, Node *node )
{
Node *head = NULL;
TY_(RemoveNode)( node ); /* make sure that node is isolated */
if ( TY_(nodeIsElement)(node) )
{
TY_(Report)(doc, element, node, TAG_NOT_ALLOWED_IN );
head = TY_(FindHEAD)(doc);
assert(head != NULL);
TY_(InsertNodeAtEnd)(head, node);
if ( node->tag->parser )
{
/* Only one of the existing test cases as of 2021-08-14 invoke
MoveToHead, and it doesn't go deeper than one level. The
parser() call is supposed to return a node if additional
parsing is needed. Keep this in mind if we start to get bug
reports.
*/
Parser* parser = node->tag->parser;
parser( doc, node, IgnoreWhitespace );
}
}
else
{
TY_(Report)(doc, element, node, DISCARDING_UNEXPECTED);
TY_(FreeNode)( doc, node );
}
}
/***************************************************************************//*
** MARK: - Decision Making
***************************************************************************/
/**
* Indicates whether or not element can be pruned based on content,
* user settings, etc.
*/
static Bool CanPrune( TidyDocImpl* doc, Node *element )
{
if ( !cfgBool(doc, TidyDropEmptyElems) )
return no;
if ( TY_(nodeIsText)(element) )
return yes;
if ( element->content )
return no;
if ( element->tag == NULL )
return no;
if ( element->tag->model & CM_BLOCK && element->attributes != NULL )
return no;
if ( nodeIsA(element) && element->attributes != NULL )
return no;
if ( nodeIsP(element) && !cfgBool(doc, TidyDropEmptyParas) )
return no;
if ( element->tag->model & CM_ROW )
return no;
if ( element->tag->model & CM_EMPTY )
return no;
if ( nodeIsAPPLET(element) )
return no;
if ( nodeIsOBJECT(element) )
return no;
if ( nodeIsSCRIPT(element) && attrGetSRC(element) )
return no;
if ( nodeIsTITLE(element) )
return no;
/* #433359 - fix by Randy Waki 12 Mar 01 */
if ( nodeIsIFRAME(element) )
return no;
/* fix for bug 770297 */
if (nodeIsTEXTAREA(element))
return no;
/* fix for ISSUE #7 https://github.com/w3c/tidy-html5/issues/7 */
if (nodeIsCANVAS(element))
return no;
if (nodeIsPROGRESS(element))
return no;
if ( attrGetID(element) || attrGetNAME(element) )
return no;
/* fix for bug 695408; a better fix would look for unknown and */
/* known proprietary attributes that make the element significant */
if (attrGetDATAFLD(element))
return no;
/* fix for bug 723772, don't trim new-...-tags */
if (element->tag->id == TidyTag_UNKNOWN)
return no;
if (nodeIsBODY(element))
return no;
if (nodeIsCOLGROUP(element))
return no;
/* HTML5 - do NOT drop empty option if it has attributes */
if ( nodeIsOPTION(element) && element->attributes != NULL )
return no;
/* fix for #103 - don't drop empty dd tags lest document not validate */
if (nodeIsDD(element))
return no;
return yes;
}
/**
* Indicates whether or not node is a descendant of a tag of the given tid.
*/
static Bool DescendantOf( Node *element, TidyTagId tid )
{
Node *parent;
for ( parent = element->parent;
parent != NULL;
parent = parent->parent )
{
if ( TagIsId(parent, tid) )
return yes;
}
return no;
}
/**
* Indicates whether or not node is a descendant of a pre tag.
*/
static Bool IsPreDescendant(Node* node)
{
Node *parent = node->parent;
while (parent)
{
if (parent->tag && parent->tag->parser == TY_(ParsePre))
return yes;
parent = parent->parent;
}
return no;
}
/**
* Indicates whether or not the only content model for the given node
* is CM_INLINE.
*/
static Bool nodeCMIsOnlyInline( Node* node )
{
return TY_(nodeHasCM)( node, CM_INLINE ) && !TY_(nodeHasCM)( node, CM_BLOCK );
}
/**
* Indicates whether or not the content of the given node is acceptable
* content for pre elements
*/
static Bool PreContent( TidyDocImpl* ARG_UNUSED(doc), Node* node )
{
/* p is coerced to br's, Text OK too */
if ( nodeIsP(node) || TY_(nodeIsText)(node) )
return yes;
if ( node->tag == NULL ||
nodeIsPARAM(node) ||
!TY_(nodeHasCM)(node, CM_INLINE|CM_NEW) )
return no;
return yes;
}
/**
* Indicates whether or not leading whitespace should be cleaned.
*/
static Bool CleanLeadingWhitespace(TidyDocImpl* ARG_UNUSED(doc), Node* node)
{
if (!TY_(nodeIsText)(node))
return no;
if (node->parent->type == DocTypeTag)
return no;
if (IsPreDescendant(node))
return no;
if (node->parent->tag && node->parent->tag->parser == TY_(ParseScript))
return no;
/* #523, prevent blank spaces after script if the next item is script.
* This is actually more generalized as, if the preceding element is
* a body level script, then indicate that we want to clean leading
* whitespace.
*/
if ( node->prev && nodeIsSCRIPT(node->prev) && nodeIsBODY(node->prev->parent) )
return yes;
/* <p>...<br> <em>...</em>...</p> */
if (nodeIsBR(node->prev))
return yes;
/* <p> ...</p> */
if (node->prev == NULL && !TY_(nodeHasCM)(node->parent, CM_INLINE))
return yes;
/* <h4>...</h4> <em>...</em> */
if (node->prev && !TY_(nodeHasCM)(node->prev, CM_INLINE) &&
TY_(nodeIsElement)(node->prev))
return yes;
/* <p><span> ...</span></p> */
if (!node->prev && !node->parent->prev && !TY_(nodeHasCM)(node->parent->parent, CM_INLINE))
return yes;
return no;
}
/**
* Indicates whether or not trailing whitespace should be cleaned.
*/
static Bool CleanTrailingWhitespace(TidyDocImpl* doc, Node* node)
{
Node* next;
if (!TY_(nodeIsText)(node))
return no;
if (node->parent->type == DocTypeTag)
return no;
if (IsPreDescendant(node))
return no;
if (node->parent->tag && node->parent->tag->parser == TY_(ParseScript))
return no;
/* #523, prevent blank spaces after script if the next item is script.
* This is actually more generalized as, if the next element is
* a body level script, then indicate that we want to clean trailing
* whitespace.
*/
if ( node->next && nodeIsSCRIPT(node->next) && nodeIsBODY(node->next->parent) )
return yes;
next = node->next;
/* <p>... </p> */
if (!next && !TY_(nodeHasCM)(node->parent, CM_INLINE))
return yes;
/* <div><small>... </small><h3>...</h3></div> */
if (!next && node->parent->next && !TY_(nodeHasCM)(node->parent->next, CM_INLINE))
return yes;
if (!next)
return no;
if (nodeIsBR(next))
return yes;
if (TY_(nodeHasCM)(next, CM_INLINE))
return no;
/* <a href='/'>...</a> <p>...</p> */
if (next->type == StartTag)
return yes;
/* <strong>...</strong> <hr /> */
if (next->type == StartEndTag)
return yes;
/* evil adjacent text nodes, Tidy should not generate these :-( */
if (TY_(nodeIsText)(next) && next->start < next->end
&& TY_(IsWhite)(doc->lexer->lexbuf[next->start]))
return yes;
return no;
}
/***************************************************************************//*
** MARK: - Information Accumulation
***************************************************************************/
/**
* Errors in positioning of form start or end tags
* generally require human intervention to fix.
* Issue #166 - repeated <main> element also uses this flag
* to indicate duplicates, discarded.
*/
static void BadForm( TidyDocImpl* doc )
{
doc->badForm |= flg_BadForm;
}
/***************************************************************************//*
** MARK: - Fixes and Touchup
***************************************************************************/
/**
* Adds style information as a class in the document or a property
* of the node to prevent indentation of inferred UL tags.
*/
static void AddClassNoIndent( TidyDocImpl* doc, Node *node )
{
ctmbstr sprop =
"padding-left: 2ex; margin-left: 0ex"
"; margin-top: 0ex; margin-bottom: 0ex";
if ( !cfgBool(doc, TidyDecorateInferredUL) )
return;
if ( cfgBool(doc, TidyMakeClean) )
TY_(AddStyleAsClass)( doc, node, sprop );
else
TY_(AddStyleProperty)( doc, node, sprop );
}
/**
* Cleans whitespace from text nodes, and drops such nodes if emptied
* completely as a result.
*/
static void CleanSpaces(TidyDocImpl* doc, Node* node)
{
Stack *stack = TY_(newStack)(doc, 16);
Node *next;
while (node)
{
next = node->next;
if (TY_(nodeIsText)(node) && CleanLeadingWhitespace(doc, node))
while (node->start < node->end && TY_(IsWhite)(doc->lexer->lexbuf[node->start]))
++(node->start);
if (TY_(nodeIsText)(node) && CleanTrailingWhitespace(doc, node))
while (node->end > node->start && TY_(IsWhite)(doc->lexer->lexbuf[node->end - 1]))
--(node->end);
if (TY_(nodeIsText)(node) && !(node->start < node->end))
{
TY_(RemoveNode)(node);
TY_(FreeNode)(doc, node);
node = next ? next : TY_(pop)(stack);
continue;
}
if (node->content)
{
TY_(push)(stack, next);
node = node->content;
continue;
}
node = next ? next : TY_(pop)(stack);
}
TY_(freeStack)(stack, doc);
}
/**
* If a table row is empty then insert an empty cell. This practice is
* consistent with browser behavior and avoids potential problems with
* row spanning cells.
*/
static void FixEmptyRow(TidyDocImpl* doc, Node *row)
{
Node *cell;
if (row->content == NULL)
{
cell = TY_(InferredTag)(doc, TidyTag_TD);
TY_(InsertNodeAtEnd)(row, cell);
TY_(Report)(doc, row, cell, MISSING_STARTTAG);
}
}
/**
* The doctype has been found after other tags,
* and needs moving to before the html element
*/
static void InsertDocType( TidyDocImpl* doc, Node *element, Node *doctype )
{
Node* existing = TY_(FindDocType)( doc );
if ( existing )
{
TY_(Report)(doc, element, doctype, DISCARDING_UNEXPECTED );
TY_(FreeNode)( doc, doctype );
}
else
{
TY_(Report)(doc, element, doctype, DOCTYPE_AFTER_TAGS );
while ( !nodeIsHTML(element) )
element = element->parent;
TY_(InsertNodeBeforeElement)( element, doctype );
}
}
/**
* This maps
* <p>hello<em> world</em>
* to
* <p>hello <em>world</em>
*
* Trims initial space, by moving it before the
* start tag, or if this element is the first in
* parent's content, then by discarding the space
*/
static void TrimInitialSpace( TidyDocImpl* doc, Node *element, Node *text )
{
Lexer* lexer = doc->lexer;
Node *prev, *node;
if ( TY_(nodeIsText)(text) &&
lexer->lexbuf[text->start] == ' ' &&
text->start < text->end )
{
if ( (element->tag->model & CM_INLINE) &&
!(element->tag->model & CM_FIELD) )
{
prev = element->prev;
if (TY_(nodeIsText)(prev))
{
if (prev->end == 0 || lexer->lexbuf[prev->end - 1] != ' ')
lexer->lexbuf[(prev->end)++] = ' ';
++(element->start);
}
else /* create new node */
{
node = TY_(NewNode)(lexer->allocator, lexer);
node->start = (element->start)++;
node->end = element->start;
lexer->lexbuf[node->start] = ' ';
TY_(InsertNodeBeforeElement)(element ,node);
DEBUG_LOG(SPRTF("TrimInitialSpace: Created text node, inserted before <%s>\n",
(element->element ? element->element : "unknown")));
}
}
/* discard the space in current node */
++(text->start);
}
}
/**
* This maps
* <em>hello </em><strong>world</strong>
* to
* <em>hello</em> <strong>world</strong>
*
* If last child of element is a text node
* then trim trailing white space character
* moving it to after element's end tag.
*/
static void TrimTrailingSpace( TidyDocImpl* doc, Node *element, Node *last )
{
Lexer* lexer = doc->lexer;
byte c;
if (TY_(nodeIsText)(last))
{
if (last->end > last->start)
{
c = (byte) lexer->lexbuf[ last->end - 1 ];
if ( c == ' ' )
{
last->end -= 1;
if ( (element->tag->model & CM_INLINE) &&
!(element->tag->model & CM_FIELD) )
lexer->insertspace = yes;
}
}
}
}
/**
* Move initial and trailing space out.
* This routine maps:
* hello<em> world</em>
* to
* hello <em>world</em>
* and
* <em>hello </em><strong>world</strong>
* to
* <em>hello</em> <strong>world</strong>
*/
static void TrimSpaces( TidyDocImpl* doc, Node *element)
{
Node* text = element->content;
if (nodeIsPRE(element) || IsPreDescendant(element))
return;
if (TY_(nodeIsText)(text))
TrimInitialSpace(doc, element, text);
text = element->last;
if (TY_(nodeIsText)(text))
TrimTrailingSpace(doc, element, text);
}
/***************************************************************************//*
** MARK: - Parsers Support
***************************************************************************/
/**
* Structure used by FindDescendant_cb.
*/
struct MatchingDescendantData
{
Node *found_node;
Bool *passed_marker_node;
/* input: */
TidyTagId matching_tagId;
Node *node_to_find;
Node *marker_node;
};
/**
* The main engine for FindMatchingDescendant.
*/
static NodeTraversalSignal FindDescendant_cb(TidyDocImpl* ARG_UNUSED(doc), Node* node, void *propagate)
{
struct MatchingDescendantData *cb_data = (struct MatchingDescendantData *)propagate;
if (TagId(node) == cb_data->matching_tagId)
{
/* make sure we match up 'unknown' tags exactly! */
if (cb_data->matching_tagId != TidyTag_UNKNOWN ||
(node->element != NULL &&
cb_data->node_to_find != NULL &&
cb_data->node_to_find->element != NULL &&
0 == TY_(tmbstrcmp)(cb_data->node_to_find->element, node->element)))
{
cb_data->found_node = node;
return ExitTraversal;
}
}
if (cb_data->passed_marker_node && node == cb_data->marker_node)
*cb_data->passed_marker_node = yes;
return VisitParent;
}
/**
* Search the parent chain (from `parent` upwards up to the root) for a node
* matching the given 'node'.
*
* When the search passes beyond the `marker_node` (which is assumed to sit
* in the parent chain), this will be flagged by setting the boolean
* referenced by `is_parent_of_marker` to `yes`.
*
* 'is_parent_of_marker' and 'marker_node' are optional parameters and may
* be NULL.
*/
static Node *FindMatchingDescendant( Node *parent, Node *node, Node *marker_node, Bool *is_parent_of_marker )
{
struct MatchingDescendantData cb_data = { 0 };
cb_data.matching_tagId = TagId(node);
cb_data.node_to_find = node;
cb_data.marker_node = marker_node;
assert(node);
if (is_parent_of_marker)
*is_parent_of_marker = no;
TY_(TraverseNodeTree)(NULL, parent, FindDescendant_cb, &cb_data);
return cb_data.found_node;
}
/**
* Finds the last list item for the given list, providing it in the
* in-out parameter. Returns yes or no if the item was the last list
* item.
*/
static Bool FindLastLI( Node *list, Node **lastli )
{
Node *node;
*lastli = NULL;
for ( node = list->content; node ; node = node->next )
if ( nodeIsLI(node) && node->type == StartTag )
*lastli=node;
return *lastli ? yes:no;
}
/***************************************************************************//*
** MARK: - Parser Stack
***************************************************************************/
/**
* Allocates and initializes the parser's stack.
*/
void TY_(InitParserStack)( TidyDocImpl* doc )
{
enum { default_size = 32 };
TidyParserMemory *content = (TidyParserMemory *) TidyAlloc( doc->allocator, sizeof(TidyParserMemory) * default_size );
doc->stack.content = content;
doc->stack.size = default_size;
doc->stack.top = -1;
}
/**
* Frees the parser's stack when done.
*/
void TY_(FreeParserStack)( TidyDocImpl* doc )
{
TidyFree( doc->allocator, doc->stack.content );
doc->stack.content = NULL;
doc->stack.size = 0;
doc->stack.top = -1;
}
/**
* Increase the stack size.
*/
static void growParserStack( TidyDocImpl* doc )
{
TidyParserMemory *content;
content = (TidyParserMemory *) TidyAlloc( doc->allocator, sizeof(TidyParserMemory) * doc->stack.size * 2 );
memcpy( content, doc->stack.content, sizeof(TidyParserMemory) * (doc->stack.top + 1) );
TidyFree(doc->allocator, doc->stack.content);
doc->stack.content = content;
doc->stack.size = doc->stack.size * 2;
}
/**
* Indicates whether or not the stack is empty.
*/
Bool TY_(isEmptyParserStack)( TidyDocImpl* doc )
{
return doc->stack.top < 0;
}
/**
* Peek at the parser memory.
*/
TidyParserMemory TY_(peekMemory)( TidyDocImpl* doc )
{
return doc->stack.content[doc->stack.top];
}
/**
* Peek at the parser memory "identity" field. This is just a convenience
* to avoid having to create a new struct instance in the caller.
*/
Parser* TY_(peekMemoryIdentity)( TidyDocImpl* doc )
{
return doc->stack.content[doc->stack.top].identity;
}
/**
* Peek at the parser memory "mode" field. This is just a convenience
* to avoid having to create a new struct instance in the caller.
*/
GetTokenMode TY_(peekMemoryMode)( TidyDocImpl* doc )
{
return doc->stack.content[doc->stack.top].mode;
}
/**
* Pop out a parser memory.
*/
TidyParserMemory TY_(popMemory)( TidyDocImpl* doc )
{
if ( !TY_(isEmptyParserStack)( doc ) )
{
TidyParserMemory data = doc->stack.content[doc->stack.top];
DEBUG_LOG(SPRTF("\n"
"<--POP original: %s @ %p\n"
" reentry: %s @ %p\n"
" stack depth: %lu @ %p\n"
" mode: %u\n"
" register 1: %i\n"
" register 2: %i\n\n",
data.original_node ? data.original_node->element : "none", data.original_node,
data.reentry_node ? data.reentry_node->element : "none", data.reentry_node,
doc->stack.top, &doc->stack.content[doc->stack.top],
data.mode,
data.register_1,
data.register_2
));
doc->stack.top = doc->stack.top - 1;
return data;
}
TidyParserMemory blank = { NULL };
return blank;
}
/**
* Push the parser memory to the stack.
*/
void TY_(pushMemory)( TidyDocImpl* doc, TidyParserMemory data )
{
if ( doc->stack.top == doc->stack.size - 1 )
growParserStack( doc );
doc->stack.top++;
doc->stack.content[doc->stack.top] = data;
DEBUG_LOG(SPRTF("\n"
"-->PUSH original: %s @ %p\n"
" reentry: %s @ %p\n"
" stack depth: %lu @ %p\n"
" mode: %u\n"
" register 1: %i\n"
" register 2: %i\n\n",
data.original_node ? data.original_node->element : "none", data.original_node,
data.reentry_node ? data.reentry_node->element : "none", data.reentry_node,
doc->stack.top, &doc->stack.content[doc->stack.top],
data.mode,
data.register_1,
data.register_2
));
}
/***************************************************************************//*
** MARK: Convenience Logging Macros
***************************************************************************/
#if defined(ENABLE_DEBUG_LOG)
# define DEBUG_LOG_COUNTERS \
static int depth_parser = 0;\
static int count_parser = 0;\
int old_mode = IgnoreWhitespace;
# define DEBUG_LOG_GET_OLD_MODE old_mode = mode;
# define DEBUG_LOG_REENTER_WITH_NODE(NODE) SPRTF("\n>>>Re-Enter %s-%u with '%s', +++mode: %u, depth: %d, cnt: %d\n", __FUNCTION__, __LINE__, NODE->element, mode, ++depth_parser, ++count_parser);
# define DEBUG_LOG_ENTER_WITH_NODE(NODE) SPRTF("\n>>>Enter %s-%u with '%s', +++mode: %u, depth: %d, cnt: %d\n", __FUNCTION__, __LINE__, NODE->element, mode, ++depth_parser, ++count_parser);
# define DEBUG_LOG_CHANGE_MODE SPRTF("+++%s-%u Changing mode to %u (was %u)\n", __FUNCTION__, __LINE__, mode, old_mode);
# define DEBUG_LOG_GOT_TOKEN(NODE) SPRTF("---%s-%u got token '%s' with mode '%u'.\n", __FUNCTION__, __LINE__, NODE ? NODE->element : NULL, mode);
# define DEBUG_LOG_EXIT_WITH_NODE(NODE) SPRTF("<<<Exit %s-%u with a node to parse: '%s', depth: %d\n", __FUNCTION__, __LINE__, NODE->element, depth_parser--);
# define DEBUG_LOG_EXIT SPRTF("<<<Exit %s-%u, depth: %d\n", __FUNCTION__, __LINE__, depth_parser--);
#else
# define DEBUG_LOG_COUNTERS
# define DEBUG_LOG_GET_OLD_MODE
# define DEBUG_LOG_REENTER_WITH_NODE(NODE)
# define DEBUG_LOG_ENTER_WITH_NODE(NODE)
# define DEBUG_LOG_CHANGE_MODE