nokogiri 1.11.7 → 1.12.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of nokogiri might be problematic. Click here for more details.

Files changed (110) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE-DEPENDENCIES.md +243 -22
  3. data/LICENSE.md +1 -1
  4. data/README.md +6 -5
  5. data/ext/nokogiri/depend +35 -34
  6. data/ext/nokogiri/extconf.rb +185 -103
  7. data/ext/nokogiri/gumbo.c +584 -0
  8. data/ext/nokogiri/{html_document.c → html4_document.c} +8 -8
  9. data/ext/nokogiri/{html_element_description.c → html4_element_description.c} +21 -19
  10. data/ext/nokogiri/{html_entity_lookup.c → html4_entity_lookup.c} +7 -7
  11. data/ext/nokogiri/{html_sax_parser_context.c → html4_sax_parser_context.c} +6 -5
  12. data/ext/nokogiri/{html_sax_push_parser.c → html4_sax_push_parser.c} +4 -4
  13. data/ext/nokogiri/libxml2_backwards_compat.c +30 -30
  14. data/ext/nokogiri/nokogiri.c +51 -38
  15. data/ext/nokogiri/nokogiri.h +19 -9
  16. data/ext/nokogiri/xml_document.c +14 -14
  17. data/ext/nokogiri/xml_element_content.c +2 -0
  18. data/ext/nokogiri/xml_encoding_handler.c +11 -6
  19. data/ext/nokogiri/xml_namespace.c +4 -2
  20. data/ext/nokogiri/xml_node.c +107 -100
  21. data/ext/nokogiri/xml_node_set.c +20 -20
  22. data/ext/nokogiri/xml_reader.c +2 -0
  23. data/ext/nokogiri/xml_sax_parser.c +6 -6
  24. data/ext/nokogiri/xml_sax_parser_context.c +2 -0
  25. data/ext/nokogiri/xml_schema.c +2 -0
  26. data/ext/nokogiri/xml_xpath_context.c +67 -65
  27. data/ext/nokogiri/xslt_stylesheet.c +2 -1
  28. data/gumbo-parser/CHANGES.md +63 -0
  29. data/gumbo-parser/Makefile +101 -0
  30. data/gumbo-parser/THANKS +27 -0
  31. data/gumbo-parser/src/Makefile +34 -0
  32. data/gumbo-parser/src/README.md +41 -0
  33. data/gumbo-parser/src/ascii.c +75 -0
  34. data/gumbo-parser/src/ascii.h +115 -0
  35. data/gumbo-parser/src/attribute.c +42 -0
  36. data/gumbo-parser/src/attribute.h +17 -0
  37. data/gumbo-parser/src/char_ref.c +22225 -0
  38. data/gumbo-parser/src/char_ref.h +29 -0
  39. data/gumbo-parser/src/char_ref.rl +2154 -0
  40. data/gumbo-parser/src/error.c +626 -0
  41. data/gumbo-parser/src/error.h +148 -0
  42. data/gumbo-parser/src/foreign_attrs.c +104 -0
  43. data/gumbo-parser/src/foreign_attrs.gperf +27 -0
  44. data/gumbo-parser/src/gumbo.h +943 -0
  45. data/gumbo-parser/src/insertion_mode.h +33 -0
  46. data/gumbo-parser/src/macros.h +91 -0
  47. data/gumbo-parser/src/parser.c +4886 -0
  48. data/gumbo-parser/src/parser.h +41 -0
  49. data/gumbo-parser/src/replacement.h +33 -0
  50. data/gumbo-parser/src/string_buffer.c +103 -0
  51. data/gumbo-parser/src/string_buffer.h +68 -0
  52. data/gumbo-parser/src/string_piece.c +48 -0
  53. data/gumbo-parser/src/svg_attrs.c +174 -0
  54. data/gumbo-parser/src/svg_attrs.gperf +77 -0
  55. data/gumbo-parser/src/svg_tags.c +137 -0
  56. data/gumbo-parser/src/svg_tags.gperf +55 -0
  57. data/gumbo-parser/src/tag.c +222 -0
  58. data/gumbo-parser/src/tag_lookup.c +382 -0
  59. data/gumbo-parser/src/tag_lookup.gperf +169 -0
  60. data/gumbo-parser/src/tag_lookup.h +13 -0
  61. data/gumbo-parser/src/token_buffer.c +79 -0
  62. data/gumbo-parser/src/token_buffer.h +71 -0
  63. data/gumbo-parser/src/token_type.h +17 -0
  64. data/gumbo-parser/src/tokenizer.c +3463 -0
  65. data/gumbo-parser/src/tokenizer.h +112 -0
  66. data/gumbo-parser/src/tokenizer_states.h +339 -0
  67. data/gumbo-parser/src/utf8.c +245 -0
  68. data/gumbo-parser/src/utf8.h +164 -0
  69. data/gumbo-parser/src/util.c +68 -0
  70. data/gumbo-parser/src/util.h +30 -0
  71. data/gumbo-parser/src/vector.c +111 -0
  72. data/gumbo-parser/src/vector.h +45 -0
  73. data/lib/nokogiri/css/parser.rb +1 -1
  74. data/lib/nokogiri/css/parser.y +1 -1
  75. data/lib/nokogiri/css/syntax_error.rb +1 -1
  76. data/lib/nokogiri/css.rb +14 -14
  77. data/lib/nokogiri/extension.rb +7 -2
  78. data/lib/nokogiri/gumbo.rb +14 -0
  79. data/lib/nokogiri/html.rb +31 -27
  80. data/lib/nokogiri/{html → html4}/builder.rb +2 -2
  81. data/lib/nokogiri/{html → html4}/document.rb +4 -4
  82. data/lib/nokogiri/{html → html4}/document_fragment.rb +3 -3
  83. data/lib/nokogiri/{html → html4}/element_description.rb +1 -1
  84. data/lib/nokogiri/{html → html4}/element_description_defaults.rb +1 -1
  85. data/lib/nokogiri/{html → html4}/entity_lookup.rb +1 -1
  86. data/lib/nokogiri/{html → html4}/sax/parser.rb +11 -14
  87. data/lib/nokogiri/html4/sax/parser_context.rb +19 -0
  88. data/lib/nokogiri/{html → html4}/sax/push_parser.rb +5 -5
  89. data/lib/nokogiri/html4.rb +40 -0
  90. data/lib/nokogiri/html5/document.rb +74 -0
  91. data/lib/nokogiri/html5/document_fragment.rb +80 -0
  92. data/lib/nokogiri/html5/node.rb +93 -0
  93. data/lib/nokogiri/html5.rb +473 -0
  94. data/lib/nokogiri/version/constant.rb +1 -1
  95. data/lib/nokogiri/version/info.rb +11 -2
  96. data/lib/nokogiri/xml/builder.rb +38 -0
  97. data/lib/nokogiri/xml/document.rb +46 -0
  98. data/lib/nokogiri/xml/node/save_options.rb +1 -1
  99. data/lib/nokogiri/xml/node.rb +6 -5
  100. data/lib/nokogiri/xml/parse_options.rb +2 -0
  101. data/lib/nokogiri/xml/pp.rb +2 -2
  102. data/lib/nokogiri/xml/sax/document.rb +24 -30
  103. data/lib/nokogiri/xml/sax.rb +4 -4
  104. data/lib/nokogiri/xml/xpath.rb +2 -2
  105. data/lib/nokogiri/xml.rb +35 -36
  106. data/lib/nokogiri/xslt/stylesheet.rb +1 -1
  107. data/lib/nokogiri/xslt.rb +16 -16
  108. data/lib/nokogiri.rb +31 -29
  109. metadata +100 -58
  110. data/lib/nokogiri/html/sax/parser_context.rb +0 -17
