Coverage Report

Created: 2026-12-01 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tidy-html5/src/tidylib.c
Line
Count
Source
1
/* tidylib.c -- internal library definitions
2
3
  (c) 1998-2008 (W3C) MIT, ERCIM, Keio University
4
  See tidy.h for the copyright notice.
5
6
  Defines HTML Tidy API implemented by tidy library.
7
8
  Very rough initial cut for discussion purposes.
9
10
  Public interface is const-correct and doesn't explicitly depend
11
  on any globals.  Thus, thread-safety may be introduced w/out
12
  changing the interface.
13
14
  Looking ahead to a C++ wrapper, C functions always pass
15
  this-equivalent as 1st arg.
16
17
  Created 2001-05-20 by Charles Reitzel
18
19
*/
20
21
#include <errno.h>
22
23
#include "tidy-int.h"
24
#include "parser.h"
25
#include "clean.h"
26
#include "gdoc.h"
27
#include "config.h"
28
#include "message.h"
29
#include "messageobj.h"
30
#include "pprint.h"
31
#include "entities.h"
32
#include "tmbstr.h"
33
#include "utf8.h"
34
#include "mappedio.h"
35
#include "language.h"
36
#include "attrs.h"
37
#include "sprtf.h"
38
#if SUPPORT_LOCALIZATIONS
39
#  include "stdlib.h"
40
#  include "locale.h"
41
#endif
42
43
/* Create/Destroy a Tidy "document" object */
44
static TidyDocImpl* tidyDocCreate( TidyAllocator *allocator );
45
static void         tidyDocRelease( TidyDocImpl* impl );
46
47
static int          tidyDocStatus( TidyDocImpl* impl );
48
49
/* Parse Markup */
50
static int          tidyDocParseFile( TidyDocImpl* impl, ctmbstr htmlfil );
51
static int          tidyDocParseStdin( TidyDocImpl* impl );
52
static int          tidyDocParseString( TidyDocImpl* impl, ctmbstr content );
53
static int          tidyDocParseBuffer( TidyDocImpl* impl, TidyBuffer* inbuf );
54
static int          tidyDocParseSource( TidyDocImpl* impl, TidyInputSource* docIn );
55
56
57
/* Execute post-parse diagnostics and cleanup.
58
** Note, the order is important.  You will get different
59
** results from the diagnostics depending on if they are run
60
** pre-or-post repair.
61
*/
62
static int          tidyDocRunDiagnostics( TidyDocImpl* doc );
63
static void         tidyDocReportDoctype( TidyDocImpl* doc );
64
static int          tidyDocCleanAndRepair( TidyDocImpl* doc );
65
66
67
/* Save cleaned up file to file/buffer/sink */
68
static int          tidyDocSaveFile( TidyDocImpl* impl, ctmbstr htmlfil );
69
static int          tidyDocSaveStdout( TidyDocImpl* impl );
70
static int          tidyDocSaveString( TidyDocImpl* impl, tmbstr buffer, uint* buflen );
71
static int          tidyDocSaveBuffer( TidyDocImpl* impl, TidyBuffer* outbuf );
72
static int          tidyDocSaveSink( TidyDocImpl* impl, TidyOutputSink* docOut );
73
static int          tidyDocSaveStream( TidyDocImpl* impl, StreamOut* out );
74
75
76
/* Tidy public interface
77
**
78
** Most functions return an integer:
79
**
80
** 0    -> SUCCESS
81
** >0   -> WARNING
82
** <0   -> ERROR
83
**
84
*/
85
86
TidyDoc TIDY_CALL       tidyCreate(void)
87
22.1k
{
88
22.1k
  TidyDocImpl* impl = tidyDocCreate( &TY_(g_default_allocator) );
89
22.1k
  return tidyImplToDoc( impl );
90
22.1k
}
91
92
TidyDoc TIDY_CALL tidyCreateWithAllocator( TidyAllocator *allocator )
93
0
{
94
0
  TidyDocImpl* impl = tidyDocCreate( allocator );
95
0
  return tidyImplToDoc( impl );
96
0
}
97
98
void TIDY_CALL          tidyRelease( TidyDoc tdoc )
99
22.1k
{
100
22.1k
  TidyDocImpl* impl = tidyDocToImpl( tdoc );
101
22.1k
  tidyDocRelease( impl );
102
22.1k
}
103
104
TidyDocImpl* tidyDocCreate( TidyAllocator *allocator )
105
22.1k
{
106
22.1k
    TidyDocImpl* doc = (TidyDocImpl*)TidyAlloc( allocator, sizeof(TidyDocImpl) );
107
22.1k
    TidyClearMemory( doc, sizeof(*doc) );
108
22.1k
    doc->allocator = allocator;
109
110
22.1k
    TY_(InitMap)();
111
22.1k
    TY_(InitTags)( doc );
112
22.1k
    TY_(InitAttrs)( doc );
113
22.1k
    TY_(InitConfig)( doc );
114
22.1k
    TY_(InitPrintBuf)( doc );
115
22.1k
    TY_(InitParserStack)( doc );
116
117
    /* Set the locale for tidy's output. This both configures
118
    ** LibTidy to use the environment's locale as well as the
119
    ** standard library.
120
    */
121
22.1k
#if SUPPORT_LOCALIZATIONS
122
22.1k
    if ( TY_(tidyGetLanguageSetByUser)() == no )
123
22.1k
    {
124
22.1k
        if( ! TY_(tidySetLanguage)( getenv( "LC_MESSAGES" ) ) )
125
22.1k
        {
126
22.1k
            if( ! TY_(tidySetLanguage)( getenv( "LANG" ) ) )
127
22.1k
            {
128
                /*\
129
                *  Is. #770 #783 #780 #790 and maybe others -
130
                *  TY_(tidySetLanguage)( setlocale( LC_ALL, "" ) );
131
                *  this seems a 'bad' choice!
132
               \*/
133
22.1k
            }
134
22.1k
        }
135
22.1k
    }
136
22.1k
#endif
137
138
    /* By default, wire tidy messages to standard error.
139
    ** Document input will be set by parsing routines.
140
    ** Document output will be set by pretty print routines.
141
    ** Config input will be set by config parsing routines.
142
    ** But we need to start off with a way to report errors.
143
    */
144
22.1k
    doc->errout = TY_(StdErrOutput)();
145
22.1k
    return doc;
146
22.1k
}
147
148
void          tidyDocRelease( TidyDocImpl* doc )
149
22.1k
{
150
    /* doc in/out opened and closed by parse/print routines */
151
22.1k
    if ( doc )
152
22.1k
    {
153
22.1k
        assert( doc->docIn == NULL );
154
22.1k
        assert( doc->docOut == NULL );
155
156
22.1k
        TY_(ReleaseStreamOut)( doc, doc->errout );
157
22.1k
        doc->errout = NULL;
158
159
22.1k
        TY_(FreePrintBuf)( doc );
160
22.1k
        TY_(FreeNode)(doc, &doc->root);
161
22.1k
        TidyClearMemory(&doc->root, sizeof(Node));
162
163
22.1k
        if (doc->givenDoctype)
164
22.1k
            TidyDocFree(doc, doc->givenDoctype);
165
166
22.1k
        TY_(FreeConfig)( doc );
167
22.1k
        TY_(FreeAttrTable)( doc );
168
22.1k
        TY_(FreeAttrPriorityList)( doc );
169
22.1k
        TY_(FreeMutedMessageList( doc ));
170
22.1k
        TY_(FreeTags)( doc );
171
        /*\ 
172
         *  Issue #186 - Now FreeNode depend on the doctype, so the lexer is needed
173
         *  to determine which hash is to be used, so free it last.
174
        \*/
175
22.1k
        TY_(FreeLexer)( doc );
176
22.1k
        TY_(FreeParserStack)( doc );
177
22.1k
        TidyDocFree( doc, doc );
178
22.1k
    }
179
22.1k
}
180
181
/* Let application store a chunk of data w/ each Tidy tdocance.
182
** Useful for callbacks.
183
*/
184
void TIDY_CALL        tidySetAppData( TidyDoc tdoc, void* appData )
185
0
{
186
0
  TidyDocImpl* impl = tidyDocToImpl( tdoc );
187
0
  if ( impl )
188
0
    impl->appData = appData;
189
0
}
190
void* TIDY_CALL       tidyGetAppData( TidyDoc tdoc )
191
0
{
192
0
  TidyDocImpl* impl = tidyDocToImpl( tdoc );
193
0
  if ( impl )
194
0
    return impl->appData;
195
0
  return NULL;
196
0
}
197
198
ctmbstr TIDY_CALL     tidyReleaseDate(void)
199
0
{
200
0
    return TY_(ReleaseDate)();
201
0
}
202
203
ctmbstr TIDY_CALL     tidyLibraryVersion(void)
204
22.1k
{
205
22.1k
    return TY_(tidyLibraryVersion)();
206
22.1k
}
207
208
ctmbstr TIDY_CALL     tidyPlatform(void)
209
0
{
210
0
#ifdef PLATFORM_NAME
211
0
    return PLATFORM_NAME;
212
#else
213
    return NULL;
214
#endif
215
0
}
216
217
218
/* Get/set configuration options
219
*/
220
Bool TIDY_CALL     tidySetOptionCallback( TidyDoc tdoc, TidyOptCallback pOptCallback )
221
0
{
222
0
  TidyDocImpl* impl = tidyDocToImpl( tdoc );
223
0
  if ( impl )
224
0
  {
225
0
    impl->pOptCallback = pOptCallback;
226
0
    return yes;
227
0
  }
228
0
  return no;
229
0
}
230
231
Bool TIDY_CALL     tidySetConfigCallback(TidyDoc tdoc, TidyConfigCallback pConfigCallback)
232
0
{
233
0
  TidyDocImpl* impl = tidyDocToImpl( tdoc );
234
0
  if ( impl )
235
0
  {
236
0
    impl->pConfigCallback = pConfigCallback;
237
0
    return yes;
238
0
  }
239
0
  return no;
240
0
}
241
242
Bool TIDY_CALL    tidySetConfigChangeCallback(TidyDoc tdoc, TidyConfigChangeCallback pCallback)
243
0
{
244
0
  TidyDocImpl* impl = tidyDocToImpl( tdoc );
245
0
  if ( impl )
246
0
  {
247
0
    impl->pConfigChangeCallback = pCallback;
248
0
    return yes;
249
0
  }
250
0
  return no;
251
0
}
252
253
254
255
int TIDY_CALL     tidyLoadConfig( TidyDoc tdoc, ctmbstr cfgfil )
256
0
{
257
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
258
0
    if ( impl )
259
0
        return TY_(ParseConfigFile)( impl, cfgfil );
260
0
    return -EINVAL;
261
0
}
262
263
int TIDY_CALL     tidyLoadConfigEnc( TidyDoc tdoc, ctmbstr cfgfil, ctmbstr charenc )
264
0
{
265
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
266
0
    if ( impl )
267
0
        return TY_(ParseConfigFileEnc)( impl, cfgfil, charenc );
268
0
    return -EINVAL;
269
0
}
270
271
int TIDY_CALL         tidySetCharEncoding( TidyDoc tdoc, ctmbstr encnam )
272
0
{
273
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
274
0
    if ( impl )
275
0
    {
276
0
        int enc = TY_(CharEncodingId)( impl, encnam );
277
0
        if ( enc >= 0 && TY_(AdjustCharEncoding)(impl, enc) )
278
0
            return 0;
279
280
0
        TY_(ReportBadArgument)( impl, "char-encoding" );
281
0
    }
282
0
    return -EINVAL;
283
0
}
284
285
int TIDY_CALL           tidySetInCharEncoding( TidyDoc tdoc, ctmbstr encnam )
286
0
{
287
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
288
0
    if ( impl )
289
0
    {
290
0
        int enc = TY_(CharEncodingId)( impl, encnam );
291
0
        if ( enc >= 0 && TY_(SetOptionInt)( impl, TidyInCharEncoding, enc ) )
292
0
            return 0;
293
294
0
        TY_(ReportBadArgument)( impl, "in-char-encoding" );
295
0
    }
296
0
    return -EINVAL;
297
0
}
298
299
int TIDY_CALL           tidySetOutCharEncoding( TidyDoc tdoc, ctmbstr encnam )
300
0
{
301
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
302
0
    if ( impl )
303
0
    {
304
0
        int enc = TY_(CharEncodingId)( impl, encnam );
305
0
        if ( enc >= 0 && TY_(SetOptionInt)( impl, TidyOutCharEncoding, enc ) )
306
0
            return 0;
307
308
0
        TY_(ReportBadArgument)( impl, "out-char-encoding" );
309
0
    }
310
0
    return -EINVAL;
311
0
}
312
313
TidyOptionId TIDY_CALL tidyOptGetIdForName( ctmbstr optnam )
314
0
{
315
0
    const TidyOptionImpl* option = TY_(lookupOption)( optnam );
316
0
    if ( option )
317
0
        return option->id;
318
0
    return N_TIDY_OPTIONS;  /* Error */
319
0
}
320
321
TidyIterator TIDY_CALL  tidyGetOptionList( TidyDoc tdoc )
322
0
{
323
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
324
0
    if ( impl )
325
0
        return TY_(getOptionList)( impl );
326
0
    return (TidyIterator) -1;
327
0
}
328
329
TidyOption TIDY_CALL    tidyGetNextOption( TidyDoc tdoc, TidyIterator* pos )
330
0
{
331
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
332
0
    const TidyOptionImpl* option = NULL;
333
0
    if ( impl )
334
0
        option = TY_(getNextOption)( impl, pos );
335
0
    else if ( pos )
336
0
        *pos = 0;
337
0
    return tidyImplToOption( option );
338
0
}
339
340
341
TidyOption TIDY_CALL    tidyGetOption( TidyDoc ARG_UNUSED(tdoc), TidyOptionId optId )
342
0
{
343
0
    const TidyOptionImpl* option = TY_(getOption)( optId );
344
0
    return tidyImplToOption( option );
345
0
}
346
TidyOption TIDY_CALL    tidyGetOptionByName( TidyDoc ARG_UNUSED(doc), ctmbstr optnam )
347
0
{
348
0
    const TidyOptionImpl* option = TY_(lookupOption)( optnam );
349
0
    return tidyImplToOption( option );
350
0
}
351
352
TidyOptionId TIDY_CALL  tidyOptGetId( TidyOption topt )
353
0
{
354
0
    const TidyOptionImpl* option = tidyOptionToImpl( topt );
355
0
    if ( option )
356
0
        return option->id;
357
0
    return N_TIDY_OPTIONS;
358
0
}
359
ctmbstr TIDY_CALL       tidyOptGetName( TidyOption topt )
360
0
{
361
0
    const TidyOptionImpl* option = tidyOptionToImpl( topt );
362
0
    if ( option )
363
0
        return option->name;
364
0
    return NULL;
365
0
}
366
TidyOptionType TIDY_CALL tidyOptGetType( TidyOption topt )
367
0
{
368
0
    const TidyOptionImpl* option = tidyOptionToImpl( topt );
369
0
    if ( option )
370
0
        return option->type;
371
0
    return (TidyOptionType) -1;
372
0
}
373
Bool TIDY_CALL           tidyOptionIsList( TidyOption opt )
374
0
{
375
0
    const TidyOptionImpl* option = tidyOptionToImpl( opt );
376
0
    if ( option )
377
0
        return TY_(getOptionIsList)( option->id );
378
0
    return no;
379
0
}
380
TidyConfigCategory TIDY_CALL tidyOptGetCategory( TidyOption topt )
381
0
{
382
0
    const TidyOptionImpl* option = tidyOptionToImpl( topt );
383
0
    if ( option )
384
0
        return option->category;
385
0
    return (TidyConfigCategory) -1;
386
0
}
387
ctmbstr TIDY_CALL       tidyOptGetDefault( TidyOption topt )
388
0
{
389
0
    const TidyOptionImpl* option = tidyOptionToImpl( topt );
390
    /* Special case for TidyDoctype, because it is declared as string */
391
0
    if ( option && option->id == TidyDoctype )
392
0
    {
393
0
        const TidyOptionImpl* newopt = TY_(getOption)( TidyDoctypeMode );
394
0
        return TY_(GetPickListLabelForPick)( TidyDoctypeMode, newopt->dflt );
395
0
    }
396
0
    if ( option && option->type == TidyString )
397
0
        return option->pdflt; /* Issue #306 - fix an old typo hidden by a cast! */
398
0
    return NULL;
399
0
}
400
ulong TIDY_CALL          tidyOptGetDefaultInt( TidyOption topt )
401
0
{
402
0
    const TidyOptionImpl* option = tidyOptionToImpl( topt );
403
0
    if ( option && option->type != TidyString )
404
0
        return option->dflt;
405
406
    /* Special case for TidyDoctype, because it has a picklist */
407
0
    if ( option->id == TidyDoctype )
408
0
    {
409
0
        const TidyOptionImpl* newopt = TY_(getOption)( TidyDoctypeMode );
410
0
        return newopt->dflt;
411
0
    }
412
413
0
    return ~0U;
414
0
}
415
Bool TIDY_CALL          tidyOptGetDefaultBool( TidyOption topt )
416
0
{
417
0
    const TidyOptionImpl* option = tidyOptionToImpl( topt );
418
0
    if ( option && option->type != TidyString )
419
0
        return ( option->dflt ? yes : no );
420
0
    return no;
421
0
}
422
Bool TIDY_CALL          tidyOptIsReadOnly( TidyOption topt )
423
0
{
424
0
    const TidyOptionImpl* option = tidyOptionToImpl( topt );
425
0
    if ( option  )
426
0
        return ( option->parser == NULL );
427
0
    return yes;
428
0
}
429
430
431
TidyIterator TIDY_CALL  tidyOptGetPickList( TidyOption topt )
432
0
{
433
0
    const TidyOptionImpl* option = tidyOptionToImpl( topt );
434
0
    if ( option )
435
0
      return TY_(getOptionPickList)( option );
436
0
    return (TidyIterator) -1;
437
0
}
438
ctmbstr TIDY_CALL       tidyOptGetNextPick( TidyOption topt, TidyIterator* pos )
439
0
{
440
0
    const TidyOptionImpl* option = tidyOptionToImpl( topt );
441
0
    if ( option )
442
0
        return TY_(getNextOptionPick)( option, pos );
443
0
    return NULL;
444
0
}
445
446
447
ctmbstr TIDY_CALL       tidyOptGetValue( TidyDoc tdoc, TidyOptionId optId )
448
0
{
449
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
450
0
    ctmbstr optval = NULL;
451
0
    if ( impl )
452
0
    {
453
0
        if ( optId == TidyDoctype )
454
0
        {
455
            /* Special case for TidyDoctype, because it has a picklist and is a string. */
456
0
            uint pick = tidyOptGetInt( tdoc, TidyDoctypeMode );
457
0
            if ( pick != TidyDoctypeUser )
458
0
            {
459
0
                optval = TY_(GetPickListLabelForPick)( TidyDoctypeMode, pick );
460
0
            } else {
461
0
                optval = cfgStr( impl, optId );
462
0
            }
463
0
        } else {
464
            /* Standard case. */
465
0
            optval = cfgStr( impl, optId );
466
0
        }
467
0
    }
468
0
    return optval;
469
0
}
470
Bool TIDY_CALL        tidyOptSetValue( TidyDoc tdoc, TidyOptionId optId, ctmbstr val )
471
0
{
472
0
  TidyDocImpl* impl = tidyDocToImpl( tdoc );
473
0
  if ( impl )
474
0
    return TY_(ParseConfigValue)( impl, optId, val );
475
0
  return no;
476
0
}
477
Bool TIDY_CALL        tidyOptParseValue( TidyDoc tdoc, ctmbstr optnam, ctmbstr val )
478
0
{
479
0
  TidyDocImpl* impl = tidyDocToImpl( tdoc );
480
0
  if ( impl )
481
0
    return TY_(ParseConfigOption)( impl, optnam, val );
482
0
  return no;
483
0
}
484
485
ulong TIDY_CALL        tidyOptGetInt( TidyDoc tdoc, TidyOptionId optId )
486
0
{
487
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
488
0
    ulong opti = 0;
489
0
    if ( impl )
490
0
    {
491
        /* Special case for TidyDoctype, because it has a picklist */
492
0
        if ( optId == TidyDoctype )
493
0
            opti = cfg( impl, TidyDoctypeMode);
494
0
        else
495
0
            opti = cfg( impl, optId );
496
0
    }
497
0
    return opti;
498
0
}
499
500
Bool TIDY_CALL        tidyOptSetInt( TidyDoc tdoc, TidyOptionId optId, ulong val )
501
0
{
502
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
503
0
    if ( impl )
504
0
    {
505
        /* Special case for TidyDoctype, because it has a picklist */
506
0
        if ( optId == TidyDoctype )
507
0
            return TY_(SetOptionInt)( impl, TidyDoctypeMode, val );
508
0
        else
509
0
            return TY_(SetOptionInt)( impl, optId, val );
510
0
    }
511
0
    return no;
512
0
}
513
514
Bool TIDY_CALL         tidyOptGetBool( TidyDoc tdoc, TidyOptionId optId )
515
0
{
516
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
517
0
    Bool optb = no;
518
0
    if ( impl )
519
0
    {
520
0
        const TidyOptionImpl* option = TY_(getOption)( optId );
521
0
        if ( option )
522
0
        {
523
0
            optb = cfgBool( impl, optId );
524
0
        }
525
0
    }
526
0
    return optb;
527
0
}
528
529
Bool TIDY_CALL        tidyOptSetBool( TidyDoc tdoc, TidyOptionId optId, Bool val )
530
44.2k
{
531
44.2k
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
532
44.2k
    if ( impl )
533
44.2k
        return TY_(SetOptionBool)( impl, optId, val );
534
0
    return no;
535
44.2k
}
536
537
ctmbstr TIDY_CALL       tidyOptGetEncName( TidyDoc tdoc, TidyOptionId optId )
538
0
{
539
0
  uint enc = tidyOptGetInt( tdoc, optId );
540
0
  return TY_(CharEncodingOptName)( enc );
541
0
}
542
543
ctmbstr TIDY_CALL       tidyOptGetCurrPick( TidyDoc tdoc, TidyOptionId optId )
544
0
{
545
0
    uint pick = tidyOptGetInt( tdoc, optId );
546
0
    return TY_(GetPickListLabelForPick)( optId, pick );
547
0
}
548
549
550
TidyIterator TIDY_CALL tidyOptGetDeclTagList( TidyDoc tdoc )
551
0
{
552
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
553
0
    TidyIterator declIter = 0;
554
0
    if ( impl )
555
0
        declIter = TY_(GetDeclaredTagList)( impl );
556
0
    return declIter;
557
0
}
558
559
ctmbstr TIDY_CALL       tidyOptGetNextDeclTag( TidyDoc tdoc, TidyOptionId optId,
560
                                     TidyIterator* iter )
