leptris 1.9.338.0-arm-linux → 1.9.341.0-arm-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +33 -0
  3. data/Rakefile +1 -1
  4. data/ext/leptris/native/native.c +18 -1
  5. data/lib/leptris/version.rb +1 -1
  6. data/lib/leptris/xml/descriptor.rb +39 -3
  7. data/lib/leptris/xml/element.rb +18 -7
  8. data/lib/leptris/xml/ffi.rb +27 -0
  9. data/lib/leptris/xml/native.so +0 -0
  10. data/lib/leptris/xml/node.rb +22 -0
  11. data/lib/libleptris.so +0 -0
  12. data/vendor-src/README.md +1 -1
  13. data/vendor-src/libleptris/CHANGELOG.md +87 -0
  14. data/vendor-src/libleptris/CMakeLists.txt +1 -1
  15. data/vendor-src/libleptris/src/include/leptris/descriptor.h +27 -0
  16. data/vendor-src/libleptris/src/include/leptris.h +12 -0
  17. data/vendor-src/libleptris/src/leptris/descriptor/plan.c +226 -12
  18. data/vendor-src/libleptris/src/leptris/dom/element_modify.c +88 -4
  19. data/vendor-src/libleptris/src/leptris/serialize/serialize.c +38 -0
  20. data/vendor-src/libleptris/src/leptris/xsd/compile.c +79 -0
  21. data/vendor-src/libleptris/test/abi/test_descriptor.cpp +120 -0
  22. data/vendor-src/libleptris/test/dom/test_dom.cpp +193 -0
  23. data/vendor-src/libleptris/test/serializer/test_serialize.cpp +25 -0
  24. data/vendor-src/libleptris/test/xsd/libxml2-cases/import_dag/a1.xsd +5 -0
  25. data/vendor-src/libleptris/test/xsd/libxml2-cases/import_dag/a2.xsd +4 -0
  26. data/vendor-src/libleptris/test/xsd/libxml2-cases/import_dag/b1.xsd +5 -0
  27. data/vendor-src/libleptris/test/xsd/libxml2-cases/import_dag/b2.xsd +4 -0
  28. data/vendor-src/libleptris/test/xsd/libxml2-cases/import_dag/s.xsd +3 -0
  29. data/vendor-src/libleptris/test/xsd/libxml2-cases/import_dag/w.xsd +5 -0
  30. data/vendor-src/libleptris/test/xsd/test_xsd_compile.cpp +15 -0
  31. data/vendor-src/libleptris/vcpkg.json +1 -1
  32. metadata +7 -1
@@ -1384,14 +1384,40 @@ static int dp_emit_element_open(const struct leptris_plan_result* v,
1384
1384
  return 1;
1385
1385
  }
1386
1386
 
1387
+ /* #425: plan-serialize formatting — depth-tracked indent for
1388
+ * element boundaries. NULL fmt = the compact historical form.
1389
+ * RAW/CONTENT runs stay verbatim inline (the byte-exact
1390
+ * mixed-content rule, #534 discipline): a subtree with any
1391
+ * non-element kid is emitted compact as a whole. */
1392
+ typedef struct {
1393
+ int indent; /* spaces per level; 0 = compact */
1394
+ const char* unit; /* NULL = spaces */
1395
+ } dp_fmt;
1396
+
1397
+ static int dp_sb_indent_newline(dp_sb* b, const dp_fmt* fmt, int depth) {
1398
+ if (!dp_sb_putn(b, "\n", 1)) return 0;
1399
+ /* One unit (or one indent-step of spaces) PER LEVEL. */
1400
+ if (fmt->unit) {
1401
+ size_t ul = strlen(fmt->unit);
1402
+ for (int i = 0; i < depth; i++)
1403
+ if (!dp_sb_putn(b, fmt->unit, ul)) return 0;
1404
+ return 1;
1405
+ }
1406
+ for (int i = 0; i < depth * fmt->indent; i++)
1407
+ if (!dp_sb_putn(b, " ", 1)) return 0;
1408
+ return 1;
1409
+ }
1410
+
1387
1411
  static int dp_emit_value(const struct leptris_plan_result* v,
1388
1412
  const dp_plan* container, const LeptrisPlan pool,
1389
- dp_sb* b, const dp_decls* decls, int is_root);
1413
+ dp_sb* b, const dp_decls* decls, int is_root,
1414
+ const dp_fmt* fmt, int depth);
1390
1415
 
