Coverage Report

Created: 2024-09-06 07:53

/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/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
        int res;
389
390
0
        res = xmlOpenCharEncodingHandler(encoding, /* output */ 1,
391
0
                                         &handler);
392
0
        if (res != XML_ERR_OK)
393
0
            htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
394
0
    } else {
395
        /*
396
         * Fallback to HTML when the encoding is unspecified
397
         */
398
0
        xmlOpenCharEncodingHandler("HTML", /* output */ 1, &handler);
399
0
    }
400
401
0
    return(handler);
402
0
}
403
404
/**
405
 * htmlBufNodeDumpFormat:
406
 * @buf:  the xmlBufPtr output
407
 * @doc:  the document
408
 * @cur:  the current node
409
 * @format:  should formatting spaces been added
410
 *
411
 * Dump an HTML node, recursive behaviour,children are printed too.
412
 *
413
 * Returns the number of byte written or -1 in case of error
414
 */
415
static size_t
416
htmlBufNodeDumpFormat(xmlBufPtr buf, xmlDocPtr doc, xmlNodePtr cur,
417
0
             int format) {
418
0
    size_t use;
419
0
    size_t ret;
420
0
    xmlOutputBufferPtr outbuf;
421
422
0
    if (cur == NULL) {
423
0
  return ((size_t) -1);
424
0
    }
425
0
    if (buf == NULL) {
426
0
  return ((size_t) -1);
427
0
    }
428
0
    outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
429
0
    if (outbuf == NULL)
430
0
  return ((size_t) -1);
431
0
    memset(outbuf, 0, sizeof(xmlOutputBuffer));
432
0
    outbuf->buffer = buf;
433
0
    outbuf->encoder = NULL;
434
0
    outbuf->writecallback = NULL;
435
0
    outbuf->closecallback = NULL;
436
0
    outbuf->context = NULL;
437
0
    outbuf->written = 0;
438
439
0
    use = xmlBufUse(buf);
440
0
    htmlNodeDumpFormatOutput(outbuf, doc, cur, NULL, format);
441
0
    if (outbuf->error)
442
0
        ret = (size_t) -1;
443
0
    else
444
0
        ret = xmlBufUse(buf) - use;
445
0
    xmlFree(outbuf);
446
0
    return (ret);
447
0
}
448
449
/**
450
 * htmlNodeDump:
451
 * @buf:  the HTML buffer output
452
 * @doc:  the document
453
 * @cur:  the current node
454
 *
455
 * Dump an HTML node, recursive behaviour,children are printed too,
456
 * and formatting returns are added.
457
 *
458
 * Returns the number of byte written or -1 in case of error
459
 */
460
int
461
0
htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur) {
462
0
    xmlBufPtr buffer;
463
0
    size_t ret1;
464
0
    int ret2;
465
466
0
    if ((buf == NULL) || (cur == NULL))
467
0
        return(-1);
468
469
0
    xmlInitParser();
470
0
    buffer = xmlBufFromBuffer(buf);
471
0
    if (buffer == NULL)
472
0
        return(-1);
473
474
0
    ret1 = htmlBufNodeDumpFormat(buffer, doc, cur, 1);
475
476
0
    ret2 = xmlBufBackToBuffer(buffer, buf);
477
478
0
    if ((ret1 == (size_t) -1) || (ret2 < 0))
479
0
        return(-1);
480
0
    return(ret1 > INT_MAX ? INT_MAX : ret1);
481
0
}
482
483
/**
484
 * htmlNodeDumpFileFormat:
485
 * @out:  the FILE pointer
486
 * @doc:  the document
487
 * @cur:  the current node
488
 * @encoding: the document encoding
489
 * @format:  should formatting spaces been added
490
 *
491
 * Dump an HTML node, recursive behaviour,children are printed too.
492
 *
493
 * TODO: if encoding == NULL try to save in the doc encoding
494
 *
495
 * returns: the number of byte written or -1 in case of failure.
496
 */