561
0
{
562
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
563
0
    ctmbstr tagnam = NULL;
564
0
    if ( impl )
565
0
    {
566
0
        UserTagType tagtyp = tagtype_null;
567
0
        if ( optId == TidyInlineTags )
568
0
            tagtyp = tagtype_inline;
569
0
        else if ( optId == TidyBlockTags )
570
0
            tagtyp = tagtype_block;
571
0
        else if ( optId == TidyEmptyTags )
572
0
            tagtyp = tagtype_empty;
573
0
        else if ( optId == TidyPreTags )
574
0
            tagtyp = tagtype_pre;
575
0
        if ( tagtyp != tagtype_null )
576
0
            tagnam = TY_(GetNextDeclaredTag)( impl, tagtyp, iter );
577
0
    }
578
0
    return tagnam;
579
0
}
580
581
TidyIterator TIDY_CALL tidyOptGetPriorityAttrList( TidyDoc tdoc )
582
0
{
583
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
584
0
    if ( impl )
585
0
        return TY_(getPriorityAttrList)( impl );
586
0
    return (TidyIterator) -1;
587
0
}
588
589
ctmbstr TIDY_CALL      tidyOptGetNextPriorityAttr(TidyDoc tdoc, TidyIterator* iter )
590
0
{
591
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
592
0
    ctmbstr result = NULL;
593
0
    if ( impl )
594
0
        result = TY_(getNextPriorityAttr)( impl, iter );
595
0
    else if ( iter )
596
0
        *iter = 0;
597
0
    return result;
598
0
}
599
600
TidyIterator TIDY_CALL tidyOptGetMutedMessageList( TidyDoc tdoc )
601
0
{
602
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
603
0
    if ( impl )
604
0
        return TY_(getMutedMessageList)( impl );
605
0
    return (TidyIterator) -1;
606
0
}
607
608
ctmbstr TIDY_CALL      tidyOptGetNextMutedMessage(TidyDoc tdoc, TidyIterator* iter )
609
0
{
610
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
611
0
    ctmbstr result = NULL;
612
0
    if ( impl )
613
0
        result = TY_(getNextMutedMessage)( impl, iter );
614
0
    else if ( iter )
615
0
        *iter = 0;
616
0
    return result;
617
0
}
618
619
ctmbstr TIDY_CALL tidyOptGetDoc( TidyDoc ARG_UNUSED(tdoc), TidyOption opt )
620
0
{
621
0
    const TidyOptionId optId = tidyOptGetId( opt );
622
0
    return tidyLocalizedString(optId);
623
0
}
624
625
#if SUPPORT_CONSOLE_APP
626
/* TODO - GROUP ALL CONSOLE-ONLY FUNCTIONS */
627
TidyIterator TIDY_CALL tidyOptGetDocLinksList( TidyDoc ARG_UNUSED(tdoc), TidyOption opt )
628
0
{
629
0
    const TidyOptionId optId = tidyOptGetId( opt );
630
0
    const TidyOptionDoc* docDesc = TY_(OptGetDocDesc)( optId );
631
0
    if (docDesc && docDesc->links)
632
0
        return (TidyIterator)docDesc->links;
633
0
    return (TidyIterator)NULL;
634
0
}
635
#endif /* SUPPORT_CONSOLE_APP */
636
637
TidyOption TIDY_CALL tidyOptGetNextDocLinks( TidyDoc tdoc, TidyIterator* pos )
638
0
{
639
0
    const TidyOptionId* curr = (const TidyOptionId *)*pos;
640
0
    TidyOption opt;
641
642
0
    if (*curr == TidyUnknownOption)
643
0
    {
644
0
        *pos = (TidyIterator)NULL;
645
0
        return (TidyOption)0;
646
0
    }
647
0
    opt = tidyGetOption(tdoc, *curr);
648
0
    curr++;
649
0
    *pos = (*curr == TidyUnknownOption ) ?
650
0
        (TidyIterator)NULL:(TidyIterator)curr;
651
0
    return opt;
652
0
}
653
654
int TIDY_CALL tidyOptSaveFile( TidyDoc tdoc, ctmbstr cfgfil )
655
0
{
656
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
657
0
    if ( impl )
658
0
        return TY_(SaveConfigFile)( impl, cfgfil );
659
0
    return -EINVAL;
660
0
}
661
662
int TIDY_CALL tidyOptSaveSink( TidyDoc tdoc, TidyOutputSink* sink )
663
0
{
664
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
665
0
    if ( impl )
666
0
        return TY_(SaveConfigSink)( impl, sink );
667
0
    return -EINVAL;
668
0
}
669
670
Bool TIDY_CALL tidyOptSnapshot( TidyDoc tdoc )
671
0
{
672
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
673
0
    if ( impl )
674
0
    {
675
0
        TY_(TakeConfigSnapshot)( impl );
676
0
        return yes;
677
0
    }
678
0
    return no;
679
0
}
680
Bool TIDY_CALL tidyOptResetToSnapshot( TidyDoc tdoc )
681
0
{
682
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
683
0
    if ( impl )
684
0
    {
685
0
        TY_(ResetConfigToSnapshot)( impl );
686
0
        return yes;
687
0
    }
688
0
    return no;
689
0
}
690
Bool TIDY_CALL tidyOptResetAllToDefault( TidyDoc tdoc )
691
0
{
692
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
693
0
    if ( impl )
694
0
    {
695
0
        TY_(ResetConfigToDefault)( impl );
696
0
        return yes;
697
0
    }
698
0
    return no;
699
0
}
700
701
Bool TIDY_CALL tidyOptResetToDefault( TidyDoc tdoc, TidyOptionId optId )
702
0
{
703
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
704
0
    if ( impl )
705
0
        return TY_(ResetOptionToDefault)( impl, optId );
706
0
    return no;
707
0
}
708
709
Bool TIDY_CALL tidyOptDiffThanDefault( TidyDoc tdoc )
710
0
{
711
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
712
0
    if ( impl )
713
0
        return TY_(ConfigDiffThanDefault)( impl );
714
0
    return no;
715
0
}
716
Bool TIDY_CALL          tidyOptDiffThanSnapshot( TidyDoc tdoc )
717
0
{
718
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
719
0
    if ( impl )
720
0
        return TY_(ConfigDiffThanSnapshot)( impl );
721
0
    return no;
722
0
}
723
724
Bool TIDY_CALL tidyOptCopyConfig( TidyDoc to, TidyDoc from )
725
0
{
726
0
    TidyDocImpl* docTo = tidyDocToImpl( to );
727
0
    TidyDocImpl* docFrom = tidyDocToImpl( from );
728
0
    if ( docTo && docFrom )
729
0
    {
730
0
        TY_(CopyConfig)( docTo, docFrom );
731
0
        return yes;
732
0
    }
733
0
    return no;
734
0
}
735
736
737
/* I/O and Message handling interface
738
**
739
** By default, Tidy will define, create and use instance of input and output 
740
** handlers for standard C buffered I/O (i.e. FILE* stdin, FILE* stdout and
741
** FILE* stderr for content input, content output and diagnostic output,
742
** respectively.  A FILE* cfgFile input handler will be used for config files.
743
** Command line options will just be set directly.
744
*/
745
746
void TIDY_CALL tidySetEmacsFile( TidyDoc tdoc, ctmbstr filePath )
747
0
{
748
0
    tidyOptSetValue( tdoc, TidyEmacsFile, filePath );
749
0
}
750
751
ctmbstr TIDY_CALL tidyGetEmacsFile( TidyDoc tdoc )
752
0
{
753
0
    return tidyOptGetValue( tdoc, TidyEmacsFile );
754
0
}
755
756
757
/* Use TidyReportFilter to filter messages by diagnostic level:
758
** info, warning, etc.  Just set diagnostic output
759
** handler to redirect all diagnostics output.  Return true
760
** to proceed with output, false to cancel.
761
*/
762
Bool TIDY_CALL tidySetReportFilter( TidyDoc tdoc, TidyReportFilter filt )
763
0
{
764
0
  TidyDocImpl* impl = tidyDocToImpl( tdoc );
765
0
  if ( impl )
766
0
  {
767
0
    impl->reportFilter = filt;
768
0
    return yes;
769
0
  }
770
0
  return no;
771
0
}
772
773
/* tidySetReportCallback functions similar to TidyReportFilter, but provides the
774
 * string version of the internal enum name so that LibTidy users can use
775
** the string as a lookup key for providing their own error localizations.
776
** See the string key definitions in tidyenum.h.
777
*/
778
Bool TIDY_CALL tidySetReportCallback( TidyDoc tdoc, TidyReportCallback filt )
779
0
{
780
0
  TidyDocImpl* impl = tidyDocToImpl( tdoc );
781
0
  if ( impl )
782
0
  {
783
0
    impl->reportCallback = filt;
784
0
    return yes;
785
0
  }
786
0
  return no;
787
0
}
788
789
Bool TIDY_CALL tidySetMessageCallback( TidyDoc tdoc, TidyMessageCallback filt )
790
0
{
791
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
792
0
    if ( impl )
793
0
    {
794
0
        impl->messageCallback = filt;
795
0
        return yes;
796
0
    }
797
0
    return no;
798
0
}
799
800
TidyDoc TIDY_CALL tidyGetMessageDoc( TidyMessage tmessage )
801
0
{
802
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
803
0
    TidyDocImpl* doc = TY_(getMessageDoc)(*message);
804
0
    return tidyImplToDoc(doc);
805
0
}
806
807
uint TIDY_CALL tidyGetMessageCode( TidyMessage tmessage )
808
0
{
809
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
810
0
    return TY_(getMessageCode)(*message);
811
0
}
812
813
ctmbstr TIDY_CALL tidyGetMessageKey( TidyMessage tmessage )
814
0
{
815
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
816
0
    return TY_(getMessageKey)(*message);
817
0
}
818
819
int TIDY_CALL tidyGetMessageLine( TidyMessage tmessage )
820
0
{
821
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
822
0
    return TY_(getMessageLine)(*message);
823
0
}
824
825
int TIDY_CALL tidyGetMessageColumn( TidyMessage tmessage )
826
0
{
827
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
828
0
    return TY_(getMessageColumn)(*message);
829
0
}
830
831
TidyReportLevel TIDY_CALL tidyGetMessageLevel( TidyMessage tmessage )
832
0
{
833
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
834
0
    return TY_(getMessageLevel)(*message);
835
0
}
836
837
Bool TIDY_CALL tidyGetMessageIsMuted( TidyMessage tmessage )
838
0
{
839
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
840
0
    return TY_(getMessageIsMuted)(*message);
841
0
}
842
843
ctmbstr TIDY_CALL tidyGetMessageFormatDefault( TidyMessage tmessage )
844
0
{
845
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
846
0
    return TY_(getMessageFormatDefault)(*message);
847
0
}
848
849
ctmbstr TIDY_CALL tidyGetMessageFormat( TidyMessage tmessage )
850
0
{
851
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
852
0
    return TY_(getMessageFormat)(*message);
853
0
}
854
855
ctmbstr TIDY_CALL tidyGetMessageDefault( TidyMessage tmessage )
856
0
{
857
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
858
0
    return TY_(getMessageDefault)(*message);
859
0
}
860
861
ctmbstr TIDY_CALL tidyGetMessage( TidyMessage tmessage )
862
0
{
863
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
864
0
    return TY_(getMessage)(*message);
865
0
}
866
867
ctmbstr TIDY_CALL tidyGetMessagePosDefault( TidyMessage tmessage )
868
0
{
869
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
870
0
    return TY_(getMessagePosDefault)(*message);
871
0
}
872
873
ctmbstr TIDY_CALL tidyGetMessagePos( TidyMessage tmessage )
874
0
{
875
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
876
0
    return TY_(getMessagePos)(*message);
877
0
}
878
879
ctmbstr TIDY_CALL tidyGetMessagePrefixDefault( TidyMessage tmessage )
880
0
{
881
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
882
0
    return TY_(getMessagePrefixDefault)(*message);
883
0
}
884
885
ctmbstr TIDY_CALL tidyGetMessagePrefix( TidyMessage tmessage )
886
0
{
887
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
888
0
    return TY_(getMessagePrefix)(*message);
889
0
}
890
891
892
ctmbstr TIDY_CALL tidyGetMessageOutputDefault( TidyMessage tmessage )
893
0
{
894
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
895
0
    return TY_(getMessageOutputDefault)(*message);
896
0
}
897
898
ctmbstr TIDY_CALL tidyGetMessageOutput( TidyMessage tmessage )
899
0
{
900
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
901
0
    return TY_(getMessageOutput)(*message);
902
0
}
903
904
TidyIterator TIDY_CALL tidyGetMessageArguments( TidyMessage tmessage )
905
0
{
906
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
907
0
    return TY_(getMessageArguments)(*message);
908
0
}
909
910
TidyMessageArgument TIDY_CALL tidyGetNextMessageArgument( TidyMessage tmessage, TidyIterator* iter )
911
0
{
912
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
913
0
    return TY_(getNextMessageArgument)(*message, iter);
914
0
}
915
916
TidyFormatParameterType TIDY_CALL tidyGetArgType( TidyMessage tmessage, TidyMessageArgument* arg )
917
0
{
918
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
919
0
    return TY_(getArgType)(*message, arg);
920
0
}
921
922
ctmbstr TIDY_CALL tidyGetArgFormat( TidyMessage tmessage, TidyMessageArgument* arg )
923
0
{
924
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
925
0
    return TY_(getArgFormat)(*message, arg);
926
0
}
927
928
ctmbstr TIDY_CALL tidyGetArgValueString( TidyMessage tmessage, TidyMessageArgument* arg )
929
0
{
930
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
931
0
    return TY_(getArgValueString)(*message, arg);
932
0
}
933
934
uint TIDY_CALL tidyGetArgValueUInt( TidyMessage tmessage, TidyMessageArgument* arg )
935
0
{
936
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
937
0
    return TY_(getArgValueUInt)(*message, arg);
938
0
}
939
940
int TIDY_CALL tidyGetArgValueInt( TidyMessage tmessage, TidyMessageArgument* arg )
941
0
{
942
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
943
0
    return TY_(getArgValueInt)(*message, arg);
944
0
}
945
946
double TIDY_CALL tidyGetArgValueDouble( TidyMessage tmessage, TidyMessageArgument* arg )
947
0
{
948
0
    TidyMessageImpl *message = tidyMessageToImpl(tmessage);
949
0
    return TY_(getArgValueDouble)(*message, arg);
950
0
}
951
952
953
FILE* TIDY_CALL   tidySetErrorFile( TidyDoc tdoc, ctmbstr errfilnam )
954
0
{
955
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
956
0
    if ( impl )
957
0
    {
958
0
        FILE* errout = fopen( errfilnam, "wb" );
959
0
        if ( errout )
960
0
        {
961
0
            uint outenc = cfg( impl, TidyOutCharEncoding );
962
0
            uint nl = cfg( impl, TidyNewline );
963
0
            TY_(ReleaseStreamOut)( impl, impl->errout );
964
0
            impl->errout = TY_(FileOutput)( impl, errout, outenc, nl );
965
0
            return errout;
966
0
        }
967
0
        else /* Emit message to current error sink */
968
0
            TY_(ReportFileError)( impl, errfilnam, FILE_CANT_OPEN );
969
0
    }
970
0
    return NULL;
971
0
}
972
973
int TIDY_CALL    tidySetErrorBuffer( TidyDoc tdoc, TidyBuffer* errbuf )
974
22.1k
{
975
22.1k
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
976
22.1k
    if ( impl )
977
22.1k
    {
978
22.1k
        uint outenc = cfg( impl, TidyOutCharEncoding );
979
22.1k
        uint nl = cfg( impl, TidyNewline );
980
22.1k
        TY_(ReleaseStreamOut)( impl, impl->errout );
981
22.1k
        impl->errout = TY_(BufferOutput)( impl, errbuf, outenc, nl );
982
22.1k
        return ( impl->errout ? 0 : -ENOMEM );
983
22.1k
    }
984
0
    return -EINVAL;
985
22.1k
}
986
987
int TIDY_CALL    tidySetErrorSink( TidyDoc tdoc, TidyOutputSink* sink )
988
0
{
989
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
990
0
    if ( impl )
991
0
    {
992
0
        uint outenc = cfg( impl, TidyOutCharEncoding );
993
0
        uint nl = cfg( impl, TidyNewline );
994
0
        TY_(ReleaseStreamOut)( impl, impl->errout );
995
0
        impl->errout = TY_(UserOutput)( impl, sink, outenc, nl );
996
0
        return ( impl->errout ? 0 : -ENOMEM );
997
0
    }
998
0
    return -EINVAL;
999
0
}
1000
1001
/* Use TidyPPProgress to monitor the progress of the pretty printer.
1002
 */
