AtlatestRepositorysigil-doc
1
(import (sigil test) (sigil string) (sigil doc) (sigil doc publication) (sigil doc render html))3
(define loc (make-srcloc "publication.sgd" 1 1 0 1))4
(define loc2 (make-srcloc "publication.sgd" 20 3 200 1))5
(define (txt value) (make-text value loc))6
(define (raises? thunk) (guard (exn (else #t)) (thunk) #f))7
(define (raised-message thunk)8
(guard (exn (else (exception-message exn))) (thunk) #f))9
(define destination10
(make-section "destination" (list (txt "Destination <script>"))11
(list (make-paragraph (list (txt "Safe & readable.")) loc)) loc))12
(define source13
(make-section "source" (list (txt "Source"))14
(list (make-paragraph15
(list (txt "Continue at ") (make-reference "destination" (list (txt "the destination")) loc)) loc)) loc))16
(define first-doc (make-document 'chapter "First" '("Author") (list source) loc))17
(define second-doc (make-document 'chapter "Second" '("Author") (list destination) loc))18
(define publication19
(build-publication-index20
(list (make-publication-document "first" first-doc)21
(make-publication-document "second" second-doc))))22
(define (target-url target)23
(string-append "/" (publication-target-document-id target) "/#" (publication-target-id target)))25
(test "publication index resolves global stable ids"26
(assert-equal 2 (length (publication-index-targets publication)))27
(assert-equal "second" (publication-target-document-id (resolve-logical-reference publication "destination")))28
(assert-equal 2 (length (publication-index-outlines publication)))29
(assert-equal 2 (length (publication-index-search-records publication))))31
(test "publication rejects duplicate and missing targets with context"32
(assert-true33
(raises? (lambda ()34
(build-publication-index35
(list (make-publication-document "one" second-doc)36
(make-publication-document "two" (make-document 'chapter "Dup" '() (list destination) loc)))))))37
(let* ((missing-section38
(make-section "missing-source" (list (txt "Missing"))39
(list (make-paragraph (list (make-reference "not-there" #f loc)) loc)) loc))40
(missing-doc (make-document 'chapter "Missing" '() (list missing-section) loc)))41
(assert-true (raises? (lambda ()42
(build-publication-index (list (make-publication-document "missing" missing-doc))))))))44
(test "publication reserves every renderer-emitted heading id before output"45
(let* ((base-section46
(make-section "foo" (list (txt "Foo"))47
(list (make-paragraph (list (txt "Body")) loc)) loc))48
(colliders49
(list50
(make-section "heading-foo" (list (make-text "Section" loc2))51
(list (make-paragraph (list (make-text "Body" loc2)) loc2)) loc2)52
(make-exercise "heading-foo" (list (make-text "Exercise" loc2))53
(list (make-paragraph (list (make-text "Body" loc2)) loc2)) loc2)54
(make-example "heading-foo" 'inline "(+ 1 1)" "sigil" #f 'bytecode55
'value "2" "" 5 'hidden loc2)56
(make-project-check "heading-foo" "." '("package.sgl") 'dev 'bytecode "" 5 loc2)57
(make-anchor "heading-foo" loc2))))58
(for-each59
(lambda (collider)60
(let* ((doc (make-document 'chapter "Collision" '() (list base-section collider) loc))61
(message (raised-message (lambda ()62
(build-publication-index (list (make-publication-document "collision" doc)))))))63
(assert-true (string? message))64
(assert-true (string-contains? message "duplicate emitted HTML id 'heading-foo'"))65
(assert-true (string-contains? message "publication.sgd:1:1"))66
(assert-true (string-contains? message "publication.sgd:20:3"))))67
colliders)))69
(test "HTML fragment is semantic escaped and route-neutral"70
(let* ((context (make-html-render-context publication "second" target-url '() 2))71
(html (document->html-fragment second-doc context)))72
(assert-true (string-contains? html "<article"))73
(assert-true (string-contains? html "id=\"destination\""))74
(assert-true (string-contains? html "<h2"))75
(assert-true (string-contains? html "class=\"visually-hidden\">Permalink to Destination"))76
(assert-true (string-contains? html "Destination <script>"))77
(assert-false (string-contains? html "<script>"))))79
(test "HTML references use injected publisher URLs"80
(let* ((context (make-html-render-context publication "first" target-url '() 2))81
(html (document->html-fragment first-doc context)))82
(assert-true (string-contains? html "href=\"/second/#destination\""))83
(assert-true (string-contains? html ">the destination</a>"))84
(assert-true (raises? (lambda ()85
(document->html-fragment first-doc86
(make-html-render-context publication "first"87
(lambda (target) "javascript:alert(1)") '() 2)))))88
(assert-true (raises? (lambda ()89
(document->html-fragment first-doc90
(make-html-render-context publication "first"91
(lambda (target) "//example.invalid/destination") '() 2)))))92
(for-each93
(lambda (unsafe-url)94
(assert-true (raises? (lambda ()95
(document->html-fragment first-doc96
(make-html-render-context publication "first"97
(lambda (target) unsafe-url) '() 2))))))98
(list "/\\evil" "/path\\evil" "/ path" "/\tpath" "/\rpath" "/\npath"))))100
(test "publication observations are immutable copies"101
(let ((targets (publication-index-targets publication))102
(loc-copy (publication-target-location (car (publication-index-targets publication)))))103
(set-car! targets "corrupt")104
(string-set! (srcloc-file loc-copy) 0 #\X)105
(assert-true (publication-target? (car (publication-index-targets publication))))106
(assert-equal "publication.sgd"107
(srcloc-file (publication-target-location (car (publication-index-targets publication)))))))109
(define (nested-section depth)110
(make-section (string-append "depth-" (number->string depth))111
(list (txt (string-append "Depth " (number->string depth))))112
(if (= depth 1)113
(list (make-paragraph (list (txt "bottom")) loc))114
(list (nested-section (- depth 1))))115
loc))117
(test "heading depth is explicit and never skips beyond h6"118
(let* ((deep-doc (make-document 'chapter "Deep" '() (list (nested-section 6)) loc))119
(deep-index (build-publication-index (list (make-publication-document "deep" deep-doc))))120
(context (make-html-render-context deep-index "deep" target-url '() 2)))121
(assert-true (raises? (lambda () (document->html-fragment deep-doc context))))))