497
int
498
htmlNodeDumpFileFormat(FILE *out, xmlDocPtr doc,
499
0
                 xmlNodePtr cur, const char *encoding, int format) {
500
0
    xmlOutputBufferPtr buf;
501
0
    xmlCharEncodingHandlerPtr handler;
502
0
    int ret;
503
504
0
    xmlInitParser();
505
506
    /*
507
     * save the content to a temp buffer.
508
     */
509
0
    handler = htmlFindOutputEncoder(encoding);
510
0
    buf = xmlOutputBufferCreateFile(out, handler);
511
0
    if (buf == NULL)
512
0
        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
    *mem = NULL;
555
0
    *size = 0;
556
0
    if (cur == NULL)
557
0
  return;
558
559
0
    encoding = (const char *) htmlGetMetaEncoding(cur);
560
0
    handler = htmlFindOutputEncoder(encoding);
561
0
    buf = xmlAllocOutputBuffer(handler);
562
0
    if (buf == NULL)
563
0
  return;
564
565
0
    htmlDocContentDumpFormatOutput(buf, cur, NULL, format);
566
567
0
    xmlOutputBufferFlush(buf);
568
569
0
    if (!buf->error) {
570
0
        if (buf->conv != NULL) {
571
0
            *size = xmlBufUse(buf->conv);
572
0
            *mem = xmlStrndup(xmlBufContent(buf->conv), *size);
573
0
        } else {
574
0
            *size = xmlBufUse(buf->buffer);
575
0
            *mem = xmlStrndup(xmlBufContent(buf->buffer), *size);
576
0
        }
577
0
    }
578
579
0
    xmlOutputBufferClose(buf);
580
0
}
581
582
/**
583
 * htmlDocDumpMemory:
584
 * @cur:  the document
585
 * @mem:  OUT: the memory pointer
586
 * @size:  OUT: the memory length
587
 *
588
 * Dump an HTML document in memory and return the xmlChar * and it's size.
589
 * It's up to the caller to free the memory.
590
 */
591
void
592
0
htmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
593
0
  htmlDocDumpMemoryFormat(cur, mem, size, 1);
594
0
}
595
596
597
/************************************************************************
598
 *                  *
599
 *    Dumping HTML tree content to an I/O output buffer *
600
 *                  *
601
 ************************************************************************/
602
603
/**
604
 * htmlDtdDumpOutput:
605
 * @buf:  the HTML buffer output
606
 * @doc:  the document
607
 * @encoding:  the encoding string
608
 *
609
 * TODO: check whether encoding is needed
610
 *
611
 * Dump the HTML document DTD, if any.
612
 */
613
static void
614
htmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
615
0
            const char *encoding ATTRIBUTE_UNUSED) {
616
0
    xmlDtdPtr cur = doc->intSubset;
617
618
0
    if (cur == NULL) {
619
0
  htmlSaveErr(XML_SAVE_NO_DOCTYPE, (xmlNodePtr) doc, NULL);
620
0
  return;
621
0
    }
622
0
    xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
623
0
    xmlOutputBufferWriteString(buf, (const char *)cur->name);
624
0
    if (cur->ExternalID != NULL) {
625
0
  xmlOutputBufferWriteString(buf, " PUBLIC ");
626
0
  xmlOutputBufferWriteQuotedString(buf, cur->ExternalID);
627
0
  if (cur->SystemID != NULL) {
628
0
      xmlOutputBufferWriteString(buf, " ");
629
0
      xmlOutputBufferWriteQuotedString(buf, cur->SystemID);
630
0
  }
631
0
    } else if (cur->SystemID != NULL &&
632
0
         xmlStrcmp(cur->SystemID, BAD_CAST "about:legacy-compat")) {
633
0
  xmlOutputBufferWriteString(buf, " SYSTEM ");
634
0
  xmlOutputBufferWriteQuotedString(buf, cur->SystemID);
635
0
    }
636
0
    xmlOutputBufferWriteString(buf, ">\n");
637
0
}
638
639
/**
640
 * htmlAttrDumpOutput:
641
 * @buf:  the HTML buffer output
642
 * @doc:  the document
643
 * @cur:  the attribute pointer
644
 *
645
 * Dump an HTML attribute
646
 */