1003
Bool TIDY_CALL        tidySetPrettyPrinterCallback(TidyDoc tdoc, TidyPPProgress callback)
1004
0
{
1005
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
1006
0
    if ( impl )
1007
0
    {
1008
0
        impl->progressCallback = callback;
1009
0
        return yes;
1010
0
    }
1011
0
    return no;
1012
0
}
1013
1014
1015
/* Document info */
1016
int TIDY_CALL        tidyStatus( TidyDoc tdoc )
1017
0
{
1018
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
1019
0
    int tidyStat = -EINVAL;
1020
0
    if ( impl )
1021
0
        tidyStat = tidyDocStatus( impl );
1022
0
    return tidyStat;
1023
0
}
1024
int TIDY_CALL        tidyDetectedHtmlVersion( TidyDoc ARG_UNUSED(tdoc) )
1025
0
{
1026
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
1027
0
    return TY_(HTMLVersionNumberFromCode)( impl->lexer->versionEmitted );
1028
0
}
1029
1030
Bool TIDY_CALL        tidyDetectedXhtml( TidyDoc ARG_UNUSED(tdoc) )
1031
0
{
1032
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc ); 
1033
0
    return impl->lexer->isvoyager;
1034
0
}
1035
Bool TIDY_CALL        tidyDetectedGenericXml( TidyDoc ARG_UNUSED(tdoc) )
1036
0
{
1037
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc ); 
1038
0
    return impl->xmlDetected;
