From 43c7b5de358e8a158e9ed3c396d3866845682515 Mon Sep 17 00:00:00 2001 From: unwox Date: Wed, 16 Oct 2024 22:42:37 +0600 Subject: improve html library handling of self-closing tags --- lib/utils.fnl | 2 +- vendor/html.fnl | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/utils.fnl b/lib/utils.fnl index fcc235c..405bf72 100644 --- a/lib/utils.fnl +++ b/lib/utils.fnl @@ -5,7 +5,7 @@ (fn measure [what f] (local start (luna.utf8.now)) (local res (table.pack (f))) - (print (.. what ": " (/ (- (luna.utf8.now) start) 1e6))) + (print (.. what ": " (/ (- (luna.time.now) start) 1e6))) (values (table.unpack res))) {: must : measure} diff --git a/vendor/html.fnl b/vendor/html.fnl index 1be5d5d..753bbb9 100644 --- a/vendor/html.fnl +++ b/vendor/html.fnl @@ -10,22 +10,23 @@ (assert (= (type s) :string)) (s:gsub entity-search entity-replacements)) -(fn tag [tag-name attrs] +(fn tag [tag-name attrs self-closing?] (assert (= (type attrs) "table") (.. "Missing attrs table: " tag-name)) (let [attr-str (table.concat (icollect [k v (pairs attrs)] (if (= v true) k (.. k "=\"" (escape v)"\""))) " ")] - (.. "<" tag-name " " attr-str">"))) + (.. "<" tag-name " " attr-str (if self-closing? " />" ">")))) (fn render [document allow-no-escape?] (if (= (type document) :string) (escape document) (and allow-no-escape? (= (. document 1) :NO-ESCAPE)) (. document 2) - (let [[tag-name attrs & body] document] - (.. (tag tag-name attrs) + (let [[tag-name attrs & body] document + self-closing? (= 0 (# body))] + (.. (tag tag-name attrs self-closing?) (table.concat (icollect [_ element (ipairs body)] (render element allow-no-escape?)) " ") - "")))) + (if (not self-closing?) (.. "") ""))))) { :render render } -- cgit v1.2.3