647
static void
648
0
htmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
649
0
    xmlChar *value;
650
651
    /*
652
     * The html output method should not escape a & character
653
     * occurring in an attribute value immediately followed by
654
     * a { character (see Section B.7.1 of the HTML 4.0 Recommendation).
655
     * This is implemented in xmlEncodeEntitiesReentrant
656
     */
657
658
0
    if (cur == NULL) {
659
0
  return;
660
0
    }
661
0
    xmlOutputBufferWriteString(buf, " ");
662
0
    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
663
0
        xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
664
0
  xmlOutputBufferWriteString(buf, ":");
665
0
    }
666
0
    xmlOutputBufferWriteString(buf, (const char *)cur->name);
667
0
    if ((cur->children != NULL) && (!htmlIsBooleanAttr(cur->name))) {
668
0
  value = xmlNodeListGetString(doc, cur->children, 0);
669
0
  if (value) {
670
0
      xmlOutputBufferWriteString(buf, "=");
671
0
      if ((cur->ns == NULL) && (cur->parent != NULL) &&
672
0
    (cur->parent->ns == NULL) &&
673
0
    ((!xmlStrcasecmp(cur->name, BAD_CAST "href")) ||
674
0
           (!xmlStrcasecmp(cur->name, BAD_CAST "action")) ||
675
0
     (!xmlStrcasecmp(cur->name, BAD_CAST "src")) ||
676
0
     ((!xmlStrcasecmp(cur->name, BAD_CAST "name")) &&
677
0
      (!xmlStrcasecmp(cur->parent->name, BAD_CAST "a"))))) {
678
0
    xmlChar *escaped;
679
0
    xmlChar *tmp = value;
680
681
0
    while (IS_BLANK_CH(*tmp)) tmp++;
682
683
    /*
684
                 * Angle brackets are technically illegal in URIs, but they're
685
                 * used in server side includes, for example. Curly brackets
686
                 * are illegal as well and often used in templates.
687
                 * Don't escape non-whitespace, printable ASCII chars for
688
                 * improved interoperability. Only escape space, control
689
                 * and non-ASCII chars.
690
     */
691
0
    escaped = xmlURIEscapeStr(tmp,
692
0
                        BAD_CAST "\"#$%&+,/:;<=>?@[\\]^`{|}");
693
0
    if (escaped != NULL) {
694
0
        xmlOutputBufferWriteQuotedString(buf, escaped);
695
0
        xmlFree(escaped);
696
0
    } else {
697
0
                    buf->error = XML_ERR_NO_MEMORY;
698
0
    }
699
0
      } else {
700
0
    xmlOutputBufferWriteQuotedString(buf, value);
701
0
      }
702
0
      xmlFree(value);
703
0
  } else  {
704
0
            buf->error = XML_ERR_NO_MEMORY;
705
0
  }
706
0
    }
707
0
}
708
709
/**
710
 * htmlNodeDumpFormatOutput:
711
 * @buf:  the HTML buffer output
712
 * @doc:  the document
713
 * @cur:  the current node
714
 * @encoding:  the encoding string (unused)
715
 * @format:  should formatting spaces been added
716
 *
717
 * Dump an HTML node, recursive behaviour,children are printed too.
718
 */
719
void
720
htmlNodeDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
721
                   xmlNodePtr cur, const char *encoding ATTRIBUTE_UNUSED,