1039
0
}
1040
1041
uint TIDY_CALL       tidyErrorCount( TidyDoc tdoc )
1042
0
{
1043
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
1044
0
    uint count = 0xFFFFFFFF;
1045
0
    if ( impl )
1046
0
        count = impl->errors;
1047
0
    return count;
1048
0
}
1049
uint TIDY_CALL       tidyWarningCount( TidyDoc tdoc )
1050
0
{
1051
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
1052
0
    uint count = 0xFFFFFFFF;
1053
0
    if ( impl )
1054
0
        count = impl->warnings;
1055
0
    return count;
1056
0
}
1057
uint TIDY_CALL       tidyAccessWarningCount( TidyDoc tdoc )
1058
0
{
1059
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
1060
0
    uint count = 0xFFFFFFFF;
1061
0
    if ( impl )
1062
0
        count = impl->accessErrors;
1063
0
    return count;
1064
0
}
1065
uint TIDY_CALL       tidyConfigErrorCount( TidyDoc tdoc )
1066
0
{
1067
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
1068
0
    uint count = 0xFFFFFFFF;
1069
0
    if ( impl )
1070
0
        count = impl->optionErrors;
1071
0
    return count;
1072
0
}
1073
1074
1075
/* Error reporting functions
1076
*/
1077
void TIDY_CALL         tidyErrorSummary( TidyDoc tdoc )
1078
0
{
1079
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
1080
0
    if ( impl )
1081
0
        TY_(ErrorSummary)( impl );
1082
0
}
1083
void TIDY_CALL         tidyGeneralInfo( TidyDoc tdoc )
1084
0
{
1085
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
1086
0
    if ( impl )
1087
0
    {
1088
0
        TY_(Dialogue)( impl, TEXT_GENERAL_INFO );
1089
0
        TY_(Dialogue)( impl, TEXT_GENERAL_INFO_PLEA );
1090
0
    }
1091
0
}
1092
1093
1094
/* I/O Functions
1095
**
1096
** Initial version supports only whole-file operations.
1097
** Do not expose Tidy StreamIn or Out data structures - yet.
1098
*/
1099
1100
/* Parse/load Functions
1101
**
1102
** HTML/XHTML version determined from input.
1103
*/
1104
int TIDY_CALL  tidyParseFile( TidyDoc tdoc, ctmbstr filnam )
1105
22.1k
{
1106
22.1k
    TidyDocImpl* doc = tidyDocToImpl( tdoc );
1107
22.1k
    return tidyDocParseFile( doc, filnam );
1108
22.1k
}
1109
int TIDY_CALL  tidyParseStdin( TidyDoc tdoc )
1110
0
{
1111
0
    TidyDocImpl* doc = tidyDocToImpl( tdoc );
1112
0
    return tidyDocParseStdin( doc );
1113
0
}
1114
int TIDY_CALL  tidyParseString( TidyDoc tdoc, ctmbstr content )
1115
0
{
1116
0
    TidyDocImpl* doc = tidyDocToImpl( tdoc );
1117
0
    return tidyDocParseString( doc, content );
1118
0
}
1119
int TIDY_CALL  tidyParseBuffer( TidyDoc tdoc, TidyBuffer* inbuf )
1120
0
{
1121
0
    TidyDocImpl* doc = tidyDocToImpl( tdoc );
1122
0
    return tidyDocParseBuffer( doc, inbuf );
1123
0
}
1124
int TIDY_CALL  tidyParseSource( TidyDoc tdoc, TidyInputSource* source )
1125
0
{
1126
0
    TidyDocImpl* doc = tidyDocToImpl( tdoc );
1127
0
    return tidyDocParseSource( doc, source );
1128
0
}
1129
1130
#ifdef WIN32
1131
#define M_IS_DIR _S_IFDIR
1132
#else // !WIN32
1133
22.1k
#define M_IS_DIR S_IFDIR
1134
#endif
1135
int   tidyDocParseFile( TidyDocImpl* doc, ctmbstr filnam )
1136
22.1k
{
1137
22.1k
    int status = -ENOENT;
1138
22.1k
    FILE* fin = 0;
1139
22.1k
    struct stat sbuf = { 0 }; /* Is. #681 - read-only files */
1140
22.1k
    if ( stat(filnam,&sbuf) != 0 )
1141
0
    {
1142
0
        TY_(ReportFileError)( doc, filnam, FILE_NOT_FILE );
1143
0
        return status;
1144
0
    }
1145
22.1k
    if (sbuf.st_mode & M_IS_DIR) /* and /NOT/ if a DIRECTORY */
1146
0
    {
1147
0
        TY_(ReportFileError)(doc, filnam, FILE_NOT_FILE);
1148
0
        return status;
1149
0
    }
1150
1151
#ifdef _WIN32
1152
    return TY_(DocParseFileWithMappedFile)( doc, filnam );
1153
#else
1154
1155
22.1k
    fin = fopen( filnam, "rb" );
1156
1157
22.1k
#if PRESERVE_FILE_TIMES
1158
22.1k
    {
1159
        /* get last modified time */
1160
22.1k
        TidyClearMemory(&doc->filetimes, sizeof(doc->filetimes));
1161
22.1k
        if (fin && cfgBool(doc, TidyKeepFileTimes) &&
1162
0
            fstat(fileno(fin), &sbuf) != -1)
1163
0
        {
1164
0
            doc->filetimes.actime = sbuf.st_atime;
1165
0
            doc->filetimes.modtime = sbuf.st_mtime;
1166
0
        }
1167
22.1k
    }
1168
22.1k
#endif
1169
1170
22.1k
    if ( fin )
1171
22.1k
    {
1172
22.1k
        StreamIn* in = TY_(FileInput)( doc, fin, cfg( doc, TidyInCharEncoding ));
1173
22.1k
        if ( !in )
1174
0
        {
1175
0
            fclose( fin );
1176
0
            return status;
1177
0
        }
1178
22.1k
        status = TY_(DocParseStream)( doc, in );
1179
22.1k
        TY_(freeFileSource)(&in->source, yes);
1180
22.1k
        TY_(freeStreamIn)(in);
1181
22.1k
    }
1182
0
    else /* Error message! */
1183
0
        TY_(ReportFileError)( doc, filnam, FILE_CANT_OPEN );
1184
22.1k
    return status;
1185
22.1k
#endif
1186
22.1k
}
1187
1188
int   tidyDocParseStdin( TidyDocImpl* doc )
1189
0
{
1190
0
    StreamIn* in = TY_(FileInput)( doc, stdin, cfg( doc, TidyInCharEncoding ));
1191
0
    int status = TY_(DocParseStream)( doc, in );
1192
0
    TY_(freeFileSource)(&in->source, yes);
1193
0
    TY_(freeStreamIn)(in);
1194
0
    return status;
1195
0
}
1196
1197
int   tidyDocParseBuffer( TidyDocImpl* doc, TidyBuffer* inbuf )
1198
0
{
1199
0
    int status = -EINVAL;
1200
0
    if ( inbuf )
1201
0
    {
1202
0
        StreamIn* in = TY_(BufferInput)( doc, inbuf, cfg( doc, TidyInCharEncoding ));
1203
0
        status = TY_(DocParseStream)( doc, in );
1204
0
        TY_(freeStreamIn)(in);
1205
0
    }
1206
0
    return status;
1207
0
}
1208
1209
int   tidyDocParseString( TidyDocImpl* doc, ctmbstr content )
1210
0
{
1211
0
    int status = -EINVAL;
1212
0
    TidyBuffer inbuf;
1213
0
    StreamIn* in = NULL;
1214
1215
0
    if ( content )
1216
0
    {
1217
0
        tidyBufInitWithAllocator( &inbuf, doc->allocator );
1218
0
        tidyBufAttach( &inbuf, (byte*)content, TY_(tmbstrlen)(content)+1 );
1219
0
        in = TY_(BufferInput)( doc, &inbuf, cfg( doc, TidyInCharEncoding ));
1220
0
        status = TY_(DocParseStream)( doc, in );
1221
0
        tidyBufDetach( &inbuf );
1222
0
        TY_(freeStreamIn)(in);
1223
0
    }
1224
0
    return status;
1225
0
}
1226
1227
int   tidyDocParseSource( TidyDocImpl* doc, TidyInputSource* source )
1228
0
{
1229
0
    StreamIn* in = TY_(UserInput)( doc, source, cfg( doc, TidyInCharEncoding ));
1230
0
    int status = TY_(DocParseStream)( doc, in );
1231
0
    TY_(freeStreamIn)(in);
1232
0
    return status;
1233
0
}
1234
1235
1236
/* Print/save Functions
1237
**
1238
*/
1239
int TIDY_CALL        tidySaveFile( TidyDoc tdoc, ctmbstr filnam )
1240
0
{
1241
0
    TidyDocImpl* doc = tidyDocToImpl( tdoc );
1242
0
    return tidyDocSaveFile( doc, filnam );
1243
0
}
1244
int TIDY_CALL        tidySaveStdout( TidyDoc tdoc )
1245
0
{
1246
0
    TidyDocImpl* doc = tidyDocToImpl( tdoc );
1247
0
    return tidyDocSaveStdout( doc );
1248
0
}
1249
int TIDY_CALL        tidySaveString( TidyDoc tdoc, tmbstr buffer, uint* buflen )
1250
0
{
1251
0
    TidyDocImpl* doc = tidyDocToImpl( tdoc );
1252
0
    return tidyDocSaveString( doc, buffer, buflen );
1253
0
}
1254
int TIDY_CALL        tidySaveBuffer( TidyDoc tdoc, TidyBuffer* outbuf )
1255
22.1k
{
1256
22.1k
    TidyDocImpl* doc = tidyDocToImpl( tdoc );
1257
22.1k
    return tidyDocSaveBuffer( doc, outbuf );
1258
22.1k
}
1259
int TIDY_CALL        tidySaveSink( TidyDoc tdoc, TidyOutputSink* sink )
1260
0
{
1261
0
    TidyDocImpl* doc = tidyDocToImpl( tdoc );
1262
0
    return tidyDocSaveSink( doc, sink );
1263
0
}
1264
1265
int         tidyDocSaveFile( TidyDocImpl* doc, ctmbstr filnam )
1266
0
{
1267
0
    int status = -ENOENT;
1268
0
    FILE* fout = NULL;
1269
1270
    /* Don't zap input file if no output */
1271
0
    if ( doc->errors > 0 &&
1272
0
         cfgBool(doc, TidyWriteBack) && !cfgBool(doc, TidyForceOutput) )
1273
0
        status = tidyDocStatus( doc );
1274
0
    else
1275
0
        fout = fopen( filnam, "wb" );
1276
1277
0
    if ( fout )
1278
0
    {
1279
0
        uint outenc = cfg( doc, TidyOutCharEncoding );
1280
0
        uint nl = cfg( doc, TidyNewline );
1281
0
        StreamOut* out = TY_(FileOutput)( doc, fout, outenc, nl );
1282
1283
0
        status = tidyDocSaveStream( doc, out );
1284
1285
0
        fclose( fout );
1286
0
        TidyDocFree( doc, out );
1287
1288
0
#if PRESERVE_FILE_TIMES
1289
0
        if ( doc->filetimes.actime )
1290
0
        {
1291
            /* set file last accessed/modified times to original values */
1292
0
            utime( filnam, &doc->filetimes );
1293
0
            TidyClearMemory( &doc->filetimes, sizeof(doc->filetimes) );
1294
0
        }
1295
0
#endif /* PRESERVFILETIMES */
1296
0
    }
1297
0
    if ( status < 0 ) /* Error message! */
1298
0
        TY_(ReportFileError)( doc, filnam, FILE_CANT_OPEN );
1299
0
    return status;
1300
0
}
1301
1302
1303
1304
/* Note, _setmode() does NOT work on Win2K Pro w/ VC++ 6.0 SP3.
1305
** The code has been left in in case it works w/ other compilers
1306
** or operating systems.  If stdout is in Text mode, be aware that
1307
** it will garble UTF16 documents.  In text mode, when it encounters
1308
** a single byte of value 10 (0xA), it will insert a single byte
1309
** value 13 (0xD) just before it.  This has the effect of garbling
1310
** the entire document.
1311
*/
1312
1313
#if !defined(NO_SETMODE_SUPPORT)
1314
#  if defined(_WIN32) || defined(OS2_OS)
1315
#   include <fcntl.h>
1316
#     include <io.h>
1317
#   endif
1318
#endif
1319
1320
int         tidyDocSaveStdout( TidyDocImpl* doc )
1321
0
{
1322
0
#if !defined(NO_SETMODE_SUPPORT)
1323
#  if defined(_WIN32) || defined(OS2_OS)
1324
    int oldstdoutmode = -1, oldstderrmode = -1;
1325
#  endif
1326
0
#endif
1327
1328
0
    int status = 0;
1329
0
    uint outenc = cfg( doc, TidyOutCharEncoding );
1330
0
    uint nl = cfg( doc, TidyNewline );
1331
0
    StreamOut* out = TY_(FileOutput)( doc, stdout, outenc, nl );
1332
1333
0
#if !defined(NO_SETMODE_SUPPORT)
1334
#  if defined(_WIN32) || defined(OS2_OS)
1335
    oldstdoutmode = setmode( fileno(stdout), _O_BINARY );
1336
    oldstderrmode = setmode( fileno(stderr), _O_BINARY );
1337
#  endif
1338
0
#endif
1339
1340
0
    if ( 0 == status )
1341
0
      status = tidyDocSaveStream( doc, out );
1342
1343
0
    fflush(stdout);
1344
0
    fflush(stderr);
1345
1346
0
#if !defined(NO_SETMODE_SUPPORT)
1347
#  if defined(_WIN32) || defined(OS2_OS)
1348
    if ( oldstdoutmode != -1 )
1349
        oldstdoutmode = setmode( fileno(stdout), oldstdoutmode );
1350
    if ( oldstderrmode != -1 )
1351
        oldstderrmode = setmode( fileno(stderr), oldstderrmode );
1352
#  endif
1353
0
#endif
1354
1355
0
    TidyDocFree( doc, out );
1356
0
    return status;
1357
0
}
1358
1359
int         tidyDocSaveString( TidyDocImpl* doc, tmbstr buffer, uint* buflen )
1360
0
{
1361
0
    uint outenc = cfg( doc, TidyOutCharEncoding );
1362
0
    uint nl = cfg( doc, TidyNewline );
1363
0
    TidyBuffer outbuf;
1364
0
    StreamOut* out;
1365
0
    int status;
1366
1367
0
    tidyBufInitWithAllocator( &outbuf, doc->allocator );
1368
0
    out = TY_(BufferOutput)( doc, &outbuf, outenc, nl );
1369
0
    status = tidyDocSaveStream( doc, out );
1370
1371
0
    if ( outbuf.size > *buflen )
1372
0
        status = -ENOMEM;
1373
0
    else
1374
0
        memcpy( buffer, outbuf.bp, outbuf.size );
1375
1376
0
    *buflen = outbuf.size;
1377
0
    tidyBufFree( &outbuf );
1378
0
    TidyDocFree( doc, out );
1379
0
    return status;
1380
0
}
1381
1382
int         tidyDocSaveBuffer( TidyDocImpl* doc, TidyBuffer* outbuf )
1383
22.1k
{
1384
22.1k
    int status = -EINVAL;
1385
22.1k
    if ( outbuf )
1386
22.1k
    {
1387
22.1k
        uint outenc = cfg( doc, TidyOutCharEncoding );
1388
22.1k
        uint nl = cfg( doc, TidyNewline );
1389
22.1k
        StreamOut* out = TY_(BufferOutput)( doc, outbuf, outenc, nl );
1390
1391
22.1k
        status = tidyDocSaveStream( doc, out );
1392
22.1k
        TidyDocFree( doc, out );
1393
22.1k
    }
1394
22.1k
    return status;
1395
22.1k
}
1396
1397
int         tidyDocSaveSink( TidyDocImpl* doc, TidyOutputSink* sink )
1398
0
{
1399
0
    uint outenc = cfg( doc, TidyOutCharEncoding );
1400
0
    uint nl = cfg( doc, TidyNewline );
1401
0
    StreamOut* out = TY_(UserOutput)( doc, sink, outenc, nl );
1402
0
    int status = tidyDocSaveStream( doc, out );
1403
0
    TidyDocFree( doc, out );
1404
0
    return status;
1405
0
}
1406
1407
int         tidyDocStatus( TidyDocImpl* doc )
1408
88.4k
{
1409
88.4k
    if ( doc->errors > 0 )
1410
19.6k
        return 2;
1411
68.7k
    if ( doc->warnings > 0 || doc->accessErrors > 0 )
1412
68.7k
        return 1;
1413
4
    return 0;
1414
68.7k
}
1415
1416
1417
1418
int TIDY_CALL        tidyCleanAndRepair( TidyDoc tdoc )
1419
22.1k
{
1420
22.1k
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
1421
22.1k
    if ( impl )
1422
22.1k
      return tidyDocCleanAndRepair( impl );
1423
0
    return -EINVAL;
1424
22.1k
}
1425
1426
int TIDY_CALL        tidyRunDiagnostics( TidyDoc tdoc )
1427
22.1k
{
1428
22.1k
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
1429
22.1k
    if ( impl )
1430
22.1k
      return tidyDocRunDiagnostics( impl );
1431
0
    return -EINVAL;
1432
22.1k
}
1433
1434
int TIDY_CALL        tidyReportDoctype( TidyDoc tdoc )
1435
0
{
1436
0
    int iret = -EINVAL;
1437
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
1438
0
    if ( impl ) {
1439
0
      tidyDocReportDoctype( impl );
1440
0
      iret = 0;
1441
0
    }
1442
0
    return iret;
1443
0
}
1444
1445
/* Workhorse functions.
1446
**
1447
** Parse requires input source, all input config items
1448
** and diagnostic sink to have all been set before calling.
1449
**
1450
** Emit likewise requires that document sink and all
1451
** pretty printing options have been set.
1452
*/
1453
static ctmbstr integrity = "\nPanic - tree has lost its integrity\n";
1454
1455
int         TY_(DocParseStream)( TidyDocImpl* doc, StreamIn* in )
1456
22.1k
{
1457
22.1k
    Bool xmlIn = cfgBool( doc, TidyXmlTags );
1458
22.1k
    TidyConfigChangeCallback callback = doc->pConfigChangeCallback;
1459
    
1460
22.1k
    int bomEnc;
1461
22.1k
    doc->pConfigChangeCallback = NULL;
1462
1463
22.1k
    assert( doc != NULL && in != NULL );
1464
22.1k
    assert( doc->docIn == NULL );
1465
22.1k
    doc->docIn = in;
1466
1467
22.1k
    TY_(ResetTags)(doc);             /* Reset table to html5 mode */
1468
22.1k
    TY_(TakeConfigSnapshot)( doc );  /* Save config state */
1469
22.1k
    TY_(AdjustConfig)( doc );        /* Ensure config internal consistency */
1470
22.1k
    TY_(FreeAnchors)( doc );
1471
1472
22.1k
    TY_(FreeNode)(doc, &doc->root);
1473
22.1k
    TidyClearMemory(&doc->root, sizeof(Node));
1474
1475
22.1k
    if (doc->givenDoctype)
1476
22.1k
        TidyDocFree(doc, doc->givenDoctype);
1477
    /*\ 
1478
     *  Issue #186 - Now FreeNode depend on the doctype, so the lexer is needed
1479
     *  to determine which hash is to be used, so free it last.
1480
    \*/
1481
22.1k
    TY_(FreeLexer)( doc );
1482
22.1k
    doc->givenDoctype = NULL;
1483
1484
22.1k
    doc->lexer = TY_(NewLexer)( doc );
1485
    /* doc->lexer->root = &doc->root; */
1486
22.1k
    doc->root.line = doc->lexer->lines;
1487
22.1k
    doc->root.column = doc->lexer->columns;
1488
22.1k
    doc->inputHadBOM = no;
1489
22.1k
    doc->xmlDetected = no;
1490
1491
22.1k
    bomEnc = TY_(ReadBOMEncoding)(in);
1492
1493
22.1k
    if (bomEnc != -1)
1494
180
    {
1495
180
        in->encoding = bomEnc;
1496
180
        TY_(SetOptionInt)(doc, TidyInCharEncoding, bomEnc);
1497
180
    }
1498
1499
    /* Tidy doesn't alter the doctype for generic XML docs */
1500
22.1k
    if ( xmlIn )
1501
0
    {
1502
0
        TY_(ParseXMLDocument)( doc );
1503
0
        if ( !TY_(CheckNodeIntegrity)( &doc->root ) )
1504
0
            TidyPanic( doc->allocator, integrity );
1505
0
    }
1506
22.1k
    else
1507
22.1k
    {
1508
22.1k
        doc->warnings = 0;
1509
22.1k
        TY_(ParseDocument)( doc );
1510
22.1k
        if ( !TY_(CheckNodeIntegrity)( &doc->root ) )
1511
0
            TidyPanic( doc->allocator, integrity );
1512
22.1k
    }
1513
1514
22.1k
    doc->docIn = NULL;
1515
22.1k
    doc->pConfigChangeCallback = callback;
1516
1517
22.1k
    return tidyDocStatus( doc );
1518
22.1k
}
1519
1520
int         tidyDocRunDiagnostics( TidyDocImpl* doc )
1521
22.1k
{
1522
22.1k
    TY_(ReportMarkupVersion)( doc );
1523
22.1k
    TY_(ReportNumWarnings)( doc );
1524
1525
22.1k
    if ( doc->errors > 0 && !cfgBool( doc, TidyForceOutput ) )
1526
4.92k
        TY_(Dialogue)(doc, STRING_NEEDS_INTERVENTION );
1527
1528
22.1k
     return tidyDocStatus( doc );
1529
22.1k
}
1530
1531
void         tidyDocReportDoctype( TidyDocImpl* doc )
1532
0
{
1533
0
        TY_(ReportMarkupVersion)( doc );
1534
0
}
1535
1536
1537
/*****************************************************************************
1538
 *  HTML5 STUFF
1539
 *****************************************************************************/
