Coverage Report

Created: 2023-12-13 20:02

/src/libxml2/HTMLtree.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * HTMLtree.c : implementation of access function for an HTML tree.
3
 *
4
 * See Copyright for the status of this software.
5
 *
6
 * daniel@veillard.com
7
 */
8
9
10
#define IN_LIBXML
11
#include "libxml.h"
12
#ifdef LIBXML_HTML_ENABLED
13
14
#include <string.h> /* for memset() only ! */
15
#include <ctype.h>
16
#include <stdlib.h>
17
18
#include <libxml/xmlmemory.h>
19
#include <libxml/HTMLparser.h>
20
#include <libxml/HTMLtree.h>
21
#include <libxml/entities.h>
22
#include <libxml/valid.h>
23
#include <libxml/xmlerror.h>
24
#include <libxml/parserInternals.h>
25
#include <libxml/globals.h>
26
#include <libxml/uri.h>
27
28
#include "private/buf.h"
29
#include "private/error.h"
30
#include "private/io.h"
31
#include "private/save.h"
32
33
/************************************************************************
34
 *                  *
35
 *    Getting/Setting encoding meta tags      *
36
 *                  *
37
 ************************************************************************/
38
39
/**
40
 * htmlGetMetaEncoding:
41
 * @doc:  the document
42
 *
43
 * Encoding definition lookup in the Meta tags
44
 *
45
 * Returns the current encoding as flagged in the HTML source
46
 */
47
const xmlChar *
48
0
htmlGetMetaEncoding(htmlDocPtr doc) {
49
0
    htmlNodePtr cur;
50
0
    const xmlChar *content;
51
0
    const xmlChar *encoding;
52
53
0
    if (doc == NULL)
54
0
  return(NULL);
55
0
    cur = doc->children;
56
57
    /*
58
     * Search the html
59
     */
60
0
    while (cur != NULL) {
61
0
  if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
62
0
      if (xmlStrEqual(cur->name, BAD_CAST"html"))
63
0
    break;
64
0
      if (xmlStrEqual(cur->name, BAD_CAST"head"))
65
0
    goto found_head;
66
0
      if (xmlStrEqual(cur->name, BAD_CAST"meta"))
67
0
    goto found_meta;
68
0
  }
69
0
  cur = cur->next;
70
0
    }
71
0
    if (cur == NULL)
72
0
  return(NULL);
73
0
    cur = cur->children;
74
75
    /*
76
     * Search the head
77
     */
78
0
    while (cur != NULL) {
79
0
  if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
80
0
      if (xmlStrEqual(cur->name, BAD_CAST"head"))
81
0
    break;
82
0
      if (xmlStrEqual(cur->name, BAD_CAST"meta"))
83
0
    goto found_meta;
84
0
  }
85
0
  cur = cur->next;
86
0
    }
87
0
    if (cur == NULL)
88
0
  return(NULL);
89
0
found_head:
90
0
    cur = cur->children;
91
92
    /*
93
     * Search the meta elements
94
     */
95
0
found_meta:
96
0
    while (cur != NULL) {
97
0
  if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
98
0
      if (xmlStrEqual(cur->name, BAD_CAST"meta")) {
99
0
    xmlAttrPtr attr = cur->properties;
100
0
    int http;
101
0
    const xmlChar *value;
102
103
0
    content = NULL;
104
0
    http = 0;
105
0
    while (attr != NULL) {
106
0
        if ((attr->children != NULL) &&
107
0
            (attr->children->type == XML_TEXT_NODE) &&
108
0
            (attr->children->next == NULL)) {
109
0
      value = attr->children->content;
110
0
      if ((!xmlStrcasecmp(attr->name, BAD_CAST"http-equiv"))
111
0
       && (!xmlStrcasecmp(value, BAD_CAST"Content-Type")))
112
0
          http = 1;
113
0
      else if ((value != NULL)
114
0
       && (!xmlStrcasecmp(attr->name, BAD_CAST"content")))
115
0
          content = value;
116
0
      if ((http != 0) && (content != NULL))
117
0
          goto found_content;
118
0
        }
119
0
        attr = attr->next;
120
0
    }
121
0
      }
122
0
  }
123
0
  cur = cur->next;
124
0
    }
125
0
    return(NULL);
126
127
0
found_content:
128
0
    encoding = xmlStrstr(content, BAD_CAST"charset=");
129
0
    if (encoding == NULL)
130
0
  encoding = xmlStrstr(content, BAD_CAST"Charset=");
131
0
    if (encoding == NULL)
132
0
  encoding = xmlStrstr(content, BAD_CAST"CHARSET=");
133
0
    if (encoding != NULL) {
134
0
  encoding += 8;
135
0
    } else {
136
0
  encoding = xmlStrstr(content, BAD_CAST"charset =");
137
0
  if (encoding == NULL)
138
0
      encoding = xmlStrstr(content, BAD_CAST"Charset =");
139
0
  if (encoding == NULL)
140
0
      encoding = xmlStrstr(content, BAD_CAST"CHARSET =");
141
0
  if (encoding != NULL)
142
0
      encoding += 9;
143
0
    }
144
0
    if (encoding != NULL) {
145
0
  while ((*encoding == ' ') || (*encoding == '\t')) encoding++;
146
0
    }
147
0
    return(encoding);
148
0
}
149
150
/**
151
 * htmlSetMetaEncoding:
152
 * @doc:  the document
153
 * @encoding:  the encoding string
154
 *
155
 * Sets the current encoding in the Meta tags
156
 * NOTE: this will not change the document content encoding, just
157
 * the META flag associated.
158
 *
159
 * Returns 0 in case of success and -1 in case of error
160
 */
