leptris 1.9.355.0-ppc64le-linux → 1.9.359.0-ppc64le-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 (59) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/README.adoc +13 -5
  4. data/Rakefile +1 -1
  5. data/ext/leptris/native/native.c +383 -5
  6. data/lib/leptris/version.rb +1 -1
  7. data/lib/leptris/xml/cdata.rb +3 -0
  8. data/lib/leptris/xml/comment.rb +3 -0
  9. data/lib/leptris/xml/declaration.rb +59 -0
  10. data/lib/leptris/xml/doc_type.rb +33 -1
  11. data/lib/leptris/xml/document.rb +74 -2
  12. data/lib/leptris/xml/element.rb +21 -1
  13. data/lib/leptris/xml/entity_reference.rb +3 -0
  14. data/lib/leptris/xml/native.so +0 -0
  15. data/lib/leptris/xml/node_set.rb +22 -0
  16. data/lib/leptris/xml/processing_instruction.rb +3 -0
  17. data/lib/leptris/xml/sax/parser.rb +102 -78
  18. data/lib/leptris/xml/text.rb +4 -0
  19. data/lib/leptris/xml.rb +14 -0
  20. data/lib/libleptris.so +0 -0
  21. data/scripts/gem_smoke.rb +6 -3
  22. data/vendor-src/README.md +1 -1
  23. data/vendor-src/libleptris/CHANGELOG.md +134 -0
  24. data/vendor-src/libleptris/CMakeLists.txt +1 -1
  25. data/vendor-src/libleptris/TODO.xslt-full/REMAINING.md +37 -18
  26. data/vendor-src/libleptris/cli/commands/validate.c +15 -2
  27. data/vendor-src/libleptris/src/include/leptris.h +60 -4
  28. data/vendor-src/libleptris/src/leptris/dom/element.c +8 -0
  29. data/vendor-src/libleptris/src/leptris/dtd/content_check.c +280 -144
  30. data/vendor-src/libleptris/src/leptris/dtd/parser.c +17 -4
  31. data/vendor-src/libleptris/src/leptris/rng/parse.c +53 -8
  32. data/vendor-src/libleptris/src/leptris/rng/validate.c +63 -21
  33. data/vendor-src/libleptris/src/leptris/xinclude/xinclude.c +304 -12
  34. data/vendor-src/libleptris/src/leptris/xpath/evaluator.c +30 -0
  35. data/vendor-src/libleptris/src/leptris/xpath/evaluator.h +11 -0
  36. data/vendor-src/libleptris/src/leptris/xpath/evaluator_axes.c +3 -3
  37. data/vendor-src/libleptris/src/leptris/xpath/evaluator_path.c +4 -20
  38. data/vendor-src/libleptris/src/leptris/xpath/parser.c +4 -1
  39. data/vendor-src/libleptris/src/leptris/xpath/vm.c +41 -11
  40. data/vendor-src/libleptris/src/leptris/xpath/xpath_compiled.c +4 -3
  41. data/vendor-src/libleptris/src/leptris/xpath/xpath_public.c +2 -1
  42. data/vendor-src/libleptris/src/leptris/xsd/compile.c +981 -38
  43. data/vendor-src/libleptris/src/leptris/xsd/content_model.c +15 -3
  44. data/vendor-src/libleptris/src/leptris/xsd/validate.c +27 -9
  45. data/vendor-src/libleptris/src/leptris/xsd/xsd_internal.h +1 -0
  46. data/vendor-src/libleptris/test/cli/test_cli.cpp +50 -14
  47. data/vendor-src/libleptris/test/dom/test_dom.cpp +27 -0
  48. data/vendor-src/libleptris/test/dtd/test_dtd_validate.cpp +80 -4
  49. data/vendor-src/libleptris/test/rng/include-cases/attr-text-choice.rng +18 -0
  50. data/vendor-src/libleptris/test/rng/include-cases/dup-inc-mid.rng +7 -0
  51. data/vendor-src/libleptris/test/rng/include-cases/dup-inc-shared.rng +5 -0
  52. data/vendor-src/libleptris/test/rng/include-cases/dup-inc-top.rng +6 -0
  53. data/vendor-src/libleptris/test/rng/test_rng.cpp +101 -0
  54. data/vendor-src/libleptris/test/xinclude/test_xinclude.cpp +261 -0
  55. data/vendor-src/libleptris/test/xpath/test_xpath.cpp +169 -0
  56. data/vendor-src/libleptris/test/xpath/test_xpath_conformance.cpp +8 -1
  57. data/vendor-src/libleptris/test/xsd/test_xsd_compile.cpp +128 -11
  58. data/vendor-src/libleptris/vcpkg.json +1 -1
  59. metadata +7 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9fbd247c0dbdbc145f3493538bbd76b1358af79765d2d6605373f7d359b8689a