1540
#if 0 && defined(ENABLE_DEBUG_LOG)
1541
extern void show_not_html5(void);
1542
/* -----------------------------
1543
List tags that do not have version HTML5 (HT50|XH50)
1544
1545
acronym applet basefont big center dir font frame frameset isindex
1546
listing noframes plaintext rb rbc rtc strike tt xmp nextid
1547
align bgsound blink comment ilayer layer marquee multicol nobr noembed
1548
nolayer nosave server servlet spacer
1549
1550
Listed total 35 tags that do not have version 393216
1551
   ------------------------------ */
1552
1553
static void list_not_html5(void)
1554
{
1555
    static Bool done_list = no;
1556
    if (done_list == no) {
1557
        done_list = yes;
1558
        show_not_html5();
1559
    }
1560
}
1561
#endif
1562
1563
/* What about <blink>, <s> stike-through, <u> underline */
1564
static struct _html5Info
1565
{
1566
    const char *tag;
1567
    uint id;
1568
} const html5Info[] = {
1569
    {"acronym", TidyTag_ACRONYM},
1570
    {"applet", TidyTag_APPLET  },
1571
    {"basefont",TidyTag_BASEFONT },
1572
    { "big", TidyTag_BIG },
1573
    { "center", TidyTag_CENTER },
1574
    { "dir", TidyTag_DIR },
1575
    { "font", TidyTag_FONT },
1576
    { "frame", TidyTag_FRAME},
1577
    { "frameset", TidyTag_FRAMESET},
1578
    { "noframes", TidyTag_NOFRAMES },
1579
    { "strike", TidyTag_STRIKE },
1580
    { "tt", TidyTag_TT },
1581
    { 0, 0 }
1582
};
1583
static Bool inRemovedInfo( uint tid )
1584
780k
{
1585
780k
    int i;
1586
10.1M
    for (i = 0; ; i++) {
1587
10.1M
        if (html5Info[i].tag == 0)
1588
780k
            break;
1589
9.36M
        if (html5Info[i].id == tid)
1590
0
            return yes;
1591
9.36M
    }
1592
780k
    return no;
1593
780k
}
1594
1595
/* Things that should not be in an HTML5 body. This is special for CheckHTML5(),
1596
 and we might just want to remove CheckHTML5()'s output altogether and count
1597
 on the default --strict-tags-attributes.
1598
 */
1599
static int BadBody5Attribs[] = {
1600
    TidyAttr_BACKGROUND,
1601
    TidyAttr_BGCOLOR,
1602
    TidyAttr_TEXT,
1603
    TidyAttr_LINK,
1604
    TidyAttr_VLINK,
1605
    TidyAttr_ALINK,
1606
    TidyAttr_UNKNOWN /* Must be last! */
1607
};
1608
1609
static Bool nodeHasAlignAttr( Node *node )
1610
2.58M
{
1611
    /* #define attrIsALIGN(av) AttrIsId( av, TidyAttr_ALIGN  ) */
1612
2.58M
    AttVal* av;
1613
3.08M
    for ( av = node->attributes; av != NULL; av = av->next ) {
1614
504k
        if (attrIsALIGN(av))
1615
5.68k
            return yes;
1616
504k
    }
1617
2.58M
    return no;
1618
2.58M
}
1619
1620
/*
1621
 *  Perform special checks for HTML, even when we're not using the default
1622
 *  option `--strict-tags-attributes yes`. This will ensure that HTML5 warning
1623
 *  and error output is given regardless of the new option, and ensure that
1624
 *  cleanup takes place. This provides mostly consistent Tidy behavior even with
1625
 *  the introduction of this new option. Note that strings have changed, though,
1626
 *  in order to maintain consistency with the `--strict-tags-attributes` 
1627
 *  messages.
1628
 *
1629
 *  See also: http://www.whatwg.org/specs/web-apps/current-work/multipage/obsolete.html#obsolete
1630
 */
1631
static void TY_(CheckHTML5)( TidyDocImpl* doc, Node* node )
1632
21.9k
{
1633
21.9k
    Stack *stack = TY_(newStack)(doc, 16);
1634
21.9k
    Bool clean = cfgBool( doc, TidyMakeClean );
1635
21.9k
    Bool already_strict = cfgBool( doc, TidyStrictTagsAttr );
1636
21.9k
    Node* body = TY_(FindBody)( doc );
1637
21.9k
    Node* next;
1638
21.9k
    Bool warn = yes;    /* should this be a warning, error, or report??? */
1639
21.9k
    AttVal* attr = NULL;
1640
21.9k
    int i = 0;
1641
1642
2.61M
    while (node)
1643
2.58M
    {
1644
2.58M
        next = node->next;
1645
        
1646
2.58M
        if ( nodeHasAlignAttr( node ) ) {
1647
            /* @todo: Is this for ALL elements that accept an 'align' attribute,
1648
             * or should this be a sub-set test?
1649
             */
1650
1651
            /* We will only emit this message if `--strict-tags-attributes==no`;
1652
             * otherwise if yes this message will be output during later
1653
             * checking.
1654
             */
1655
5.68k
            if ( !already_strict )
1656
5.68k
                TY_(ReportAttrError)(doc, node, TY_(AttrGetById)(node, TidyAttr_ALIGN), MISMATCHED_ATTRIBUTE_WARN);
1657
5.68k
        }
1658
2.58M
        if ( node == body ) {
1659
21.7k
            i = 0;
1660
            /* We will only emit these messages if `--strict-tags-attributes==no`;
1661
             * otherwise if yes these messages will be output during later
1662
             * checking.
1663
             */
1664
21.7k
            if ( !already_strict ) {
1665
151k
                while ( BadBody5Attribs[i] != TidyAttr_UNKNOWN ) {
1666
130k
                    attr = TY_(AttrGetById)(node, BadBody5Attribs[i]);
1667
130k
                    if ( attr )
1668
1
                        TY_(ReportAttrError)(doc, node, attr , MISMATCHED_ATTRIBUTE_WARN);
1669
130k
                    i++;
1670
130k
                }
1671
21.7k
            }
1672
21.7k
        } else
1673
2.56M
        if ( nodeIsACRONYM(node) ) {
1674
196
            if (clean) {
1675
                /* Replace with 'abbr' with warning to that effect.
1676
                 * Maybe should use static void RenameElem( TidyDocImpl* doc, Node* node, TidyTagId tid )
1677
                 */
1678
0
                TY_(CoerceNode)(doc, node, TidyTag_ABBR, warn, no);
1679
196
            } else {
1680
196
                if ( !already_strict )
1681
196
                    TY_(Report)(doc, node, node, REMOVED_HTML5);
1682
196
            }
1683
196
        } else
1684
2.56M
        if ( nodeIsAPPLET(node) ) {
1685
686
            if (clean) {
1686
                /* replace with 'object' with warning to that effect
1687
                 * maybe should use static void RenameElem( TidyDocImpl* doc, Node* node, TidyTagId tid )
1688
                 */
1689
0
                TY_(CoerceNode)(doc, node, TidyTag_OBJECT, warn, no);
1690
686
            } else {
1691
686
                if ( !already_strict )
1692
686
                    TY_(Report)(doc, node, node, REMOVED_HTML5);
1693
686
            }
1694
686
        } else
1695
2.56M
        if ( nodeIsBASEFONT(node) ) {
1696
            /* basefont: CSS equivalent 'font-size', 'font-family' and 'color' 
1697
             * on body or class on each subsequent element.
1698
             * Difficult - If it is the first body element, then could consider
1699
             * adding that to the <body> as a whole, else could perhaps apply it
1700
             * to all subsequent elements. But also in consideration is the fact
1701
             * that it was NOT supported in many browsers.
1702
             * - For now just report a warning
1703
             */
1704
203
            if ( !already_strict )
1705
203
                TY_(Report)(doc, node, node, REMOVED_HTML5);
1706
203
        } else
1707
2.56M
        if ( nodeIsBIG(node) ) {
1708
            /* big: CSS equivalent 'font-size:larger'
1709
             * so could replace the <big> ... </big> with
1710
             * <span style="font-size: larger"> ... </span>
1711
             * then replace <big> with <span>
1712
             * Need to think about that...
1713
             * Could use -
1714
             *   TY_(AddStyleProperty)( doc, node, "font-size: larger" );
1715
             *   TY_(CoerceNode)(doc, node, TidyTag_SPAN, no, no);
1716
             * Alternatively generated a <style> but how to get the style name
1717
             * TY_(AddAttribute)( doc, node, "class", "????" );
1718
             * Also maybe need a specific message like
1719
             * Element '%s' replaced with 'span' with a 'font-size: larger style attribute
1720
             * maybe should use static void RenameElem( TidyDocImpl* doc, Node* node, TidyTagId tid )
1721
             */
1722
298
            if (clean) {
1723
0
                TY_(AddStyleProperty)( doc, node, "font-size: larger" );
1724
0
                TY_(CoerceNode)(doc, node, TidyTag_SPAN, warn, no);
1725
298
            } else {
1726
298
                if ( !already_strict )
1727
298
                    TY_(Report)(doc, node, node, REMOVED_HTML5);
1728
298
            }
1729
298
        } else
1730
2.56M
        if ( nodeIsCENTER(node) ) {
1731
            /* center: CSS equivalent 'text-align:center'
1732
             * and 'margin-left:auto; margin-right:auto' on descendant blocks
1733
             * Tidy already handles this if 'clean' by SILENTLY generating the
1734
             * <style> and adding a <div class="c1"> around the elements.
1735
             * see: static Bool Center2Div( TidyDocImpl* doc, Node *node, Node **pnode)
1736
             */
1737
446
            if ( !already_strict )
1738
446
                TY_(Report)(doc, node, node, REMOVED_HTML5);
1739
446
        } else
1740
2.56M
        if ( nodeIsDIR(node) ) {
1741
            /* dir: replace by <ul>
1742
             * Tidy already actions this and issues a warning
1743
             * Should this be CHANGED???
1744
             */
1745
0
            if ( !already_strict )
1746
0
                TY_(Report)(doc, node, node, REMOVED_HTML5);
1747
0
        } else
1748
2.56M
        if ( nodeIsFONT(node) ) {
1749
            /* Tidy already handles this -
1750
             * If 'clean' replaced by CSS, else
1751
             * if is NOT clean, and doctype html5 then warnings issued
1752
             * done in Bool Font2Span( TidyDocImpl* doc, Node *node, Node **pnode ) (I think?)
1753
             */
1754
1.46M
            if ( !already_strict )
1755
1.46M
                TY_(Report)(doc, node, node, REMOVED_HTML5);
1756
1.46M
        } else
1757
1.10M
        if (( nodesIsFRAME(node) ) || ( nodeIsFRAMESET(node) ) || ( nodeIsNOFRAMES(node) )) {
1758
            /* YOW: What to do here?????? Maybe <iframe>????
1759
             */
1760
11.8k
            if ( !already_strict )
1761
11.8k
                TY_(Report)(doc, node, node, REMOVED_HTML5);
1762
11.8k
        } else
1763
1.09M
        if ( nodeIsSTRIKE(node) ) {
1764
            /* strike: CSS equivalent 'text-decoration:line-through'
1765
             * maybe should use static void RenameElem( TidyDocImpl* doc, Node* node, TidyTagId tid )
1766
             */
1767
194
            if (clean) {
1768
0
                TY_(AddStyleProperty)( doc, node, "text-decoration: line-through" );
1769
0
                TY_(CoerceNode)(doc, node, TidyTag_SPAN, warn, no);
1770
194
            } else {
1771
194
                if ( !already_strict )
1772
194
                    TY_(Report)(doc, node, node, REMOVED_HTML5);
1773
194
            }
1774
194
        } else
1775
1.09M
        if ( nodeIsTT(node) ) {
1776
            /* tt: CSS equivalent 'font-family:monospace'
1777
             * Tidy presently does nothing. Tidy5 issues a warning
1778
             * But like the 'clean' <font> replacement this could also be replaced with CSS
1779
             * maybe should use static void RenameElem( TidyDocImpl* doc, Node* node, TidyTagId tid )
1780
             */
1781
1.25k
            if (clean) {
1782
0
                TY_(AddStyleProperty)( doc, node, "font-family: monospace" );
1783
0
                TY_(CoerceNode)(doc, node, TidyTag_SPAN, warn, no);
1784
1.25k
            } else {
1785
1.25k
                if ( !already_strict )
1786
1.25k
                    TY_(Report)(doc, node, node, REMOVED_HTML5);
1787
1.25k
            }
1788
1.25k
        } else
1789
1.09M
            if (TY_(nodeIsElement)(node)) {
1790
792k
                if (node->tag) {
1791
780k
                    if ( (!(node->tag->versions & VERS_HTML5) && !(node->tag->versions & VERS_PROPRIETARY)) || (inRemovedInfo(node->tag->id)) ) {
1792
323
                        if ( !already_strict )
1793
323
                            TY_(Report)(doc, node, node, REMOVED_HTML5);
1794
323
                    }
1795
780k
                }
1796
792k
            }
1797
1798
2.58M
        if (node->content)
1799
2.13M
        {
1800
2.13M
            TY_(push)(stack, next);
1801
2.13M
            node = node->content;
1802
2.13M
            continue;
1803
2.13M
        }
1804
1805
453k
        node = next ? next : TY_(pop)(stack);
1806
453k
    }
1807
21.9k
    TY_(freeStack)(stack);
1808
21.9k
}
1809
/*****************************************************************************
1810
 *  END HTML5 STUFF
1811
 *****************************************************************************/
