rxerces 0.6.1 → 0.7.1

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.
@@ -1,8 +1,10 @@
1
1
  #include "rxerces.h"
2
+ #include <ruby/encoding.h>
2
3
  #include <xercesc/util/PlatformUtils.hpp>
3
4
  #include <xercesc/parsers/XercesDOMParser.hpp>
4
5
  #include <xercesc/dom/DOM.hpp>
5
6
  #include <xercesc/util/XMLString.hpp>
7
+ #include <xercesc/util/XMLUni.hpp>
6
8
  #include <xercesc/framework/MemBufInputSource.hpp>
7
9
  #include <xercesc/framework/MemBufFormatTarget.hpp>
8
10
  #include <xercesc/util/XercesDefs.hpp>
@@ -10,8 +12,13 @@
10
12
  #include <xercesc/dom/DOMXPathExpression.hpp>
11
13
  #include <xercesc/sax/ErrorHandler.hpp>
12
14
  #include <xercesc/sax/SAXParseException.hpp>
15
+ #include <xercesc/sax/SAXException.hpp>
13
16
  #include <sstream>
14
17
  #include <vector>
18
+ #include <mutex>
19
+ #include <list>
20
+ #include <unordered_map>
21
+ #include <memory>
15
22
 
16
23
  #ifdef HAVE_XALAN
17
24
  #include <xalanc/XPath/XPathEvaluator.hpp>
@@ -50,6 +57,64 @@ static bool xerces_initialized = false;
50
57
  #ifdef HAVE_XALAN
51
58
  static bool xalan_initialized = false;
52
59
  #endif
60
+ static std::mutex init_mutex;
61
+
62
+ // XPath validation cache with LRU eviction
63
+ // Uses a list for LRU ordering (front = most recently used)
64
+ // and a map for O(1) lookup of list iterators
65
+ static std::list<std::string>* xpath_cache_lru_list = nullptr;
66
+ static std::unordered_map<std::string, std::list<std::string>::iterator>* xpath_cache_map = nullptr;
67
+ static std::mutex xpath_cache_mutex;
68
+ static bool cache_xpath_validation = true; // Default: enabled
69
+ static size_t xpath_cache_max_size = 10000; // Max cached expressions
70
+ static size_t xpath_max_length = 10000; // Max XPath expression length
71
+
72
+ #ifdef HAVE_XALAN
73
+ // Cached Xalan context per document for XPath performance
74
+ // This avoids recreating expensive Xalan infrastructure on every XPath query
75
+ struct XalanContext {
76
+ XercesParserLiaison* liaison;
77
+ XercesDOMSupport* domSupport;
78
+ XalanDocument* xalanDoc;
79
+ XercesDocumentWrapper* docWrapper;
80
+ XPathEnvSupportDefault* envSupport;
81
+ XObjectFactoryDefault* objectFactory;
82
+ XPathExecutionContextDefault* executionContext;
83
+ XPathConstructionContextDefault* constructionContext;
84
+ XPathFactoryDefault* factory;
85
+ XPathProcessorImpl* processor;
86
+
87
+ XalanContext() : liaison(nullptr), domSupport(nullptr), xalanDoc(nullptr),
88
+ docWrapper(nullptr), envSupport(nullptr), objectFactory(nullptr),
89
+ executionContext(nullptr), constructionContext(nullptr),
90
+ factory(nullptr), processor(nullptr) {}
91
+
92
+ ~XalanContext() {
93
+ // Clean up in reverse order of creation
94
+ delete executionContext;
95
+ delete constructionContext;
96
+ delete objectFactory;
97
+ delete envSupport;
98
+ delete factory;
99
+ delete processor;
100
+ // domSupport must be deleted before liaison
101
+ delete domSupport;
102
+ // liaison owns xalanDoc/docWrapper, so don't delete them separately
103
+ delete liaison;
104
+ }
105
+ };
106
+
107
+ // Compiled XPath expression cache per document
108
+ struct CompiledXPath {
109
+ XPath* xpath;
110
+ std::string expression;
111
+
112
+ CompiledXPath(XPath* x, const std::string& expr) : xpath(x), expression(expr) {}
113
+ };
114
+
115
+ // LRU cache for compiled XPath expressions
116
+ static const size_t XPATH_COMPILE_CACHE_SIZE = 100;
117
+ #endif
53
118
 
54
119
  // Forward declarations
55
120
  static std::string css_to_xpath(const char* css);
@@ -63,6 +128,12 @@ static void ensure_xerces_initialized() {
63
128
  return;
64
129
  }
65
130
 
