File: domhtml5.c

package info (click to toggle)
tdom 0.9.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,260 kB
  • sloc: ansic: 56,762; xml: 20,797; tcl: 3,618; sh: 658; makefile: 83; cpp: 30
file content (310 lines) | stat: -rw-r--r-- 13,135 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*----------------------------------------------------------------------------
|   Copyright (c) 2017  Rolf Ade (rolf@pointsman.de)
|-----------------------------------------------------------------------------
|
|
|   The contents of this file are subject to the Mozilla Public License
|   Version 2.0 (the "License"); you may not use this file except in
|   compliance with the License. You may obtain a copy of the License at
|   http://www.mozilla.org/MPL/
|
|   Software distributed under the License is distributed on an "AS IS"
|   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|   License for the specific language governing rights and limitations
|   under the License.
|
|   Contributor(s):
|
|
|   written by Rolf Ade
|   March 2017
|
\---------------------------------------------------------------------------*/

#ifdef TDOM_HAVE_GUMBO

#define MAX_TAG_LEN 201

/*----------------------------------------------------------------------------
|   Includes
|
\---------------------------------------------------------------------------*/
#include <tcl.h>
#include <dom.h>
#include "gumbo.h"
#include <assert.h>

static const char *xhtml = "http://www.w3.org/1999/xhtml";
static const char *svg = "http://www.w3.org/2000/svg";
static const char *mathml = "http://www.w3.org/1998/Math/MathML";
static const char *xlink = "http://www.w3.org/1999/xlink";

#ifdef DEBUG
# define DBG(x) x
#else
# define DBG(x) 
#endif