@@ -0,0 +1,245 @@
1
+ /*
2
+ Copyright 2018 Craig Barnes.
3
+ Copyright 2010 Google Inc.
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ https://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ */
17
+
18
+ #include "utf8.h"
19
+
20
+ #include <assert.h>
21
+ #include <stdint.h>
22
+ #include <string.h>
23
+
24
+ #include "error.h"
25
+ #include "gumbo.h"
26
+ #include "parser.h"
27
+ #include "ascii.h"
28
+ #include "vector.h"
29
+
30
+ // References:
31
+ // * https://tools.ietf.org/html/rfc3629
32
+ // * https://html.spec.whatwg.org/multipage/parsing.html#preprocessing-the-input-stream
33
+
34
+ // The following code is a DFA-based UTF-8 decoder by Bjoern Hoehrmann.
35
+ // We wrap the inner table-based decoder routine in our own handling for
36
+ // newlines, tabs, invalid continuation bytes, and other conditions that
37
+ // the HTML5 spec fully specifies but normal UTF-8 decoders do not handle.
38
+ // See https://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
39
+
40
+ // Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
41
+ //
42
+ // Permission is hereby granted, free of charge, to any person obtaining a
43
+ // copy of this software and associated documentation files (the "Software"),
44
+ // to deal in the Software without restriction, including without limitation
45
+ // the rights to use, copy, modify, merge, publish, distribute, sublicense,
46
+ // and/or sell copies of the Software, and to permit persons to whom the
47
+ // Software is furnished to do so, subject to the following conditions:
48
+ //
49
+ // The above copyright notice and this permission notice shall be included in
50
+ // all copies or substantial portions of the Software.
51
+
52
+ #define UTF8_ACCEPT 0
53
+ #define UTF8_REJECT 12
54
+
55
+ static const uint8_t utf8d[] = {
56
+ // The first part of the table maps bytes to character classes that
57
+ // to reduce the size of the transition table and create bitmasks.
58
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
59
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
60
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
61
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
62
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
63
+ 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
64
+ 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
65
+ 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
66
+
67
+ // The second part is a transition table that maps a combination
68
+ // of a state of the automaton and a character class to a state.
69
+ 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
70
+ 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
71
+ 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
72
+ 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
73
+ 12,36,12,12,12,12,12,12,12,12,12,12,
74
+ };
75
+
76
+ static inline uint32_t decode(uint32_t* state, uint32_t* codep, uint32_t byte) {
77
+ uint32_t type = utf8d[byte];
78
+
79
+ *codep =
80
+ (*state != UTF8_ACCEPT)
81
+ ? (byte & 0x3fu) | (*codep << 6)
82
+ : (0xff >> type) & (byte);
83
+
84
+ *state = utf8d[256 + *state + type];
85
+ return *state;
86
+ }
87
+
88
+ // END COPIED CODE.
89
+
90
+ // Adds a decoding error to the parser's error list, based on the current state
91
+ // of the Utf8Iterator.
92
+ static void add_error(Utf8Iterator* iter, GumboErrorType type) {
93
+ GumboParser* parser = iter->_parser;
94
+
95
+ GumboError* error = gumbo_add_error(parser);
96
+ if (!error) {
97
+ return;
98
+ }
99
+ error->type = type;
100
+ error->position = iter->_pos;
101
+ error->original_text.data = iter->_start;
102
+ error->original_text.length = iter->_width;
103
+ error->v.tokenizer.codepoint = iter->_current;
104
+ }
105
+
106
+ // Reads the next UTF-8 character in the iter.
107
+ // This assumes that iter->_start points to the beginning of the character.
108
+ // When this method returns, iter->_width and iter->_current will be set
109
+ // appropriately, as well as any error flags.
110
+ static void read_char(Utf8Iterator* iter) {
111
+ if (iter->_start >= iter->_end) {
112
+ // No input left to consume; emit an EOF and set width = 0.
113
+ iter->_current = -1;
114
+ iter->_width = 0;
115
+ return;
116
+ }
117
+
118
+ uint32_t code_point = 0;
119
+ uint32_t state = UTF8_ACCEPT;
120
+ for (const char* c = iter->_start; c < iter->_end; ++c) {
121
+ decode(&state, &code_point, (uint32_t)(unsigned char) (*c));
122
+ if (state == UTF8_ACCEPT) {
123
+ iter->_width = c - iter->_start + 1;
124
+ // This is the special handling for carriage returns that is mandated by
125
+ // the HTML5 spec. Since we're looking for particular 7-bit literal
126
+ // characters, we operate in terms of chars and only need a check for iter
127
+ // overrun, instead of having to read in a full next code point.
128
+ // https://html.spec.whatwg.org/multipage/parsing.html#preprocessing-the-input-stream
129
+ if (code_point == '\r') {
130
+ assert(iter->_width == 1);
131
+ const char* next = c + 1;
132
+ if (next < iter->_end && *next == '\n') {
133
+ // Advance the iter, as if the carriage return didn't exist.
134
+ ++iter->_start;
135
+ // Preserve the true offset, since other tools that look at it may be
136
+ // unaware of HTML5's rules for converting \r into \n.
137
+ ++iter->_pos.offset;
138
+ }
139
+ code_point = '\n';
140
+ }
141
+ iter->_current = code_point;
142
+ if (utf8_is_surrogate(code_point)) {
143
+ add_error(iter, GUMBO_ERR_SURROGATE_IN_INPUT_STREAM);
144
+ } else if (utf8_is_noncharacter(code_point)) {
145
+ add_error(iter, GUMBO_ERR_NONCHARACTER_IN_INPUT_STREAM);
146
+ } else if (utf8_is_control(code_point)
147
+ && !(gumbo_ascii_isspace(code_point) || code_point == 0)) {
148
+ add_error(iter, GUMBO_ERR_CONTROL_CHARACTER_IN_INPUT_STREAM);
149
+ }
150
+ return;
151
+ } else if (state == UTF8_REJECT) {
152
+ // We don't want to consume the invalid continuation byte of a multi-byte
153
+ // run, but we do want to skip past an invalid first byte.
154
+ iter->_width = c - iter->_start + (c == iter->_start);
155
+ iter->_current = kUtf8ReplacementChar;
156
+ add_error(iter, GUMBO_ERR_UTF8_INVALID);
157
+ return;
158
+ }
159
+ }
160
+ // If we got here without exiting early, then we've reached the end of the
161
+ // iterator. Add an error for truncated input, set the width to consume the
162
+ // rest of the iterator, and emit a replacement character. The next time we
163
+ // enter this method, it will detect that there's no input to consume and
164
+ // output an EOF.
165
+ iter->_width = iter->_end - iter->_start;
166
+ iter->_current = kUtf8ReplacementChar;
167
+ add_error(iter, GUMBO_ERR_UTF8_TRUNCATED);
168
+ }
169
+
170
+ static void update_position(Utf8Iterator* iter) {
171
+ iter->_pos.offset += iter->_width;
172
+ if (iter->_current == '\n') {
173
+ ++iter->_pos.line;
174
+ iter->_pos.column = 1;
175
+ } else if (iter->_current == '\t') {
176
+ int tab_stop = iter->_parser->_options->tab_stop;
177
+ iter->_pos.column = ((iter->_pos.column / tab_stop) + 1) * tab_stop;
178
+ } else if (iter->_current != -1) {
179
+ ++iter->_pos.column;
180
+ }
181
+ }
182
+
183
+ void utf8iterator_init (
184
+ GumboParser* parser,
185
+ const char* source,
186
+ size_t source_length,
187
+ Utf8Iterator* iter
188
+ ) {
189
+ iter->_start = source;
190
+ iter->_end = source + source_length;
191
+ iter->_pos.line = 1;
192
+ iter->_pos.column = 1;
193
+ iter->_pos.offset = 0;
194
+ iter->_parser = parser;
195
+ read_char(iter);
196
+ if (iter->_current == kUtf8BomChar) {
197
+ iter->_start += iter->_width;
198
+ iter->_pos.offset += iter->_width;
199
+ read_char(iter);
200
+ }
201
+ }
202
+
203
+ void utf8iterator_next(Utf8Iterator* iter) {
204
+ // We update positions based on the *last* character read, so that the first
205
+ // character following a newline is at column 1 in the next line.
206
+ update_position(iter);
207
+ iter->_start += iter->_width;
208
+ read_char(iter);
209
+ }
210
+
211
+ bool utf8iterator_maybe_consume_match (
212
+ Utf8Iterator* iter,
213
+ const char* prefix,
214
+ size_t length,
215
+ bool case_sensitive
216
+ ) {
217
+ bool matched =
218
+ (iter->_start + length <= iter->_end)
219
+ && (
220
+ case_sensitive
221
+ ? !strncmp(iter->_start, prefix, length)
222
+ : !gumbo_ascii_strncasecmp(iter->_start, prefix, length)
223
+ )
224
+ ;
225
+ if (matched) {
226
+ for (size_t i = 0; i < length; ++i) {
227
+ utf8iterator_next(iter);
228
+ }
229
+ return true;
230
+ } else {
231
+ return false;
232
+ }
233
+ }
234
+
235
+ void utf8iterator_mark(Utf8Iterator* iter) {
236
+ iter->_mark = iter->_start;
237
+ iter->_mark_pos = iter->_pos;
238
+ }
239
+
240
+ // Returns the current input stream position to the mark.
241
+ void utf8iterator_reset(Utf8Iterator* iter) {
242
+ iter->_start = iter->_mark;
243
+ iter->_pos = iter->_mark_pos;
244
+ read_char(iter);
245
+ }
@@ -0,0 +1,164 @@
1
+ #ifndef GUMBO_UTF8_H_
2
+ #define GUMBO_UTF8_H_
3
+
4
+ // This contains an implementation of a UTF-8 iterator and decoder suitable for
5
+ // a HTML5 parser. This does a bit more than straight UTF-8 decoding. The
6
+ // HTML5 spec specifies that:
7
+ // 1. Decoding errors are parse errors.
8
+ // 2. Certain other codepoints (e.g. control characters) are parse errors.
9
+ // 3. Carriage returns and CR/LF groups are converted to line feeds.
10
+ // https://encoding.spec.whatwg.org/#utf-8-decode
11
+ //
12
+ // Also, we want to keep track of source positions for error handling. As a
13
+ // result, we fold all that functionality into this decoder, and can't use an
14
+ // off-the-shelf library.
15
+ //
16
+ // This header is internal-only, which is why we prefix functions with only
17
+ // utf8_ or utf8_iterator_ instead of gumbo_utf8_.
18
+
19
+ #include <stdbool.h>
20
+ #include <stddef.h>
21
+
22
+ #include "gumbo.h"
23
+ #include "macros.h"
24
+
25
+ #ifdef __cplusplus
26
+ extern "C" {
27
+ #endif
28
+
29
+ struct GumboInternalError;
30
+ struct GumboInternalParser;
31
+
32
+ // Unicode replacement char.
33
+ #define kUtf8ReplacementChar 0xFFFD
34
+ #define kUtf8BomChar 0xFEFF
35
+ #define kUtf8MaxChar 0x10FFFF
36
+
37
+ typedef struct GumboInternalUtf8Iterator {
38
+ // Points at the start of the code point most recently read into 'current'.
39
+ const char* _start;
40
+
41
+ // Points at the mark. The mark is initially set to the beginning of the
42
+ // input.
43
+ const char* _mark;
44
+
45
+ // Points past the end of the iter, like a past-the-end iterator in the STL.
46
+ const char* _end;
47
+
48
+ // The code point under the cursor.
49
+ int _current;
50
+
51
+ // The width in bytes of the current code point.
52
+ size_t _width;
53
+
54
+ // The SourcePosition for the current location.
55
+ GumboSourcePosition _pos;
56
+
57
+ // The SourcePosition for the mark.
58
+ GumboSourcePosition _mark_pos;
59
+
60
+ // Pointer back to the GumboParser instance, for configuration options and
61
+ // error recording.
62
+ struct GumboInternalParser* _parser;
63
+ } Utf8Iterator;
64
+
65
+ // Returns true if this Unicode code point is a surrogate.
66
+ CONST_FN static inline bool utf8_is_surrogate(int c) {
67
+ return c >= 0xD800 && c <= 0xDFFF;
68
+ }
69
+
70
+ // Returns true if this Unicode code point is a noncharacter.
71
+ CONST_FN static inline bool utf8_is_noncharacter(int c) {
72
+ return
73
+ (c >= 0xFDD0 && c <= 0xFDEF)
74
+ || ((c & 0xFFFF) == 0xFFFE)
75
+ || ((c & 0xFFFF) == 0xFFFF);
76
+ }
77
+
78
+ // Returns true if this Unicode code point is a control.
79
+ CONST_FN static inline bool utf8_is_control(int c) {
80
+ return ((unsigned int)c < 0x1Fu) || (c >= 0x7F && c <= 0x9F);
81
+ }
82
+
83
+ // Initializes a new Utf8Iterator from the given byte buffer. The source does
84
+ // not have to be NUL-terminated, but the length must be passed in explicitly.
85
+ void utf8iterator_init (
86
+ struct GumboInternalParser* parser,
87
+ const char* source,
88
+ size_t source_length,
89
+ Utf8Iterator* iter
90
+ );
91
+
92
+ // Advances the current position by one code point.
93
+ void utf8iterator_next(Utf8Iterator* iter);
94
+
95
+ // Returns the current code point as an integer.
96
+ static inline int utf8iterator_current(const Utf8Iterator* iter) {
97
+ return iter->_current;
98
+ }
99
+
100
+ // Retrieves and fills the output parameter with the current source position.
101
+ static inline void utf8iterator_get_position (
102
+ const Utf8Iterator* iter,
103
+ GumboSourcePosition* output
104
+ ) {
105
+ *output = iter->_pos;
106
+ }
107
+
108
+ // Retrieves the marked position.
109
+ static inline GumboSourcePosition utf8iterator_get_mark_position (
110
+ const Utf8Iterator* iter
111
+ ) {
112
+ return iter->_mark_pos;
113
+ }
114
+
115
+ // Retrieves a character pointer to the start of the current character.
116
+ static inline const char* utf8iterator_get_char_pointer(const Utf8Iterator* iter) {
117
+ return iter->_start;
118
+ }
119
+
120
+ // Retrieves the width of the current character.
121
+ static inline size_t utf8iterator_get_width(const Utf8Iterator* iter) {
122
+ return iter->_width;
123
+ }
124
+
125
+ // Retrieves a character pointer to 1 past the end of the buffer. This is
126
+ // necessary for certain state machines and string comparisons that would like
127
+ // to look directly for ASCII text in the buffer without going through the
128
+ // decoder.
129
+ static inline const char* utf8iterator_get_end_pointer(const Utf8Iterator* iter) {
130
+ return iter->_end;
131
+ }
132
+
133
+ // Retrieves a character pointer to the marked position.
134
+ static inline const char* utf8iterator_get_mark_pointer(const Utf8Iterator* iter) {
135
+ return iter->_mark;
136
+ }
137
+
138
+ // If the upcoming text in the buffer matches the specified prefix (which has
139
+ // length 'length'), consume it and return true. Otherwise, return false with
140
+ // no other effects. If the length of the string would overflow the buffer,
141
+ // this returns false. Note that prefix should not contain null bytes because
142
+ // of the use of strncmp/strncasecmp internally. All existing use-cases adhere
143
+ // to this.
144
+ bool utf8iterator_maybe_consume_match (
145
+ Utf8Iterator* iter,
146
+ const char* prefix,
147
+ size_t length,
148
+ bool case_sensitive
149
+ );
150
+
151
+ // "Marks" a particular location of interest in the input stream, so that it can
152
+ // later be reset() to. There's also the ability to record an error at the
153
+ // point that was marked, as oftentimes that's more useful than the last
154
+ // character before the error was detected.
155
+ void utf8iterator_mark(Utf8Iterator* iter);
156
+
157
+ // Returns the current input stream position to the mark.
158
+ void utf8iterator_reset(Utf8Iterator* iter);
159
+
160
+ #ifdef __cplusplus
161
+ }
162
+ #endif
163
+
164
+ #endif // GUMBO_UTF8_H_
@@ -0,0 +1,68 @@
1
+ /*
2
+ Copyright 2017-2018 Craig Barnes.
3
+ Copyright 2010 Google Inc.
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ https://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ */
17
+
18
+ #include <stdio.h>
19
+ #include <stdlib.h>
20
+ #include <string.h>
21
+ #include "util.h"
22
+ #include "gumbo.h"
23
+
24
+ void* gumbo_alloc(size_t size) {
25
+ void* ptr = malloc(size);
26
+ if (unlikely(ptr == NULL)) {
27
+ perror(__func__);
28
+ abort();
29
+ }
30
+ return ptr;
31
+ }
32
+
33
+ void* gumbo_realloc(void* ptr, size_t size) {
34
+ ptr = realloc(ptr, size);
35
+ if (unlikely(ptr == NULL)) {
36
+ perror(__func__);
37
+ abort();
38
+ }
39
+ return ptr;
40
+ }
41
+
42
+ void gumbo_free(void* ptr) {
43
+ free(ptr);
44
+ }
45
+
46
+ char* gumbo_strdup(const char* str) {
47
+ const size_t size = strlen(str) + 1;
48
+ // The strdup(3) function isn't available in strict "-std=c99" mode
49
+ // (it's part of POSIX, not C99), so use malloc(3) and memcpy(3)
50
+ // instead:
51
+ char* buffer = gumbo_alloc(size);
52
+ return memcpy(buffer, str, size);
53
+ }
54
+
55
+ #ifdef GUMBO_DEBUG
56
+ #include <stdarg.h>
57
+ // Debug function to trace operation of the parser
58
+ // (define GUMBO_DEBUG to use).
59
+ void gumbo_debug(const char* format, ...) {
60
+ va_list args;
61
+ va_start(args, format);
62
+ vprintf(format, args);
63
+ va_end(args);
64
+ fflush(stdout);
65
+ }
66
+ #else
67
+ void gumbo_debug(const char* UNUSED_ARG(format), ...) {}
68
+ #endif
@@ -0,0 +1,30 @@
1
+ #ifndef GUMBO_UTIL_H_
2
+ #define GUMBO_UTIL_H_
3
+
4
+ #include <stdbool.h>
5
+ #include <stddef.h>
6
+ #include "macros.h"
7
+
8
+ #ifdef __cplusplus
9
+ extern "C" {
10
+ #endif
11
+
12
+ // Utility function for allocating & copying a null-terminated string into a
13
+ // freshly-allocated buffer. This is necessary for proper memory management; we
14
+ // have the convention that all const char* in parse tree structures are
15
+ // freshly-allocated, so if we didn't copy, we'd try to delete a literal string
16
+ // when the parse tree is destroyed.
17
+ char* gumbo_strdup(const char* str) XMALLOC NONNULL_ARGS;
18
+
19
+ void* gumbo_alloc(size_t size) XMALLOC;
20
+ void* gumbo_realloc(void* ptr, size_t size) RETURNS_NONNULL;
21
+ void gumbo_free(void* ptr);
22
+
23
+ // Debug wrapper for printf
24
+ void gumbo_debug(const char* format, ...) PRINTF(1);
25
+
26
+ #ifdef __cplusplus
27
+ }
28
+ #endif
29
+
30
+ #endif // GUMBO_UTIL_H_
@@ -0,0 +1,111 @@
1
+ /*
2
+ Copyright 2018 Craig Barnes.
3
+ Copyright 2010 Google Inc.
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ https://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ */
17
+
18
+ #include <assert.h>
19
+ #include <stdlib.h>
20
+ #include <string.h>
21
+ #include "vector.h"
22
+ #include "util.h"
23
+
24
+ void gumbo_vector_init(unsigned int initial_capacity, GumboVector* vector) {
25
+ vector->length = 0;
26
+ vector->capacity = initial_capacity;
27
+ if (initial_capacity > 0) {
28
+ vector->data = gumbo_alloc(sizeof(void*) * initial_capacity);
29
+ } else {
30
+ vector->data = NULL;
31
+ }
32
+ }
33
+
34
+ void gumbo_vector_destroy(GumboVector* vector) {
35
+ if (vector->capacity > 0) {
36
+ gumbo_free(vector->data);
37
+ }
38
+ }
39
+
40
+ static void enlarge_vector_if_full(GumboVector* vector) {
41
+ if (vector->length >= vector->capacity) {
42
+ if (vector->capacity) {
43
+ vector->capacity *= 2;
44
+ size_t num_bytes = sizeof(void*) * vector->capacity;
45
+ vector->data = gumbo_realloc(vector->data, num_bytes);
46
+ } else {
47
+ // 0-capacity vector; no previous array to deallocate.
48
+ vector->capacity = 2;
49
+ vector->data = gumbo_alloc(sizeof(void*) * vector->capacity);
50
+ }
51
+ }
52
+ }
53
+
54
+ void gumbo_vector_add(void* element, GumboVector* vector) {
55
+ enlarge_vector_if_full(vector);
56
+ assert(vector->data);
57
+ assert(vector->length < vector->capacity);
58
+ vector->data[vector->length++] = element;
59
+ }
60
+
61
+ void* gumbo_vector_pop(GumboVector* vector) {
62
+ if (vector->length == 0) {
63
+ return NULL;
64
+ }
65
+ return vector->data[--vector->length];
66
+ }
67
+
68
+ int gumbo_vector_index_of(GumboVector* vector, const void* element) {
69
+ for (unsigned int i = 0; i < vector->length; ++i) {
70
+ if (vector->data[i] == element) {
71
+ return i;
72
+ }
73
+ }
74
+ return -1;
75
+ }
76
+
77
+ void gumbo_vector_insert_at (
78
+ void* element,
79
+ unsigned int index,
80
+ GumboVector* vector
81
+ ) {
82
+ assert(index <= vector->length);
83
+ enlarge_vector_if_full(vector);
84
+ ++vector->length;
85
+ memmove (
86
+ &vector->data[index + 1],
87
+ &vector->data[index],
88
+ sizeof(void*) * (vector->length - index - 1)
89
+ );
90
+ vector->data[index] = element;
91
+ }
92
+
93
+ void gumbo_vector_remove(void* node, GumboVector* vector) {
94
+ int index = gumbo_vector_index_of(vector, node);
95
+ if (index == -1) {
96
+ return;
97
+ }
98
+ gumbo_vector_remove_at(index, vector);
99
+ }
100
+
101
+ void* gumbo_vector_remove_at(unsigned int index, GumboVector* vector) {
102
+ assert(index < vector->length);
103
+ void* result = vector->data[index];
104
+ memmove (
105
+ &vector->data[index],
106
+ &vector->data[index + 1],
107
+ sizeof(void*) * (vector->length - index - 1)
108
+ );
109
+ --vector->length;
110
+ return result;
111
+ }
@@ -0,0 +1,45 @@
1
+ #ifndef GUMBO_VECTOR_H_
2
+ #define GUMBO_VECTOR_H_
3
+
4
+ #include "gumbo.h"
5
+
6
+ #ifdef __cplusplus
7
+ extern "C" {
8
+ #endif
9
+
10
+ // Initializes a new GumboVector with the specified initial capacity.
11
+ void gumbo_vector_init(unsigned int initial_capacity, GumboVector* vector);
12
+
13
+ // Frees the memory used by a GumboVector. Does not free the contained
14
+ // pointers.
15
+ void gumbo_vector_destroy(GumboVector* vector);
16
+
17
+ // Adds a new element to a GumboVector.
18
+ void gumbo_vector_add(void* element, GumboVector* vector);
19
+
20
+ // Removes and returns the element most recently added to the GumboVector.
21
+ // Ownership is transferred to caller. Capacity is unchanged. If the vector is
22
+ // empty, NULL is returned.
23
+ void* gumbo_vector_pop(GumboVector* vector);
24
+
25
+ // Inserts an element at a specific index. This is potentially O(N) time, but
26
+ // is necessary for some of the spec's behavior.
27
+ void gumbo_vector_insert_at (
28
+ void* element,
29
+ unsigned int index,
30
+ GumboVector* vector
31
+ );
32
+
33
+ // Removes an element from the vector, or does nothing if the element is not in
34
+ // the vector.
35
+ void gumbo_vector_remove(void* element, GumboVector* vector);
36
+
37
+ // Removes and returns an element at a specific index. Note that this is
38
+ // potentially O(N) time and should be used sparingly.
39
+ void* gumbo_vector_remove_at(unsigned int index, GumboVector* vector);
40
+
41
+ #ifdef __cplusplus
42
+ }
43
+ #endif
44
+
45
+ #endif // GUMBO_VECTOR_H_
@@ -8,7 +8,7 @@
8
8
  require 'racc/parser.rb'
9
9
 
10
10
 
11
- require 'nokogiri/css/parser_extras'
11
+ require_relative "parser_extras"
12
12
 
13
13
  module Nokogiri
14
14
  module CSS
@@ -253,7 +253,7 @@ end
253
253
 
254
254
  ---- header
255
255
 
256
- require 'nokogiri/css/parser_extras'
256
+ require_relative "parser_extras"
257
257
 
258
258
  ---- inner
259
259
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- require 'nokogiri/syntax_error'
2
+ require_relative "../syntax_error"
3
3
  module Nokogiri
4
4
  module CSS
5
5
  class SyntaxError < ::Nokogiri::SyntaxError