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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
|
# frozen_string_literal: true
require_relative 'test_helper'
context 'Attributes' do
default_logger = Asciidoctor::LoggerManager.logger
setup do
Asciidoctor::LoggerManager.logger = (@logger = Asciidoctor::MemoryLogger.new)
end
teardown do
Asciidoctor::LoggerManager.logger = default_logger
end
context 'Assignment' do
test 'creates an attribute' do
doc = document_from_string(':frog: Tanglefoot')
assert_equal 'Tanglefoot', doc.attributes['frog']
end
test 'requires a space after colon following attribute name' do
doc = document_from_string 'foo:bar'
assert_nil doc.attributes['foo']
end
# NOTE AsciiDoc.py recognizes this entry
test 'does not recognize attribute entry if name contains colon' do
input = ':foo:bar: baz'
doc = document_from_string input
refute doc.attr?('foo:bar')
assert_equal 1, doc.blocks.size
assert_equal :paragraph, doc.blocks[0].context
end
# NOTE AsciiDoc.py recognizes this entry
test 'does not recognize attribute entry if name ends with colon' do
input = ':foo:: bar'
doc = document_from_string input
refute doc.attr?('foo:')
assert_equal 1, doc.blocks.size
assert_equal :dlist, doc.blocks[0].context
end
# NOTE AsciiDoc.py does not recognize this entry
test 'allows any word character defined by Unicode in an attribute name' do
[['café', 'a coffee shop'], ['سمن', %(سازمان مردمنهاد)]].each do |(name, value)|
str = <<~EOS
:#{name}: #{value}
{#{name}}
EOS
result = convert_string_to_embedded str
assert_includes result, %(<p>#{value}</p>)
end
end
test 'creates an attribute by fusing a legacy multi-line value' do
str = <<~'EOS'
:description: This is the first +
Ruby implementation of +
AsciiDoc.
EOS
doc = document_from_string(str)
assert_equal 'This is the first Ruby implementation of AsciiDoc.', doc.attributes['description']
end
test 'creates an attribute by fusing a multi-line value' do
str = <<~'EOS'
:description: This is the first \
Ruby implementation of \
AsciiDoc.
EOS
doc = document_from_string(str)
assert_equal 'This is the first Ruby implementation of AsciiDoc.', doc.attributes['description']
end
test 'honors line break characters in multi-line values' do
str = <<~'EOS'
:signature: Linus Torvalds + \
Linux Hacker + \
linus.torvalds@example.com
EOS
doc = document_from_string(str)
assert_equal %(Linus Torvalds +\nLinux Hacker +\nlinus.torvalds@example.com), doc.attributes['signature']
end
test 'should allow pass macro to surround a multi-line value that contains line breaks' do
str = <<~'EOS'
:signature: pass:a[{author} + \
{title} + \
{email}]
EOS
doc = document_from_string str, attributes: { 'author' => 'Linus Torvalds', 'title' => 'Linux Hacker', 'email' => 'linus.torvalds@example.com' }
assert_equal %(Linus Torvalds +\nLinux Hacker +\nlinus.torvalds@example.com), (doc.attr 'signature')
end
test 'should delete an attribute that ends with !' do
doc = document_from_string(":frog: Tanglefoot\n:frog!:")
assert_nil doc.attributes['frog']
end
test 'should delete an attribute that ends with ! set via API' do
doc = document_from_string(":frog: Tanglefoot", attributes: { 'frog!' => '' })
assert_nil doc.attributes['frog']
end
test 'should delete an attribute that begins with !' do
doc = document_from_string(":frog: Tanglefoot\n:!frog:")
assert_nil doc.attributes['frog']
end
test 'should delete an attribute that begins with ! set via API' do
doc = document_from_string(":frog: Tanglefoot", attributes: { '!frog' => '' })
assert_nil doc.attributes['frog']
end
test 'should delete an attribute set via API to nil value' do
doc = document_from_string(":frog: Tanglefoot", attributes: { 'frog' => nil })
assert_nil doc.attributes['frog']
end
test "doesn't choke when deleting a non-existing attribute" do
doc = document_from_string(':frog!:')
assert_nil doc.attributes['frog']
end
test "replaces special characters in attribute value" do
doc = document_from_string(":xml-busters: <>&")
assert_equal '<>&', doc.attributes['xml-busters']
end
test "performs attribute substitution on attribute value" do
doc = document_from_string(":version: 1.0\n:release: Asciidoctor {version}")
assert_equal 'Asciidoctor 1.0', doc.attributes['release']
end
test 'assigns attribute to empty string if substitution fails to resolve attribute' do
input = ':release: Asciidoctor {version}'
document_from_string input, attributes: { 'attribute-missing' => 'drop-line' }
assert_message @logger, :INFO, 'dropping line containing reference to missing attribute: version'
end
test 'assigns multi-line attribute to empty string if substitution fails to resolve attribute' do
input = <<~'EOS'
:release: Asciidoctor +
{version}
EOS
doc = document_from_string input, attributes: { 'attribute-missing' => 'drop-line' }
assert_equal '', doc.attributes['release']
assert_message @logger, :INFO, 'dropping line containing reference to missing attribute: version'
end
test 'resolves attributes inside attribute value within header' do
input = <<~'EOS'
= Document Title
:big: big
:bigfoot: {big}foot
{bigfoot}
EOS
result = convert_string_to_embedded input
assert_includes result, 'bigfoot'
end
test 'resolves attributes and pass macro inside attribute value outside header' do
input = <<~'EOS'
= Document Title
content
:big: pass:a,q[_big_]
:bigfoot: {big}foot
{bigfoot}
EOS
result = convert_string_to_embedded input
assert_includes result, '<em>big</em>foot'
end
test 'should limit maximum size of attribute value if safe mode is SECURE' do
expected = 'a' * 4096
input = <<~EOS
:name: #{'a' * 5000}
{name}
EOS
result = convert_inline_string input
assert_equal expected, result
assert_equal 4096, result.bytesize
end
test 'should handle multibyte characters when limiting attribute value size' do
expected = '日本'
input = <<~'EOS'
:name: 日本語
{name}
EOS
result = convert_inline_string input, attributes: { 'max-attribute-value-size' => 6 }
assert_equal expected, result
assert_equal 6, result.bytesize
end
test 'should not mangle multibyte characters when limiting attribute value size' do
expected = '日本'
input = <<~'EOS'
:name: 日本語
{name}
EOS
result = convert_inline_string input, attributes: { 'max-attribute-value-size' => 8 }
assert_equal expected, result
assert_equal 6, result.bytesize
end
test 'should allow maximize size of attribute value to be disabled' do
expected = 'a' * 5000
input = <<~EOS
:name: #{'a' * 5000}
{name}
EOS
result = convert_inline_string input, attributes: { 'max-attribute-value-size' => nil }
assert_equal expected, result
assert_equal 5000, result.bytesize
end
test 'resolves user-home attribute if safe mode is less than SERVER' do
input = <<~'EOS'
:imagesdir: {user-home}/etc/images
{imagesdir}
EOS
output = convert_inline_string input, safe: :safe
assert_equal %(#{Asciidoctor::USER_HOME}/etc/images), output
end
test 'user-home attribute resolves to . if safe mode is SERVER or greater' do
input = <<~'EOS'
:imagesdir: {user-home}/etc/images
{imagesdir}
EOS
output = convert_inline_string input, safe: :server
assert_equal './etc/images', output
end
test 'user-home attribute can be overridden by API if safe mode is less than SERVER' do
input = <<~'EOS'
Go {user-home}!
EOS
output = convert_inline_string input, attributes: { 'user-home' => '/home' }
assert_equal 'Go /home!', output
end
test 'user-home attribute can be overridden by API if safe mode is SERVER or greater' do
input = <<~'EOS'
Go {user-home}!
EOS
output = convert_inline_string input, safe: :server, attributes: { 'user-home' => '/home' }
assert_equal 'Go /home!', output
end
test "apply custom substitutions to text in passthrough macro and assign to attribute" do
doc = document_from_string(":xml-busters: pass:[<>&]")
assert_equal '<>&', doc.attributes['xml-busters']
doc = document_from_string(":xml-busters: pass:none[<>&]")
assert_equal '<>&', doc.attributes['xml-busters']
doc = document_from_string(":xml-busters: pass:specialcharacters[<>&]")
assert_equal '<>&', doc.attributes['xml-busters']
doc = document_from_string(":xml-busters: pass:n,-c[<(C)>]")
assert_equal '<©>', doc.attributes['xml-busters']
end
test 'should not recognize pass macro with invalid substitution list in attribute value' do
[',', '42', 'a,'].each do |subs|
doc = document_from_string %(:pass-fail: pass:#{subs}[whale])
assert_equal %(pass:#{subs}[whale]), doc.attributes['pass-fail']
end
end
test "attribute is treated as defined until it's not" do
input = <<~'EOS'
:holygrail:
ifdef::holygrail[]
The holy grail has been found!
endif::holygrail[]
:holygrail!:
ifndef::holygrail[]
Buggers! What happened to the grail?
endif::holygrail[]
EOS
output = convert_string input
assert_xpath '//p', output, 2
assert_xpath '(//p)[1][text() = "The holy grail has been found!"]', output, 1
assert_xpath '(//p)[2][text() = "Buggers! What happened to the grail?"]', output, 1
end
test 'attribute set via API overrides attribute set in document' do
doc = document_from_string(':cash: money', attributes: { 'cash' => 'heroes' })
assert_equal 'heroes', doc.attributes['cash']
end
test 'attribute set via API cannot be unset by document' do
doc = document_from_string(':cash!:', attributes: { 'cash' => 'heroes' })
assert_equal 'heroes', doc.attributes['cash']
end
test 'attribute soft set via API using modifier on name can be overridden by document' do
doc = document_from_string(':cash: money', attributes: { 'cash@' => 'heroes' })
assert_equal 'money', doc.attributes['cash']
end
test 'attribute soft set via API using modifier on value can be overridden by document' do
doc = document_from_string(':cash: money', attributes: { 'cash' => 'heroes@' })
assert_equal 'money', doc.attributes['cash']
end
test 'attribute soft set via API using modifier on name can be unset by document' do
doc = document_from_string(':cash!:', attributes: { 'cash@' => 'heroes' })
assert_nil doc.attributes['cash']
doc = document_from_string(':cash!:', attributes: { 'cash@' => true })
assert_nil doc.attributes['cash']
end
test 'attribute soft set via API using modifier on value can be unset by document' do
doc = document_from_string(':cash!:', attributes: { 'cash' => 'heroes@' })
assert_nil doc.attributes['cash']
end
test 'attribute unset via API cannot be set by document' do
[
{ 'cash!' => '' },
{ '!cash' => '' },
{ 'cash' => nil },
].each do |attributes|
doc = document_from_string(':cash: money', attributes: attributes)
assert_nil doc.attributes['cash']
end
end
test 'attribute soft unset via API can be set by document' do
[
{ 'cash!@' => '' },
{ '!cash@' => '' },
{ 'cash!' => '@' },
{ '!cash' => '@' },
{ 'cash' => false },
].each do |attributes|
doc = document_from_string(':cash: money', attributes: attributes)
assert_equal 'money', doc.attributes['cash']
end
end
test 'can soft unset built-in attribute from API and still override in document' do
[
{ 'sectids!@' => '' },
{ '!sectids@' => '' },
{ 'sectids!' => '@' },
{ '!sectids' => '@' },
{ 'sectids' => false },
].each do |attributes|
doc = document_from_string '== Heading', attributes: attributes
refute doc.attr?('sectids')
assert_css '#_heading', (doc.convert standalone: false), 0
doc = document_from_string %(:sectids:\n\n== Heading), attributes: attributes
assert doc.attr?('sectids')
assert_css '#_heading', (doc.convert standalone: false), 1
end
end
test 'backend and doctype attributes are set by default in default configuration' do
input = <<~'EOS'
= Document Title
Author Name
content
EOS
doc = document_from_string input
expect = {
'backend' => 'html5',
'backend-html5' => '',
'backend-html5-doctype-article' => '',
'outfilesuffix' => '.html',
'basebackend' => 'html',
'basebackend-html' => '',
'basebackend-html-doctype-article' => '',
'doctype' => 'article',
'doctype-article' => '',
'filetype' => 'html',
'filetype-html' => '',
}
expect.each do |key, val|
assert doc.attributes.key? key
assert_equal val, doc.attributes[key]
end
end
test 'backend and doctype attributes are set by default in custom configuration' do
input = <<~'EOS'
= Document Title
Author Name
content
EOS
doc = document_from_string input, doctype: 'book', backend: 'docbook'
expect = {
'backend' => 'docbook5',
'backend-docbook5' => '',
'backend-docbook5-doctype-book' => '',
'outfilesuffix' => '.xml',
'basebackend' => 'docbook',
'basebackend-docbook' => '',
'basebackend-docbook-doctype-book' => '',
'doctype' => 'book',
'doctype-book' => '',
'filetype' => 'xml',
'filetype-xml' => '',
}
expect.each do |key, val|
assert doc.attributes.key? key
assert_equal val, doc.attributes[key]
end
end
test 'backend attributes are updated if backend attribute is defined in document and safe mode is less than SERVER' do
input = <<~'EOS'
= Document Title
Author Name
:backend: docbook
:doctype: book
content
EOS
doc = document_from_string input, safe: Asciidoctor::SafeMode::SAFE
expect = {
'backend' => 'docbook5',
'backend-docbook5' => '',
'backend-docbook5-doctype-book' => '',
'outfilesuffix' => '.xml',
'basebackend' => 'docbook',
'basebackend-docbook' => '',
'basebackend-docbook-doctype-book' => '',
'doctype' => 'book',
'doctype-book' => '',
'filetype' => 'xml',
'filetype-xml' => '',
}
expect.each do |key, val|
assert doc.attributes.key?(key)
assert_equal val, doc.attributes[key]
end
refute doc.attributes.key?('backend-html5')
refute doc.attributes.key?('backend-html5-doctype-article')
refute doc.attributes.key?('basebackend-html')
refute doc.attributes.key?('basebackend-html-doctype-article')
refute doc.attributes.key?('doctype-article')
refute doc.attributes.key?('filetype-html')
end
test 'backend attributes defined in document options overrides backend attribute in document' do
doc = document_from_string(':backend: docbook5', safe: Asciidoctor::SafeMode::SAFE, attributes: { 'backend' => 'html5' })
assert_equal 'html5', doc.attributes['backend']
assert doc.attributes.key? 'backend-html5'
assert_equal 'html', doc.attributes['basebackend']
assert doc.attributes.key? 'basebackend-html'
end
test 'can only access a positional attribute from the attributes hash' do
node = Asciidoctor::Block.new nil, :paragraph, attributes: { 1 => 'position 1' }
assert_nil node.attr(1)
refute node.attr?(1)
assert_equal 'position 1', node.attributes[1]
end
test 'attr should not retrieve attribute from document if not set on block' do
doc = document_from_string 'paragraph', attributes: { 'name' => 'value' }
para = doc.blocks[0]
assert_nil para.attr 'name'
end
test 'attr looks for attribute on document if fallback name is true' do
doc = document_from_string 'paragraph', attributes: { 'name' => 'value' }
para = doc.blocks[0]
assert_equal 'value', (para.attr 'name', nil, true)
end
test 'attr uses fallback name when looking for attribute on document' do
doc = document_from_string 'paragraph', attributes: { 'alt-name' => 'value' }
para = doc.blocks[0]
assert_equal 'value', (para.attr 'name', nil, 'alt-name')
end
test 'attr? should not check for attribute on document if not set on block' do
doc = document_from_string 'paragraph', attributes: { 'name' => 'value' }
para = doc.blocks[0]
refute para.attr? 'name'
end
test 'attr? checks for attribute on document if fallback name is true' do
doc = document_from_string 'paragraph', attributes: { 'name' => 'value' }
para = doc.blocks[0]
assert para.attr? 'name', nil, true
end
test 'attr? checks for fallback name when looking for attribute on document' do
doc = document_from_string 'paragraph', attributes: { 'alt-name' => 'value' }
para = doc.blocks[0]
assert para.attr? 'name', nil, 'alt-name'
end
test 'set_attr should set value to empty string if no value is specified' do
node = Asciidoctor::Block.new nil, :paragraph, attributes: {}
node.set_attr 'foo'
assert_equal '', (node.attr 'foo')
end
test 'remove_attr should remove attribute and return previous value' do
doc = empty_document
node = Asciidoctor::Block.new doc, :paragraph, attributes: { 'foo' => 'bar' }
assert_equal 'bar', (node.remove_attr 'foo')
assert_nil node.attr('foo')
end
test 'set_attr should not overwrite existing key if overwrite is false' do
node = Asciidoctor::Block.new nil, :paragraph, attributes: { 'foo' => 'bar' }
assert_equal 'bar', (node.attr 'foo')
node.set_attr 'foo', 'baz', false
assert_equal 'bar', (node.attr 'foo')
end
test 'set_attr should overwrite existing key by default' do
node = Asciidoctor::Block.new nil, :paragraph, attributes: { 'foo' => 'bar' }
assert_equal 'bar', (node.attr 'foo')
node.set_attr 'foo', 'baz'
assert_equal 'baz', (node.attr 'foo')
end
test 'set_attr should set header attribute in loaded document' do
input = <<~'EOS'
:uri: http://example.org
{uri}
EOS
doc = Asciidoctor.load input, attributes: { 'uri' => 'https://github.com' }
doc.set_attr 'uri', 'https://google.com'
output = doc.convert
assert_xpath '//a[@href="https://google.com"]', output, 1
end
test 'set_attribute should set attribute if key is not locked' do
doc = empty_document
refute doc.attr? 'foo'
res = doc.set_attribute 'foo', 'baz'
assert res
assert_equal 'baz', (doc.attr 'foo')
end
test 'set_attribute should not set key if key is locked' do
doc = empty_document attributes: { 'foo' => 'bar' }
assert_equal 'bar', (doc.attr 'foo')
res = doc.set_attribute 'foo', 'baz'
refute res
assert_equal 'bar', (doc.attr 'foo')
end
test 'set_attribute should update backend attributes' do
doc = empty_document attributes: { 'backend' => 'html5@' }
assert_equal '', (doc.attr 'backend-html5')
res = doc.set_attribute 'backend', 'docbook5'
assert res
refute doc.attr? 'backend-html5'
assert_equal '', (doc.attr 'backend-docbook5')
end
test 'verify toc attribute matrix' do
expected_data = <<~'EOS'
#attributes |toc|toc-position|toc-placement|toc-class
toc | |nil |auto |nil
toc=header | |nil |auto |nil
toc=beeboo | |nil |auto |nil
toc=left | |left |auto |toc2
toc2 | |left |auto |toc2
toc=right | |right |auto |toc2
toc=preamble | |content |preamble |nil
toc=macro | |content |macro |nil
toc toc-placement=macro toc-position=left | |content |macro |nil
toc toc-placement! | |content |macro |nil
EOS
expected = expected_data.lines.map do |l|
next if l.start_with? '#'
l.split('|').map {|e| (e = e.strip) == 'nil' ? nil : e }
end.compact
expected.each do |expect|
raw_attrs, toc, toc_position, toc_placement, toc_class = expect
attrs = Hash[*raw_attrs.split.map {|e| e.include?('=') ? e.split('=', 2) : [e, ''] }.flatten]
doc = document_from_string '', attributes: attrs
toc ? (assert doc.attr?('toc', toc)) : (refute doc.attr?('toc'))
toc_position ? (assert doc.attr?('toc-position', toc_position)) : (refute doc.attr?('toc-position'))
toc_placement ? (assert doc.attr?('toc-placement', toc_placement)) : (refute doc.attr?('toc-placement'))
toc_class ? (assert doc.attr?('toc-class', toc_class)) : (refute doc.attr?('toc-class'))
end
end
end
context 'Interpolation' do
test "convert properly with simple names" do
html = convert_string(":frog: Tanglefoot\n:my_super-hero: Spiderman\n\nYo, {frog}!\nBeat {my_super-hero}!")
assert_xpath %(//p[text()="Yo, Tanglefoot!\nBeat Spiderman!"]), html, 1
end
test 'attribute lookup is not case sensitive' do
input = <<~'EOS'
:He-Man: The most powerful man in the universe
He-Man: {He-Man}
She-Ra: {She-Ra}
EOS
result = convert_string_to_embedded input, attributes: { 'She-Ra' => 'The Princess of Power' }
assert_xpath '//p[text()="He-Man: The most powerful man in the universe"]', result, 1
assert_xpath '//p[text()="She-Ra: The Princess of Power"]', result, 1
end
test "convert properly with single character name" do
html = convert_string(":r: Ruby\n\nR is for {r}!")
assert_xpath %(//p[text()="R is for Ruby!"]), html, 1
end
test "collapses spaces in attribute names" do
input = <<~'EOS'
Main Header
===========
:My frog: Tanglefoot
Yo, {myfrog}!
EOS
output = convert_string input
assert_xpath '(//p)[1][text()="Yo, Tanglefoot!"]', output, 1
end
test 'ignores lines with bad attributes if attribute-missing is drop-line' do
input = <<~'EOS'
:attribute-missing: drop-line
This is
blah blah {foobarbaz}
all there is.
EOS
output = convert_string_to_embedded input
para = xmlnodes_at_css 'p', output, 1
refute_includes 'blah blah', para.content
assert_message @logger, :INFO, 'dropping line containing reference to missing attribute: foobarbaz'
end
test 'attribute value gets interpreted when converting' do
doc = document_from_string(":google: http://google.com[Google]\n\n{google}")
assert_equal 'http://google.com[Google]', doc.attributes['google']
output = doc.convert
assert_xpath '//a[@href="http://google.com"][text() = "Google"]', output, 1
end
test 'should drop line with reference to missing attribute if attribute-missing attribute is drop-line' do
input = <<~'EOS'
:attribute-missing: drop-line
Line 1: This line should appear in the output.
Line 2: Oh no, a {bogus-attribute}! This line should not appear in the output.
EOS
output = convert_string_to_embedded input
assert_match(/Line 1/, output)
refute_match(/Line 2/, output)
assert_message @logger, :INFO, 'dropping line containing reference to missing attribute: bogus-attribute'
end
test 'should not drop line with reference to missing attribute by default' do
input = <<~'EOS'
Line 1: This line should appear in the output.
Line 2: A {bogus-attribute}! This time, this line should appear in the output.
EOS
output = convert_string_to_embedded input
assert_match(/Line 1/, output)
assert_match(/Line 2/, output)
assert_match(/\{bogus-attribute\}/, output)
end
test 'should drop line with attribute unassignment by default' do
input = <<~'EOS'
:a:
Line 1: This line should appear in the output.
Line 2: {set:a!}This line should not appear in the output.
EOS
output = convert_string_to_embedded input
assert_match(/Line 1/, output)
refute_match(/Line 2/, output)
end
test 'should not drop line with attribute unassignment if attribute-undefined is drop' do
input = <<~'EOS'
:attribute-undefined: drop
:a:
Line 1: This line should appear in the output.
Line 2: {set:a!}This line should appear in the output.
EOS
output = convert_string_to_embedded input
assert_match(/Line 1/, output)
assert_match(/Line 2/, output)
refute_match(/\{set:a!\}/, output)
end
test 'should drop line that only contains attribute assignment' do
input = <<~'EOS'
Line 1
{set:a}
Line 2
EOS
output = convert_string_to_embedded input
assert_xpath %(//p[text()="Line 1\nLine 2"]), output, 1
end
test 'should drop line that only contains unresolved attribute when attribute-missing is drop' do
input = <<~'EOS'
Line 1
{unresolved}
Line 2
EOS
output = convert_string_to_embedded input, attributes: { 'attribute-missing' => 'drop' }
assert_xpath %(//p[text()="Line 1\nLine 2"]), output, 1
end
test "substitutes inside unordered list items" do
html = convert_string(":foo: bar\n* snort at the {foo}\n* yawn")
assert_xpath %(//li/p[text()="snort at the bar"]), html, 1
end
test 'substitutes inside section title' do
output = convert_string(":prefix: Cool\n\n== {prefix} Title\n\ncontent")
assert_xpath '//h2[text()="Cool Title"]', output, 1
assert_css 'h2#_cool_title', output, 1
end
test 'interpolates attribute defined in header inside attribute entry in header' do
input = <<~'EOS'
= Title
Author Name
:attribute-a: value
:attribute-b: {attribute-a}
preamble
EOS
doc = document_from_string(input, parse_header_only: true)
assert_equal 'value', doc.attributes['attribute-b']
end
test 'interpolates author attribute inside attribute entry in header' do
input = <<~'EOS'
= Title
Author Name
:name: {author}
preamble
EOS
doc = document_from_string(input, parse_header_only: true)
assert_equal 'Author Name', doc.attributes['name']
end
test 'interpolates revinfo attribute inside attribute entry in header' do
input = <<~'EOS'
= Title
Author Name
2013-01-01
:date: {revdate}
preamble
EOS
doc = document_from_string(input, parse_header_only: true)
assert_equal '2013-01-01', doc.attributes['date']
end
test 'attribute entries can resolve previously defined attributes' do
input = <<~'EOS'
= Title
Author Name
v1.0, 2010-01-01: First release!
:a: value
:a2: {a}
:revdate2: {revdate}
{a} == {a2}
{revdate} == {revdate2}
EOS
doc = document_from_string input
assert_equal '2010-01-01', doc.attr('revdate')
assert_equal '2010-01-01', doc.attr('revdate2')
assert_equal 'value', doc.attr('a')
assert_equal 'value', doc.attr('a2')
output = doc.convert
assert_includes output, 'value == value'
assert_includes output, '2010-01-01 == 2010-01-01'
end
test 'should warn if unterminated block comment is detected in document header' do
input = <<~'EOS'
= Document Title
:foo: bar
////
:hey: there
content
EOS
doc = document_from_string input
assert_nil doc.attr('hey')
assert_message @logger, :WARN, '<stdin>: line 3: unterminated comment block', Hash
end
test 'substitutes inside block title' do
input = <<~'EOS'
:gem_name: asciidoctor
.Require the +{gem_name}+ gem
To use {gem_name}, the first thing to do is to import it in your Ruby source file.
EOS
output = convert_string_to_embedded input, attributes: { 'compat-mode' => '' }
assert_xpath '//*[@class="title"]/code[text()="asciidoctor"]', output, 1
input = <<~'EOS'
:gem_name: asciidoctor
.Require the `{gem_name}` gem
To use {gem_name}, the first thing to do is to import it in your Ruby source file.
EOS
output = convert_string_to_embedded input
assert_xpath '//*[@class="title"]/code[text()="asciidoctor"]', output, 1
end
test 'sets attribute until it is deleted' do
input = <<~'EOS'
:foo: bar
Crossing the {foo}.
:foo!:
Belly up to the {foo}.
EOS
output = convert_string_to_embedded input
assert_xpath '//p[text()="Crossing the bar."]', output, 1
assert_xpath '//p[text()="Belly up to the bar."]', output, 0
end
test 'should allow compat-mode to be set and unset in middle of document' do
input = <<~'EOS'
:foo: bar
[[paragraph-a]]
`{foo}`
:compat-mode!:
[[paragraph-b]]
`{foo}`
:compat-mode:
[[paragraph-c]]
`{foo}`
EOS
result = convert_string_to_embedded input, attributes: { 'compat-mode' => '@' }
assert_xpath '/*[@id="paragraph-a"]//code[text()="{foo}"]', result, 1
assert_xpath '/*[@id="paragraph-b"]//code[text()="bar"]', result, 1
assert_xpath '/*[@id="paragraph-c"]//code[text()="{foo}"]', result, 1
end
test 'does not disturb attribute-looking things escaped with backslash' do
html = convert_string(":foo: bar\nThis is a \\{foo} day.")
assert_xpath '//p[text()="This is a {foo} day."]', html, 1
end
test 'does not disturb attribute-looking things escaped with literals' do
html = convert_string(":foo: bar\nThis is a +++{foo}+++ day.")
assert_xpath '//p[text()="This is a {foo} day."]', html, 1
end
test 'does not substitute attributes inside listing blocks' do
input = <<~'EOS'
:forecast: snow
----
puts 'The forecast for today is {forecast}'
----
EOS
output = convert_string(input)
assert_match(/\{forecast\}/, output)
end
test 'does not substitute attributes inside literal blocks' do
input = <<~'EOS'
:foo: bar
....
You insert the text {foo} to expand the value
of the attribute named foo in your document.
....
EOS
output = convert_string(input)
assert_match(/\{foo\}/, output)
end
test 'does not show docdir and shows relative docfile if safe mode is SERVER or greater' do
input = <<~'EOS'
* docdir: {docdir}
* docfile: {docfile}
EOS
docdir = Dir.pwd
docfile = File.join(docdir, 'sample.adoc')
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SERVER, attributes: { 'docdir' => docdir, 'docfile' => docfile }
assert_xpath '//li[1]/p[text()="docdir: "]', output, 1
assert_xpath '//li[2]/p[text()="docfile: sample.adoc"]', output, 1
end
test 'shows absolute docdir and docfile paths if safe mode is less than SERVER' do
input = <<~'EOS'
* docdir: {docdir}
* docfile: {docfile}
EOS
docdir = Dir.pwd
docfile = File.join(docdir, 'sample.adoc')
output = convert_string_to_embedded input, safe: Asciidoctor::SafeMode::SAFE, attributes: { 'docdir' => docdir, 'docfile' => docfile }
assert_xpath %(//li[1]/p[text()="docdir: #{docdir}"]), output, 1
assert_xpath %(//li[2]/p[text()="docfile: #{docfile}"]), output, 1
end
test 'assigns attribute defined in attribute reference with set prefix and value' do
input = '{set:foo:bar}{foo}'
output = convert_string_to_embedded input
assert_xpath '//p', output, 1
assert_xpath '//p[text()="bar"]', output, 1
end
test 'assigns attribute defined in attribute reference with set prefix and no value' do
input = "{set:foo}\n{foo}yes"
output = convert_string_to_embedded input
assert_xpath '//p', output, 1
assert_xpath '//p[normalize-space(text())="yes"]', output, 1
end
test 'assigns attribute defined in attribute reference with set prefix and empty value' do
input = "{set:foo:}\n{foo}yes"
output = convert_string_to_embedded input
assert_xpath '//p', output, 1
assert_xpath '//p[normalize-space(text())="yes"]', output, 1
end
test 'unassigns attribute defined in attribute reference with set prefix' do
input = <<~'EOS'
:attribute-missing: drop-line
:foo:
{set:foo!}
{foo}yes
EOS
output = convert_string_to_embedded input
assert_xpath '//p', output, 1
assert_xpath '//p/child::text()', output, 0
assert_message @logger, :INFO, 'dropping line containing reference to missing attribute: foo'
end
end
context "Intrinsic attributes" do
test "substitute intrinsics" do
Asciidoctor::INTRINSIC_ATTRIBUTES.each_pair do |key, value|
html = convert_string("Look, a {#{key}} is here")
# can't use Nokogiri because it interprets the HTML entities and we can't match them
assert_match(/Look, a #{Regexp.escape(value)} is here/, html)
end
end
test "don't escape intrinsic substitutions" do
html = convert_string('happy{nbsp}together')
assert_match(/happy together/, html)
end
test "escape special characters" do
html = convert_string('<node>&</node>')
assert_match(/<node>&<\/node>/, html)
end
test 'creates counter' do
input = '{counter:mycounter}'
doc = document_from_string input
output = doc.convert
assert_equal 1, doc.attributes['mycounter']
assert_xpath '//p[text()="1"]', output, 1
end
test 'creates counter silently' do
input = '{counter2:mycounter}'
doc = document_from_string input
output = doc.convert
assert_equal 1, doc.attributes['mycounter']
assert_xpath '//p[text()="1"]', output, 0
end
test 'creates counter with numeric seed value' do
input = '{counter2:mycounter:10}'
doc = document_from_string input
doc.convert
assert_equal 10, doc.attributes['mycounter']
end
test 'creates counter with character seed value' do
input = '{counter2:mycounter:A}'
doc = document_from_string input
doc.convert
assert_equal 'A', doc.attributes['mycounter']
end
test 'can seed counter to start at 1' do
input = <<~'EOS'
:mycounter: 0
{counter:mycounter}
EOS
output = convert_string_to_embedded input
assert_xpath '//p[text()="1"]', output, 1
end
test 'can seed counter to start at A' do
input = <<~'EOS'
:mycounter: @
{counter:mycounter}
EOS
output = convert_string_to_embedded input
assert_xpath '//p[text()="A"]', output, 1
end
test 'increments counter with positive numeric value' do
input = <<~'EOS'
[subs=attributes]
++++
{counter:mycounter:1}
{counter:mycounter}
{counter:mycounter}
{mycounter}
++++
EOS
doc = document_from_string input, standalone: false
output = doc.convert
assert_equal 3, doc.attributes['mycounter']
assert_equal %w(1 2 3 3), output.lines.map {|l| l.rstrip }
end
test 'increments counter with negative numeric value' do
input = <<~'EOS'
[subs=attributes]
++++
{counter:mycounter:-2}
{counter:mycounter}
{counter:mycounter}
{mycounter}
++++
EOS
doc = document_from_string input, standalone: false
output = doc.convert
assert_equal 0, doc.attributes['mycounter']
assert_equal %w(-2 -1 0 0), output.lines.map {|l| l.rstrip }
end
test 'increments counter with ASCII character value' do
input = <<~'EOS'
[subs=attributes]
++++
{counter:mycounter:A}
{counter:mycounter}
{counter:mycounter}
{mycounter}
++++
EOS
output = convert_string_to_embedded input
assert_equal %w(A B C C), output.lines.map {|l| l.rstrip }
end
test 'increments counter with non-ASCII character value' do
input = <<~'EOS'
[subs=attributes]
++++
{counter:mycounter:é}
{counter:mycounter}
{counter:mycounter}
{mycounter}
++++
EOS
output = convert_string_to_embedded input
assert_equal %w(é ê ë ë), output.lines.map {|l| l.rstrip }
end
test 'increments counter with emoji character value' do
input = <<~'EOS'
[subs=attributes]
++++
{counter:smiley:😋}
{counter:smiley}
{counter:smiley}
{smiley}
++++
EOS
output = convert_string_to_embedded input
assert_equal %w(😋 😌 😍 😍), output.lines.map {|l| l.rstrip }
end
test 'increments counter with multi-character value' do
input = <<~'EOS'
[subs=attributes]
++++
{counter:math:1x}
{counter:math}
{counter:math}
{math}
++++
EOS
output = convert_string_to_embedded input
assert_equal %w(1x 1y 1z 1z), output.lines.map {|l| l.rstrip }
end
test 'counter uses 0 as seed value if seed attribute is nil' do
input = <<~'EOS'
:mycounter:
{counter:mycounter}
{mycounter}
EOS
doc = document_from_string input
output = doc.convert standalone: false
assert_equal 1, doc.attributes['mycounter']
assert_xpath '//p[text()="1"]', output, 2
end
test 'counter value can be reset by attribute entry' do
input = <<~'EOS'
:mycounter:
before: {counter:mycounter} {counter:mycounter} {counter:mycounter}
:mycounter!:
after: {counter:mycounter}
EOS
doc = document_from_string input
output = doc.convert standalone: false
assert_equal 1, doc.attributes['mycounter']
assert_xpath '//p[text()="before: 1 2 3"]', output, 1
assert_xpath '//p[text()="after: 1"]', output, 1
end
test 'counter value can be advanced by attribute entry' do
input = <<~'EOS'
before: {counter:mycounter}
:mycounter: 10
after: {counter:mycounter}
EOS
doc = document_from_string input
output = doc.convert standalone: false
assert_equal 11, doc.attributes['mycounter']
assert_xpath '//p[text()="before: 1"]', output, 1
assert_xpath '//p[text()="after: 11"]', output, 1
end
test 'nested document should use counter from parent document' do
input = <<~'EOS'
.Title for Foo
image::foo.jpg[]
[cols="2*a"]
|===
|
.Title for Bar
image::bar.jpg[]
|
.Title for Baz
image::baz.jpg[]
|===
.Title for Qux
image::qux.jpg[]
EOS
output = convert_string_to_embedded input
assert_xpath '//div[@class="title"]', output, 4
assert_xpath '//div[@class="title"][text() = "Figure 1. Title for Foo"]', output, 1
assert_xpath '//div[@class="title"][text() = "Figure 2. Title for Bar"]', output, 1
assert_xpath '//div[@class="title"][text() = "Figure 3. Title for Baz"]', output, 1
assert_xpath '//div[@class="title"][text() = "Figure 4. Title for Qux"]', output, 1
end
test 'should not allow counter to modify locked attribute' do
input = <<~'EOS'
{counter:foo:ignored} is not {foo}
EOS
output = convert_string_to_embedded input, attributes: { 'foo' => 'bar' }
assert_xpath '//p[text()="bas is not bar"]', output, 1
end
test 'should not allow counter2 to modify locked attribute' do
input = <<~'EOS'
{counter2:foo:ignored}{foo}
EOS
output = convert_string_to_embedded input, attributes: { 'foo' => 'bar' }
assert_xpath '//p[text()="bar"]', output, 1
end
test 'should not allow counter to modify built-in locked attribute' do
input = <<~'EOS'
{counter:max-include-depth:128} is one more than {max-include-depth}
EOS
doc = document_from_string input, standalone: false
output = doc.convert
assert_xpath '//p[text()="65 is one more than 64"]', output, 1
assert_equal 64, doc.attributes['max-include-depth']
end
test 'should not allow counter2 to modify built-in locked attribute' do
input = <<~'EOS'
{counter2:max-include-depth:128}{max-include-depth}
EOS
doc = document_from_string input, standalone: false
output = doc.convert
assert_xpath '//p[text()="64"]', output, 1
assert_equal 64, doc.attributes['max-include-depth']
end
end
context 'Block attributes' do
test 'parses attribute names as name token' do
input = <<~'EOS'
[normal,foo="bar",_foo="_bar",foo1="bar1",foo-foo="bar-bar",foo.foo="bar.bar"]
content
EOS
block = block_from_string input
assert_equal 'bar', block.attr('foo')
assert_equal '_bar', block.attr('_foo')
assert_equal 'bar1', block.attr('foo1')
assert_equal 'bar-bar', block.attr('foo-foo')
assert_equal 'bar.bar', block.attr('foo.foo')
end
test 'positional attributes assigned to block' do
input = <<~'EOS'
[quote, author, source]
____
A famous quote.
____
EOS
doc = document_from_string(input)
qb = doc.blocks.first
assert_equal 'quote', qb.style
assert_equal 'author', qb.attr('attribution')
assert_equal 'author', qb.attr(:attribution)
assert_equal 'author', qb.attributes['attribution']
assert_equal 'source', qb.attributes['citetitle']
end
test 'normal substitutions are performed on single-quoted positional attribute' do
input = <<~'EOS'
[quote, author, 'http://wikipedia.org[source]']
____
A famous quote.
____
EOS
doc = document_from_string(input)
qb = doc.blocks.first
assert_equal 'quote', qb.style
assert_equal 'author', qb.attr('attribution')
assert_equal 'author', qb.attr(:attribution)
assert_equal 'author', qb.attributes['attribution']
assert_equal '<a href="http://wikipedia.org">source</a>', qb.attributes['citetitle']
end
test 'normal substitutions are performed on single-quoted named attribute' do
input = <<~'EOS'
[quote, author, citetitle='http://wikipedia.org[source]']
____
A famous quote.
____
EOS
doc = document_from_string(input)
qb = doc.blocks.first
assert_equal 'quote', qb.style
assert_equal 'author', qb.attr('attribution')
assert_equal 'author', qb.attr(:attribution)
assert_equal 'author', qb.attributes['attribution']
assert_equal '<a href="http://wikipedia.org">source</a>', qb.attributes['citetitle']
end
test 'normal substitutions are performed once on single-quoted named title attribute' do
input = <<~'EOS'
[title='*title*']
content
EOS
output = convert_string_to_embedded input
assert_xpath '//*[@class="title"]/strong[text()="title"]', output, 1
end
test 'attribute list may not begin with space' do
input = <<~'EOS'
[ quote]
____
A famous quote.
____
EOS
doc = document_from_string input
b1 = doc.blocks.first
assert_equal ['[ quote]'], b1.lines
end
test 'attribute list may begin with comma' do
input = <<~'EOS'
[, author, source]
____
A famous quote.
____
EOS
doc = document_from_string input
qb = doc.blocks.first
assert_equal 'quote', qb.style
assert_equal 'author', qb.attributes['attribution']
assert_equal 'source', qb.attributes['citetitle']
end
test 'first attribute in list may be double quoted' do
input = <<~'EOS'
["quote", "author", "source", role="famous"]
____
A famous quote.
____
EOS
doc = document_from_string input
qb = doc.blocks.first
assert_equal 'quote', qb.style
assert_equal 'author', qb.attributes['attribution']
assert_equal 'source', qb.attributes['citetitle']
assert_equal 'famous', qb.attributes['role']
end
test 'first attribute in list may be single quoted' do
input = <<~'EOS'
['quote', 'author', 'source', role='famous']
____
A famous quote.
____
EOS
doc = document_from_string input
qb = doc.blocks.first
assert_equal 'quote', qb.style
assert_equal 'author', qb.attributes['attribution']
assert_equal 'source', qb.attributes['citetitle']
assert_equal 'famous', qb.attributes['role']
end
test 'attribute with value None without quotes is ignored' do
input = <<~'EOS'
[id=None]
paragraph
EOS
doc = document_from_string input
para = doc.blocks.first
refute para.attributes.key?('id')
end
test 'role? returns true if role is assigned' do
input = <<~'EOS'
[role="lead"]
A paragraph
EOS
doc = document_from_string input
p = doc.blocks.first
assert p.role?
end
test 'role? does not return true if role attribute is set on document' do
input = <<~'EOS'
:role: lead
A paragraph
EOS
doc = document_from_string input
p = doc.blocks.first
refute p.role?
end
test 'role? can check for exact role name match' do
input = <<~'EOS'
[role="lead"]
A paragraph
EOS
doc = document_from_string input
p = doc.blocks.first
assert p.role?('lead')
p2 = doc.blocks.last
refute p2.role?('final')
end
test 'has_role? can check for presence of role name' do
input = <<~'EOS'
[role="lead abstract"]
A paragraph
EOS
doc = document_from_string input
p = doc.blocks.first
refute p.role?('lead')
assert p.has_role?('lead')
end
test 'has_role? does not look for role defined as document attribute' do
input = <<~'EOS'
:role: lead abstract
A paragraph
EOS
doc = document_from_string input
p = doc.blocks.first
refute p.has_role?('lead')
end
test 'roles returns array of role names' do
input = <<~'EOS'
[role="story lead"]
A paragraph
EOS
doc = document_from_string input
p = doc.blocks.first
assert_equal ['story', 'lead'], p.roles
end
test 'roles returns empty array if role attribute is not set' do
input = 'a paragraph'
doc = document_from_string input
p = doc.blocks.first
assert_equal [], p.roles
end
test 'roles does not return value of roles document attribute' do
input = <<~'EOS'
:role: story lead
A paragraph
EOS
doc = document_from_string input
p = doc.blocks.first
assert_equal [], p.roles
end
test 'roles= sets the role attribute on the node' do
doc = document_from_string 'a paragraph'
p = doc.blocks.first
p.role = 'foobar'
assert_equal 'foobar', (p.attr 'role')
end
test 'roles= coerces array value to a space-separated string' do
doc = document_from_string 'a paragraph'
p = doc.blocks.first
p.role = %w(foo bar)
assert_equal 'foo bar', (p.attr 'role')
end
test "Attribute substitutions are performed on attribute list before parsing attributes" do
input = <<~'EOS'
:lead: role="lead"
[{lead}]
A paragraph
EOS
doc = document_from_string(input)
para = doc.blocks.first
assert_equal 'lead', para.attributes['role']
end
test 'id, role and options attributes can be specified on block style using shorthand syntax' do
input = <<~'EOS'
[literal#first.lead%step]
A literal paragraph.
EOS
doc = document_from_string(input)
para = doc.blocks.first
assert_equal :literal, para.context
assert_equal 'first', para.attributes['id']
assert_equal 'lead', para.attributes['role']
assert para.attributes.key?('step-option')
refute para.attributes.key?('options')
end
test 'id, role and options attributes can be specified using shorthand syntax on block style using multiple block attribute lines' do
input = <<~'EOS'
[literal]
[#first]
[.lead]
[%step]
A literal paragraph.
EOS
doc = document_from_string(input)
para = doc.blocks.first
assert_equal :literal, para.context
assert_equal 'first', para.attributes['id']
assert_equal 'lead', para.attributes['role']
assert para.attributes.key?('step-option')
refute para.attributes.key?('options')
end
test 'multiple roles and options can be specified in block style using shorthand syntax' do
input = <<~'EOS'
[.role1%option1.role2%option2]
Text
EOS
doc = document_from_string input
para = doc.blocks.first
assert_equal 'role1 role2', para.attributes['role']
assert para.attributes.key?('option1-option')
assert para.attributes.key?('option2-option')
refute para.attributes.key?('options')
end
test 'options specified using shorthand syntax on block style across multiple lines should be additive' do
input = <<~'EOS'
[%option1]
[%option2]
Text
EOS
doc = document_from_string input
para = doc.blocks.first
assert para.attributes.key?('option1-option')
assert para.attributes.key?('option2-option')
refute para.attributes.key?('options')
end
test 'roles specified using shorthand syntax on block style across multiple lines should be additive' do
input = <<~'EOS'
[.role1]
[.role2.role3]
Text
EOS
doc = document_from_string input
para = doc.blocks.first
assert_equal 'role1 role2 role3', para.attributes['role']
end
test 'setting a role using the role attribute replaces any existing roles' do
input = <<~'EOS'
[.role1]
[role=role2]
[.role3]
Text
EOS
doc = document_from_string input
para = doc.blocks.first
assert_equal 'role2 role3', para.attributes['role']
end
test 'setting a role using the shorthand syntax on block style should not clear the ID' do
input = <<~'EOS'
[#id]
[.role]
Text
EOS
doc = document_from_string input
para = doc.blocks.first
assert_equal 'id', para.id
assert_equal 'role', para.role
end
test 'a role can be added using add_role when the node has no roles' do
input = 'A normal paragraph'
doc = document_from_string(input)
para = doc.blocks.first
res = para.add_role 'role1'
assert res
assert_equal 'role1', para.attributes['role']
assert para.has_role? 'role1'
end
test 'a role can be added using add_role when the node already has a role' do
input = <<~'EOS'
[.role1]
A normal paragraph
EOS
doc = document_from_string(input)
para = doc.blocks.first
res = para.add_role 'role2'
assert res
assert_equal 'role1 role2', para.attributes['role']
assert para.has_role? 'role1'
assert para.has_role? 'role2'
end
test 'a role is not added using add_role if the node already has that role' do
input = <<~'EOS'
[.role1]
A normal paragraph
EOS
doc = document_from_string(input)
para = doc.blocks.first
res = para.add_role 'role1'
refute res
assert_equal 'role1', para.attributes['role']
assert para.has_role? 'role1'
end
test 'an existing role can be removed using remove_role' do
input = <<~'EOS'
[.role1.role2]
A normal paragraph
EOS
doc = document_from_string(input)
para = doc.blocks.first
res = para.remove_role 'role1'
assert res
assert_equal 'role2', para.attributes['role']
assert para.has_role? 'role2'
refute para.has_role?('role1')
end
test 'roles are removed when last role is removed using remove_role' do
input = <<~'EOS'
[.role1]
A normal paragraph
EOS
doc = document_from_string(input)
para = doc.blocks.first
res = para.remove_role 'role1'
assert res
refute para.role?
assert_nil para.attributes['role']
refute para.has_role? 'role1'
end
test 'roles are not changed when a non-existent role is removed using remove_role' do
input = <<~'EOS'
[.role1]
A normal paragraph
EOS
doc = document_from_string(input)
para = doc.blocks.first
res = para.remove_role 'role2'
refute res
assert_equal 'role1', para.attributes['role']
assert para.has_role? 'role1'
refute para.has_role?('role2')
end
test 'roles are not changed when using remove_role if the node has no roles' do
input = 'A normal paragraph'
doc = document_from_string(input)
para = doc.blocks.first
res = para.remove_role 'role1'
refute res
assert_nil para.attributes['role']
refute para.has_role?('role1')
end
test 'option can be specified in first position of block style using shorthand syntax' do
input = <<~'EOS'
[%interactive]
- [x] checked
EOS
doc = document_from_string input
list = doc.blocks.first
assert list.attributes.key? 'interactive-option'
refute list.attributes.key? 'options'
end
test 'id and role attributes can be specified on section style using shorthand syntax' do
input = <<~'EOS'
[dedication#dedication.small]
== Section
Content.
EOS
output = convert_string_to_embedded input
assert_xpath '/div[@class="sect1 small"]', output, 1
assert_xpath '/div[@class="sect1 small"]/h2[@id="dedication"]', output, 1
end
test 'id attribute specified using shorthand syntax should not create a special section' do
input = <<~'EOS'
[#idname]
== Section
content
EOS
doc = document_from_string input, backend: 'docbook'
section = doc.blocks[0]
refute_nil section
assert_equal :section, section.context
refute section.special
output = doc.convert
assert_css 'article:root > section', output, 1
assert_css 'article:root > section[xml|id="idname"]', output, 1
end
test "Block attributes are additive" do
input = <<~'EOS'
[id='foo']
[role='lead']
A paragraph.
EOS
doc = document_from_string(input)
para = doc.blocks.first
assert_equal 'foo', para.id
assert_equal 'lead', para.attributes['role']
end
test "Last wins for id attribute" do
input = <<~'EOS'
[[bar]]
[[foo]]
== Section
paragraph
[[baz]]
[id='coolio']
=== Section
EOS
doc = document_from_string(input)
sec = doc.first_section
assert_equal 'foo', sec.id
subsec = sec.blocks.last
assert_equal 'coolio', subsec.id
end
test "trailing block attributes transfer to the following section" do
input = <<~'EOS'
[[one]]
== Section One
paragraph
[[sub]]
// try to mess this up!
=== Sub-section
paragraph
[role='classy']
////
block comment
////
== Section Two
content
EOS
doc = document_from_string(input)
section_one = doc.blocks.first
assert_equal 'one', section_one.id
subsection = section_one.blocks.last
assert_equal 'sub', subsection.id
section_two = doc.blocks.last
assert_equal 'classy', section_two.attr(:role)
end
end
end
|