Hubbub
before_html.c
Go to the documentation of this file.
1 /*
2  * This file is part of Hubbub.
3  * Licensed under the MIT License,
4  * http://www.opensource.org/licenses/mit-license.php
5  * Copyright 2008 John-Mark Bell <jmb@netsurf-browser.org>
6  */
7 
8 #include <assert.h>
9 #include <string.h>
10 
11 #include "treebuilder/modes.h"
12 #include "treebuilder/internal.h"
14 #include "utils/utils.h"
15 
16 
25  const hubbub_token *token)
26 {
27  hubbub_error err = HUBBUB_OK;
28  bool handled = false;
29 
30  switch (token->type) {
33  break;
35  err = process_comment_append(treebuilder, token,
36  treebuilder->context.document);
37  break;
39  err = process_characters_expect_whitespace(treebuilder,
40  token, false);
41  break;
43  {
45  &token->data.tag.name);
46 
47  if (type == HTML) {
48  handled = true;
49  } else {
50  err = HUBBUB_REPROCESS;
51  }
52  }
53  break;
55  case HUBBUB_TOKEN_EOF:
56  err = HUBBUB_REPROCESS;
57  break;
58  }
59 
60 
61  if (handled || err == HUBBUB_REPROCESS) {
62  hubbub_error e;
63  void *html, *appended;
64 
65  /* We can't use insert_element() here, as it assumes
66  * that we're inserting into current_node. There is
67  * no current_node to insert into at this point so
68  * we get to do it manually. */
69 
70  if (err == HUBBUB_REPROCESS) {
71  /* Need to manufacture html element */
72  hubbub_tag tag;
73 
74  tag.ns = HUBBUB_NS_HTML;
75  tag.name.ptr = (const uint8_t *) "html";
76  tag.name.len = SLEN("html");
77 
78  tag.n_attributes = 0;
79  tag.attributes = NULL;
80 
81  e = treebuilder->tree_handler->create_element(
82  treebuilder->tree_handler->ctx,
83  &tag, &html);
84  } else {
85  e = treebuilder->tree_handler->create_element(
86  treebuilder->tree_handler->ctx,
87  &token->data.tag, &html);
88  }
89 
90  if (e != HUBBUB_OK)
91  return e;
92 
93  e = treebuilder->tree_handler->append_child(
94  treebuilder->tree_handler->ctx,
95  treebuilder->context.document,
96  html, &appended);
97 
98  treebuilder->tree_handler->unref_node(
99  treebuilder->tree_handler->ctx,
100  html);
101 
102  if (e != HUBBUB_OK)
103  return e;
104 
105  /* We can't use element_stack_push() here, as it
106  * assumes that current_node is pointing at the index
107  * before the one to insert at. For the first entry in
108  * the stack, this does not hold so we must insert
109  * manually. */
110  treebuilder->context.element_stack[0].type = HTML;
111  treebuilder->context.element_stack[0].node = appended;
112  treebuilder->context.current_node = 0;
113 
116  treebuilder->context.mode = BEFORE_HEAD;
117  }
118 
119  return err;
120 }
121 
#define SLEN(s)
Definition: utils.h:21
hubbub_error process_comment_append(hubbub_treebuilder *treebuilder, const hubbub_token *token, void *parent)
Process a comment token, appending it to the given parent.
Definition: treebuilder.c:421
hubbub_token_type type
The token type.
Definition: types.h:120
void * ctx
Context pointer.
Definition: tree.h:292
hubbub_ns ns
Tag namespace.
Definition: types.h:109
Data for a tag.
Definition: types.h:108
Token data.
Definition: types.h:119
hubbub_string name
Tag name.
Definition: types.h:110
hubbub_tree_handler * tree_handler
Callback table.
Definition: internal.h:122
const uint8_t * ptr
Pointer to data.
Definition: types.h:77
element_type
Definition: internal.h:13
hubbub_error process_characters_expect_whitespace(hubbub_treebuilder *treebuilder, const hubbub_token *token, bool insert_into_current_node)
Process a character token in cases where we expect only whitespace.
Definition: treebuilder.c:375
Definition: internal.h:25
insertion_mode mode
The current insertion mode.
Definition: internal.h:75
size_t len
Byte length of string.
Definition: types.h:78
hubbub_treebuilder_context context
Our context.
Definition: internal.h:120
hubbub_error handle_before_html(hubbub_treebuilder *treebuilder, const hubbub_token *token)
Handle token in "before html" insertion mode.
Definition: before_html.c:24
hubbub_attribute * attributes
Array of attribute data.
Definition: types.h:112
hubbub_error
Definition: errors.h:18
void * node
Node pointer.
Definition: internal.h:54
hubbub_tree_unref_node unref_node
Unreference node.
Definition: tree.h:279
element_type type
Definition: treebuilder.c:26
No error.
Definition: errors.h:19
hubbub_tag tag
Definition: types.h:125
void * document
Pointer to the document node.
Definition: internal.h:93
hubbub_tree_create_element create_element
Create element.
Definition: tree.h:276
union hubbub_token::@3 data
Type-specific data.
element_type type
Element type.
Definition: internal.h:45
element_type element_type_from_name(hubbub_treebuilder *treebuilder, const hubbub_string *tag_name)
Convert an element name into an element type.
Definition: treebuilder.c:987
Treebuilder object.
Definition: internal.h:116
hubbub_tree_append_child append_child
Append child.
Definition: tree.h:280
element_context * element_stack
Stack of open elements.
Definition: internal.h:79
uint32_t n_attributes
Count of attributes.
Definition: types.h:111
uint32_t current_node
Index of current node in stack.
Definition: internal.h:81