161
int
162
0
htmlSetMetaEncoding(htmlDocPtr doc, const xmlChar *encoding) {
163
0
    htmlNodePtr cur, meta = NULL, head = NULL;
164
0
    const xmlChar *content = NULL;
165
0
    char newcontent[100];
166
167
0
    newcontent[0] = 0;
168
169
0
    if (doc == NULL)
170
0
  return(-1);
171
172
    /* html isn't a real encoding it's just libxml2 way to get entities */
173
0
    if (!xmlStrcasecmp(encoding, BAD_CAST "html"))
174
0
        return(-1);
175
176
0
    if (encoding != NULL) {
177
0
  snprintf(newcontent, sizeof(newcontent), "text/html; charset=%s",
178
0
                (char *)encoding);
179
0
  newcontent[sizeof(newcontent) - 1] = 0;
180
0
    }
181
182
0
    cur = doc->children;
183
184
    /*
185
     * Search the html
186
     */
187
0
    while (cur != NULL) {
188
0
  if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
189
0
      if (xmlStrcasecmp(cur->name, BAD_CAST"html") == 0)
190
0
    break;
191
0
      if (xmlStrcasecmp(cur->name, BAD_CAST"head") == 0)
192
0
    goto found_head;
193
0
      if (xmlStrcasecmp(cur->name, BAD_CAST"meta") == 0)
194
0
    goto found_meta;
195
0
  }
196
0
  cur = cur->next;
197
0
    }
198
0
    if (cur == NULL)
199
0
  return(-1);
200
0
    cur = cur->children;
201
202
    /*
203
     * Search the head
204
     */
205
0
    while (cur != NULL) {
206
0
  if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
207
0
      if (xmlStrcasecmp(cur->name, BAD_CAST"head") == 0)
208
0
    break;
209
0
      if (xmlStrcasecmp(cur->name, BAD_CAST"meta") == 0) {
210
0
                head = cur->parent;
211
0
    goto found_meta;
212
0
            }
213
0
  }
214
0
  cur = cur->next;
215
0
    }
216
0
    if (cur == NULL)
217
0
  return(-1);
218
0
found_head:
219
0
    head = cur;
220
0
    if (cur->children == NULL)
221
0
        goto create;
222
0
    cur = cur->children;
223
224
0
found_meta:
225
    /*
226
     * Search and update all the remaining the meta elements carrying
227
     * encoding information
228
     */
229
0
    while (cur != NULL) {
230
0
  if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
231
0
      if (xmlStrcasecmp(cur->name, BAD_CAST"meta") == 0) {
232
0
    xmlAttrPtr attr = cur->properties;
233
0
    int http;
234
0
    const xmlChar *value;
235
236
0
    content = NULL;
237
0
    http = 0;
238
0
    while (attr != NULL) {
239
0
        if ((attr->children != NULL) &&
240
0
            (attr->children->type == XML_TEXT_NODE) &&
241
0
            (attr->children->next == NULL)) {
242
0
      value = attr->children->content;
243
0
      if ((!xmlStrcasecmp(attr->name, BAD_CAST"http-equiv"))
244
0
       && (!xmlStrcasecmp(value, BAD_CAST"Content-Type")))
245
0
          http = 1;
246
0
      else
247
0
                        {
248
0
                           if ((value != NULL) &&
249
0
                               (!xmlStrcasecmp(attr->name, BAD_CAST"content")))
250
0
             content = value;
251
0
                        }
252
0
            if ((http != 0) && (content != NULL))
253
0
          break;
254
0
        }
255
0
        attr = attr->next;
256
0
    }
257
0
    if ((http != 0) && (content != NULL)) {
258
0
        meta = cur;
259
0
        break;
260
0
    }
261
262
0
      }
263
0
  }
264
0
  cur = cur->next;
265
0
    }
266
0
create:
267
0
    if (meta == NULL) {
268
0
        if ((encoding != NULL) && (head != NULL)) {
269
            /*
270
             * Create a new Meta element with the right attributes
271
             */
272
273
0
            meta = xmlNewDocNode(doc, NULL, BAD_CAST"meta", NULL);
274
0
            if (head->children == NULL)
275
0
                xmlAddChild(head, meta);
276
0
            else
277
0
                xmlAddPrevSibling(head->children, meta);
278
0
            xmlNewProp(meta, BAD_CAST"http-equiv", BAD_CAST"Content-Type");
279
0
            xmlNewProp(meta, BAD_CAST"content", BAD_CAST newcontent);
280
0
        }
281
0
    } else {
282
        /* remove the meta tag if NULL is passed */
283
0
        if (encoding == NULL) {
284
0
            xmlUnlinkNode(meta);
285
0
            xmlFreeNode(meta);
286
0
        }
287
        /* change the document only if there is a real encoding change */
288
0
        else if (xmlStrcasestr(content, encoding) == NULL) {
289
0
            xmlSetProp(meta, BAD_CAST"content", BAD_CAST newcontent);
290
0
        }
291
0
    }
292
293
294
0
    return(0);
295
0
}
296
297
/**
298
 * booleanHTMLAttrs:
299
 *
300
 * These are the HTML attributes which will be output
301
 * in minimized form, i.e. <option selected="selected"> will be
302
 * output as <option selected>, as per XSLT 1.0 16.2 "HTML Output Method"
303
 *
304
 */
305
static const char* const htmlBooleanAttrs[] = {
306
  "checked", "compact", "declare", "defer", "disabled", "ismap",
307
  "multiple", "nohref", "noresize", "noshade", "nowrap", "readonly",
308
  "selected", NULL
309
};
310
311
312
/**
313
 * htmlIsBooleanAttr:
314
 * @name:  the name of the attribute to check
315
 *
316
 * Determine if a given attribute is a boolean attribute.
317
 *
318
 * returns: false if the attribute is not boolean, true otherwise.
319
 */
