AtlatestRepositorysigil-doc
1
;; SD-P2: the inline node for a link that leaves the publication.2
;;3
;; The manuscript below is loaded through the real `#lang sigil/doc` reader, so4
;; the author command, the model constructor, and all three renderers are5
;; exercised on the same bytes rather than on hand-built nodes.6
(import (sigil test) (sigil string) (sigil doc) (sigil doc publication)7
(sigil doc print) (sigil doc render html) (sigil doc render typst))9
(load "test/fixtures/url-links.sgd")11
(define loc (make-srcloc "url-link.sgd" 1 1 0 1))12
(define (txt value) (make-text value loc))13
(define (raised-message thunk)14
(guard (exn (else (exception-message exn))) (thunk) #f))16
(define publication-document (make-publication-document "url-links" document))17
(define publication-index (build-publication-index (list publication-document)))18
(define html19
(document->html-fragment document20
(make-html-render-context publication-index "url-links" (lambda (target) "#") '() 1)))21
(define plain (document->plain document))22
(define typst23
(print-publication->typst24
(make-print-publication "Links" #f '("David Wilson") (list publication-document) '())))26
;; Four targets taken VERBATIM from the Guile Beginners corpus. That corpus's27
;; unrepresentable-link population is 60 external https occurrences (5928
;; distinct), listed in `test/fixtures/course-external-links.txt`; the gate29
;; asserts each target below is a member of that file, so these cannot drift30
;; into invented URLs.31
(define course-targets32
(list "https://www.gnu.org/software/guile/manual/html_node/String-Selection.html#index-substring"33
"https://www.gnu.org/software/guile/manual/html_node/Ports.html"34
"https://elpa.nongnu.org/nongnu/doc/geiser.html"35
"https://www.msys2.org/#installation"))37
(test "the author command builds the node through the real reader"38
(let ((targets (map cadr (filter (lambda (event) (eq? (car event) 'url-link))39
(document-events document)))))40
(assert-equal 8 (length targets))41
(for-each (lambda (target) (assert-true (member target targets))) course-targets)42
(assert-true (member "http://example.org/plain" targets))43
(assert-true (member "mailto:david@example.com" targets))))45
(test "an empty body makes the target its own label"46
(let* ((shapes (cadr (document-children document)))47
(paragraph (car (section-children shapes)))48
(node (cadr (paragraph-children paragraph))))49
(assert-true (url-link? node))50
(assert-equal "http://example.org/plain" (url-link-href node))51
(assert-equal 1 (length (url-link-children node)))52
(assert-equal "http://example.org/plain" (text-content (car (url-link-children node))))))54
(test "the node is inline, located, and canonically serializable"55
(let ((node (make-url-link "https://example.com/a" (list (txt "label")) loc)))56
(assert-true (inline-node? node))57
(assert-false (block-node? node))58
(assert-true (document-node? node))59
(assert-equal "url-link.sgd" (srcloc-file (node-location node)))60
(assert-equal 'url-link (car (document->datum node)))61
(assert-equal "https://example.com/a" (cadr (document->datum node)))62
(assert-equal (document->debug document) (document->string document))63
;; A paragraph accepts it exactly where the other inline nodes are accepted.64
(assert-true (paragraph? (make-paragraph (list (txt "see ") node) loc)))))66
(test "the node owns its target and its label"67
(let* ((href (string-copy "https://example.com/owned"))68
(label (list (txt "label")))69
(node (make-url-link href label loc)))70
(string-set! href 8 #\X)71
(set-car! label (txt "changed"))72
(assert-equal "https://example.com/owned" (url-link-href node))73
(assert-equal "label" (text-content (car (url-link-children node))))74
(let ((returned-href (url-link-href node))75
(returned-label (url-link-children node)))76
(string-set! returned-href 8 #\X)77
(set-car! returned-label (txt "changed"))78
(assert-equal "https://example.com/owned" (url-link-href node))79
(assert-equal "label" (text-content (car (url-link-children node)))))))81
(test "the constructor accepts exactly the allowlisted shapes"82
(for-each83
(lambda (target)84
(assert-equal target (url-link-href (make-url-link target (list (txt "l")) loc))))85
(list "https://example.com"86
"https://example.com/a/b?c=1&d=2#e"87
"http://example.com/plain"88
"HTTPS://EXAMPLE.COM/shouting"89
"mailto:name@example.com"90
"https://example.com/quote\"and<angle>")))92
;; Each refusal names the thing that is missing rather than dropping the link.93
(test "the constructor refuses everything outside the policy, by name"94
(for-each95
(lambda (entry)96
(let ((message (raised-message (lambda () (make-url-link (car entry) (list (txt "l")) loc)))))97
(assert-true (string? message))98
(assert-true (string-contains? message (cadr entry)))))99
(list100
;; scheme allowlist, including the mixed-case bypass101
(list "javascript:alert(1)" "scheme javascript is refused")102
(list "JaVaScRiPt:alert(1)" "scheme javascript is refused")103
(list "data:text/plain;base64,QQ==" "scheme data is refused")104
(list "file:///etc/passwd" "scheme file is refused")105
;; no scheme at all: publication-relative targets belong to make-reference106
(list "/courses/guile-beginners/" "needs an explicit scheme")107
(list "#fragment" "needs an explicit scheme")108
(list "example.com/no-scheme" "needs an explicit scheme")109
;; control bytes, spaces, non-ASCII, backslashes110
(list "https://example.com/a b" "printable ASCII")111
(list "https://example.com/a\nb" "printable ASCII")112
(list "https://example.com/a\tb" "printable ASCII")113
(list "https://example.com/a\rb" "printable ASCII")114
(list "https://example.com/a\\b" "printable ASCII")115
(list "https://example.com/café" "printable ASCII")116
;; missing authority117
(list "https://" "needs an authority")118
(list "https:///etc/passwd" "needs an authority")119
(list "http://?a=1" "needs an authority")120
;; malformed addresses121
(list "mailto:" "needs one address")122
(list "mailto:nobody" "needs one address")123
(list "mailto:a@b@c" "needs one address")124
(list "mailto:a@b/c" "needs one address")))125
(assert-error (make-url-link "" (list (txt "l")) loc))126
(assert-error (make-url-link 'not-a-string (list (txt "l")) loc))127
;; the label is inline content and may not be empty or a block128
(assert-error (make-url-link "https://example.com" '() loc))129
(assert-error (make-url-link "https://example.com" (list (make-anchor "block" loc)) loc)))131
(test "all three renderers keep the target and escape it in place"132
(for-each133
(lambda (target)134
(assert-true (string-contains? html (string-append "href=\"" target "\"")))135
(assert-true (string-contains? typst (string-append "#link(\"" target "\")")))136
(assert-true (string-contains? plain (string-append "<" target ">"))))137
course-targets)138
(assert-true (string-contains? html "rel=\"external noopener noreferrer\""))139
(assert-true (string-contains? html ">substring</a>"))140
(assert-true (string-contains? plain "substring <https://www.gnu.org"))141
(assert-true (string-contains? typst "#link(\"mailto:david@example.com\")"))142
(assert-true (string-contains? html "href=\"mailto:david@example.com\"")))144
;; The escaping is `(sigil sxml)`'s and the print backend's, not hand-rolled.145
(test "a target needing escapes is escaped, never emitted raw"146
(assert-true (string-contains? html147
"href=\"https://example.com/search?a=1&b=<2>&c="3"\""))148
(assert-false (string-contains? html "?a=1&b=<2>"))149
(assert-true (string-contains? typst150
"#link(\"https://example.com/search?a=1&b=<2>&c=\\\"3\\\"\")"))151
(assert-false (string-contains? typst "&c=\"3\"\")"))152
(assert-true (string-contains? html "<em>emphasis</em></a>")))154
(test "the target is not a publication reference"155
;; It resolves against nothing, contributes only its label to titles and156
;; search, and cannot smuggle an unchecked internal link into the index.157
(let* ((titles (map publication-target-title (publication-index-targets publication-index))))158
(assert-true (member "course-links" (map publication-target-id (publication-index-targets publication-index))))159
(assert-false (member "https://elpa.nongnu.org/nongnu/doc/geiser.html" titles)))160
(let* ((titled (make-section "titled" (list (txt "See ") (make-url-link "https://example.com/x" (list (txt "the site")) loc))161
(list (make-paragraph (list (txt "body")) loc)) loc))162
(doc (make-document 'chapter "Titled" '() (list titled) loc))163
(index (build-publication-index (list (make-publication-document "titled" doc)))))164
(assert-equal "See the site"165
(publication-target-title (resolve-logical-reference index "titled")))))