1812
1813
1814
/*
1815
 * Check and report HTML tags and attributes that are:
1816
 *  - Proprietary, and/or
1817
 *  - Not supported in the current version of HTML, defined as the version
1818
 *    of HTML that we are emitting.
1819
 * Proprietary items are reported as WARNINGS, and version mismatches will
1820
 * be reported as WARNING or ERROR in the following conditions:
1821
 *  - ERROR if the emitted doctype is a strict doctype.
1822
 *  - WARNING if the emitted doctype is a non-strict doctype.
1823
 * The propriety checks are *always* run as they have always been an integral
1824
 * part of Tidy. The version checks are controlled by `strict-tags-attributes`.
1825
 */
1826
static void TY_(CheckHTMLTagsAttribsVersions)( TidyDocImpl* doc, Node* node )
1827
22.1k
{
1828
22.1k
    Stack *stack = TY_(newStack)(doc, 16);
1829
22.1k
    Node *next;
1830
22.1k
    uint versionEmitted = doc->lexer->versionEmitted;
1831
22.1k
    uint declared = doc->lexer->doctype;
1832
22.1k
    uint version = versionEmitted == 0 ? declared : versionEmitted;
1833
22.1k
    int tagReportType = VERS_STRICT & version ? ELEMENT_VERS_MISMATCH_ERROR : ELEMENT_VERS_MISMATCH_WARN;
1834
22.1k
    int attrReportType = VERS_STRICT & version ? MISMATCHED_ATTRIBUTE_ERROR : MISMATCHED_ATTRIBUTE_WARN;
1835
22.1k
    Bool check_versions = cfgBool( doc, TidyStrictTagsAttr );
1836
22.1k
    AttVal *next_attr, *attval;
1837
22.1k
    Bool attrIsProprietary = no;
1838
22.1k
    Bool attrIsMismatched = yes;
1839
22.1k
    Bool tagLooksCustom = no;
1840
22.1k
    Bool htmlIs5 = (doc->lexer->doctype & VERS_HTML5) > 0;
1841
1842
2.61M
    while (node)
1843
2.59M
    {
1844
2.59M
        next = node->next;
1845
1846
        /* This bit here handles our HTML tags */
1847
2.59M
        if ( TY_(nodeIsElement)(node) && node->tag ) {
1848
1849
            /* Leave XML stuff alone. */
1850
2.28M
            if ( !cfgBool(doc, TidyXmlTags) )
1851
2.28M
            {
1852
                /* Version mismatches take priority. */
1853
2.28M
                if ( check_versions && !(node->tag->versions & version) )
1854
0
                {
1855
0
                    TY_(Report)(doc, NULL, node, tagReportType );
1856
0
                }
1857
                /* If it's not mismatched, it could still be proprietary. */
1858
2.28M
                else if ( node->tag->versions & VERS_PROPRIETARY )
1859
1.75k
                {
1860
1.75k
                    if ( !cfgBool(doc, TidyMakeClean) ||
1861
0
                        ( !nodeIsNOBR(node) && !nodeIsWBR(node) ) )
1862
1.75k
                    {
1863
                        /* It looks custom, despite whether it's a known tag. */
1864
1.75k
                        tagLooksCustom = TY_(nodeIsAutonomousCustomFormat)( node );
1865
1866
                        /* If we're in HTML5 mode and the tag does not look
1867
                           like a valid custom tag, then issue a warning.
1868
                           Appearance is good enough because invalid tags have
1869
                           been dropped. Also, if we're not in HTML5 mode, then
1870
                           then everything that reaches here gets the warning.
1871
                           Everything else can be ignored. */
1872
1873
1.75k
                        if ( (htmlIs5 && !tagLooksCustom) || !htmlIs5 )
1874
1.75k
                        {
1875
1.75k
                            TY_(Report)(doc, NULL, node, PROPRIETARY_ELEMENT );
1876
1.75k
                        }
1877
1878
1.75k
                        if ( nodeIsLAYER(node) )
1879
194
                            doc->badLayout |= USING_LAYER;
1880
1.56k
                        else if ( nodeIsSPACER(node) )
1881
195
                            doc->badLayout |= USING_SPACER;
1882
1.36k
                        else if ( nodeIsNOBR(node) )
1883
1.11k
                            doc->badLayout |= USING_NOBR;
1884
1.75k
                    }
1885
1.75k
                }
1886
2.28M
            }
1887
2.28M
        }
1888
1889
        /* And this bit here handles our attributes */
1890
2.59M
        if (TY_(nodeIsElement)(node))
1891
2.29M
        {
1892
2.29M
            attval = node->attributes;
1893
1894
2.77M
            while (attval)
1895
482k
            {
1896
482k
                next_attr = attval->next;
1897
1898
482k
                attrIsProprietary = TY_(AttributeIsProprietary)(node, attval);
1899
                /* Is. #729 - always check version match if HTML5 */
1900
482k
                attrIsMismatched = (check_versions | htmlIs5) ? TY_(AttributeIsMismatched)(node, attval, doc) : no;
1901
                /* Let the PROPRIETARY_ATTRIBUTE warning have precedence. */
1902
482k
                if ( attrIsProprietary )
1903
189k
                {
1904
189k
                    if ( cfgBool(doc, TidyWarnPropAttrs) )
1905
189k
                        TY_(ReportAttrError)(doc, node, attval, PROPRIETARY_ATTRIBUTE);
1906
189k
                }
1907
292k
                else if ( attrIsMismatched )
1908
433
                {
1909
433
                    if (htmlIs5) 
1910
433
                    { 
1911
                        /* Is. #729 - In html5 TidyStrictTagsAttr controls error or warn */
1912
433
                        TY_(ReportAttrError)(doc, node, attval,
1913
433
                            check_versions ? MISMATCHED_ATTRIBUTE_ERROR : MISMATCHED_ATTRIBUTE_WARN);
1914
433
                    }
1915
0
                    else
1916
0
                        TY_(ReportAttrError)(doc, node, attval, attrReportType);
1917
1918
433
                }
1919
1920
                /* @todo: do we need a new option to drop mismatches? Or should we
1921
                 simply drop them? */
1922
482k
                if ( ( attrIsProprietary || attrIsMismatched ) && cfgBool(doc, TidyDropPropAttrs) )
1923
0
                    TY_(RemoveAttribute)( doc, node, attval );
1924
1925
482k
                attval = next_attr;
1926
482k
            }
1927
2.29M
        }
1928
1929
2.59M
        if (node->content)
1930
2.13M
        {
1931
2.13M
            TY_(push)(stack, next);
1932
2.13M
            node = node->content;
1933
2.13M
            continue;
1934
2.13M
        }
1935
1936
459k
        node = next ? next : TY_(pop)(stack);
1937
459k
    }
1938
22.1k
    TY_(freeStack)(stack);
1939
22.1k
}
1940
1941
1942
#if defined(ENABLE_DEBUG_LOG)
1943
/* *** FOR DEBUG ONLY *** */
1944
const char *dbg_get_lexer_type( void *vp )
1945
{
1946
    Node *node = (Node *)vp;
1947
    switch ( node->type )
1948
    {
1949
    case RootNode:      return "Root";
1950
    case DocTypeTag:    return "DocType";
1951
    case CommentTag:    return "Comment";
1952
    case ProcInsTag:    return "ProcIns";
1953
    case TextNode:      return "Text";
1954
    case StartTag:      return "StartTag";
1955
    case EndTag:        return "EndTag";
1956
    case StartEndTag:   return "StartEnd";
1957
    case CDATATag:      return "CDATA";
1958
    case SectionTag:    return "Section";
1959
    case AspTag:        return "Asp";
1960
    case JsteTag:       return "Jste";
1961
    case PhpTag:        return "Php";
1962
    case XmlDecl:       return "XmlDecl";
1963
    }
1964
    return "Uncased";
1965
}
1966
1967
/* NOTE: THis matches the above lexer type, except when element has a name */
1968
const char *dbg_get_element_name( void *vp )
1969
{
1970
    Node *node = (Node *)vp;
1971
    switch ( node->type )
1972
    {
1973
    case TidyNode_Root:       return "Root";
1974
    case TidyNode_DocType:    return "DocType";
1975
    case TidyNode_Comment:    return "Comment";
1976
    case TidyNode_ProcIns:    return "ProcIns";
1977
    case TidyNode_Text:       return "Text";
1978
    case TidyNode_CDATA:      return "CDATA";
1979
    case TidyNode_Section:    return "Section";
1980
    case TidyNode_Asp:        return "Asp";
1981
    case TidyNode_Jste:       return "Jste";
1982
    case TidyNode_Php:        return "Php";
1983
    case TidyNode_XmlDecl:    return "XmlDecl";
1984
1985
    case TidyNode_Start:
1986
    case TidyNode_End:
1987
    case TidyNode_StartEnd:
1988
    default:
1989
        if (node->element)
1990
            return node->element;
1991
    }
1992
    return "Unknown";
1993
}
1994
1995
void dbg_show_node( TidyDocImpl* doc, Node *node, int caller, int indent )
1996
{
1997
    AttVal* av;
1998
    Lexer* lexer = doc->lexer;
1999
    ctmbstr call = "";
2000
    ctmbstr name = dbg_get_element_name(node);
2001
    ctmbstr type = dbg_get_lexer_type(node);
2002
    ctmbstr impl = node->implicit ? "implicit" : "";
2003
    switch ( caller )
2004
    {
2005
    case 1: call = "discard";   break;
2006
    case 2: call = "trim";      break;
2007
    case 3: call = "test";      break;
2008
    }
2009
    while (indent--)
2010
        SPRTF(" ");
2011
    if (strcmp(type,name))
2012
        SPRTF("%s %s %s %s", type, name, impl, call );
2013
    else
2014
        SPRTF("%s %s %s", name, impl, call );
2015
    if (lexer && (strcmp("Text",name) == 0)) {
2016
        uint len = node->end - node->start;
2017
        uint i;
2018
        SPRTF(" (%d) '", len);
2019
        if (len < 40) {
2020
            /* show it all */
2021
            for (i = node->start; i < node->end; i++) {
2022
                SPRTF("%c", lexer->lexbuf[i]);
2023
            }
2024
        } else {
2025
            /* partial display */
2026
            uint max = 19;
2027
            for (i = node->start; i < max; i++) {
2028
                SPRTF("%c", lexer->lexbuf[i]);
2029
            }
2030
            SPRTF("...");
2031
            i = node->end - 19;
2032
            for (; i < node->end; i++) {
2033
                SPRTF("%c", lexer->lexbuf[i]);
2034
            }
2035
        }
2036
        SPRTF("'");
2037
    }
2038
    for (av = node->attributes; av; av = av->next) {
2039
        name = av->attribute;
2040
        if (name) {
2041
            SPRTF(" %s",name);
2042
            if (av->value) {
2043
                SPRTF("=\"%s\"", av->value);
2044
            }
2045
        }
2046
    }
2047
2048
    SPRTF("\n");
2049
}
2050
2051
/* Make this non-recursive, because we really do want to eliminate
2052
   recursion that makes us crash, even when debugging.
2053
 */
