forked from asciidoctor/asciidoctor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
links_test.rb
1218 lines (988 loc) · 48.5 KB
/
links_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
require_relative 'test_helper'
context 'Links' do
test 'qualified url inline with text' do
assert_xpath '//a[@href="http://asciidoc.org"][@class="bare"][text() = "http://asciidoc.org"]', convert_string('The AsciiDoc project is located at http://asciidoc.org.')
end
test 'qualified url with role inline with text' do
assert_xpath '//a[@href="http://asciidoc.org"][@class="bare project"][text() = "http://asciidoc.org"]', convert_string('The AsciiDoc project is located at http://asciidoc.org[role=project].')
end
test 'qualified http url inline with hide-uri-scheme set' do
assert_xpath '//a[@href="http://asciidoc.org"][@class="bare"][text() = "asciidoc.org"]', convert_string('The AsciiDoc project is located at http://asciidoc.org.', attributes: { 'hide-uri-scheme' => '' })
end
test 'qualified file url inline with label' do
assert_xpath '//a[@href="file:///home/user/bookmarks.html"][text() = "My Bookmarks"]', convert_string_to_embedded('file:///home/user/bookmarks.html[My Bookmarks]')
end
test 'qualified file url inline with hide-uri-scheme set' do
assert_xpath '//a[@href="file:///etc/app.conf"][text() = "/etc/app.conf"]', convert_string('Edit the configuration file link:file:///etc/app.conf[]', attributes: { 'hide-uri-scheme' => '' })
end
test 'should not hide bare URI scheme in implicit text of link macro when hide-uri-scheme is set' do
{
'link:https://[]' => 'https://',
'link:ssh://[]' => 'ssh://',
}.each do |input, expected|
assert_xpath %(/a[text() = "#{expected}"]), (convert_inline_string input, attributes: { 'hide-uri-scheme' => '' })
end
end
test 'qualified url with label' do
assert_xpath '//a[@href="http://asciidoc.org"][text() = "AsciiDoc"]', convert_string("We're parsing http://asciidoc.org[AsciiDoc] markup")
end
test 'qualified url with label containing escaped right square bracket' do
assert_xpath '//a[@href="http://asciidoc.org"][text() = "[Ascii]Doc"]', convert_string("We're parsing http://asciidoc.org[[Ascii\\]Doc] markup")
end
test 'qualified url with backslash label' do
assert_xpath '//a[@href="https://google.com"][text() = "Google for \\"]', convert_string('I advise you to https://google.com[Google for +\\+]')
end
test 'qualified url with label using link macro' do
assert_xpath '//a[@href="http://asciidoc.org"][text() = "AsciiDoc"]', convert_string("We're parsing link:http://asciidoc.org[AsciiDoc] markup")
end
test 'qualified url with role using link macro' do
assert_xpath '//a[@href="http://asciidoc.org"][@class="bare project"][text() = "http://asciidoc.org"]', convert_string("We're parsing link:http://asciidoc.org[role=project] markup")
end
test 'qualified url using macro syntax with multi-line label inline with text' do
assert_xpath %{//a[@href='http://asciidoc.org'][text() = 'AsciiDoc\nmarkup']}, convert_string("We're parsing link:http://asciidoc.org[AsciiDoc\nmarkup]")
end
test 'qualified url with label containing square brackets using link macro' do
str = 'http://example.com[[bracket1\]]'
doc = document_from_string str, standalone: false, doctype: 'inline'
assert_match '<a href="http://example.com">[bracket1]</a>', doc.convert, 1
doc = document_from_string str, standalone: false, backend: 'docbook', doctype: 'inline'
assert_match '<link xl:href="http://example.com">[bracket1]</link>', doc.convert, 1
end
test 'link macro with empty target' do
input = 'Link to link:[this page].'
output = convert_string_to_embedded input
assert_xpath '//a', output, 1
assert_xpath '//a[@href=""]', output, 1
end
test 'should not recognize link macro with double colons' do
input = 'The link::http://example.org[example domain] is reserved for tests and documentation.'
output = convert_string_to_embedded input
assert_includes output, 'link::http://example.org[example domain]'
end
test 'qualified url surrounded by angled brackets' do
assert_xpath '//a[@href="http://asciidoc.org"][text()="http://asciidoc.org"]', convert_string('<http://asciidoc.org> is the project page for AsciiDoc.'), 1
end
test 'qualified url surrounded by round brackets' do
assert_xpath '//a[@href="http://asciidoc.org"][text()="http://asciidoc.org"]', convert_string('(http://asciidoc.org) is the project page for AsciiDoc.'), 1
end
test 'qualified url with trailing period' do
result = convert_string_to_embedded 'The homepage for Asciidoctor is https://asciidoctor.org.'
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', result, 1
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]/following-sibling::text()[starts-with(.,".")]', result, 1
end
test 'qualified url with trailing explanation point' do
result = convert_string_to_embedded 'Check out https://asciidoctor.org!'
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', result, 1
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]/following-sibling::text()[starts-with(.,"!")]', result, 1
end
test 'qualified url with trailing question mark' do
result = convert_string_to_embedded 'Is the homepage for Asciidoctor https://asciidoctor.org?'
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', result, 1
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]/following-sibling::text()[starts-with(.,"?")]', result, 1
end
test 'qualified url with trailing round bracket' do
result = convert_string_to_embedded 'Asciidoctor is a Ruby-based AsciiDoc processor (see https://asciidoctor.org)'
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', result, 1
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]/following-sibling::text()[starts-with(.,")")]', result, 1
end
test 'qualified url with trailing period followed by round bracket' do
result = convert_string_to_embedded '(The homepage for Asciidoctor is https://asciidoctor.org.)'
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', result, 1
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]/following-sibling::text()[starts-with(.,".)")]', result, 1
end
test 'qualified url with trailing exclamation point followed by round bracket' do
result = convert_string_to_embedded '(Check out https://asciidoctor.org!)'
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', result, 1
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]/following-sibling::text()[starts-with(.,"!)")]', result, 1
end
test 'qualified url with trailing question mark followed by round bracket' do
result = convert_string_to_embedded '(Is the homepage for Asciidoctor https://asciidoctor.org?)'
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', result, 1
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]/following-sibling::text()[starts-with(.,"?)")]', result, 1
end
test 'qualified url with trailing semi-colon' do
result = convert_string_to_embedded 'https://asciidoctor.org; where text gets parsed'
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', result, 1
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]/following-sibling::text()[starts-with(.,";")]', result, 1
end
test 'qualified url with trailing colon' do
result = convert_string_to_embedded 'https://asciidoctor.org: where text gets parsed'
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', result, 1
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]/following-sibling::text()[starts-with(.,":")]', result, 1
end
test 'qualified url in round brackets with trailing colon' do
result = convert_string_to_embedded '(https://asciidoctor.org): where text gets parsed'
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', result, 1
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]/following-sibling::text()[starts-with(.,"):")]', result, 1
end
test 'qualified url with trailing round bracket followed by colon' do
result = convert_string_to_embedded '(from https://asciidoctor.org): where text gets parsed'
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', result, 1
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]/following-sibling::text()[starts-with(., "):")]', result, 1
end
test 'qualified url in round brackets with trailing semi-colon' do
result = convert_string_to_embedded '(https://asciidoctor.org); where text gets parsed'
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', result, 1
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]/following-sibling::text()[starts-with(., ");")]', result, 1
end
test 'qualified url with trailing round bracket followed by semi-colon' do
result = convert_string_to_embedded '(from https://asciidoctor.org); where text gets parsed'
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]', result, 1
assert_xpath '//a[@href="https://asciidoctor.org"][text()="https://asciidoctor.org"]/following-sibling::text()[starts-with(., ");")]', result, 1
end
test 'URI scheme with trailing characters should not be converted to a link' do
input_sources = %w(
(https://)
http://;
file://:
<ftp://>
)
expected_outputs = %w(
(https://)
http://;
file://:
<ftp://>
)
input_sources.each_with_index do |input_source, i|
expected_output = expected_outputs[i]
actual = block_from_string input_source
assert_equal expected_output, actual.content
end
end
test 'qualified url containing round brackets' do
assert_xpath '//a[@href="http://jruby.org/apidocs/org/jruby/Ruby.html#addModule(org.jruby.RubyModule)"][text()="addModule() adds a Ruby module"]', convert_string('http://jruby.org/apidocs/org/jruby/Ruby.html#addModule(org.jruby.RubyModule)[addModule() adds a Ruby module]'), 1
end
test 'qualified url adjacent to text in square brackets' do
assert_xpath '//a[@href="http://asciidoc.org"][text()="AsciiDoc"]', convert_string(']http://asciidoc.org[AsciiDoc] project page.'), 1
end
test 'qualified url adjacent to text in round brackets' do
assert_xpath '//a[@href="http://asciidoc.org"][text()="AsciiDoc"]', convert_string(')http://asciidoc.org[AsciiDoc] project page.'), 1
end
test 'qualified url following no-break space' do
assert_xpath '//a[@href="http://asciidoc.org"][text()="AsciiDoc"]', convert_string(%(#{[0xa0].pack 'U1'}http://asciidoc.org[AsciiDoc] project page.)), 1
end
test 'qualified url following smart apostrophe' do
output = convert_string_to_embedded 'l’http://www.irit.fr[IRIT]'
assert_match(/l’<a href=/, output)
end
test 'should convert qualified url as macro enclosed in double quotes' do
output = convert_string_to_embedded '"https://asciidoctor.org[]"'
assert_include '"<a href="https://asciidoctor.org" class="bare">https://asciidoctor.org</a>"', output
end
test 'should convert qualified url as macro enclosed in single quotes' do
output = convert_string_to_embedded '\'https://asciidoctor.org[]\''
assert_include '\'<a href="https://asciidoctor.org" class="bare">https://asciidoctor.org</a>\'', output
end
test 'qualified url using invalid link macro should not create link' do
assert_xpath '//a', convert_string('link:http://asciidoc.org is the project page for AsciiDoc.'), 0
end
test 'escaped inline qualified url should not create link' do
assert_xpath '//a', convert_string('\http://asciidoc.org is the project page for AsciiDoc.'), 0
end
test 'url in link macro with at (@) sign should not create mailto link' do
assert_xpath '//a[@href="http://xircles.codehaus.org/lists/[email protected]"][text()="subscribe"]', convert_string('http://xircles.codehaus.org/lists/[email protected][subscribe]')
end
test 'implicit url with at (@) sign should not create mailto link' do
assert_xpath '//a[@href="http://xircles.codehaus.org/lists/[email protected]"][text()="http://xircles.codehaus.org/lists/[email protected]"]', convert_string('http://xircles.codehaus.org/lists/[email protected]')
end
test 'escaped inline qualified url using macro syntax should not create link' do
assert_xpath '//a', convert_string('\http://asciidoc.org[AsciiDoc] is the key to good docs.'), 0
end
test 'inline qualified url followed by a newline should not include newline in link' do
assert_xpath '//a[@href="https://github.com/asciidoctor"]', convert_string("The source code for Asciidoctor can be found at https://github.com/asciidoctor\nwhich is a GitHub organization."), 1
end
test 'qualified url divided by newline using macro syntax should not create link' do
assert_xpath '//a', convert_string("The source code for Asciidoctor can be found at link:https://github.com/asciidoctor\n[]which is a GitHub organization."), 0
end
test 'qualified url containing whitespace using macro syntax should not create link' do
assert_xpath '//a', convert_string('I often need to refer to the chapter on link:http://asciidoc.org?q=attribute references[Attribute References].'), 0
end
test 'qualified url containing an encoded space using macro syntax should create a link' do
assert_xpath '//a', convert_string('I often need to refer to the chapter on link:http://asciidoc.org?q=attribute%20references[Attribute References].'), 1
end
test 'inline quoted qualified url should not consume surrounding angled brackets' do
assert_xpath '//a[@href="https://github.com/asciidoctor"]', convert_string('Asciidoctor GitHub organization: <**https://github.com/asciidoctor**>'), 1
end
test 'link with quoted text should not be separated into attributes when text contains an equal sign' do
assert_xpath '//a[@href="http://search.example.com"][text()="Google, Yahoo, Bing = Search Engines"]', convert_string_to_embedded('http://search.example.com["Google, Yahoo, Bing = Search Engines"]'), 1
end
test 'should leave link text as is if it contains an equals sign but no attributes are found' do
assert_xpath %(//a[@href="https://example.com"][text()="What You Need\n= What You Get"]), convert_string_to_embedded(%(https://example.com[What You Need\n= What You Get])), 1
end
test 'link with quoted text but no equal sign should carry quotes over to output' do
assert_xpath %(//a[@href="http://search.example.com"][text()='"Google, Yahoo, Bing"']), convert_string_to_embedded('http://search.example.com["Google, Yahoo, Bing"]'), 1
end
test 'link with comma in text but no equal sign should not be separated into attributes' do
assert_xpath '//a[@href="http://search.example.com"][text()="Google, Yahoo, Bing"]', convert_string_to_embedded('http://search.example.com[Google, Yahoo, Bing]'), 1
end
test 'link with formatted wrapped text should not be separated into attributes' do
result = convert_string_to_embedded %(https://example.com[[.role]#Foo\nBar#])
assert_include %(<a href="https://example.com"><span class="role">Foo\nBar</span></a>), result
end
test 'should process role and window attributes on link' do
assert_xpath '//a[@href="http://google.com"][@class="external"][@target="_blank"]', convert_string_to_embedded('http://google.com[Google, role=external, window="_blank"]'), 1
end
test 'should parse link with wrapped text that includes attributes' do
result = convert_string_to_embedded %(https://example.com[Foo\nBar,role=foobar])
assert_include %(<a href="https://example.com" class="foobar">Foo Bar</a>), result
end
test 'link macro with attributes but no text should use URL as text' do
url = 'https://fonts.googleapis.com/css?family=Roboto:400,400italic,'
assert_xpath %(//a[@href="#{url}"][text()="#{url}"]), convert_string_to_embedded(%(link:#{url}[family=Roboto,weight=400])), 1
end
test 'link macro with attributes but blank text should use URL as text' do
url = 'https://fonts.googleapis.com/css?family=Roboto:400,400italic,'
assert_xpath %(//a[@href="#{url}"][text()="#{url}"]), convert_string_to_embedded(%(link:#{url}[,family=Roboto,weight=400])), 1
end
test 'link macro with comma but no explicit attributes in text should not parse text' do
url = 'https://fonts.googleapis.com/css?family=Roboto:400,400italic,'
assert_xpath %(//a[@href="#{url}"][text()="Roboto,400"]), convert_string_to_embedded(%(link:#{url}[Roboto,400])), 1
end
test 'link macro should support id and role attributes' do
url = 'https://fonts.googleapis.com/css?family=Roboto:400'
assert_xpath %(//a[@href="#{url}"][@id="roboto-regular"][@class="bare font"][text()="#{url}"]), convert_string_to_embedded(%(link:#{url}[,id=roboto-regular,role=font])), 1
end
test 'link text that ends in ^ should set link window to _blank' do
assert_xpath '//a[@href="http://google.com"][@target="_blank"]', convert_string_to_embedded('http://google.com[Google^]'), 1
end
test 'rel=noopener should be added to a link that targets the _blank window' do
assert_xpath '//a[@href="http://google.com"][@target="_blank"][@rel="noopener"]', convert_string_to_embedded('http://google.com[Google^]'), 1
end
test 'rel=noopener should be added to a link that targets a named window when the noopener option is set' do
assert_xpath '//a[@href="http://google.com"][@target="name"][@rel="noopener"]', convert_string_to_embedded('http://google.com[Google,window=name,opts=noopener]'), 1
end
test 'rel=noopener should not be added to a link if it does not target a window' do
result = convert_string_to_embedded 'http://google.com[Google,opts=noopener]'
assert_xpath '//a[@href="http://google.com"]', result, 1
assert_xpath '//a[@href="http://google.com"][@rel="noopener"]', result, 0
end
test 'rel=nofollow should be added to a link when the nofollow option is set' do
assert_xpath '//a[@href="http://google.com"][@target="name"][@rel="nofollow noopener"]', convert_string_to_embedded('http://google.com[Google,window=name,opts="nofollow,noopener"]'), 1
end
test 'id attribute on link is processed' do
assert_xpath '//a[@href="http://google.com"][@id="link-1"]', convert_string_to_embedded('http://google.com[Google, id="link-1"]'), 1
end
test 'title attribute on link is processed' do
assert_xpath '//a[@href="http://google.com"][@title="title-1"]', convert_string_to_embedded('http://google.com[Google, title="title-1"]'), 1
end
test 'inline irc link' do
assert_xpath '//a[@href="irc://irc.freenode.net"][text()="irc://irc.freenode.net"]', convert_string_to_embedded('irc://irc.freenode.net'), 1
end
test 'inline irc link with text' do
assert_xpath '//a[@href="irc://irc.freenode.net"][text()="Freenode IRC"]', convert_string_to_embedded('irc://irc.freenode.net[Freenode IRC]'), 1
end
test 'inline ref' do
variations = %w([[tigers]] anchor:tigers[])
variations.each do |anchor|
doc = document_from_string %(Here you can read about tigers.#{anchor})
output = doc.convert
assert_kind_of Asciidoctor::Inline, doc.catalog[:refs]['tigers']
assert_nil doc.catalog[:refs]['tigers'].text
assert_xpath '//a[@id="tigers"]', output, 1
assert_xpath '//a[@id="tigers"]/child::text()', output, 0
end
end
test 'escaped inline ref' do
variations = %w([[tigers]] anchor:tigers[])
variations.each do |anchor|
doc = document_from_string %(Here you can read about tigers.\\#{anchor})
output = doc.convert
refute doc.catalog[:refs].key?('tigers')
assert_xpath '//a[@id="tigers"]', output, 0
end
end
test 'inline ref can start with colon' do
input = '[[:idname]] text'
output = convert_string_to_embedded input
assert_xpath '//a[@id=":idname"]', output, 1
end
test 'inline ref cannot start with digit' do
input = '[[1-install]] text'
output = convert_string_to_embedded input
assert_includes output, '[[1-install]]'
assert_xpath '//a[@id = "1-install"]', output, 0
end
test 'inline ref with reftext' do
%w([[tigers,Tigers]] anchor:tigers[Tigers]).each do |anchor|
doc = document_from_string %(Here you can read about tigers.#{anchor})
output = doc.convert
assert_kind_of Asciidoctor::Inline, doc.catalog[:refs]['tigers']
assert_equal 'Tigers', doc.catalog[:refs]['tigers'].text
assert_xpath '//a[@id="tigers"]', output, 1
assert_xpath '//a[@id="tigers"]/child::text()', output, 0
end
end
test 'should encode double quotes in reftext of anchor macro in DocBook output' do
input = 'anchor:uncola[the "un"-cola]'
result = convert_inline_string input, backend: :docbook
assert_equal '<anchor xml:id="uncola" xreflabel="the "un"-cola"/>', result
end
test 'should substitute attribute references in reftext when registering inline ref' do
%w([[tigers,{label-tigers}]] anchor:tigers[{label-tigers}]).each do |anchor|
doc = document_from_string %(Here you can read about tigers.#{anchor}), attributes: { 'label-tigers' => 'Tigers' }
doc.convert
assert_kind_of Asciidoctor::Inline, doc.catalog[:refs]['tigers']
assert_equal 'Tigers', doc.catalog[:refs]['tigers'].text
end
end
test 'inline ref with reftext converted to DocBook' do
%w([[tigers,<Tigers>]] anchor:tigers[<Tigers>]).each do |anchor|
doc = document_from_string %(Here you can read about tigers.#{anchor}), backend: :docbook
output = doc.convert standalone: false
assert_kind_of Asciidoctor::Inline, doc.catalog[:refs]['tigers']
assert_equal '<Tigers>', doc.catalog[:refs]['tigers'].text
assert_includes output, '<anchor xml:id="tigers" xreflabel="<Tigers>"/>'
end
end
test 'does not match bibliography anchor in prose when scanning for inline anchor' do
doc = document_from_string 'Use [[[label]]] to assign a label to a bibliography entry.'
refute doc.catalog[:refs].key? 'label'
end
test 'repeating inline anchor macro with empty reftext' do
input = 'anchor:one[] anchor:two[] anchor:three[]'
result = convert_inline_string input
assert_equal '<a id="one"></a> <a id="two"></a> <a id="three"></a>', result
end
test 'mixed inline anchor macro and anchor shorthand with empty reftext' do
input = 'anchor:one[][[two]]anchor:three[][[four]]anchor:five[]'
result = convert_inline_string input
assert_equal '<a id="one"></a><a id="two"></a><a id="three"></a><a id="four"></a><a id="five"></a>', result
end
test 'assigns xreflabel value for anchor macro without reftext in DocBook output' do
['anchor:foo[]bar', '[[foo]]bar'].each do |input|
result = convert_inline_string input, backend: :docbook
assert_equal '<anchor xml:id="foo" xreflabel="[foo]"/>bar', result
end
end
test 'unescapes square bracket in reftext of anchor macro' do
input = <<~'EOS'
see <<foo>>
anchor:foo[b[a\]r]tex
EOS
result = convert_string_to_embedded input
assert_includes result, 'see <a href="#foo">b[a]r</a>'
end
test 'unescapes square bracket in reftext of anchor macro in DocBook output' do
input = 'anchor:foo[b[a\]r]'
result = convert_inline_string input, backend: :docbook
assert_equal '<anchor xml:id="foo" xreflabel="b[a]r"/>', result
end
test 'xref using angled bracket syntax' do
doc = document_from_string '<<tigers>>'
doc.register :refs, ['tigers', (Asciidoctor::Inline.new doc, :anchor, '[tigers]', type: :ref, target: 'tigers'), '[tigers]']
assert_xpath '//a[@href="#tigers"][text() = "[tigers]"]', doc.convert, 1
end
test 'xref using angled bracket syntax with explicit hash' do
doc = document_from_string '<<#tigers>>'
doc.register :refs, ['tigers', (Asciidoctor::Inline.new doc, :anchor, 'Tigers', type: :ref, target: 'tigers'), 'Tigers']
assert_xpath '//a[@href="#tigers"][text() = "Tigers"]', doc.convert, 1
end
test 'xref using angled bracket syntax with label' do
input = <<~'EOS'
<<tigers,About Tigers>>
[#tigers]
== Tigers
EOS
assert_xpath '//a[@href="#tigers"][text() = "About Tigers"]', convert_string(input), 1
end
test 'xref should use title of target as link text when no explicit reftext is specified' do
input = <<~'EOS'
<<tigers>>
[#tigers]
== Tigers
EOS
assert_xpath '//a[@href="#tigers"][text() = "Tigers"]', convert_string(input), 1
end
test 'xref should use title of target as link text when explicit link text is empty' do
input = <<~'EOS'
<<tigers,>>
[#tigers]
== Tigers
EOS
assert_xpath '//a[@href="#tigers"][text() = "Tigers"]', convert_string(input), 1
end
test 'xref using angled bracket syntax with quoted label' do
input = <<~'EOS'
<<tigers,"About Tigers">>
[#tigers]
== Tigers
EOS
assert_xpath %q(//a[@href="#tigers"][text() = '"About Tigers"']), convert_string(input), 1
end
test 'should not interpret path sans extension in xref with angled bracket syntax in compat mode' do
using_memory_logger do |logger|
doc = document_from_string '<<tigers#>>', standalone: false, attributes: { 'compat-mode' => '' }
assert_xpath '//a[@href="#tigers#"][text() = "[tigers#]"]', doc.convert, 1
assert_empty logger
end
end
test 'xref using angled bracket syntax with path sans extension' do
doc = document_from_string '<<tigers#>>', standalone: false
assert_xpath '//a[@href="tigers.html"][text() = "tigers.html"]', doc.convert, 1
end
test 'inter-document xref shorthand syntax should assume AsciiDoc extension if AsciiDoc extension not present' do
{
'using-.net-web-services#' => 'Using .NET web services',
'asciidoctor.1#' => 'Asciidoctor Manual',
'path/to/document#' => 'Document Title',
}.each do |target, text|
result = convert_string_to_embedded %(<<#{target},#{text}>>)
assert_xpath %(//a[@href="#{target.chop}.html"][text()="#{text}"]), result, 1
end
end
test 'xref macro with explicit inter-document target should assume implicit AsciiDoc file extension if no file extension is present' do
{
'using-.net-web-services#' => 'Using .NET web services',
'asciidoctor.1#' => 'Asciidoctor Manual',
}.each do |target, text|
result = convert_string_to_embedded %(xref:#{target}[#{text}])
assert_xpath %(//a[@href="#{target.chop}"][text()="#{text}"]), result, 1
end
{
'document#' => 'Document Title',
'path/to/document#' => 'Document Title',
'include.d/document#' => 'Document Title',
}.each do |target, text|
result = convert_string_to_embedded %(xref:#{target}[#{text}])
assert_xpath %(//a[@href="#{target.chop}.html"][text()="#{text}"]), result, 1
end
end
test 'xref macro with implicit inter-document target should preserve path with file extension' do
{
'refcard.pdf' => 'Refcard',
'asciidoctor.1' => 'Asciidoctor Manual',
}.each do |path, text|
result = convert_string_to_embedded %(xref:#{path}[#{text}])
assert_xpath %(//a[@href="#{path}"][text()="#{text}"]), result, 1
end
{
'sections.d/first' => 'First Section',
}.each do |path, text|
result = convert_string_to_embedded %(xref:#{path}[#{text}])
assert_xpath %(//a[@href="##{path}"][text()="#{text}"]), result, 1
end
end
test 'inter-document xref should only remove the file extension part if the path contains a period elsewhere' do
result = convert_string_to_embedded '<<using-.net-web-services.adoc#,Using .NET web services>>'
assert_xpath '//a[@href="using-.net-web-services.html"][text() = "Using .NET web services"]', result, 1
end
test 'xref macro target containing dot should be interpreted as a path unless prefixed by #' do
result = convert_string_to_embedded 'xref:using-.net-web-services[Using .NET web services]'
assert_xpath '//a[@href="using-.net-web-services"][text() = "Using .NET web services"]', result, 1
result = convert_string_to_embedded 'xref:#using-.net-web-services[Using .NET web services]'
assert_xpath '//a[@href="#using-.net-web-services"][text() = "Using .NET web services"]', result, 1
end
test 'should not interpret double underscore in target of xref macro if sequence is preceded by a backslash' do
result = convert_string_to_embedded 'xref:doc\__with_double__underscore.adoc[text]'
assert_xpath '//a[@href="doc__with_double__underscore.html"][text() = "text"]', result, 1
end
test 'should not interpret double underscore in target of xref shorthand if sequence is preceded by a backslash' do
result = convert_string_to_embedded '<<doc\__with_double__underscore.adoc#,text>>'
assert_xpath '//a[@href="doc__with_double__underscore.html"][text() = "text"]', result, 1
end
test 'xref using angled bracket syntax with path sans extension using docbook backend' do
doc = document_from_string '<<tigers#>>', standalone: false, backend: 'docbook'
assert_match '<link xl:href="tigers.xml">tigers.xml</link>', doc.convert, 1
end
test 'xref using angled bracket syntax with ancestor path sans extension' do
doc = document_from_string '<<../tigers#,tigers>>', standalone: false
assert_xpath '//a[@href="../tigers.html"][text() = "tigers"]', doc.convert, 1
end
test 'xref using angled bracket syntax with absolute path sans extension' do
doc = document_from_string '<</path/to/tigers#,tigers>>', standalone: false
assert_xpath '//a[@href="/path/to/tigers.html"][text() = "tigers"]', doc.convert, 1
end
test 'xref using angled bracket syntax with path and extension' do
using_memory_logger do |logger|
doc = document_from_string '<<tigers.adoc>>', standalone: false
assert_xpath '//a[@href="#tigers.adoc"][text() = "[tigers.adoc]"]', doc.convert, 1
assert_empty logger
end
end
test 'xref using angled bracket syntax with path and extension with hash' do
doc = document_from_string '<<tigers.adoc#>>', standalone: false
assert_xpath '//a[@href="tigers.html"][text() = "tigers.html"]', doc.convert, 1
end
test 'xref using angled bracket syntax with path and extension with fragment' do
doc = document_from_string '<<tigers.adoc#id>>', standalone: false
assert_xpath '//a[@href="tigers.html#id"][text() = "tigers.html"]', doc.convert, 1
end
test 'xref using macro syntax with path and extension in compat mode' do
using_memory_logger do |logger|
doc = document_from_string 'xref:tigers.adoc[]', standalone: false, attributes: { 'compat-mode' => '' }
assert_xpath '//a[@href="#tigers.adoc"][text() = "[tigers.adoc]"]', doc.convert, 1
assert_empty logger
end
end
test 'xref using macro syntax with path and extension' do
doc = document_from_string 'xref:tigers.adoc[]', standalone: false
assert_xpath '//a[@href="tigers.html"][text() = "tigers.html"]', doc.convert, 1
end
test 'xref using angled bracket syntax with path and fragment' do
doc = document_from_string '<<tigers#about>>', standalone: false
assert_xpath '//a[@href="tigers.html#about"][text() = "tigers.html"]', doc.convert, 1
end
test 'xref using angled bracket syntax with path, fragment and text' do
doc = document_from_string '<<tigers#about,About Tigers>>', standalone: false
assert_xpath '//a[@href="tigers.html#about"][text() = "About Tigers"]', doc.convert, 1
end
test 'xref using angled bracket syntax with path and custom relfilesuffix and outfilesuffix' do
attributes = { 'relfileprefix' => '../', 'outfilesuffix' => '/' }
doc = document_from_string '<<tigers#about,About Tigers>>', standalone: false, attributes: attributes
assert_xpath '//a[@href="../tigers/#about"][text() = "About Tigers"]', doc.convert, 1
end
test 'xref using angled bracket syntax with path and custom relfilesuffix' do
attributes = { 'relfilesuffix' => '/' }
doc = document_from_string '<<tigers#about,About Tigers>>', standalone: false, attributes: attributes
assert_xpath '//a[@href="tigers/#about"][text() = "About Tigers"]', doc.convert, 1
end
test 'xref using angled bracket syntax with path which has been included in this document' do
using_memory_logger do |logger|
in_verbose_mode do
doc = document_from_string '<<tigers#about,About Tigers>>', standalone: false
doc.catalog[:includes]['tigers'] = true
output = doc.convert
assert_xpath '//a[@href="#about"][text() = "About Tigers"]', output, 1
assert_message logger, :INFO, 'possible invalid reference: about'
end
end
end
test 'xref using angled bracket syntax with nested path which has been included in this document' do
using_memory_logger do |logger|
in_verbose_mode do
doc = document_from_string '<<part1/tigers#about,About Tigers>>', standalone: false
doc.catalog[:includes]['part1/tigers'] = true
output = doc.convert
assert_xpath '//a[@href="#about"][text() = "About Tigers"]', output, 1
assert_message logger, :INFO, 'possible invalid reference: about'
end
end
end
test 'xref using angled bracket syntax inline with text' do
input = <<~'EOS'
Want to learn <<tigers,about tigers>>?
[#tigers]
== Tigers
EOS
assert_xpath '//a[@href="#tigers"][text() = "about tigers"]', convert_string(input), 1
end
test 'xref using angled bracket syntax with multi-line label inline with text' do
input = <<~'EOS'
Want to learn <<tigers,about
tigers>>?
[#tigers]
== Tigers
EOS
assert_xpath %{//a[@href="#tigers"][normalize-space(text()) = "about tigers"]}, convert_string(input), 1
end
test 'xref with escaped text' do
# when \x0 was used as boundary character for passthrough, it was getting stripped
# now using unicode marks as boundary characters, which resolves issue
input = <<~'EOS'
See the <<tigers, `+[tigers]+`>> section for details about tigers.
[#tigers]
== Tigers
EOS
output = convert_string_to_embedded input
assert_xpath %(//a[@href="#tigers"]/code[text()="[tigers]"]), output, 1
end
test 'xref with target that begins with attribute reference in title' do
['<<{lessonsdir}/lesson-1#,Lesson 1>>', 'xref:{lessonsdir}/lesson-1.adoc[Lesson 1]'].each do |xref|
input = <<~EOS
:lessonsdir: lessons
[#lesson-1-listing]
== #{xref}
A summary of the first lesson.
EOS
output = convert_string_to_embedded input
assert_xpath '//h2/a[@href="lessons/lesson-1.html"]', output, 1
end
end
test 'xref using macro syntax' do
doc = document_from_string 'xref:tigers[]'
doc.register :refs, ['tigers', (Asciidoctor::Inline.new doc, :anchor, '[tigers]', type: :ref, target: 'tigers'), '[tigers]']
assert_xpath '//a[@href="#tigers"][text() = "[tigers]"]', doc.convert, 1
end
test 'multiple xref macros with implicit text in single line' do
input = <<~'EOS'
This document has two sections, xref:sect-a[] and xref:sect-b[].
[#sect-a]
== Section A
[#sect-b]
== Section B
EOS
result = convert_string_to_embedded input
assert_xpath '//a[@href="#sect-a"][text() = "Section A"]', result, 1
assert_xpath '//a[@href="#sect-b"][text() = "Section B"]', result, 1
end
test 'xref using macro syntax with explicit hash' do
doc = document_from_string 'xref:#tigers[]'
doc.register :refs, ['tigers', (Asciidoctor::Inline.new doc, :anchor, 'Tigers', type: :ref, target: 'tigers'), 'Tigers']
assert_xpath '//a[@href="#tigers"][text() = "Tigers"]', doc.convert, 1
end
test 'xref using macro syntax with label' do
input = <<~'EOS'
xref:tigers[About Tigers]
[#tigers]
== Tigers
EOS
assert_xpath '//a[@href="#tigers"][text() = "About Tigers"]', convert_string(input), 1
end
test 'xref using macro syntax inline with text' do
input = <<~'EOS'
Want to learn xref:tigers[about tigers]?
[#tigers]
== Tigers
EOS
assert_xpath '//a[@href="#tigers"][text() = "about tigers"]', convert_string(input), 1
end
test 'xref using macro syntax with multi-line label inline with text' do
input = <<~'EOS'
Want to learn xref:tigers[about
tigers]?
[#tigers]
== Tigers
EOS
assert_xpath %{//a[@href="#tigers"][normalize-space(text()) = "about tigers"]}, convert_string(input), 1
end
test 'xref using macro syntax with text that ends with an escaped closing bracket' do
input = <<~'EOS'
xref:tigers[[tigers\]]
[#tigers]
== Tigers
EOS
assert_xpath '//a[@href="#tigers"][text() = "[tigers]"]', convert_string_to_embedded(input), 1
end
test 'xref using macro syntax with text that contains an escaped closing bracket' do
input = <<~'EOS'
xref:tigers[[tigers\] are cats]
[#tigers]
== Tigers
EOS
assert_xpath '//a[@href="#tigers"][text() = "[tigers] are cats"]', convert_string_to_embedded(input), 1
end
test 'unescapes square bracket in reftext used by xref' do
input = <<~'EOS'
anchor:foo[b[a\]r]about
see <<foo>>
EOS
result = convert_string_to_embedded input
assert_xpath '//a[@href="#foo"]', result, 1
assert_xpath '//a[@href="#foo"][text()="b[a]r"]', result, 1
end
test 'xref using invalid macro syntax does not create link' do
doc = document_from_string 'xref:tigers'
doc.register :refs, ['tigers', (Asciidoctor::Inline.new doc, :anchor, 'Tigers', type: :ref, target: 'tigers'), 'Tigers']
assert_xpath '//a', doc.convert, 0
end
test 'should warn and create link if verbose flag is set and reference is not found' do
input = <<~'EOS'
[#foobar]
== Foobar
== Section B
See <<foobaz>>.
EOS
using_memory_logger do |logger|
in_verbose_mode do
output = convert_string_to_embedded input
assert_xpath '//a[@href="#foobaz"][text() = "[foobaz]"]', output, 1
assert_message logger, :INFO, 'possible invalid reference: foobaz'
end
end
end
test 'should not warn if verbose flag is set and reference is found in compat mode' do
input = <<~'EOS'
[[foobar]]
== Foobar
== Section B
See <<foobar>>.
EOS
using_memory_logger do |logger|
in_verbose_mode do
output = convert_string_to_embedded input, attributes: { 'compat-mode' => '' }
assert_xpath '//a[@href="#foobar"][text() = "Foobar"]', output, 1
assert_empty logger
end
end
end
test 'should warn and create link if verbose flag is set and reference using # notation is not found' do
input = <<~'EOS'
[#foobar]
== Foobar
== Section B
See <<#foobaz>>.
EOS
using_memory_logger do |logger|
in_verbose_mode do
output = convert_string_to_embedded input
assert_xpath '//a[@href="#foobaz"][text() = "[foobaz]"]', output, 1
assert_message logger, :INFO, 'possible invalid reference: foobaz'
end
end
end
test 'should produce an internal anchor from an inter-document xref to file included into current file' do
input = <<~'EOS'
= Book Title
:doctype: book
[#ch1]
== Chapter 1
So it begins.
Read <<other-chapters.adoc#ch2>> to find out what happens next!
include::other-chapters.adoc[]
EOS
doc = document_from_string input, safe: :safe, base_dir: fixturedir
assert doc.catalog[:includes].key?('other-chapters')
assert doc.catalog[:includes]['other-chapters']
output = doc.convert
assert_xpath '//a[@href="#ch2"][text()="Chapter 2"]', output, 1
end
test 'should produce an internal anchor from an inter-document xref to file included entirely into current file using tags' do
input = <<~'EOS'
= Book Title
:doctype: book
[#ch1]
== Chapter 1
So it begins.
Read <<other-chapters.adoc#ch2>> to find out what happens next!
include::other-chapters.adoc[tags=**]
EOS
output = convert_string_to_embedded input, safe: :safe, base_dir: fixturedir
assert_xpath '//a[@href="#ch2"][text()="Chapter 2"]', output, 1
end
test 'should not produce an internal anchor for inter-document xref to file partially included into current file' do
input = <<~'EOS'
= Book Title
:doctype: book
[#ch1]
== Chapter 1
So it begins.
Read <<other-chapters.adoc#ch2,the next chapter>> to find out what happens next!
include::other-chapters.adoc[tags=ch2]
EOS
doc = document_from_string input, safe: :safe, base_dir: fixturedir
assert doc.catalog[:includes].key?('other-chapters')
refute doc.catalog[:includes]['other-chapters']
output = doc.convert
assert_xpath '//a[@href="other-chapters.html#ch2"][text()="the next chapter"]', output, 1
end
test 'should warn and create link if debug mode is enabled, inter-document xref points to current doc, and reference not found' do
input = <<~'EOS'
[#foobar]
== Foobar
== Section B
See <<test.adoc#foobaz>>.
EOS
using_memory_logger do |logger|
in_verbose_mode do
output = convert_string_to_embedded input, attributes: { 'docname' => 'test' }
assert_xpath '//a[@href="#foobaz"][text() = "[foobaz]"]', output, 1
assert_message logger, :INFO, 'possible invalid reference: foobaz'
end
end
end
test 'should use doctitle as fallback link text if inter-document xref points to current doc and no link text is provided' do
input = <<~'EOS'
= Links & Stuff at https://example.org
See xref:test.adoc[]
EOS
output = convert_string_to_embedded input, attributes: { 'docname' => 'test' }
assert_include '<a href="#">Links & Stuff at https://example.org</a>', output
end
test 'should use doctitle of root document as fallback link text for inter-document xref in AsciiDoc table cell that resolves to current doc' do
input = <<~'EOS'
= Document Title
|===
a|See xref:test.adoc[]
|===
EOS
output = convert_string_to_embedded input, attributes: { 'docname' => 'test' }
assert_include '<a href="#">Document Title</a>', output
end
test 'should use reftext on document as fallback link text if inter-document xref points to current doc and no link text is provided' do
input = <<~'EOS'
[reftext="Links and Stuff"]
= Links & Stuff
See xref:test.adoc[]
EOS
output = convert_string_to_embedded input, attributes: { 'docname' => 'test' }
assert_include '<a href="#">Links and Stuff</a>', output
end
test 'should use reftext on document as fallback link text if xref points to empty fragment and no link text is provided' do
input = <<~'EOS'
[reftext="Links and Stuff"]
= Links & Stuff
See xref:#[]
EOS