722
0
                         int format) {
723
0
    xmlNodePtr root, parent;
724
0
    xmlAttrPtr attr;
725
0
    const htmlElemDesc * info;
726
727
0
    xmlInitParser();
728
729
0
    if ((cur == NULL) || (buf == NULL)) {
730
0
  return;
731
0
    }
732
733
0
    root = cur;
734
0
    parent = cur->parent;
735
0
    while (1) {
736
0
        switch (cur->type) {
737
0
        case XML_HTML_DOCUMENT_NODE:
738
0
        case XML_DOCUMENT_NODE:
739
0
            if (((xmlDocPtr) cur)->intSubset != NULL) {
740
0
                htmlDtdDumpOutput(buf, (xmlDocPtr) cur, NULL);
741
0
            }
742
0
            if (cur->children != NULL) {
743
                /* Always validate cur->parent when descending. */
744
0
                if (cur->parent == parent) {
745
0
                    parent = cur;
746
0
                    cur = cur->children;
747
0
                    continue;
748
0
                }
749
0
            } else {
750
0
                xmlOutputBufferWriteString(buf, "\n");
751
0
            }
752
0
            break;
753
754
0
        case XML_ELEMENT_NODE:
755
            /*
756
             * Some users like lxml are known to pass nodes with a corrupted
757
             * tree structure. Fall back to a recursive call to handle this
758
             * case.
759
             */
760
0
            if ((cur->parent != parent) && (cur->children != NULL)) {
761
0
                htmlNodeDumpFormatOutput(buf, doc, cur, encoding, format);
762
0
                break;
763
0
            }
764
765
            /*
766
             * Get specific HTML info for that node.
767
             */
768
0
            if (cur->ns == NULL)
769
0
                info = htmlTagLookup(cur->name);
770
0
            else
771
0
                info = NULL;
772
773
0
            xmlOutputBufferWriteString(buf, "<");
774
0
            if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
775
0
                xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
776
0
                xmlOutputBufferWriteString(buf, ":");
777
0
            }
778
0
            xmlOutputBufferWriteString(buf, (const char *)cur->name);
779
0
            if (cur->nsDef)
780
0
                xmlNsListDumpOutput(buf, cur->nsDef);
781
0
            attr = cur->properties;
782
0
            while (attr != NULL) {
783
0
                htmlAttrDumpOutput(buf, doc, attr);
784
0
                attr = attr->next;
785
0
            }
786
787
0
            if ((info != NULL) && (info->empty)) {
788
0
                xmlOutputBufferWriteString(buf, ">");
789
0
            } else if (cur->children == NULL) {
790
0
                if ((info != NULL) && (info->saveEndTag != 0) &&
791
0
                    (xmlStrcmp(BAD_CAST info->name, BAD_CAST "html")) &&
792
0
                    (xmlStrcmp(BAD_CAST info->name, BAD_CAST "body"))) {
793
0
                    xmlOutputBufferWriteString(buf, ">");
794
0
                } else {
795
0
                    xmlOutputBufferWriteString(buf, "></");
796
0
                    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
797
0
                        xmlOutputBufferWriteString(buf,
798
0
                                (const char *)cur->ns->prefix);
799
0
                        xmlOutputBufferWriteString(buf, ":");
800
0
                    }
801
0
                    xmlOutputBufferWriteString(buf, (const char *)cur->name);
802
0
                    xmlOutputBufferWriteString(buf, ">");
803
0
                }
804
0
            } else {
805
0
                xmlOutputBufferWriteString(buf, ">");
806
0
                if ((format) && (info != NULL) && (!info->isinline) &&
807
0
                    (cur->children->type != HTML_TEXT_NODE) &&
808
0
                    (cur->children->type != HTML_ENTITY_REF_NODE) &&
809
0
                    (cur->children != cur->last) &&
810
0
                    (cur->name != NULL) &&
811
0
                    (cur->name[0] != 'p')) /* p, pre, param */
812
0
                    xmlOutputBufferWriteString(buf, "\n");
813
0
                parent = cur;
814
0
                cur = cur->children;
815
0
                continue;
816
0
            }
817
818
0
            if ((format) && (cur->next != NULL) &&
819
0
                (info != NULL) && (!info->isinline)) {
820
0
                if ((cur->next->type != HTML_TEXT_NODE) &&
821
0
                    (cur->next->type != HTML_ENTITY_REF_NODE) &&
822
0
                    (parent != NULL) &&
823
0
                    (parent->name != NULL) &&
824
0
                    (parent->name[0] != 'p')) /* p, pre, param */
825
0
                    xmlOutputBufferWriteString(buf, "\n");
826
0
            }
827
828
0
            break;
829
830
0
        case XML_ATTRIBUTE_NODE:
831
0
            htmlAttrDumpOutput(buf, doc, (xmlAttrPtr) cur);
832
0
            break;
833
834
0
        case HTML_TEXT_NODE:
835
0
            if (cur->content == NULL)
836
0
                break;
837
0
            if (((cur->name == (const xmlChar *)xmlStringText) ||
838
0
                 (cur->name != (const xmlChar *)xmlStringTextNoenc)) &&
839
0
                ((parent == NULL) ||
840
0
                 ((xmlStrcasecmp(parent->name, BAD_CAST "script")) &&
841
0
                  (xmlStrcasecmp(parent->name, BAD_CAST "style"))))) {
842
0
                xmlChar *buffer;
843
844
0
                buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
845
0
                if (buffer == NULL) {
846
0
                    buf->error = XML_ERR_NO_MEMORY;
847
0
                    return;
848
0
                }
849
0
                xmlOutputBufferWriteString(buf, (const char *)buffer);
850
0
                xmlFree(buffer);
851
0
            } else {
852
0
                xmlOutputBufferWriteString(buf, (const char *)cur->content);
853
0
            }
854
0
            break;
855
856
0
        case HTML_COMMENT_NODE:
857
0
            if (cur->content != NULL) {
858
0
                xmlOutputBufferWriteString(buf, "<!--");
859
0
                xmlOutputBufferWriteString(buf, (const char *)cur->content);
860
0
                xmlOutputBufferWriteString(buf, "-->");
861
0
            }
862
0
            break;
863
864
0
        case HTML_PI_NODE:
865
0
            if (cur->name != NULL) {
866
0
                xmlOutputBufferWriteString(buf, "<?");
867
0
                xmlOutputBufferWriteString(buf, (const char *)cur->name);
868
0
                if (cur->content != NULL) {
869
0
                    xmlOutputBufferWriteString(buf, " ");
870
0
                    xmlOutputBufferWriteString(buf,
871
0
                            (const char *)cur->content);
872
0
                }
873
0
                xmlOutputBufferWriteString(buf, ">");
874
0
            }
875
0
            break;
876
877
0
        case HTML_ENTITY_REF_NODE:
878
0
            xmlOutputBufferWriteString(buf, "&");
879
0
            xmlOutputBufferWriteString(buf, (const char *)cur->name);
880
0
            xmlOutputBufferWriteString(buf, ";");
881
0
            break;
882
883
0
        case HTML_PRESERVE_NODE:
884
0
            if (cur->content != NULL) {
885
0
                xmlOutputBufferWriteString(buf, (const char *)cur->content);
886
0
            }
887
0
            break;
888
889
0
        default:
890
0
            break;
891
0
        }