320
int
321
htmlIsBooleanAttr(const xmlChar *name)
322
244k
{
323
244k
    int i = 0;
324
325
3.38M
    while (htmlBooleanAttrs[i] != NULL) {
326
3.14M
        if (xmlStrcasecmp((const xmlChar *)htmlBooleanAttrs[i], name) == 0)
327
3.33k
            return 1;
328
3.14M
        i++;
329
3.14M
    }
330
241k
    return 0;
331
244k
}
332
333
#ifdef LIBXML_OUTPUT_ENABLED
334
/************************************************************************
335
 *                  *
336
 *      Output error handlers       *
337
 *                  *
338
 ************************************************************************/
339
/**
340
 * htmlSaveErrMemory:
341
 * @extra:  extra information
342
 *
343
 * Handle an out of memory condition
344
 */
345
static void
346
htmlSaveErrMemory(const char *extra)
347
0
{
348
0
    __xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra);
349
0
}
350
351
/**
352
 * htmlSaveErr:
353
 * @code:  the error number
354
 * @node:  the location of the error.
355
 * @extra:  extra information
356
 *
357
 * Handle an out of memory condition
358
 */
359
static void
360
htmlSaveErr(int code, xmlNodePtr node, const char *extra)
361
0
{
362
0
    const char *msg = NULL;
363
364
0
    switch(code) {
365
0
        case XML_SAVE_NOT_UTF8:
366
0
      msg = "string is not in UTF-8\n";
367
0
      break;
368
0
  case XML_SAVE_CHAR_INVALID:
369
0
      msg = "invalid character value\n";
370
0
      break;
371
0
  case XML_SAVE_UNKNOWN_ENCODING:
372
0
      msg = "unknown encoding %s\n";
373
0
      break;
374
0
  case XML_SAVE_NO_DOCTYPE:
375
0
      msg = "HTML has no DOCTYPE\n";
376
0
      break;
377
0
  default:
378
0
      msg = "unexpected error number\n";
379
0
    }
380
0
    __xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra);
381
0
}
382
383
/************************************************************************
384
 *                  *
385
 *    Dumping HTML tree content to a simple buffer    *
386
 *                  *
387
 ************************************************************************/
388
389
/**
390
 * htmlBufNodeDumpFormat:
391
 * @buf:  the xmlBufPtr output
392
 * @doc:  the document
393
 * @cur:  the current node
394
 * @format:  should formatting spaces been added
395
 *
396
 * Dump an HTML node, recursive behaviour,children are printed too.
397
 *
398
 * Returns the number of byte written or -1 in case of error
399
 */
400
static size_t
401
htmlBufNodeDumpFormat(xmlBufPtr buf, xmlDocPtr doc, xmlNodePtr cur,
402
0
             int format) {
403
0
    size_t use;
404
0
    int ret;
405
0
    xmlOutputBufferPtr outbuf;
406
407
0
    if (cur == NULL) {
408
0
  return (-1);
409
0
    }
410
0
    if (buf == NULL) {
411
0
  return (-1);
412
0
    }
413
0
    outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
414
0
    if (outbuf == NULL) {
415
0
        htmlSaveErrMemory("allocating HTML output buffer");
416
0
  return (-1);
417
0
    }
418
0
    memset(outbuf, 0, sizeof(xmlOutputBuffer));
419
0
    outbuf->buffer = buf;
420
0
    outbuf->encoder = NULL;
421
0
    outbuf->writecallback = NULL;
422
0
    outbuf->closecallback = NULL;
423
0
    outbuf->context = NULL;
424
0
    outbuf->written = 0;
425
426
0
    use = xmlBufUse(buf);
427
0
    htmlNodeDumpFormatOutput(outbuf, doc, cur, NULL, format);
428
0
    xmlFree(outbuf);
429
0
    ret = xmlBufUse(buf) - use;
430
0
    return (ret);
431
0
}
432
433
/**
434
 * htmlNodeDump:
435
 * @buf:  the HTML buffer output
436
 * @doc:  the document
437
 * @cur:  the current node
438
 *
439
 * Dump an HTML node, recursive behaviour,children are printed too,
440
 * and formatting returns are added.
441
 *
442
 * Returns the number of byte written or -1 in case of error
443
 */
444
int
445
0
htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur) {
446
0
    xmlBufPtr buffer;
447
0
    size_t ret;
448
449
0
    if ((buf == NULL) || (cur == NULL))
450
0
        return(-1);
451
452
0
    xmlInitParser();
453
0
    buffer = xmlBufFromBuffer(buf);
454
0
    if (buffer == NULL)
455
0
        return(-1);
456
457
0
    ret = htmlBufNodeDumpFormat(buffer, doc, cur, 1);
458
459
0
    xmlBufBackToBuffer(buffer);
460
461
0
    if (ret > INT_MAX)
462
0
        return(-1);
463
0
    return((int) ret);
464
0
}
465
466
/**
467
 * htmlNodeDumpFileFormat:
468
 * @out:  the FILE pointer
469
 * @doc:  the document
470
 * @cur:  the current node
471
 * @encoding: the document encoding
472
 * @format:  should formatting spaces been added
473
 *
474
 * Dump an HTML node, recursive behaviour,children are printed too.
475
 *
476
 * TODO: if encoding == NULL try to save in the doc encoding
477
 *
478
 * returns: the number of byte written or -1 in case of failure.
479
 */