static void
convertGumboToDom (
    domNode *parent,
    GumboNode *gumboParent,
    int ignoreWhiteSpaces,
    int ignorexmlns
    ) 
{
    int i, j, hnew;
    GumboVector *children = &gumboParent->v.element.children;
    GumboNode *child;
    GumboElement *gumboElm;
    GumboAttribute *gumboAtt;
    const char *tag;
    const char *attValue;
    const char *attUri = NULL;
    char buf[MAX_TAG_LEN];
    domNode *node;
    domNS *ns;
    domNodeType nodeType = ALL_NODES;
    domAttrNode *attr = NULL;
    Tcl_HashEntry *h;
    const char *elmns = NULL;
    
    for (i = 0; i < children->length; ++i) {
        child = (GumboNode*) (children->data[i]);
        switch (child->type) {
        case GUMBO_NODE_DOCUMENT:
            assert(false &&
                   "This gumbo node type can't happen here.");
            break;
        case GUMBO_NODE_ELEMENT:
        case GUMBO_NODE_TEMPLATE:
            gumboElm = &child->v.element;
            tag = gumbo_normalized_tagname(gumboElm->tag);
            if (!domIsNAME(tag)) {
                gumbo_tag_from_original_text(&gumboElm->original_tag);
                if (gumboElm->original_tag.length < MAX_TAG_LEN - 1) {
                    strncpy(&buf[0],
                            gumboElm->original_tag.data,
                            gumboElm->original_tag.length);
                    buf[gumboElm->original_tag.length] = '\0';
                    Tcl_UtfToLower(&buf[0]);
                    if (!domIsNAME(&buf[0])) {
                        DBG(fprintf (stderr, "invalid tag name '%s'\n", tag););
                        continue;
                    }
                    tag = &buf[0];
                } else {
                    /* Just skip this subtree */
                    DBG(fprintf(stderr, "long tag: %d bytes\n", gumboElm->original_tag.length););
                    continue;
                }
            }
            if (!ignorexmlns) {
                switch (gumboElm->tag_namespace) {
                case GUMBO_NAMESPACE_HTML:
                    elmns = xhtml;
                    break;
                case GUMBO_NAMESPACE_SVG:
                    elmns = svg;
                    break;
                case GUMBO_NAMESPACE_MATHML:
                    elmns = mathml;
                    break;
                default:
                    /* do nothing */
                    break;
                }
            }
            if (elmns == NULL) {
                node = domNewElementNode (parent->ownerDocument, tag);
            } else {
                DBG(fprintf (stderr, "namespaced node %s\n", tag););
                node = domNewElementNodeNS (parent->ownerDocument, tag,
                                            elmns);
            }
            domAppendChild(parent, node);
            for (j = 0; j < gumboElm->attributes.length; ++j) {
                gumboAtt = gumboElm->attributes.data[j];
                /* This is to ensure the same behavior as the -html
                 * parser: if there is just the attribute name given
                 * in the source (as 'selected' on a checkbox) then do
                 * it the XHTML style (att value is the att name,
                 * selected="selected"). If there is any value given
                 * in the source, including the empty string, use
                 * that. See gumbo.h for the details why/how this
                 * works.*/
                if (gumboAtt->original_value.data[0] != '"'
                    && gumboAtt->original_value.data[0] != '\'') {
                    attValue = gumboAtt->name;
                } else {
                    attValue = gumboAtt->value;
                }

                if (ignorexmlns) {
                    if (gumboAtt->attr_namespace != GUMBO_ATTR_NAMESPACE_NONE) {
                        if (gumboAtt->original_name.length < MAX_TAG_LEN - 1) {
                            strncpy(&buf[0],
                                    gumboAtt->original_name.data,
                                    gumboAtt->original_name.length);
                            buf[gumboAtt->original_name.length] = '\0';
                            Tcl_UtfToLower(&buf[0]);
                            DBG(fprintf (stderr, "original att name: %s\n",
                                         &buf[0]););
                            if (!domIsNAME(&buf[0])) {
                                DBG(fprintf (stderr, "invalid att name '%s'\n", tag););
                                continue;
                            }
                            domSetAttribute(node, &buf[0], attValue);
                        } else {
                            continue;
                        }
                    } else {
                        attr = domSetAttribute (node, gumboAtt->name, attValue);
                    }
                } else {
                    attUri = NULL;
                    switch (gumboAtt->attr_namespace) {
                    case GUMBO_ATTR_NAMESPACE_XLINK:
                        DBG(fprintf (stderr, "GUMBO_ATTR_NAMESPACE_XLINK\n"););
                        attUri = xlink;
                        break;
                    case GUMBO_ATTR_NAMESPACE_XMLNS:
                        DBG(fprintf (stderr, "GUMBO_ATTR_NAMESPACE_XMLNS\n"););
                        if (attValue[5] == ':') {
                            ns = domLookupPrefix (node, &(attValue[6]));
                        } else {
                            ns = domLookupPrefix (node, "");
                        }
                        DBG(fprintf (stderr, "xmns att name: %s\n att value %s\n",
                                     gumboAtt->name,
                                     attValue););
                        if (ns) {
                            if (strcmp(ns->uri, attValue) == 0) {
                                /* Namespace already in scope. Skip
                                 * this attribute to prevent invalid
                                 * double attributes and unnecessary
                                 * namespace declarations. */
                                DBG(fprintf (stderr, "namespace %s already in scope\n",
                                             attValue););
                                continue;
                            }
                        }
                        if (gumboAtt->original_name.length < MAX_TAG_LEN - 1) {
                            strncpy(&buf[0],
                                    gumboAtt->original_name.data,
                                    gumboAtt->original_name.length);
                            buf[gumboAtt->original_name.length] = '\0';
                            Tcl_UtfToLower(&buf[0]);
                            DBG(fprintf (stderr, "original att name: %s\n",
                                         &buf[0]););
                            if (!domIsNAME(&buf[0])) {
                                DBG(fprintf (stderr, "invalid att name '%s'\n", tag););
                                continue;
                            }
                            domSetAttributeNS(node, &buf[0], attValue, NULL, 1);
                        }
                        continue;
                    case GUMBO_ATTR_NAMESPACE_XML:
                        /* The xml namespace is always in scope, nothing
                         * to do. */
                        continue;
                    default:
                        break;
                    }

                    if (attUri) {
                        if (gumboAtt->original_name.length < MAX_TAG_LEN - 1) {
                            strncpy(&buf[0],
                                    gumboAtt->original_name.data,
                                    gumboAtt->original_name.length);
                            buf[gumboAtt->original_name.length] = '\0';
                            Tcl_UtfToLower(&buf[0]);
                            DBG(fprintf (stderr, "original att name: %s\n",
                                         &buf[0]););
                            if (!domIsNAME(&buf[0])) {
                                DBG(fprintf (stderr, "invalid att name '%s'\n", tag););
                                continue;
                            }
                        } else {
                            continue;
                        }
                        DBG(fprintf (stderr, "name: %s value %s\n", &buf[0], attValue););
                        attr = domSetAttributeNS (node, &buf[0],
                                                  attValue, xlink, 0);
                        DBG(fprintf(stderr, "attr: %p\n", attr););
                    } else {
                        attr = domSetAttribute (node, gumboAtt->name,
                                                attValue);
                    }
                }
                if (attr) {
                    if (strcmp(gumboAtt->name, "id") == 0) {
                        if (!node->ownerDocument->ids) {
                            node->ownerDocument->ids = (Tcl_HashTable *)
                                MALLOC (sizeof (Tcl_HashTable));
                            Tcl_InitHashTable (
                                node->ownerDocument->ids,
                                TCL_STRING_KEYS);
                        }
                        h = Tcl_CreateHashEntry (
                            node->ownerDocument->ids,
                            gumboAtt->value,
                            &hnew);
                        /* How to resolve in case of dublicates?  We
                           follow, what the core dom building code does:
                           the first value in document order wins. */
                        if (hnew) {
                            Tcl_SetHashValue (h, node);
                            attr->nodeFlags |= IS_ID_ATTRIBUTE;
                        }
                    }
                }
            }
            convertGumboToDom(node, child, ignoreWhiteSpaces, ignorexmlns);
            break;
        case GUMBO_NODE_WHITESPACE:
            if (ignoreWhiteSpaces) {
                continue;
            }
            /* fall through */;
        case GUMBO_NODE_CDATA:
        case GUMBO_NODE_TEXT:
            nodeType = TEXT_NODE
            /* fall through */;
        case GUMBO_NODE_COMMENT:
            if (nodeType == ALL_NODES) nodeType = COMMENT_NODE;
            node = (domNode*)domNewTextNode(parent->ownerDocument,
                                            child->v.text.text,
                                            strlen(child->v.text.text),
                                            nodeType);
            domAppendChild(parent, node);
            break;
        default:
            assert(false && "unknown node type");
        }
    }
}

domDocument *
HTML_GumboParseDocument (
    char   *html,              /* Complete text of the XML being parsed.  */
    int     ignoreWhiteSpaces,
    int     ignorexmlns
    ) {
    domDocument *doc = domCreateDoc(NULL, 0);
    GumboOutput *output = gumbo_parse(html);
    GumboDocument* doctype = & output->document->v.document;
    /* Generate and populate doctype info. */
    doc->doctype = (domDocInfo *)MALLOC(sizeof(domDocInfo));
    memset(doc->doctype, 0,(sizeof(domDocInfo)));
    doc->doctype->publicId = tdomstrdup(doctype->public_identifier);
    doc->doctype->systemId = tdomstrdup(doctype->system_identifier);
    convertGumboToDom (doc->rootNode, output->document, ignoreWhiteSpaces,
                       ignorexmlns);
    domSetDocumentElement (doc);
    gumbo_destroy_output(&kGumboDefaultOptions, output);    
    return doc;
}
#else
typedef int make_pedantic_compiler_happy;
#endif