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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
|
// Copyright (C) 2022-2024 Artifex Software, Inc.
//
// This file is part of MuPDF.
//
// MuPDF is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License
// along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
//
// Alternative licensing terms are available from the licensor.
// For commercial licensing, see <https://www.artifex.com/> or contact
// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
// CA 94129, USA, for further information.
#include "html-imp.h"
#include "string.h"
fz_xml *fz_story_document(fz_context *ctx, fz_story *story)
{
if (story == NULL || story->dom == NULL)
return NULL;
return story->dom;
}
fz_xml *fz_dom_body(fz_context *ctx, fz_xml *dom)
{
if (dom == NULL)
return NULL;
return fz_xml_find_dfs(dom, "body", NULL, NULL);
}
fz_xml *fz_dom_document_element(fz_context *ctx, fz_xml *dom)
{
if (dom == NULL)
return NULL;
while (dom->up)
dom = dom->up;
return dom->down;
}
static fz_xml *
doc_pointer(fz_xml *a)
{
while (a->up)
a = a->up;
return a;
}
static void
check_same_doc(fz_context *ctx, fz_xml *a, fz_xml *b)
{
/* Sanity check: The child and parent must come from the same doc. */
if (doc_pointer(a) != doc_pointer(b))
fz_throw(ctx, FZ_ERROR_ARGUMENT, "Parent and child must be from the same document");
}
/* Helper function to skip forward if we are passed a
* doc pointer in circumstances where we should not be. */
static fz_xml *
skip_doc_pointer(fz_xml *x)
{
return (x == NULL || !FZ_DOCUMENT_ITEM(x)) ? x : x->down;
}
fz_xml *
fz_new_dom(fz_context *ctx, const char *tag)
{
fz_pool *pool = fz_new_pool(ctx);
fz_xml *xml;
fz_try(ctx)
{
xml = fz_pool_alloc(ctx, pool, sizeof *xml);
xml->up = NULL;
xml->down = NULL;
xml->u.doc.refs = 1;
xml->u.doc.pool = pool;
xml->down = fz_new_dom_node(ctx, xml, tag);
xml->down->up = xml;
}
fz_catch(ctx)
{
fz_drop_pool(ctx, pool);
fz_rethrow(ctx);
}
return xml->down;
}
fz_xml *
fz_new_dom_node(fz_context *ctx, fz_xml *dom, const char *tag)
{
const char *ns;
fz_xml *xml;
size_t size;
dom = doc_pointer(dom);
/* skip namespace prefix */
for (ns = tag; *ns; ++ns)
if (*ns == ':')
tag = ns + 1;
size = offsetof(fz_xml, u.node.u.d.name) + ns-tag+1;
xml = fz_pool_alloc(ctx, dom->u.doc.pool, size);
memcpy(xml->u.node.u.d.name, tag, ns-tag+1);
xml->u.node.u.d.atts = NULL;
xml->down = NULL;
xml->up = dom;
xml->u.node.next = NULL;
xml->u.node.prev = NULL;
#ifdef FZ_XML_SEQ
/* We don't have sequence numbers here. */
xml->seq = 0;
#endif
return xml;
}
fz_xml *
fz_new_dom_text_node(fz_context *ctx, fz_xml *dom, const char *text)
{
fz_xml *xml;
size_t len = text ? strlen(text) : 0;
size_t size;
dom = doc_pointer(dom);
size = offsetof(fz_xml, u.node.u.text) + len + 1;
xml = fz_pool_alloc(ctx, dom->u.doc.pool, size);
if (text)
memcpy(xml->u.node.u.text, text, len);
xml->u.node.u.text[len] = 0;
xml->down = MAGIC_TEXT;
xml->up = dom;
xml->u.node.next = NULL;
xml->u.node.prev = NULL;
#ifdef FZ_XML_SEQ
/* We don't have sequence numbers here. */
xml->u.node.seq = 0;
#endif
return xml;
}
static fz_xml *
clone_xml(fz_context *ctx, fz_xml *dom, fz_xml *node)
{
fz_xml *clone;
struct attribute **dst;
struct attribute *attr;
fz_xml *child, *prev;
if (dom == NULL || node == NULL)
return NULL;
/* Text nodes are simple. No children. */
if (FZ_TEXT_ITEM(node))
{
return fz_new_dom_text_node(ctx, dom, node->u.node.u.text);
}
/* Clone a non-text node. */
clone = fz_new_dom_node(ctx, dom, node->u.node.u.d.name);
/* Clone the attributes. */
attr = node->u.node.u.d.atts;
dst = &clone->u.node.u.d.atts;
while (attr)
{
size_t len = strlen(attr->name) + 1;
size_t size = offsetof(struct attribute, name) + len;
struct attribute *a = fz_pool_alloc(ctx, dom->u.doc.pool, size);
memcpy(a->name, attr->name, len);
a->next = NULL;
a->value = NULL;
if (attr->value)
{
a->value = fz_pool_alloc(ctx, dom->u.doc.pool, strlen(attr->value)+1);
strcpy(a->value, attr->value);
}
*dst = a;
dst = &a->next;
attr = attr->next;
}
/* If we have no children, we're done. */
if (node->down == NULL)
return clone;
/* Copy the first child. */
clone->down = clone_xml(ctx, dom, node->down);
clone->down->up = clone;
/* And then run along all the successive children. */
prev = clone->down;
child = node->down->u.node.next;
while (child)
{
prev->u.node.next = clone_xml(ctx, dom, child);
prev->u.node.prev = prev;
prev = prev->u.node.next;
prev->up = clone;
child = child->u.node.next;
}
return clone;
}
fz_xml *fz_dom_clone(fz_context *ctx, fz_xml *elt)
{
fz_xml *dom;
if (elt == NULL)
return NULL;
/* We shouldn't be passed a document item really, but
* cope. */
if (FZ_DOCUMENT_ITEM(elt))
elt = elt->down;
/* Find the document pointer. */
dom = elt;
while (dom->up)
dom = dom->up;
return clone_xml(ctx, dom, elt);
}
fz_xml *fz_dom_create_element(fz_context *ctx, fz_xml *dom, const char *tag)
{
if (dom == NULL || tag == NULL)
return NULL;
/* We make a new node, unconnected to anything else.
* up will stil point to the dom root though. */
return fz_new_dom_node(ctx, dom, tag);
}
fz_xml *fz_dom_create_text_node(fz_context *ctx, fz_xml *dom, const char *text)
{
if (dom == NULL || text == NULL)
return NULL;
/* We make a new node, unconnected to anything else. */
return fz_new_dom_text_node(ctx, dom, text);
}
fz_xml *fz_dom_find(fz_context *ctx, fz_xml *elt, const char *tag, const char *att, const char *match)
{
if (elt == NULL)
return NULL;
return fz_xml_find_dfs(elt, tag, att, match);
}
fz_xml *fz_dom_find_next(fz_context *ctx, fz_xml *elt, const char *tag, const char *att, const char *match)
{
if (elt == NULL)
return NULL;
return fz_xml_find_next_dfs(elt, tag, att, match);
}
void fz_dom_append_child(fz_context *ctx, fz_xml *parent, fz_xml *child)
{
fz_xml *x;
child = skip_doc_pointer(child);
if (parent == NULL || child == NULL)
return;
check_same_doc(ctx, parent, child);
/* Sanity checks: We can't add child to parent if parent is
* a child of child. */
x = parent;
while (x)
{
if (x == child)
fz_throw(ctx, FZ_ERROR_ARGUMENT, "Can't add a parent to its child.");
x = x->up;
}
/* First unlink child from anywhere it's currently linked in. */
if (child->u.node.prev)
child->u.node.prev->u.node.next = child->u.node.next;
else if (child->up->down == child && !FZ_DOCUMENT_ITEM(child->up))
child->up->down = child->u.node.next;
if (child->u.node.next)
child->u.node.next->u.node.prev = child->u.node.prev;
child->u.node.next = NULL;
child->u.node.prev = NULL;
/* Now find where to insert the child. */
if (parent->down == NULL)
{
/* Insert as first (and only) child. */
parent->down = child;
}
else
{
/* Find x, the current last child. */
x = parent->down;
while (x->u.node.next)
x = x->u.node.next;
/* And insert xchild after that. */
x->u.node.next = child;
child->u.node.prev = x;
}
child->up = parent;
}
void fz_dom_insert_before(fz_context *ctx, fz_xml *existing, fz_xml *elt)
{
fz_xml *x;
existing = skip_doc_pointer(existing);
elt = skip_doc_pointer(elt);
if (existing == NULL || elt == NULL)
return;
check_same_doc(ctx, existing, elt);
/* Sanity check: We can't add elt before existing if existing is
* a child of elt. */
x = existing;
while (x)
{
if (x == elt)
fz_throw(ctx, FZ_ERROR_ARGUMENT, "Can't add a node before its child.");
x = x->up;
}
/* First unlink elt from anywhere it's currently linked in. */
if (elt->u.node.prev)
elt->u.node.prev->u.node.next = elt->u.node.next;
else if (elt->up && !FZ_DOCUMENT_ITEM(elt->up))
elt->up->down = elt->u.node.next;
if (elt->u.node.next)
elt->u.node.next->u.node.prev = elt->u.node.prev;
elt->u.node.next = NULL;
elt->u.node.prev = NULL;
elt->up = NULL;
/* Now insert the element */
elt->u.node.prev = existing->u.node.prev;
if (elt->u.node.prev)
elt->u.node.prev->u.node.next = elt;
else if (existing->up && !FZ_DOCUMENT_ITEM(existing->up))
existing->up->down = elt;
elt->u.node.next = existing;
existing->u.node.prev = elt;
elt->up = existing->up;
}
void fz_dom_insert_after(fz_context *ctx, fz_xml *existing, fz_xml *elt)
{
fz_xml *x;
existing = skip_doc_pointer(existing);
elt = skip_doc_pointer(elt);
if (existing == NULL || elt == NULL)
return;
check_same_doc(ctx, existing, elt);
/* Sanity check: We can't add elt before existing if existing is
* a child of elt. */
x = existing;
while (x)
{
if (x == elt)
fz_throw(ctx, FZ_ERROR_ARGUMENT, "Can't add a node after its child.");
x = x->up;
}
/* First unlink child from anywhere it's currently linked in. */
if (elt->u.node.prev)
elt->u.node.prev->u.node.next = elt->u.node.next;
else if (elt->up && !FZ_DOCUMENT_ITEM(elt->up))
elt->up->down = elt->u.node.next;
if (elt->u.node.next)
elt->u.node.next->u.node.prev = elt->u.node.prev;
elt->u.node.next = NULL;
elt->u.node.prev = NULL;
/* Now insert the element */
elt->u.node.next = existing->u.node.next;
if (elt->u.node.next)
elt->u.node.next->u.node.prev = elt;
elt->u.node.prev = existing;
existing->u.node.next = elt;
elt->up = existing->up;
}
void fz_dom_remove(fz_context *ctx, fz_xml *elt)
{
elt = skip_doc_pointer(elt);
if (elt == NULL)
return;
/* Unlink child from anywhere it's currently linked in. */
if (elt->u.node.prev)
elt->u.node.prev->u.node.next = elt->u.node.next;
else if (elt->up && !FZ_DOCUMENT_ITEM(elt))
elt->up->down = elt->u.node.next;
if (elt->u.node.next)
elt->u.node.next->u.node.prev = elt->u.node.prev;
elt->u.node.next = NULL;
elt->u.node.prev = NULL;
elt->up = doc_pointer(elt);
}
fz_xml *fz_dom_first_child(fz_context *ctx, fz_xml *elt)
{
elt = skip_doc_pointer(elt);
if (elt == NULL || FZ_TEXT_ITEM(elt))
return NULL;
return elt->down;
}
fz_xml *fz_dom_parent(fz_context *ctx, fz_xml *elt)
{
elt = skip_doc_pointer(elt);
if (elt == NULL)
return NULL;
if (FZ_DOCUMENT_ITEM(elt->up))
return NULL;
return elt->up;
}
fz_xml *fz_dom_next(fz_context *ctx, fz_xml *elt)
{
elt = skip_doc_pointer(elt);
if (elt == NULL)
return NULL;
return elt->u.node.next;
}
fz_xml *fz_dom_previous(fz_context *ctx, fz_xml *elt)
{
elt = skip_doc_pointer(elt);
if (elt == NULL)
return NULL;
return elt->u.node.prev;
}
void fz_dom_add_attribute(fz_context *ctx, fz_xml *elt, const char *att, const char *value)
{
struct attribute *attr;
size_t len, size;
char *mvalue = NULL;
fz_xml *doc;
elt = skip_doc_pointer(elt);
if (elt == NULL || att == NULL)
return;
if (FZ_TEXT_ITEM(elt))
fz_throw(ctx, FZ_ERROR_ARGUMENT, "Cannot add attributes to text node.");
/* Move value to being a malloced thing, with the entity parsing done. */
if (value) {
char *d;
const char *s = value;
d = mvalue = fz_malloc(ctx, strlen(value)+1);
while (*s)
{
if (*s == '&') {
int c;
s += xml_parse_entity(&c, s);
d += fz_runetochar(d, c);
}
else
*d++ = *s++;
}
*d = 0;
}
/* Do we have an attribute we can reuse? */
attr = elt->u.node.u.d.atts;
while (attr)
{
if (strcmp(att, attr->name) == 0)
{
/* Reuse this one. */
break;
}
attr = attr->next;
}
if (attr && attr->value)
{
if (mvalue == NULL)
{
/* Just rewrite the existing value to be NULL. This
* 'leaks' the old value within the pool, so it will
* be cleaned up at the end. */
attr->value = NULL;
return;
}
if (strcmp(mvalue, attr->value) == 0)
{
/* Old and new values match. Nothing to change. */
return;
}
}
doc = doc_pointer(elt);
/* Move mvalue to be an fz_pool thing. */
if (mvalue)
{
char *tmp;
fz_try(ctx)
{
tmp = fz_pool_alloc(ctx, doc->u.doc.pool, strlen(mvalue)+1);
strcpy(tmp, mvalue);
}
fz_always(ctx)
fz_free(ctx, mvalue);
fz_catch(ctx)
fz_rethrow(ctx);
mvalue = tmp;
}
/* Make a new one and prepend it. */
len = strlen(att) + 1;
size = offsetof(struct attribute, name) + len;
attr = fz_pool_alloc(ctx, doc->u.doc.pool, size);
memcpy(attr->name, att, len);
attr->next = elt->u.node.u.d.atts;
elt->u.node.u.d.atts = attr;
attr->value = mvalue;
}
void fz_dom_remove_attribute(fz_context *ctx, fz_xml *elt, const char *att)
{
struct attribute **attr;
elt = skip_doc_pointer(elt);
if (elt == NULL || att == NULL)
return;
if (FZ_TEXT_ITEM(elt))
fz_throw(ctx, FZ_ERROR_ARGUMENT, "Cannot add attributes to text node.");
attr = &elt->u.node.u.d.atts;
while (*attr)
{
if (strcmp(att, (*attr)->name) == 0)
{
/* Delete this one. */
/* The old attr/value are 'leaked' within the pool. */
*attr = (*attr)->next;
break;
}
attr = &(*attr)->next;
}
}
const char *fz_dom_attribute(fz_context *ctx, fz_xml *elt, const char *att)
{
struct attribute *attr;
elt = skip_doc_pointer(elt);
if (elt == NULL || att == NULL)
return NULL;
/* Text nodes don't have attributes. */
if (FZ_TEXT_ITEM(elt))
return NULL;
attr = elt->u.node.u.d.atts;
while (attr)
{
if (strcmp(att, attr->name) == 0)
{
/* Found! */
return attr->value;
}
}
return NULL;
}
const char *fz_dom_get_attribute(fz_context *ctx, fz_xml *elt, int i, const char **att)
{
struct attribute *attr;
if (elt == NULL || att == NULL)
{
if (att)
*att = NULL;
return NULL;
}
/* Text nodes don't have attributes. */
if (FZ_TEXT_ITEM(elt) || i < 0)
{
*att = NULL;
return NULL;
}
attr = elt->u.node.u.d.atts;
while (attr)
{
if (i == 0)
{
/* Found! */
*att = attr->name;
return attr->value;
}
i--;
attr = attr->next;
}
*att = NULL;
return NULL;
}
|