480
int
481
htmlNodeDumpFileFormat(FILE *out, xmlDocPtr doc,
482
0
                 xmlNodePtr cur, const char *encoding, int format) {
483
0
    xmlOutputBufferPtr buf;
484
0
    xmlCharEncodingHandlerPtr handler = NULL;
485
0
    int ret;
486
487
0
    xmlInitParser();
488
489
0
    if (encoding != NULL) {
490
0
  xmlCharEncoding enc;
491
492
0
  enc = xmlParseCharEncoding(encoding);
493
0
  if (enc != XML_CHAR_ENCODING_UTF8) {
494
0
      handler = xmlFindCharEncodingHandler(encoding);
495
0
      if (handler == NULL)
496
0
    htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
497
0
  }
498
0
    } else {
499
        /*
500
         * Fallback to HTML or ASCII when the encoding is unspecified
501
         */
502
0
        if (handler == NULL)
503
0
            handler = xmlFindCharEncodingHandler("HTML");
504
0
        if (handler == NULL)
505
0
            handler = xmlFindCharEncodingHandler("ascii");
506
0
    }
507
508
    /*
509
     * save the content to a temp buffer.
510
     */
511
0
    buf = xmlOutputBufferCreateFile(out, handler);
512
0
    if (buf == NULL) return(0);
513
514
0
    htmlNodeDumpFormatOutput(buf, doc, cur, NULL, format);
515
516
0
    ret = xmlOutputBufferClose(buf);
517
0
    return(ret);
518
0
}
519
520
/**
521
 * htmlNodeDumpFile:
522
 * @out:  the FILE pointer
523
 * @doc:  the document
524
 * @cur:  the current node
525
 *
526
 * Dump an HTML node, recursive behaviour,children are printed too,
527
 * and formatting returns are added.
528
 */
529
void
530
0
htmlNodeDumpFile(FILE *out, xmlDocPtr doc, xmlNodePtr cur) {
531
0
    htmlNodeDumpFileFormat(out, doc, cur, NULL, 1);
532
0
}
533
534
/**
535
 * htmlDocDumpMemoryFormat:
536
 * @cur:  the document
537
 * @mem:  OUT: the memory pointer
538
 * @size:  OUT: the memory length
539
 * @format:  should formatting spaces been added
540
 *
541
 * Dump an HTML document in memory and return the xmlChar * and it's size.
542
 * It's up to the caller to free the memory.
543
 */
544
void
545
0
htmlDocDumpMemoryFormat(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
546
0
    xmlOutputBufferPtr buf;
547
0
    xmlCharEncodingHandlerPtr handler = NULL;
548
0
    const char *encoding;
549
550
0
    xmlInitParser();
551
552
0
    if ((mem == NULL) || (size == NULL))
553
0
        return;
554
0
    if (cur == NULL) {
555
0
  *mem = NULL;
556
0
  *size = 0;
557
0
  return;
558
0
    }
559
560
0
    encoding = (const char *) htmlGetMetaEncoding(cur);
561
562
0
    if (encoding != NULL) {
563
0
  xmlCharEncoding enc;
564
565
0
  enc = xmlParseCharEncoding(encoding);
566
0
  if (enc != XML_CHAR_ENCODING_UTF8) {
567
0
      handler = xmlFindCharEncodingHandler(encoding);
568
0
      if (handler == NULL)
569
0
                htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
570
571
0
  }
572
0
    } else {
573
        /*
574
         * Fallback to HTML or ASCII when the encoding is unspecified
575
         */
576
0
        if (handler == NULL)
577
0
            handler = xmlFindCharEncodingHandler("HTML");
578
0
        if (handler == NULL)
579
0
            handler = xmlFindCharEncodingHandler("ascii");
580
0
    }
581
582
0
    buf = xmlAllocOutputBufferInternal(handler);
583
0
    if (buf == NULL) {
584
0
  *mem = NULL;
585
0
  *size = 0;
586
0
  return;
587
0
    }
588
589
0
    htmlDocContentDumpFormatOutput(buf, cur, NULL, format);
590
591
0
    xmlOutputBufferFlush(buf);
592
0
    if (buf->conv != NULL) {
593
0
  *size = xmlBufUse(buf->conv);
594
0
  *mem = xmlStrndup(xmlBufContent(buf->conv), *size);
595
0
    } else {
596
0
  *size = xmlBufUse(buf->buffer);
597
0
  *mem = xmlStrndup(xmlBufContent(buf->buffer), *size);
598
0
    }
599
0
    (void)xmlOutputBufferClose(buf);
600
0
}
601
602
/**
603
 * htmlDocDumpMemory:
604
 * @cur:  the document
605
 * @mem:  OUT: the memory pointer
606
 * @size:  OUT: the memory length
607
 *
608
 * Dump an HTML document in memory and return the xmlChar * and it's size.
609
 * It's up to the caller to free the memory.
610
 */
611
void
612
0
htmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
613
0
  htmlDocDumpMemoryFormat(cur, mem, size, 1);
614
0
}
615
616
617
/************************************************************************
618
 *                  *
619
 *    Dumping HTML tree content to an I/O output buffer *
620
 *                  *
621
 ************************************************************************/
622
623
/**
624
 * htmlDtdDumpOutput:
625
 * @buf:  the HTML buffer output
626
 * @doc:  the document
627
 * @encoding:  the encoding string
628
 *
629
 * TODO: check whether encoding is needed
630
 *
631
 * Dump the HTML document DTD, if any.
632
 */
