From d6332dfe78f687ab77050c4c03452cb514ca297c Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Wed, 17 Feb 2016 10:22:15 -0800 Subject: [PATCH] fix some defective logic with handling empty strings --- pollen/decode.rkt | 13 +++++-------- pollen/private/splice.rkt | 14 ++++++++++++-- pollen/private/ts.rktd | 2 +- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/pollen/decode.rkt b/pollen/decode.rkt index 4375eda..42f0da3 100644 --- a/pollen/decode.rkt +++ b/pollen/decode.rkt @@ -1,13 +1,9 @@ #lang racket/base (require xml txexpr racket/list sugar/list sugar/define sugar/test) -(require "setup.rkt" - "private/whitespace.rkt") +(require "setup.rkt" "private/splice.rkt") (require "unstable/typography.rkt") -(provide (all-from-out "unstable/typography.rkt")) ; bw compat - -(require "private/whitespace.rkt") -(provide (all-from-out "private/whitespace.rkt")) ; bw compat +(provide (all-from-out "unstable/typography.rkt")) ; bw compat, includes `whitespace?` (define (->list/tx x) ;; same as ->list but catches special case of single txexpr, @@ -170,7 +166,7 @@ xs)) (define not-empty-string? (λ(x) (not (and (string? x) (= (string-length x) 0))))) (let loop ([x x]) - (if (pair? x) + (if (and (pair? x) (not (attrs? x))) (let ([xs (map loop (filter not-empty-string? x))]) (append-map merge-if-newlines (slicef xs newlines?))) x))) @@ -178,7 +174,8 @@ (module-test-external (require racket/list) (check-equal? (merge-newlines empty) empty) - (check-equal? (merge-newlines '(p "\n" "" "\n")) '(p "\n\n")) +(check-equal? (merge-newlines '((p ((id "")) "\n" "" "\n"))) '((p ((id "")) "\n\n"))) + (check-equal? (merge-newlines '((p "\n" "" "\n"))) '((p "\n\n"))) (check-equal? (merge-newlines '(p "\n" "\n" "foo" "\n" "\n\n" "bar" (em "\n" "\n" "\n"))) '(p "\n\n" "foo" "\n\n\n" "bar" (em "\n\n\n")))) diff --git a/pollen/private/splice.rkt b/pollen/private/splice.rkt index 9a9e4fb..20bbb36 100644 --- a/pollen/private/splice.rkt +++ b/pollen/private/splice.rkt @@ -4,12 +4,21 @@ ;; (string->symbol (format "~a" #\u200B)) (define splice-signal-tag '@) +(define (attrs? x) + (and (list? x) + (andmap (λ(xi) + (and (list? xi) + (= (length xi) 2) + (symbol? (car xi)) + (string? (cadr xi)))) x))) + + (define (splice x [splicing-tag splice-signal-tag]) ; (listof txexpr-elements?) . -> . (listof txexpr-elements?)) (define spliceable? (λ(x) (and (pair? x) (eq? (car x) splicing-tag)))) (define not-null-string? (λ(x) (not (and (string? x) (= (string-length x) 0))))) (let loop ([x x]) - (if (list? x) + (if (and (list? x) (not (attrs? x))) ; don't splice null strings inside attrs (apply append (map (λ(x) ((if (spliceable? x) cdr list) (loop x))) (filter not-null-string? x))) @@ -22,7 +31,8 @@ (check-equal? (splice `((,splice-signal-tag 1 (,splice-signal-tag 2 "" (,splice-signal-tag 3 (div 4 (,splice-signal-tag 5))) 6) "" 7))) '(1 2 3 (div 4 5) 6 7)) (check-equal? (splice `((,splice-signal-tag "foo" "" "bar"))) '("foo" "bar")) - (check-equal? (splice null) null)) + (check-equal? (splice null) null) + (check-equal? (splice '(a ((href "")(foo "bar")) "zam")) '(a ((href "")(foo "bar")) "zam"))) (define (strip-empty-attrs x) diff --git a/pollen/private/ts.rktd b/pollen/private/ts.rktd index 42584b1..8311203 100644 --- a/pollen/private/ts.rktd +++ b/pollen/private/ts.rktd @@ -1 +1 @@ -1455676575 +1455733334