Coverage Report

Created: 2025-07-11 06:47

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