633
static void
634
htmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
635
0
            const char *encoding ATTRIBUTE_UNUSED) {
636
0
    xmlDtdPtr cur = doc->intSubset;
637
638
0
    if (cur == NULL) {
639
0
  htmlSaveErr(XML_SAVE_NO_DOCTYPE, (xmlNodePtr) doc, NULL);
640
0
  return;
641
0
    }
642
0
    xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
643
0
    xmlOutputBufferWriteString(buf, (const char *)cur->name);
644
0
    if (cur->ExternalID != NULL) {
645
0
  xmlOutputBufferWriteString(buf, " PUBLIC ");
646
0
  xmlBufWriteQuotedString(buf->buffer, cur->ExternalID);
647
0
  if (cur->SystemID != NULL) {
648
0
      xmlOutputBufferWriteString(buf, " ");
649
0
      xmlBufWriteQuotedString(buf->buffer, cur->SystemID);
650
0
  }
651
0
    } else if (cur->SystemID != NULL &&
652
0
         xmlStrcmp(cur->SystemID, BAD_CAST "about:legacy-compat")) {
653
0
  xmlOutputBufferWriteString(buf, " SYSTEM ");
654
0
  xmlBufWriteQuotedString(buf->buffer, cur->SystemID);
655
0
    }
656
0
    xmlOutputBufferWriteString(buf, ">\n");
657
0
}
658
659
/**
660
 * htmlAttrDumpOutput:
661
 * @buf:  the HTML buffer output
662
 * @doc:  the document
663
 * @cur:  the attribute pointer
664
 *
665
 * Dump an HTML attribute
666
 */
667
static void
668
0
htmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
669
0
    xmlChar *value;
670
671
    /*
672
     * The html output method should not escape a & character
673
     * occurring in an attribute value immediately followed by
674
     * a { character (see Section B.7.1 of the HTML 4.0 Recommendation).
675
     * This is implemented in xmlEncodeEntitiesReentrant
676
     */
677
678
0
    if (cur == NULL) {
679
0
  return;
680
0
    }
681
0
    xmlOutputBufferWriteString(buf, " ");
682
0
    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
683
0
        xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
684
0
  xmlOutputBufferWriteString(buf, ":");
685
0
    }
686
0
    xmlOutputBufferWriteString(buf, (const char *)cur->name);
687
0
    if ((cur->children != NULL) && (!htmlIsBooleanAttr(cur->name))) {
688
0
  value = xmlNodeListGetString(doc, cur->children, 0);
689
0
  if (value) {
690
0
      xmlOutputBufferWriteString(buf, "=");
691
0
      if ((cur->ns == NULL) && (cur->parent != NULL) &&
692
0
    (cur->parent->ns == NULL) &&
693
0
    ((!xmlStrcasecmp(cur->name, BAD_CAST "href")) ||
694
0
           (!xmlStrcasecmp(cur->name, BAD_CAST "action")) ||
695
0
     (!xmlStrcasecmp(cur->name, BAD_CAST "src")) ||
696
0
     ((!xmlStrcasecmp(cur->name, BAD_CAST "name")) &&
697
0
      (!xmlStrcasecmp(cur->parent->name, BAD_CAST "a"))))) {
698
0
    xmlChar *escaped;
699
0
    xmlChar *tmp = value;
700
701
0
    while (IS_BLANK_CH(*tmp)) tmp++;
702
703
    /*
704
     * the < and > have already been escaped at the entity level
705
     * And doing so here breaks server side includes
706
     */
707
0
    escaped = xmlURIEscapeStr(tmp, BAD_CAST"@/:=?;#%&,+<>");
708
0
    if (escaped != NULL) {
709
0
        xmlBufWriteQuotedString(buf->buffer, escaped);
710
0
        xmlFree(escaped);
711
0
    } else {
712
0
        xmlBufWriteQuotedString(buf->buffer, value);
713
0
    }
714
0
      } else {
715
0
    xmlBufWriteQuotedString(buf->buffer, value);
716
0
      }
717
0
      xmlFree(value);
718
0
  } else  {
719
0
      xmlOutputBufferWriteString(buf, "=\"\"");
720
0
  }
721
0
    }
722
0
}
723
724
/**
725
 * htmlNodeDumpFormatOutput:
726
 * @buf:  the HTML buffer output
727
 * @doc:  the document
728
 * @cur:  the current node
729
 * @encoding:  the encoding string (unused)
730
 * @format:  should formatting spaces been added
731
 *
732
 * Dump an HTML node, recursive behaviour,children are printed too.
733
 */
734
void
735
htmlNodeDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
736
                   xmlNodePtr cur, const char *encoding ATTRIBUTE_UNUSED,
