ソースを参照

fix(core): correct route parameter extraction and node ownership

- Use requested_path_segments for parameter values instead of route_segments
  to correctly capture actual URL values rather than route pattern segments
- Filter empty segments from route pattern splits to handle leading/trailing slashes
- Remove fragment mode from MarkupDocument to simplify document handling
- Copy XML nodes when replacing/appending to prevent use-after-free issues
  when the source document is modified or disposed
Billy Barrow 1 週間 前
コミット
5161e2e1a7

+ 3 - 3
src/Components/EndpointRouter.vala

@@ -129,7 +129,7 @@ namespace Astralis {
                 for(int i = 0; i < prefix_components.length; i++) {
                     if(prefix_components[i].has_prefix("{") && prefix_components[i].has_suffix("}")) {
                         if(i < matched_route.route_segments.length) {
-                            parameter_map.add(prefix_components[i][1:-1], matched_route.route_segments[i]);
+                            parameter_map.add(prefix_components[i][1:-1], this.requested_path_segments[i]);
                         }
                     }
                 }
@@ -161,7 +161,7 @@ namespace Astralis {
                 if(!endpoint_components[i].has_prefix("{") || !endpoint_components[i].has_suffix("}")) {
                     continue;
                 }
-                parameter_map.add(endpoint_components[i][1:-1], matched_route.route_segments[i]);
+                parameter_map.add(endpoint_components[i][1:-1], this.requested_path_segments[i]);
             }
 
             this.mapped_parameters = parameter_map;
@@ -181,7 +181,7 @@ namespace Astralis {
 
             this.route  = route;
             this.methods = Wrap.va_list<Method>(method, va_list()).to_immutable_buffer();
-            this.route_segments = Wrap.array<string>(this.route.split("/")).to_immutable_buffer();
+            this.route_segments = Wrap.array<string>(this.route.split("/")).where(s => s.length != 0).to_immutable_buffer();
             this.route_parameters = this.route_segments
                 .where(s => s.has_prefix("{") && s.has_suffix("}"))
                 .select<string>(s => s.substring(1, s.length-2))

+ 1 - 15
src/Markup/MarkupDocument.vala

@@ -8,7 +8,6 @@ namespace Astralis {
     /// Represents an HTML document that can be loaded, manipulated, and rendered
     /// </summary>
     public class MarkupDocument : GLib.Object {
-        public bool fragment { get; private set; }
         private Html.Doc* doc;
 
         /// <summary>
@@ -78,7 +77,6 @@ namespace Astralis {
             }
             
             var copied_doc = new MarkupDocument.from_doc((Html.Doc*)xml_copy);
-            copied_doc.fragment = this.fragment;
             return copied_doc;
         }
 
@@ -91,15 +89,7 @@ namespace Astralis {
             
             char[] buffer = html.to_utf8();
             doc = Html.Doc.read_memory(buffer, buffer.length, "", null, options);
-            
-            if (doc == null) {
-                // Try parsing as a fragment wrapped in a basic structure
-                string wrapped = "<!DOCTYPE html><html><head><meta charset=\"UTF-8\"/></head><body>%s</body></html>".printf(html);
-                char[] wrapped_buffer = wrapped.to_utf8();
-                doc = Html.Doc.read_memory(wrapped_buffer, wrapped_buffer.length, "", null, options);
-                fragment = true;
-            }
-            
+
             if (doc == null) {
                 throw new MarkupError.PARSE_ERROR("Failed to parse HTML document");
             }
@@ -126,7 +116,6 @@ namespace Astralis {
         /// </summary>
         public MarkupNode? head {
             owned get {
-                if(fragment) return null;
                 var root = doc->get_root_element();
                 if (root == null) return null;
                 
@@ -271,9 +260,6 @@ namespace Astralis {
         /// Converts the document to an HTML string using libxml's HTML serializer
         /// </summary>
         public string to_html() {
-            if(fragment) {
-                return body.inner_html;
-            }
             string buffer;
             int len;
             doc->dump_memory(out buffer, out len);

+ 9 - 5
src/Markup/MarkupNode.vala

@@ -285,7 +285,7 @@ namespace Astralis {
         /// <param name="node">The node to replace this element with</param>
         public void replace_with_node(MarkupNode node) {
             // Insert the new node before this node
-            xml_node->add_prev_sibling(node.native);
+            xml_node->add_prev_sibling(node.native->copy(1));
             
             // Remove this node
             remove();
@@ -293,14 +293,18 @@ namespace Astralis {
 
         public void replace_with_nodes(Enumerable<MarkupNode> nodes) {           
             foreach(var node in nodes) {
-                xml_node->add_prev_sibling(node.native);
+                xml_node->add_prev_sibling(node.native->copy(1));
             }
             remove();
         }
 
-        public void append_document_contents(MarkupDocument document) {
-            foreach(var child in document.body.children) {
-                xml_node->add_child(child.native);
+        public void append_node(MarkupNode node) {
+            xml_node->add_child(node.native->copy(1));
+        }
+
+        public void append_nodes(Enumerable<MarkupNode> nodes) {
+            foreach(var child in nodes) {
+                xml_node->add_child(child.native->copy(1));
             }
         }