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
|
/*
This file is part of the KDE libraries
Copyright (C) 1997 Martin Jones (mjones@kde.org)
(C) 1997 Torben Weis (weis@kde.org)
(C) 1999,2001 Lars Knoll (knoll@kde.org)
(C) 2000,2001 Dirk Mueller (mueller@kde.org)
(C) 2003 Apple Computer, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
//----------------------------------------------------------------------------
//
// KDE HTML Widget -- HTML Parser
// #define PARSER_DEBUG
#include "htmlparser.h"
#include <dom/dom_exception.h>
#include <html/html_baseimpl.h>
#include <html/html_blockimpl.h>
#include <html/html_canvasimpl.h>
#include <html/html_documentimpl.h>
#include <html/html_elementimpl.h>
#include <html/html_formimpl.h>
#include <html/html_headimpl.h>
#include <html/html_imageimpl.h>
#include <html/html_inlineimpl.h>
#include <html/html_listimpl.h>
#include <html/html_miscimpl.h>
#include <html/html_tableimpl.h>
#include <html/html_objectimpl.h>
#include <html/HTMLAudioElement.h>
#include <html/HTMLVideoElement.h>
#include <html/HTMLSourceElement.h>
#include <xml/dom_textimpl.h>
#include <xml/dom_nodeimpl.h>
#include <html/htmltokenizer.h>
#include <khtmlview.h>
#include <khtml_part.h>
#include <khtml_global.h>
#include <css/cssproperties.h>
#include <css/cssvalues.h>
#include <rendering/render_object.h>
#include "khtml_debug.h"
#include <klocalizedstring.h>
// Turn off gnu90 inlining to avoid linker errors
#undef __GNUC_STDC_INLINE__
#undef __GNUC_GNU_INLINE__
#include <doctypes.h>
#undef OPTIONAL // for win32, MinGW
using namespace DOM;
using namespace khtml;
#ifdef PARSER_DEBUG
static QString getParserPrintableName(int id)
{
if (id >= ID_CLOSE_TAG) {
return "/" + getPrintableName(id - ID_CLOSE_TAG);
} else {
return getPrintableName(id);
}
}
#endif
//----------------------------------------------------------------------------
/**
* @internal
*/
class HTMLStackElem
{
public:
HTMLStackElem(int _id,
int _level,
DOM::NodeImpl *_node,
bool _inline_,
HTMLStackElem *_next)
:
id(_id),
level(_level),
strayTableContent(false),
m_inline(_inline_),
node(_node),
next(_next)
{
node->ref();
}
~HTMLStackElem()
{
node->deref();
}
void setNode(NodeImpl *newNode)
{
newNode->ref();
node->deref();
node = newNode;
}
int id;
int level;
bool strayTableContent;
bool m_inline;
NodeImpl *node;
HTMLStackElem *next;
};
/**
* @internal
*
* The parser parses tokenized input into the document, building up the
* document tree. If the document is wellformed, parsing it is
* straightforward.
* Unfortunately, people can't write wellformed HTML documents, so the parser
* has to be tolerant about errors.
*
* We have to take care of the following error conditions:
* 1. The element being added is explicitly forbidden inside some outer tag.
* In this case we should close all tags up to the one, which forbids
* the element, and add it afterwards.
* 2. We are not allowed to add the element directly. It could be, that
* the person writing the document forgot some tag inbetween (or that the
* tag inbetween is optional...) This could be the case with the following
* tags: HTML HEAD BODY TBODY TR TD LI (did I forget any?)
* 3. We wan't to add a block element inside to an inline element. Close all
* inline elements up to the next higher block element.
* 4. If this doesn't help close elements, until we are allowed to add the
* element or ignore the tag.
*
*/
KHTMLParser::KHTMLParser(KHTMLView *_parent, DocumentImpl *doc)
{
//qCDebug(KHTML_LOG) << "parser constructor";
#if SPEED_DEBUG > 0
qt.start();
#endif
HTMLWidget = _parent;
document = doc;
blockStack = nullptr;
current = nullptr;
// ID_CLOSE_TAG == Num of tags
forbiddenTag = new ushort[ID_CLOSE_TAG + 1];
reset();
}
KHTMLParser::KHTMLParser(DOM::DocumentFragmentImpl *i, DocumentImpl *doc)
{
HTMLWidget = nullptr;
document = doc;
forbiddenTag = new ushort[ID_CLOSE_TAG + 1];
blockStack = nullptr;
current = nullptr;
reset();
setCurrent(i);
inBody = true;
}
KHTMLParser::~KHTMLParser()
{
#if SPEED_DEBUG > 0
qCDebug(KHTML_LOG) << "TIME: parsing time was = " << qt.elapsed();
#endif
freeBlock();
if (current) {
current->deref();
}
delete [] forbiddenTag;
delete isindex;
}
void KHTMLParser::reset()
{
setCurrent(document);
freeBlock();
// before parsing no tags are forbidden...
memset(forbiddenTag, 0, (ID_CLOSE_TAG + 1)*sizeof(ushort));
inBody = false;
haveFrameSet = false;
haveContent = false;
haveBody = false;
haveTitle = false;
inSelect = false;
inStrayTableContent = 0;
m_inline = false;
form = nullptr;
map = nullptr;
end = false;
isindex = nullptr;
discard_until = 0;
}
void KHTMLParser::parseToken(Token *t)
{
if (t->tid > 2 * ID_CLOSE_TAG) {
// qCDebug(KHTML_LOG) << "Unknown tag!! tagID = " << t->tid;
return;
}
if (discard_until) {
if (t->tid == discard_until) {
discard_until = 0;
}
// do not skip </iframe>
if (discard_until || current->id() + ID_CLOSE_TAG != t->tid) {
return;
}
}
#ifdef PARSER_DEBUG
qCDebug(KHTML_LOG) << "\n\n==> parser: processing token " << getParserPrintableName(t->tid) << "(" << t->tid << ")"
<< " current = " << getParserPrintableName(current->id()) << "(" << current->id() << ")";
qCDebug(KHTML_LOG) << "inline=" << m_inline << " inBody=" << inBody << " haveFrameSet=" << haveFrameSet << " haveContent=" << haveContent;
#endif
// holy shit. apparently some sites use </br> instead of <br>
// be compatible with IE and NS
if (t->tid == ID_BR + ID_CLOSE_TAG && document->inCompatMode()) {
t->tid -= ID_CLOSE_TAG;
}
if (t->tid > ID_CLOSE_TAG) {
processCloseTag(t);
return;
}
// ignore spaces, if we're not inside a paragraph or other inline code
if (t->tid == ID_TEXT && t->text) {
if (inBody && !skipMode() &&
current->id() != ID_STYLE && current->id() != ID_TITLE &&
current->id() != ID_SCRIPT &&
!t->text->containsOnlyWhitespace()) {
haveContent = true;
}
#ifdef PARSER_DEBUG
qCDebug(KHTML_LOG) << "length=" << t->text->l << " text='" << QString::fromRawData(t->text->s, t->text->l) << "'";
#endif
}
NodeImpl *n = getElement(t);
// just to be sure, and to catch currently unimplemented stuff
if (!n) {
return;
}
// set attributes
if (n->isElementNode() && t->tid != ID_ISINDEX) {
ElementImpl *e = static_cast<ElementImpl *>(n);
e->setAttributeMap(t->attrs);
}
// if this tag is forbidden inside the current context, pop
// blocks until we are allowed to add it...
while (blockStack && forbiddenTag[t->tid]) {
#ifdef PARSER_DEBUG
qCDebug(KHTML_LOG) << "t->id: " << t->tid << " is forbidden :-( ";
#endif
popOneBlock();
}
// sometimes flat doesn't make sense
switch (t->tid) {
case ID_SELECT:
case ID_OPTION:
t->flat = false;
}
// the tokenizer needs the feedback for space discarding
if (tagPriority(t->tid) == 0) {
t->flat = true;
}
if (!insertNode(n, t->flat)) {
// we couldn't insert the node...
#ifdef PARSER_DEBUG
qCDebug(KHTML_LOG) << "insertNode failed current=" << current->id() << ", new=" << n->id() << "!";
#endif
if (map == n) {
#ifdef PARSER_DEBUG
qCDebug(KHTML_LOG) << " --> resetting map!";
#endif
map = nullptr;
}
if (form == n) {
#ifdef PARSER_DEBUG
qCDebug(KHTML_LOG) << " --> resetting form!";
#endif
form = nullptr;
}
delete n;
}
}
void KHTMLParser::parseDoctypeToken(DoctypeToken *t)
{
// Ignore any doctype after the first. TODO It should be also ignored when processing DocumentFragment
if (current != document || document->doctype()) {
return;
}
DocumentTypeImpl *doctype = new DocumentTypeImpl(document->implementation(), document, t->name, t->publicID, t->systemID);
if (!t->internalSubset.isEmpty()) {
doctype->setInternalSubset(t->internalSubset);
}
document->addChild(doctype);
// Determine parse mode here
// This code more or less mimics Mozilla's implementation.
//
// There are three possible parse modes:
// COMPAT - quirks mode emulates WinIE
// and NS4. CSS parsing is also relaxed in this mode, e.g., unit types can
// be omitted from numbers.
// ALMOST STRICT - This mode is identical to strict mode
// except for its treatment of line-height in the inline box model. For
// now (until the inline box model is re-written), this mode is identical
// to STANDARDS mode.
// STRICT - no quirks apply. Web pages will obey the specifications to
// the letter.
if (!document->isHTMLDocument()) { // FIXME Could document be non-HTML?
return;
}
DOM::HTMLDocumentImpl *htmldoc = static_cast<DOM::HTMLDocumentImpl *>(document);
if (t->name.toLower() == "html") {
if (!t->internalSubset.isEmpty() || t->publicID.isEmpty()) {
// Internal subsets always denote full standards, as does
// a doctype without a public ID.
htmldoc->changeModes(DOM::DocumentImpl::Strict, DOM::DocumentImpl::Html4);
} else {
// We have to check a list of public IDs to see what we
// should do.
QString lowerPubID = t->publicID.toLower();
QByteArray pubIDStr = lowerPubID.toLocal8Bit();
// Look up the entry in our gperf-generated table.
const PubIDInfo *doctypeEntry = findDoctypeEntry(pubIDStr.constData(), t->publicID.length());
if (!doctypeEntry) {
// The DOCTYPE is not in the list. Assume strict mode.
// ### Doesn't make any sense, but it's what Mozilla does.
htmldoc->changeModes(DOM::DocumentImpl::Strict, DOM::DocumentImpl::Html4);
} else {
switch ((!t->systemID.isEmpty()) ?
doctypeEntry->mode_if_sysid :
doctypeEntry->mode_if_no_sysid) {
case PubIDInfo::eQuirks3:
htmldoc->changeModes(DOM::DocumentImpl::Compat, DOM::DocumentImpl::Html3);
break;
case PubIDInfo::eQuirks:
htmldoc->changeModes(DOM::DocumentImpl::Compat, DOM::DocumentImpl::Html4);
break;
case PubIDInfo::eAlmostStandards:
htmldoc->changeModes(DOM::DocumentImpl::Transitional, DOM::DocumentImpl::Html4);
break;
default:
assert(!"Unknown parse mode");
}
}
}
} else {
// Malformed doctype implies quirks mode.
htmldoc->changeModes(DOM::DocumentImpl::Compat, DOM::DocumentImpl::Html3);
}
}
static bool isTableRelatedTag(int id)
{
return (id == ID_TR || id == ID_TD || id == ID_TABLE || id == ID_TBODY || id == ID_TFOOT || id == ID_THEAD ||
id == ID_TH);
}
bool KHTMLParser::insertNode(NodeImpl *n, bool flat)
{
int id = n->id();
// <table> is never allowed inside stray table content. Always pop out of the stray table content
// and close up the first table, and then start the second table as a sibling.
if (inStrayTableContent && id == ID_TABLE) {
popBlock(ID_TABLE);
}
// let's be stupid and just try to insert it.
// this should work if the document is wellformed
#ifdef PARSER_DEBUG
NodeImpl *tmp = current;
#endif
NodeImpl *newNode = current->addChild(n);
if (newNode) {
#ifdef PARSER_DEBUG
qCDebug(KHTML_LOG) << "added " << n->nodeName().string() << " to " << tmp->nodeName().string() << ", new current=" << newNode->nodeName().string();
#endif
// We allow TABLE > FORM in dtd.cpp, but do not allow the form have children in this case
if (current->id() == ID_TABLE && id == ID_FORM) {
flat = true;
static_cast<HTMLFormElementImpl *>(n)->setMalformed(true);
}
// don't push elements without end tag on the stack
if (tagPriority(id) != 0 && !flat) {
#if SPEED_DEBUG < 2
if (!n->attached() && HTMLWidget) {
n->attach();
}
#endif
if (n->isInline()) {
m_inline = true;
}
pushBlock(id, tagPriority(id));
setCurrent(newNode);
} else {
#if SPEED_DEBUG < 2
if (!n->attached() && HTMLWidget) {
n->attach();
}
if (n->maintainsState()) {
document->registerMaintainsState(n);
document->attemptRestoreState(n);
}
n->close();
#endif
if (n->isInline()) {
m_inline = true;
}
}
#if SPEED_DEBUG < 1
if (tagPriority(id) == 0 && n->renderer()) {
n->renderer()->calcMinMaxWidth();
}
#endif
return true;
} else {
#ifdef PARSER_DEBUG
qCDebug(KHTML_LOG) << "ADDING NODE FAILED!!!! current = " << current->nodeName().string() << ", new = " << n->nodeName().string();
#endif
// error handling...
HTMLElementImpl *e;
bool handled = false;
// first switch on current element for elements with optional end-tag and inline-only content
switch (current->id()) {
case ID_P:
case ID_DT:
if (!n->isInline()) {
popBlock(current->id());
return insertNode(n);
}
break;
case ID_TITLE:
popBlock(current->id());
return insertNode(n);
default:
break;
}
// switch according to the element to insert
switch (id) {
case ID_TR:
case ID_TH:
case ID_TD:
if (inStrayTableContent && !isTableRelatedTag(current->id())) {
// pop out to the nearest enclosing table-related tag.
while (blockStack && !isTableRelatedTag(current->id())) {
popOneBlock();
}
return insertNode(n);
}
break;
case ID_HEAD:
// ### allow not having <HTML> in at all, as per HTML spec
if (!current->isDocumentNode() && current->id() != ID_HTML) {
return false;
}
break;
case ID_COMMENT:
if (head) {
break;
}
case ID_META:
case ID_LINK:
case ID_ISINDEX:
case ID_BASE:
if (!head) {
createHead();
}
if (head) {
if (head->addChild(n)) {
#if SPEED_DEBUG < 2
if (!n->attached() && HTMLWidget) {
n->attach();
}
#endif
}
return true;
}
break;
case ID_HTML:
if (!current->isDocumentNode()) {
if (doc()->documentElement()->id() == ID_HTML) {
// we have another <HTML> element.... apply attributes to existing one
// make sure we don't overwrite already existing attributes
NamedAttrMapImpl *map = static_cast<ElementImpl *>(n)->attributes(true);
NamedAttrMapImpl *bmap = static_cast<ElementImpl *>(doc()->documentElement())->attributes(false);
bool changed = false;
for (unsigned long l = 0; map && l < map->length(); ++l) {
NodeImpl::Id attrId = map->idAt(l);
DOMStringImpl *attrValue = map->valueAt(l);
changed = !bmap->getValue(attrId);
bmap->setValue(attrId, attrValue);
}
if (changed) {
doc()->recalcStyle(NodeImpl::Inherit);
}
}
return false;
}
break;
case ID_TITLE:
case ID_STYLE:
if (!head) {
createHead();
}
if (head) {
DOM::NodeImpl *newNode = head->addChild(n);
if (newNode) {
pushBlock(id, tagPriority(id));
setCurrent(newNode);
#if SPEED_DEBUG < 2
if (!n->attached() && HTMLWidget) {
n->attach();
}
#endif
} else {
#ifdef PARSER_DEBUG
qCDebug(KHTML_LOG) << "adding style before to body failed!!!!";
#endif
discard_until = ID_STYLE + ID_CLOSE_TAG;
return false;
}
return true;
} else if (inBody) {
discard_until = id + ID_CLOSE_TAG;
return false;
}
break;
case ID_SCRIPT:
// if we failed to insert it, go into skip mode
discard_until = id + ID_CLOSE_TAG;
break;
case ID_BODY:
if (inBody && doc()->body()) {
// we have another <BODY> element.... apply attributes to existing one
// make sure we don't overwrite already existing attributes
// some sites use <body bgcolor=rightcolor>...<body bgcolor=wrongcolor>
NamedAttrMapImpl *map = static_cast<ElementImpl *>(n)->attributes(true);
NamedAttrMapImpl *bmap = doc()->body()->attributes(false);
bool changed = false;
for (unsigned long l = 0; map && l < map->length(); ++l) {
NodeImpl::Id attrId = map->idAt(l);
DOMStringImpl *attrValue = map->valueAt(l);
if (!bmap->getValue(attrId)) {
bmap->setValue(attrId, attrValue);
changed = true;
}
}
if (changed) {
doc()->recalcStyle(NodeImpl::Inherit);
}
} else if (current->isDocumentNode()) {
break;
}
return false;
break;
// the following is a hack to move non rendered elements
// outside of tables.
// needed for broken constructs like <table><form ...><tr>....
case ID_INPUT: {
ElementImpl *e = static_cast<ElementImpl *>(n);
DOMString type = e->getAttribute(ATTR_TYPE);
if (strcasecmp(type, "hidden") != 0) {
break;
}
// Fall through!
}
case ID_TEXT: {
// Don't try to fit random white-space anywhere
TextImpl *t = static_cast<TextImpl *>(n);
if (t->containsOnlyWhitespace()) {
return false;
}
// ignore text inside the following elements.
switch (current->id()) {
case ID_SELECT:
return false;
default:
;
// fall through!!
};
break;
}
case ID_DL:
popBlock(ID_DT);
if (current->id() == ID_DL) {
e = new HTMLGenericElementImpl(document, ID_DD);
insertNode(e);
handled = true;
}
break;
case ID_DT:
e = new HTMLDListElementImpl(document);
if (insertNode(e)) {
insertNode(n);
return true;
}
break;
case ID_AREA: {
if (map) {
map->addChild(n);
#if SPEED_DEBUG < 2
if (!n->attached() && HTMLWidget) {
n->attach();
}
#endif
handled = true;
return true;
} else {
return false;
}
}
case ID_THEAD:
case ID_TBODY:
case ID_TFOOT:
case ID_CAPTION:
case ID_COLGROUP: {
if (isTableRelatedTag(current->id())) {
while (blockStack && current->id() != ID_TABLE && isTableRelatedTag(current->id())) {
popOneBlock();
}
return insertNode(n);
}
}
default:
break;
}
// switch on the currently active element
switch (current->id()) {
case ID_HTML:
switch (id) {
case ID_SCRIPT:
case ID_STYLE:
case ID_META:
case ID_LINK:
case ID_OBJECT:
case ID_EMBED:
case ID_TITLE:
case ID_ISINDEX:
case ID_BASE:
if (!head) {
head = new HTMLHeadElementImpl(document);
insertNode(head.get());
handled = true;
}
break;
case ID_TEXT: {
TextImpl *t = static_cast<TextImpl *>(n);
if (t->containsOnlyWhitespace()) {
return false;
}
/* Fall through to default */
}
default:
if (haveFrameSet) {
break;
}
e = new HTMLBodyElementImpl(document);
startBody();
insertNode(e);
handled = true;
break;
}
break;
case ID_HEAD:
// we can get here only if the element is not allowed in head.
if (id == ID_HTML) {
return false;
} else {
// This means the body starts here...
if (haveFrameSet) {
break;
}
popBlock(ID_HEAD);
e = new HTMLBodyElementImpl(document);
startBody();
insertNode(e);
handled = true;
}
break;
case ID_BODY:
break;
case ID_CAPTION:
// Illegal content in a caption. Close the caption and try again.
popBlock(ID_CAPTION);
switch (id) {
case ID_THEAD:
case ID_TFOOT:
case ID_TBODY:
case ID_TR:
case ID_TD:
case ID_TH:
return insertNode(n, flat);
}
break;
case ID_TABLE:
case ID_THEAD:
case ID_TFOOT:
case ID_TBODY:
case ID_TR:
switch (id) {
case ID_TABLE:
popBlock(ID_TABLE); // end the table
handled = checkChild(current->id(), id, doc()->inStrictMode());
break;
default: {
NodeImpl *node = current;
NodeImpl *parent = node->parentNode();
// A script may have removed the current node's parent from the DOM
// http://bugzilla.opendarwin.org/show_bug.cgi?id=7137
// FIXME: we should do real recovery here and re-parent with the correct node.
if (!parent) {
return false;
}
NodeImpl *parentparent = parent->parentNode();
if (n->isTextNode() ||
(node->id() == ID_TR &&
(parent->id() == ID_THEAD ||
parent->id() == ID_TBODY ||
parent->id() == ID_TFOOT) && parentparent->id() == ID_TABLE) ||
(!checkChild(ID_TR, id) && (node->id() == ID_THEAD || node->id() == ID_TBODY || node->id() == ID_TFOOT) &&
parent->id() == ID_TABLE)) {
node = (node->id() == ID_TABLE) ? node :
((node->id() == ID_TR) ? parentparent : parent);
NodeImpl *parent = node->parentNode();
if (!parent) {
return false;
}
int exceptioncode = 0;
#ifdef PARSER_DEBUG
qCDebug(KHTML_LOG) << "calling insertBefore(" << n->nodeName().string() << "," << node->nodeName().string() << ")";
#endif
parent->insertBefore(n, node, exceptioncode);
if (exceptioncode) {
#ifndef PARSER_DEBUG
if (!n->isTextNode())
#endif
// qCDebug(KHTML_LOG) << "adding content before table failed..";
break;
}
if (n->isElementNode() && tagPriority(id) != 0 &&
!flat && endTagRequirement(id) != DOM::FORBIDDEN) {
pushBlock(id, tagPriority(id));
setCurrent(n);
inStrayTableContent++;
blockStack->strayTableContent = true;
}
return true;
}
if (current->id() == ID_TR) {
e = new HTMLTableCellElementImpl(document, ID_TD);
} else if (current->id() == ID_TABLE) {
e = new HTMLTableSectionElementImpl(document, ID_TBODY, true /* implicit */);
} else {
e = new HTMLTableRowElementImpl(document);
}
insertNode(e);
handled = true;
break;
} // end default
} // end switch
break;
case ID_OBJECT:
discard_until = id + ID_CLOSE_TAG;
return false;
case ID_UL:
case ID_OL:
case ID_DIR:
case ID_MENU:
e = new HTMLLIElementImpl(document);
e->addCSSProperty(CSS_PROP_LIST_STYLE_TYPE, CSS_VAL_NONE);
insertNode(e);
handled = true;
break;
case ID_FORM:
popBlock(ID_FORM);
handled = true;
break;
case ID_SELECT:
if (n->isInline()) {
return false;
}
break;
case ID_P:
case ID_H1:
case ID_H2:
case ID_H3:
case ID_H4:
case ID_H5:
case ID_H6:
if (!n->isInline()) {
popBlock(current->id());
handled = true;
}
break;
case ID_OPTION:
case ID_OPTGROUP:
if (id == ID_OPTGROUP) {
popBlock(current->id());
handled = true;
} else if (id == ID_SELECT) {
// IE treats a nested select as </select>. Let's do the same
popBlock(ID_SELECT);
break;
}
break;
// head elements in the body should be ignored.
case ID_ADDRESS:
case ID_COLGROUP:
case ID_FONT:
popBlock(current->id());
handled = true;
break;
default:
if (current->isDocumentNode()) {
DocumentImpl *doc = static_cast<DocumentImpl *>(current);
if (!doc->documentElement()) {
e = new HTMLHtmlElementImpl(document);
insertNode(e);
handled = true;
}
} else if (current->isInline()) {
popInlineBlocks();
handled = true;
}
}
// if we couldn't handle the error, just rethrow the exception...
if (!handled) {
//qCDebug(KHTML_LOG) << "Exception handler failed in HTMLPArser::insertNode()";
return false;
}
return insertNode(n);
}
}
NodeImpl *KHTMLParser::getElement(Token *t)
{
NodeImpl *n = nullptr;
switch (t->tid) {
case ID_HTML:
n = new HTMLHtmlElementImpl(document);
break;
case ID_HEAD:
if (!head && (current->id() == ID_HTML || current->isDocumentNode())) {
head = new HTMLHeadElementImpl(document);
n = head.get();
}
break;
case ID_BODY:
// body no longer allowed if we have a frameset
if (haveFrameSet) {
break;
}
popBlock(ID_HEAD);
n = new HTMLBodyElementImpl(document);
haveBody = true;
startBody();
break;
// head elements
case ID_BASE:
n = new HTMLBaseElementImpl(document);
break;
case ID_LINK:
n = new HTMLLinkElementImpl(document);
break;
case ID_META:
n = new HTMLMetaElementImpl(document);
break;
case ID_STYLE:
n = new HTMLStyleElementImpl(document);
break;
case ID_TITLE:
// only one non-empty <title> allowed
if (haveTitle) {
discard_until = ID_TITLE + ID_CLOSE_TAG;
break;
}
n = new HTMLTitleElementImpl(document);
// we'll set haveTitle when closing the tag
break;
// frames
case ID_FRAME:
n = new HTMLFrameElementImpl(document);
break;
case ID_FRAMESET:
popBlock(ID_HEAD);
if (inBody && !haveFrameSet && !haveContent && !haveBody) {
popBlock(ID_BODY);
// ### actually for IE document.body returns the now hidden "body" element
// we can't implement that behavior now because it could cause too many
// regressions and the headaches are not worth the work as long as there is
// no site actually relying on that detail (Dirk)
if (static_cast<HTMLDocumentImpl *>(document)->body())
static_cast<HTMLDocumentImpl *>(document)->body()
->addCSSProperty(CSS_PROP_DISPLAY, CSS_VAL_NONE);
inBody = false;
}
if ((haveBody || haveContent || haveFrameSet) && current->id() == ID_HTML) {
break;
}
n = new HTMLFrameSetElementImpl(document);
haveFrameSet = true;
startBody();
break;
// a bit a special case, since the frame is inlined...
case ID_IFRAME:
n = new HTMLIFrameElementImpl(document);
break;
// form elements
case ID_FORM:
// thou shall not nest <form> - NS/IE quirk
if (form) {
break;
}
n = form = new HTMLFormElementImpl(document, false);
break;
case ID_BUTTON:
n = new HTMLButtonElementImpl(document, form);
break;
case ID_FIELDSET:
n = new HTMLFieldSetElementImpl(document, form);
break;
case ID_INPUT:
if (t->attrs &&
KHTMLGlobal::defaultHTMLSettings()->isAdFilterEnabled() &&
KHTMLGlobal::defaultHTMLSettings()->isHideAdsEnabled() &&
!strcasecmp(t->attrs->getValue(ATTR_TYPE), "image")) {
const QString url = doc()->completeURL(DOMString(t->attrs->getValue(ATTR_SRC)).trimSpaces().string());
if (KHTMLGlobal::defaultHTMLSettings()->isAdFiltered(url)) {
return nullptr;
}
}
n = new HTMLInputElementImpl(document, form);
break;
case ID_ISINDEX:
n = handleIsindex(t);
if (!inBody) {
isindex = n;
n = nullptr;
} else {
t->flat = true;
}
break;
case ID_KEYGEN:
n = new HTMLKeygenElementImpl(document, form);
break;
case ID_LABEL:
n = new HTMLLabelElementImpl(document);
break;
case ID_LEGEND:
n = new HTMLLegendElementImpl(document, form);
break;
case ID_OPTGROUP:
n = new HTMLOptGroupElementImpl(document, form);
break;
case ID_OPTION:
popOptionalBlock(ID_OPTION);
n = new HTMLOptionElementImpl(document, form);
break;
case ID_SELECT:
inSelect = true;
n = new HTMLSelectElementImpl(document, form);
break;
case ID_TEXTAREA:
n = new HTMLTextAreaElementImpl(document, form);
break;
// lists
case ID_DL:
n = new HTMLDListElementImpl(document);
break;
case ID_DD:
popOptionalBlock(ID_DT);
popOptionalBlock(ID_DD);
n = new HTMLGenericElementImpl(document, t->tid);
break;
case ID_DT:
popOptionalBlock(ID_DD);
popOptionalBlock(ID_DT);
n = new HTMLGenericElementImpl(document, t->tid);
break;
case ID_UL: {
n = new HTMLUListElementImpl(document);
break;
}
case ID_OL: {
n = new HTMLOListElementImpl(document);
break;
}
case ID_DIR:
n = new HTMLDirectoryElementImpl(document);
break;
case ID_MENU:
n = new HTMLMenuElementImpl(document);
break;
case ID_LI:
popOptionalBlock(ID_LI);
n = new HTMLLIElementImpl(document);
break;
// formatting elements (block)
case ID_BLOCKQUOTE:
n = new HTMLGenericElementImpl(document, t->tid);
break;
case ID_LAYER:
case ID_ILAYER:
n = new HTMLLayerElementImpl(document, t->tid);
break;
case ID_P:
case ID_DIV:
n = new HTMLDivElementImpl(document, t->tid);
break;
case ID_H1:
case ID_H2:
case ID_H3:
case ID_H4:
case ID_H5:
case ID_H6:
n = new HTMLGenericElementImpl(document, t->tid);
break;
case ID_HR:
n = new HTMLHRElementImpl(document);
break;
case ID_PRE:
case ID_XMP:
case ID_PLAINTEXT:
case ID_LISTING:
n = new HTMLPreElementImpl(document, t->tid);
break;
// font stuff
case ID_BASEFONT:
n = new HTMLBaseFontElementImpl(document);
break;
case ID_FONT:
n = new HTMLFontElementImpl(document);
break;
// ins/del
case ID_DEL:
case ID_INS:
n = new HTMLGenericElementImpl(document, t->tid);
break;
// anchor
case ID_A:
popBlock(ID_A);
n = new HTMLAnchorElementImpl(document);
break;
// images
case ID_IMAGE:
case ID_IMG:
if (t->attrs &&
KHTMLGlobal::defaultHTMLSettings()->isAdFilterEnabled() &&
KHTMLGlobal::defaultHTMLSettings()->isHideAdsEnabled()) {
const QString url = doc()->completeURL(DOMString(t->attrs->getValue(ATTR_SRC)).trimSpaces().string());
if (KHTMLGlobal::defaultHTMLSettings()->isAdFiltered(url)) {
return nullptr;
}
}
n = new HTMLImageElementImpl(document, form);
break;
case ID_CANVAS:
n = new HTMLCanvasElementImpl(document);
break;
case ID_MAP:
map = new HTMLMapElementImpl(document);
n = map;
break;
case ID_AREA:
n = new HTMLAreaElementImpl(document);
break;
// objects, applets and scripts
case ID_APPLET:
n = new HTMLAppletElementImpl(document);
break;
case ID_EMBED:
n = new HTMLEmbedElementImpl(document);
break;
case ID_OBJECT:
n = new HTMLObjectElementImpl(document);
break;
case ID_PARAM:
n = new HTMLParamElementImpl(document);
break;
case ID_SCRIPT: {
HTMLScriptElementImpl *scriptElement = new HTMLScriptElementImpl(document);
scriptElement->setCreatedByParser(true);
n = scriptElement;
break;
}
// media
case ID_AUDIO:
n = new HTMLAudioElement(document);
break;
case ID_VIDEO:
n = new HTMLVideoElement(document);
break;
case ID_SOURCE:
n = new HTMLSourceElement(document);
break;
// tables
case ID_TABLE:
n = new HTMLTableElementImpl(document);
break;
case ID_CAPTION:
n = new HTMLTableCaptionElementImpl(document);
break;
case ID_COLGROUP:
case ID_COL:
n = new HTMLTableColElementImpl(document, t->tid);
break;
case ID_TR:
popBlock(ID_TR);
n = new HTMLTableRowElementImpl(document);
break;
case ID_TD:
case ID_TH:
popBlock(ID_TH);
popBlock(ID_TD);
n = new HTMLTableCellElementImpl(document, t->tid);
break;
case ID_TBODY:
case ID_THEAD:
case ID_TFOOT:
popBlock(ID_THEAD);
popBlock(ID_TBODY);
popBlock(ID_TFOOT);
n = new HTMLTableSectionElementImpl(document, t->tid, false);
break;
// inline elements
case ID_BR:
n = new HTMLBRElementImpl(document);
break;
case ID_Q:
n = new HTMLGenericElementImpl(document, t->tid);
break;
// elements with no special representation in the DOM
// block:
case ID_ADDRESS:
case ID_CENTER:
n = new HTMLGenericElementImpl(document, t->tid);
break;
// inline
// %fontstyle
case ID_TT:
case ID_U:
case ID_B:
case ID_I:
case ID_S:
case ID_STRIKE:
case ID_BIG:
case ID_SMALL:
// %phrase
case ID_EM:
case ID_STRONG:
case ID_DFN:
case ID_CODE:
case ID_SAMP:
case ID_KBD:
case ID_VAR:
case ID_CITE:
case ID_ABBR:
case ID_ACRONYM:
// %special
case ID_SUB:
case ID_SUP:
case ID_SPAN:
case ID_WBR:
case ID_NOBR:
if (t->tid == ID_NOBR || t->tid == ID_WBR) {
popOptionalBlock(t->tid);
}
case ID_BDO:
n = new HTMLGenericElementImpl(document, t->tid);
break;
// these are special, and normally not rendered
case ID_NOEMBED:
if (!t->flat) {
n = new HTMLGenericElementImpl(document, t->tid);
discard_until = ID_NOEMBED + ID_CLOSE_TAG;
}
return n;
case ID_NOFRAMES:
if (!t->flat) {
n = new HTMLGenericElementImpl(document, t->tid);
discard_until = ID_NOFRAMES + ID_CLOSE_TAG;
}
return n;
case ID_NOSCRIPT:
if (!t->flat) {
n = new HTMLGenericElementImpl(document, t->tid);
if (HTMLWidget && HTMLWidget->part()->jScriptEnabled()) {
discard_until = ID_NOSCRIPT + ID_CLOSE_TAG;
}
}
return n;
case ID_NOLAYER:
// discard_until = ID_NOLAYER + ID_CLOSE_TAG;
return nullptr;
break;
case ID_MARQUEE:
n = new HTMLMarqueeElementImpl(document);
break;
// text
case ID_TEXT:
// qCDebug(KHTML_LOG) << "ID_TEXT: \"" << DOMString(t->text).string() << "\"";
n = new TextImpl(document, t->text);
break;
case ID_COMMENT:
n = new CommentImpl(document, t->text);
break;
default:
n = new HTMLGenericElementImpl(document, t->tid);
break;
// qCDebug(KHTML_LOG) << "Unknown tag " << t->tid << "!";
}
return n;
}
void KHTMLParser::processCloseTag(Token *t)
{
// FIXME: the below only behaves according to "in body" insertion mode (HTML5 8.2.5.10)
// - might need fixing when we have other insertion modes.
switch (t->tid) {
case ID_HTML+ID_CLOSE_TAG:
case ID_BODY+ID_CLOSE_TAG:
// we never trust those close tags, since stupid webpages close
// them prematurely
return;
case ID_FORM+ID_CLOSE_TAG: // needs additional error checking. See spec.
form = nullptr;
if (!isElementInScope(ID_FORM)) {
// Parse error. Ignore.
return;
}
// this one is to get the right style on the body element
break;
case ID_MAP+ID_CLOSE_TAG:
map = nullptr;
break;
case ID_SELECT+ID_CLOSE_TAG:
inSelect = false;
break;
case ID_TITLE+ID_CLOSE_TAG:
// Set haveTitle only if <title> isn't empty
if (current->firstChild()) {
haveTitle = true;
}
break;
case ID_P+ID_CLOSE_TAG:
if (!isElementInScope(ID_P)) {
// Parse error. Handle as if <p> had been seen.
t->tid = ID_P;
parseToken(t);
popBlock(ID_P);
return;
}
break;
case ID_ADDRESS+ID_CLOSE_TAG:
// case ID_ARTICLE+ID_CLOSE_TAG:
case ID_BLOCKQUOTE+ID_CLOSE_TAG:
case ID_CENTER+ID_CLOSE_TAG:
// case ID_DATAGRID+ID_CLOSE_TAG:
// case ID_DETAILS+ID_CLOSE_TAG:
// case ID_DIALOG+ID_CLOSE_TAG:
case ID_DIR+ID_CLOSE_TAG:
case ID_DIV+ID_CLOSE_TAG:
case ID_DL+ID_CLOSE_TAG:
case ID_FIELDSET+ID_CLOSE_TAG:
// case ID_FIGURE+ID_CLOSE_TAG:
// case ID_FOOTER+ID_CLOSE_TAG:
// case ID_HEADER+ID_CLOSE_TAG:
case ID_LISTING+ID_CLOSE_TAG:
case ID_MENU+ID_CLOSE_TAG:
// case ID_NAV+ID_CLOSE_TAG:
case ID_OL+ID_CLOSE_TAG:
case ID_PRE+ID_CLOSE_TAG:
// case ID_SECTION+ID_CLOSE_TAG:
case ID_UL+ID_CLOSE_TAG:
case ID_DD+ID_CLOSE_TAG:
case ID_DT+ID_CLOSE_TAG:
case ID_LI+ID_CLOSE_TAG:
case ID_APPLET+ID_CLOSE_TAG: // those four should also "Clear the list of active formatting elements
case ID_BUTTON+ID_CLOSE_TAG: // up to the last marker." whenever we implement adoption agency.
case ID_MARQUEE+ID_CLOSE_TAG:
case ID_OBJECT+ID_CLOSE_TAG:
case ID_HEAD+ID_CLOSE_TAG: // ### according to HTML5, should be treated as 'Any other end tag'
// We'll do that when proper 'Any other end tag' handling is implemented.
// In the meantime, test scoping at least (#170694)
if (!isElementInScope(t->tid - ID_CLOSE_TAG)) {
// Parse error. Ignore token.
return;
}
break;
case ID_H1:
case ID_H2:
case ID_H3:
case ID_H4:
case ID_H5:
case ID_H6:
if (!isHeadingInScope()) {
// Parse error. Ignore token.
return;
}
break;
case ID_A: // Formatting elements - will need special handling - cf. HTML5 "adoption agency algorithm"
case ID_B: // meant to replace the "residual style" handling we have now.
case ID_BIG:
case ID_CODE:
case ID_EM:
case ID_FONT:
case ID_I:
case ID_NOBR:
case ID_S:
case ID_SMALL:
case ID_STRIKE:
case ID_STRONG:
case ID_TT:
case ID_U:
break;
default:
// otherTag = true; // FIXME: implement 'Any other end tag' handling
break;
}
#ifdef PARSER_DEBUG
qCDebug(KHTML_LOG) << "added the following children to " << current->nodeName().string();
NodeImpl *child = current->firstChild();
while (child != 0) {
qCDebug(KHTML_LOG) << " " << child->nodeName().string();
child = child->nextSibling();
}
#endif
generateImpliedEndTags(t->tid - ID_CLOSE_TAG);
popBlock(t->tid - ID_CLOSE_TAG);
#ifdef PARSER_DEBUG
qCDebug(KHTML_LOG) << "closeTag --> current = " << current->nodeName().string();
#endif
}
bool KHTMLParser::isResidualStyleTag(int _id)
{
switch (_id) {
case ID_A:
case ID_B:
case ID_BIG:
case ID_EM:
case ID_FONT:
case ID_I:
case ID_NOBR:
case ID_S:
case ID_SMALL:
case ID_STRIKE:
case ID_STRONG:
case ID_TT:
case ID_U:
case ID_DFN:
case ID_CODE:
case ID_SAMP:
case ID_KBD:
case ID_VAR:
case ID_DEL:
case ID_INS:
return true;
default:
return false;
}
}
bool KHTMLParser::isAffectedByResidualStyle(int _id)
{
if (isResidualStyleTag(_id)) {
return true;
}
switch (_id) {
case ID_P:
case ID_DIV:
case ID_BLOCKQUOTE:
case ID_ADDRESS:
case ID_H1:
case ID_H2:
case ID_H3:
case ID_H4:
case ID_H5:
case ID_H6:
case ID_CENTER:
case ID_UL:
case ID_OL:
case ID_LI:
case ID_DL:
case ID_DT:
case ID_DD:
case ID_PRE:
case ID_LISTING:
return true;
default:
return false;
}
}
void KHTMLParser::handleResidualStyleCloseTagAcrossBlocks(HTMLStackElem *elem)
{
// Find the element that crosses over to a higher level.
// ### For now, if there is more than one, we will only make sure we close the residual style.
int exceptionCode = 0;
HTMLStackElem *curr = blockStack;
HTMLStackElem *maxElem = nullptr;
HTMLStackElem *endElem = nullptr;
HTMLStackElem *prev = nullptr;
HTMLStackElem *prevMaxElem = nullptr;
bool advancedResidual = false; // ### if set we only close the residual style
while (curr && curr != elem) {
if (curr->level > elem->level) {
if (!isAffectedByResidualStyle(curr->id)) {
return;
}
if (maxElem) {
advancedResidual = true;
} else {
endElem = curr;
}
maxElem = curr;
prevMaxElem = prev;
}
prev = curr;
curr = curr->next;
}
if (!curr || !maxElem) {
return;
}
NodeImpl *residualElem = prev->node;
NodeImpl *blockElem = prevMaxElem ? prevMaxElem->node : current;
RefPtr<NodeImpl> parentElem = elem->node;
// Check to see if the reparenting that is going to occur is allowed according to the DOM.
// FIXME: We should either always allow it or perform an additional fixup instead of
// just bailing here.
// Example: <p><font><center>blah</font></center></p> isn't doing a fixup right now.
if (!parentElem->childAllowed(blockElem)) {
return;
}
if (maxElem->node->parentNode() != elem->node && !advancedResidual) {
// Walk the stack and remove any elements that aren't residual style tags. These
// are basically just being closed up. Example:
// <font><span>Moo<p>Goo</font></p>.
// In the above example, the <span> doesn't need to be reopened. It can just close.
HTMLStackElem *currElem = maxElem->next;
HTMLStackElem *prevElem = maxElem;
while (currElem != elem) {
HTMLStackElem *nextElem = currElem->next;
if (!isResidualStyleTag(currElem->id)) {
prevElem->next = nextElem;
prevElem->setNode(currElem->node);
delete currElem;
} else {
prevElem = currElem;
}
currElem = nextElem;
}
// We have to reopen residual tags in between maxElem and elem. An example of this case s:
// <font><i>Moo<p>Foo</font>.
// In this case, we need to transform the part before the <p> into:
// <font><i>Moo</i></font><i>
// so that the <i> will remain open. This involves the modification of elements
// in the block stack.
// This will also affect how we ultimately reparent the block, since we want it to end up
// under the reopened residual tags (e.g., the <i> in the above example.)
RefPtr<NodeImpl> prevNode = nullptr;
RefPtr<NodeImpl> currNode = nullptr;
currElem = maxElem;
while (currElem->node != residualElem) {
if (isResidualStyleTag(currElem->node->id())) {
// Create a clone of this element.
currNode = currElem->node->cloneNode(false);
currElem->node->close();
removeForbidden(currElem->id, forbiddenTag);
// Change the stack element's node to point to the clone.
currElem->setNode(currNode.get());
// Attach the previous node as a child of this new node.
if (prevNode) {
currNode->appendChild(prevNode.get(), exceptionCode);
} else { // The new parent for the block element is going to be the innermost clone.
parentElem = currNode;
}
prevNode = currNode;
}
currElem = currElem->next;
}
// Now append the chain of new residual style elements if one exists.
if (prevNode) {
elem->node->appendChild(prevNode.get(), exceptionCode);
}
}
// We need to make a clone of |residualElem| and place it just inside |blockElem|.
// All content of |blockElem| is reparented to be under this clone. We then
// reparent |blockElem| using real DOM calls so that attachment/detachment will
// be performed to fix up the rendering tree.
// So for this example: <b>...<p>Foo</b>Goo</p>
// The end result will be: <b>...</b><p><b>Foo</b>Goo</p>
//
// Step 1: Remove |blockElem| from its parent, doing a batch detach of all the kids.
SharedPtr<NodeImpl> guard(blockElem);
blockElem->parentNode()->removeChild(blockElem, exceptionCode);
if (!advancedResidual) {
// Step 2: Clone |residualElem|.
RefPtr<NodeImpl> newNode = residualElem->cloneNode(false); // Shallow clone. We don't pick up the same kids.
// Step 3: Place |blockElem|'s children under |newNode|. Remove all of the children of |blockElem|
// before we've put |newElem| into the document. That way we'll only do one attachment of all
// the new content (instead of a bunch of individual attachments).
NodeImpl *currNode = blockElem->firstChild();
while (currNode) {
NodeImpl *nextNode = currNode->nextSibling();
SharedPtr<NodeImpl> guard(currNode); //Protect from deletion while moving
blockElem->removeChild(currNode, exceptionCode);
newNode->appendChild(currNode, exceptionCode);
currNode = nextNode;
// TODO - To be replaced.
// Re-register form elements with currently active form, step 1 will have removed them
if (form && currNode && currNode->isGenericFormElement()) {
HTMLGenericFormElementImpl *e = static_cast<HTMLGenericFormElementImpl *>(currNode);
form->registerFormElement(e);
}
}
// Step 4: Place |newNode| under |blockElem|. |blockElem| is still out of the document, so no
// attachment can occur yet.
blockElem->appendChild(newNode.get(), exceptionCode);
}
// Step 5: Reparent |blockElem|. Now the full attachment of the fixed up tree takes place.
parentElem->appendChild(blockElem, exceptionCode);
// Step 6: Elide |elem|, since it is effectively no longer open. Also update
// the node associated with the previous stack element so that when it gets popped,
// it doesn't make the residual element the next current node.
HTMLStackElem *currElem = maxElem;
HTMLStackElem *prevElem = nullptr;
while (currElem != elem) {
prevElem = currElem;
currElem = currElem->next;
}
prevElem->next = elem->next;
prevElem->setNode(elem->node);
delete elem;
// Step 7: Reopen intermediate inlines, e.g., <b><p><i>Foo</b>Goo</p>.
// In the above example, Goo should stay italic.
curr = blockStack;
HTMLStackElem *residualStyleStack = nullptr;
while (curr && curr != endElem) {
// We will actually schedule this tag for reopening
// after we complete the close of this entire block.
NodeImpl *currNode = current;
if (isResidualStyleTag(curr->id)) {
// We've overloaded the use of stack elements and are just reusing the
// struct with a slightly different meaning to the variables. Instead of chaining
// from innermost to outermost, we build up a list of all the tags we need to reopen
// from the outermost to the innermost, i.e., residualStyleStack will end up pointing
// to the outermost tag we need to reopen.
// We also set curr->node to be the actual element that corresponds to the ID stored in
// curr->id rather than the node that you should pop to when the element gets pulled off
// the stack.
popOneBlock(false);
curr->setNode(currNode);
curr->next = residualStyleStack;
residualStyleStack = curr;
} else {
popOneBlock();
}
curr = blockStack;
}
reopenResidualStyleTags(residualStyleStack, nullptr); // FIXME: Deal with stray table content some day
// if it becomes necessary to do so.
}
void KHTMLParser::reopenResidualStyleTags(HTMLStackElem *elem, DOM::NodeImpl *malformedTableParent)
{
// Loop for each tag that needs to be reopened.
while (elem) {
// Create a shallow clone of the DOM node for this element.
RefPtr<NodeImpl> newNode = elem->node->cloneNode(false);
// Append the new node. In the malformed table case, we need to insert before the table,
// which will be the last child.
int exceptionCode = 0;
if (malformedTableParent) {
malformedTableParent->insertBefore(newNode.get(), malformedTableParent->lastChild(), exceptionCode);
} else {
current->appendChild(newNode.get(), exceptionCode);
}
// FIXME: Is it really OK to ignore the exceptions here?
// Now push a new stack element for this node we just created.
pushBlock(elem->id, elem->level);
// Set our strayTableContent boolean if needed, so that the reopened tag also knows
// that it is inside a malformed table.
blockStack->strayTableContent = malformedTableParent != nullptr;
if (blockStack->strayTableContent) {
inStrayTableContent++;
}
// Clear our malformed table parent variable.
malformedTableParent = nullptr;
// Update |current| manually to point to the new node.
setCurrent(newNode.get());
// Advance to the next tag that needs to be reopened.
HTMLStackElem *next = elem->next;
delete elem;
elem = next;
}
}
void KHTMLParser::pushBlock(int _id, int _level)
{
HTMLStackElem *Elem = new HTMLStackElem(_id, _level, current, m_inline, blockStack);
blockStack = Elem;
addForbidden(_id, forbiddenTag);
}
void KHTMLParser::generateImpliedEndTags(int _id)
{
HTMLStackElem *Elem = blockStack;
int level = tagPriority(_id);
while (Elem && Elem->id != _id) {
HTMLStackElem *NextElem = Elem->next;
if (endTagRequirement(Elem->id) == DOM::OPTIONAL && Elem->level <= level) {
popOneBlock();
} else {
break;
}
Elem = NextElem;
}
}
void KHTMLParser::popOptionalBlock(int _id)
{
bool found = false;
HTMLStackElem *Elem = blockStack;
int level = tagPriority(_id);
while (Elem) {
if (Elem->id == _id) {
found = true;
break;
}
if (Elem->level > level || (endTagRequirement(Elem->id) != DOM::OPTIONAL && !isResidualStyleTag(Elem->id))) {
break;
}
Elem = Elem->next;
}
if (found) {
generateImpliedEndTags(_id);
popBlock(_id);
}
}
bool KHTMLParser::isElementInScope(int _id)
{
// HTML5 8.2.3.2
HTMLStackElem *Elem = blockStack;
while (Elem && Elem->id != _id) {
if (DOM::checkIsScopeBoundary(Elem->id)) {
return false;
}
Elem = Elem->next;
}
return Elem;
}
bool KHTMLParser::isHeadingInScope()
{
HTMLStackElem *Elem = blockStack;
while (Elem && (Elem->id < ID_H1 || Elem->id > ID_H6)) {
if (DOM::checkIsScopeBoundary(Elem->id)) {
return false;
}
Elem = Elem->next;
}
return Elem;
}
void KHTMLParser::popBlock(int _id)
{
HTMLStackElem *Elem = blockStack;
int maxLevel = 0;
#ifdef PARSER_DEBUG
qCDebug(KHTML_LOG) << "popBlock(" << getParserPrintableName(_id) << ")";
while (Elem) {
qCDebug(KHTML_LOG) << " > " << getParserPrintableName(Elem->id);
Elem = Elem->next;
}
Elem = blockStack;
#endif
while (Elem && (Elem->id != _id)) {
if (maxLevel < Elem->level) {
maxLevel = Elem->level;
}
Elem = Elem->next;
}
if (!Elem) {
return;
}
if (maxLevel > Elem->level) {
// We didn't match because the tag is in a different scope, e.g.,
// <b><p>Foo</b>. Try to correct the problem.
if (!isResidualStyleTag(_id)) {
return;
}
return handleResidualStyleCloseTagAcrossBlocks(Elem);
}
bool isAffectedByStyle = isAffectedByResidualStyle(Elem->id);
HTMLStackElem *residualStyleStack = nullptr;
NodeImpl *malformedTableParent = nullptr;
Elem = blockStack;
while (Elem) {
if (Elem->id == _id) {
int strayTable = inStrayTableContent;
popOneBlock();
Elem = nullptr;
// This element was the root of some malformed content just inside an implicit or
// explicit <tbody> or <tr>.
// If we end up needing to reopen residual style tags, the root of the reopened chain
// must also know that it is the root of malformed content inside a <tbody>/<tr>.
if (strayTable && (inStrayTableContent < strayTable) && residualStyleStack) {
NodeImpl *curr = current;
while (curr && curr->id() != ID_TABLE) {
curr = curr->parentNode();
}
malformedTableParent = curr ? curr->parentNode() : nullptr;
}
} else {
// Schedule this tag for reopening
// after we complete the close of this entire block.
NodeImpl *currNode = current;
if (isAffectedByStyle && isResidualStyleTag(Elem->id)) {
// We've overloaded the use of stack elements and are just reusing the
// struct with a slightly different meaning to the variables. Instead of chaining
// from innermost to outermost, we build up a list of all the tags we need to reopen
// from the outermost to the innermost, i.e., residualStyleStack will end up pointing
// to the outermost tag we need to reopen.
// We also set Elem->node to be the actual element that corresponds to the ID stored in
// Elem->id rather than the node that you should pop to when the element gets pulled off
// the stack.
popOneBlock(false);
Elem->next = residualStyleStack;
Elem->setNode(currNode);
residualStyleStack = Elem;
} else {
popOneBlock();
}
Elem = blockStack;
}
}
reopenResidualStyleTags(residualStyleStack, malformedTableParent);
}
void KHTMLParser::popOneBlock(bool delBlock)
{
HTMLStackElem *Elem = blockStack;
// we should never get here, but some bad html might cause it.
#ifndef PARSER_DEBUG
if (!Elem) {
return;
}
#else
qCDebug(KHTML_LOG) << "popping block: " << getParserPrintableName(Elem->id) << "(" << Elem->id << ")";
#endif
#if SPEED_DEBUG < 1
if ((Elem->node != current)) {
if (current->maintainsState() && document) {
document->registerMaintainsState(current);
document->attemptRestoreState(current);
}
current->close();
}
#endif
removeForbidden(Elem->id, forbiddenTag);
blockStack = Elem->next;
// we only set inline to false, if the element we close is a block level element.
// This helps getting cases as <p><b>bla</b> <b>bla</b> right.
m_inline = Elem->m_inline;
if (current->id() == ID_FORM && form && inStrayTableContent) {
form->setMalformed(true);
}
setCurrent(Elem->node);
if (Elem->strayTableContent) {
inStrayTableContent--;
}
if (delBlock) {
delete Elem;
}
}
void KHTMLParser::popInlineBlocks()
{
while (blockStack && current->isInline() && current->id() != ID_FONT) {
popOneBlock();
}
}
void KHTMLParser::freeBlock()
{
while (blockStack) {
popOneBlock();
}
blockStack = nullptr;
}
void KHTMLParser::createHead()
{
if (head || !doc()->documentElement()) {
return;
}
head = new HTMLHeadElementImpl(document);
HTMLElementImpl *body = doc()->body();
int exceptioncode = 0;
doc()->documentElement()->insertBefore(head.get(), body, exceptioncode);
if (exceptioncode) {
#ifdef PARSER_DEBUG
qCDebug(KHTML_LOG) << "creation of head failed!!!!:" << exceptioncode;
#endif
delete head.get();
head = nullptr;
}
// If the body does not exist yet, then the <head> should be pushed as the current block.
if (head && !body) {
pushBlock(head->id(), tagPriority(head->id()));
setCurrent(head.get());
}
}
NodeImpl *KHTMLParser::handleIsindex(Token *t)
{
NodeImpl *n;
HTMLFormElementImpl *myform = form;
if (!myform) {
myform = new HTMLFormElementImpl(document, true);
n = myform;
} else {
n = new HTMLDivElementImpl(document, ID_DIV);
}
NodeImpl *child = new HTMLHRElementImpl(document);
n->addChild(child);
DOMStringImpl *a = t->attrs ? t->attrs->getValue(ATTR_PROMPT) : nullptr;
DOMString text = i18n("This is a searchable index. Enter search keywords: ");
if (a) {
text = a;
}
child = new TextImpl(document, text.implementation());
n->addChild(child);
child = new HTMLIsIndexElementImpl(document, myform);
static_cast<ElementImpl *>(child)->setAttribute(ATTR_TYPE, "khtml_isindex");
n->addChild(child);
child = new HTMLHRElementImpl(document);
n->addChild(child);
return n;
}
void KHTMLParser::startBody()
{
if (inBody) {
return;
}
inBody = true;
if (isindex) {
insertNode(isindex, true /* don't decend into this node */);
isindex = nullptr;
}
}
|