From a9754463b6d9d95c63516e501f39dc83aa5ef2fa Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sat, 27 May 2023 23:41:20 +1200 Subject: [PATCH] Fix (& x) sequences truncating the page --- lib/tree-updater.rkt | 10 ++++++++-- lib/xexpr-utils.rkt | 9 +++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/tree-updater.rkt b/lib/tree-updater.rkt index e7d48b43..098af3d1 100644 --- a/lib/tree-updater.rkt +++ b/lib/tree-updater.rkt @@ -60,7 +60,9 @@ (figcaption "Test figure!")) (iframe (@ (src "https://example.com/iframe-src"))) (div (@ (class "reviews")) - (header "GameSpot Expert Reviews")))))) + (header "GameSpot Expert Reviews")) + (div (@ (data-test-ampersand) (class "mw-collapsible-content")) + (& ndash)))))) (define (updater wikiname #:strict-proxy? [strict-proxy? #f]) ;; precompute wikiurl regex for efficency @@ -159,7 +161,7 @@ (u (λ (v) (has-class? "mw-collapsible-content" attributes)) (λ (v) (for/list ([element v]) - (u (λ (element) (pair? element)) + (u (λ (element) (element-is-element? element)) (λ (element) `(,(car element) (@ ,@(attribute-maybe-update 'style (λ (a) (regexp-replace #rx"display: *none" a "display:inline")) (bits->attributes element))) @@ -304,6 +306,10 @@ (check-equal? ((query-selector (λ (t a c) (eq? t 'noscript)) transformed)) #f) ; check that gamespot reviews/ads are removed (check-equal? ((query-selector (λ (t a c) (has-class? "reviews" a)) transformed)) #f) + ; check that (& x) sequences are not broken + (check-equal? ((query-selector (λ (t a c) (dict-has-key? a 'data-test-ampersand)) transformed)) + '(div (@ (data-test-ampersand) (class "mw-collapsible-content")) + (& ndash))) ; benchmark (when (file-exists? "../storage/Frog.html") (with-input-from-file "../storage/Frog.html" diff --git a/lib/xexpr-utils.rkt b/lib/xexpr-utils.rkt index cb405107..e1ac9571 100644 --- a/lib/xexpr-utils.rkt +++ b/lib/xexpr-utils.rkt @@ -86,15 +86,16 @@ ; "element" is a real element with a type and everything (non-string, non-attributes) (define (element-is-element? element) - (and (element-is-bits? element) (not (element-is-xattributes? element)))) + (and (element-is-bits? element) (not (eq? (car element) '&))(not (element-is-xattributes? element)))) (module+ test (check-true (element-is-element? '(span "hi"))) (check-false (element-is-element? '(@ (alt "Cute cat.")))) - (check-false (element-is-element? "hi"))) + (check-false (element-is-element? "hi")) + (check-false (element-is-element? '(& ndash)))) -; "element content" is a real element or a string +; "element content" is a real element or a string or a (& x) sequence (define (element-is-content? element) - (or (string? element) (element-is-element? element))) + (or (string? element) (element-is-element? element) (and (pair? element) (eq? (car element) '&)))) (module+ test (check-true (element-is-content? '(span "hi"))) (check-false (element-is-content? '(@ (alt "Cute cat."))))