From 1426a6c942b6fdbdb28a78c61f73aab31c7f83c6 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 31 May 2023 22:26:04 -0700 Subject: Allow fmt and escj to be used to calculate attributes. * who.tl (attr-warning-macrolet): Do not warn when fmt and escj are used on attributes. Instead, give them a useful definition so they do what the programmer wants: return a string. * test/simple.tl: Trim it to just the tags that should warn now. Also fix a bug that rendered this case invalid: unquote occurred with no backquote, and this produced an undefined variable warning on sym, which was treated by the test case's handler as a false positive that made it pass. New test case 45 testing fmt and escj in attributes. * README.md: Added to notes on differences between TL-WHO and CL-WHO. --- README.md | 6 ++++++ test/simple.tl | 11 +++++++++-- who.tl | 14 +++++++++----- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 12bb321..3ef41c6 100644 --- a/README.md +++ b/README.md @@ -195,6 +195,12 @@ Here are the differences to be aware of: * TL-WHO's `esc` accepts arguments which are not strings. They are turned to a string using `tostringp`, and HTML-escaped. +* TL-WHO allows `fmt` as well as `escj` to be used to calculate attributes. + These local macros have alternative definitions when used in attributes. + The remaining local macros don't make sense in attributes, and make + a mess of the output if used; unlike CL-WHO, TL-WHO warns when they + are used in attributes. + Additionally, users (of CL-WHO and TL-WHO alike) are advised to watch for the following issue: the CL-WHO documentation is not accurately maintained and makes some references to material that no longer exists in CL-WHO, such as the diff --git a/test/simple.tl b/test/simple.tl index 3ed4685..4dc5b88 100644 --- a/test/simple.tl +++ b/test/simple.tl @@ -453,9 +453,9 @@ ;;; 41 ;;; Test that warning is produced when any of the WHO local macros are used in ;;; an Lisp expression that calculates an attribute -(each ((sym '(htm noesc-fmt fmt esc escq escj str))) +(each ((sym '(htm noesc-fmt esc escq str))) (test (catch - (eval '(progn + (eval ^(progn (with-html-output-to-string (out) (:foo :attr (,sym "abc"))) nil)) @@ -480,3 +480,10 @@ (test (with-html-output-to-string (out) (:p (esc 5) (escq 6) (escj 7))) "

567

") + +;;; 45 +;;; Test that we can use fmt and escj on attributes +(test (with-html-output-to-string (out) + (:div :span (fmt "~a&" 42) + :color (escj ""))) + "
") diff --git a/who.tl b/who.tl index 8ac2ebc..3817e43 100644 --- a/who.tl +++ b/who.tl @@ -88,11 +88,15 @@ (defun attr-warning-macrolet (form) (with-gensyms (warn) ^(macrolet ((,warn (f . rest) - ^(compile-warning ,f - "not recommended in attribute expr"))) - (macrolet ,(collect-each ((sym '(htm noesc-fmt fmt esc escq escj str))) - ^(,sym (:form f . rest) (,warn f) f)) - ,form)))) + ^(compile-warning ,f + "not recommended in attribute expr"))) + (macrolet ((fmt (. args) + ^(usr:fmt ,*args)) + (escj (thing) + ^[(tojson (tostringp ,thing)) 1..-1]) + ,*(collect-each ((sym '(htm noesc-fmt esc escq str))) + ^(,sym (:form f . rest) (,warn f) f))) + ,form)))) ;; Helper function for convert-tag-to-string-list which converts the ;; alist attr-list of attributes into a list of strings and/or Lisp -- cgit v1.2.3