737
0
                         int format) {
738
0
    xmlNodePtr root, parent;
739
0
    xmlAttrPtr attr;
740
0
    const htmlElemDesc * info;
741
742
0
    xmlInitParser();
743
744
0
    if ((cur == NULL) || (buf == NULL)) {
745
0
  return;
746
0
    }
747
748
0
    root = cur;
749
0
    parent = cur->parent;
750
0
    while (1) {
751
0
        switch (cur->type) {
752
0
        case XML_HTML_DOCUMENT_NODE:
753
0
        case XML_DOCUMENT_NODE:
754
0
            if (((xmlDocPtr) cur)->intSubset != NULL) {
755
0
                htmlDtdDumpOutput(buf, (xmlDocPtr) cur, NULL);
756
0
            }
757
0
            if (cur->children != NULL) {
758
                /* Always validate cur->parent when descending. */
759
0
                if (cur->parent == parent) {
760
0
                    parent = cur;
761
0
                    cur = cur->children;
762
0
                    continue;
763
0
                }
764
0
            } else {
765
0
                xmlOutputBufferWriteString(buf, "\n");
766
0
            }
767
0
            break;
768
769
0
        case XML_ELEMENT_NODE:
770
            /*
771
             * Some users like lxml are known to pass nodes with a corrupted
772
             * tree structure. Fall back to a recursive call to handle this
773
             * case.
774
             */
775
0
            if ((cur->parent != parent) && (cur->children != NULL)) {
776
0
                htmlNodeDumpFormatOutput(buf, doc, cur, encoding, format);
777
0
                break;
778
0
            }
779
780
            /*
781
             * Get specific HTML info for that node.
782
             */
783
0
            if (cur->ns == NULL)
784
0
                info = htmlTagLookup(cur->name);
785
0
            else
786
0
                info = NULL;
787
788
0
            xmlOutputBufferWriteString(buf, "<");
789
0
            if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
790
0
                xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
791
0
                xmlOutputBufferWriteString(buf, ":");
792
0
            }
793
0
            xmlOutputBufferWriteString(buf, (const char *)cur->name);
794
0
            if (cur->nsDef)
795
0
                xmlNsListDumpOutput(buf, cur->nsDef);
796
0
            attr = cur->properties;
797
0
            while (attr != NULL) {
798
0
                htmlAttrDumpOutput(buf, doc, attr);
799
0
                attr = attr->next;
800
0
            }
801
802
0
            if ((info != NULL) && (info->empty)) {
803
0
                xmlOutputBufferWriteString(buf, ">");
804
0
            } else if (cur->children == NULL) {
805
0
                if ((info != NULL) && (info->saveEndTag != 0) &&
806
0
                    (xmlStrcmp(BAD_CAST info->name, BAD_CAST "html")) &&
807
0
                    (xmlStrcmp(BAD_CAST info->name, BAD_CAST "body"))) {
808
0
                    xmlOutputBufferWriteString(buf, ">");
809
0
                } else {
810
0
                    xmlOutputBufferWriteString(buf, "></");
811
0
                    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
812
0
                        xmlOutputBufferWriteString(buf,
813
0
                                (const char *)cur->ns->prefix);
814
0
                        xmlOutputBufferWriteString(buf, ":");
815
0
                    }
816
0
                    xmlOutputBufferWriteString(buf, (const char *)cur->name);
817
0
                    xmlOutputBufferWriteString(buf, ">");
818
0
                }
819
0
            } else {
820
0
                xmlOutputBufferWriteString(buf, ">");
821
0
                if ((format) && (info != NULL) && (!info->isinline) &&
822
0
                    (cur->children->type != HTML_TEXT_NODE) &&
823
0
                    (cur->children->type != HTML_ENTITY_REF_NODE) &&
824
0
                    (cur->children != cur->last) &&
825
0
                    (cur->name != NULL) &&
826
0
                    (cur->name[0] != 'p')) /* p, pre, param */
827
0
                    xmlOutputBufferWriteString(buf, "\n");
828
0
                parent = cur;
829
0
                cur = cur->children;
830
0
                continue;
831
0
            }
832
833
0
            if ((format) && (cur->next != NULL) &&
834
0
                (info != NULL) && (!info->isinline)) {
835
0
                if ((cur->next->type != HTML_TEXT_NODE) &&
836
0
                    (cur->next->type != HTML_ENTITY_REF_NODE) &&
837
0
                    (parent != NULL) &&
838
0
                    (parent->name != NULL) &&
839
0
                    (parent->name[0] != 'p')) /* p, pre, param */
840
0
                    xmlOutputBufferWriteString(buf, "\n");
841
0
            }
842
843
0
            break;
844
845
0
        case XML_ATTRIBUTE_NODE:
846
0
            htmlAttrDumpOutput(buf, doc, (xmlAttrPtr) cur);
847
0
            break;
848
849
0
        case HTML_TEXT_NODE:
850
0
            if (cur->content == NULL)
851
0
                break;
852
0
            if (((cur->name == (const xmlChar *)xmlStringText) ||
853
0
                 (cur->name != (const xmlChar *)xmlStringTextNoenc)) &&
854
0
                ((parent == NULL) ||
855
0
                 ((xmlStrcasecmp(parent->name, BAD_CAST "script")) &&
856
0
                  (xmlStrcasecmp(parent->name, BAD_CAST "style"))))) {
857
0
                xmlChar *buffer;
858
859
0
                buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
860
0
                if (buffer != NULL) {
861
0
                    xmlOutputBufferWriteString(buf, (const char *)buffer);
862
0
                    xmlFree(buffer);
863
0
                }
864
0
            } else {
865
0
                xmlOutputBufferWriteString(buf, (const char *)cur->content);
866
0
            }
867
0
            break;
868
869
0
        case HTML_COMMENT_NODE:
870
0
            if (cur->content != NULL) {
871
0
                xmlOutputBufferWriteString(buf, "<!--");
872
0
                xmlOutputBufferWriteString(buf, (const char *)cur->content);
873
0
                xmlOutputBufferWriteString(buf, "-->");
874
0
            }
875
0
            break;
876
877
0
        case HTML_PI_NODE:
878
0
            if (cur->name != NULL) {
879
0
                xmlOutputBufferWriteString(buf, "<?");
880
0
                xmlOutputBufferWriteString(buf, (const char *)cur->name);
881
0
                if (cur->content != NULL) {
882
0
                    xmlOutputBufferWriteString(buf, " ");
883
0
                    xmlOutputBufferWriteString(buf,
884
0
                            (const char *)cur->content);
885
0
                }
886
0
                xmlOutputBufferWriteString(buf, ">");
887
0
            }
888
0
            break;
889
890
0
        case HTML_ENTITY_REF_NODE:
891
0
            xmlOutputBufferWriteString(buf, "&");
892
0
            xmlOutputBufferWriteString(buf, (const char *)cur->name);
893
0
            xmlOutputBufferWriteString(buf, ";");
894
0
            break;
895
896
0
        case HTML_PRESERVE_NODE:
897
0
            if (cur->content != NULL) {
898
0
                xmlOutputBufferWriteString(buf, (const char *)cur->content);
899
0
            }
900
0
            break;
901
902
0
        default:
903
0
            break;
904
0
        }