2054
void dbg_show_all_nodes( TidyDocImpl* doc, Node *node, int indent )
2055
{
2056
    Stack *stack = TY_(newStack)(doc, 16);
2057
    Node *child = NULL;
2058
    Node *next = NULL;
2059
2060
    dbg_show_node( doc, node, 0, indent++ );
2061
2062
    if ( (child = node->content) )
2063
    {
2064
        while ( child )
2065
        {
2066
            if ( (next = child->next) )
2067
            {
2068
                next->idx = indent;
2069
            }
2070
            
2071
            dbg_show_node( doc, child, 0, indent );
2072
            
2073
            if (child->content)
2074
            {
2075
                TY_(push)(stack, next);
2076
                indent++;
2077
                child = child->content;
2078
                continue;
2079
            }
2080
2081
            if (next)
2082
            {
2083
                child = next;
2084
            }
2085
            else
2086
            {
2087
                if ( (child = TY_(pop)(stack)) )
2088
                {
2089
                    indent = child->idx;
2090
                }
2091
            }
2092
2093
        }
2094
        TY_(freeStack)(stack);
2095
    }
2096
}
2097
#endif
2098
2099
int         tidyDocCleanAndRepair( TidyDocImpl* doc )
2100
22.1k
{
2101
22.1k
    Bool word2K   = cfgBool( doc, TidyWord2000 );
2102
22.1k
    Bool logical  = cfgBool( doc, TidyLogicalEmphasis );
2103
22.1k
    Bool clean    = cfgBool( doc, TidyMakeClean );
2104
22.1k
    Bool gdoc     = cfgBool( doc, TidyGDocClean );
2105
22.1k
    Bool htmlOut  = cfgBool( doc, TidyHtmlOut );
2106
22.1k
    Bool xmlOut   = cfgBool( doc, TidyXmlOut );
2107
22.1k
    Bool xhtmlOut = cfgBool( doc, TidyXhtmlOut );
2108
22.1k
    Bool xmlDecl  = cfgBool( doc, TidyXmlDecl );
2109
22.1k
    Bool tidyMark = cfgBool( doc, TidyMark );
2110
22.1k
    Bool tidyXmlTags = cfgBool( doc, TidyXmlTags );
2111
22.1k
    Bool wantNameAttr = cfgBool( doc, TidyAnchorAsName );
2112
22.1k
    Bool mergeEmphasis = cfgBool( doc, TidyMergeEmphasis );
2113
22.1k
    Node* node;
2114
22.1k
    TidyConfigChangeCallback callback = doc->pConfigChangeCallback;
2115
22.1k
    doc->pConfigChangeCallback = NULL;
2116
2117
#if defined(ENABLE_DEBUG_LOG)
2118
    SPRTF("All nodes BEFORE clean and repair\n");
2119
    dbg_show_all_nodes( doc, &doc->root, 0  );
2120
#endif
2121
22.1k
    if (tidyXmlTags)
2122
0
    {
2123
0
        doc->pConfigChangeCallback = callback;
2124
0
        return tidyDocStatus( doc );
2125
0
    }
2126
2127
    /* Issue #567 - move style elements from body to head */
2128
22.1k
    TY_(CleanStyle)(doc, &doc->root);
2129
2130
    /* simplifies <b><b> ... </b> ...</b> etc. */
2131
22.1k
    if ( mergeEmphasis )
2132
22.1k
        TY_(NestedEmphasis)( doc, &doc->root );
2133
2134
    /* cleans up <dir>indented text</dir> etc. */
2135
22.1k
    TY_(List2BQ)( doc, &doc->root );
2136
22.1k
    TY_(BQ2Div)( doc, &doc->root );
2137
2138
    /* replaces i by em and b by strong */
2139
22.1k
    if ( logical )
2140
0
        TY_(EmFromI)( doc, &doc->root );
2141
2142
22.1k
    if ( word2K && TY_(IsWord2000)(doc) )
2143
0
    {
2144
        /* prune Word2000's <![if ...]> ... <![endif]> */
2145
0
        TY_(DropSections)( doc, &doc->root );
2146
2147
        /* drop style & class attributes and empty p, span elements */
2148
0
        TY_(CleanWord2000)( doc, &doc->root );
2149
0
        TY_(DropEmptyElements)(doc, &doc->root);
2150
0
    }
2151
2152
    /* replaces presentational markup by style rules */
2153
22.1k
    if ( clean )
2154
0
        TY_(CleanDocument)( doc );
2155
2156
    /* clean up html exported by Google Docs */
2157
22.1k
    if ( gdoc )
2158
0
        TY_(CleanGoogleDocument)( doc );
2159
2160
    /*  Reconcile http-equiv meta element with output encoding  */
2161
22.1k
    TY_(TidyMetaCharset)(doc);
2162
2163
22.1k
    if ( !TY_(CheckNodeIntegrity)( &doc->root ) )
2164
0
        TidyPanic( doc->allocator, integrity );
2165
2166
    /* remember given doctype for reporting */
2167
22.1k
    node = TY_(FindDocType)(doc);
2168
2169
22.1k
    if (node)
2170
2.62k
    {
2171
2.62k
        AttVal* fpi = TY_(GetAttrByName)(node, "PUBLIC");
2172
2.62k
        if (AttrHasValue(fpi))
2173
99
        {
2174
99
            if (doc->givenDoctype)
2175
99
                TidyDocFree(doc, doc->givenDoctype);
2176
99
            doc->givenDoctype = TY_(tmbstrdup)(doc->allocator,fpi->value);
2177
99
        }
2178
2.62k
    }
2179
2180
22.1k
    if ( doc->root.content )
2181
22.1k
    {
2182
        /* If we had XHTML input but want HTML output */
2183
22.1k
        if ( htmlOut && doc->lexer->isvoyager )
2184
0
        {
2185
0
            Node* node = TY_(FindDocType)(doc);
2186
            /* Remove reference, but do not free */
2187
0
            if (node)
2188
0
              TY_(RemoveNode)(node);
2189
0
        }
2190
2191
22.1k
        if (xhtmlOut && !htmlOut)
2192
22.1k
        {
2193
22.1k
            TY_(SetXHTMLDocType)(doc);
2194
22.1k
            TY_(FixAnchors)(doc, &doc->root, wantNameAttr, yes);
2195
22.1k
            TY_(FixXhtmlNamespace)(doc, yes);
2196
22.1k
            TY_(FixLanguageInformation)(doc, &doc->root, yes, yes);
2197
22.1k
        }
2198
0
        else
2199
0
        {
2200
0
            TY_(FixDocType)(doc);
2201
0
            TY_(FixAnchors)(doc, &doc->root, wantNameAttr, yes);
2202
0
            TY_(FixXhtmlNamespace)(doc, no);
2203
0
            TY_(FixLanguageInformation)(doc, &doc->root, no, yes);
2204
0
        }
2205
2206
22.1k
        if (tidyMark )
2207
22.1k
            TY_(AddGenerator)(doc);
2208
2209
22.1k
    }
2210
2211
    /* ensure presence of initial <?xml version="1.0"?> */
2212
22.1k
    if ( xmlOut && xmlDecl )
2213
0
        TY_(FixXmlDecl)( doc );
2214
2215
    /* At this point the apparent doctype is going to be as stable as
2216
       it can ever be, so we can start detecting things that shouldn't
2217
       be in this version of HTML
2218
     */
2219
22.1k
    if (doc->lexer) 
2220
22.1k
    {
2221
        /*\ 
2222
         *  Issue #429 #426 - These services can only be used
2223
         *  when there is a document loaded, ie a lexer created.
2224
         *  But really should not be calling a Clean and Repair
2225
         *  service with no doc!
2226
        \*/
2227
22.1k
        if (doc->lexer->versionEmitted & VERS_HTML5)
2228
21.9k
            TY_(CheckHTML5)( doc, &doc->root );
2229
22.1k
        TY_(CheckHTMLTagsAttribsVersions)( doc, &doc->root );
2230
2231
22.1k
        if ( !doc->lexer->isvoyager && doc->xmlDetected )
2232
145
        {
2233
145
            TY_(Report)(doc, NULL, TY_(FindXmlDecl)(doc), XML_DECLARATION_DETECTED );
2234
2235
145
        }
2236
22.1k
    }
2237
2238
22.1k
    TY_(CleanHead)(doc); /* Is #692 - discard multiple <title> tags */
2239
2240
#if defined(ENABLE_DEBUG_LOG)
2241
    SPRTF("All nodes AFTER clean and repair\n");
2242
    dbg_show_all_nodes( doc, &doc->root, 0  );
2243
#endif
2244
2245
22.1k
    doc->pConfigChangeCallback = callback;
2246
22.1k
    return tidyDocStatus( doc );
2247
22.1k
}
2248
2249
static
2250
Bool showBodyOnly( TidyDocImpl* doc, TidyTriState bodyOnly )
2251
22.1k
{
2252
22.1k
    Node* node;
2253
2254
22.1k
    switch( bodyOnly )
2255
22.1k
    {
2256
22.1k
    case TidyNoState:
2257
22.1k
        return no;
2258
0
    case TidyYesState:
2259
0
        return yes;
2260
0
    default:
2261
0
        node = TY_(FindBody)( doc );
2262
0
        if (node && node->implicit )
2263
0
            return yes;
2264
22.1k
    }
2265
0
    return no;
2266
22.1k
}
2267
2268
2269
int         tidyDocSaveStream( TidyDocImpl* doc, StreamOut* out )
2270
22.1k
{
2271
22.1k
    Bool showMarkup  = cfgBool( doc, TidyShowMarkup );
2272
22.1k
    Bool forceOutput = cfgBool( doc, TidyForceOutput );
2273
22.1k
    Bool outputBOM   = ( cfgAutoBool(doc, TidyOutputBOM) == TidyYesState );
2274
22.1k
    Bool smartBOM    = ( cfgAutoBool(doc, TidyOutputBOM) == TidyAutoState );
2275
22.1k
    Bool xmlOut      = cfgBool( doc, TidyXmlOut );
2276
22.1k
    Bool xhtmlOut    = cfgBool( doc, TidyXhtmlOut );
2277
22.1k
    TidyTriState bodyOnly    = cfgAutoBool( doc, TidyBodyOnly );
2278
2279
22.1k
    Bool dropComments = cfgBool(doc, TidyHideComments);
2280
22.1k
    Bool makeClean    = cfgBool(doc, TidyMakeClean);
2281
22.1k
    Bool asciiChars   = cfgBool(doc, TidyAsciiChars);
2282
22.1k
    Bool makeBare     = cfgBool(doc, TidyMakeBare);
2283
22.1k
    Bool escapeCDATA  = cfgBool(doc, TidyEscapeCdata);
2284
22.1k
    TidyAttrSortStrategy sortAttrStrat = cfg(doc, TidySortAttributes);
2285
22.1k
    TidyConfigChangeCallback callback = doc->pConfigChangeCallback;
2286
22.1k
    doc->pConfigChangeCallback = NULL;
2287
2288
    
2289
22.1k
    if (escapeCDATA)
2290
0
        TY_(ConvertCDATANodes)(doc, &doc->root);
2291
2292
22.1k
    if (dropComments)
2293
0
        TY_(DropComments)(doc, &doc->root);
2294
2295
22.1k
    if (makeClean)
2296
0
    {
2297
        /* noop */
2298
0
        TY_(DropFontElements)(doc, &doc->root, NULL);
2299
0
    }
2300
2301
22.1k
    if ((makeClean && asciiChars) || makeBare)
2302
0
        TY_(DowngradeTypography)(doc, &doc->root);
2303
2304
22.1k
    if (makeBare)
2305
        /* Note: no longer replaces &nbsp; in */
2306
        /* attribute values / non-text tokens */
2307
0
        TY_(NormalizeSpaces)(doc->lexer, &doc->root);
2308
22.1k
    else
2309
22.1k
        TY_(ReplacePreformattedSpaces)(doc, &doc->root);
2310
2311
22.1k
    TY_(SortAttributes)(doc, &doc->root, sortAttrStrat);
2312
2313
22.1k
    if ( showMarkup && (doc->errors == 0 || forceOutput) )
2314
22.1k
    {
2315
        /* Output a Byte Order Mark if required */
2316
22.1k
        if ( outputBOM || (doc->inputHadBOM && smartBOM) )
2317
0
            TY_(outBOM)( out );
2318
2319
        /* No longer necessary. No DOCTYPE == HTML 3.2,
2320
        ** which gives you only the basic character entities,
2321
        ** which are safe in any browser.
2322
        ** if ( !TY_(FindDocType)(doc) )
2323
        **    TY_(SetOptionBool)( doc, TidyNumEntities, yes );
2324
        */
2325
2326
22.1k
        doc->docOut = out;
2327
22.1k
        if ( xmlOut && !xhtmlOut )
2328
0
            TY_(PPrintXMLTree)( doc, NORMAL, 0, &doc->root );
2329
22.1k
        else if ( showBodyOnly( doc, bodyOnly ) )
2330
0
            TY_(PrintBody)( doc );
2331
22.1k
        else
2332
22.1k
            TY_(PPrintTree)( doc, NORMAL, 0, &doc->root );
2333
2334
22.1k
        TY_(PFlushLine)( doc, 0 );
2335
22.1k
        doc->docOut = NULL;
2336
22.1k
    }
2337
2338
    /* @jsd: removing this should solve #673, and allow saving of the buffer multiple times. */
2339
//    TY_(ResetConfigToSnapshot)( doc );
2340
22.1k
    doc->pConfigChangeCallback = callback;
2341
    
2342
22.1k
    return tidyDocStatus( doc );
2343
22.1k
}
2344
2345
/* Tree traversal functions
2346
**
2347
** The big issue here is the degree to which we should mimic
2348
** a DOM and/or SAX nodes.
2349
**
2350
** Is it 100% possible (and, if so, how difficult is it) to
2351
** emit SAX events from this API?  If SAX events are possible,
2352
** is that 100% of data needed to build a DOM?
2353
*/
2354
2355
TidyNode TIDY_CALL   tidyGetRoot( TidyDoc tdoc )
2356
0
{
2357
0
    TidyDocImpl* impl = tidyDocToImpl( tdoc );
2358
0
    Node* node = NULL;
2359
0
    if ( impl )
2360
0
        node = &impl->root;
2361
0
    return tidyImplToNode( node );
2362
0
}
2363
2364
TidyNode TIDY_CALL   tidyGetHtml( TidyDoc tdoc )
2365
0
{
2366
0
  TidyDocImpl* impl = tidyDocToImpl( tdoc );
2367
0
  Node* node = NULL;
2368
0
  if ( impl )
2369
0
      node = TY_(FindHTML)( impl );
2370
0
  return tidyImplToNode( node );
2371
0
}
2372
2373
TidyNode TIDY_CALL    tidyGetHead( TidyDoc tdoc )
2374
0
{
2375
0
  TidyDocImpl* impl = tidyDocToImpl( tdoc );
2376
0
  Node* node = NULL;
2377
0
  if ( impl )
2378
0
      node = TY_(FindHEAD)( impl );
2379
0
  return tidyImplToNode( node );
2380
0
}
2381
2382
TidyNode TIDY_CALL    tidyGetBody( TidyDoc tdoc )
2383
0
{
2384
0
  TidyDocImpl* impl = tidyDocToImpl( tdoc );
2385
0
  Node* node = NULL;
2386
0
  if ( impl )
2387
0
      node = TY_(FindBody)( impl );
2388
0
  return tidyImplToNode( node );
2389
0
}
2390
2391
/* parent / child */
2392
TidyNode TIDY_CALL    tidyGetParent( TidyNode tnod )
2393
0
{
2394
0
  Node* nimp = tidyNodeToImpl( tnod );
2395
0
  return tidyImplToNode( nimp->parent );
2396
0
}
2397
TidyNode TIDY_CALL    tidyGetChild( TidyNode tnod )
2398
0
{
2399
0
  Node* nimp = tidyNodeToImpl( tnod );
2400
0
  return tidyImplToNode( nimp->content );
2401
0
}
2402
2403
/* remove a node */
2404
TidyNode TIDY_CALL    tidyDiscardElement( TidyDoc tdoc, TidyNode tnod )
2405
0
{
2406
0
  TidyDocImpl* doc = tidyDocToImpl( tdoc );
2407
0
  Node* nimp = tidyNodeToImpl( tnod );
2408
0
  Node* next = TY_(DiscardElement)( doc, nimp );
2409
0
  return tidyImplToNode( next );
2410
0
}
2411
2412
/* siblings */
2413
TidyNode TIDY_CALL    tidyGetNext( TidyNode tnod )
2414
0
{
2415
0
  Node* nimp = tidyNodeToImpl( tnod );
2416
0
  return tidyImplToNode( nimp->next );
2417
0
}
2418
TidyNode TIDY_CALL    tidyGetPrev( TidyNode tnod )
2419
0
{
2420
0
  Node* nimp = tidyNodeToImpl( tnod );
2421
0
  return tidyImplToNode( nimp->prev );
2422
0
}
2423
2424
/* Node info */
2425
TidyNodeType TIDY_CALL tidyNodeGetType( TidyNode tnod )
2426
0
{
2427
0
  Node* nimp = tidyNodeToImpl( tnod );
2428
0
  TidyNodeType ntyp = TidyNode_Root;
2429
0
  if ( nimp )
2430
0
    ntyp = (TidyNodeType) nimp->type;
2431
0
  return ntyp;
2432
0
}
2433
2434
uint TIDY_CALL tidyNodeLine( TidyNode tnod )
2435
0
{
2436
0
  Node* nimp = tidyNodeToImpl( tnod );
2437
0
  uint line = 0;
2438
0
  if ( nimp )
2439
0
    line = nimp->line;
2440
0
  return line;
2441
0
}
2442
uint TIDY_CALL tidyNodeColumn( TidyNode tnod )
2443
0
{
2444
0
  Node* nimp = tidyNodeToImpl( tnod );
2445
0
  uint col = 0;
2446
0
  if ( nimp )
2447
0
    col = nimp->column;
2448
0
  return col;
2449
0
}
2450
2451
ctmbstr TIDY_CALL tidyNodeGetName( TidyNode tnod )
2452
0
{
2453
0
  Node* nimp = tidyNodeToImpl( tnod );
2454
0
  ctmbstr nnam = NULL;
2455
0
  if ( nimp )
2456
0
    nnam = nimp->element;
2457
0
  return nnam;
2458
0
}
2459
2460
2461
Bool TIDY_CALL tidyNodeHasText( TidyDoc tdoc, TidyNode tnod )
2462
0
{
2463
0
  TidyDocImpl* doc = tidyDocToImpl( tdoc );
2464
0
  if ( doc )
2465
0
      return TY_(nodeHasText)( doc, tidyNodeToImpl(tnod) );
2466
0
  return no;
2467
0
}
2468
2469
2470
Bool TIDY_CALL tidyNodeGetText( TidyDoc tdoc, TidyNode tnod, TidyBuffer* outbuf )
2471
0
{
2472
0
  TidyDocImpl* doc = tidyDocToImpl( tdoc );
2473
0
  Node* nimp = tidyNodeToImpl( tnod );
2474
0
  if ( doc && nimp && outbuf )
2475
0
  {
2476
0
      uint outenc     = cfg( doc, TidyOutCharEncoding );
2477
0
      uint nl         = cfg( doc, TidyNewline );
2478
0
      StreamOut* out  = TY_(BufferOutput)( doc, outbuf, outenc, nl );
2479
0
      Bool xmlOut     = cfgBool( doc, TidyXmlOut );
2480
0
      Bool xhtmlOut   = cfgBool( doc, TidyXhtmlOut );
2481
2482
0
      doc->docOut = out;
2483
0
      if ( xmlOut && !xhtmlOut )
2484
0
          TY_(PPrintXMLTree)( doc, NORMAL, 0, nimp );
2485
0
      else
2486
0
          TY_(PPrintTree)( doc, NORMAL, 0, nimp );
2487
2488
0
      TY_(PFlushLine)( doc, 0 );
2489
0
      doc->docOut = NULL;
2490
2491
0
      TidyDocFree( doc, out );
2492
0
      return yes;
2493
0
  }
2494
0
  return no;
2495
0
}
2496
2497
Bool TIDY_CALL tidyNodeGetValue( TidyDoc tdoc, TidyNode tnod, TidyBuffer* buf )
2498
0
{
2499
0
    TidyDocImpl *doc = tidyDocToImpl( tdoc );
2500
0
    Node *node = tidyNodeToImpl( tnod );
2501
0
    if ( doc == NULL || node == NULL || buf == NULL )
2502
0
        return no;
2503
2504
0
    switch( node->type ) {
2505
0
    case TextNode:
2506
0
    case CDATATag:
2507
0
    case CommentTag:
2508
0
    case ProcInsTag:
2509
0
    case SectionTag:
2510
0
    case AspTag:
2511
0
    case JsteTag:
2512
0
    case PhpTag:
2513
0
    {
2514
0
        tidyBufClear( buf );
2515
0
        tidyBufAppend( buf, doc->lexer->lexbuf + node->start,
2516
0
                       node->end - node->start );
2517
0
        break;
2518
0
    }
2519
0
    default:
2520
        /* The node doesn't have a value */
2521
0
        return no;
2522
0
    }
2523
2524
0
    return yes;
2525
0
}
2526
2527
Bool TIDY_CALL tidyNodeIsProp( TidyDoc ARG_UNUSED(tdoc), TidyNode tnod )
2528
0
{
2529
0
  Node* nimp = tidyNodeToImpl( tnod );
2530
0
  Bool isProprietary = yes;
2531
0
  if ( nimp )
2532
0
  {
2533
0
    switch ( nimp->type )
2534
0
    {
2535
0
    case RootNode:
2536
0
    case DocTypeTag:
2537
0
    case CommentTag:
2538
0
    case XmlDecl:
2539
0
    case ProcInsTag:
2540
0
    case TextNode:
2541
0
    case CDATATag:
2542
0
        isProprietary = no;
2543
0
        break;
2544
2545
0
    case SectionTag:
2546
0
    case AspTag:
2547
0
    case JsteTag:
2548
0
    case PhpTag:
2549
0
        isProprietary = yes;
2550
0
        break;
2551
2552
0
    case StartTag:
2553
0
    case EndTag:
2554
0
    case StartEndTag:
2555
0
        isProprietary = ( nimp->tag
2556
0
                          ? (nimp->tag->versions&VERS_PROPRIETARY)!=0
2557
0
                          : yes );
2558
0
        break;
2559
0
    }
2560
0
  }
2561
0
  return isProprietary;
2562
0
}
2563
2564
TidyTagId TIDY_CALL tidyNodeGetId(TidyNode tnod)
2565
0
{
2566
0
    Node* nimp = tidyNodeToImpl(tnod);
2567
2568
0
    TidyTagId tagId = TidyTag_UNKNOWN;
2569
0
    if (nimp && nimp->tag)
2570
0
        tagId = nimp->tag->id;
2571
2572
0
    return tagId;
2573
0
}
2574
2575
2576
/* Iterate over attribute values */
2577
TidyAttr TIDY_CALL   tidyAttrFirst( TidyNode tnod )
2578
0
{
2579
0
  Node* nimp = tidyNodeToImpl( tnod );
2580
0
  AttVal* attval = NULL;
2581
0
  if ( nimp )
2582
0
    attval = nimp->attributes;
2583
0
  return tidyImplToAttr( attval );
2584
0
}
2585
TidyAttr TIDY_CALL    tidyAttrNext( TidyAttr tattr )
2586
0
{
2587
0
  AttVal* attval = tidyAttrToImpl( tattr );
2588
0
  AttVal* nxtval = NULL;
2589
0
  if ( attval )
2590
0
    nxtval = attval->next;
2591
0
  return tidyImplToAttr( nxtval );
2592
0
}
2593
2594
ctmbstr TIDY_CALL       tidyAttrName( TidyAttr tattr )
2595
0
{
2596
0
  AttVal* attval = tidyAttrToImpl( tattr );
2597
0
  ctmbstr anam = NULL;
2598
0
  if ( attval )
2599
0
    anam = attval->attribute;
2600
0
  return anam;
2601
0
}
2602
ctmbstr TIDY_CALL       tidyAttrValue( TidyAttr tattr )
2603
0
{
2604
0
  AttVal* attval = tidyAttrToImpl( tattr );
2605
0
  ctmbstr aval = NULL;
2606
0
  if ( attval )
2607
0
    aval = attval->value;
2608
0
  return aval;
2609
0
}
2610
2611
void TIDY_CALL           tidyAttrDiscard( TidyDoc tdoc, TidyNode tnod, TidyAttr tattr )
2612
0
{
2613
0
  TidyDocImpl* impl = tidyDocToImpl( tdoc );
2614
0
  Node* nimp = tidyNodeToImpl( tnod );
2615
0
  AttVal* attval = tidyAttrToImpl( tattr );
2616
0
  TY_(RemoveAttribute)( impl, nimp, attval );
2617
0
}
2618
2619
TidyAttrId TIDY_CALL tidyAttrGetId( TidyAttr tattr )
2620
0
{
2621
0
  AttVal* attval = tidyAttrToImpl( tattr );
2622
0
  TidyAttrId attrId = TidyAttr_UNKNOWN;
2623
0
  if ( attval && attval->dict )
2624
0
    attrId = attval->dict->id;
2625
0
  return attrId;
2626
0
}
2627
2628
TidyAttr TIDY_CALL tidyAttrGetById( TidyNode tnod, TidyAttrId attId )
2629
0
{
2630
0
    Node* nimp = tidyNodeToImpl(tnod);
2631
0
    return tidyImplToAttr( TY_(AttrGetById)( nimp, attId ) );
2632
0
}
2633
2634
2635
Bool TIDY_CALL tidyAttrIsEvent( TidyAttr tattr )
2636
0
{
2637
0
    return TY_(attrIsEvent)( tidyAttrToImpl(tattr) );
2638
0
}
2639
2640
2641
/*******************************************************************
2642
 ** Message Key Management
2643
 *******************************************************************/