4
- data.tar.gz: 4e12fa371b515f080adaa61a1a5f9427e3f64785ba4779c3bcb723bb15a25fa8
3
+ metadata.gz: c3f6fc840f3e069bf53cacbe814e674a1a9969cce4df6be3b6cc3f1f3173fead
4
+ data.tar.gz: a5b931174f0f06be08b5aacc869fc5f3acf9f8561cc3f33b447a998673e69d03
5
5
  SHA512:
6
- metadata.gz: 9ecbf3034d45e54c8265bd4bb822f96cc4eae56965cb9eb58ed2ac1f6ed634126d8eaf2c92069a9cc571556673ed8d956d65518ac6adb6099aecba1dd6801d5a
7
- data.tar.gz: 1b37a7e199f6de387b009570b85b167e8c9531638a2c3c8f3f56339897491a0a90ca929767e28789cae91b91eb1223ee379d178a951f4b596b6183fd1db587ce
6
+ metadata.gz: '080104d25a364330783731dc603d640c313c8e423157de16ee2112ab01ce013c6f3b3b78d0e51a663eac19f20c7e00ff954ce62bc9c3e23c781b8b58098dbdea'
7
+ data.tar.gz: 4a6907785411bf42d122b05f3b14ad9941c16ed72a7e54ed52323f438a06dd5ead2d7f30ac4317e0685d0b3312c532a289519c61bdb9bb796489c0da127cc3ec
data/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ ## [1.9.356.0] - 2026-10-14
2
+
3
+ ### Fixed — engine sync (libleptris 1.9.356)
4
+
5
+ - **Native attribute faces type-guarded (leptris/leptris#1712,
6
+ PR #453)**: duck-typed consumers (svg_conform's
7
+ NamespaceRemediation) pass text nodes to `#attributes`; the
8
+ unchecked decode handed C a dangling `attr == node + 1` and the
9
+ first string read segfaulted on svg corpora with adjacent
10
+ `<text>` nodes. Non-element handles now answer the empty
11
+ contract (`{}` / `nil`) across `nn_attributes`, `nn_attribute`,
12
+ `nf_fast_attribute2`, `nf_attribute`, and `nf_bulk_attr_faces`.
13
+ The engine-side public-boundary guard rides libleptris
14
+ 1.9.356 (leptris/leptris PR #1713).
15
+
1
16
  ## [1.9.355.0] - 2026-10-14
2
17
 
3
18
  ### Fixed — engine sync (libleptris 1.9.355)
data/README.adoc CHANGED
@@ -115,16 +115,24 @@ returned object is a `Leptris::XML::Document`.
115
115
 
116
116
  Malformed input raises `Leptris::XML::ParseError`. Parse with
117
117
  `recover: true` to get libxml2's `XML_PARSE_RECOVER` semantics
118
- instead — an empty document back, with the failure recorded on the
119
- thread-global last error:
118
+ instead — a best-effort partial tree back, with the recovered
119
+ diagnostics on `doc.errors` (leptris-ruby#465):
120
120
 
121
121
  [source,ruby]
122
122
  ----
123
- doc = Leptris::XML.parse("<broken", recover: true)
124
- doc.root # => nil
125
- Leptris::XML::FFI.leptris_last_error # => "..."
123
+ doc = Leptris::XML.parse("<a><b></a>", recover: true)
124
+ doc.root.name # => "a"
125
+ doc.errors.first.message # => "..."
126
126
  ----
127
127
 
128
+ **Nokogiri compatibility note (leptris-ruby#465):** nokogiri
129
+ recovers by DEFAULT — `Nokogiri::XML("<a><b></a>")` never raises and
130
+ exposes `doc.errors`. leptris keeps the stricter default (malformed
131
+ input raises; the failure detail is on the raised ParseError) — the
132
+ same information, delivered as an exception instead of silent
133
+ recovery. Migrating consumers that relied on nokogiri's recover-by-
134
+ default should pass `recover: true` or rescue `Leptris::XML::ParseError`.
135
+
128
136
  == HTML parsing
129
137
 
130
138
  `Leptris::XML.parse_html(html)` parses tolerant HTML4/5 into a
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ RSpec::Core::RakeTask.new(:spec)
8
8
  # Pin for `rake compile` and the platform-gem builds. Keep in lockstep
9
9
  # with .github/workflows/build.yml (which calls `rake compile`) and the
10
10
  # CHANGELOG when libleptris releases.
11
- LIBLEPTRIS_VERSION = "1.9.355"
11
+ LIBLEPTRIS_VERSION = "1.9.359"
12
12
  # Vendored alongside libleptris for fn:normalize-unicode (TODO
13
13
  # .restructure/20): built per platform with a RELOCATABLE @rpath
14
14
  # install name, loaded by ffi.rb before libleptris so the
@@ -119,6 +119,34 @@ typedef void *(*parse_str_fn)(const char *, size_t, void *);
119
119
  typedef void *(*parse_frag_fn)(const char *, size_t, void *, int *);
120
120
  typedef const char *(*last_err_fn)(void);
121
121
  typedef const char *(*status_str_fn)(int);
122
+
123
+ /* ==== SAX single transport (leptris-ruby SAX parity round) ====
124
+ * The engine's SAX handler struct — mirrors
125
+ * include/leptris/sax/sax.h field order. */
126
+ struct engine_sax_handler {
127
+ void (*start_document)(void*);
128
+ void (*end_document)(void*);
129
+ void (*start_element)(void*, const char*, const char**);
130
+ void (*end_element)(void*, const char*);
131
+ void (*characters)(void*, const char*, size_t);
132
+ void (*comment)(void*, const char*);
133
+ void (*cdata)(void*, const char*);
134
+ void (*processing_instruction)(void*, const char*, const char*);
135
+ void (*start_prefix_mapping)(void*, const char*, const char*);
136
+ void (*end_prefix_mapping)(void*, const char*);
137
+ void (*error)(void*, const char*, int, int);
138
+ };
139
+ typedef int (*sax_parse_fn)(const char*, size_t, struct engine_sax_handler*, void*);
140
+ typedef void* (*sax_create_fn)(struct engine_sax_handler*, void*);
141
+ typedef int (*sax_feed_fn)(void*, const char*, size_t, int);
142
+ typedef void (*sax_pfree_fn)(void*);
143
+ typedef void (*sax_flags_fn)(void*, unsigned);
144
+ static sax_parse_fn f_sax_parse;
145
+ static sax_create_fn f_sax_create;
146
+ static sax_feed_fn f_sax_feed;
147
+ static sax_pfree_fn f_sax_pfree;
148
+ static sax_flags_fn f_sax_set_flags;
149
+
122
150
  static set_str_fn f_elem_set_name, f_elem_set_text, f_text_set_content;
123
151
  static node_unlink_fn f_node_unlink;
124
152
  static traverse_fn f_node_traverse;
@@ -135,6 +163,7 @@ static set_root_fn f_set_root;
135
163
  static doc_free_fn f_doc_free;
136
164
 
137
165
  static VALUE c_native_node;
166
+ static VALUE c_sax_parser;
138
167
  /* Ivar reads (TODO.perf/07 tail): the binding Document keeps the
139
168
  * C address and the caches in plain ivars, so the hot C faces skip
140
169
  * rb_funcall dispatch entirely. */
@@ -280,6 +309,12 @@ static void resolve_symbols(const char *lib_path)
280
309
  f_status_str = (status_str_fn)lib_sym(h, "leptris_status_string");
281
310
  f_set_root = (set_root_fn)lib_sym(h, "leptris_document_set_root");
282
311
  f_doc_free = (doc_free_fn)lib_sym(h, "leptris_document_free");
312
+ f_sax_parse = (sax_parse_fn)lib_sym(h, "leptris_sax_parse");
313
+ f_sax_create = (sax_create_fn)lib_sym(h, "leptris_sax_parser_create");
314
+ f_sax_feed = (sax_feed_fn)lib_sym(h, "leptris_sax_parser_feed");
315
+ f_sax_pfree = (sax_pfree_fn)lib_sym(h, "leptris_sax_parser_free");
316
+ f_sax_set_flags =
317
+ (sax_flags_fn)lib_sym(h, "leptris_sax_parser_set_skip_flags");
283
318
  if (!f_elem_name || !f_text_content || !f_attr ||
284
319
  !f_children_ex || !f_node_type || !f_next_sibling || !f_parent ||
285
320
  !f_element_text || !f_doc_create || !f_elem_create || !f_text_create ||
@@ -292,7 +327,8 @@ static void resolve_symbols(const char *lib_path)
292
327
  !f_node_traverse || !f_node_visit || !f_free_str ||
293
328
  !f_node_xpath || !f_elem_copy || !f_parse_str ||
294
329
  !f_parse_frag || !f_last_err || !f_status_str ||
295
- !f_doc_free ||
330
+ !f_doc_free || !f_sax_parse || !f_sax_create || !f_sax_feed ||
331
+ !f_sax_pfree || !f_sax_set_flags ||
296
332
  !f_elem_prefix || !f_xp_count || !f_xp_nodes_ex ||
297
333
  !f_xp_node_kind || !f_xp_node_name || !f_xp_node_value ||
298
334
  !f_doc_serialize || !f_elem_serialize || !f_attr_first ||
@@ -355,9 +391,14 @@ static VALUE nn_content(VALUE self)
355
391
  static VALUE nf_fast_attribute2(VALUE self, VALUE addr, VALUE name)
356
392
  {
357
393
  const char *s;
394
+ void *node = (void *)(uintptr_t)NUM2ULL(addr);
395
+
358
396
  (void)self;
359
- s = f_attr((void *)(uintptr_t)NUM2ULL(addr),
360
- RSTRING_PTR(StringValue(name)));
397
+ /* leptris#1712: non-element handles must not reach the attr
398
+ * chain (same dangling-overlay shape as nn_attributes). */
399
+ if (f_node_type(node) != WS_NODE_ELEMENT)
400
+ return Qnil;
401
+ s = f_attr(node, RSTRING_PTR(StringValue(name)));
361
402
  return s ? rb_utf8_str_new_cstr(s) : Qnil;
362
403
  }
363
404
 
@@ -376,6 +417,10 @@ static VALUE nn_attribute(VALUE self, VALUE name)
376
417
  name = rb_obj_as_string(name);
377
418
  TypedData_Get_Struct(self, struct native_node, &nn_type, n);
378
419
  doc = n->document;
420
+ /* leptris#1712: non-element handles must not reach the attr
421
+ * chain (same dangling-overlay shape as nn_attributes). */
422
+ if (f_node_type(n->ptr) != WS_NODE_ELEMENT)
423
+ return Qnil;
379
424
  /* Single-slot memo first: the last-queried name by VALUE
380
425
  * (callers may mint a fresh String per call) against the same
381
426
  * version. Then the hash memo for other repeat names; both
@@ -1413,6 +1458,14 @@ static VALUE nn_attributes(VALUE self)
1413
1458
 
1414
1459
  TypedData_Get_Struct(self, struct native_node, &nn_type, n);
1415
1460
  hash = rb_hash_new();
1461
+ /* leptris#1712: duck-typed consumers pass non-element handles
1462
+ * (svg_conform's remediation walks text nodes with an
1463
+ * element? probe that answers on #name). The element attr
1464
+ * overlay sits 4 bytes past a 32-byte text struct — the next
1465
+ * node's type word — so an unchecked decode hands C a dangling
1466
+ * attr == node + 1 and the first string read segfaults. */
1467
+ if (f_node_type(n->ptr) != WS_NODE_ELEMENT)
1468
+ return hash;
1416
1469
  attr = f_attr_first(n->ptr);
1417
1470
  while (attr) {
1418
1471
  const char *name = f_attr_name(attr);
@@ -1441,6 +1494,30 @@ static VALUE nn_byte_offset(VALUE self)
1441
1494
 
1442
1495
  /* The owning binding Document (moxml #213: adapters route
1443
1496
  * native.document exactly like Node#document). */
1497
+ /* leptris-ruby#460: the last contract face that was Ruby-only.
1498
+ * Element set-text / text set-content with the same freed and
1499
+ * readonly gates as set_binding_text, defined on the C class so
1500
+ * subclasses inherit it at native speed. The version bump (via
1501
+ * document_advance_version) invalidates the content memo. */
1502
+ static VALUE nn_content_set(VALUE self, VALUE content)
1503
+ {
1504
+ struct native_node *n;
1505
+ int st;
1506
+ TypedData_Get_Struct(self, struct native_node, &nn_type, n);
1507
+ if (NIL_P(rb_ivar_get(n->document, id_iv_c_address)))
1508
+ rb_raise(c_use_after_free_error,
1509
+ "owning document has been freed");
1510
+ if (rb_ivar_get(n->document, id_iv_readonly) == Qtrue)
1511
+ rb_raise(c_readonly_error,
1512
+ "document is readonly — mutation attempted");
1513
+ document_advance_version(n->document);
1514
+ st = f_node_type(n->ptr) == NT_ELEMENT
1515
+ ? f_elem_set_text(n->ptr, StringValueCStr(content))
1516
+ : f_text_set_content(n->ptr, StringValueCStr(content));
1517
+ check_status_c(st);
1518
+ return content;
1519
+ }
1520
+
1444
1521
  static VALUE nn_document(VALUE self)
1445
1522
  {
1446
1523
  struct native_node *n;
@@ -1939,10 +2016,14 @@ static VALUE nf_bulk_attr_faces(VALUE self, VALUE addr, VALUE element)
1939
2016
  void *node = (void *)(uintptr_t)NUM2ULL(addr);
1940
2017
  VALUE faces = rb_hash_new();
1941
2018
  VALUE values = rb_hash_new();
1942
- void *attr = f_attr_first(node);
2019
+ void *attr;
1943
2020
 
1944
2021
  (void)self;
2022
+ /* leptris#1712: same non-element guard as nn_attributes. */
2023
+ if (f_node_type(node) != WS_NODE_ELEMENT)
2024
+ return rb_assoc_new(faces, values);
1945
2025
  resolve_binding_classes();
2026
+ attr = f_attr_first(node);
1946
2027
  while (attr) {
1947
2028
  const char *name = f_attr_name(attr);
1948
2029
  const char *value = f_attr_value(node, attr);
@@ -2981,8 +3062,13 @@ static VALUE nf_element_text(VALUE self, VALUE addr)
2981
3062
  static VALUE nf_attribute(VALUE self, VALUE addr, VALUE name)
2982
3063
  {
2983
3064
  const char *s;
3065
+ void *node = (void *)(uintptr_t)NUM2ULL(addr);
3066
+
2984
3067
  (void)self;
2985
- s = f_attr((void *)(uintptr_t)NUM2ULL(addr), StringValueCStr(name));
3068
+ /* leptris#1712: same non-element guard as nn_attributes. */
3069
+ if (f_node_type(node) != WS_NODE_ELEMENT)
3070
+ return Qnil;
3071
+ s = f_attr(node, StringValueCStr(name));
2986
3072
  return s ? rb_utf8_str_new_cstr(s) : Qnil;
2987
3073
  }
2988
3074
 
@@ -3036,6 +3122,275 @@ static VALUE leptris_native_resolve_rb(VALUE self, VALUE lib_path)
3036
3122
  # define LEPTRIS_INIT_EXPORT
3037
3123
  #endif
3038
3124
 
3125
+ /* ==== SAX single transport: C callbacks dispatch to Ruby via
3126
+ * rb_funcall (~100ns/event). Replaces BOTH Ruby-side transports —
3127
+ * the FFI::Function trampolines (~1.3us/event) and the record-
3128
+ * then-drain bulk pass — with one path that is fastest for every
3129
+ * handler shape. Only the kinds the handler overrides are attached
3130
+ * (the engine skips NULL callbacks), preserving the pay-for-what-
3131
+ * you-consume property. */
3132
+
3133
+ enum sax_kind_bits {
3134
+ SK_START_DOC = 1u << 0,
3135
+ SK_END_DOC = 1u << 1,
3136
+ SK_START_1ARG = 1u << 2, /* start_element(name) */
3137
+ SK_START_2ARG = 1u << 3, /* start_element(name, attrs) */
3138
+ SK_END_ELEM = 1u << 4,
3139
+ SK_CHARS = 1u << 5,
3140
+ SK_COMMENT = 1u << 6,
3141
+ SK_CDATA = 1u << 7,
3142
+ SK_PI = 1u << 8,
3143
+ SK_START_PREFIX = 1u << 9,
3144
+ SK_END_PREFIX = 1u << 10,
3145
+ SK_ERROR = 1u << 11
3146
+ };
3147
+
3148
+ static ID id_sax_start_document, id_sax_end_document, id_sax_start_element,
3149
+ id_sax_end_element, id_sax_characters, id_sax_comment, id_sax_cdata,
3150
+ id_sax_pi, id_sax_start_prefix, id_sax_end_prefix, id_sax_error;
3151
+
3152
+ struct sax_ctx {
3153
+ VALUE handler;
3154
+ unsigned kinds;
3155
+ };
3156
+
3157
+ static VALUE sax_utf8_str(const char* s)
3158
+ {
3159
+ return s ? rb_utf8_str_new_cstr(s) : Qnil;
3160
+ }
3161
+
3162
+ /* PI data strips leading whitespace — read_pi_data parity. */
3163
+ static VALUE sax_pi_data_str(const char* s)
3164
+ {
3165
+ if (!s) return Qnil;
3166
+ while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n')
3167
+ s++;
3168
+ return rb_utf8_str_new_cstr(s);
3169
+ }
3170
+
3171
+ static void sax_cb_start_document(void* ud)
3172
+ {
3173
+ struct sax_ctx* c = ud;
3174
+ rb_funcall(c->handler, id_sax_start_document, 0);
3175
+ }
3176
+
3177
+ static void sax_cb_end_document(void* ud)
3178
+ {
3179
+ struct sax_ctx* c = ud;
3180
+ rb_funcall(c->handler, id_sax_end_document, 0);
3181
+ }
3182
+
3183
+ static void sax_cb_start_element(void* ud, const char* name,
3184
+ const char** attrs)
3185
+ {
3186
+ struct sax_ctx* c = ud;
3187
+ VALUE n = rb_utf8_str_new_cstr(name);
3188
+ if (c->kinds & SK_START_1ARG) {
3189
+ rb_funcall(c->handler, id_sax_start_element, 1, n);
3190
+ return;
3191
+ }
3192
+ VALUE pairs = rb_ary_new();
3193
+ if (attrs) {
3194
+ for (size_t i = 0; attrs[i]; i += 2)
3195
+ rb_ary_push(pairs, rb_assoc_new(
3196
+ rb_utf8_str_new_cstr(attrs[i]),
3197
+ rb_utf8_str_new_cstr(attrs[i + 1])));
3198
+ }
3199
+ rb_funcall(c->handler, id_sax_start_element, 2, n, pairs);
3200
+ }
3201
+
3202
+ static void sax_cb_end_element(void* ud, const char* name)
3203
+ {
3204
+ struct sax_ctx* c = ud;
3205
+ rb_funcall(c->handler, id_sax_end_element, 1,
3206
+ rb_utf8_str_new_cstr(name));
3207
+ }
3208
+
3209
+ static void sax_cb_characters(void* ud, const char* text, size_t len)
3210
+ {
3211
+ struct sax_ctx* c = ud;
3212
+ rb_funcall(c->handler, id_sax_characters, 1,
3213
+ rb_utf8_str_new(text, len));
3214
+ }
3215
+
3216
+ static void sax_cb_comment(void* ud, const char* comment)
3217
+ {
3218
+ struct sax_ctx* c = ud;
3219
+ rb_funcall(c->handler, id_sax_comment, 1,
3220
+ rb_utf8_str_new_cstr(comment));
3221
+ }
3222
+
3223
+ static void sax_cb_cdata(void* ud, const char* cdata)
3224
+ {
3225
+ struct sax_ctx* c = ud;
3226
+ rb_funcall(c->handler, id_sax_cdata, 1, rb_utf8_str_new_cstr(cdata));
3227
+ }
3228
+
3229
+ static void sax_cb_pi(void* ud, const char* target, const char* data)
3230
+ {
3231
+ struct sax_ctx* c = ud;
3232
+ rb_funcall(c->handler, id_sax_pi, 2, sax_utf8_str(target),
3233
+ sax_pi_data_str(data));
3234
+ }
3235
+
3236
+ static void sax_cb_start_prefix(void* ud, const char* prefix,
3237
+ const char* uri)
3238
+ {
3239
+ struct sax_ctx* c = ud;
3240
+ rb_funcall(c->handler, id_sax_start_prefix, 2, sax_utf8_str(prefix),
3241
+ sax_utf8_str(uri));
3242
+ }
3243
+
3244
+ static void sax_cb_end_prefix(void* ud, const char* prefix)
3245
+ {
3246
+ struct sax_ctx* c = ud;
3247
+ rb_funcall(c->handler, id_sax_end_prefix, 1, sax_utf8_str(prefix));
3248
+ }
3249
+
3250
+ static void sax_cb_error(void* ud, const char* msg, int line, int col)
3251
+ {
3252
+ struct sax_ctx* c = ud;
3253
+ rb_funcall(c->handler, id_sax_error, 3, rb_utf8_str_new_cstr(msg),
3254
+ INT2NUM(line), INT2NUM(col));
3255
+ }
3256
+
3257
+ static void sax_handler_fill(struct engine_sax_handler* h,
3258
+ struct sax_ctx* c)
3259
+ {
3260
+ memset(h, 0, sizeof(*h));
3261
+ if (c->kinds & SK_START_DOC)
3262
+ h->start_document = sax_cb_start_document;
3263
+ if (c->kinds & SK_END_DOC)
3264
+ h->end_document = sax_cb_end_document;
3265
+ if (c->kinds & (SK_START_1ARG | SK_START_2ARG))
3266
+ h->start_element = sax_cb_start_element;
3267
+ if (c->kinds & SK_END_ELEM)
3268
+ h->end_element = sax_cb_end_element;
3269
+ if (c->kinds & SK_CHARS)
3270
+ h->characters = sax_cb_characters;
3271
+ if (c->kinds & SK_COMMENT)
3272
+ h->comment = sax_cb_comment;
3273
+ if (c->kinds & SK_CDATA)
3274
+ h->cdata = sax_cb_cdata;
3275
+ if (c->kinds & SK_PI)
3276
+ h->processing_instruction = sax_cb_pi;
3277
+ if (c->kinds & SK_START_PREFIX)
3278
+ h->start_prefix_mapping = sax_cb_start_prefix;
3279
+ if (c->kinds & SK_END_PREFIX)
3280
+ h->end_prefix_mapping = sax_cb_end_prefix;
3281
+ if (c->kinds & SK_ERROR)
3282
+ h->error = sax_cb_error;
3283
+ }
3284
+
3285
+ static VALUE nf_sax_parse(VALUE self, VALUE handler, VALUE kinds,
3286
+ VALUE string)
3287
+ {
3288
+ struct engine_sax_handler h;
3289
+ struct sax_ctx c;
3290
+ int rc;
3291
+ (void)self;
3292
+ c.handler = handler;
3293
+ c.kinds = NUM2UINT(kinds);
3294
+ sax_handler_fill(&h, &c);
3295
+ rc = f_sax_parse(RSTRING_PTR(string), RSTRING_LEN(string), &h, &c);
3296
+ return INT2NUM(rc);
3297
+ }
3298
+
3299
+ /* Streaming parser wrapper: the handler VALUE must stay live for
3300
+ * the parser's lifetime, so the ctx rides a TypedData with mark. */
3301
+ struct sax_parser_obj {
3302
+ void* parser;
3303
+ struct engine_sax_handler h;
3304
+ struct sax_ctx c;
3305
+ int skip_flags;
3306
+ int flags_applied;
3307
+ };
3308
+
3309
+ static void sax_parser_mark(void* p)
3310
+ {
3311
+ struct sax_parser_obj* o = p;
3312
+ rb_gc_mark(o->c.handler);
3313
+ }
3314
+
3315
+ static void sax_parser_free(void* p)
3316
+ {
3317
+ struct sax_parser_obj* o = p;
3318
+ if (o->parser)
3319
+ f_sax_pfree(o->parser);
3320
+ xfree(o);
3321
+ }
3322
+
3323
+ static size_t sax_parser_size(const void* p)
3324
+ {
3325
+ return sizeof(struct sax_parser_obj);
3326
+ }
3327
+
3328
+ static const rb_data_type_t sax_parser_type = {
3329
+ "Leptris/XML/Native/SaxParser",
3330
+ {sax_parser_mark, sax_parser_free, sax_parser_size},
3331
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3332
+ };
3333
+
3334
+ static VALUE nf_sax_parser_create(VALUE self, VALUE handler, VALUE kinds)
3335
+ {
3336
+ struct sax_parser_obj* o;
3337
+ VALUE obj;
3338
+ (void)self;
3339
+ o = ALLOC(struct sax_parser_obj);
3340
+ o->c.handler = handler;
3341
+ o->c.kinds = NUM2UINT(kinds);
3342
+ o->skip_flags = 0;
3343
+ o->flags_applied = 0;
3344
+ sax_handler_fill(&o->h, &o->c);
3345
+ o->parser = f_sax_create(&o->h, &o->c);
3346
+ if (!o->parser) {
3347
+ xfree(o);
3348
+ return Qnil;
3349
+ }
3350
+ obj = TypedData_Wrap_Struct(c_sax_parser, &sax_parser_type, o);
3351
+ return obj;
3352
+ }
3353
+
3354
+ static struct sax_parser_obj* sax_parser_unwrap(VALUE obj)
3355
+ {
3356
+ struct sax_parser_obj* o;
3357
+ TypedData_Get_Struct(obj, struct sax_parser_obj, &sax_parser_type, o);
3358
+ return o;
3359
+ }
3360
+
3361
+ static VALUE nf_sax_parser_feed(VALUE self, VALUE obj, VALUE chunk,
3362
+ VALUE is_final)
3363
+ {
3364
+ int rc;
3365
+ (void)self;
3366
+ rc = f_sax_feed(sax_parser_unwrap(obj)->parser, RSTRING_PTR(chunk),
3367
+ RSTRING_LEN(chunk), RTEST(is_final) ? 1 : 0);
3368
+ return INT2NUM(rc);
3369
+ }
3370
+
3371
+ static VALUE nf_sax_parser_set_skip_flags(VALUE self, VALUE obj,
3372
+ VALUE flags)
3373
+ {
3374
+ struct sax_parser_obj* o;
3375
+ (void)self;
3376
+ o = sax_parser_unwrap(obj);
3377
+ o->skip_flags = NUM2UINT(flags);
3378
+ f_sax_set_flags(o->parser, (unsigned)o->skip_flags);
3379
+ return Qnil;
3380
+ }
3381
+
3382
+ static VALUE nf_sax_parser_free(VALUE self, VALUE obj)
3383
+ {
3384
+ struct sax_parser_obj* o;
3385
+ (void)self;
3386
+ o = sax_parser_unwrap(obj);
3387
+ if (o->parser) {
3388
+ f_sax_pfree(o->parser);
3389
+ o->parser = 0;
3390
+ }
3391
+ return Qnil;
3392
+ }
3393
+
3039
3394
  LEPTRIS_INIT_EXPORT void Init_native(void)
3040
3395
  {
3041
3396
  VALUE m_leptris, m_xml, m_native;
@@ -3099,6 +3454,7 @@ LEPTRIS_INIT_EXPORT void Init_native(void)
3099
3454
  rb_define_method(c_native_node, "byte_offset", nn_byte_offset, 0);
3100
3455
  rb_define_method(c_native_node, "name", nn_name, 0);
3101
3456
  rb_define_method(c_native_node, "content", nn_content, 0);
3457
+ rb_define_method(c_native_node, "content=", nn_content_set, 1);
3102
3458
  rb_define_method(c_native_node, "attribute", nn_attribute, 1);
3103
3459
  rb_define_alias(c_native_node, "[]", "attribute");
3104
3460
  /* Unshadowed aliases for consumers whose contract modules shadow
@@ -3117,6 +3473,28 @@ LEPTRIS_INIT_EXPORT void Init_native(void)
3117
3473
  m_native = rb_define_module_under(m_xml, "Native");
3118
3474
  m_native_sentinel = m_native;
3119
3475
  rb_define_module_function(m_native, "fast_name", nf_name, 1);
3476
+ id_sax_start_document = rb_intern("start_document");
3477
+ id_sax_end_document = rb_intern("end_document");
3478
+ id_sax_start_element = rb_intern("start_element");
3479
+ id_sax_end_element = rb_intern("end_element");
3480
+ id_sax_characters = rb_intern("characters");
3481
+ id_sax_comment = rb_intern("comment");
3482
+ id_sax_cdata = rb_intern("cdata_block");
3483
+ id_sax_pi = rb_intern("processing_instruction");
3484
+ id_sax_start_prefix = rb_intern("start_prefix_mapping");
3485
+ id_sax_end_prefix = rb_intern("end_prefix_mapping");
3486
+ id_sax_error = rb_intern("error");
3487
+ c_sax_parser = rb_define_class_under(m_native, "SaxParser",
3488
+ rb_cObject);
3489
+ rb_define_module_function(m_native, "sax_parse", nf_sax_parse, 3);
3490
+ rb_define_module_function(m_native, "sax_parser_create",
3491
+ nf_sax_parser_create, 2);
3492
+ rb_define_module_function(m_native, "sax_parser_feed",
3493
+ nf_sax_parser_feed, 3);
3494
+ rb_define_module_function(m_native, "sax_parser_set_skip_flags",
3495
+ nf_sax_parser_set_skip_flags, 2);
3496
+ rb_define_module_function(m_native, "sax_parser_free",
3497
+ nf_sax_parser_free, 1);
3120
3498
  rb_define_module_function(m_native, "fast_element_text", nf_element_text, 1);
3121
3499
  rb_define_module_function(m_native, "fast_attribute", nf_attribute, 2);
3122
3500
  rb_define_module_function(m_native, "fast_prefix", nf_prefix, 1);
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Leptris
4
- VERSION = "1.9.355.0"
4
+ VERSION = "1.9.359.0"
5
5
  end
@@ -35,4 +35,7 @@ class Leptris::XML::CDATA < Leptris::XML::Text
35
35
  def to_s
36
36
  "<![CDATA[#{content}]]>"
37
37
  end
38
+
39
+ # leptris-ruby#464: CDATA serialization face.
40
+ alias_method :to_xml, :to_s
38
41
  end
@@ -35,4 +35,7 @@ class Leptris::XML::Comment < Leptris::XML::Node
35
35
  def to_s
36
36
  "<!--#{content}-->"
37
37
  end
38
+
39
+ # leptris-ruby#464: comment serialization face.
40
+ alias_method :to_xml, :to_s
38
41
  end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Node-shaped view of the document's XML declaration state
4
+ # (leptris-ruby#459 ask 1 — retires moxml's CustomizedLeptris value
5
+ # object). The engine stores the declaration as document state; this
6
+ # face reads and writes it in place. `remove`/`unlink` clears the
7
+ # state entirely (Document#clear_declaration).
8
+ class Leptris::XML::Declaration
9
+ attr_reader :document
10
+
11
+ def initialize(document)
12
+ @document = document
13
+ end
14
+
15
+ def version
16
+ @document.xml_version
17
+ end
18
+
19
+ def encoding
20
+ @document.xml_encoding
21
+ end
22
+
23
+ def standalone
24
+ @document.xml_standalone
25
+ end
26
+
27
+ def version=(value)
28
+ @document.xml_version = value
29
+ value
30
+ end
31
+
32
+ def encoding=(value)
33
+ @document.xml_encoding = value
34
+ value
35
+ end
36
+
37
+ def standalone=(value)
38
+ @document.xml_standalone = value
39
+ value
40
+ end
41
+
42
+ def remove
43
+ @document.clear_declaration
44
+ self
45
+ end
46
+ alias_method :unlink, :remove
47
+
48
+ def to_s
49
+ parts = ['<?xml version="' + version.to_s + '"']
50
+ parts << 'encoding="' + encoding.to_s + '"' if encoding
51
+ parts << 'standalone="' + (standalone ? "yes" : "no") + '"' unless standalone.nil?
52
+ parts.join(" ") + "?>"
53
+ end
54
+
55
+ def inspect
56
+ "#<#{self.class.name} version=#{version.inspect} " \
57
+ "encoding=#{encoding.inspect} standalone=#{standalone.inspect}>"
58
+ end
59
+ end