905
906
0
        while (1) {
907
0
            if (cur == root)
908
0
                return;
909
0
            if (cur->next != NULL) {
910
0
                cur = cur->next;
911
0
                break;
912
0
            }
913
914
0
            cur = parent;
915
            /* cur->parent was validated when descending. */
916
0
            parent = cur->parent;
917
918
0
            if ((cur->type == XML_HTML_DOCUMENT_NODE) ||
919
0
                (cur->type == XML_DOCUMENT_NODE)) {
920
0
                xmlOutputBufferWriteString(buf, "\n");
921
0
            } else {
922
0
                if ((format) && (cur->ns == NULL))
923
0
                    info = htmlTagLookup(cur->name);
924
0
                else
925
0
                    info = NULL;
926
927
0
                if ((format) && (info != NULL) && (!info->isinline) &&
928
0
                    (cur->last->type != HTML_TEXT_NODE) &&
929
0
                    (cur->last->type != HTML_ENTITY_REF_NODE) &&
930
0
                    (cur->children != cur->last) &&
931
0
                    (cur->name != NULL) &&
932
0
                    (cur->name[0] != 'p')) /* p, pre, param */
933
0
                    xmlOutputBufferWriteString(buf, "\n");
934
935
0
                xmlOutputBufferWriteString(buf, "</");
936
0
                if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
937
0
                    xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
938
0
                    xmlOutputBufferWriteString(buf, ":");
939
0
                }
940
0
                xmlOutputBufferWriteString(buf, (const char *)cur->name);
941
0
                xmlOutputBufferWriteString(buf, ">");
942
943
0
                if ((format) && (info != NULL) && (!info->isinline) &&
944
0
                    (cur->next != NULL)) {
945
0
                    if ((cur->next->type != HTML_TEXT_NODE) &&
946
0
                        (cur->next->type != HTML_ENTITY_REF_NODE) &&
947
0
                        (parent != NULL) &&
948
0
                        (parent->name != NULL) &&
949
0
                        (parent->name[0] != 'p')) /* p, pre, param */
950
0
                        xmlOutputBufferWriteString(buf, "\n");
951
0
                }
952
0
            }
953
0
        }
954
0
    }
955
0
}
956
957
/**
958
 * htmlNodeDumpOutput:
959
 * @buf:  the HTML buffer output
960
 * @doc:  the document
961
 * @cur:  the current node
962
 * @encoding:  the encoding string (unused)
963
 *
964
 * Dump an HTML node, recursive behaviour,children are printed too,
965
 * and formatting returns/spaces are added.
966
 */
967
void
968
htmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
969
0
             xmlNodePtr cur, const char *encoding ATTRIBUTE_UNUSED) {
970
0
    htmlNodeDumpFormatOutput(buf, doc, cur, NULL, 1);
971
0
}
972
973
/**
974
 * htmlDocContentDumpFormatOutput:
975
 * @buf:  the HTML buffer output
976
 * @cur:  the document
977
 * @encoding:  the encoding string (unused)
978
 * @format:  should formatting spaces been added
979
 *
980
 * Dump an HTML document.
981
 */
982
void
983
htmlDocContentDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
984
                         const char *encoding ATTRIBUTE_UNUSED,
985
0
                               int format) {
986
0
    int type = 0;
987
0
    if (cur) {
988
0
        type = cur->type;
989
0
        cur->type = XML_HTML_DOCUMENT_NODE;
990
0
    }
991
0
    htmlNodeDumpFormatOutput(buf, cur, (xmlNodePtr) cur, NULL, format);
992
0
    if (cur)
993
0
        cur->type = (xmlElementType) type;
994
0
}
995
996
/**
997
 * htmlDocContentDumpOutput:
998
 * @buf:  the HTML buffer output
999
 * @cur:  the document
1000
 * @encoding:  the encoding string (unused)
1001
 *
1002
 * Dump an HTML document. Formatting return/spaces are added.
1003
 */
1004
void
1005
htmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
1006
0
                   const char *encoding ATTRIBUTE_UNUSED) {
1007
0
    htmlNodeDumpFormatOutput(buf, cur, (xmlNodePtr) cur, NULL, 1);
1008
0
}
1009
1010
/************************************************************************
1011
 *                  *
1012
 *    Saving functions front-ends       *
1013
 *                  *
1014
 ************************************************************************/
1015
1016
/**
1017
 * htmlDocDump:
1018
 * @f:  the FILE*
1019
 * @cur:  the document
1020
 *
1021
 * Dump an HTML document to an open FILE.
1022
 *
1023
 * returns: the number of byte written or -1 in case of failure.
1024
 */