2644
ctmbstr TIDY_CALL tidyErrorCodeAsKey(uint code)
2645
0
{
2646
0
    return TY_(tidyErrorCodeAsKey)( code );
2647
0
}
2648
2649
uint TIDY_CALL tidyErrorCodeFromKey(ctmbstr code)
2650
0
{
2651
0
    return TY_(tidyErrorCodeFromKey)( code );
2652
0
}
2653
2654
TidyIterator TIDY_CALL getErrorCodeList()
2655
0
{
2656
0
    return TY_(getErrorCodeList)();
2657
0
}
2658
2659
uint TIDY_CALL getNextErrorCode( TidyIterator* iter )
2660
0
{
2661
0
    return TY_(getNextErrorCode)(iter);
2662
0
}
2663
2664
2665
/*******************************************************************
2666
 ** Localization Support
2667
 *******************************************************************/
2668
2669
2670
Bool TIDY_CALL tidySetLanguage( ctmbstr languageCode )
2671
0
{
2672
0
    Bool result = TY_(tidySetLanguage)( languageCode );
2673
2674
0
    if ( result )
2675
0
        TY_(tidySetLanguageSetByUser)();
2676
2677
0
    return result;
2678
0
}
2679
2680
ctmbstr TIDY_CALL tidyGetLanguage()
2681
0
{
2682
0
    return TY_(tidyGetLanguage)();
2683
0
}
2684
2685
ctmbstr TIDY_CALL tidyLocalizedStringN( uint messageType, uint quantity )
2686
18.4M
{
2687
18.4M
    return TY_(tidyLocalizedStringN)( messageType, quantity);
2688
18.4M
}
2689
2690
ctmbstr TIDY_CALL tidyLocalizedString( uint messageType )
2691
47.6M
{
2692
47.6M
    return TY_(tidyLocalizedString)( messageType );
2693
47.6M
}
2694
2695
ctmbstr TIDY_CALL tidyDefaultStringN( uint messageType, uint quantity )
2696
18.4M
{
2697
18.4M
    return TY_(tidyDefaultStringN)( messageType, quantity);
2698
18.4M
}
2699
2700
ctmbstr TIDY_CALL tidyDefaultString( uint messageType )
2701
55.4M
{
2702
55.4M
    return TY_(tidyDefaultString)( messageType );
2703
55.4M
}
2704
2705
TidyIterator TIDY_CALL getStringKeyList()
2706
0
{
2707
0
    return TY_(getStringKeyList)();
2708
0
}
2709
2710
uint TIDY_CALL getNextStringKey( TidyIterator* iter )
2711
0
{
2712
0
    return TY_(getNextStringKey)( iter );
2713
0
}
2714
2715
TidyIterator TIDY_CALL getWindowsLanguageList()
2716
0
{
2717
0
    return TY_(getWindowsLanguageList)();
2718
0
}
2719
2720
//#define tidyOptionToImpl( topt )    ((const TidyOptionImpl*)(topt))
2721
//#define tidyImplToOption( option )  ((TidyOption)(option))
2722
2723
const tidyLocaleMapItem* TIDY_CALL getNextWindowsLanguage( TidyIterator* iter )
2724
0
{
2725
    /* Get a real structure */
2726
0
    const tidyLocaleMapItemImpl *item = TY_(getNextWindowsLanguage)( iter );
2727
2728
    /* Return it as the opaque version */
2729
0
    return ((tidyLocaleMapItem*)(item));
2730
0
}
2731
2732
2733
ctmbstr TIDY_CALL TidyLangWindowsName( const tidyLocaleMapItem *item )
2734
0
{
2735
0
    return TY_(TidyLangWindowsName)( (tidyLocaleMapItemImpl*)(item) );
2736
0
}
2737
2738
2739
ctmbstr TIDY_CALL TidyLangPosixName( const tidyLocaleMapItem *item )
2740
0
{
2741
0
    return TY_(TidyLangPosixName)( (tidyLocaleMapItemImpl*)(item) );
2742
0
}
2743
2744
2745
TidyIterator TIDY_CALL getInstalledLanguageList()
2746
0
{
2747
0
    return TY_(getInstalledLanguageList)();
2748
0
}
2749
2750
2751
ctmbstr TIDY_CALL getNextInstalledLanguage( TidyIterator* iter )
2752
0
{
2753
0
    return TY_(getNextInstalledLanguage)( iter );
2754
0
}
2755
2756
2757
2758
2759
/*
2760
 * local variables:
2761
 * mode: c
2762
 * indent-tabs-mode: nil
2763
 * c-basic-offset: 4
2764
 * eval: (c-set-offset 'substatement-open 0)
2765
 * end:
2766
 */