892
893
0
        while (1) {
894
0
            if (cur == root)
895
0
                return;
896
0
            if (cur->next != NULL) {
897
0
                cur = cur->next;
898
0
                break;
899
0
            }
900
901
0
            cur = parent;
902
            /* cur->parent was validated when descending. */
903
0
            parent = cur->parent;
904
905
0
            if ((cur->type == XML_HTML_DOCUMENT_NODE) ||
906
0
                (cur->type == XML_DOCUMENT_NODE)) {
907
0
                xmlOutputBufferWriteString(buf, "\n");
908
0
            } else {
909
0
                if ((format) && (cur->ns == NULL))
910
0
                    info = htmlTagLookup(cur->name);
911
0
                else
912
0
                    info = NULL;
913
914
0
                if ((format) && (info != NULL) && (!info->isinline) &&
915
0
                    (cur->last->type != HTML_TEXT_NODE) &&
916
0
                    (cur->last->type != HTML_ENTITY_REF_NODE) &&
917
0
                    (cur->children != cur->last) &&
918
0
                    (cur->name != NULL) &&
919
0
                    (cur->name[0] != 'p')) /* p, pre, param */
920
0
                    xmlOutputBufferWriteString(buf, "\n");
921
922
0
                xmlOutputBufferWriteString(buf, "</");
923
0
                if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
924
0
                    xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
925
0
                    xmlOutputBufferWriteString(buf, ":");
926
0
                }
927
0
                xmlOutputBufferWriteString(buf, (const char *)cur->name);
928
0
                xmlOutputBufferWriteString(buf, ">");
929
930
0
                if ((format) && (info != NULL) && (!info->isinline) &&
931
0
                    (cur->next != NULL)) {
932
0
                    if ((cur->next->type != HTML_TEXT_NODE) &&
933
0
                        (cur->next->type != HTML_ENTITY_REF_NODE) &&
934
0
                        (parent != NULL) &&
935
0
                        (parent->name != NULL) &&
936
0
                        (parent->name[0] != 'p')) /* p, pre, param */
937
0
                        xmlOutputBufferWriteString(buf, "\n");
938
0
                }
939
0
            }