1025
int
1026
0
htmlDocDump(FILE *f, xmlDocPtr cur) {
1027
0
    xmlOutputBufferPtr buf;
1028
0
    xmlCharEncodingHandlerPtr handler = NULL;
1029
0
    const char *encoding;
1030
0
    int ret;
1031
1032
0
    xmlInitParser();
1033
1034
0
    if ((cur == NULL) || (f == NULL)) {
1035
0
  return(-1);
1036
0
    }
1037
1038
0
    encoding = (const char *) htmlGetMetaEncoding(cur);
1039
1040
0
    if (encoding != NULL) {
1041
0
  xmlCharEncoding enc;
1042
1043
0
  enc = xmlParseCharEncoding(encoding);
1044
0
  if (enc != XML_CHAR_ENCODING_UTF8) {
1045
0
      handler = xmlFindCharEncodingHandler(encoding);
1046
0
      if (handler == NULL)
1047
0
    htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
1048
0
  }
1049
0
    } else {
1050
        /*
1051
         * Fallback to HTML or ASCII when the encoding is unspecified
1052
         */
1053
0
        if (handler == NULL)
1054
0
            handler = xmlFindCharEncodingHandler("HTML");
1055
0
        if (handler == NULL)
1056
0
            handler = xmlFindCharEncodingHandler("ascii");
1057
0
    }
1058
1059
0
    buf = xmlOutputBufferCreateFile(f, handler);
1060
0
    if (buf == NULL) return(-1);
1061
0
    htmlDocContentDumpOutput(buf, cur, NULL);
1062
1063
0
    ret = xmlOutputBufferClose(buf);
1064
0
    return(ret);
1065
0
}
1066
1067
/**
1068
 * htmlSaveFile:
1069
 * @filename:  the filename (or URL)
1070
 * @cur:  the document
1071
 *
1072
 * Dump an HTML document to a file. If @filename is "-" the stdout file is
1073
 * used.
1074
 * returns: the number of byte written or -1 in case of failure.
1075
 */
1076
int
1077
0
htmlSaveFile(const char *filename, xmlDocPtr cur) {
1078
0
    xmlOutputBufferPtr buf;
1079
0
    xmlCharEncodingHandlerPtr handler = NULL;
1080
0
    const char *encoding;
1081
0
    int ret;
1082
1083
0
    if ((cur == NULL) || (filename == NULL))
1084
0
        return(-1);
1085
1086
0
    xmlInitParser();
1087
1088
0
    encoding = (const char *) htmlGetMetaEncoding(cur);
1089
1090
0
    if (encoding != NULL) {
1091
0
  xmlCharEncoding enc;
1092
1093
0
  enc = xmlParseCharEncoding(encoding);
1094
0
  if (enc != XML_CHAR_ENCODING_UTF8) {
1095
0
      handler = xmlFindCharEncodingHandler(encoding);
1096
0
      if (handler == NULL)
1097
0
    htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
1098
0
  }
1099
0
    } else {
1100
        /*
1101
         * Fallback to HTML or ASCII when the encoding is unspecified
1102
         */
1103
0
        if (handler == NULL)
1104
0
            handler = xmlFindCharEncodingHandler("HTML");
1105
0
        if (handler == NULL)
1106
0
            handler = xmlFindCharEncodingHandler("ascii");
1107
0
    }
1108
1109
    /*
1110
     * save the content to a temp buffer.
1111
     */
1112
0
    buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
1113
0
    if (buf == NULL) return(0);
1114
1115
0
    htmlDocContentDumpOutput(buf, cur, NULL);
1116
1117
0
    ret = xmlOutputBufferClose(buf);
1118
0
    return(ret);
1119
0
}
1120
1121
/**
1122
 * htmlSaveFileFormat:
1123
 * @filename:  the filename
1124
 * @cur:  the document
1125
 * @format:  should formatting spaces been added
1126
 * @encoding: the document encoding
1127
 *
1128
 * Dump an HTML document to a file using a given encoding.
1129
 *
1130
 * returns: the number of byte written or -1 in case of failure.
1131
 */
1132
int
1133
htmlSaveFileFormat(const char *filename, xmlDocPtr cur,
1134
0
             const char *encoding, int format) {
1135
0
    xmlOutputBufferPtr buf;
1136
0
    xmlCharEncodingHandlerPtr handler = NULL;
1137
0
    int ret;
1138
1139
0
    if ((cur == NULL) || (filename == NULL))
1140
0
        return(-1);
1141
1142
0
    xmlInitParser();
1143
1144
0
    if (encoding != NULL) {
1145
0
  xmlCharEncoding enc;
1146
1147
0
  enc = xmlParseCharEncoding(encoding);
1148
0
  if (enc != XML_CHAR_ENCODING_UTF8) {
1149
0
      handler = xmlFindCharEncodingHandler(encoding);
1150
0
      if (handler == NULL)
1151
0
    htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
1152
0
  }
1153
0
        htmlSetMetaEncoding(cur, (const xmlChar *) encoding);
1154
0
    } else {
1155
0
  htmlSetMetaEncoding(cur, (const xmlChar *) "UTF-8");
1156
1157
        /*
1158
         * Fallback to HTML or ASCII when the encoding is unspecified
1159
         */
1160
0
        if (handler == NULL)
1161
0
            handler = xmlFindCharEncodingHandler("HTML");
1162
0
        if (handler == NULL)
1163
0
            handler = xmlFindCharEncodingHandler("ascii");
1164
0
    }
1165
1166
    /*
1167
     * save the content to a temp buffer.
1168
     */
1169
0
    buf = xmlOutputBufferCreateFilename(filename, handler, 0);
1170
0
    if (buf == NULL) return(0);
1171
1172
0
    htmlDocContentDumpFormatOutput(buf, cur, encoding, format);
1173
1174
0
    ret = xmlOutputBufferClose(buf);
1175
0
    return(ret);
1176
0
}
1177
1178
/**
1179
 * htmlSaveFileEnc:
1180
 * @filename:  the filename
1181
 * @cur:  the document
1182
 * @encoding: the document encoding
1183
 *
1184
 * Dump an HTML document to a file using a given encoding
1185
 * and formatting returns/spaces are added.
1186
 *
1187
 * returns: the number of byte written or -1 in case of failure.
1188
 */
1189
int
1190
0
htmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
1191
0
    return(htmlSaveFileFormat(filename, cur, encoding, 1));
1192
0
}
1193
1194
#endif /* LIBXML_OUTPUT_ENABLED */
1195
1196
#endif /* LIBXML_HTML_ENABLED */