1391
1416
  static int dp_emit_element(const struct leptris_plan_result* v,
1392
1417
  const char* name, const LeptrisPlan pool,
1393
1418
  const dp_plan* kids_container, dp_sb* b,
1394
- const dp_decls* decls, int is_root) {
1419
+ const dp_decls* decls, int is_root,
1420
+ const dp_fmt* fmt, int depth) {
1395
1421
  if (!dp_emit_element_open(v, name, b, decls, is_root)) return 0;
1396
1422
  const struct leptris_plan_result** kids = NULL;
1397
1423
  size_t nk = 0, cap = 0;
@@ -1408,23 +1434,38 @@ static int dp_emit_element(const struct leptris_plan_result* v,
1408
1434
  return 0;
1409
1435
  }
1410
1436
  qsort((void*)kids, nk, sizeof(*kids), dp_kid_pos_cmp);
1437
+ int indent_kids = fmt && fmt->indent > 0;
1438
+ for (size_t i = 0; i < nk && indent_kids; i++)
1439
+ if (kids[i]->kind == LEPTRIS_PLAN_VALUE_RAW)
1440
+ indent_kids = 0;
1411
1441
  /* #1565: kid ROWS live in the plan that WALKED this element —
1412
1442
  * container_plan is the plan whose row CAPTURED it (the
1413
1443
  * parent), so a nested member resolved its kids' rows against
1414
1444
  * the parent's rows and a CONTENT row serialized as an
1415
1445
  * empty-named wrapper. */
1416
1446
  int ok = 1;
1417
- for (size_t i = 0; i < nk && ok; i++)
1418
- ok = dp_emit_value(kids[i], kids_container, pool, b, NULL, 0);
1447
+ for (size_t i = 0; i < nk && ok; i++) {
1448
+ if (indent_kids && !dp_sb_indent_newline(b, fmt, depth + 1)) {
1449
+ ok = 0;
1450
+ break;
1451
+ }
1452
+ ok = dp_emit_value(kids[i], kids_container, pool, b, NULL, 0,
1453
+ fmt, indent_kids ? depth + 1 : depth);
1454
+ }
1419
1455
  free((void*)kids);
1420
1456
  if (!ok) return 0;
1421
- return dp_sb_puts(b, "</") && dp_sb_puts(b, name) &&
1422
- dp_sb_putn(b, ">", 1);
1457
+ if (indent_kids && !dp_sb_indent_newline(b, fmt, depth)) return 0;
1458
+ ok = dp_sb_puts(b, "</") && dp_sb_puts(b, name) &&
1459
+ dp_sb_putn(b, ">", 1);
1460
+ if (ok && is_root && fmt && fmt->indent > 0)
1461
+ ok = dp_sb_putn(b, "\n", 1);
1462
+ return ok;
1423
1463
  }
1424
1464
 
1425
1465
  static int dp_emit_value(const struct leptris_plan_result* v,
1426
1466
  const dp_plan* container, const LeptrisPlan pool,
1427
- dp_sb* b, const dp_decls* decls, int is_root) {
1467
+ dp_sb* b, const dp_decls* decls, int is_root,
1468
+ const dp_fmt* fmt, int depth) {
1428
1469
  if (v->kind == LEPTRIS_PLAN_VALUE_RAW)
1429
1470
  return dp_sb_puts(b, v->str ? v->str : "");
1430
1471
  if (v->kind == LEPTRIS_PLAN_VALUE_ELEMENT) {
@@ -1475,7 +1516,7 @@ static int dp_emit_value(const struct leptris_plan_result* v,
1475
1516
  &poolp->plans[cap->child_plan_index];
1476
1517
  }
1477
1518
  int ok = dp_emit_element(v, name, pool, kids_container, b,
1478
- decls, is_root);
1519
+ decls, is_root, fmt, depth);
1479
1520
  free(name);
1480
1521
  return ok;
1481
1522
  }
@@ -1500,9 +1541,9 @@ static int dp_emit_value(const struct leptris_plan_result* v,
1500
1541
  return dp_sb_escape(b, text, 0);
1501
1542
  }
1502
1543
 
