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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
/*
+----------------------------------------------------------------------+
| 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> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "php.h"
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include "php_dom.h"
#include "html5_serializer.h"
#include "namespace_compat.h"
#include "serialize_common.h"
#include <lexbor/encoding/encoding.h>
/* This file implements the HTML 5 serialization algorithm.
* https://html.spec.whatwg.org/multipage/parsing.html#serialising-html-fragments (Date 2024-12-11)
*/
#define TRY(x) do { if (UNEXPECTED((x) != SUCCESS)) { return FAILURE; } } while (0)
static zend_result dom_html5_serialize_doctype(dom_html5_serialize_context *ctx, const xmlDtd *dtd)
{
TRY(ctx->write_string_len(ctx->application_data, "<!DOCTYPE ", strlen("<!DOCTYPE ")));
TRY(ctx->write_string(ctx->application_data, (const char *) dtd->name));
return ctx->write_string_len(ctx->application_data, ">", strlen(">"));
}
static zend_result dom_html5_serialize_comment(dom_html5_serialize_context *ctx, const xmlNode *node)
{
TRY(ctx->write_string_len(ctx->application_data, "<!--", strlen("<!--")));
if (node->content) {
TRY(ctx->write_string(ctx->application_data, (const char*) node->content));
}
return ctx->write_string_len(ctx->application_data, "-->", strlen("-->"));
}
static zend_result dom_html5_serialize_processing_instruction(dom_html5_serialize_context *ctx, const xmlNode *node)
{
TRY(ctx->write_string_len(ctx->application_data, "<?", strlen("<?")));
TRY(ctx->write_string(ctx->application_data, (const char *) node->name));
TRY(ctx->write_string_len(ctx->application_data, " ", strlen(" ")));
if (node->content) {
TRY(ctx->write_string(ctx->application_data, (const char *) node->content));
}
return ctx->write_string_len(ctx->application_data, ">", strlen(">"));
}
static zend_result dom_html5_serialize_entity_ref(dom_html5_serialize_context *ctx, const xmlNode *node)
{
TRY(ctx->write_string_len(ctx->application_data, "&", strlen("&")));
TRY(ctx->write_string(ctx->application_data, (const char *) node->name));
return ctx->write_string_len(ctx->application_data, ";", strlen(";"));
}
/* https://html.spec.whatwg.org/multipage/parsing.html#escapingString */
static zend_result dom_html5_escape_string(dom_html5_serialize_context *ctx, const char *content, bool attribute_mode)
{
const char *last_output = content;
/* Note: uses UTF-8 internally, so <C2 A0> indicates a non-breaking space */
const char *mask = attribute_mode ? "&\xC2\"" : "&\xC2<>";
while (true) {
size_t chunk_length = strcspn(content, mask);
content += chunk_length;
if (*content == '\0') {
break;
}
switch (*content) {
/* Step 1 */
case '&': {
TRY(ctx->write_string_len(ctx->application_data, last_output, content - last_output));
TRY(ctx->write_string_len(ctx->application_data, "&", strlen("&")));
last_output = content + 1;
break;
}
/* Step 2 (non-breaking space) (note: uses UTF-8 internally) */
case '\xC2': {
if (content[1] == '\xA0') {
TRY(ctx->write_string_len(ctx->application_data, last_output, content - last_output));
TRY(ctx->write_string_len(ctx->application_data, " ", strlen(" ")));
content++; /* Consume A0 too */
last_output = content + 1;
}
break;
}
/* Step 3 */
case '"': {
TRY(ctx->write_string_len(ctx->application_data, last_output, content - last_output));
TRY(ctx->write_string_len(ctx->application_data, """, strlen(""")));
last_output = content + 1;
break;
}
/* Step 4 */
case '<': {
TRY(ctx->write_string_len(ctx->application_data, last_output, content - last_output));
TRY(ctx->write_string_len(ctx->application_data, "<", strlen("<")));
last_output = content + 1;
break;
}
case '>': {
TRY(ctx->write_string_len(ctx->application_data, last_output, content - last_output));
TRY(ctx->write_string_len(ctx->application_data, ">", strlen(">")));
last_output = content + 1;
break;
}
}
content++;
}
return ctx->write_string_len(ctx->application_data, last_output, content - last_output);
}
static zend_result dom_html5_serialize_text_node(dom_html5_serialize_context *ctx, const xmlNode *node)
{
if (!node->content) {
return SUCCESS;
}
const xmlNode *parent = node->parent;
if (parent != NULL && parent->type == XML_ELEMENT_NODE && php_dom_ns_is_fast(parent, php_dom_ns_is_html_magic_token)) {
size_t name_length = strlen((const char *) parent->name);
/* Spec tells us to only emit noscript content as-is if scripting is enabled.
* However, the user agent (PHP) does not support (JS) scripting.
* Furthermore, if actually consumed by a browser then we should err on the safe side and not emit the content as-is. */
if (dom_local_name_compare_ex(parent, "style", strlen("style"), name_length)
|| dom_local_name_compare_ex(parent, "script", strlen("script"), name_length)
|| dom_local_name_compare_ex(parent, "xmp", strlen("xmp"), name_length)
|| dom_local_name_compare_ex(parent, "iframe", strlen("iframe"), name_length)
|| dom_local_name_compare_ex(parent, "noembed", strlen("noembed"), name_length)
|| dom_local_name_compare_ex(parent, "noframes", strlen("noframes"), name_length)
|| dom_local_name_compare_ex(parent, "plaintext", strlen("plaintext"), name_length)) {
return ctx->write_string(ctx->application_data, (const char *) node->content);
}
}
return dom_html5_escape_string(ctx, (const char *) node->content, false);
}
static zend_result dom_html5_serialize_element_tag_name(dom_html5_serialize_context *ctx, const xmlNode *node)
{
/* Note: it is not the serializer's responsibility to care about uppercase/lowercase (see createElement() note) */
if (node->ns != NULL && node->ns->prefix != NULL
&& !(php_dom_ns_is_fast(node, php_dom_ns_is_html_magic_token) || php_dom_ns_is_fast(node, php_dom_ns_is_mathml_magic_token) || php_dom_ns_is_fast(node, php_dom_ns_is_svg_magic_token))) {
TRY(ctx->write_string(ctx->application_data, (const char *) node->ns->prefix));
TRY(ctx->write_string_len(ctx->application_data, ":", strlen(":")));
}
return ctx->write_string(ctx->application_data, (const char *) node->name);
}
static zend_result dom_html5_serialize_element_start(dom_html5_serialize_context *ctx, const xmlNode *node)
{
TRY(ctx->write_string_len(ctx->application_data, "<", strlen("<")));
TRY(dom_html5_serialize_element_tag_name(ctx, node));
/* We don't support the "is" value during element creation, so no handling here. */
for (const xmlAttr *attr = node->properties; attr; attr = attr->next) {
TRY(ctx->write_string_len(ctx->application_data, " ", strlen(" ")));
if (attr->ns == NULL) {
TRY(ctx->write_string(ctx->application_data, (const char *) attr->name));
} else {
if (php_dom_ns_is_fast((const xmlNode *) attr, php_dom_ns_is_xml_magic_token)) {
TRY(ctx->write_string_len(ctx->application_data, "xml:", strlen("xml:")));
TRY(ctx->write_string(ctx->application_data, (const char *) attr->name));
} else if (php_dom_ns_is_fast((const xmlNode *) attr, php_dom_ns_is_xmlns_magic_token)) {
/* Compatibility for real attributes */
if (strcmp((const char *) attr->name, "xmlns") == 0) {
TRY(ctx->write_string_len(ctx->application_data, "xmlns", strlen("xmlns")));
} else {
TRY(ctx->write_string_len(ctx->application_data, "xmlns:", strlen("xmlns:")));
TRY(ctx->write_string(ctx->application_data, (const char *) attr->name));
}
} else if (php_dom_ns_is_fast((const xmlNode *) attr, php_dom_ns_is_xlink_magic_token)) {
TRY(ctx->write_string_len(ctx->application_data, "xlink:", strlen("xlink:")));
TRY(ctx->write_string(ctx->application_data, (const char *) attr->name));
} else if (attr->ns->prefix == NULL) {
TRY(ctx->write_string(ctx->application_data, (const char *) attr->name));
} else {
TRY(ctx->write_string(ctx->application_data, (const char *) attr->ns->prefix));
TRY(ctx->write_string_len(ctx->application_data, ":", strlen(":")));
TRY(ctx->write_string(ctx->application_data, (const char *) attr->name));
}
}
TRY(ctx->write_string_len(ctx->application_data, "=\"", strlen("=\"")));
for (xmlNodePtr child = attr->children; child != NULL; child = child->next) {
if (child->type == XML_TEXT_NODE) {
if (child->content != NULL) {
TRY(dom_html5_escape_string(ctx, (const char *) child->content, true));
}
} else if (child->type == XML_ENTITY_REF_NODE) {
TRY(ctx->write_string_len(ctx->application_data, "&", strlen("&")));
TRY(dom_html5_escape_string(ctx, (const char *) child->name, true));
TRY(ctx->write_string_len(ctx->application_data, ";", strlen(";")));
}
}
TRY(ctx->write_string_len(ctx->application_data, "\"", strlen("\"")));
}
return ctx->write_string_len(ctx->application_data, ">", strlen(">"));
/* Note: "continue on to the next child if the element is void" is handled in the iteration and dom_html5_serialize_element_end() */
}
/* https://html.spec.whatwg.org/multipage/syntax.html#void-elements
* https://html.spec.whatwg.org/multipage/parsing.html#serializes-as-void */
static bool dom_html5_serializes_as_void(const xmlNode *node)
{
if (php_dom_ns_is_fast(node, php_dom_ns_is_html_magic_token)) {
size_t name_length = strlen((const char *) node->name);
if (/* These are the void elements from https://html.spec.whatwg.org/multipage/syntax.html#void-elements */
dom_local_name_compare_ex(node, "area", strlen("area"), name_length)
|| dom_local_name_compare_ex(node, "base", strlen("base"), name_length)
|| dom_local_name_compare_ex(node, "br", strlen("br"), name_length)
|| dom_local_name_compare_ex(node, "col", strlen("col"), name_length)
|| dom_local_name_compare_ex(node, "embed", strlen("embed"), name_length)
|| dom_local_name_compare_ex(node, "hr", strlen("hr"), name_length)
|| dom_local_name_compare_ex(node, "img", strlen("img"), name_length)
|| dom_local_name_compare_ex(node, "input", strlen("input"), name_length)
|| dom_local_name_compare_ex(node, "link", strlen("link"), name_length)
|| dom_local_name_compare_ex(node, "meta", strlen("meta"), name_length)
|| dom_local_name_compare_ex(node, "source", strlen("source"), name_length)
|| dom_local_name_compare_ex(node, "track", strlen("track"), name_length)
|| dom_local_name_compare_ex(node, "wbr", strlen("wbr"), name_length)
/* These are the additional names from https://html.spec.whatwg.org/multipage/parsing.html#serializes-as-void */
|| dom_local_name_compare_ex(node, "basefont", strlen("basefont"), name_length)
|| dom_local_name_compare_ex(node, "bgsound", strlen("bgsound"), name_length)
|| dom_local_name_compare_ex(node, "frame", strlen("frame"), name_length)
|| dom_local_name_compare_ex(node, "keygen", strlen("keygen"), name_length)
|| dom_local_name_compare_ex(node, "param", strlen("param"), name_length)) {
return true;
}
}
return false;
}
static zend_result dom_html5_serialize_element_end(dom_html5_serialize_context *ctx, const xmlNode *node)
{
if (!dom_html5_serializes_as_void(node)) {
TRY(ctx->write_string_len(ctx->application_data, "</", strlen("</")));
TRY(dom_html5_serialize_element_tag_name(ctx, node));
return ctx->write_string_len(ctx->application_data, ">", strlen(">"));
}
return SUCCESS;
}
/* https://html.spec.whatwg.org/multipage/parsing.html#html-fragment-serialisation-algorithm */
static zend_result dom_html5_serialize_node(dom_html5_serialize_context *ctx, const xmlNode *node, const xmlNode *bound)
{
while (node != NULL) {
switch (node->type) {
case XML_DTD_NODE: {
TRY(dom_html5_serialize_doctype(ctx, (const xmlDtd *) node));
break;
}
case XML_CDATA_SECTION_NODE:
case XML_TEXT_NODE: {
TRY(dom_html5_serialize_text_node(ctx, node));
break;
}
case XML_PI_NODE: {
TRY(dom_html5_serialize_processing_instruction(ctx, node));
break;
}
case XML_COMMENT_NODE: {
TRY(dom_html5_serialize_comment(ctx, node));
break;
}
case XML_ELEMENT_NODE: {
TRY(dom_html5_serialize_element_start(ctx, node));
const xmlNode *children = node->children;
if (php_dom_ns_is_fast(node, php_dom_ns_is_html_magic_token) && xmlStrEqual(node->name, BAD_CAST "template")) {
children = php_dom_retrieve_templated_content(ctx->private_data, node);
}
if (children) {
if (!dom_html5_serializes_as_void(node)) {
node = children;
continue;
}
} else {
/* Not descended, so wouldn't put the closing tag as it's normally only done when going back upwards. */
TRY(dom_html5_serialize_element_end(ctx, node));
}
break;
}
case XML_DOCUMENT_FRAG_NODE: {
if (node->children) {
node = node->children;
continue;
}
break;
}
/* Only exists for compatibility with XML and old DOM. */
case XML_ENTITY_REF_NODE: {
TRY(dom_html5_serialize_entity_ref(ctx, node));
break;
}
default:
break;
}
if (node->next) {
node = node->next;
} else {
/* Go upwards, until we find a parent node with a next sibling, or until we hit the bound. */
do {
node = node->parent;
if (node == bound) {
return SUCCESS;
}
if (node->type == XML_ELEMENT_NODE) {
TRY(dom_html5_serialize_element_end(ctx, node));
}
} while (node->next == NULL);
node = node->next;
}
}
return SUCCESS;
}
/* Note: this serializes the _children_, excluding the node itself! */
zend_result dom_html5_serialize(dom_html5_serialize_context *ctx, const xmlNode *node)
{
/* Step 1. Note that this algorithm serializes children. Only elements, documents, and fragments can have children. */
if (node->type != XML_ELEMENT_NODE
&& node->type != XML_DOCUMENT_FRAG_NODE
&& node->type != XML_DOCUMENT_NODE
&& node->type != XML_HTML_DOCUMENT_NODE) {
return SUCCESS;
}
if (node->type == XML_ELEMENT_NODE && dom_html5_serializes_as_void(node)) {
return SUCCESS;
}
/* Step 2 not needed because we're not using a string to store the serialized data */
/* Step 3. If the node is a template element, then let the node instead be the template element's template contents (a DocumentFragment node). */
xmlNodePtr children = php_dom_retrieve_templated_content(ctx->private_data, node);
if (!children) {
children = node->children;
}
/* Step 4 concerns shadow roots, but we don't have these, so skip. */
/* Step 5 */
return dom_html5_serialize_node(ctx, children, node);
}
/* Variant on the above that is equivalent to the "outer HTML". */
zend_result dom_html5_serialize_outer(dom_html5_serialize_context *ctx, const xmlNode *node)
{
if (node->type == XML_DOCUMENT_NODE || node->type == XML_HTML_DOCUMENT_NODE || node->type == XML_DOCUMENT_FRAG_NODE) {
node = node->children;
if (!node) {
return SUCCESS;
}
return dom_html5_serialize_node(ctx, node, node->parent);
} else {
xmlNodePtr old_next = node->next;
((xmlNodePtr) node)->next = NULL;
zend_result result = dom_html5_serialize_node(ctx, node, node->parent);
((xmlNodePtr) node)->next = old_next;
return result;
}
}
#endif /* HAVE_LIBXML && HAVE_DOM */
|