940
0
        }
941
0
    }
942
0
}
943
944
/**
945
 * htmlNodeDumpOutput:
946
 * @buf:  the HTML buffer output
947
 * @doc:  the document
948
 * @cur:  the current node
949
 * @encoding:  the encoding string (unused)
950
 *
951
 * Dump an HTML node, recursive behaviour,children are printed too,
952
 * and formatting returns/spaces are added.
953
 */
954
void
955
htmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
956
0
             xmlNodePtr cur, const char *encoding ATTRIBUTE_UNUSED) {
957
0
    htmlNodeDumpFormatOutput(buf, doc, cur, NULL, 1);
958
0
}
959
960
/**
961
 * htmlDocContentDumpFormatOutput:
962
 * @buf:  the HTML buffer output
963
 * @cur:  the document
964
 * @encoding:  the encoding string (unused)
965
 * @format:  should formatting spaces been added
966
 *
967
 * Dump an HTML document.
968
 */
969
void
970
htmlDocContentDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
971
                         const char *encoding ATTRIBUTE_UNUSED,
972
0
                               int format) {
973
0
    int type = 0;
974
0
    if (cur) {
975
0
        type = cur->type;
976
0
        cur->type = XML_HTML_DOCUMENT_NODE;
977
0
    }
978
0
    htmlNodeDumpFormatOutput(buf, cur, (xmlNodePtr) cur, NULL, format);
979
0
    if (cur)
980
0
        cur->type = (xmlElementType) type;
981
0
}
982
983
/**
984
 * htmlDocContentDumpOutput:
985
 * @buf:  the HTML buffer output
986
 * @cur:  the document
987
 * @encoding:  the encoding string (unused)
988
 *
989
 * Dump an HTML document. Formatting return/spaces are added.
990
 */
991
void
992
htmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
993
0
                   const char *encoding ATTRIBUTE_UNUSED) {
994
0
    htmlNodeDumpFormatOutput(buf, cur, (xmlNodePtr) cur, NULL, 1);
995
0
}
996
997
/************************************************************************
998
 *                  *
999
 *    Saving functions front-ends       *
1000
 *                  *
1001
 ************************************************************************/
1002
1003
/**
1004
 * htmlDocDump:
1005
 * @f:  the FILE*
1006
 * @cur:  the document
1007
 *
1008
 * Dump an HTML document to an open FILE.
1009
 *
1010
 * returns: the number of byte written or -1 in case of failure.
1011
 */
