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 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262
|
import sys, re
import builtins
from typing import Optional, NewType, List
NodeType = NewType("NodeType", int)
class DOMString(str):
"""
DOM String
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-C74D1578
"""
class DOMTimeStamp(int):
"""
DOM Time Stamp
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Core-DOMTimeStamp
"""
class DOMUserData(dict):
"""
DOM User Data
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Core-DOMUserData
"""
def setPath(self, path, value):
"""
Traverse the nested dictionary `d` and set the value
Arguments:
path -- a '/' delimited string of keys
value -- value to set at the last key
Examples::
userdata.setPath('packages/graphics/extensions', ['.ps','jpg'])
See Also:
getPath()
"""
keys = path.split('/')
for key in keys[:-1]:
if key not in self:
self[key] = {}
self = self[key]
self[keys[-1]] = value
def getPath(self, path, default=None):
"""
Return the value of the nested dictionary `d` at the path
Arguments:
path -- a '/' delimited string of keys
default -- value to return if the path doesn't exist
Examples::
userdata.getPath('packages/graphics/extensions')
See Also:
setPath()
"""
keys = path.split('/')
for key in keys[:-1]:
if key not in self:
return default
self = self[key]
return self.get(keys[-1], default)
class DOMObject(object):
"""
DOM Object
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Core-DOMObject
"""
# Exception Code
INDEX_SIZE_ERR = 1
DOMSTRING_SIZE_ERR = 2
HIERARCHY_REQUEST_ERR = 3
WRONG_DOCUMENT_ERR = 4
INVALID_CHARACTER_ERR = 5
NO_DATA_ALLOWED_ERR = 6
NO_MODIFICATION_ALLOWED_ERR = 7
NOT_FOUND_ERR = 8
NOT_SUPPORTED_ERR = 9
INUSE_ATTRIBUTE_ERR = 10
INVALID_STATE_ERR = 11
SYNTAX_ERR = 12
INVALID_MODIFICATION_ERR = 13
NAMESPACE_ERR = 14
INVALID_ACCESS_ERR = 15
VALIDATION_ERR = 16
class DOMException(Exception):
"""
DOM Exception
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-17189187
"""
def __init__(self, *args, **kw):
if self.__class__ is DOMException:
raise RuntimeError(
"DOMException should not be instantiated directly")
Exception.__init__(self, *args, **kw)
def _get_code(self):
return self.code
class IndexSizeErr(DOMException):
code = INDEX_SIZE_ERR
class DomstringSizeErr(DOMException):
code = DOMSTRING_SIZE_ERR
class HierarchyRequestErr(DOMException):
code = HIERARCHY_REQUEST_ERR
class WrongDocumentErr(DOMException):
code = WRONG_DOCUMENT_ERR
class InvalidCharacterErr(DOMException):
code = INVALID_CHARACTER_ERR
class NoDataAllowedErr(DOMException):
code = NO_DATA_ALLOWED_ERR
class NoModificationAllowedErr(DOMException):
code = NO_MODIFICATION_ALLOWED_ERR
class NotFoundErr(DOMException):
code = NOT_FOUND_ERR
class NotSupportedErr(DOMException):
code = NOT_SUPPORTED_ERR
class InuseAttributeErr(DOMException):
code = INUSE_ATTRIBUTE_ERR
class InvalidStateErr(DOMException):
code = INVALID_STATE_ERR
class SyntaxErr(DOMException):
code = SYNTAX_ERR
class InvalidModificationErr(DOMException):
code = INVALID_MODIFICATION_ERR
class NamespaceErr(DOMException):
code = NAMESPACE_ERR
class InvalidAccessErr(DOMException):
code = INVALID_ACCESS_ERR
class ValidationErr(DOMException):
code = VALIDATION_ERR
class _DOMList(list):
""" Generic List """
@property
def length(self):
return len(self)
def item(self, i):
try: return self[i]
except IndexError: return None
class DOMStringList(_DOMList):
"""
DOM String List
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#DOMStringList
"""
class NameList(_DOMList):
"""
Name List
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#NameList
"""
def getName(self, i):
try: return self[i]
except IndexError: return None
def getNamespaceURI(self, i):
return None
def containsNS(self, ns, name):
if ns: return False
return self.contains(name)
class NodeList(_DOMList):
"""
Node List
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-536297177
"""
class DOMImplementationList(_DOMList):
"""
DOM Implementation List
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#DOMImplementationList
"""
class DOMImplementationSource(object):
"""
DOM Implementation Source
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#DOMImplementationSource
"""
def getDOMImplementation(self, features):
raise NotImplementedError
def getDOMImplementationList(self, features):
raise NotImplementedError
class DOMImplementation(object):
"""
DOM Implementation
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-102161490
"""
def hasFeature(self, feature, version):
raise NotSupportedErr
def createDocumentType(self, qualifiedName, publicId=None, systemId=None):
raise NotSupportedErr
def createDocument(self, namespaceURI, qualifiedName, doctype):
raise NotSupportedErr
def getFeature(self, feature, version):
raise NotSupportedErr
class NamedNodeMap(dict):
"""
DOM Named Node Map
This class is a Python dictionary that also supports the
DOM Named Node Map interface. It is used for the `attributes`
attribute on Node. Since LaTeX's attributes are actually
objects instead of strings, 'parentNode' and 'ownerDocument'
attributes were also added. Whenever a new object is added to
the dictionary, the object's parent and owner are set to
the parent and owner of this object.
"""
@property
def parentNode(self):
"""
Get/Set the parent node
Since the children of this object can contain document fragments
when it is set we have to reset the parentNode of all of
our children.
"""
return getattr(self, '_dom_parentNode', None)
@parentNode.setter
def parentNode(self, value):
if getattr(self, '_dom_parentNode', None) is not value:
self._dom_parentNode = value
for value in list(self.values()):
self._resetPosition(value.parentNode)
@property
def ownerDocument(self):
if self.parentNode is not None:
return self.parentNode.ownerDocument
return
def getNamedItem(self, name):
"""
Get the value for name `name`
Required Arguments:
name -- string containing the name of the item
Returns:
the value stored in `name`, or None if it doesn't exist
"""
return self.get(name)
def setNamedItem(self, arg):
"""
Add a new item
Required Arguments:
arg -- node to add. The nodeName attribute determines the name
that it will be store under.
Returns:
the node given in `arg`
"""
self[arg.nodeName] = arg
return arg
def removeNamedItem(self, name):
"""
Remove item by name `name`
Required Arguments:
name -- name of the item to remove
Returns:
the value at `name`
"""
try:
value = self[name]
del self[name]
except KeyError:
raise NotFoundErr('Could not find name "%s"' % name)
return value
def item(self, index):
"""
Return item at index `index`
Required Arguments:
index -- the index of the item to return
Returns:
the requested value, or None if it doesn't exist
"""
items = list(self.items())
items.sort()
try: return items[num][1]
except IndexError: return None
@property
def length(self):
""" Return the number of stored items """
return len(self)
def getNamedItemNS(self, ns, name):
"""
Get a named item in a particular namespace
Required Arguments:
ns -- the namespace for the item (ignored)
name -- the name of the item
Returns:
the requested value
"""
return self.getNamedItem(name)
def setNamedItemNS(self, arg):
"""
Set a named item in a particular namespace
Required Arguments:
arg -- node containing the value to store
"""
return self.setNamedItem(arg)
def removeNamedItemNS(self, ns, name):
"""
Remove a named item in a particular namespace
Required Arguments:
ns -- the namespace for the item (ignored)
name -- the name of the item
"""
return self.removeNamedItem(name)
def __setitem__(self, name, value):
"""
Set the value at name `name` to `value`
Required Arguments:
name -- the name to use
value -- the value to put under `name`
"""
self._resetPosition(value)
dict.__setitem__(self, name, value)
def _resetPosition(self, value, parent=None):
"""
Set the parent node and owner document of the value
Required Arguments:
value -- the object to set the position of
"""
nodeType = getattr(value, 'nodeType', None)
if parent is None:
parent = self.parentNode
if value is None:
return
elif nodeType == Node.DOCUMENT_FRAGMENT_NODE:
for item in value:
self._resetPosition(item, parent=value)
elif nodeType is not None:
value.parentNode = parent
value.ownerDocument = self.ownerDocument
elif isinstance(value, list):
for item in value:
self._resetPosition(item)
elif isinstance(value, dict):
for item in list(value.values()):
self._resetPosition(item)
else:
if hasattr(value, 'parentNode'):
value.parentNode = parent
if hasattr(value, 'ownerDocument'):
value.ownerDocument = self.ownerDocument
def update(self, other):
"""
Merge another named node map into this one
Required Arguments:
other -- another instance of a NamedNodeMap
"""
for key, value in list(other.items()):
self[key] = value
def _compareDocumentPosition(self, other):
"""
Compare the position of the current node to `other`
Required Arguments:
other -- the node to compare our position against
Returns:
DOCUMENT_POSITION_DISCONNECTED -- nodes are disconnected
DOCUMENT_POSITION_PRECEDING -- `other` precedes this node
DOCUMENT_POSITION_FOLLOWING -- `other` follows this node
DOCUMENT_POSITION_CONTAINS -- `other` contains this node
DOCUMENT_POSITION_CONTAINED_BY -- `other` is contained by this node
DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC -- unknown
"""
if self.ownerDocument is not other.ownerDocument:
return Node.DOCUMENT_POSITION_DISCONNECTED
if self.previousSibling is other:
return Node.DOCUMENT_POSITION_PRECEDING
if self.nextSibling is other:
return Node.DOCUMENT_POSITION_FOLLOWING
if self is other:
return Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
sparents = []
parent = self
while parent is not None:
if parent is other:
return Node.DOCUMENT_POSITION_CONTAINS
sparents.append(parent)
parent = parent.parentNode
oparents = []
parent = other
while parent is not None:
if parent is self:
return Node.DOCUMENT_POSITION_CONTAINED_BY
oparents.append(parent)
parent = parent.parentNode
sparents.reverse()
oparents.reverse()
for i, sparent in enumerate(sparents):
for j, oparent in enumerate(oparents):
if sparent is oparent:
s = sparents[i+1]
o = oparents[j+1]
for item in sparent:
if item is s:
return Node.DOCUMENT_POSITION_FOLLOWING
if item is o:
return Node.DOCUMENT_POSITION_PRECEDING
return Node.DOCUMENT_POSITION_DISCONNECTED
def _previousSibling(self):
"""
Return the previous sibling
NOTE: This is fairly inefficient. The reason that it has
to be done this way is because Text nodes are a subclass of
`str` which is an immutable object. This means that
we can't have two references to the same Text object (i.e.
`previousSibling` and `nextSibling` can't be variables).
"""
if not self.parentNode:
return None
previous = None
for i, item in enumerate(self.parentNode):
if item is self:
return previous
previous = item
return None
def _nextSibling(self):
"""
Return the next sibling
NOTE: This is fairly inefficient. The reason that it has
to be done this way is because Text nodes are a subclass of
`str` which is an immutable object. This means that
we can't have two references to the same Text object (i.e.
`previousSibling` and `nextSibling` can't be variables).
"""
if not self.parentNode:
return None
next = False
for i, item in enumerate(self.parentNode):
if next:
return item
if item is self:
next = True
return None
def xmlstr(obj):
""" Escape special characters to create a legal xml string """
if isinstance(obj, str):
return obj.replace('&','&').replace('<','<').replace('>','>')
elif isinstance(obj, list):
return str([xmlstr(x) for x in obj])
elif isinstance(obj, dict):
return str(dict([(xmlstr(x),xmlstr(y)) for x,y in list(obj.items())]))
else:
return xmlstr(str(obj))
class Node(object):
"""
Node
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-1950641247
"""
#
# LaTeX Node extensions
#
# LaTeX document hierarchy
DOCUMENT_LEVEL = -sys.maxsize
VOLUME_LEVEL = -2
PART_LEVEL = -1
CHAPTER_LEVEL = 0
SECTION_LEVEL = 1
SUBSECTION_LEVEL = 2
SUBSUBSECTION_LEVEL = 3
PARAGRAPH_LEVEL = 4
SUBPARAGRAPH_LEVEL = 5
SUBSUBPARAGRAPH_LEVEL = 6
ENDSECTIONS_LEVEL = 100
PAR_LEVEL = 101
ENVIRONMENT_LEVEL = 201
CHARACTER_LEVEL = COMMAND_LEVEL = 1001
level = CHARACTER_LEVEL # Document hierarchy level of the node
blockType = False # Indicates that this node is a block-level element
# (i.e. should not be in a paragraph)
contextDepth = 1000 # TeX context level of this node (used during digest)
#
# End LaTeX Node extensions
#
ELEMENT_NODE = NodeType(1)
ATTRIBUTE_NODE = NodeType(2)
TEXT_NODE = NodeType(3)
CDATA_SECTION_NODE = NodeType(4)
ENTITY_REFERENCE_NODE = NodeType(5)
ENTITY_NODE = NodeType(6)
PROCESSING_INSTRUCTION_NODE = NodeType(7)
COMMENT_NODE = NodeType(8)
DOCUMENT_NODE = NodeType(9)
DOCUMENT_TYPE_NODE = NodeType(10)
DOCUMENT_FRAGMENT_NODE = NodeType(11)
NOTATION_NODE = NodeType(12)
DOCUMENT_POSITION_DISCONNECTED = 0x01
DOCUMENT_POSITION_PRECEDING = 0x02
DOCUMENT_POSITION_FOLLOWING = 0x04
DOCUMENT_POSITION_CONTAINS = 0x08
DOCUMENT_POSITION_CONTAINED_BY = 0x10
DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 0x20
NODE_SLOTS = ['parentNode','contextDepth','ownerDocument',
'_dom_childNodes','_dom_userdata']
ELEMENT_SLOTS = NODE_SLOTS + ['_dom_attributes','nodeName']
TEXT_SLOTS = ['parentNode','contextDepth','ownerDocument','isMarkup']
__slots__ = []
isElementContentWhitespace = False
namespaceURI = None
prefix = None
localName = None
baseURI = None
nodeName = None # type: Optional[str]
nodeValue = None # type: Optional[str]
nodeType = None # type: Optional[NodeType]
parentNode = None
ownerDocument = None # type: Optional[Document]
attributes = None
nonNormalizedAttrs = [] # type: List[str]
str = None # type: Optional[str]
# String containing type of node relating to navigation.
# Common values are: glossary, bibliography, contents, index, search, etc.
linkType = None # type: Optional[builtins.str]
def toXML(self, debug=False):
"""
Dump the object as XML
Returns:
string in XML format
"""
# Only the content of DocumentFragments get rendered
if self.nodeType == Node.DOCUMENT_FRAGMENT_NODE:
s = []
for value in self:
if hasattr(value, 'toXML'):
value = value.toXML()
else:
value = xmlstr(value)
s.append(value)
return ''.join(s)
# Remap name into valid XML tag name
name = self.nodeName
name = name.replace('@','-')
name = name.replace('#','dom-')
if name.startswith('-'):
name = 'x%s' % name
modifier = ''
if '::' in name:
name, modifier = name.split('::')
modifier = ' char="%s"' % xmlstr(modifier)
else:
modifier = re.search(r'(\W*)$', name).group(1)
if modifier:
name = re.sub(r'(\W*)$', r'', name)
modifier = ' modifier="%s"' % xmlstr(modifier)
if not name:
name = 'unknown'
source = ''
#source = ' source="%s"' % xmlstr(self.source)
style = ''
if hasattr(self, 'style') and self.style:
style = ' style="%s"' % xmlstr(getattr(self.style, 'inline', self.style))
ref = ''
try:
if self.ref is not None:
ref = ' ref="%s"' % self.ref.toXML()
except AttributeError: pass
label = ''
try:
if self.id != ('a%s' % id(self)):
lid = xmlstr(self.id).strip()
if lid:
label = ' id="%s"' % lid
except AttributeError: pass
extra = ''
if debug:
extra = ' parentNode="%s" ownerDocument="%s"' % \
(id(self.parentNode), id(self.ownerDocument))
if not self.parentNode:
extra += ' xmlns:plastex="http://plastex.sf.net/"'
beginning = False
if getattr(self, 'macroMode', -2) == getattr(self, 'MODE_BEGIN', -1):
beginning = True
ending = ''
if getattr(self, 'macroMode', -2) == getattr(self, 'MODE_END', -1):
ending = '/'
# Bail out early if the element is empty
if not(self.attributes) and not(self.hasChildNodes()):
if ending:
return '</%s%s>' % (name, modifier)
if beginning:
return '<%s%s%s%s%s%s%s>' % (name, modifier, style, source, ref, label, extra)
return '<%s%s%s%s%s%s%s/>' % (name, modifier, style, source, ref, label, extra)
s = ['<%s%s%s%s%s%s%s%s>\n' % (ending, name, modifier, style, source, ref, label, extra)]
# Render attributes
if self.attributes:
for key, value in self.attributes.items():
if value is None:
s.append(' <plastex:arg name="%s"/>\n' % key)
elif isinstance(value, dict):
newdict = {}
for k, v in list(value.items()):
if hasattr(v, 'toXML'):
newdict[k] = v.toXML()
else:
newdict[k] = xmlstr(v)
s.append(' <plastex:arg name="%s">%s</plastex:arg>\n' % (key, newdict))
else:
if hasattr(value, 'toXML'):
value = value.toXML()
else:
value = xmlstr(value)
s.append(' <plastex:arg name="%s">%s</plastex:arg>\n' % (key, value))
# Render content
if self.hasChildNodes():
if not(self.attributes and 'self' in self.attributes):
for value in self.childNodes:
if hasattr(value, 'toXML'):
value = value.toXML()
else:
value = xmlstr(value)
s.append(value)
s.append('</%s>' % name)
return ''.join(s)
@property
def childNodes(self):
try:
return self._dom_childNodes
except AttributeError:
pass
# Allow the `self` key of attributes to act as the `childNodes`
a = self.attributes
if a and 'self' in a:
nodes = a['self']
if nodes is None:
nodes = []
self._dom_childNodes = nodes
return nodes
else:
self._dom_childNodes = nodes = []
return nodes
def hasChildNodes(self):
""" Do we have any child nodes? """
if hasattr(self, '_dom_childNodes'):
return True
a = self.attributes
return a and 'self' in a
@property
def firstChild(self):
""" Return the first child in the list """
if self.hasChildNodes() and self.childNodes: return self.childNodes[0]
@property
def lastChild(self):
""" Return the last child in the list """
if self.hasChildNodes() and self.childNodes: return self.childNodes[-1]
previousSibling = property(_previousSibling)
nextSibling = property(_nextSibling)
compareDocumentPosition = _compareDocumentPosition
def insertBefore(self, newChild, refChild):
"""
Insert `newChild` before `refChild`
Required Arguments:
newChild -- the child to insert
refChild -- the child that `newChild` should be inserted before
Returns:
`newChild`
"""
try: self.removeChild(newChild)
except NotFoundErr: pass
# Insert the new item
for i, item in enumerate(self):
if item is refChild:
self.insert(i, newChild)
return newChild
raise NotFoundErr
def insertAfter(self, newChild, refChild):
"""
Insert `newChild` after `refChild`
Required Arguments:
newChild -- the child to insert
refChild -- the child that `newChild` should be inserted after
Returns:
`newChild`
"""
try: self.removeChild(newChild)
except NotFoundErr: pass
# Insert the new item
for i, item in enumerate(self):
if item is refChild:
self.insert(i+1, newChild)
return newChild
raise NotFoundErr
def replaceChild(self, newChild, oldChild):
"""
Replace `newChild` with `oldChild`
Required Arguments:
newChild -- the child to insert
oldChild -- the child that `newChild` will replace
Returns:
`oldChild`
"""
try: self.removeChild(newChild)
except NotFoundErr: pass
# Do the replacement
for i, item in enumerate(self):
if item is oldChild:
self.pop(i)
self.insert(i, newChild)
return oldChild
raise NotFoundErr
def removeChild(self, oldChild):
"""
Remove 'oldChild' from list of children
Required Arguments:
oldChild -- the child to remove
Returns:
`oldChild`
"""
for i, item in enumerate(self):
if item is oldChild:
return self.pop(i)
raise NotFoundErr
remove = removeChild
def pop(self, index=-1):
"""
Pop an item from the list
Required Arguments:
index -- the index of the item to remove
Returns:
the item removed from the list
"""
try: return self.childNodes.pop(index)
except: raise IndexError('object has no childNodes')
def append(self, newChild, setParent=True):
"""
Append `newChild` to child list
Required Arguments:
newChild -- the child to add
Returns:
`newChild`
"""
if type(newChild) == str:
newChild = self.ownerDocument.createTextNode(newChild)
if newChild.nodeType == Node.DOCUMENT_FRAGMENT_NODE:
for item in newChild:
self.append(item, setParent=setParent)
else:
self.childNodes.append(newChild)
if setParent:
if self.nodeType == self.DOCUMENT_FRAGMENT_NODE:
newChild.parentNode = self.parentNode
else:
newChild.parentNode = self
newChild.ownerDocument = self.ownerDocument
return newChild
appendChild = append
def insert(self, i, newChild, setParent=True):
"""
Insert `newChild` into child list at position `i`
Required Arguments:
i -- the position to insert the new child
newChild -- the object to insert
Returns:
`newChild`
"""
if type(newChild) == str:
newChild = self.ownerDocument.createTextNode(newChild)
if newChild.nodeType == Node.DOCUMENT_FRAGMENT_NODE:
for item in newChild:
self.insert(i, item, setParent=setParent)
i += 1
else:
self.childNodes.insert(i, newChild)
if setParent:
if self.nodeType == self.DOCUMENT_FRAGMENT_NODE:
newChild.parentNode = self.parentNode
else:
newChild.parentNode = self
newChild.ownerDocument = self.ownerDocument
return newChild
def __setitem__(self, i, node):
"""
Set the item at index `i` to `node`
Required Arguments:
i -- the index to set the item of
node -- the object to put at that index
"""
if type(node) == str:
node = self.ownerDocument.createTextNode(node)
# If a DocumentFragment is being inserted, but it isn't replacing
# a slice, we need to put each child in manually.
if node.nodeType == Node.DOCUMENT_FRAGMENT_NODE \
and not(isinstance(i, slice)):
for item in node:
self.insert(i, item)
i += 1
self.pop(i)
else:
self.insert(i, node)
self.pop(i+1)
def extend(self, other, setParent=True):
""" self += other """
for item in other:
self.append(item, setParent=setParent)
return self
__iadd__ = extend
def appendText(self, text, charsubs=None, setParent=True):
""" Append a list of text nodes as one node """
charsubs = charsubs or []
if not text:
return
value = ''.join(text)
for src, dest in charsubs:
value = value.replace(src, dest)
text[:] = []
value = self.ownerDocument.createTextNode(value)
if setParent:
value.parentNode = self
value.ownerDocument = self.ownerDocument
self.appendChild(value)
def __radd__(self, other):
""" other + self """
obj = type(self)()
obj.ownerDocument = self.ownerDocument
obj.parentNode = None
for item in other:
obj.append(item)
for item in self:
obj.append(item)
return obj
def __add__(self, other):
""" self + other """
obj = type(self)()
obj.ownerDocument = self.ownerDocument
obj.parentNode = None
for item in self:
obj.append(item)
for item in other:
obj.append(item)
return obj
def cloneNode(self, deep=False):
"""
Clone the current node
Required Arguments:
deep -- boolean indicating if the copy is a deep copy
Returns:
new node
"""
node = type(self)()
try: node.nodeName = self.nodeName
except: pass
# node.nodeValue = self.nodeValue
# node.nodeType = self.nodeType
node.parentNode = self.parentNode
node.ownerDocument = self.ownerDocument
if deep:
if node.attributes is not None and self.attributes is not None:
node.attributes.update(self.attributes)
if self.hasChildNodes():
for x in self.childNodes:
node.append(x.cloneNode(deep))
else:
if node.attributes is not None and self.attributes is not None:
node.attributes.update(self.attributes)
if self.hasChildNodes():
for x in self.childNodes:
node.append(x)
return node
def normalize(self, charsubs=None):
"""
Combine consecutive text nodes and remove comments
Keyword Arguments:
charsubs -- a list of two-element tuples that contain string
replacements. The first element in each tuple is the source
string. The second element is the string to convert the
source to.
"""
charsubs = charsubs or []
if self.hasAttributes():
for key, value in self.attributes.items():
if isinstance(value, Node) and key not in self.nonNormalizedAttrs:
value.normalize(charsubs)
if not self.hasChildNodes():
return
nodes = list(self.childNodes)
while self.childNodes:
self.childNodes.pop()
text = []
for item in nodes:
if item.nodeType == item.TEXT_NODE:
text.append(item)
continue
self.appendText(text, charsubs)
self.appendChild(item)
item.normalize(charsubs)
self.appendText(text, charsubs)
def isSupported(self, feature, version):
""" Is the requested feature supported? """
return True
def hasAttributes(self):
""" Are there any attributes set? """
return bool(self.attributes)
@property
def textContent(self):
""" Get the text content of the current node """
output = []
if getattr(self, 'str', None) is not None:
output.append(self.str)
else:
for item in self:
if item.nodeType == Node.TEXT_NODE:
output.append(item)
elif getattr(item, 'str', None) is not None:
output.append(item.str)
else:
output.append(item.textContent)
if self.ownerDocument is not None:
return self.ownerDocument.createTextNode(''.join(output))
else:
return Text(''.join(output))
def isSameNode(self, other):
""" Is this the same node as `other`? """
return other is self
def lookupPrefix(self, ns):
""" Lookup the prefix for the given namespace """
return None
def isDefaultNamespace(self, ns):
"""
Is `ns` the default namespace?
Required Arguments:
ns -- requested namespace
Returns:
boolean indicating whether this is the default namespace or not
"""
if ns is None: return True
return False
def lookupNamespaceURI(self, ns):
"""
Lookup the namespace URI for `ns`
Required Arguments:
ns -- the namespace to lookup
Returns:
the namespace URI
"""
return None
def isEqualNode(self, other):
""" Is this node equivalent to `other`? """
return other == self
def __eq__(self, other):
try:
return (self.nodeName == other.nodeName and
self.attributes == other.attributes and
self.childNodes == other.childNodes)
except:
return False
def __hash__(self):
return id(self)
def __lt__(self, other):
try:
res = self.nodeName < other.nodeName
if res: return res
res = self.attributes < other.attributes
if res: return res
if self.hasChildNodes() and other.hasChildNodes():
return self.childNodes < other.childNodes
except (AttributeError, TypeError):
pass
return self.nodeName < other.nodeName
def getFeature(self, feature, version):
""" Get the requested feature """
return None
def setUserData(self, key, data, handler=None):
"""
Set user data
Required Arguments:
key -- the name to store the data under
data -- the data to store
Keyword Arguments:
handler -- data handler
"""
self.userdata[key] = data
def getUserData(self, key):
"""
Get the user data at `key`
Required Arguments:
key -- the name of the data entry to get
Returns:
the stored value, or None if it wasn't set
"""
try: return self.userdata[key]
except (AttributeError, KeyError): pass
return None
@property
def userdata(self):
try:
return self._dom_userdata
except AttributeError:
pass
userdata = DOMUserData()
self._dom_userdata = userdata
return userdata
def __iter__(self):
if self.hasChildNodes():
return iter(self.childNodes)
return iter([])
def __len__(self):
if self.hasChildNodes():
return len(self.childNodes)
return 0
def __getitem__(self, i):
if self.hasChildNodes():
return self.childNodes[i]
raise IndexError('object has no childNodes')
@property
def allChildNodes(self):
""" Return a list containing all of the child nodes in the branch """
nodes = []
if not self.hasChildNodes():
return nodes
for child in self.childNodes:
nodes.append(child)
nodes.extend(child.allChildNodes)
return nodes
def _getElementsByTagName(self, tagname):
"""
Get a list of nodes with the given name
Required Arguments:
tagname -- the name or list of names of the elements to find
Returns:
list of elements
"""
output = NodeList()
# Allow a list of names
if not isinstance(tagname, (tuple,list)):
tagname = [tagname]
# Look in attributes dictionary for document fragments as well
if self.attributes and list(self.attributes.keys()):
for item in list(self.attributes.values()):
if getattr(item, 'tagName', None) in tagname:
output.append(item)
if hasattr(item, 'getElementsByTagName'):
output += item.getElementsByTagName(tagname)
elif isinstance(item, list):
for e in item:
if getattr(e, 'tagName', None) in tagname:
output.append(e)
if hasattr(item, 'getElementsByTagName'):
output += item.getElementsByTagName(tagname)
elif isinstance(item, dict):
for e in list(item.values()):
if getattr(e, 'tagName', None) in tagname:
output.append(e)
if hasattr(item, 'getElementsByTagName'):
output += item.getElementsByTagName(tagname)
# Now look in the child elements
for item in self:
if getattr(item, 'tagName', None) in tagname:
output.append(item)
if hasattr(item, 'getElementsByTagName'):
output += item.getElementsByTagName(tagname)
return output
def _getElementById(self, elementId):
"""
Get element with the given ID
Required Arguments:
elementId -- ID of the element to find
Returns:
element with the given ID
"""
# Look in attributes dictionary for document fragments as well
if getattr(self, 'attributes', None):
for item in list(self.attributes.values()):
if id(item) == elementId:
return item
if hasattr(item, 'getElementById'):
e = item.getElementsById(elementId)
if e is not None:
return e
elif isinstance(item, list):
for e in item:
if id(e) == elementId:
return e
if hasattr(item, 'getElementById'):
e = item.getElementById(elementId)
if e is not None:
return e
elif isinstance(item, dict):
for e in list(item.values()):
if id(e) == elementId:
return e
if hasattr(item, 'getElementById'):
e = item.getElementById(elementId)
if e is not None:
return e
# Now look in the child elements
for item in self:
if id(item) == elementId:
return item
if hasattr(item, 'getElementById'):
e = item.getElementById(elementId)
if e is not None:
return e
return None
class DocumentFragment(Node):
"""
Document Fragment
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-B63ED1A3
"""
nodeName = '#document-fragment'
nodeType = Node.DOCUMENT_FRAGMENT_NODE
__slots__ = Node.NODE_SLOTS
def getElementsByTagNameNS(self, namespaceURI, localName):
"""
Get list of elements of a specific name and namespace
Required Arguments:
namespaceURI -- namespace of the element
localName -- name of the element to find
Returns:
list of elements
"""
return self.getElementsByTagName(localName)
getElementById = _getElementById
getElementsByTagName = _getElementsByTagName
class Attr(Node):
"""
Attr
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-637646024
"""
nodeType = Node.ATTRIBUTE_NODE
# __slots__ = Node.NODE_SLOTS + ['name']
name = None # type: Optional[str]
specified = None
value = None # type: Optional[str]
ownerElement = None
schemaTypeInfo = None
isId = None
def __repr__(self):
return '<%s attribute at 0x%s>' % (self.nodeName, id(self))
@property # type: ignore # mypy#4125
def nodeName(self) -> Optional[str]: # type: ignore # mypy#4125
return self.name
@nodeName.setter
def nodeName(self, value):
self.name = value
@property # type: ignore # mypy#4125
def nodeValue(self):
return self.value
@nodeValue.setter
def nodeValue(self, value):
self.value = value
class Element(Node):
"""
Element
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-745549614
"""
nodeType = Node.ELEMENT_NODE
# __slots__ = Node.ELEMENT_SLOTS
def __repr__(self):
return '<%s element at 0x%s>' % (self.nodeName, id(self))
@property
def attributes(self):
try:
return self._dom_attributes
except AttributeError:
pass
nnm = NamedNodeMap()
nnm.parentNode = self
self._dom_attributes = nnm
return nnm
@property
def tagName(self):
return self.nodeName
@tagName.setter
def tagName(self, value):
self.nodeName = value
def getAttribute(self, name):
"""
Get attribute with name `name`
Required Arguments:
name -- name of attribute to retrieve
Returns:
value of attribute, or None if it doesn't exist
"""
return self.attributes.get(name)
def setAttribute(self, name, value):
"""
Set attribute
Required Arguments:
name -- name of attribute to set
value -- value to set the attribute to
"""
self.attributes[name] = value
def removeAttribute(self, name):
"""
Remove attribute
Required Arguments:
name -- name of attribute to remove
"""
try: del self.attributes[name]
except KeyError: pass
getAttributeNode = getAttribute
def setAttributeNode(self, newAttr):
"""
Set an attribute node
Required Arguments:
newAttr -- attribute node
"""
self.setAttribute(newAttr.name, newAttr)
def removeAttributeNode(self, oldAttr):
"""
Remove an attribute node
Required Arguments:
oldAttr -- attribute node to remove
"""
self.removeAttribute(oldAttr.name)
getElementsByTagName = _getElementsByTagName
def getAttributeNS(self, namespaceURI, localName):
"""
Get attribute in given namespace
Required Arguments:
namespaceURI -- namespace of attribute
localName -- name of attribute
Returns:
attribute
"""
return self.getAttribute(localName)
def setAttributeNS(self, namespaceURI, qualifiedName, value):
"""
Set an attribute with given namespace
Required Argument:
namespaceURI -- namespace of attribute
qualifiedName -- name of attribute
value -- value of the attribute
"""
return self.setAttribute(qualifiedName, value)
def removeAttributeNS(self, namespaceURI, localName):
"""
Remove an attribute in the given namespace
Required Arguments:
namespaceURI -- namespace of attribute
localName -- name of attribute
"""
return self.removeAttribute(localName)
def getAttributeNodeNS(self, namespaceURI, localName):
"""
Get attribute from given namespace
Required Arguments:
namespaceURI -- namespace of attribute
localName -- name of attribute
Returns:
attribute node
"""
return self.getAttributeNode(localName)
setAttributeNodeNS = setAttributeNode
def getElementsByTagNameNS(self, namespaceURI, localName):
"""
Get elements with tag name in given namespace
Required Arguments:
namespaceURI -- namespace of element
localName -- name of element to retrieve
Returns:
list of elements
"""
return _getElementsByTagName(localName)
def hasAttribute(self, name):
"""
Does the attribute exist?
Required Arguments:
name -- name of attribute to look for
Returns:
boolean indicating whether or not the attribute exists
"""
return name in list(self.attributes.keys())
def hasAttributeNS(self, namespaceURI, localName):
"""
Does the attribute in the given namespace exist
Required Arguments:
namespaceURI -- namespace of attribute
localName -- name of attribute
Returns:
boolean indicating whether or not the attribute exists
"""
return self.hasAttribute(localName)
def setIdAttribute(self, name, isId=True):
"""
Set attribute as an ID attribute
Required Arguments:
name -- name of attribute
Keyword Arguments:
isId -- boolean indicating whether this attribute should be an
ID attribute or not
"""
try: self.attributes[name].isId = isId
except KeyError: raise NotFoundErr
def setIdAttributeNS(self, namespaceURI, localName, isId=True):
"""
Set attribute as an ID attribute in the given namespace
Required Arguments:
namespaceURI -- namespace of attribute
localName -- name of attribute
Keyword Arguments:
isId -- boolean indicating whether this attribute should be an
ID attribute or not
"""
self.setIdAttribute(localName, isId)
def setIdAttributeNode(self, idAttr, isId=True):
"""
Set attribute node as an ID attribute node
Required Arguments:
idAttr -- attribute node
Keyword Arguments:
isId -- boolean indicating whether this attribute should be an
ID attribute or not
"""
self.setIdAttribute(idAttr.name, isId)
class CharacterData(str, Node):
"""
Character Data
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-FF21A306
This class doesn't follow the entire API. Because it is also
a subclass of str it is immutable making methods like insertData,
deleteData, etc. impossible.
"""
__slots__ = Node.TEXT_SLOTS
_dummyChildNodes = []
@property
def childNodes(self):
return CharacterData._dummyChildNodes
# LaTeX extension that allows getting the LaTeX source from a plain string
@property
def source(self):
return self
@property
def str(self):
return self
def toXML(self, *args, **kwargs):
return xmlstr(self)
@property
def nodeValue(self):
return self
def cloneNode(self, deep=True):
o = type(self)(self)
o.ownerDocument = self.ownerDocument
o.parentNode = self.parentNode
return o
@property
def data(self):
return self
@property
def length(self):
""" Number of characters in string """
return len(self)
def _notImplemented(self, *args, **kwargs):
raise NotImplementedError
substringData = _notImplemented
appendData = _notImplemented
insertData = _notImplemented
deleteData = _notImplemented
replaceData = _notImplemented
insertBefore = _notImplemented
replaceChild = _notImplemented
removeChild = _notImplemented
appendChild = _notImplemented
def normalize(self, charsubs=None):
pass
@property
def textContent(self):
return self
def getElementsByTagName(self, name):
return []
def __add__(self, other):
return str.__add__(self, other)
def __radd__(self, other):
return other.__add__(self)
def __len__(self):
return str.__len__(self)
def __iter__(self):
for i in range(len(self)):
yield self[i]
def __getitem__(self, i):
return str.__getitem__(self, i)
def __str__(self):
return self
class Text(CharacterData):
"""
Text
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-1312295772
"""
nodeName = '#text'
nodeType = Node.TEXT_NODE
__slots__ = Node.TEXT_SLOTS
replaceWholeText = CharacterData._notImplemented
splitText = CharacterData._notImplemented
@property
def isElementContentWhitespace(self):
return not(self.strip())
@property
def wholeText(self):
""" Return text from siblings and self """
return self.parentNode.textContent
class TypeInfo(object):
"""
Type Info
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#TypeInfo
"""
# DerivationMethods
DERIVATION_RESTRICTION = 0x00000001
DERIVATION_EXTENSION = 0x00000002
DERIVATION_UNION = 0x00000004
DERIVATION_LIST = 0x00000008
typeName = None
typeNamespace = None
def isDerivedFrom(self, typeNamespaceArg, typeNameArg, derivationMethod):
raise NotImplementedError
class UserDataHandler(object):
"""
User Data Handler
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#UserDataHandler
"""
# OperationType
NODE_CLONED = 1
NODE_IMPORTED = 2
NODE_DELETED = 3
NODE_RENAMED = 4
NODE_ADOPTED = 5
def handle(self, operation, key, data, src, dst):
raise NotImplementedError
class DOMError(object):
"""
DOM Error
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ERROR-Interfaces-DOMError
"""
# ErrorSeverity
SEVERITY_WARNING = 1
SEVERITY_ERROR = 2
SEVERITY_FATAL_ERROR = 3
severity = None
message = None
type = None
relatedException = None
relatedData = None
location = None
class DOMErrorHandler(object):
"""
DOM Error Handler
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ERROR-Interfaces-DOMErrorHandler
"""
def handleError(self, error):
raise NotImplementedError
class DOMLocator(object):
"""
DOM Locator
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Interfaces-DOMLocator
"""
lineNumber = 0
columnNumber = 0
byteOffset = 0
utf16Offset = 0
relatedNode = None
uri = None
class DOMConfiguration(dict):
"""
DOM Configuration
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#DOMConfiguration
"""
def __init__(self, data=None):
dict.__init__(self, data or {})
self['canonical-form'] = False
self['cdata-sections'] = True
self['check-character-normalization'] = False
self['comments'] = True
self['datatype-normalization'] = False
self['element-content-whitespace'] = True
self['entities'] = True
self['error-handler'] = DOMErrorHandler()
self['infoset'] = True
self['namespaces'] = True
self['namespace-declarations'] = True
self['normalize-characters'] = False
self['schema-location'] = None
self['schema-type'] = None
self['split-cdata-sections'] = True
self['validate'] = False
self['validate-if-schema'] = False
self['well-formed'] = True
@property
def parameterNames(self):
""" Return list of all possible parameter names """
return list(self.keys())
def setParameter(self, name, value):
"""
Set the given parameter
Required Arguments:
name -- name of parameter
value -- value of parameter
"""
if name in list(self.keys()):
raise NotFoundErr
self[name] = value
def getParameter(self, name):
"""
Get the specified paramater value
Required Arguments:
name -- name of parameter
Returns:
value of parameter `name`
"""
try: return self[name]
except KeyError: raise NotFoundErr
def canSetParameter(self, name, value):
"""
Can the parameter `name` be set to `value`?
Required Arguments:
name -- name of parameter
value -- value of parameter
Returns:
boolean indicating whether the parameter value can be set or not
"""
return True
class CDATASection(Text):
"""
CDATA Section
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-667469212
"""
nodeName = '#cdata-section'
nodeType = Node.CDATA_SECTION_NODE
__slots__ = Node.TEXT_SLOTS
class DocumentType(Node):
"""
Document Type
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-412266927
"""
nodeType = Node.DOCUMENT_TYPE_NODE
__slots__ = Node.NODE_SLOTS
name = None
entities = None
notations = None
publicId = None
systemId = None
internalSubset = None
@property # type: ignore # mypy#4125
def nodeName(self):
return self.name
@nodeName.setter
def nodeName(self, value):
self.name = value
class Notation(Node):
"""
Notation
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-5431D1B9
"""
nodeType = Node.NOTATION_NODE
__slots__ = Node.NODE_SLOTS
publicId = None
systemId = None
class Entity(Node):
"""
Entity
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-527DCFF2
"""
nodeType = Node.ENTITY_NODE
__slots__ = Node.NODE_SLOTS
publicId = None
systemId = None
notationName = None
inputEncoding = None
xmlEncoding = None
xmlVersion = None
class EntityReference(Node):
"""
Entity Reference
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-11C98490
"""
nodeType = Node.ENTITY_REFERENCE_NODE
__slots__ = Node.NODE_SLOTS
class ProcessingInstruction(Node):
"""
Processing Instruction
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-1004215813
"""
nodeType = Node.PROCESSING_INSTRUCTION_NODE
__slots__ = Node.NODE_SLOTS
target = None # type: Optional[str]
data = None # type: Optional[str]
@property # type: ignore # mypy#4125
def nodeName(self):
return self.target
@nodeName.setter
def nodeName(self, value):
self.target = value
@property # type: ignore # mypy#4125
def nodeValue(self):
return self.data
@nodeName.setter
def nodeValue(self, value):
self.data = value
class Document(Node):
"""
Document
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#i-Document
"""
elementClass = Element
documentFragmentClass = DocumentFragment
textNodeClass = Text
cdataSectionClass = CDATASection
processingInstructionClass = ProcessingInstruction
attributeClass = Attr
entityReferenceClass = EntityReference
nodeName = '#document'
nodeType = Node.DOCUMENT_NODE
#__slots__ = Node.NODE_SLOTS
doctype = None
implementation = None
documentElement = None
inputEncoding = None
xmlEncoding = None
xmlStandalone = None
xmlVersion = None
strictErrorChecking = None
documentURI = None
domConfig = None
@property
def parentNode(self):
return None
@property
def ownerDocument(self):
return self
def createElement(self, tagName):
"""
Instantiate a new element
Required Arguments:
tagName -- the name of the element to create
Returns:
the new instance
"""
o = self.elementClass()
o.nodeName = tagName
o.parentNode = None
o.ownerDocument = self
return o
def createDocumentFragment(self):
""" Instantiate a new document fragment """
o = self.documentFragmentClass()
o.ownerDocument = self
o.parentNode = None
return o
def createTextNode(self, data):
"""
Instantiate a new text node
Required Arguments:
data -- string to initialize text node with
Returns:
new text node
"""
o = self.textNodeClass(data)
o.ownerDocument = self
o.parentNode = None
return o
def createCDATASection(self, data):
"""
Instantiate a new CDATA section
Required Arguments:
data -- string to initialize CDATA section with
Returns:
new CDATA section node
"""
o = self.cdataSectionClass(data)
o.ownerDocument = self
o.parentNode = None
return o
def createProcessingInstruction(self, target, data):
"""
Instantiate a new processing instruction node
Required Arguments:
target --
data -- string to initialize processing instruction with
Returns:
new processing instruction node
"""
o = self.processingInstructionClass(data)
o.ownerDocument = self
o.parentNode = None
return o
def createAttribute(self, name):
"""
Instantiate a new attribute node
Required Arguments:
name -- name of attribute
Returns:
new attribute node
"""
o = self.attributeClass()
o.name = name
o.ownerDocument = self
o.parentNode = None
return o
def createEntityReference(self, name):
"""
Instantiate a new entity reference
Required Arguments:
name -- name of entity
Returns:
new entity reference node
"""
o = self.entityReferenceClass()
o.name = name
o.ownerDocument = self
o.parentNode = None
return o
getElementsByTagName = _getElementsByTagName
def importNode(self, importedNode, deep=False):
"""
Import a node from another document
Required Arguments:
importedNode -- node to import
Keyword Arguments:
deep -- boolean indicating whether this should be a deep copy
Returns:
imported node
"""
node = importedNode.cloneNode(deep)
node.parentNode = self
node.ownerDocument = self
return node
def createElementNS(self, namespaceURI, qualifiedName):
"""
Create an element in the given namespace
Required Arguments:
namespaceURI -- namespace of the new element
qualifiedName -- name of the element
Returns:
new element node
"""
return self.createElement(qualifiedName)
def createAttributeNS(self, namespaceURI, qualifiedName):
"""
Create attribute in the given namespace
Required Arguments:
namespaceURI -- namespace of the attribute
qualifiedName -- name of the attribute
Returns:
new attribute node
"""
return self.createAttribute(qualifiedName)
def getElementsByTagNameNS(self, namespaceURI, localName):
"""
Get list of elements of a specific name and namespace
Required Arguments:
namespaceURI -- namespace of the element
localName -- name of the element to find
Returns:
list of elements
"""
return self.getElementsByTagName(localName)
getElementById = _getElementById
def adoptNode(self, source):
"""
Adopt node into document
Required Arguments:
source -- node to adopt
Returns:
`source`
"""
if source.parentNode is not None:
for i, item in enumerate(source.parentNode):
if item is source:
source.parentNode.pop(i)
self.append(source)
return source
normalizeDocument = Node.normalize
def renameNode(self, n, namespaceURI, qualifiedName):
"""
Rename a node
Required Arguments:
n -- node to rename
namespaceURI -- namespace to use
qualifiedName -- name to change to
Returns:
`n`
"""
raise NotImplementedError
|