- Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathinner_html_mixin.c
369 lines (318 loc) · 13.9 KB
/
inner_html_mixin.c
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Niels Dossche <nielsdos@php.net> |
+----------------------------------------------------------------------+
*/
#ifdefHAVE_CONFIG_H
#include"config.h"
#endif
#include"php.h"
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include"php_dom.h"
#include"dom_properties.h"
#include"html5_parser.h"
#include"html5_serializer.h"
#include"xml_serializer.h"
#include"domexception.h"
#include<libxml/xmlsave.h>
#include<lexbor/dom/interfaces/element.h>
#include<lexbor/html/interfaces/document.h>
#include<lexbor/tag/tag.h>
#include<lexbor/encoding/encoding.h>
/* Spec date: 2024-04-14 */
staticzend_resultdom_inner_html_write_string(void*application_data, constchar*buf)
{
smart_str*output=application_data;
smart_str_appends(output, buf);
returnSUCCESS;
}
staticzend_resultdom_inner_html_write_string_len(void*application_data, constchar*buf, size_tlen)
{
smart_str*output=application_data;
smart_str_appendl(output, buf, len);
returnSUCCESS;
}
staticintdom_write_smart_str(void*context, constchar*buffer, intlen)
{
smart_str*str=context;
smart_str_appendl(str, buffer, len);
returnlen;
}
/* https://w3c.github.io/DOM-Parsing/#the-innerhtml-mixin
* and https://w3c.github.io/DOM-Parsing/#dfn-fragment-serializing-algorithm */
zend_resultdom_element_inner_html_read(dom_object*obj, zval*retval)
{
DOM_PROP_NODE(xmlNodePtr, node, obj);
/* 1. Let context document be the value of node's node document. */
constxmlDoc*context_document=node->doc;
/* 2. If context document is an HTML document, return an HTML serialization of node. */
if (context_document->type==XML_HTML_DOCUMENT_NODE) {
smart_stroutput= {0};
dom_html5_serialize_contextctx;
ctx.private_data=php_dom_get_private_data(obj);
ctx.application_data=&output;
ctx.write_string=dom_inner_html_write_string;
ctx.write_string_len=dom_inner_html_write_string_len;
dom_html5_serialize(&ctx, node);
ZVAL_STR(retval, smart_str_extract(&output));
}
/* 3. Otherwise, context document is an XML document; return an XML serialization of node passing the flag require well-formed. */
else {
ZEND_ASSERT(context_document->type==XML_DOCUMENT_NODE);
intstatus=-1;
smart_strstr= {0};
/* No need to check buf's return value, as xmlSaveToBuffer() will fail instead. */
xmlSaveCtxtPtrctxt=xmlSaveToIO(dom_write_smart_str, NULL, &str, "UTF-8", XML_SAVE_AS_XML);
if (EXPECTED(ctxt!=NULL)) {
xmlCharEncodingHandlerPtrhandler=xmlFindCharEncodingHandler("UTF-8");
xmlOutputBufferPtrout=xmlOutputBufferCreateIO(dom_write_smart_str, NULL, &str, handler);
if (EXPECTED(out!=NULL)) {
php_dom_private_data*private_data=php_dom_get_private_data(obj);
/* Note: the innerHTML mixin sets the well-formed flag to true. */
xmlNodePtrchild=node->children;
status=0;
while (child!=NULL&&status==0) {
status=dom_xml_serialize(ctxt, out, child, false, true, private_data);
child=child->next;
}
status |= xmlOutputBufferFlush(out);
status |= xmlOutputBufferClose(out);
}
(void) xmlSaveClose(ctxt);
xmlCharEncCloseFunc(handler);
}
if (UNEXPECTED(status<0)) {
smart_str_free_ex(&str, false);
php_dom_throw_error_with_message(SYNTAX_ERR, "The resulting XML serialization is not well-formed", true);
returnFAILURE;
}
ZVAL_STR(retval, smart_str_extract(&str));
}
returnSUCCESS;
}
staticlxb_dom_node_t*dom_html_fragment_lexbor_parse(lxb_html_document_t*document, lxb_dom_element_t*element, constzend_string*input)
{
lxb_status_tstatus=lxb_html_document_parse_fragment_chunk_begin(document, element);
if (status!=LXB_STATUS_OK) {
returnNULL;
}
constlxb_encoding_data_t*encoding_data=lxb_encoding_data(LXB_ENCODING_UTF_8);
lxb_encoding_decode_tdecode;
lxb_encoding_decode_init_single(&decode, encoding_data);
constlxb_char_t*buf_ref= (constlxb_char_t*) ZSTR_VAL(input);
if (ZSTR_IS_VALID_UTF8(input)) {
/* If we know the input is valid UTF-8, we don't have to perform checks and replace invalid sequences. */
status=lxb_html_document_parse_fragment_chunk(document, buf_ref, ZSTR_LEN(input));
if (UNEXPECTED(status!=LXB_STATUS_OK)) {
returnNULL;
}
} else {
/* See dom_decode_encode_fast_path(), simplified version for in-memory use-case. */
constlxb_char_t*buf_end=buf_ref+ZSTR_LEN(input);
constlxb_char_t*last_output=buf_ref;
while (buf_ref<buf_end) {
if (decode.u.utf_8.need==0&&*buf_ref<0x80) {
buf_ref++;
continue;
}
constlxb_char_t*buf_ref_backup=buf_ref;
lxb_codepoint_tcodepoint=lxb_encoding_decode_utf_8_single(&decode, &buf_ref, buf_end);
if (UNEXPECTED(codepoint>LXB_ENCODING_MAX_CODEPOINT)) {
status=lxb_html_document_parse_fragment_chunk(document, last_output, buf_ref_backup-last_output);
if (UNEXPECTED(status!=LXB_STATUS_OK)) {
returnNULL;
}
status=lxb_html_document_parse_fragment_chunk(document, LXB_ENCODING_REPLACEMENT_BYTES, LXB_ENCODING_REPLACEMENT_SIZE);
if (UNEXPECTED(status!=LXB_STATUS_OK)) {
returnNULL;
}
last_output=buf_ref;
}
}
if (buf_ref!=last_output) {
status=lxb_html_document_parse_fragment_chunk(document, last_output, buf_ref-last_output);
if (UNEXPECTED(status!=LXB_STATUS_OK)) {
returnNULL;
}
}
}
returnlxb_html_document_parse_fragment_chunk_end(document);
}
staticlxb_dom_document_cmode_tdom_translate_quirks_mode(php_libxml_quirks_modequirks_mode)
{
switch (quirks_mode) {
casePHP_LIBXML_NO_QUIRKS: returnLXB_DOM_DOCUMENT_CMODE_NO_QUIRKS;
casePHP_LIBXML_LIMITED_QUIRKS: returnLXB_DOM_DOCUMENT_CMODE_LIMITED_QUIRKS;
casePHP_LIBXML_QUIRKS: returnLXB_DOM_DOCUMENT_CMODE_QUIRKS;
EMPTY_SWITCH_DEFAULT_CASE();
}
}
/* https://html.spec.whatwg.org/#html-fragment-parsing-algorithm */
staticxmlNodePtrdom_html_fragment_parsing_algorithm(dom_object*obj, xmlNodePtrcontext_node, constzend_string*input, php_libxml_quirks_modequirks_mode)
{
/* The whole algorithm is implemented in Lexbor, we just have to be the adapter between the
* data structures used in PHP and what Lexbor expects. */
lxb_html_document_t*document=lxb_html_document_create();
document->dom_document.compat_mode=dom_translate_quirks_mode(quirks_mode);
lxb_dom_element_t*element=lxb_dom_element_interface_create(&document->dom_document);
constlxb_tag_data_t*tag_data=lxb_tag_data_by_name(document->dom_document.tags, (lxb_char_t*) context_node->name, xmlStrlen(context_node->name));
element->node.local_name=tag_data==NULL ? LXB_TAG__UNDEF : tag_data->tag_id;
constlxb_char_t*ns_uri;
size_tns_uri_len;
if (context_node->ns==NULL||context_node->ns->href==NULL) {
ns_uri= (lxb_char_t*) "";
ns_uri_len=0;
} else {
ns_uri=context_node->ns->href;
ns_uri_len=xmlStrlen(ns_uri);
}
constlxb_ns_data_t*ns_data=lxb_ns_data_by_link(document->dom_document.ns, ns_uri, ns_uri_len);
element->node.ns=ns_data==NULL ? LXB_NS__UNDEF : ns_data->ns_id;
lxb_dom_node_t*node=dom_html_fragment_lexbor_parse(document, element, input);
xmlNodePtrfragment=NULL;
if (node!=NULL) {
/* node->last_child could be NULL, but that is allowed. */
lexbor_libxml2_bridge_statusstatus=lexbor_libxml2_bridge_convert_fragment(node->last_child, context_node->doc, &fragment, true, true, php_dom_get_private_data(obj));
if (UNEXPECTED(status!=LEXBOR_LIBXML2_BRIDGE_STATUS_OK)) {
php_dom_throw_error(INVALID_STATE_ERR, true);
}
} else {
php_dom_throw_error(INVALID_STATE_ERR, true);
}
lxb_html_document_destroy(document);
returnfragment;
}
staticvoiddom_xml_parser_tag_name(constxmlNode*context_node, xmlParserCtxtPtrparser)
{
if (context_node->ns!=NULL&&context_node->ns->prefix!=NULL) {
xmlParseChunk(parser, (constchar*) context_node->ns->prefix, xmlStrlen(context_node->ns->prefix), 0);
xmlParseChunk(parser, ":", 1, 0);
}
xmlParseChunk(parser, (constchar*) context_node->name, xmlStrlen(context_node->name), 0);
}
staticvoiddom_xml_fragment_parsing_algorithm_parse(php_dom_libxml_ns_mapper*ns_mapper, constxmlNode*context_node, constzend_string*input, xmlParserCtxtPtrparser)
{
xmlParseChunk(parser, "<", 1, 0);
dom_xml_parser_tag_name(context_node, parser);
/* Namespaces: we have to declare all in-scope namespaces including the default namespace */
/* xmlns attributes */
php_dom_in_scope_nsin_scope_ns=php_dom_get_in_scope_ns(ns_mapper, context_node, true);
for (size_ti=0; i<in_scope_ns.count; i++) {
constxmlNs*ns=in_scope_ns.list[i];
xmlParseChunk(parser, " xmlns:", 7, 0);
ZEND_ASSERT(ns->prefix!=NULL);
xmlParseChunk(parser, (constchar*) ns->prefix, xmlStrlen(ns->prefix), 0);
xmlParseChunk(parser, "=\"", 2, 0);
xmlParseChunk(parser, (constchar*) ns->href, xmlStrlen(ns->href), 0);
xmlParseChunk(parser, "\"", 1, 0);
}
php_dom_in_scope_ns_destroy(&in_scope_ns);
/* default namespace */
constchar*default_ns=dom_locate_a_namespace(context_node, NULL);
if (default_ns!=NULL) {
xmlParseChunk(parser, " xmlns=\"", 8, 0);
xmlParseChunk(parser, default_ns, strlen(default_ns), 0);
xmlParseChunk(parser, "\"", 1, 0);
}
xmlParseChunk(parser, ">", 1, 0);
xmlParseChunk(parser, (constchar*) ZSTR_VAL(input), ZSTR_LEN(input), 0);
xmlParseChunk(parser, "</", 2, 0);
dom_xml_parser_tag_name(context_node, parser);
xmlParseChunk(parser, ">", 1, 1);
}
/* https://html.spec.whatwg.org/#xml-fragment-parsing-algorithm */
staticxmlNodePtrdom_xml_fragment_parsing_algorithm(dom_object*obj, constxmlNode*context_node, constzend_string*input)
{
/* Steps 1-4 below */
xmlParserCtxtPtrparser=xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL);
if (UNEXPECTED(parser==NULL)) {
php_dom_throw_error(INVALID_STATE_ERR, true);
returnNULL;
}
/* This is not only good to avoid a performance cost of changing the tree, but also to work around an old bug
* in xmlSetTreeDoc(). */
xmlDictFree(parser->dict);
if (context_node->doc->dict==NULL) {
context_node->doc->dict=xmlDictCreate();
xmlDictSetLimit(context_node->doc->dict, XML_MAX_DICTIONARY_LIMIT);
}
parser->dict=context_node->doc->dict;
php_libxml_sanitize_parse_ctxt_options(parser);
xmlCtxtUseOptions(parser, XML_PARSE_IGNORE_ENC | XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
xmlCharEncodingHandlerPtrencoding=xmlFindCharEncodingHandler("UTF-8");
(void) xmlSwitchToEncoding(parser, encoding);
php_dom_libxml_ns_mapper*ns_mapper=php_dom_get_ns_mapper(obj);
dom_xml_fragment_parsing_algorithm_parse(ns_mapper, context_node, input, parser);
/* 5. If there is an XML well-formedness or XML namespace well-formedness error, then throw a "SyntaxError" DOMException. */
if (!parser->wellFormed|| !parser->nsWellFormed) {
parser->dict=NULL;
xmlFreeDoc(parser->myDoc);
xmlFreeParserCtxt(parser);
php_dom_throw_error_with_message(SYNTAX_ERR, "XML fragment is not well-formed", true);
returnNULL;
}
xmlDocPtrdoc=parser->myDoc;
xmlFreeParserCtxt(parser);
if (EXPECTED(doc!=NULL)) {
doc->dict=NULL;
/* 6. If the document element of the resulting Document has any sibling nodes, then throw a "SyntaxError" DOMException. */
xmlNodePtrdocument_element=doc->children;
if (document_element==NULL||document_element->next!=NULL) {
xmlFreeDoc(doc);
php_dom_throw_error_with_message(SYNTAX_ERR, "XML fragment is not well-formed", true);
returnNULL;
}
/* 7. Return the child nodes of the document element of the resulting Document, in tree order. */
xmlNodePtrfragment=xmlNewDocFragment(context_node->doc);
if (EXPECTED(fragment!=NULL)) {
xmlNodePtrchild=document_element->children;
/* Yes, we have to call both xmlSetTreeDoc() prior to xmlAddChildList()
* because xmlAddChildList() _only_ sets the tree for the topmost elements in the subtree! */
xmlSetTreeDoc(document_element, context_node->doc);
xmlAddChildList(fragment, child);
dom_mark_namespaces_as_attributes_too(ns_mapper, doc);
document_element->children=NULL;
document_element->last=NULL;
}
xmlFreeDoc(doc);
returnfragment;
}
returnNULL;
}
/* https://w3c.github.io/DOM-Parsing/#the-innerhtml-mixin
* and https://w3c.github.io/DOM-Parsing/#dfn-fragment-parsing-algorithm */
zend_resultdom_element_inner_html_write(dom_object*obj, zval*newval)
{
DOM_PROP_NODE(xmlNodePtr, context_node, obj);
xmlNodePtrfragment;
if (context_node->doc->type==XML_DOCUMENT_NODE) {
fragment=dom_xml_fragment_parsing_algorithm(obj, context_node, Z_STR_P(newval));
} else {
fragment=dom_html_fragment_parsing_algorithm(obj, context_node, Z_STR_P(newval), obj->document->quirks_mode);
}
if (fragment==NULL) {
returnFAILURE;
}
if (php_dom_ns_is_fast(context_node, php_dom_ns_is_html_magic_token) &&xmlStrEqual(context_node->name, BAD_CAST"template")) {
context_node=php_dom_ensure_templated_content(php_dom_get_private_data(obj), context_node);
if (context_node==NULL) {
xmlFreeNode(fragment);
returnFAILURE;
}
}
ZEND_ASSERT(obj->document!=NULL);
php_libxml_invalidate_node_list_cache(obj->document);
dom_remove_all_children(context_node);
returnphp_dom_pre_insert(obj->document, fragment, context_node, NULL) ? SUCCESS : FAILURE;
}
#endif