Преглед на файлове

feat(markup): add outer_html property and to_result method

The outer_html property serializes a markup node including the element
itself. The to_result method provides a convenient way to return HTML
fragments for HTMX requests with proper Content-Type headers.
Billy Barrow преди 1 седмица
родител
ревизия
3bfb553db4
променени са 1 файла, в които са добавени 36 реда и са изтрити 0 реда
  1. 36 0
      src/Markup/MarkupNode.vala

+ 36 - 0
src/Markup/MarkupNode.vala

@@ -351,6 +351,42 @@ namespace Astralis {
             }
         }
 
+        /// <summary>
+        /// Gets the outer HTML of this element (including the element itself)
+        /// </summary>
+        public string outer_html {
+            owned get {
+                // Create a temporary document to serialize this node
+                var temp_doc = new Xml.Doc();
+                temp_doc.set_root_element(xml_node->copy(1));
+                string buffer;
+                temp_doc.dump_memory(out buffer);
+                // Strip XML declaration
+                if (buffer.has_prefix("<?xml")) {
+                    int end = buffer.index_of("?>");
+                    if (end >= 0) {
+                        int start = end + 2;
+                        while (start < buffer.length && buffer[start].isspace()) {
+                            start++;
+                        }
+                        return buffer.substring(start);
+                    }
+                }
+                return buffer ?? "";
+            }
+        }
+
+        /// <summary>
+        /// Creates an HttpResult from this node's outer HTML.
+        /// Useful for returning HTML fragments to HTMX requests.
+        /// </summary>
+        /// <param name="status">HTTP status code (defaults to OK)</param>
+        /// <returns>An HttpResult containing the node's HTML with Content-Type text/html</returns>
+        public HttpResult to_result(StatusCode status = StatusCode.OK) {
+            return new HttpStringResult(outer_html, status)
+                .set_header("Content-Type", "text/html; charset=UTF-8");
+        }
+
         internal MarkupDocument doc {
             get { return document; }
         }