1012
int
1013
0
htmlDocDump(FILE *f, xmlDocPtr cur) {
1014
0
    xmlOutputBufferPtr buf;
1015
0
    xmlCharEncodingHandlerPtr handler = NULL;
1016
0
    const char *encoding;
1017
0
    int ret;
1018
1019
0
    xmlInitParser();
1020
1021
0
    if ((cur == NULL) || (f == NULL)) {
1022
0
  return(-1);
1023
0
    }
1024
1025
0
    encoding = (const char *) htmlGetMetaEncoding(cur);
1026
0
    handler = htmlFindOutputEncoder(encoding);
1027
0
    buf = xmlOutputBufferCreateFile(f, handler);
1028
0
    if (buf == NULL)
1029
0
        return(-1);
1030
0
    htmlDocContentDumpOutput(buf, cur, NULL);
1031
1032
0
    ret = xmlOutputBufferClose(buf);
1033
0
    return(ret);
1034
0
}
1035
1036
/**
1037
 * htmlSaveFile:
1038
 * @filename:  the filename (or URL)
1039
 * @cur:  the document
1040
 *
1041
 * Dump an HTML document to a file. If @filename is "-" the stdout file is
1042
 * used.
1043
 * returns: the number of byte written or -1 in case of failure.
1044
 */
1045
int
1046
0
htmlSaveFile(const char *filename, xmlDocPtr cur) {
1047
0
    xmlOutputBufferPtr buf;
1048
0
    xmlCharEncodingHandlerPtr handler = NULL;
1049
0
    const char *encoding;
1050
0
    int ret;
1051
1052
0
    if ((cur == NULL) || (filename == NULL))
1053
0
        return(-1);
1054
1055
0
    xmlInitParser();
1056
1057
0
    encoding = (const char *) htmlGetMetaEncoding(cur);
1058
0
    handler = htmlFindOutputEncoder(encoding);
1059
0
    buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
1060
0
    if (buf == NULL)
1061
0
        return(0);
1062
1063
0
    htmlDocContentDumpOutput(buf, cur, NULL);
1064
1065
0
    ret = xmlOutputBufferClose(buf);
1066
0
    return(ret);
1067
0
}
1068
1069
/**
1070
 * htmlSaveFileFormat:
1071
 * @filename:  the filename
1072
 * @cur:  the document
1073
 * @format:  should formatting spaces been added
1074
 * @encoding: the document encoding
1075
 *
1076
 * Dump an HTML document to a file using a given encoding.
1077
 *
1078
 * returns: the number of byte written or -1 in case of failure.
1079
 */
1080
int
1081
htmlSaveFileFormat(const char *filename, xmlDocPtr cur,
1082
0
             const char *encoding, int format) {
1083
0
    xmlOutputBufferPtr buf;
1084
0
    xmlCharEncodingHandlerPtr handler = NULL;
1085
0
    int ret;
1086
1087
0
    if ((cur == NULL) || (filename == NULL))
1088
0
        return(-1);
1089
1090
0
    xmlInitParser();
1091
1092
0
    handler = htmlFindOutputEncoder(encoding);
1093
0
    if (handler != NULL)
1094
0
        htmlSetMetaEncoding(cur, (const xmlChar *) handler->name);
1095
0
    else
1096
0
  htmlSetMetaEncoding(cur, (const xmlChar *) "UTF-8");
1097
1098
    /*
1099
     * save the content to a temp buffer.
1100
     */
1101
0
    buf = xmlOutputBufferCreateFilename(filename, handler, 0);
1102
0
    if (buf == NULL)
1103
0
        return(0);
1104
1105
0
    htmlDocContentDumpFormatOutput(buf, cur, encoding, format);
1106
1107
0
    ret = xmlOutputBufferClose(buf);
1108
0
    return(ret);
1109
0
}
1110
1111
/**
1112
 * htmlSaveFileEnc:
1113
 * @filename:  the filename
1114
 * @cur:  the document
1115
 * @encoding: the document encoding
1116
 *
1117
 * Dump an HTML document to a file using a given encoding
1118
 * and formatting returns/spaces are added.
1119
 *
1120
 * returns: the number of byte written or -1 in case of failure.
1121
 */
1122
int
1123
0
htmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
1124
0
    return(htmlSaveFileFormat(filename, cur, encoding, 1));
1125
0
}
1126
1127
#endif /* LIBXML_OUTPUT_ENABLED */
1128
1129
#endif /* LIBXML_HTML_ENABLED */