131
+ std::lock_guard<std::mutex> lock(init_mutex);
132
+
133
+ if (xerces_initialized) {
134
+ return;
135
+ }
136
+
66
137
  try {
67
138
  XMLPlatformUtils::Initialize();
68
139
  #ifdef HAVE_XALAN
@@ -80,6 +151,16 @@ static void ensure_xerces_initialized() {
80
151
 
81
152
  // Cleanup function called at exit
82
153
  static void cleanup_xerces() {
154
+ // Clean up XPath validation cache (LRU)
155
+ if (xpath_cache_lru_list) {
156
+ delete xpath_cache_lru_list;
157
+ xpath_cache_lru_list = nullptr;
158
+ }
159
+ if (xpath_cache_map) {
160
+ delete xpath_cache_map;
161
+ xpath_cache_map = nullptr;
162
+ }
163
+
83
164
  #ifdef HAVE_XALAN
84
165
  if (xalan_initialized) {
85
166
  XPathEvaluator::terminate();
@@ -92,6 +173,167 @@ static void cleanup_xerces() {
92
173
  }
93
174
  }
94
175
 
176
+ // Validate XPath expression to prevent XPath injection attacks
177
+ static void validate_xpath_expression(const char* xpath_str) {
178
+ if (!xpath_str || strlen(xpath_str) == 0) {
179
+ rb_raise(rb_eArgError, "XPath expression cannot be empty");
180
+ }
181
+
182
+ std::string xpath(xpath_str);
183
+
184
+ // Check cache first if caching is enabled (LRU cache)
185
+ if (cache_xpath_validation) {
186
+ std::lock_guard<std::mutex> lock(xpath_cache_mutex);
187
+ if (!xpath_cache_lru_list) {
188
+ xpath_cache_lru_list = new std::list<std::string>();
189
+ }
190
+ if (!xpath_cache_map) {
191
+ xpath_cache_map = new std::unordered_map<std::string, std::list<std::string>::iterator>();
192
+ }
193
+ auto it = xpath_cache_map->find(xpath);
194
+ if (it != xpath_cache_map->end()) {
195
+ // Cache hit: move to front (most recently used)
196
+ xpath_cache_lru_list->splice(xpath_cache_lru_list->begin(), *xpath_cache_lru_list, it->second);
197
+ return; // Already validated
198
+ }
199
+ }
200
+ size_t len = xpath.length();
201
+
202
+ // Check for excessively long XPath expressions (potential DoS)
203
+ if (xpath_max_length > 0 && len > xpath_max_length) {
204
+ rb_raise(rb_eArgError, "XPath expression is too long (max %zu characters)", xpath_max_length);
205
+ }
206
+
207
+ // Check for dangerous patterns that could indicate XPath injection
208
+ // These patterns are commonly used in XPath injection attacks
209
+
210
+ // 1. Check for unbalanced quotes which could break out of string literals
211
+ int single_quotes = 0;
212
+ int double_quotes = 0;
213
+ bool in_single_quote = false;
214
+ bool in_double_quote = false;
215
+
216
+ for (size_t i = 0; i < len; i++) {
217
+ char c = xpath[i];
218
+
219
+ // Track quote state
220
+ if (c == '\'' && !in_double_quote) {
221
+ in_single_quote = !in_single_quote;
222
+ single_quotes++;
223
+ } else if (c == '"' && !in_single_quote) {
224
+ in_double_quote = !in_double_quote;
225
+ double_quotes++;
226
+ }
227
+ }
228
+
229
+ // Unbalanced quotes are suspicious
230
+ if (single_quotes % 2 != 0 || double_quotes % 2 != 0) {
231
+ rb_raise(rb_eArgError, "XPath expression contains unbalanced quotes");
232
+ }
233
+
234
+ // 2. Check for suspicious comment patterns that could be used to bypass validation
235
+ if (xpath.find("(:") != std::string::npos || xpath.find(":)") != std::string::npos) {
236
+ rb_raise(rb_eArgError, "XPath expression contains suspicious comment patterns");
237
+ }
238
+
239
+ // 3. Check for null bytes which could truncate validation
240
+ if (xpath.find('\0') != std::string::npos) {
241
+ rb_raise(rb_eArgError, "XPath expression contains null bytes");
242
+ }
243
+
244
+ // 4. Check for excessive nesting which could cause stack overflow
245
+ int bracket_depth = 0;
246
+ int paren_depth = 0;
247
+ const int MAX_DEPTH = 100;
248
+
249
+ for (size_t i = 0; i < len; i++) {
250
+ char c = xpath[i];
251
+
252
+ if (c == '[') bracket_depth++;
253
+ else if (c == ']') bracket_depth--;
254
+ else if (c == '(') paren_depth++;
255
+ else if (c == ')') paren_depth--;
256
+
257
+ if (bracket_depth > MAX_DEPTH || paren_depth > MAX_DEPTH) {
258
+ rb_raise(rb_eArgError, "XPath expression has excessive nesting depth");
259
+ }
260
+
261
+ if (bracket_depth < 0 || paren_depth < 0) {
262
+ rb_raise(rb_eArgError, "XPath expression has unbalanced brackets or parentheses");
263
+ }
264
+ }
265
+
266
+ if (bracket_depth != 0 || paren_depth != 0) {
267
+ rb_raise(rb_eArgError, "XPath expression has unbalanced brackets or parentheses");
268
+ }
269
+
270
+ // 5. Check for suspicious function calls that could access system functions
271
+ // or perform dangerous operations
272
+ std::vector<std::string> dangerous_patterns = {
273
+ "document(", // Can access external documents
274
+ "doc(", // Can access external documents
275
+ "collection(", // Can access external collections
276
+ "unparsed-text(", // Can read arbitrary files
277
+ "system-property(", // Can leak system information
278
+ "environment-variable(", // Can leak environment variables
279
+ };
280
+
281
+ for (const auto& pattern : dangerous_patterns) {
282
+ if (xpath.find(pattern) != std::string::npos) {
283
+ rb_raise(rb_eArgError, "XPath expression contains potentially dangerous function: %s", pattern.c_str());
284
+ }
285
+ }
286
+
287
+ // 6. Check for encoded characters that could bypass validation
288
+ // Use specific patterns to avoid false positives (e.g., "Q&A" in text)
289
+ if (xpath.find("&#") != std::string::npos || // Numeric character reference (&#60;)
290
+ xpath.find("&#x") != std::string::npos || // Hex character reference (&#x3C;)
291
+ xpath.find("&amp;#") != std::string::npos) { // Encoded entity reference
292
+ rb_raise(rb_eArgError, "XPath expression contains encoded characters");
293
+ }
294
+
295
+ // 7. Detect potential boolean-based blind XPath injection patterns
296
+ // These patterns use 'or' with always-true conditions
297
+ std::vector<std::string> injection_patterns = {
298
+ "or 1=1",
299
+ "or '1'='1'",
300
+ "or \"1\"=\"1\"",
301
+ "or true()",
302
+ "and 1=0",
303
+ "and false()",
304
+ "or 'a'='a'",
305
+ "or \"a\"=\"a\"",
306
+ };
307
+
308
+ // Convert to lowercase for case-insensitive comparison
309
+ std::string xpath_lower = xpath;
310
+ std::transform(xpath_lower.begin(), xpath_lower.end(), xpath_lower.begin(), ::tolower);
311
+
312
+ for (const auto& pattern : injection_patterns) {
313
+ if (xpath_lower.find(pattern) != std::string::npos) {
314
+ rb_raise(rb_eArgError, "XPath expression contains suspicious injection pattern");
315
+ }
316
+ }
317
+
318
+ // Add to cache if caching is enabled (LRU eviction)
319
+ if (cache_xpath_validation) {
320
+ std::lock_guard<std::mutex> lock(xpath_cache_mutex);
321
+ if (xpath_cache_lru_list && xpath_cache_map) {
322
+ // If cache is full, evict least recently used (back of list)
323
+ if (xpath_cache_max_size > 0 && xpath_cache_map->size() >= xpath_cache_max_size) {
324
+ std::string& lru = xpath_cache_lru_list->back();
325
+ xpath_cache_map->erase(lru);
326
+ xpath_cache_lru_list->pop_back();
327
+ }
328
+ // Add new entry to front (most recently used)
329
+ if (xpath_cache_max_size > 0) {
330
+ xpath_cache_lru_list->push_front(xpath);
331
+ (*xpath_cache_map)[xpath] = xpath_cache_lru_list->begin();
332
+ }
333
+ }
334
+ }
335
+ }
336
+
95
337
  // Helper class to manage XMLCh strings
96
338
  class XStr {
97
339
  public:
@@ -134,11 +376,23 @@ private:
134
376
  char* fLocalForm;
135
377
  };
136
378
 
379
+ // DOM objects use release(), rather than delete, for cleanup.
380
+ struct DOMRelease {
381
+ template <typename T> void operator()(T* object) const {
382
+ if (object) object->release();
383
+ }
384
+ };
385
+
137
386
  // Wrapper structure for DOMDocument
138
387
  typedef struct {
139
388
  DOMDocument* doc;
140
389
  XercesDOMParser* parser;
141
390
  std::vector<std::string>* parse_errors;
391
+ #ifdef HAVE_XALAN
392
+ XalanContext* xalan_context; // Cached Xalan context for XPath performance
393
+ std::list<CompiledXPath*>* xpath_cache_list; // LRU list of compiled expressions
394
+ std::unordered_map<std::string, std::list<CompiledXPath*>::iterator>* xpath_cache_map;
395
+ #endif
142
396
  } DocumentWrapper;
143
397
 
144
398
  // Wrapper structure for DOMNode
@@ -253,6 +507,25 @@ public:
253
507
  static void document_free(void* ptr) {
254
508
  DocumentWrapper* wrapper = (DocumentWrapper*)ptr;
255
509
  if (wrapper) {
510
+ #ifdef HAVE_XALAN
511
+ // Clean up XPath cache first
512
+ if (wrapper->xpath_cache_list) {
513
+ for (auto& compiled : *wrapper->xpath_cache_list) {
514
+ if (compiled && compiled->xpath && wrapper->xalan_context && wrapper->xalan_context->factory) {
515
+ wrapper->xalan_context->factory->returnObject(compiled->xpath);
516
+ }
517
+ delete compiled;
518
+ }
519
+ delete wrapper->xpath_cache_list;
520
+ }
521
+ if (wrapper->xpath_cache_map) {
522
+ delete wrapper->xpath_cache_map;
523
+ }
524
+ // Clean up Xalan context
525
+ if (wrapper->xalan_context) {
526
+ delete wrapper->xalan_context;
527
+ }
528
+ #endif
256
529
  if (wrapper->parser) {
257
530
  delete wrapper->parser;
258
531
  }
@@ -353,35 +626,110 @@ static VALUE wrap_node(DOMNode* node, VALUE doc_ref) {
353
626
  return Qnil;
354
627
  }
355
628
 
356
- NodeWrapper* wrapper = ALLOC(NodeWrapper);
357
- wrapper->node = node;
358
- wrapper->doc_ref = doc_ref;
359
-
360
- VALUE rb_node;
361
-
629
+ VALUE rb_class;
362
630
  switch (node->getNodeType()) {
363
631
  case DOMNode::ELEMENT_NODE:
364
- rb_node = TypedData_Wrap_Struct(rb_cElement, &node_type, wrapper);
632
+ rb_class = rb_cElement;
365
633
  break;
366
634
  case DOMNode::TEXT_NODE:
367
- rb_node = TypedData_Wrap_Struct(rb_cText, &node_type, wrapper);
635
+ rb_class = rb_cText;
368
636
  break;
369
637
  default:
370
- rb_node = TypedData_Wrap_Struct(rb_cNode, &node_type, wrapper);
638
+ rb_class = rb_cNode;
371
639
  break;
372
640
  }
373
641
 
642
+ VALUE rb_node = TypedData_Wrap_Struct(rb_class, &node_type, NULL);
643
+ NodeWrapper* wrapper = ALLOC(NodeWrapper);
644
+ wrapper->node = node;
645
+ wrapper->doc_ref = doc_ref;
646
+ DATA_PTR(rb_node) = wrapper;
647
+
374
648
  return rb_node;
375
649
  }
376
650
 
377
- // RXerces::XML::Document.parse(string)
378
- static VALUE document_parse(VALUE klass, VALUE str) {
651
+ // RXerces::XML::Document.parse(string, options = {})
652
+ // Validate options hash for document_parse - only allow known keys
653
+ static void validate_parse_options(VALUE options) {
654
+ if (NIL_P(options)) {
655
+ return;
656
+ }
657
+
658
+ Check_Type(options, T_HASH);
659
+
660
+ // Define allowed option keys
661
+ std::vector<const char*> allowed_keys = {
662
+ "allow_external_entities"
663
+ };
664
+
665
+ // Get all keys from the provided options hash
666
+ VALUE keys = rb_funcall(options, rb_intern("keys"), 0);
667
+ long keys_len = RARRAY_LEN(keys);
668
+
669
+ // Check each key against the allowed list
670
+ for (long i = 0; i < keys_len; i++) {
671
+ VALUE key = rb_ary_entry(keys, i);
672
+
673
+ // Convert symbol or string key to string for comparison
674
+ VALUE key_str;
675
+ if (TYPE(key) == T_SYMBOL) {
676
+ key_str = rb_sym_to_s(key);
677
+ } else if (TYPE(key) == T_STRING) {
678
+ key_str = key;
679
+ } else {
680
+ rb_raise(rb_eArgError, "Option keys must be symbols or strings");
681
+ }
682
+
683
+ const char* key_cstr = StringValueCStr(key_str);
684
+ bool found = false;
685
+
686
+ for (const auto& allowed : allowed_keys) {
687
+ if (strcmp(key_cstr, allowed) == 0) {
688
+ found = true;
689
+ break;
690
+ }
691
+ }
692
+
693
+ if (!found) {
694
+ rb_raise(rb_eArgError, "Unknown option: %s. Allowed options are: allow_external_entities", key_cstr);
695
+ }
696
+ }
697
+ }
698
+
699
+ static VALUE document_parse(int argc, VALUE* argv, VALUE klass) {
700
+ VALUE str, options;
701
+ rb_scan_args(argc, argv, "11", &str, &options);
702
+
379
703
  ensure_xerces_initialized();
380
704
 
381
705
  Check_Type(str, T_STRING);
382
706
  const char* xml_str = StringValueCStr(str);
383
707
 
708
+ // Validate options hash before processing
709
+ validate_parse_options(options);
710
+
384
711
  XercesDOMParser* parser = new XercesDOMParser();
712
+
713
+ // Check if external entities should be allowed (default: false for security)
714
+ bool allow_external = false;
715
+ if (!NIL_P(options)) {
716
+ VALUE allow_key = rb_intern("allow_external_entities");
717
+ VALUE allow_val = rb_hash_aref(options, ID2SYM(allow_key));
718
+ if (RTEST(allow_val)) {
719
+ allow_external = true;
720
+ }
721
+ }
722
+
723
+ if (allow_external) {
724
+ // Allow external entities (less secure)
725
+ parser->setLoadExternalDTD(true);
726
+ parser->setDisableDefaultEntityResolution(false);
727
+ } else {
728
+ // Security: Disable external entity processing to prevent XXE attacks
729
+ parser->setLoadExternalDTD(false);
730
+ parser->setDisableDefaultEntityResolution(true);
731
+ }
732
+
385
733
  parser->setValidationScheme(XercesDOMParser::Val_Never);
386
734
  parser->setDoNamespaces(true);
387
735
  parser->setDoSchema(false);
@@ -392,7 +740,10 @@ static VALUE document_parse(VALUE klass, VALUE str) {
392
740
  parser->setErrorHandler(&error_handler);
393
741
 
394
742
  try {
395
- MemBufInputSource input((const XMLByte*)xml_str, strlen(xml_str), "memory");
743
+ MemBufInputSource input((const XMLByte*)xml_str, RSTRING_LEN(str), "memory");
744
+ // Parsing is synchronous and holds the GVL, so the Ruby string stays
745
+ // valid until the stream is finished. Avoid copying the entire input.
746
+ input.setCopyBufToStream(false);
396
747
  parser->parse(input);
397
748
 
398
749
  DOMDocument* doc = parser->getDocument();
@@ -401,6 +752,11 @@ static VALUE document_parse(VALUE klass, VALUE str) {
401
752
  wrapper->doc = doc;
402
753
  wrapper->parser = parser;
403
754
  wrapper->parse_errors = parse_errors;
755
+ #ifdef HAVE_XALAN
756
+ wrapper->xalan_context = nullptr; // Lazily initialized on first XPath query
757
+ wrapper->xpath_cache_list = nullptr;
758
+ wrapper->xpath_cache_map = nullptr;
759
+ #endif
404
760
 
405
761
  VALUE rb_doc = TypedData_Wrap_Struct(rb_cDocument, &document_type, wrapper);
406
762
 
@@ -474,19 +830,30 @@ static VALUE document_to_s(VALUE self) {
474
830
 
475
831
  try {
476
832
  DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(XStr("LS").unicodeForm());
477
- DOMLSSerializer* serializer = ((DOMImplementationLS*)impl)->createLSSerializer();
478
-
479
- XMLCh* xml_str = serializer->writeToString(wrapper->doc);
480
- CharStr utf8_str(xml_str);
481
-
482
- VALUE result = rb_str_new_cstr(utf8_str.localForm());
483
-
484
- XMLString::release(&xml_str);
485
- serializer->release();
833
+ MemBufFormatTarget target;
834
+ std::unique_ptr<DOMLSSerializer, DOMRelease> serializer(
835
+ ((DOMImplementationLS*)impl)->createLSSerializer());
836
+ std::unique_ptr<DOMLSOutput, DOMRelease> output(
837
+ ((DOMImplementationLS*)impl)->createLSOutput());
838
+ output->setEncoding(XStr("UTF-8").unicodeForm());
839
+ output->setByteStream(&target);
486
840
 
487
- return result;
841
+ // Write UTF-8 bytes directly instead of allocating a UTF-16 document
842
+ // string, transcoding it back to bytes, and scanning for its length.
843
+ serializer->write(wrapper->doc, output.get());
844
+ output.reset();
845
+ serializer.reset();
846
+ return rb_utf8_str_new((const char*)target.getRawBuffer(), target.getLen());
847
+ } catch (const DOMException& e) {
848
+ CharStr message(e.getMessage());
849
+ rb_raise(rb_eRuntimeError, "Failed to serialize document: %s", message.localForm());
850
+ } catch (const XMLException& e) {
851
+ CharStr message(e.getMessage());
852
+ rb_raise(rb_eRuntimeError, "Failed to serialize document (XMLException): %s", message.localForm());
853
+ } catch (const std::exception& e) {
854
+ rb_raise(rb_eRuntimeError, "Failed to serialize document (std::exception): %s", e.what());
488
855
  } catch (...) {
489
- rb_raise(rb_eRuntimeError, "Failed to serialize document");
856
+ rb_raise(rb_eRuntimeError, "Failed to serialize document (unknown exception type)");
490
857
  }
491
858
 
492
859
  return Qnil;
@@ -613,17 +980,17 @@ static VALUE document_children(VALUE self) {
613
980
  DocumentWrapper* wrapper;
614
981
  TypedData_Get_Struct(self, DocumentWrapper, &document_type, wrapper);
615
982
 
616
- VALUE children = rb_ary_new();
617
-
618
983
  if (!wrapper->doc) {
619
- return children;
984
+ return rb_ary_new();
620
985
  }
621
986
 
622
987
  DOMNodeList* child_nodes = wrapper->doc->getChildNodes();
623
988
  XMLSize_t count = child_nodes->getLength();
624
989
 
625
- for (XMLSize_t i = 0; i < count; i++) {
626
- DOMNode* child = child_nodes->item(i);
990
+ // Pre-size array for better performance
991
+ VALUE children = rb_ary_new_capa((long)count);
992
+
993
+ for (DOMNode* child = wrapper->doc->getFirstChild(); child; child = child->getNextSibling()) {
627
994
  rb_ary_push(children, wrap_node(child, self));
628
995
  }
629
996
 
@@ -635,17 +1002,17 @@ static VALUE document_element_children(VALUE self) {
635
1002
  DocumentWrapper* wrapper;
636
1003
  TypedData_Get_Struct(self, DocumentWrapper, &document_type, wrapper);
637
1004
 
638
- VALUE children = rb_ary_new();
639
-
640
1005
  if (!wrapper->doc) {
641
- return children;
1006
+ return rb_ary_new();
642
1007
  }
643
1008
 
644
1009
  DOMNodeList* child_nodes = wrapper->doc->getChildNodes();
645
1010
  XMLSize_t count = child_nodes->getLength();
646
1011
 
647
- for (XMLSize_t i = 0; i < count; i++) {
648
- DOMNode* child = child_nodes->item(i);
1012
+ // Pre-size array (may be smaller than count, but good estimate)
1013
+ VALUE children = rb_ary_new_capa((long)count);
1014
+
1015
+ for (DOMNode* child = wrapper->doc->getFirstChild(); child; child = child->getNextSibling()) {
649
1016
  if (child->getNodeType() == DOMNode::ELEMENT_NODE) {
650
1017
  rb_ary_push(children, wrap_node(child, self));
651
1018
  }
@@ -654,6 +1021,18 @@ static VALUE document_element_children(VALUE self) {
654
1021
  return children;
655
1022
  }
656
1023
 
1024
+ // Shared endpoint traversal for document and node element access.
1025
+ static VALUE element_child(DOMNode* parent, VALUE doc_ref, bool last) {
1026
+ // Walk from the requested end without counting all siblings first.
1027
+ for (DOMNode* child = last ? parent->getLastChild() : parent->getFirstChild();
1028
+ child; child = last ? child->getPreviousSibling() : child->getNextSibling()) {
1029
+ if (child->getNodeType() == DOMNode::ELEMENT_NODE) {
1030
+ return wrap_node(child, doc_ref);
1031
+ }
1032
+ }
1033
+ return Qnil;
1034
+ }
1035
+
657
1036
  // document.first_element_child - returns first element child
658
1037
  static VALUE document_first_element_child(VALUE self) {
659
1038
  DocumentWrapper* wrapper;
@@ -663,17 +1042,7 @@ static VALUE document_first_element_child(VALUE self) {
663
1042
  return Qnil;
664
1043
  }
665
1044
 
666
- DOMNodeList* child_nodes = wrapper->doc->getChildNodes();
667
- XMLSize_t count = child_nodes->getLength();
668
-
669
- for (XMLSize_t i = 0; i < count; i++) {
670
- DOMNode* child = child_nodes->item(i);
671
- if (child->getNodeType() == DOMNode::ELEMENT_NODE) {
672
- return wrap_node(child, self);
673
- }
674
- }
675
-
676
- return Qnil;
1045
+ return element_child(wrapper->doc, self, false);
677
1046
  }
678
1047
 
679
1048
  // document.last_element_child - returns last element child
@@ -685,74 +1054,134 @@ static VALUE document_last_element_child(VALUE self) {
685
1054
  return Qnil;
686
1055
  }
687
1056
 
688
- DOMNodeList* child_nodes = wrapper->doc->getChildNodes();
689
- XMLSize_t count = child_nodes->getLength();
1057
+ return element_child(wrapper->doc, self, true);
1058
+ }
690
1059
 
691
- // Search backwards for last element
692
- for (XMLSize_t i = count; i > 0; i--) {
693
- DOMNode* child = child_nodes->item(i - 1);
694
- if (child->getNodeType() == DOMNode::ELEMENT_NODE) {
695
- return wrap_node(child, self);
1060
+ #ifdef HAVE_XALAN
1061
+ // Helper to initialize or get cached Xalan context for a document
1062
+ static XalanContext* get_or_create_xalan_context(DocumentWrapper* doc_wrapper) {
1063
+ if (doc_wrapper->xalan_context) {
1064
+ return doc_wrapper->xalan_context;
1065
+ }
1066
+
1067
+ // Create new context
1068
+ XalanContext* ctx = new XalanContext();
1069
+
1070
+ try {
1071
+ ctx->liaison = new XercesParserLiaison();
1072
+ ctx->domSupport = new XercesDOMSupport(*ctx->liaison);
1073
+
1074
+ // Create Xalan document wrapper - this is owned by liaison
1075
+ ctx->xalanDoc = ctx->liaison->createDocument(doc_wrapper->doc, false, false, false);
1076
+ if (!ctx->xalanDoc) {
1077
+ delete ctx;
1078
+ return nullptr;
696
1079
  }
1080
+ ctx->docWrapper = static_cast<XercesDocumentWrapper*>(ctx->xalanDoc);
1081
+
1082
+ // Create XPath infrastructure
1083
+ ctx->envSupport = new XPathEnvSupportDefault();
1084
+ ctx->objectFactory = new XObjectFactoryDefault();
1085
+ ctx->executionContext = new XPathExecutionContextDefault(*ctx->envSupport, *ctx->domSupport, *ctx->objectFactory);
1086
+ ctx->constructionContext = new XPathConstructionContextDefault();
1087
+ ctx->factory = new XPathFactoryDefault();
1088
+ ctx->processor = new XPathProcessorImpl();
1089
+
1090
+ doc_wrapper->xalan_context = ctx;
1091
+
1092
+ // Initialize XPath expression cache
1093
+ doc_wrapper->xpath_cache_list = new std::list<CompiledXPath*>();
1094
+ doc_wrapper->xpath_cache_map = new std::unordered_map<std::string, std::list<CompiledXPath*>::iterator>();
1095
+
1096
+ return ctx;
1097
+ } catch (...) {
1098
+ delete ctx;
1099
+ return nullptr;
697
1100
  }
1101
+ }
698
1102
 
699
- return Qnil;
1103
+ // Get or compile XPath expression with LRU caching
1104
+ static XPath* get_or_compile_xpath(DocumentWrapper* doc_wrapper, XalanContext* ctx, const char* xpath_str) {
1105
+ std::string expr(xpath_str);
1106
+
1107
+ // Check cache
1108
+ auto it = doc_wrapper->xpath_cache_map->find(expr);
1109
+ if (it != doc_wrapper->xpath_cache_map->end()) {
1110
+ // Splicing preserves the iterator and avoids allocating a list entry.
1111
+ doc_wrapper->xpath_cache_list->splice(doc_wrapper->xpath_cache_list->begin(),
1112
+ *doc_wrapper->xpath_cache_list, it->second);
1113
+ return (*(it->second))->xpath;
1114
+ }
1115
+
1116
+ // Cache miss - compile new XPath
1117
+ XPath* xpath = ctx->factory->create();
1118
+
1119
+ // Get document element for resolver (can be null, that's ok)
1120
+ XalanElement* docElem = ctx->docWrapper->getDocumentElement();
1121
+ ElementPrefixResolverProxy resolver(docElem, *ctx->envSupport, *ctx->domSupport);
1122
+ ctx->processor->initXPath(*xpath, *ctx->constructionContext, XalanDOMString(xpath_str), resolver);
1123
+
1124
+ // Add to cache
1125
+ CompiledXPath* compiled = new CompiledXPath(xpath, expr);
1126
+ doc_wrapper->xpath_cache_list->push_front(compiled);
1127
+ (*doc_wrapper->xpath_cache_map)[expr] = doc_wrapper->xpath_cache_list->begin();
1128
+
1129
+ // Evict if cache is too large
1130
+ if (doc_wrapper->xpath_cache_list->size() > XPATH_COMPILE_CACHE_SIZE) {
1131
+ CompiledXPath* lru = doc_wrapper->xpath_cache_list->back();
1132
+ doc_wrapper->xpath_cache_map->erase(lru->expression);
1133
+ ctx->factory->returnObject(lru->xpath);
1134
+ delete lru;
1135
+ doc_wrapper->xpath_cache_list->pop_back();
1136
+ }
1137
+
1138
+ return xpath;
700
1139
  }
701
1140
 
702
- #ifdef HAVE_XALAN
703
1141
  // Helper function to execute XPath using Xalan for full XPath 1.0 support
1142
+ // Uses cached Xalan context and compiled XPath expressions for performance
704
1143
  static VALUE execute_xpath_with_xalan(DOMNode* context_node, const char* xpath_str, VALUE doc_ref) {
1144
+ // Validate XPath expression before execution
1145
+ validate_xpath_expression(xpath_str);
1146
+
705
1147
  ensure_xerces_initialized();
706
1148
 
707
1149
  try {
708
- // Get the document
709
- DOMDocument* domDoc = context_node->getOwnerDocument();
710
- if (!domDoc && context_node->getNodeType() == DOMNode::DOCUMENT_NODE) {
711
- domDoc = static_cast<DOMDocument*>(context_node);
712
- }
1150
+ // Get the document wrapper
1151
+ DocumentWrapper* doc_wrapper;
1152
+ TypedData_Get_Struct(doc_ref, DocumentWrapper, &document_type, doc_wrapper);
713
1153
 
1154
+ DOMDocument* domDoc = doc_wrapper->doc;
714
1155
  if (!domDoc) {
715
1156
  NodeSetWrapper* wrapper = ALLOC(NodeSetWrapper);
716
1157
  wrapper->nodes_array = rb_ary_new();
717
1158
  return TypedData_Wrap_Struct(rb_cNodeSet, &nodeset_type, wrapper);
718
1159
  }
719
1160
 
720
- // Create Xalan support objects
721
- XercesParserLiaison liaison;
722
- XercesDOMSupport domSupport(liaison);
723
-
724
- // Create Xalan document - this creates and returns a XercesDocumentWrapper
725
- XalanDocument* xalanDoc = liaison.createDocument(domDoc, false, false, false);
726
- if (!xalanDoc) {
727
- rb_raise(rb_eRuntimeError, "Failed to create Xalan document wrapper");
1161
+ // Get or create cached Xalan context
1162
+ XalanContext* ctx = get_or_create_xalan_context(doc_wrapper);
1163
+ if (!ctx) {
1164
+ rb_raise(rb_eRuntimeError, "Failed to create Xalan context");
728
1165
  }
729
1166
 
730
- // The document IS the wrapper
731
- XercesDocumentWrapper* docWrapper = static_cast<XercesDocumentWrapper*>(xalanDoc);
732
-
733
1167
  // Map the context node to Xalan
734
- XalanNode* xalanContextNode = docWrapper->mapNode(context_node);
1168
+ XalanNode* xalanContextNode = ctx->docWrapper->mapNode(context_node);
735
1169
  if (!xalanContextNode) {
736
- xalanContextNode = docWrapper;
1170
+ xalanContextNode = ctx->docWrapper;
737
1171
  }
738
1172
 
739
- // Set up XPath factories and contexts
740
- XPathEnvSupportDefault envSupport;
741
- XObjectFactoryDefault objectFactory;
742
- XPathExecutionContextDefault executionContext(envSupport, domSupport, objectFactory);
743
- XPathConstructionContextDefault constructionContext;
744
- XPathFactoryDefault factory;
1173
+ // Get or compile XPath expression (cached)
1174
+ XPath* xpath = get_or_compile_xpath(doc_wrapper, ctx, xpath_str);
745
1175
 
746
- // Create XPath
747
- XPathProcessorImpl processor;
748
- XPath* xpath = factory.create();
1176
+ // Create resolver for execution
1177
+ XalanElement* docElem = ctx->docWrapper->getDocumentElement();
1178
+ ElementPrefixResolverProxy resolver(docElem, *ctx->envSupport, *ctx->domSupport);
749
1179
 
750
- // Compile XPath expression
751
- ElementPrefixResolverProxy resolver(docWrapper->getDocumentElement(), envSupport, domSupport);
752
- processor.initXPath(*xpath, constructionContext, XalanDOMString(xpath_str), resolver);
1180
+ // Reset execution context for clean state
1181
+ ctx->objectFactory->reset();
753
1182
 
754
1183
  // Execute XPath query
755
- const XObjectPtr result = xpath->execute(xalanContextNode, resolver, executionContext);
1184
+ const XObjectPtr result = xpath->execute(xalanContextNode, resolver, *ctx->executionContext);
756
1185
 
757
1186
  VALUE nodes_array = rb_ary_new();
758
1187
 
@@ -765,7 +1194,7 @@ static VALUE execute_xpath_with_xalan(DOMNode* context_node, const char* xpath_s
765
1194
  XalanNode* xalanNode = nodeList.item(i);
766
1195
  if (xalanNode) {
767
1196
  // Map back to Xerces DOM node
768
- const DOMNode* domNode = docWrapper->mapNode(xalanNode);
1197
+ const DOMNode* domNode = ctx->docWrapper->mapNode(xalanNode);
769
1198
  if (domNode) {
770
1199
  rb_ary_push(nodes_array, wrap_node(const_cast<DOMNode*>(domNode), doc_ref));
771
1200
  }
@@ -773,7 +1202,7 @@ static VALUE execute_xpath_with_xalan(DOMNode* context_node, const char* xpath_s
773
1202
  }
774
1203
  }
775
1204
 
776
- factory.returnObject(xpath);
1205
+ // Don't return xpath to factory - it's cached!
777
1206
 
778
1207
  NodeSetWrapper* wrapper = ALLOC(NodeSetWrapper);
779
1208
  wrapper->nodes_array = nodes_array;
@@ -793,6 +1222,118 @@ static VALUE execute_xpath_with_xalan(DOMNode* context_node, const char* xpath_s
793
1222
  wrapper->nodes_array = rb_ary_new();
794
1223
  return TypedData_Wrap_Struct(rb_cNodeSet, &nodeset_type, wrapper);
795
1224
  }
1225
+
1226
+ // Optimized version that only returns the first matching node (for at_xpath)
1227
+ // Avoids creating Ruby wrappers for all nodes when we only need one
1228
+ static VALUE execute_xpath_with_xalan_first(DOMNode* context_node, const char* xpath_str, VALUE doc_ref) {
1229
+ // Validate XPath expression before execution
1230
+ validate_xpath_expression(xpath_str);
1231
+
1232
+ ensure_xerces_initialized();
1233
+
1234
+ try {
1235
+ // Get the document wrapper
1236
+ DocumentWrapper* doc_wrapper;
1237
+ TypedData_Get_Struct(doc_ref, DocumentWrapper, &document_type, doc_wrapper);
1238
+
1239
+ DOMDocument* domDoc = doc_wrapper->doc;
1240
+ if (!domDoc) {
1241
+ return Qnil;
1242
+ }
1243
+
1244
+ // Get or create cached Xalan context
1245
+ XalanContext* ctx = get_or_create_xalan_context(doc_wrapper);
1246
+ if (!ctx) {
1247
+ rb_raise(rb_eRuntimeError, "Failed to create Xalan context");
1248
+ }
1249
+
1250
+ // Map the context node to Xalan
1251
+ XalanNode* xalanContextNode = ctx->docWrapper->mapNode(context_node);
1252
+ if (!xalanContextNode) {
1253
+ xalanContextNode = ctx->docWrapper;
1254
+ }
1255
+
1256
+ // Get or compile XPath expression (cached)
1257
+ XPath* xpath = get_or_compile_xpath(doc_wrapper, ctx, xpath_str);
1258
+
1259
+ // Create resolver for execution
1260
+ XalanElement* docElem = ctx->docWrapper->getDocumentElement();
1261
+ ElementPrefixResolverProxy resolver(docElem, *ctx->envSupport, *ctx->domSupport);
1262
+
1263
+ // Reset execution context for clean state
1264
+ ctx->objectFactory->reset();
1265
+
1266
+ // Execute XPath query
1267
+ const XObjectPtr result = xpath->execute(xalanContextNode, resolver, *ctx->executionContext);
1268
+
1269
+ if (result.get() != 0) {
1270
+ // Check if result is a node set
1271
+ const NodeRefListBase& nodeList = result->nodeset();
1272
+ if (nodeList.getLength() > 0) {
1273
+ XalanNode* xalanNode = nodeList.item(0);
1274
+ if (xalanNode) {
1275
+ // Map back to Xerces DOM node - only wrap the first one!
1276
+ const DOMNode* domNode = ctx->docWrapper->mapNode(xalanNode);
1277
+ if (domNode) {
1278
+ return wrap_node(const_cast<DOMNode*>(domNode), doc_ref);
1279
+ }
1280
+ }
1281
+ }
1282
+ }
1283
+
1284
+ return Qnil;
1285
+
1286
+ } catch (const XalanXPathException& e) {
1287
+ CharStr msg(e.getMessage().c_str());
1288
+ rb_raise(rb_eRuntimeError, "XPath error: %s", msg.localForm());
1289
+ } catch (const XMLException& e) {
1290
+ CharStr message(e.getMessage());
1291
+ rb_raise(rb_eRuntimeError, "XML error: %s", message.localForm());
1292
+ } catch (...) {
1293
+ rb_raise(rb_eRuntimeError, "Unknown XPath error");
1294
+ }
1295
+
1296
+ return Qnil;
1297
+ }
1298
+ #endif
1299
+
1300
+ #ifndef HAVE_XALAN
1301
+ // Keep the snapshot result semantics of xpath(), but allocate only one Ruby
1302
+ // node for first-match queries. Xerces' XPath subset need not support every
1303
+ // result type exposed by the DOM XPath API.
1304
+ static VALUE execute_xpath_with_xerces_first(DOMNode* context_node, const char* xpath_str, VALUE doc_ref) {
1305
+ if (!context_node) return Qnil;
1306
+
1307
+ DocumentWrapper* wrapper;
1308
+ TypedData_Get_Struct(doc_ref, DocumentWrapper, &document_type, wrapper);
1309
+ try {
1310
+ std::unique_ptr<DOMXPathNSResolver, DOMRelease> resolver(
1311
+ wrapper->doc->createNSResolver(context_node));
1312
+ XStr path(xpath_str);
1313
+ std::unique_ptr<DOMXPathExpression, DOMRelease> expression(
1314
+ wrapper->doc->createExpression(path.unicodeForm(), resolver.get()));
1315
+ std::unique_ptr<DOMXPathResult, DOMRelease> result(expression->evaluate(
1316
+ context_node, DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE, NULL));
1317
+
1318
+ DOMNode* node = nullptr;
1319
+ if (result->getSnapshotLength() > 0 && result->snapshotItem(0)) {
1320
+ node = result->getNodeValue();
1321
+ }
1322
+ result.reset();
1323
+ expression.reset();
1324
+ resolver.reset();
1325
+ return wrap_node(node, doc_ref);
1326
+ } catch (const DOMXPathException& e) {
1327
+ CharStr message(e.getMessage());
1328
+ rb_raise(rb_eRuntimeError, "XPath error: %s", message.localForm());
1329
+ } catch (const DOMException& e) {
1330
+ CharStr message(e.getMessage());
1331
+ rb_raise(rb_eRuntimeError, "DOM error: %s", message.localForm());
1332
+ } catch (...) {
1333
+ rb_raise(rb_eRuntimeError, "Unknown XPath error");
1334
+ }
1335
+ return Qnil;
1336
+ }
796
1337
  #endif
797
1338
 
798
1339
  // document.xpath(path)
@@ -809,6 +1350,10 @@ static VALUE document_xpath(VALUE self, VALUE path) {
809
1350
  Check_Type(path, T_STRING);
810
1351
  const char* xpath_str = StringValueCStr(path);
811
1352
 
1353
+ #ifndef HAVE_XALAN
1354
+ validate_xpath_expression(xpath_str);
1355
+ #endif
1356
+
812
1357
  #ifdef HAVE_XALAN
813
1358
  // Use Xalan for full XPath 1.0 support
814
1359
  DOMElement* root = doc_wrapper->doc->getDocumentElement();
@@ -875,15 +1420,30 @@ static VALUE document_xpath(VALUE self, VALUE path) {
875
1420
 
876
1421
  // document.at_xpath(path) - returns first matching node or nil
877
1422
  static VALUE document_at_xpath(VALUE self, VALUE path) {
878
- VALUE nodeset = document_xpath(self, path);
879
- NodeSetWrapper* wrapper;
880
- TypedData_Get_Struct(nodeset, NodeSetWrapper, &nodeset_type, wrapper);
1423
+ DocumentWrapper* doc_wrapper;
1424
+ TypedData_Get_Struct(self, DocumentWrapper, &document_type, doc_wrapper);
881
1425
 
882
- if (RARRAY_LEN(wrapper->nodes_array) == 0) {
1426
+ if (!doc_wrapper->doc) {
883
1427
  return Qnil;
884
1428
  }
885
1429
 
886
- return rb_ary_entry(wrapper->nodes_array, 0);
1430
+ Check_Type(path, T_STRING);
1431
+ const char* xpath_str = StringValueCStr(path);
1432
+
1433
+ #ifndef HAVE_XALAN
1434
+ validate_xpath_expression(xpath_str);
1435
+ #endif
1436
+
1437
+ #ifdef HAVE_XALAN
1438
+ // Use optimized first-only version
1439
+ DOMElement* root = doc_wrapper->doc->getDocumentElement();
1440
+ if (!root) {
1441
+ return Qnil;
1442
+ }
1443
+ return execute_xpath_with_xalan_first(root, xpath_str, self);
1444
+ #else
1445
+ return execute_xpath_with_xerces_first(doc_wrapper->doc->getDocumentElement(), xpath_str, self);
1446
+ #endif
887
1447
  }
888
1448
 
889
1449
  // document.css(selector) - Convert CSS to XPath and execute
@@ -900,16 +1460,29 @@ static VALUE document_css(VALUE self, VALUE selector) {
900
1460
 
901
1461
  // document.at_css(selector) - Returns first matching node
902
1462
  static VALUE document_at_css(VALUE self, VALUE selector) {
903
- VALUE nodeset = document_css(self, selector);
1463
+ Check_Type(selector, T_STRING);
1464
+ const char* css_str = StringValueCStr(selector);
904
1465
 
905
- NodeSetWrapper* wrapper;
906
- TypedData_Get_Struct(nodeset, NodeSetWrapper, &nodeset_type, wrapper);
1466
+ // Convert CSS to XPath
1467
+ std::string xpath_str = css_to_xpath(css_str);
907
1468
 
908
- if (RARRAY_LEN(wrapper->nodes_array) == 0) {
1469
+ #ifdef HAVE_XALAN
1470
+ DocumentWrapper* doc_wrapper;
1471
+ TypedData_Get_Struct(self, DocumentWrapper, &document_type, doc_wrapper);
1472
+
1473
+ if (!doc_wrapper->doc) {
909
1474
  return Qnil;
910
1475
  }
911
1476
 
912
- return rb_ary_entry(wrapper->nodes_array, 0);
1477
+ // Use optimized first-only version
1478
+ DOMElement* root = doc_wrapper->doc->getDocumentElement();
1479
+ if (!root) {
1480
+ return Qnil;
1481
+ }
1482
+ return execute_xpath_with_xalan_first(root, xpath_str.c_str(), self);
1483
+ #else
1484
+ return document_at_xpath(self, rb_str_new_cstr(xpath_str.c_str()));
1485
+ #endif
913
1486
  }
914
1487
 
915
1488
  // node.inspect - human-readable representation
@@ -1164,10 +1737,8 @@ static VALUE node_children(VALUE self) {
1164
1737
  NodeWrapper* wrapper;
1165
1738
  TypedData_Get_Struct(self, NodeWrapper, &node_type, wrapper);
1166
1739
 
1167
- VALUE children = rb_ary_new();
1168
-
1169
1740
  if (!wrapper->node) {
1170
- return children;
1741
+ return rb_ary_new();
1171
1742
  }
1172
1743
 
1173
1744
  VALUE doc_ref = wrapper->doc_ref;
@@ -1175,8 +1746,10 @@ static VALUE node_children(VALUE self) {
1175
1746
  DOMNodeList* child_nodes = wrapper->node->getChildNodes();
1176
1747
  XMLSize_t count = child_nodes->getLength();
1177
1748
 
1178
- for (XMLSize_t i = 0; i < count; i++) {
1179
- DOMNode* child = child_nodes->item(i);
1749
+ // Pre-size array for better performance
1750
+ VALUE children = rb_ary_new_capa((long)count);
1751
+
1752
+ for (DOMNode* child = wrapper->node->getFirstChild(); child; child = child->getNextSibling()) {
1180
1753
  rb_ary_push(children, wrap_node(child, doc_ref));
1181
1754
  }
1182
1755
 
@@ -1188,18 +1761,18 @@ static VALUE node_element_children(VALUE self) {
1188
1761
  NodeWrapper* wrapper;
1189
1762
  TypedData_Get_Struct(self, NodeWrapper, &node_type, wrapper);
1190
1763
 
1191
- VALUE children = rb_ary_new();
1192
-
1193
1764
  if (!wrapper->node) {
1194
- return children;
1765
+ return rb_ary_new();
1195
1766
  }
1196
1767
 
1197
1768
  VALUE doc_ref = wrapper->doc_ref;
1198
1769
  DOMNodeList* child_nodes = wrapper->node->getChildNodes();
1199
1770
  XMLSize_t count = child_nodes->getLength();
1200
1771
 
1201
- for (XMLSize_t i = 0; i < count; i++) {
1202
- DOMNode* child = child_nodes->item(i);
1772
+ // Pre-size array (may be smaller than count, but good estimate)
1773
+ VALUE children = rb_ary_new_capa((long)count);
1774
+
1775
+ for (DOMNode* child = wrapper->node->getFirstChild(); child; child = child->getNextSibling()) {
1203
1776
  if (child->getNodeType() == DOMNode::ELEMENT_NODE) {
1204
1777
  rb_ary_push(children, wrap_node(child, doc_ref));
1205
1778
  }
@@ -1217,18 +1790,7 @@ static VALUE node_first_element_child(VALUE self) {
1217
1790
  return Qnil;
1218
1791
  }
1219
1792
 
1220
- VALUE doc_ref = wrapper->doc_ref;
1221
- DOMNodeList* child_nodes = wrapper->node->getChildNodes();
1222
- XMLSize_t count = child_nodes->getLength();
1223
-
1224
- for (XMLSize_t i = 0; i < count; i++) {
1225
- DOMNode* child = child_nodes->item(i);
1226
- if (child->getNodeType() == DOMNode::ELEMENT_NODE) {
1227
- return wrap_node(child, doc_ref);
1228
- }
1229
- }
1230
-
1231
- return Qnil;
1793
+ return element_child(wrapper->node, wrapper->doc_ref, false);
1232
1794
  }
1233
1795
 
1234
1796
  // node.last_element_child - returns last element child
@@ -1240,19 +1802,7 @@ static VALUE node_last_element_child(VALUE self) {
1240
1802
  return Qnil;
1241
1803
  }
1242
1804
 
1243
- VALUE doc_ref = wrapper->doc_ref;
1244
- DOMNodeList* child_nodes = wrapper->node->getChildNodes();
1245
- XMLSize_t count = child_nodes->getLength();
1246
-
1247
- // Search backwards for last element
1248
- for (XMLSize_t i = count; i > 0; i--) {
1249
- DOMNode* child = child_nodes->item(i - 1);
1250
- if (child->getNodeType() == DOMNode::ELEMENT_NODE) {
1251
- return wrap_node(child, doc_ref);
1252
- }
1253
- }
1254
-
1255
- return Qnil;
1805
+ return element_child(wrapper->node, wrapper->doc_ref, true);
1256
1806
  }
1257
1807
 
1258
1808
  // node.document - returns the document that owns this node
@@ -1394,6 +1944,37 @@ static VALUE node_attributes(VALUE self) {
1394
1944
  return hash;
1395
1945
  }
1396
1946
 
1947
+ // node.attribute_nodes - returns array of attribute nodes (only for element nodes)
1948
+ static VALUE node_attribute_nodes(VALUE self) {
1949
+ NodeWrapper* wrapper;
1950
+ TypedData_Get_Struct(self, NodeWrapper, &node_type, wrapper);
1951
+
1952
+ VALUE nodes_array = rb_ary_new();
1953
+
1954
+ if (!wrapper->node || wrapper->node->getNodeType() != DOMNode::ELEMENT_NODE) {
1955
+ return nodes_array;
1956
+ }
1957
+
1958
+ DOMElement* element = dynamic_cast<DOMElement*>(wrapper->node);
1959
+ DOMNamedNodeMap* attributes = element->getAttributes();
1960
+
1961
+ if (!attributes) {
1962
+ return nodes_array;
1963
+ }
1964
+
1965
+ VALUE doc_ref = wrapper->doc_ref;
1966
+ XMLSize_t length = attributes->getLength();
1967
+
1968
+ for (XMLSize_t i = 0; i < length; i++) {
1969
+ DOMNode* attr = attributes->item(i);
1970
+ if (attr) {
1971
+ rb_ary_push(nodes_array, wrap_node(attr, doc_ref));
1972
+ }
1973
+ }
1974
+
1975
+ return nodes_array;
1976
+ }
1977
+
1397
1978
  // node.next_sibling
1398
1979
  static VALUE node_next_sibling(VALUE self) {
1399
1980
  NodeWrapper* wrapper;
@@ -1493,7 +2074,7 @@ static VALUE node_add_child(VALUE self, VALUE child) {
1493
2074
  }
1494
2075
 
1495
2076
  DOMNode* child_node = NULL;
1496
- bool needs_import = false;
2077
+ VALUE doc_ref = wrapper->doc_ref; // Keep track of the Ruby document reference
1497
2078
 
1498
2079
  // Check if child is a string or a node
1499
2080
  if (TYPE(child) == T_STRING) {
@@ -1507,13 +2088,27 @@ static VALUE node_add_child(VALUE self, VALUE child) {
1507
2088
  NodeWrapper* child_wrapper;
1508
2089
  if (rb_obj_is_kind_of(child, rb_cNode)) {
1509
2090
  TypedData_Get_Struct(child, NodeWrapper, &node_type, child_wrapper);
1510
- child_node = child_wrapper->node;
2091
+ DOMNode* original_child = child_wrapper->node;
1511
2092
 
1512
2093
  // Check if child belongs to a different document
1513
- DOMDocument* child_doc = child_node->getOwnerDocument();
2094
+ DOMDocument* child_doc = original_child->getOwnerDocument();
1514
2095
  if (child_doc && child_doc != doc) {
1515
- rb_raise(rb_eRuntimeError,
1516
- "Node belongs to a different document. Use importNode to adopt nodes from other documents.");
2096
+ // Automatically import the node from the other document
2097
+ // The second parameter 'true' means deep copy (include all descendants)
2098
+ try {
2099
+ child_node = doc->importNode(original_child, true);
2100
+
2101
+ // Update the child wrapper to point to the imported node
2102
+ // and the new document reference
2103
+ child_wrapper->node = child_node;
2104
+ child_wrapper->doc_ref = doc_ref;
2105
+ } catch (const DOMException& e) {
2106
+ CharStr message(e.getMessage());
2107
+ rb_raise(rb_eRuntimeError, "Failed to import node from different document: %s",
2108
+ message.localForm());
2109
+ }
2110
+ } else {
2111
+ child_node = original_child;
1517
2112
  }
1518
2113
  } else {
1519
2114
  rb_raise(rb_eTypeError, "Argument must be a String or Node");
@@ -1590,11 +2185,7 @@ static VALUE node_inner_html(VALUE self) {
1590
2185
 
1591
2186
  // Build a string by serializing each child
1592
2187
  std::string result;
1593
- DOMNodeList* children = wrapper->node->getChildNodes();
1594
- XMLSize_t count = children->getLength();
1595
-
1596
- for (XMLSize_t i = 0; i < count; i++) {
1597
- DOMNode* child = children->item(i);
2188
+ for (DOMNode* child = wrapper->node->getFirstChild(); child; child = child->getNextSibling()) {
1598
2189
  XMLCh* xml_str = serializer->writeToString(child);
1599
2190
  CharStr utf8_str(xml_str);
1600
2191
  result += utf8_str.localForm();
@@ -1698,16 +2289,8 @@ static VALUE node_blank_p(VALUE self) {
1698
2289
 
1699
2290
  // Element nodes are blank if they have no child elements and no non-blank text
1700
2291
  if (wrapper->node->getNodeType() == DOMNode::ELEMENT_NODE) {
1701
- DOMNodeList* children = wrapper->node->getChildNodes();
1702
- XMLSize_t count = children->getLength();
1703
-
1704
- if (count == 0) {
1705
- return Qtrue;
1706
- }
1707
-
1708
- // Check if all children are blank text nodes
1709
- for (XMLSize_t i = 0; i < count; i++) {
1710
- DOMNode* child = children->item(i);
2292
+ // Check if all children are blank text nodes.
2293
+ for (DOMNode* child = wrapper->node->getFirstChild(); child; child = child->getNextSibling()) {
1711
2294
 
1712
2295
  // If there's an element child, not blank
1713
2296
  if (child->getNodeType() == DOMNode::ELEMENT_NODE) {
@@ -1752,6 +2335,10 @@ static VALUE node_xpath(VALUE self, VALUE path) {
1752
2335
  const char* xpath_str = StringValueCStr(path);
1753
2336
  VALUE doc_ref = node_wrapper->doc_ref;
1754
2337
 
2338
+ #ifndef HAVE_XALAN
2339
+ validate_xpath_expression(xpath_str);
2340
+ #endif
2341
+
1755
2342
  #ifdef HAVE_XALAN
1756
2343
  // Use Xalan for full XPath 1.0 support
1757
2344
  return execute_xpath_with_xalan(node_wrapper->node, xpath_str, doc_ref);
@@ -1812,28 +2399,52 @@ static VALUE node_xpath(VALUE self, VALUE path) {
1812
2399
 
1813
2400
  // node.at_xpath(path) - returns first matching node or nil
1814
2401
  static VALUE node_at_xpath(VALUE self, VALUE path) {
1815
- VALUE nodeset = node_xpath(self, path);
1816
- NodeSetWrapper* wrapper;
1817
- TypedData_Get_Struct(nodeset, NodeSetWrapper, &nodeset_type, wrapper);
2402
+ NodeWrapper* node_wrapper;
2403
+ TypedData_Get_Struct(self, NodeWrapper, &node_type, node_wrapper);
1818
2404
 
1819
- if (RARRAY_LEN(wrapper->nodes_array) == 0) {
2405
+ if (!node_wrapper->node) {
1820
2406
  return Qnil;
1821
2407
  }
1822
2408
 
1823
- return rb_ary_entry(wrapper->nodes_array, 0);
2409
+ Check_Type(path, T_STRING);
2410
+ const char* xpath_str = StringValueCStr(path);
2411
+ VALUE doc_ref = node_wrapper->doc_ref;
2412
+
2413
+ #ifndef HAVE_XALAN
2414
+ validate_xpath_expression(xpath_str);
2415
+ #endif
2416
+
2417
+ #ifdef HAVE_XALAN
2418
+ // Use optimized first-only version
2419
+ return execute_xpath_with_xalan_first(node_wrapper->node, xpath_str, doc_ref);
2420
+ #else
2421
+ return execute_xpath_with_xerces_first(node_wrapper->node, xpath_str, doc_ref);
2422
+ #endif
1824
2423
  }
1825
2424
 
1826
2425
  // node.at_css(selector) - returns first matching node or nil
1827
2426
  static VALUE node_at_css(VALUE self, VALUE selector) {
1828
- VALUE nodeset = node_css(self, selector);
1829
- NodeSetWrapper* wrapper;
1830
- TypedData_Get_Struct(nodeset, NodeSetWrapper, &nodeset_type, wrapper);
2427
+ Check_Type(selector, T_STRING);
2428
+ const char* css_str = StringValueCStr(selector);
1831
2429
 
1832
- if (RARRAY_LEN(wrapper->nodes_array) == 0) {
2430
+ // Convert CSS to XPath
2431
+ std::string xpath_str = css_to_xpath(css_str);
2432
+
2433
+ #ifdef HAVE_XALAN
2434
+ NodeWrapper* node_wrapper;
2435
+ TypedData_Get_Struct(self, NodeWrapper, &node_type, node_wrapper);
2436
+
2437
+ if (!node_wrapper->node) {
1833
2438
  return Qnil;
1834
2439
  }
1835
2440
 
1836
- return rb_ary_entry(wrapper->nodes_array, 0);
2441
+ VALUE doc_ref = node_wrapper->doc_ref;
2442
+
2443
+ // Use optimized first-only version
2444
+ return execute_xpath_with_xalan_first(node_wrapper->node, xpath_str.c_str(), doc_ref);
2445
+ #else
2446
+ return node_at_xpath(self, rb_str_new_cstr(xpath_str.c_str()));
2447
+ #endif
1837
2448
  }
1838
2449
 
1839
2450
  // Helper function to convert basic CSS selectors to XPath
@@ -2121,6 +2732,31 @@ static VALUE nodeset_text(VALUE self) {
2121
2732
  }
2122
2733
 
2123
2734
  // nodeset.inspect / nodeset.to_s - human-readable representation
2735
+ // Helper function to safely truncate UTF-8 strings using Ruby's built-in UTF-8 handling
2736
+ // Ruby's rb_str_substr operates on CHARACTER positions, not byte positions
2737
+ static std::string safe_truncate_utf8(const std::string& str, long max_chars) {
2738
+ if (str.empty()) {
2739
+ return str;
2740
+ }
2741
+
2742
+ // Create a Ruby string with UTF-8 encoding
2743
+ VALUE rb_str = rb_enc_str_new(str.c_str(), str.length(), rb_utf8_encoding());
2744
+
2745
+ // Get the character length (not byte length)
2746
+ long char_len = RSTRING_LEN(rb_str);
2747
+
2748
+ if (char_len <= max_chars) {
2749
+ return str;
2750
+ }
2751
+
2752
+ // Use Ruby's rb_str_substr which correctly handles multi-byte characters
2753
+ // Parameters: string, start position (in characters), length (in characters)
2754
+ VALUE truncated = rb_str_substr(rb_str, 0, max_chars);
2755
+
2756
+ // Convert back to C++ string
2757
+ return std::string(RSTRING_PTR(truncated), RSTRING_LEN(truncated));
2758
+ }
2759
+
2124
2760
  static VALUE nodeset_inspect(VALUE self) {
2125
2761
  NodeSetWrapper* wrapper;
2126
2762
  TypedData_Get_Struct(self, NodeSetWrapper, &nodeset_type, wrapper);
@@ -2191,7 +2827,7 @@ static VALUE nodeset_inspect(VALUE self) {
2191
2827
  textStr = textStr.substr(start, end - start + 1);
2192
2828
 
2193
2829
  if (textStr.length() > 30) {
2194
- textStr = textStr.substr(0, 27) + "...";
2830
+ textStr = safe_truncate_utf8(textStr, 27) + "...";
2195
2831
  }
2196
2832
 
2197
2833
  result += ">";
@@ -2219,7 +2855,7 @@ static VALUE nodeset_inspect(VALUE self) {
2219
2855
  textStr = textStr.substr(start, end - start + 1);
2220
2856
 
2221
2857
  if (textStr.length() > 30) {
2222
- textStr = textStr.substr(0, 27) + "...";
2858
+ textStr = safe_truncate_utf8(textStr, 27) + "...";
2223
2859
  }
2224
2860
 
2225
2861
  result += "text(\"";
@@ -2241,7 +2877,10 @@ static VALUE nodeset_inspect(VALUE self) {
2241
2877
  }
2242
2878
 
2243
2879
  result += "]>";
2244
- return rb_str_new_cstr(result.c_str());
2880
+ VALUE rb_result = rb_str_new_cstr(result.c_str());
2881
+ // Ensure the string is marked as UTF-8 encoded
2882
+ rb_enc_associate(rb_result, rb_utf8_encoding());
2883
+ return rb_result;
2245
2884
  }
2246
2885
 
2247
2886
  // Schema.from_document(schema_doc) or Schema.from_string(xsd_string)
@@ -2282,6 +2921,18 @@ static VALUE schema_from_document(int argc, VALUE* argv, VALUE klass) {
2282
2921
 
2283
2922
  try {
2284
2923
  schemaParser->parse(schemaInput);
2924
+ } catch (const XMLException& e) {
2925
+ delete schemaParser;
2926
+ delete wrapper->schemaContent;
2927
+ xfree(wrapper);
2928
+ CharStr message(e.getMessage());
2929
+ rb_raise(rb_eRuntimeError, "Schema parsing failed: %s", message.localForm());
2930
+ } catch (const SAXException& e) {
2931
+ delete schemaParser;
2932
+ delete wrapper->schemaContent;
2933
+ xfree(wrapper);
2934
+ CharStr message(e.getMessage());
2935
+ rb_raise(rb_eRuntimeError, "Schema parsing failed: %s", message.localForm());
2285
2936
  } catch (...) {
2286
2937
  delete schemaParser;
2287
2938
  delete wrapper->schemaContent;
@@ -2363,6 +3014,12 @@ static VALUE document_validate(VALUE self, VALUE rb_schema) {
2363
3014
  validator->loadGrammar(schemaSource, Grammar::SchemaGrammarType, true);
2364
3015
  validator->setExternalNoNamespaceSchemaLocation("schema.xsd");
2365
3016
  validator->useCachedGrammarInParse(true);
3017
+ } catch (const XMLException& e) {
3018
+ CharStr message(e.getMessage());
3019
+ errorHandler.errors.push_back(std::string("Warning: Schema grammar could not be loaded: ") + message.localForm());
3020
+ } catch (const SAXException& e) {
3021
+ CharStr message(e.getMessage());
3022
+ errorHandler.errors.push_back(std::string("Warning: Schema grammar could not be loaded: ") + message.localForm());
2366
3023
  } catch (...) {
2367
3024
  // If grammar loading fails, just note it
2368
3025
  errorHandler.errors.push_back("Warning: Schema grammar could not be loaded");
@@ -2414,13 +3071,110 @@ static VALUE document_validate(VALUE self, VALUE rb_schema) {
2414
3071
  }
2415
3072
 
2416
3073
  return Qnil;
2417
- }extern "C" void Init_rxerces(void) {
3074
+ }
3075
+
3076
+ // RXerces.cache_xpath_validation? - check if XPath validation caching is enabled
3077
+ static VALUE rxerces_cache_xpath_validation_p(VALUE self) {
3078
+ return cache_xpath_validation ? Qtrue : Qfalse;
3079
+ }
3080
+
3081
+ // RXerces.cache_xpath_validation = bool - enable/disable XPath validation caching
3082
+ static VALUE rxerces_set_cache_xpath_validation(VALUE self, VALUE val) {
3083
+ cache_xpath_validation = RTEST(val);
3084
+ return val;
3085
+ }
3086
+
3087
+ // RXerces.clear_xpath_validation_cache - clear the XPath validation cache
3088
+ static VALUE rxerces_clear_xpath_validation_cache(VALUE self) {
3089
+ std::lock_guard<std::mutex> lock(xpath_cache_mutex);
3090
+ if (xpath_cache_lru_list) {
3091
+ xpath_cache_lru_list->clear();
3092
+ }
3093
+ if (xpath_cache_map) {
3094
+ xpath_cache_map->clear();
3095
+ }
3096
+ return Qnil;
3097
+ }
3098
+
3099
+ // RXerces.xpath_validation_cache_size - return number of cached expressions
3100
+ static VALUE rxerces_xpath_validation_cache_size(VALUE self) {
3101
+ std::lock_guard<std::mutex> lock(xpath_cache_mutex);
3102
+ if (!xpath_cache_map) {
3103
+ return LONG2NUM(0);
3104
+ }
3105
+ return LONG2NUM((long)xpath_cache_map->size());
3106
+ }
3107
+
3108
+ // RXerces.xpath_validation_cache_max_size - get max cache size
3109
+ static VALUE rxerces_xpath_validation_cache_max_size(VALUE self) {
3110
+ return LONG2NUM((long)xpath_cache_max_size);
3111
+ }
3112
+
3113
+ // RXerces.xpath_validation_cache_max_size = n - set max cache size
3114
+ static VALUE rxerces_set_xpath_validation_cache_max_size(VALUE self, VALUE val) {
3115
+ // Validate input: must be a non-negative integer
3116
+ if (!RB_INTEGER_TYPE_P(val)) {
3117
+ rb_raise(rb_eTypeError, "xpath_validation_cache_max_size must be an Integer");
3118
+ }
3119
+
3120
+ long size = NUM2LONG(val);
3121
+ if (size < 0) {
3122
+ rb_raise(rb_eArgError, "xpath_validation_cache_max_size must be non-negative");
3123
+ }
3124
+
3125
+ xpath_cache_max_size = (size_t)size;
3126
+ return val;
3127
+ }
3128
+
3129
+ // RXerces.xalan_enabled? - check if Xalan is available
3130
+ static VALUE rxerces_xalan_enabled_p(VALUE self) {
3131
+ #ifdef HAVE_XALAN
3132
+ return Qtrue;
3133
+ #else
3134
+ return Qfalse;
3135
+ #endif
3136
+ }
3137
+
3138
+ // RXerces.xpath_max_length - get max XPath expression length
3139
+ static VALUE rxerces_xpath_max_length(VALUE self) {
3140
+ return LONG2NUM((long)xpath_max_length);
3141
+ }
3142
+
3143
+ // RXerces.xpath_max_length = n - set max XPath expression length (0 = no limit)
3144
+ static VALUE rxerces_set_xpath_max_length(VALUE self, VALUE val) {
3145
+ // Validate input: must be a non-negative integer
3146
+ if (!RB_INTEGER_TYPE_P(val)) {
3147
+ rb_raise(rb_eTypeError, "xpath_max_length must be an Integer");
3148
+ }
3149
+
3150
+ long size = NUM2LONG(val);
3151
+ if (size < 0) {
3152
+ rb_raise(rb_eArgError, "xpath_max_length must be non-negative");
3153
+ }
3154
+
3155
+ xpath_max_length = (size_t)size;
3156
+ return val;
3157
+ }
3158
+
3159
+ extern "C" void Init_rxerces(void) {
2418
3160
  rb_mRXerces = rb_define_module("RXerces");
3161
+
3162
+ // Module-level configuration methods for XPath validation caching
3163
+ rb_define_singleton_method(rb_mRXerces, "cache_xpath_validation?", RUBY_METHOD_FUNC(rxerces_cache_xpath_validation_p), 0);
3164
+ rb_define_singleton_method(rb_mRXerces, "cache_xpath_validation=", RUBY_METHOD_FUNC(rxerces_set_cache_xpath_validation), 1);
3165
+ rb_define_singleton_method(rb_mRXerces, "clear_xpath_validation_cache", RUBY_METHOD_FUNC(rxerces_clear_xpath_validation_cache), 0);
3166
+ rb_define_singleton_method(rb_mRXerces, "xpath_validation_cache_size", RUBY_METHOD_FUNC(rxerces_xpath_validation_cache_size), 0);
3167
+ rb_define_singleton_method(rb_mRXerces, "xpath_validation_cache_max_size", RUBY_METHOD_FUNC(rxerces_xpath_validation_cache_max_size), 0);
3168
+ rb_define_singleton_method(rb_mRXerces, "xpath_validation_cache_max_size=", RUBY_METHOD_FUNC(rxerces_set_xpath_validation_cache_max_size), 1);
3169
+ rb_define_singleton_method(rb_mRXerces, "xpath_max_length", RUBY_METHOD_FUNC(rxerces_xpath_max_length), 0);
3170
+ rb_define_singleton_method(rb_mRXerces, "xpath_max_length=", RUBY_METHOD_FUNC(rxerces_set_xpath_max_length), 1);
3171
+ rb_define_singleton_method(rb_mRXerces, "xalan_enabled?", RUBY_METHOD_FUNC(rxerces_xalan_enabled_p), 0);
3172
+
2419
3173
  rb_mXML = rb_define_module_under(rb_mRXerces, "XML");
2420
3174
 
2421
3175
  rb_cDocument = rb_define_class_under(rb_mXML, "Document", rb_cObject);
2422
3176
  rb_undef_alloc_func(rb_cDocument);
2423
- rb_define_singleton_method(rb_cDocument, "parse", RUBY_METHOD_FUNC(document_parse), 1);
3177
+ rb_define_singleton_method(rb_cDocument, "parse", RUBY_METHOD_FUNC(document_parse), -1);
2424
3178
  rb_define_method(rb_cDocument, "root", RUBY_METHOD_FUNC(document_root), 0);
2425
3179
  rb_define_method(rb_cDocument, "errors", RUBY_METHOD_FUNC(document_errors), 0);
2426
3180
  rb_define_method(rb_cDocument, "to_s", RUBY_METHOD_FUNC(document_to_s), 0);
@@ -2464,6 +3218,7 @@ static VALUE document_validate(VALUE self, VALUE rb_schema) {
2464
3218
  rb_define_method(rb_cNode, "parent", RUBY_METHOD_FUNC(node_parent), 0);
2465
3219
  rb_define_method(rb_cNode, "ancestors", RUBY_METHOD_FUNC(node_ancestors), -1);
2466
3220
  rb_define_method(rb_cNode, "attributes", RUBY_METHOD_FUNC(node_attributes), 0);
3221
+ rb_define_method(rb_cNode, "attribute_nodes", RUBY_METHOD_FUNC(node_attribute_nodes), 0);
2467
3222
  rb_define_method(rb_cNode, "next_sibling", RUBY_METHOD_FUNC(node_next_sibling), 0);
2468
3223
  rb_define_method(rb_cNode, "next_element", RUBY_METHOD_FUNC(node_next_element), 0);
2469
3224
  rb_define_method(rb_cNode, "previous_sibling", RUBY_METHOD_FUNC(node_previous_sibling), 0);