1503
- LEPTRIS_API char* leptris_plan_serialize(LeptrisPlan plan,
1504
- const LeptrisPlanResult result,
1505
- LeptrisStatus* status) {
1544
+ static char* dp_serialize_impl(LeptrisPlan plan,
1545
+ const LeptrisPlanResult result,
1546
+ const dp_fmt* fmt, LeptrisStatus* status) {
1506
1547
  if (status) *status = LEPTRIS_OK;
1507
1548
  if (!plan || !result) {
1508
1549
  if (status) *status = LEPTRIS_ERROR_INVALID_ARG;
@@ -1518,7 +1559,7 @@ LEPTRIS_API char* leptris_plan_serialize(LeptrisPlan plan,
1518
1559
  }
1519
1560
  dp_sb b = {0};
1520
1561
  int ok = dp_emit_value(rv, NULL, (const LeptrisPlan)plan, &b,
1521
- &decls, 1);
1562
+ &decls, 1, fmt, 0);
1522
1563
  dp_decls_free(&decls);
1523
1564
  if (!ok) {
1524
1565
  free(b.s);
@@ -1535,6 +1576,179 @@ LEPTRIS_API char* leptris_plan_serialize(LeptrisPlan plan,
1535
1576
  return b.s;
1536
1577
  }
1537
1578
 
1579
+ LEPTRIS_API char* leptris_plan_serialize(LeptrisPlan plan,
1580
+ const LeptrisPlanResult result,
1581
+ LeptrisStatus* status) {
1582
+ return dp_serialize_impl(plan, result, NULL, status);
1583
+ }
1584
+
1585
+ /* #425: formatting-bearing variant — indent > 0 pretty-prints
1586
+ * element boundaries (RAW/CONTENT runs stay verbatim inline);
1587
+ * indent_unit replaces the two-space default per level. options
1588
+ * NULL = the compact historical form. opts_size must cover the
1589
+ * struct the caller compiled against (growable, #129 pattern). */
1590
+ LEPTRIS_API char* leptris_plan_serialize_ext(
1591
+ LeptrisPlan plan, const LeptrisPlanResult result,
1592
+ const leptris_plan_serialize_options* options, size_t options_size,
1593
+ LeptrisStatus* status) {
1594
+ if (status) *status = LEPTRIS_OK;
1595
+ if (options && options_size < sizeof(*options)) {
1596
+ if (status) *status = LEPTRIS_ERROR_INVALID_ARG;
1597
+ return NULL;
1598
+ }
1599
+ dp_fmt fmt;
1600
+ memset(&fmt, 0, sizeof(fmt));
1601
+ if (options) {
1602
+ fmt.indent = options->indent;
1603
+ fmt.unit = options->indent_unit;
1604
+ }
1605
+ return dp_serialize_impl(plan, result, &fmt, status);
1606
+ }
1607
+
1608
+ /* #426: materialize a built result as a native document tree —
1609
+ * the consumer path becomes build_result -> this -> the document
1610
+ * writer, with no string round trip. Attributes carry verbatim
1611
+ * (wire spellings); the #1626 root declarations ride as xmlns
1612
+ * attributes. Caller frees with leptris_document_free. */
1613
+ static LeptrisElement dp_mint_element(LeptrisDocument doc,
1614
+ const struct leptris_plan_result* v,
1615
+ const dp_plan* container,
1616
+ const LeptrisPlan pool);
1617
+
1618
+ static int dp_mint_kids(LeptrisDocument doc, LeptrisElement parent,
1619
+ const struct leptris_plan_result* v,
1620
+ const dp_plan* kids_container,
1621
+ const LeptrisPlan pool) {
1622
+ const struct leptris_plan_result** kids = NULL;
1623
+ size_t nk = 0, cap = 0;
1624
+ if (!dp_flatten_kids(v, &kids, &nk, &cap)) {
1625
+ free((void*)kids);
1626
+ return 0;
1627
+ }
1628
+ qsort((void*)kids, nk, sizeof(*kids), dp_kid_pos_cmp);
1629
+ int ok = 1;
1630
+ for (size_t i = 0; i < nk && ok; i++) {
1631
+ const struct leptris_plan_result* k = kids[i];
1632
+ if (k->kind == LEPTRIS_PLAN_VALUE_ELEMENT) {
1633
+ LeptrisElement el =
1634
+ dp_mint_element(doc, k, kids_container, pool);
1635
+ if (!el) {
1636
+ ok = 0;
1637
+ break;
1638
+ }
1639
+ ok = leptris_element_append_child(parent, el) == LEPTRIS_OK;
1640
+ } else if (k->kind == LEPTRIS_PLAN_VALUE_RAW ||
1641
+ (kids_container && k->row_index != UINT32_MAX &&
1642
+ k->row_index < kids_container->child_count &&
1643
+ kids_container->child_plans[k->row_index].kind ==
1644
+ LEPTRIS_PLAN_KIND_CONTENT)) {
1645
+ LeptrisNodeRef t =
1646
+ leptris_text_node_create(doc, k->str ? k->str : "");
1647
+ ok = t && leptris_element_append_child(
1648
+ parent, (LeptrisElement)t) == LEPTRIS_OK;
1649
+ } else {
1650
+ char* name = dp_wrapper_name(k);
1651
+ if (!name) {
1652
+ ok = 0;
1653
+ break;
1654
+ }
1655
+ LeptrisElement w = leptris_element_new_with_attributes(
1656
+ doc, name, NULL, NULL, 0);
1657
+ free(name);
1658
+ if (!w) {
1659
+ ok = 0;
1660
+ break;
1661
+ }
1662
+ ok = leptris_element_set_text(w, k->str ? k->str : "") ==
1663
+ LEPTRIS_OK &&
1664
+ leptris_element_append_child(parent, w) == LEPTRIS_OK;
1665
+ }
1666
+ }
1667
+ free((void*)kids);
1668
+ return ok;
1669
+ }
1670
+
1671
+ static LeptrisElement dp_mint_element(LeptrisDocument doc,
1672
+ const struct leptris_plan_result* v,
1673
+ const dp_plan* container,
1674
+ const LeptrisPlan pool) {
1675
+ /* Name/kids resolution mirrors dp_emit_value. */
1676
+ const struct leptris_plan* poolp = (const struct leptris_plan*)pool;
1677
+ const dp_plan* self = &poolp->plans[v->container_plan];
1678
+ const char* local = NULL;
1679
+ if (v->name) {
1680
+ const char* colon = strchr(v->name, ':');
1681
+ local = colon ? colon + 1 : v->name;
1682
+ } else {
1683
+ local = self->element_name;
1684
+ }
1685
+ char* name = v->ns_prefix
1686
+ ? dp_join_colon(v->ns_prefix, local ? local : "")
1687
+ : dp_strdup(local ? local : "");
1688
+ if (!name) return NULL;
1689
+ LeptrisElement el = leptris_element_new_with_attributes(
1690
+ doc, name, v->attr_names, v->attr_values, v->attr_count);
1691
+ free(name);
1692
+ if (!el) return NULL;
1693
+ const dp_plan* kids_container = &poolp->plans[v->container_plan];
1694
+ if (container && v->row_index != UINT32_MAX &&
1695
+ v->row_index < container->child_count) {
1696
+ const leptris_child_plan* cap =
1697
+ &container->child_plans[v->row_index];
1698
+ if (cap->child_plan_index >= 0 &&
1699
+ (uint32_t)cap->child_plan_index < poolp->plan_count)
1700
+ kids_container = &poolp->plans[cap->child_plan_index];
1701
+ }
1702
+ if (!dp_mint_kids(doc, el, v, kids_container, pool)) return NULL;
1703
+ return el;
1704
+ }
1705
+
1706
+ LEPTRIS_API LeptrisDocument leptris_document_from_plan_result(
1707
+ LeptrisPlan plan, const LeptrisPlanResult result,
1708
+ LeptrisStatus* status) {
1709
+ if (status) *status = LEPTRIS_OK;
1710
+ if (!plan || !result) {
1711
+ if (status) *status = LEPTRIS_ERROR_INVALID_ARG;
1712
+ return NULL;
1713
+ }
1714
+ const struct leptris_plan_result* rv =
1715
+ (const struct leptris_plan_result*)result;
1716
+ if (rv->kind != LEPTRIS_PLAN_VALUE_ELEMENT) {
1717
+ if (status) *status = LEPTRIS_ERROR_INVALID_ARG;
1718
+ return NULL;
1719
+ }
1720
+ LeptrisDocument doc = leptris_document_create();
1721
+ if (!doc) {
1722
+ if (status) *status = LEPTRIS_ERROR_MEMORY;
1723
+ return NULL;
1724
+ }
1725
+ dp_decls decls = {0};
1726
+ int ok = dp_collect_decls(rv, &decls);
1727
+ LeptrisElement root = NULL;
1728
+ if (ok) {
1729
+ root = dp_mint_element(doc, rv, NULL, (const LeptrisPlan)plan);
1730
+ ok = root != NULL;
1731
+ }
1732
+ for (size_t i = 0; ok && i < decls.n; i++) {
1733
+ char* dn = dp_join_colon("xmlns", decls.p[i]);
1734
+ if (!dn) {
1735
+ ok = 0;
1736
+ break;
1737
+ }
1738
+ ok = leptris_element_set_attribute(root, dn, decls.u[i]) ==
1739
+ LEPTRIS_OK;
1740
+ free(dn);
1741
+ }
1742
+ if (ok) ok = leptris_document_set_root(doc, root) == LEPTRIS_OK;
1743
+ dp_decls_free(&decls);
1744
+ if (!ok) {
1745
+ leptris_document_free(doc);
1746
+ if (status) *status = LEPTRIS_ERROR_MEMORY;
1747
+ return NULL;
1748
+ }
1749
+ return doc;
1750
+ }
1751
+
1538
1752
  LEPTRIS_API LeptrisPlanResult leptris_plan_result_build(
1539
1753
  LeptrisPlan plan, const leptris_plan_row_op* ops, size_t count,
1540
1754
  LeptrisStatus* status) {
@@ -500,9 +500,60 @@ LeptrisStatus leptris_element_add_child_addr(uintptr_t parent_addr,
500
500
  (LeptrisElement)child_addr);
501
501
  }
502
502
 
503
+ LeptrisStatus leptris_element_add_child_addr_installed(
504
+ uintptr_t parent_addr, uintptr_t child_addr,
505
+ uintptr_t* installed_addr) {
506
+ if (!parent_addr || !child_addr) return LEPTRIS_ERROR_NULL_ARG;
507
+ if (installed_addr) *installed_addr = child_addr;
508
+
509
+ /* The same-document fast path installs the argument itself;
510
+ * the foreign gate deep-copies (#1528) and the status-only
511
+ * face drops the copy's address — callers kept addressing the
512
+ * foreign original, which dies with its document (#1636).
513
+ * This face always reports the effective node. */
514
+ LeptrisElement original = (LeptrisElement)child_addr;
515
+ if (leptris_element_get_document(original) ==
516
+ leptris_element_get_document((LeptrisElement)parent_addr)) {
517
+ return leptris_element_append_child(
518
+ (LeptrisElement)parent_addr, original);
519
+ }
520
+
521
+ LeptrisElement installed =
522
+ (LeptrisElement)adopt_for_splice((LeptrisNode*)original,
523
+ leptris_element_get_document(
524
+ (LeptrisElement)parent_addr));
525
+ if (!installed) return LEPTRIS_ERROR_MEMORY;
526
+
527
+ /* Cycle rejection rides the append; report the copy. */
528
+ LeptrisStatus rc = leptris_element_append_child(
529
+ (LeptrisElement)parent_addr, installed);
530
+ if (rc != LEPTRIS_OK) return rc;
531
+ if (installed_addr) *installed_addr = (uintptr_t)installed;
532
+ return LEPTRIS_OK;
533
+ }
534
+
503
535
  LeptrisStatus leptris_element_append_child(LeptrisElement parent, LeptrisElement child) {
504
536
  if (!parent || !child) return LEPTRIS_ERROR_NULL_ARG;
505
537
 
538
+ /* #1636: appending a node into its own descendant subtree
539
+ * builds a parent cycle — every tree walker (serializer
540
+ * first) then recurses without bound and the process dies
541
+ * (_chkstk SEGV on Windows). Parse-built trees cannot cycle
542
+ * (the open stack is a stack); only mutation can. Reject the
543
+ * ancestor edge: walk child's parent chain for parent. The
544
+ * same-document fast path below never sees foreign copies as
545
+ * ancestors, so the walk runs on the caller's node — O(depth),
546
+ * depth bounded by the parse cap. */
547
+ {
548
+ if (parent == child) return LEPTRIS_ERROR_INVALID_ARG;
549
+ LeptrisElement p = leptris_element_parent(parent);
550
+ int guard = 0;
551
+ while (p && guard++ < 8192) {
552
+ if (p == child) return LEPTRIS_ERROR_INVALID_ARG;
553
+ p = leptris_element_parent(p);
554
+ }
555
+ }
556
+
506
557
  /* Single document resolution per mutation (TODO 195c): the
507
558
  * root walk + map lookup was paid three times per append
508
559
  * (twice here, once inside the internal for the tail cache). */
@@ -542,6 +593,13 @@ LeptrisStatus leptris_element_append_child(LeptrisElement parent, LeptrisElement
542
593
  LeptrisStatus leptris_element_prepend_child(LeptrisElement parent, LeptrisElement child) {
543
594
  if (!parent || !child) return LEPTRIS_ERROR_NULL_ARG;
544
595
 
596
+ /* #1636: no cycles — see leptris_element_append_child. */
597
+ if (parent == child) return LEPTRIS_ERROR_INVALID_ARG;
598
+ for (LeptrisElement p = leptris_element_parent(parent);
599
+ p; p = leptris_element_parent(p)) {
600
+ if (p == child) return LEPTRIS_ERROR_INVALID_ARG;
601
+ }
602
+
545
603
  /* #1528: a foreign document's node is adopted by copy, never
546
604
  * spliced raw. */
547
605
  child = (LeptrisElement)adopt_for_splice(
@@ -563,6 +621,19 @@ LeptrisStatus leptris_element_prepend_child(LeptrisElement parent, LeptrisElemen
563
621
  LeptrisStatus leptris_element_insert_before(LeptrisElement sibling, LeptrisElement new_node) {
564
622
  if (!sibling || !new_node) return LEPTRIS_ERROR_NULL_ARG;
565
623
 
624
+ /* #1636: reparenting a node into its own descendant subtree
625
+ * builds a parent cycle — tree walkers recurse unbounded and
626
+ * the process dies. Reject when the new parent position lies
627
+ * under the node itself. */
628
+ if (((LeptrisNode*)new_node)->type == LEPTRIS_NODE_TYPE_ELEMENT) {
629
+ if ((LeptrisElement)sibling == (LeptrisElement)new_node)
630
+ return LEPTRIS_ERROR_INVALID_ARG;
631
+ for (LeptrisElement p = leptris_element_parent(sibling);
632
+ p; p = leptris_element_parent(p)) {
633
+ if (p == (LeptrisElement)new_node)
634
+ return LEPTRIS_ERROR_INVALID_ARG;
635
+ }
636
+ }
566
637
  LeptrisNode* new_node_ptr = (LeptrisNode*)new_node;
567
638
  LeptrisNode* sibling_ptr = (LeptrisNode*)sibling;
568
639
 
@@ -634,7 +705,7 @@ LeptrisStatus leptris_element_insert_before(LeptrisElement sibling, LeptrisEleme
634
705
 
635
706
  /* Set parent (type-dispatching). TODO 155 Phase A: document field
636
707
  * removed; non-root elements reach doc via walk to root. */
637
- if (new_node_ptr->type == LEPTRIS_NODE_TYPE_ELEMENT) {
708
+ if (((LeptrisNode*)new_node)->type == LEPTRIS_NODE_TYPE_ELEMENT) {
638
709
  leptris_element_set_parent((LeptrisElement)new_node_ptr, parent);
639
710
  } else {
640
711
  switch (new_node_ptr->type) {
@@ -660,7 +731,7 @@ LeptrisStatus leptris_element_insert_before(LeptrisElement sibling, LeptrisEleme
660
731
 
661
732
  /* Issue #213: maintain child_count for element children only,
662
733
  * matching leptris_element_append_child_internal. */
663
- if (new_node_ptr->type == LEPTRIS_NODE_TYPE_ELEMENT) {
734
+ if (((LeptrisNode*)new_node)->type == LEPTRIS_NODE_TYPE_ELEMENT) {
664
735
  parent->child_count++;
665
736
  }
666
737
 
@@ -676,6 +747,19 @@ LeptrisStatus leptris_element_insert_before(LeptrisElement sibling, LeptrisEleme
676
747
  LeptrisStatus leptris_element_insert_after(LeptrisElement sibling, LeptrisElement new_node) {
677
748
  if (!sibling || !new_node) return LEPTRIS_ERROR_NULL_ARG;
678
749
 
750
+ /* #1636: reparenting a node into its own descendant subtree
751
+ * builds a parent cycle — tree walkers recurse unbounded and
752
+ * the process dies. Reject when the new parent position lies
753
+ * under the node itself. */
754
+ if (((LeptrisNode*)new_node)->type == LEPTRIS_NODE_TYPE_ELEMENT) {
755
+ if ((LeptrisElement)sibling == (LeptrisElement)new_node)
756
+ return LEPTRIS_ERROR_INVALID_ARG;
757
+ for (LeptrisElement p = leptris_element_parent(sibling);
758
+ p; p = leptris_element_parent(p)) {
759
+ if (p == (LeptrisElement)new_node)
760
+ return LEPTRIS_ERROR_INVALID_ARG;
761
+ }
762
+ }
679
763
  LeptrisNode* new_node_ptr = (LeptrisNode*)new_node;
680
764
  LeptrisNode* sibling_ptr = (LeptrisNode*)sibling;
681
765
 
@@ -731,7 +815,7 @@ LeptrisStatus leptris_element_insert_after(LeptrisElement sibling, LeptrisElemen
731
815
 
732
816
  /* Set parent (type-dispatching). TODO 155 Phase A: document field
733
817
  * removed; non-root elements reach doc via walk to root. */
734
- if (new_node_ptr->type == LEPTRIS_NODE_TYPE_ELEMENT) {
818
+ if (((LeptrisNode*)new_node)->type == LEPTRIS_NODE_TYPE_ELEMENT) {
735
819
  leptris_element_set_parent((LeptrisElement)new_node_ptr, parent);
736
820
  } else {
737
821
  switch (new_node_ptr->type) {
@@ -761,7 +845,7 @@ LeptrisStatus leptris_element_insert_after(LeptrisElement sibling, LeptrisElemen
761
845
  leptris_elem_set_last_child(parent, new_node_ptr);
762
846
  }
763
847
 
764
- if (new_node_ptr->type == LEPTRIS_NODE_TYPE_ELEMENT) {
848
+ if (((LeptrisNode*)new_node)->type == LEPTRIS_NODE_TYPE_ELEMENT) {
765
849
  parent->child_count++;
766
850
  }
767
851
 
@@ -595,6 +595,39 @@ static int html_break_after_elem(LeptrisElement elem,
595
595
  return 1;
596
596
  }
597
597
 
598
+ /* leptris#1647 (indent_html only): a block element whose SINGLE
599
+ * child is itself a block keeps the line layout — the child's own
600
+ * block content must not glue to the parent's tag (and the parent
601
+ * must not poison the subtree with mixed=1). The legacy §16.2
602
+ * shape is untouched: its site rules stay inline for a single
603
+ * child. p* parents stay inline (site-rule parity). */
604
+ static int html_block_child_needs_lines(LeptrisElement elem,
605
+ const char* name, size_t len,
606
+ int flags) {
607
+ if (!html_breaks_block(name, len, flags)) return 0;
608
+ if (name[0] == 'p') return 0;
609
+ LeptrisNode* c =
610
+ leptris_node_first_child_internal((LeptrisNode*)elem);
611
+ if (!c || leptris_node_get_next_sibling(c)) return 0;
612
+ if (c->type != LEPTRIS_NODE_TYPE_ELEMENT) return 0;
613
+ /* The child must itself take the line layout: it breaks its own
614
+ * children (site 1), or it nests a further element. A block
615
+ * whose only child is text (head>title) stays inline. */
616
+ {
617
+ LeptrisElement ce = (LeptrisElement)c;
618
+ const char* cn = elem_name(ce);
619
+ size_t cnl = (ce->name_len != 0xFF) ? (size_t)ce->name_len
620
+ : strlen(cn);
621
+ if (html_break_before_children(ce, cn, cnl,
622
+ html_elem_sem_flags(ce)))
623
+ return 1;
624
+ for (LeptrisNode* g = leptris_node_first_child_internal(c); g;
625
+ g = leptris_node_get_next_sibling(g))
626
+ if (g->type == LEPTRIS_NODE_TYPE_ELEMENT) return 1;
627
+ }
628
+ return 0;
629
+ }
630
+
598
631
  /* Emit the CDATA BODY, splitting "]]>" across section closes. The
599
632
  * caller owns the surrounding <![CDATA[ / ]]> brackets (run merging).
600
633
  * The close scan runs to len-3 INCLUSIVE — a "]]>" at the very END
@@ -1639,6 +1672,8 @@ void serialize_element_internal(LeptrisElement root_elem, SerializeBuffer* buf,
1639
1672
  * the newlines. Elements that keep their children
1640
1673
  * inline (single-child rule) own no whitespace. */
1641
1674
  int brk_bc = html_break_before_children(e, name, nl, hf);
1675
+ if (!brk_bc)
1676
+ brk_bc = html_block_child_needs_lines(e, name, nl, hf);
1642
1677
  if (brk_bc) {
1643
1678
  mixed = buf->ws_mixed
1644
1679
  ? ser_children_have_any_text(fc)
@@ -1650,6 +1685,9 @@ void serialize_element_internal(LeptrisElement root_elem, SerializeBuffer* buf,
1650
1685
  if (brk_bc) buffer_append_newline(buf);
1651
1686
  }
1652
1687
  close_brk = html_break_before_close(e, name, nl, hf);
1688
+ if (!close_brk && buf->indent_html)
1689
+ close_brk =
1690
+ html_block_child_needs_lines(e, name, nl, hf);
1653
1691
  } else {
1654
1692
  /* #129 indent_text: the formatter owns ALL whitespace —
1655
1693
  * no element is "mixed" for layout purposes. */
@@ -46,6 +46,12 @@ struct leptris_xsd_schema {
46
46
  int version_11; /* xs:schema @version = "1.1" */
47
47
  char* base_dir; /* schemaLocation resolution (file compile) */
48
48
  int include_depth; /* include/import recursion guard */
49
+ /* leptris#1644: already-captured schema units (canonical
50
+ * location per entry) — the include/import DAG is dense and
51
+ * cyclic; without this set every DAG path re-parses and
52
+ * re-captures the whole subtree (multi-GB / minutes). */
53
+ char** visited;
54
+ size_t visited_len, visited_cap;
49
55
  void* validator; /* slice-4 parked error list */
50
56
  char* error;
51
57
  };
@@ -688,6 +694,70 @@ fail:
688
694
  * the owning schema's base_dir (string-compiled schemas have none
689
695
  * and skip them); the referenced root's declarations merge into
690
696
  * the same tables. */
697
+ /* leptris#1644: lexical path normalization — collapse "." and
698
+ * ".." segments (and duplicate separators) so the same file reached
699
+ * through different relative spellings yields one dedup key.
700
+ * Purely lexical: no filesystem access, portable. */
701
+ static void xsd_path_normalize(const char* in, char* out, size_t outsz) {
702
+ struct {
703
+ const char* p;
704
+ size_t len;
705
+ } comps[512];
706
+ size_t n = 0;
707
+ int is_abs = in[0] == '/';
708
+ const char* q = in;
709
+ while (*q) {
710
+ while (*q == '/') q++;
711
+ if (!*q) break;
712
+ const char* start = q;
713
+ while (*q && *q != '/') q++;
714
+ size_t len = (size_t)(q - start);
715
+ if (len == 1 && start[0] == '.') continue;
716
+ if (len == 2 && start[0] == '.' && start[1] == '.') {
717
+ if (n > 0)
718
+ n--; /* ".." pops; a leading pop on an absolute
719
+ path would escape the root — drop it */
720
+ continue;
721
+ }
722
+ if (n < sizeof(comps) / sizeof(comps[0])) {
723
+ comps[n].p = start;
724
+ comps[n].len = len;
725
+ n++;
726
+ }
727
+ }
728
+ size_t o = 0;
729
+ if (is_abs && o < outsz) out[o++] = '/';
730
+ for (size_t i = 0; i < n; i++) {
731
+ if (o && o < outsz && out[o - 1] != '/') out[o++] = '/';
732
+ if (o + comps[i].len >= outsz) break;
733
+ memcpy(out + o, comps[i].p, comps[i].len);
734
+ o += comps[i].len;
735
+ }
736
+ if (o == 0 && outsz > 1) out[o++] = '.';
737
+ out[o] = 0;
738
+ }
739
+
740
+ static int xsd_visited_has(struct leptris_xsd_schema* s,
741
+ const char* path) {
742
+ for (size_t i = 0; i < s->visited_len; i++)
743
+ if (strcmp(s->visited[i], path) == 0) return 1;
744
+ return 0;
745
+ }
746
+
747
+ static void xsd_visited_add(struct leptris_xsd_schema* s,
748
+ const char* path) {
749
+ if (s->visited_len == s->visited_cap) {
750
+ size_t nc = s->visited_cap ? s->visited_cap * 2 : 8;
751
+ char** nv = (char**)realloc(s->visited, nc * sizeof(*nv));
752
+ if (!nv) return; /* dedup degrades to the old behavior */
753
+ s->visited = nv;
754
+ s->visited_cap = nc;
755
+ }
756
+ char* dup = xsd_strdup(path);
757
+ if (!dup) return;
758
+ s->visited[s->visited_len++] = dup;
759
+ }
760
+
691
761
  static void xsd_capture_root(struct leptris_xsd_schema* s,
692
762
  LeptrisElement root) {
693
763
  for (LeptrisNodeRef n =
@@ -712,6 +782,13 @@ static void xsd_capture_root(struct leptris_xsd_schema* s,
712
782
  char path[4096];
713
783
  snprintf(path, sizeof(path), "%s/%s", s->base_dir,
714
784
  loc);
785
+ /* leptris#1644: capture each schema unit once per
786
+ * compile — mark before recursing so cycles close
787
+ * (w.xsd includes a1.xsd includes w.xsd). */
788
+ char canon[4096];
789
+ xsd_path_normalize(path, canon, sizeof(canon));
790
+ if (xsd_visited_has(s, canon)) continue;
791
+ xsd_visited_add(s, canon);
715
792
  size_t flen = 0;
716
793
  char* text = xsd_read_file(path, &flen);
717
794
  if (text) {
@@ -1000,6 +1077,8 @@ LEPTRIS_API void leptris_xsd_free(LeptrisXsdSchema schema) {
1000
1077
  g = gn;
1001
1078
  }
1002
1079
  if (s->default_open) xsd_cm_free(s->default_open);
1080
+ for (size_t i = 0; i < s->visited_len; i++) free(s->visited[i]);
1081
+ free(s->visited);
1003
1082
  free(s->target_ns);
1004
1083
  free(s->base_dir);
1005
1084
  if (s->validator)