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
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
|
/***************************************************************************
wkafkapart.cpp
-------------------
copyright : (C) 2003, 2004 - Nicolas Deschildre
email : ndeschildre@kdewebdev.org
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#define HEAVY_DEBUG 1
#include <dom/dom_node.h>
#include <dom/dom_exception.h>
#include <dom/dom_string.h>
#include <dom/css_stylesheet.h>
#include <kdebug.h>
#include <khtmlview.h>
#include <kstandarddirs.h>
#include <ktexteditor/editinterface.h>
#include <ktexteditor/selectioninterface.h>
#include <kstandarddirs.h>
#include <klocale.h>
#include <kmultipledrag.h>
#include <kglobal.h>
#include <kcharsets.h>
#include <tqregexp.h>
#include <tqfile.h>
#include <tqtextstream.h>
#include <tqdatetime.h>
#include <tqclipboard.h>
#include <tqptrvector.h>
#include "document.h"
#include "viewmanager.h"
#include "quantacommon.h"
#include "resource.h"
#include "undoredo.h"
#include "node.h"
#include "parser.h"
#include "project.h"
#include "tag.h"
#include "nodeproperties.h"
#include "htmlenhancer.h"
#include "kafkacommon.h"
#include "kafkaresource.h"
#include "cursors.h"
#include "kafkadragobject.h"
#include "cursors.h"
#include "wkafkapart.moc"
KafkaWidget *kafkaWidget;
KafkaDocument *kafkaDoc;
KafkaDocument::KafkaDocument(TQWidget *parent, TQWidget *widgetParent, const char *name)
:domNodeProps(1021), _docLoaded(false)
{
// i18n reserve
TQString a = i18n("Selector");TQString b = i18n("Attribute");TQString c = i18n("Class");
TQString d = i18n("Pseudo-class");TQString e = i18n("CSS rules");TQString f = i18n("Universal selector");
TQString g = i18n("Linked stylesheets");TQString h = i18n("Embedded stylesheets");TQString i = i18n("Inline style attribute");
TQString j = i18n("Link");TQString k = i18n("Priority");TQString l = i18n("ID");
TQString m = i18n("Browser support");TQString n = i18n("Pseudo-element");TQString o = i18n("Imported");
TQString p = i18n("Inheritance");TQString q = i18n("Inherited");TQString r = "";
TQString s = i18n("Name");TQString tt = i18n("Undo");TQString u = i18n("Redo");
TQString v = i18n("Undo/Redo history");TQString w = i18n("CSS styles");
TQString x = i18n("Sorry, VPL does not support this functionality yet.");
TQString y = i18n("Merge cells");
TQString z = i18n("Split cells");
TQString aa = i18n("Edit CSS style of this Tag");
TQString ab = i18n("Ident all");
//end
#ifdef LIGHT_DEBUG
kdDebug(25001)<< "KafkaDocument::KafkaDocument()" << endl;
#endif
kafkaDoc = this;
m_kafkaPart = new KafkaWidget(parent, widgetParent,this, name);
kafkaWidget = m_kafkaPart;
//m_kafkaPart->showDomTree();
m_currentDoc = 0L;
mainEnhancer = new HTMLEnhancer(this);
domNodeProps.setAutoDelete(false);
KStandardDirs *m_stddirs = new KStandardDirs();
TQFile file( m_stddirs->findResource("data", "kafkapart/entities" )/**locate("appdata","chars") */);
delete m_stddirs;
TQString tmp;
if ( file.open(IO_ReadOnly) )
{
TQTextStream t( &file ); // use a text stream
t.setEncoding(TQTextStream::UnicodeUTF8);
while ( !t.eof() )
{
tmp = t.readLine();
if(tmp.left(2) == "//") continue;//comments
int begin = tmp.find("(") + 1;
if(begin == (-1 + 1)) continue;//"(" not found : invalid line
int length = tmp.find(")") - begin;
decodedChars.insert(tmp.left(1), tmp.mid(begin, length));
encodedChars.insert(tmp.mid(begin, length), tmp.left(1));
}
file.close();
}
connect(m_kafkaPart, TQT_SIGNAL(domNodeInserted(DOM::Node, bool, NodeModifsSet*)),
this, TQT_SLOT(slotDomNodeInserted(DOM::Node, bool, NodeModifsSet*)));
connect(m_kafkaPart, TQT_SIGNAL(domNodeModified(DOM::Node, NodeModifsSet*)),
this, TQT_SLOT(slotDomNodeModified(DOM::Node, NodeModifsSet*)));
connect(m_kafkaPart, TQT_SIGNAL(domNodeIsAboutToBeRemoved(DOM::Node, bool, NodeModifsSet*)),
this, TQT_SLOT(slotDomNodeAboutToBeRemoved(DOM::Node, bool, NodeModifsSet*)));
connect(m_kafkaPart, TQT_SIGNAL(domNodeIsAboutToBeMoved(DOM::Node, DOM::Node, DOM::Node, NodeModifsSet*)),
this, TQT_SLOT(slotDomNodeIsAboutToBeMoved(DOM::Node, DOM::Node, DOM::Node, NodeModifsSet*)));
connect(m_kafkaPart, TQT_SIGNAL(domNodeNewCursorPos(DOM::Node, int)),
this, TQT_SLOT(slotdomNodeNewCursorPos(DOM::Node, int)));
}
KafkaDocument::~KafkaDocument()
{
#ifdef LIGHT_DEBUG
kdDebug(25001)<< "KafkaDocument::~KafkaDocument()" << endl;
#endif
//delete the empty node linked to the DOM::Node #document
disconnectAllDomNodes();
}
void KafkaDocument::loadDocument(Document *doc)
{
#ifdef LIGHT_DEBUG
kdDebug(25001) << "KafkaDocument::loadDocument()" << endl;
#endif
Node *node;
Tag *tag;
DOM::Node domNode;
bool goUp;
if(!m_kafkaPart) return;
if(!doc) return;
#ifdef LIGHT_DEBUG
TQTime t;
t.start();
#endif
m_currentDoc = doc;
(static_cast<HTMLEnhancer *>(mainEnhancer))->setBaseURL(Project::ref()->projectBaseURL());
//create a empty document with a basic tree : HTML, HEAD, BODY
m_kafkaPart->newDocument();
// When loading a weird html file in khtml (e.g. without BODY or HTML), khtml takes care
// to create the necessary tags. But as we are handling directly the Nodes, we have to handle this!!
// creating and linking an empty node to the root DOM::Node (#document) and
// to HEAD, HTML, BODY
node = new Node(0L);
tag = new Tag();
tag->name = "#document";
tag->setNotInTree(true);
node->tag = tag;
connectDomNodeToQuantaNode(m_kafkaPart->document(), node);
node = new Node(0L);
tag = new Tag();
tag->name = "HTML";
tag->setNotInTree(true);
node->tag = tag;
connectDomNodeToQuantaNode(m_kafkaPart->document().firstChild(), node);
html = m_kafkaPart->document().firstChild();
node = new Node(0L);
tag = new Tag();
tag->name = "HEAD";
tag->setNotInTree(true);
node->tag = tag;
connectDomNodeToQuantaNode(m_kafkaPart->document().firstChild().firstChild(), node);
head = m_kafkaPart->document().firstChild().firstChild();
node = new Node(0L);
tag = new Tag();
tag->name = "BODY";
tag->setNotInTree(true);
node->tag = tag;
connectDomNodeToQuantaNode(m_kafkaPart->document().firstChild().lastChild(), node);
body = m_kafkaPart->document().firstChild().lastChild();
//load the DOM::Nodes from the node tree.
node = baseNode;
while(node)
{
#ifdef HEAVY_DEBUG
kdDebug(25001) << "KafkaDocument::loadDocument - Node name :" <<
node->tag->name.upper() << "; type : " <<
node->tag->type << "; tagstr : " << node->tag->tagStr() <<
" is opened :" << node->opened << endl;
#endif
if(!buildKafkaNodeFromNode(node))
emit loadingError(node);
node = node->nextSibling();
}
//post process the DOM::Node Tree by adding Empty TEXT so that the cursor can go
//everywhere the user wants.
domNode = m_kafkaPart->document();
goUp = false;
while(!domNode.isNull())
{
mainEnhancer->postEnhanceNode(domNode);
domNode = kafkaCommon::getNextDomNode(domNode, goUp);
}
m_kafkaPart->putCursorAtFirstAvailableLocation();
_docLoaded = true;
//Avoid moving objects...
m_kafkaPart->stopAnimations();
m_currentDoc->docUndoRedo->kafkaLoaded();
emit loaded();
m_currentDoc->docUndoRedo->syncKafkaCursorAndSelection(0);
//m_kafkaPart->document().updateRendering();
#ifdef LIGHT_DEBUG
kdDebug(25001)<< "KafkaDocument::loadDocument() in " << t.elapsed() << " ms only!" << endl;
#endif
#ifdef HEAVY_DEBUG
kafkaCommon::coutDomTree(m_kafkaPart->document(), 2);
coutLinkTree(baseNode, 2);
#endif
}
void KafkaDocument::unloadDocument()
{
#ifdef LIGHT_DEBUG
kdDebug(25001)<< "KafkaDocument::unloadDocument()" << endl;
#endif
Node *node;
domNodeProps.clear();
//clean the kafkapart
while(m_kafkaPart->document().hasChildNodes())
m_kafkaPart->document().removeChild(m_kafkaPart->document().firstChild());
m_currentDoc = 0L;
html = body = head = DOM::Node();
_docLoaded = false;
node = baseNode;
while(node)
{
if(node->rootNode())
delete node->rootNode();
node->setRootNode(0L);
if(node->leafNode())
delete node->leafNode();
node->setLeafNode(0L);
node = node->nextSibling();
}
emit unloaded();
}
void KafkaDocument::reloadDocument()
{
#ifdef LIGHT_DEBUG
kdDebug(25001)<< "KafkaDocument::reloadDocument()" << endl;
#endif
Node *node;
int offsetX, offsetY;
offsetX = m_kafkaPart->view()->contentsX();
offsetY = m_kafkaPart->view()->contentsY();
m_kafkaPart->closeURL();
if(!_docLoaded)
return;
domNodeProps.clear();
while(m_kafkaPart->document().hasChildNodes())
m_kafkaPart->document().removeChild(m_kafkaPart->document().firstChild());
node = baseNode;
while(node)
{
if(node->rootNode())
delete node->rootNode();
node->setRootNode(0L);
if(node->leafNode())
delete node->leafNode();
node->setLeafNode(0L);
node = node->nextSibling();
}
/**KParts::URLArgs args(false, offsetX, offsetY);
(static_cast<KParts::BrowserExtension *>(((KParts::ReadOnlyPart *)m_kafkaPart)->child( 0L,
"KParts::BrowserExtension" )))->setURLArgs( args );*/
loadDocument(m_currentDoc);
//m_kafkaPart->view()->setContentsPos(offsetX, offsetY);
}
kNodeAttrs *KafkaDocument::getAttrs(DOM::Node _domNode)
{
return domNodeProps[_domNode.handle()];
}
Node *KafkaDocument::getNode(DOM::Node _domNode)
{
if(_domNode.isNull() || _domNode.nodeName().string() == "#document")
{
kdDebug(25001)<< "KafkaDocument::getNode() - Bad Node given" <<
endl;
return 0L;
}
kNodeAttrs *props = domNodeProps[_domNode.handle()];
if(!props)
{
kdDebug(25001)<< "KafkaDocument::getNode() -" <<
" Corresponding Node not Found!!!" << endl;
return 0L;
}
return props->getNode();
}
kNodeAttrs* KafkaDocument::connectDomNodeToQuantaNode(DOM::Node domNode, Node *node)
{
TQString name;
kNodeAttrs *props;
if(domNode.isNull())
{
#ifdef LIGHT_DEBUG
kdDebug(25001)<< "KafkaDocument::connectDomNodeToQuantaNode()" <<
" - WARNING empty DOM::Node" << endl;
#endif
}
/**qtag = QuantaCommon::tagFromDTD(m_currentDoc->defaultDTD(),
_domNode.nodeName().string());*/
else
name = domNode.nodeName().string().lower();
// kdDebug(25001) << "KafkaDocument::connectDomNodeToQuantaNode() - domNode name: |" << name << "|" << endl;
props = new kNodeAttrs();
//We can split Nodes into several groups essentially for the deletion behavior:
//1- Text
//2- Block in which the cursor can't enter in when deleting e.g. TABLE, IMG
//3- Block in which the cursor can't escape from e.g. TD, BODY
//4- Block in which the cursor can enter in/escape from when deleting e.g. H1, DIV
//5- Inline
//6- "Invisible" Nodes e.g. HTML, HEAD
//Hmmm... I guess we can do better here...
if(domNode.nodeType() == DOM::Node::TEXT_NODE)
{
props->setCHCursorFocus(kNodeAttrs::textNode);
props->setCCEnter(true);
props->setCBModified(true);
props->setCBDeleted(true);
}
else if(name == "abbr" || name == "acronym" || name == "address" || name == "b" || name == "bdo" ||
name == "big" || name == "cite" || name == "code" ||
name == "del" || name == "dfn" || name == "dir" ||
name == "em" || name == "fieldset" || name == "font" || name == "i" || name == "iframe" ||
name == "ins" || name == "kbd" || name == "label" || name == "legend" || name == "menu" ||
name == "noframes" || name == "pre" || name == "s" || name == "samp" ||
name == "small" || name == "span" || name == "strike" || name == "strong" || name == "sub" ||
name == "sup" || name == "tt" || name == "u" || name == "var" || name == "a" ||
name == "blockquote" ||
name == "em" || name == "form" || name == "ins" ||
name == "q" || name == "tt" )
{
props->setCHCursorFocus(kNodeAttrs::inlineNode);
props->setCCEnter(true);
props->setCBModified(true);
props->setCBDeleted(true);
}
else if(name == "center" || name == "li" || name == "h1" || name == "h2" || name == "h3" ||
name == "h4" || name == "h5" || name == "h6" || name == "div" || name == "dd" ||
name == "dt" || name == "p")
{
props->setCHCursorFocus(kNodeAttrs::blockNode);
props->setCCEnter(true);
props->setCBModified(true);
props->setCBDeleted(true);
}
else if(name == "applet" || name == "button" || name == "img" || name == "map" || name == "object" ||
name == "hr" || name == "input" || name == "select" || name == "table" || name == "textarea" ||
name == "br" || name == "dl" || name == "ul" || name == "ol")
{
props->setCHCursorFocus(kNodeAttrs::singleNodeAndItself);
props->setCCEnter(false);
props->setCBModified(true);
props->setCBDeleted(true);
}
else if(name == "basefont" || name == "location" || name == "fieldset" || name == "noscript" ||
name == "script")
{
props->setCHCursorFocus(kNodeAttrs::no);
props->setCCEnter(false);
props->setCBModified(true);
props->setCBDeleted(true);
}
else if(name == "caption" || name == "frame" || name == "frameset" || name == "isindex" ||
name == "optgroup" || name == "param" || name == "title" || name == "area" || name == "base" ||
name == "body" || name == "col" || name == "colgroup" || name == "head" || name == "html" ||
name == "link" || name == "meta" || name == "option" || name == "style" || name == "tbody" ||
name == "td" || name == "tfoot" || name == "th" || name == "thead" || name == "tr")
{
props->setCHCursorFocus(kNodeAttrs::no);
props->setCCEnter(false);
props->setCBModified(false);
props->setCBDeleted(false);
}
else
{
kdDebug(25001)<< "KafkaDocument::connectDomNodeToQuantaNode () - " <<
"No QTag found! Setting default parameters..." << endl;
props->setCHCursorFocus(kNodeAttrs::no);
props->setCCEnter(false);
props->setCBModified(false);
props->setCBDeleted(false);
}
#ifdef HEAVY_DEBUG
kdDebug(25001)<< "KafkaDocument::connectDomNodeToQuantaNode() - tag name :" <<
name << " canBeDeleted:" << props->cbDel() << " canBeModified:" <<
props->cbMod() << " canHaveCursorFocus:" << props->chCurFoc() <<
" cursorCanEnter:" << props->ccanEnter() << endl;
#endif
props->setNode(node);
props->setDomNode(domNode);
domNodeProps.insert(domNode.handle(), props);
return props;
}
void KafkaDocument::disconnectDomNodeFromQuantaNode(DOM::Node _domNode)
{
if(_domNode.isNull())
return;
domNodeProps.remove(_domNode.handle());
}
void KafkaDocument::disconnectAllDomNodes()
{
domNodeProps.clear();
}
void KafkaDocument::setCursorAndSelection(NodeSelectionInd *nodeSelection)
{
bool cursorAtSelectionStart;
DOM::Node startDomNode, endDomNode, foo;
Node *startNode, *endNode;
long startOffset, endOffset;
cursorAtSelectionStart = nodeSelection->cursorAtSelectionStart();
startNode = kafkaCommon::getNodeFromLocation(nodeSelection->cursorNode());
endNode = kafkaCommon::getNodeFromLocation(nodeSelection->cursorNodeEndSel());
if(startNode && startNode->rootNode())
startDomNode = *(startNode->rootNode());
if(endNode && endNode->rootNode())
endDomNode = *(endNode->rootNode());
translateNodeIntoKafkaCursorPosition(startNode, nodeSelection->cursorOffset(), foo, startOffset);
translateNodeIntoKafkaCursorPosition(endNode, nodeSelection->cursorOffsetEndSel(), foo, endOffset);
if(cursorAtSelectionStart && !startDomNode.isNull())
{
m_kafkaPart->setCurrentNode(startDomNode, startOffset);
}
else if(!cursorAtSelectionStart && !endDomNode.isNull())
{
m_kafkaPart->setCurrentNode(endDomNode, endOffset);
}
if(!startDomNode.isNull() && !endDomNode.isNull())
m_kafkaPart->setSelection(DOM::Range(startDomNode, (long)startOffset, endDomNode, (long)endOffset));
}
void KafkaDocument::setCursor(Node* cursorNode, int cursorOffset)
{
DOM::Node domNode;
long longDomNodeOffset;
translateNodeIntoKafkaCursorPosition(cursorNode, cursorOffset, domNode, longDomNodeOffset);
if (!domNode.isNull() && domNode.nodeType() != DOM::Node::TEXT_NODE &&
!domNode.firstChild().isNull() && domNode.firstChild().nodeType() == DOM::Node::TEXT_NODE)
domNode = domNode.firstChild();
if (!domNode.isNull())
m_kafkaPart->setCurrentNode(domNode, (int)longDomNodeOffset);
}
bool KafkaDocument::buildKafkaNodeFromNode(Node *node, bool insertNode)
{
#ifdef LIGHT_DEBUG
if(node)
kdDebug(25001)<< "KafkaDocument::buildKafkaNodeFromNode() " << endl;
#endif
DOM::Node newNode, newNode2, attr, nextNode, parentNode, *ptDomNode;
bool removeLeftWhitespaces, removeRightWhitespaces;
TQString str, nodeValue;
Node *n, *parent;
int i;
// Don't create DOM::Nodes from Quanta empty nodes outside the body or inside other not allowed element, or KHTML
// will give us problems.
bool canInsertEmptyNode = false;
if(node->tag->type == Tag::Empty)
{
if(!m_currentDoc->defaultDTD()->name.contains("HTML", false))
canInsertEmptyNode = true;
else
canInsertEmptyNode = kafkaCommon::hasParent(node, "body");
Node* parent_node = node->parent;
QTag* parent_node_description_tag = QuantaCommon::tagFromDTD(parent_node);
if(parent_node_description_tag && !parent_node_description_tag->isChild(node, false, true))
canInsertEmptyNode = false;
}
if(node->tag->type == Tag::XmlTag ||
((node->tag->type == Tag::Text || (node->tag->type == Tag::Empty && canInsertEmptyNode)) && !node->insideSpecial))
{
str = node->tag->name.lower();
//The basics DOM::Nodes HTML, HEAD and BODY are loaded anyway, but we must now
// link the real HTML,... to their Nodes.
//A basic Common tree is !doctype<-html<-(head, body)
if(!node->parent)
{//FIXME:html, head and body are HTML-specific tag, for others DTDs it might result to some pbs.
if(str == "html")
{
if(!html.isNull())//delete the empty Node
disconnectDomNodeFromQuantaNode(html);
newNode = html;
insertNode = false;
}
else if(str == "body")
{
if(!body.isNull())
disconnectDomNodeFromQuantaNode(body);
newNode = body;
insertNode = false;
}
else if(str == "head")
{
if(!head.isNull())
disconnectDomNodeFromQuantaNode(head);
newNode = head;
insertNode = false;
}
else
{
if(node->tag->type == Tag::Text || node->tag->type == Tag::Empty)
{
newNode = kafkaCommon::createTextDomNode("", m_kafkaPart->document());
}
else
{
newNode = kafkaCommon::createDomNode(node, m_kafkaPart->document());
}
}
}
else if(str == "html" && (!node->parent || (node->parent && !node->parent->parent)))
{
if(!html.isNull())//delete the empty Node
disconnectDomNodeFromQuantaNode(html);
newNode = html;
insertNode = false;
}
else if(str == "body" && ((node->parent && !node->parent->parent) || (node->parent &&
node->parent->parent && !node->parent->parent->parent)))
{
if(!body.isNull())
disconnectDomNodeFromQuantaNode(body);
newNode = body;
insertNode = false;
}
else if(str == "head" && ((node->parent && !node->parent->parent) || (node->parent &&
node->parent->parent && !node->parent->parent->parent)))
{
if(!head.isNull())
disconnectDomNodeFromQuantaNode(head);
newNode = head;
insertNode = false;
}
/** else if(node->parent->tag->str == "html")*/
else
{
if(node->tag->type == Tag::Text || node->tag->type == Tag::Empty)
{
newNode = kafkaCommon::createTextDomNode("", m_kafkaPart->document());
}
else
{
newNode = kafkaCommon::createDomNode(node->tag->name, m_currentDoc->defaultDTD(),
m_kafkaPart->document());
}
}
if(newNode.isNull())
{
#ifdef LIGHT_DEBUG
kdDebug(25001)<< "KafkaDocument::buildKafkaNodeFromNode() - ERROR null newNode" << endl;
#endif
return false;
}
connectDomNodeToQuantaNode(newNode, node);
if(node->tag->type == Tag::Text || node->tag->type == Tag::Empty)
{
//Now we get if the whitespaces at the left and right are kept or not.
//Left whitespaces are removed if:
//- It is the first child of a BLOCK
//- Previous Node(skipping inlines) is a text with spaces at the end
//Right whitespaces are removed if:
//- It is the last child of a BLOCK
removeLeftWhitespaces = false;
n = node;
parent = node->parent;
while(parent && parent->SFirstChild() == n)
{
if(!kafkaCommon::isInline(parent->tag->name))
{
removeLeftWhitespaces = true;
break;
}
n = parent;
parent = parent->parent;
}
if(!removeLeftWhitespaces)
{
n = node;
n = kafkaCommon::getPrevNodeNE(n);
while(n && (n->tag->type == Tag::XmlTagEnd ||
(n->tag->type == Tag::XmlTag && kafkaCommon::isInline(n->tag->name) &&
!n->tag->single)))
n = kafkaCommon::getPrevNodeNE(n);
if(n && n->tag->type == Tag::Text)
{
nodeValue = n->tag->tagStr();
if(nodeValue.length() > 0 && nodeValue[nodeValue.length() - 1].isSpace())
removeLeftWhitespaces = true;
}
}
removeRightWhitespaces = false;
n = node;
parent = node->parent;
while(parent && parent->SLastChild() == n)
{
if(!kafkaCommon::isInline(parent->tag->name))
{
removeRightWhitespaces = true;
break;
}
n = parent;
parent = parent->parent;
}
nodeValue = node->tag->tagStr();
nodeValue = getDecodedText(nodeValue, !kafkaCommon::hasParent(node, "pre"),
removeLeftWhitespaces, removeRightWhitespaces);
newNode.setNodeValue(nodeValue);
}
for(i = 0; i < node->tag->attrCount(); i++)
{
attr = kafkaCommon::createDomNodeAttribute(node, node->tag->attribute(i),
m_kafkaPart->document());
if(!attr.isNull())
{
//TODO: create a createAttr function and add this (setNodeValue sometimes
//don't like null TQString)
if(!node->tag->attributeValue(i).isNull())
attr.setNodeValue(node->tag->attributeValue(i));
kafkaCommon::insertDomNodeAttribute(newNode, attr);
}
}
if(node->next && node->next->tag && node->next->tag->name ==
("/" + node->tag->name))
{
//DEPRECATED, NO USE TO CHANGE THE above check
node->_closingNode = node->next;
}
if(insertNode)
{
ptDomNode = new DOM::Node(newNode);
node->setRootNode(ptDomNode);
n = node;
while(n->next)
{
n = n->next;
if(n->rootNode())
{
nextNode = *n->rootNode();
break;
}
}
if(node->parent && node->parent->leafNode())
parentNode = *node->parent->leafNode();
else if(node->parent && !node->parent->leafNode())
{
//the parent tag was invalid and khtml refuse to insert it
//so impossible to inser the current node
disconnectDomNodeFromQuantaNode(newNode);
if(node->rootNode())
delete node->rootNode();
node->setRootNode(0L);
return false;
}
else
parentNode = body;
//Set the visual enhancements.
ptDomNode = new DOM::Node(newNode);
node->setLeafNode(ptDomNode);
mainEnhancer->enhanceNode(node, parentNode, nextNode);
QTag* qTag = QuantaCommon::tagFromDTD(getCurrentDoc()->defaultDTD(),
parentNode.nodeName().string());
if(qTag && qTag->isChild(node, false))
{
if(nextNode.isNull())
{
if(!kafkaCommon::insertDomNode(newNode, parentNode))
{
disconnectDomNodeFromQuantaNode(newNode);
if(node->rootNode())
delete node->rootNode();
node->setRootNode(0L);
return false;
}
}
else
{
if(!kafkaCommon::insertDomNode(newNode, parentNode, nextNode))
{
disconnectDomNodeFromQuantaNode(newNode);
if(node->rootNode())
delete node->rootNode();
node->setRootNode(0L);
return false;
}
}
}
}
else
{
ptDomNode = new DOM::Node(newNode);
node->setRootNode(ptDomNode);
ptDomNode = new DOM::Node(newNode);
node->setLeafNode(ptDomNode);
}
}
else
{
if(node->parent && node->parent->leafNode())
parentNode = *node->parent->leafNode();
else
parentNode = body;
n = node;
while(n->next)
{
n = n->next;
if(n->rootNode())
{
nextNode = *n->rootNode();
break;
}
}
mainEnhancer->enhanceNode(node, parentNode, nextNode);
}
return true;
}
void KafkaDocument::buildNodeFromKafkaNode(Node *node, DOM::Node domNode)
{
#ifdef LIGHT_DEBUG
kdDebug(25001)<< "void KafkaDocument::buildNodeFromKafkaNode() - Node* DOM::Node" << endl;
#endif
long i;
if(!node) return;
node->tag->setCleanStrBuilt(false);
node->tag->setIndentationDone(false);
if(domNode.nodeType() == DOM::Node::TEXT_NODE)
{
TQString text = domNode.nodeValue().string();
text.replace("<", "<");
text.replace(">", ">");
node->tag->setStr(text);
}
else
{
while(node->tag->attrCount())
node->tag->deleteAttribute(0);
for(i = 0; (unsigned)i < domNode.attributes().length(); i++)
{
TagAttr attr;
attr.name = domNode.attributes().item(i).nodeName().string();
attr.value = domNode.attributes().item(i).nodeValue().string();
attr.quoted = true;
node->tag->addAttribute(attr);
}
}
}
Node * KafkaDocument::buildNodeFromKafkaNode(DOM::Node domNode, Node *nodeParent,
Node *beginNode, int beginOffset, Node */*endNode*/, int endOffset, NodeModifsSet *modifs)
{
#ifdef LIGHT_DEBUG
kdDebug(25001)<< "Node* KafkaDocument::buildNodeFromKafkaNode() - DOM::Node 2xNode* int: " <<
beginOffset << " Node* int: " << endOffset << " NodeModifsSet " << endl;
#else
Q_UNUSED(beginOffset);
Q_UNUSED(endOffset);
#endif
DOM::Node *ptDomNode;
Node *node;
if(domNode.isNull())
{
kdDebug(25001)<< "Node* KafkaDocument::buildNodeFromKafkaNode(DOM::Node, 2xNode*)" <<
" *ERROR* - empty _domNode"<< endl;
}
//nodeParent can be the false body node which is not in the tree.
if(nodeParent->tag->notInTree())
nodeParent = 0L;
/**_node = new Node(_nodeParent);*/
if(domNode.nodeType() == DOM::Node::TEXT_NODE)
{
node = kafkaCommon::createNode("#text", "",Tag::Text, m_currentDoc);
}
else
{
node = kafkaCommon::createNode(domNode.nodeName().string(), "",
Tag::XmlTag, m_currentDoc);
}
buildNodeFromKafkaNode(node, domNode);
connectDomNodeToQuantaNode(domNode, node);
ptDomNode = new DOM::Node(domNode);
node->setRootNode(ptDomNode);
ptDomNode = new DOM::Node(domNode);
node->setLeafNode(ptDomNode);
kafkaCommon::insertNode(node, nodeParent, beginNode, beginNode, modifs, false);
return node;
}
TQString KafkaDocument::getDecodedChar(const TQString &encodedChar)
{
TQMap<TQString, TQString>::Iterator it = encodedChars.find(encodedChar);
if(it == encodedChars.end())
{
//try this
return KGlobal::charsets()->resolveEntities(encodedChar);;
}
return it.data();
}
TQString KafkaDocument::getDecodedText(const TQString &a_encodedText, bool translateWhiteSpacesAndLineBreaks,
bool removeLeftWhitespaces, bool removeRightWhitespaces)
{
TQString encodedText = a_encodedText;
if (encodedText.isEmpty())
encodedText = " ";
TQString decodedChar;
int i, j;
#ifdef LIGHT_DEBUG
TQString oldEncodedText = encodedText;
#endif
i = -1;
while((unsigned)++i < encodedText.length() && translateWhiteSpacesAndLineBreaks)
{
if(encodedText[i].isSpace())
{
encodedText.remove(i, 1);
encodedText.insert(i, " ");
while((unsigned)++i < encodedText.length() && encodedText[i].isSpace())
{
encodedText.remove(i, 1);
i--;
}
}
}
i = -1;
while((unsigned)++i < encodedText.length())
{
if(TQString(encodedText[i]) == "&")
{
j = i;
while((unsigned)++i < encodedText.length() &&
TQString(encodedText[i]) != ";")
{}
decodedChar = getDecodedChar(encodedText.mid(j, i - j + 1));
encodedText.remove(j, i - j + 1);
//TODO:set a special behavior if the encoded symbol doesn't exist
encodedText.insert(j, decodedChar);
i = j + decodedChar.length() - 1;
}
}
if(translateWhiteSpacesAndLineBreaks && removeLeftWhitespaces && encodedText.length() > 0 &&
encodedText[0].isSpace())
encodedText.remove(0, 1);
if(translateWhiteSpacesAndLineBreaks && removeRightWhitespaces && encodedText.length() > 0 &&
encodedText[encodedText.length() - 1].isSpace())
encodedText.remove(encodedText.length() - 1, 1);
#ifdef LIGHT_DEBUG
kdDebug(25001)<< "KafkaDocument::getDecodedText() - \"" << oldEncodedText << "\" -> \"" <<
encodedText << "\"" << endl;
#endif
return encodedText;
//return KGlobal::charsets()->resolveEntities(encodedText); =>nice but not sufficient
}
TQString KafkaDocument::getEncodedChar(const TQString &decodedChar, const TQString &previousDecodedChar)
{
if(decodedChar[0].isSpace() && !previousDecodedChar[0].isSpace())
return " ";
else if(decodedChar[0].isSpace())
//FIXME:for an unknown reason, by default it return ¿ instead of
return " ";
TQMap<TQString, TQString>::Iterator it = decodedChars.find(decodedChar);
if(it == decodedChars.end())
return decodedChar;
return it.data();
}
TQString KafkaDocument::getEncodedText(const TQString &a_decodedText, int bLine, int bCol, int &eLine, int &eCol,
bool translateWhiteSpaces)
{
TQString decodedText = a_decodedText;
TQString Encodedchar;
TQString decodedChar, previousDecodedChar;
#ifdef LIGHT_DEBUG
TQString oldDecodedText = decodedText;
#endif
int i;
#ifdef LIGHT_DEBUG
int _bLine = bLine, _bCol = bCol;
#endif
i = -1;
while((unsigned)++i < decodedText.length())
{
previousDecodedChar = decodedChar;
decodedChar = TQString(decodedText[i]);
if(translateWhiteSpaces || !decodedText[i].isSpace())
Encodedchar = getEncodedChar(TQString(decodedText[i]),
(i>=1)?previousDecodedChar:TQString(""));
else
Encodedchar = decodedChar;
bCol += Encodedchar.length();
decodedText.remove(i,1);
decodedText.insert(i, Encodedchar);
i += Encodedchar.length() - 1;
}
eLine = bLine;
eCol = bCol - 1;
#ifdef LIGHT_DEBUG
kdDebug(25001)<< "KafkaDocument::getEncodedText() - " << oldDecodedText << " -> " << decodedText <<
" - " << _bLine << ":" << _bCol << " - " << eLine << ":" << eCol << endl;
#endif
return decodedText;
}
TQString KafkaDocument::getEncodedText(const TQString& decodedText)
{
int a = 0, b = 0, c, d;
return getEncodedText(decodedText, a, b, c, d);
}
TQString KafkaDocument::generateCodeFromNode(Node *node, int bLine, int bCol, int &eLine, int &eCol, bool encodeText)
{
TQString text, _char;
Node *openingNode;
int bLineAttr, bColAttr;
int j = 0;
bool hasPreParent;
if(!node) return "";
if(node->tag->type == Tag::XmlTag)
{
text = "<" + QuantaCommon::tagCase(node->tag->name);
bCol += node->tag->name.length() + 1;
for(j = 0; j < node->tag->attrCount(); j++)
{
text += " ";
bCol++;
bLineAttr = bLine;
bColAttr = bCol;
text += node->tag->attribute(j);
bCol += node->tag->attribute(j).length();
//doctype have only attrNames.
if(node->tag->name.lower() != "!doctype" && !node->tag->getAttribute(j).special)
{
text += "=";
bCol++;
if(node->tag->isQuotedAttribute(j))
{
text += qConfig.attrValueQuotation;
bCol++;
}
node->tag->setAttributePosition(j, bLineAttr, bColAttr, bLine, bCol);
text += node->tag->attributeValue(j);
bCol += node->tag->attributeValue(j).length();
if(node->tag->isQuotedAttribute(j))
{
text += qConfig.attrValueQuotation;
bCol++;
}
}
else
node->tag->setAttributePosition(j, bLineAttr, bColAttr, -2, -2);
}
//only single Nodes except !doctype and ?xml nodes in XML tag style get the "/"
if ( node->tag->dtd()->singleTagStyle == "xml" &&
(node->tag->single || (!qConfig.closeOptionalTags &&
QuantaCommon::isOptionalTag(node->tag->dtd()->name, node->tag->name)))
&& node->tag->name.lower() != "?xml" && node->tag->name.lower() != "!doctype")
{
text += " /";
bCol += 2;
}
//?xml nodes get a "?"
if(node->tag->name.lower() == "?xml")
{
text += "?";
bCol++;
}
text += ">";
eCol = bCol;
eLine = bLine;
}
else if(node->tag->type == Tag::XmlTagEnd)
{
openingNode = node->getOpeningNode();
if(openingNode && openingNode->tag->type == Tag::ScriptTag)
{
if(openingNode->tag->name.contains("XML PI", false) ||
openingNode->tag->name.contains("PHP", false))
text = "?>";
else if(openingNode->tag->name.contains("DTD", false))
text = ">";
else
text = ">";
}
else
{
if (node->tag->tagStr() == "-->")
text = "-->";
else
text = "<" + QuantaCommon::tagCase(node->tag->name) + ">";
}
bCol += text.length();
eCol = bCol - 1;
eLine = bLine;
}
else if(node->tag->type == Tag::Text)
{
hasPreParent = kafkaCommon::hasParent(node, "pre");
if(encodeText)
text = getEncodedText(node->tag->tagStr(), bLine, bCol, eLine, eCol,
!hasPreParent);
/** Can't use KGlobal::charsets()->toEntity() :
* It translate all chars into entities! */
else if(!hasPreParent)
text = node->tag->tagStr().replace(TQRegExp("\\s+"), " ");
else
text = node->tag->tagStr();
}
else if(node->tag->type == Tag::ScriptTag)
{
//WARNING : HTML SPECIFIC
if(node->tag->name.contains("style", false))
{
text = "<" + QuantaCommon::tagCase("style") + ">";
}
else if(node->tag->name.contains("DTD", false))
{
text = "<!";
}
else if(node->tag->name.contains("XML PI", false))
{
text = "<?xml";
}
else if(node->tag->name.contains("PHP", false))
{
text = "<?php";
}
bCol += text.length();
eCol = bCol - 1;
eLine = bLine;
}
else
{
//default behavior : return node->tag->tagStr()
text = node->tag->tagStr();
kafkaCommon::getEndPosition(text, bLine, bCol, eLine, eCol);
}
return text;
}
void KafkaDocument::translateQuantaIntoKafkaCursorPosition(uint curLine, uint curCol, DOM::Node &domNode, long &offset)
{
Node *node;
int bCol, bLine, eCol, eLine, col, line;
TQString curChar, decodedChar, currentLine;
bool lookForEntity, lookForSpaces, found;
node = baseNode;
while(node)
{
node->tag->beginPos(bLine, bCol);
node->tag->endPos(eLine, eCol);
if((bLine < (signed)curLine || (bLine == (signed)curLine && bCol <= (signed)curCol)) &&
(eLine > (signed)curLine || (eLine == (signed)curLine && eCol >= (signed)curCol)))
{
if(bLine == (signed)curLine && bCol == (signed)curCol && node->tag->type != Tag::Text &&
node->previousSibling() && node->previousSibling()->tag->type == Tag::Text)
{
//if we are at the end of a text which is recognized as the beginning of the next tag
node = node->previousSibling();
//we can go directly to the last offset
if(!node->rootNode())
{
offset = 0;
kdDebug(25001)<< "KafkaDocument::getKafkaCursorPosition() - ERROR DOM::Node not found"
<< endl;
return;
}
domNode = *node->rootNode();
offset = domNode.nodeValue().string().length();
#ifdef LIGHT_DEBUG
kdDebug(25001)<< "KafkaDocument::getKafkaCursorPosition() - " <<
domNode.nodeName().string() << ":" << offset << endl;
#endif
return;
}
col = bCol;
line = bLine;
break;
}
node = node->nextSibling();
}
if(!node)
{
kdDebug(25001)<< "KafkaDocument::getKafkaCursorPosition() - ERROR node not found" << endl;
return;
}
#ifdef LIGHT_DEBUG
kdDebug(25001)<< "KafkaDocument::getKafkaCursorPosition() - node :" << node->tag->name << ": type:" <<
node->tag->type << ": pos " << bLine << ":" << bCol << " - " << eLine << ":" << eCol << endl;
#endif
offset = 0;
if(node->rootNode() && node->rootNode()->nodeType() == DOM::Node::TEXT_NODE)
{
domNode = *node->rootNode();
currentLine = m_currentDoc->editIf->textLine(line);
if(line < eLine)
currentLine += " ";//remplace the \n
while(line < (signed)curLine || (col < (signed)curCol && line == (signed)curLine))
{
lookForEntity = false;
lookForSpaces = false;
curChar = currentLine.mid(col, 1);
if(curChar == "&")
lookForEntity = true;
else if(curChar[0].isSpace())
lookForSpaces = true;
found = false;
while(!found)
{
if((lookForEntity && curChar == ";") ||
!(lookForSpaces || lookForEntity))
found = true;
else if(lookForSpaces && !curChar[0].isSpace())//curChar != " ")
break;
if((col + 1) >= (signed)currentLine.length())
{
line++;
col = 0;
currentLine = m_currentDoc->editIf->textLine(line);
if(line < eLine)
currentLine += " ";//remplace the \n
}
else col++;
curChar = currentLine.mid(col, 1);
}
#ifdef HEAVY_DEBUG
kdDebug(25001)<< "endpos at " << line << ":" << col << endl;
#endif
offset++;
}
}
else if(node->rootNode())
offset = 0;//shoud we select?
else
m_kafkaPart->putCursorAtFirstAvailableLocation();//set the cursor in the first text
#ifdef LIGHT_DEBUG
if(!domNode.isNull())
kdDebug(25001)<< "KafkaDocument::getKafkaCursorPosition() - " << domNode.nodeName().string() <<
":" << offset << endl;
else
kdDebug(25001)<< "KafkaDocument::getKafkaCursorPosition() - NULL domNode" << endl;
#endif
}
void KafkaDocument::translateQuantaIntoNodeCursorPosition(uint line, uint col, Node **node, long &offset)
{
int curCol, curLine, beginCol, beginLine;
TQString currentLine;
*node = parser->nodeAt(line, col, false);
offset = 0;
if(!*node)
return;
if((*node)->tag->cleanStrBuilt() && (*node)->tag->indentationDone())
{
(*node)->tag->beginPos(beginLine, beginCol);
curLine = beginLine;
curCol = beginCol;
while(curLine < (signed)line)
{
currentLine = ViewManager::ref()->activeDocument()->editIf->textLine(curLine);
if(curLine == beginLine)
offset += (signed)currentLine.length() - beginCol;
else
offset += (signed)currentLine.length();
offset++;
curLine++;
}
if(beginLine != (signed)line)
offset += col;
else
offset += col - beginCol;
}
else
{
//TODO
}
#ifdef LIGHT_DEBUG
kdDebug(25001)<< "KafkaDocument::translateQuantaIntoNodeCursorPosition() - " << *node <<
":" << offset << endl;
#endif
}
void KafkaDocument::translateKafkaIntoNodeCursorPosition(DOM::Node domNode, long domNodeOffset, Node **node, long &offset)
{
TQString decodedText, encodedChar, encodedText, currentChar;
TQChar curChar, oldChar;
long currentOffset;
bool waitForSpace = false, found = false;
int curNodeOffset, bLine = 0, bCol = 0, eLine, eCol;
offset = 0;
(*node) = 0L;
if(domNode.isNull())
{
#ifdef HEAVY_DEBUG
kdDebug(25001)<< "KafkaDocument::translateKafkaIntoNodeCursorPosition()" <<
" - DOM::Node not found!" << endl;
#endif
return ;
}
//get the corresponding Node*
(*node) = getNode(domNode);
if(!(*node))
{
#ifdef HEAVY_DEBUG
kdDebug(25001)<< "KafkaDocument::translateKafkaIntoNodeCursorPosition()" <<
" - Node not found!" << endl;
#endif
return ;
}
if(!(*node)->tag)
{
#ifdef HEAVY_DEBUG
kdDebug(25001)<< "KafkaDocument::translateKafkaIntoNodeCursorPosition()" <<
" - Node tag not found!" << endl;
#endif
(*node) = 0L;
return ;
}
//If this node is selected (-1 means selected) then return "node selected" ;-)
if(domNodeOffset == -1)
{
return ;
}
if(!(*node)->tag->cleanStrBuilt())
{
//We NEED to have the up-to-date string in node.
(*node)->tag->setStr(generateCodeFromNode((*node), bLine, bCol, eLine, eCol));
//FIXME we shouldn't set it but if we don't the text will be re-encoded!
(*node)->tag->setCleanStrBuilt(true);
}
decodedText = domNode.nodeValue().string();
encodedText = (*node)->tag->tagStr();
currentOffset = domNodeOffset;
curNodeOffset = 0;
#ifdef HEAVY_DEBUG
kdDebug(25001)<< "KafkaDocument::translateKafkaIntoNodeCursorPosition() - decodedText:"<<
decodedText << ": encoded text:" << encodedText << ":" << endl;
#endif
if((*node)->tag->type == Tag::Text)
{
while(currentOffset > 0)
{
curChar = decodedText.at(domNodeOffset - currentOffset);
#ifdef HEAVY_DEBUG
//kdDebug(24000)<< "CurChar:" << TQString(curChar) << ": oldChar:" <<
// TQString(oldChar) << endl;
#endif
encodedChar = getEncodedChar(TQString(curChar), TQString(oldChar));
if(encodedChar == " ")
encodedChar = " ";
found = false;
waitForSpace = false;
while(!found)
{
#ifdef HEAVY_DEBUG
//kdDebug(25001)<< "look 4 :" << encodedChar << ": found :" <<
// encodedText.mid(curNodeOffset, encodedChar.length()) << endl;
#endif
if(encodedChar != " " && encodedChar == encodedText.mid(curNodeOffset,
encodedChar.length()))
found = true;
else if(encodedChar == " " && encodedText.mid(curNodeOffset, 1).at(0).isSpace())
waitForSpace = true;
else if(waitForSpace)
break;//no more spaces
else if(encodedChar == " " && encodedText.mid(curNodeOffset, 6) == " ")
{
encodedChar = " ";
found = true;
}
if(curNodeOffset > (signed)encodedText.length() || encodedChar.isEmpty())
{
//The good nodeOffset was not found. Returns a default value.
//encodedChar.isEmpty() -->prevent an infinite loop
curNodeOffset = 0;
break;
}
else
curNodeOffset += encodedChar.length();
}
oldChar = curChar;
currentOffset--;
}
}
#ifdef LIGHT_DEBUG
kdDebug(25001)<<"KafkaDocument::translateKafkaIntoNodeCursorPosition() - " <<
curNodeOffset << endl;
#endif
offset = curNodeOffset;
}
void KafkaDocument::translateKafkaIntoQuantaCursorPosition(DOM::Node _currentDomNode, int offset, int &line, int &col)
{
Node *_currentNode, *closingNode, *node;
kNodeAttrs *attrs;
TQString decodedText, encodedChar, currentLine, currentChar;
TQChar curChar, oldChar;
int currentOffset;
int curLine, curCol, endLine, endCol;
int i;
bool waitForSpace = false, found = false;
bool tagLeft = false, tagRight = false, tagMiddle = false;
bool specialBehavior = false;
//m_kafkaPart->getCurrentNode(_currentDomNode, offset);
currentOffset = offset;
if(_currentDomNode.isNull())
{
kdDebug(25001)<< "KafkaDocument::getQuantaCursorPosition() - DOM::Node not found!" << endl;
line = 0;
col = 0;
return;
}
attrs = getAttrs(_currentDomNode);
if(!attrs)
{
kdDebug(25001)<< "KafkaDocument::getQuantaCursorPosition() - Attrs not found!" << endl;
line = 0;
col = 0;
return;
}
//If the current DOM::Node has a special behavior (cf nodeproperties.h), get the nearest
//node which can get the focus!
if(attrs->specialBehavior() != kNodeAttrs::none)
{
specialBehavior = true;
if(attrs->specialBehavior() == kNodeAttrs::emptyTextSurroundingBlockElementAtTheLeft)
{
_currentDomNode = _currentDomNode.nextSibling();
//tagRight means to put the cursor at the left of the tag so that the cursor
//looks at its right place (otherwise as there is no Node for this DOM::Node,
//the cursor won't go anywhere!)
tagLeft = true;
}
else if(attrs->specialBehavior() == kNodeAttrs::emptyTextSurroundingBlockElementAtTheRight)
{
_currentDomNode = _currentDomNode.previousSibling();
tagRight = true;
}
else if(attrs->specialBehavior() == kNodeAttrs::emptyTextAsChildOfAChildlessElement)
{
_currentDomNode = _currentDomNode.parentNode();
tagMiddle = true;
}
attrs = getAttrs(_currentDomNode);
if(!attrs)
{
kdDebug(25001)<< "KafkaDocument::getQuantaCursorPosition() - Attrs not found!" << endl;
line = 0;
col = 0;
return;
}
}
_currentNode = attrs->getNode();
if(!_currentNode /* && attrs->specialBehavior() == kNodeAttrs::none -- Andras: what happens if _currentNode is null, but this part of the if is not true?*/)
{
kdDebug(25001)<< "KafkaDocument::getQuantaCursorPosition() - Node not found!" << endl;
line = 0;
col = 0;
return;
}
decodedText = _currentDomNode.nodeValue().string();
_currentNode->tag->beginPos(curLine, curCol);
_currentNode->tag->endPos(endLine, endCol);
currentLine = m_currentDoc->editIf->textLine(curLine);
if(curLine < endLine)
currentLine += " ";//remplace the \n
if(offset == -1)
{
if (m_currentDoc->selectionIf)
m_currentDoc->selectionIf->setSelection(curLine, curCol, endLine, endCol + 1);
line = endLine;
col = endCol + 1;
return;
}
#ifdef HEAVY_DEBUG
// kdDebug(25001)<< "KafkaDocument::getQuantaCursorPosition() - decodedText:"<< decodedText << ":" << endl;
#endif
if(_currentNode->tag->type == Tag::Text)
{
while(currentOffset > 0)
{
curChar = decodedText[offset - currentOffset];
#ifdef HEAVY_DEBUG
// kdDebug(24000)<< "CurChar:" << TQString(curChar) << ": oldChar:" << TQString(oldChar) << endl;
#endif
encodedChar = getEncodedChar(TQString(curChar), TQString(oldChar));
if(encodedChar == " ")
encodedChar = " ";
found = false;
waitForSpace = false;
while(!found)
{
#ifdef HEAVY_DEBUG
// kdDebug(25001)<< "look 4 :" << encodedChar << ": found :" <<
// currentLine.mid(curCol, encodedChar.length()) << endl;
// kdDebug(25001)<< "pos " << curLine << ":" << curCol << endl;
// kdDebug(25001)<< "endPos " << endLine << ":" << endCol << endl;
#endif
if(encodedChar != " " && encodedChar == currentLine.mid(curCol, encodedChar.length()))
found = true;
else if(encodedChar == " " && currentLine.mid(curCol, 1).at(0).isSpace())
waitForSpace = true;
else if(waitForSpace)
break;//no more spaces
else if(encodedChar == " " && currentLine.mid(curCol, 6) == " ")
{
encodedChar = " ";
found = true;
}
if((curCol > endCol && curLine == endLine) || curLine > endLine ||
encodedChar.length() == 0)
{
//encodedChar.length() == 0 =>prevent infinite loop
curLine = endLine;
curCol = endCol + 1;
currentOffset = 0;
break;
}
else if((curCol + encodedChar.length()) >= currentLine.length())
{
++curLine;
curCol = 0;
currentLine = m_currentDoc->editIf->textLine(curLine);
if(curLine < endLine)
currentLine += " ";//remplace the \n
}
else
{
curCol += encodedChar.length();
}
}
oldChar = curChar;
--currentOffset;
}
}
else if(_currentNode->tag->type == Tag::XmlTag)
{
//If we are in the special case
if(specialBehavior)
{
if(tagLeft)
_currentNode->tag->beginPos(curLine, curCol);
else if(tagRight)
{
closingNode = _currentNode->getClosingNode();
if(closingNode)
closingNode->tag->endPos(curLine, curCol);
else
_currentNode->tag->endPos(curLine, curCol);
++curCol;
}
else
{
_currentNode->tag->endPos(curLine, curCol);
++curCol;
}
}
else
{
if(_currentNode->tag->single)
{
//The tag is single, there shouldn't be any (tag, x) location, but the old way
//was (tag,0) for left of the tag and (tag,1) for the right of the tag)
if(offset == 1)
{
_currentNode->tag->endPos(curLine, curCol);
curCol++;
}
else
_currentNode->tag->beginPos(curLine, curCol);
}
else
{
//The new way to define cursor position.
node = _currentNode->SFirstChild();
i = 1;
while(i < offset && node && node->SNext())
{
node = node->SNext();
++i;
}
if(!node)
node = _currentNode;
if(offset == 0)
node->tag->beginPos(curLine, curCol);
else
{
closingNode = node->getClosingNode();
if(closingNode)
closingNode->tag->endPos(curLine, curCol);
else
node->tag->endPos(curLine, curCol);
++curCol;
}
}
}
}
line = curLine;
col = curCol;
#ifdef LIGHT_DEBUG
kdDebug(25001)<<"KafkaDocument::getQuantaCursorPosition() - " << line << ":" << col << endl;
#endif
return;
}
void KafkaDocument::translateNodeIntoKafkaCursorPosition(Node *node, int offset, DOM::Node &domNode,
long &domNodeOffset)
{
TQString textNode, curChar;
int col;
bool lookForEntity, lookForSpaces, found;
if(node && node->rootNode() && node->rootNode()->nodeType() == DOM::Node::TEXT_NODE)
{
domNodeOffset = 0;
domNode = *node->rootNode();
textNode = node->tag->tagStr();
col = 0;
while(col < offset)
{
lookForEntity = false;
lookForSpaces = false;
curChar = textNode.mid(col, 1);
if(curChar == "&")
lookForEntity = true;
else if(curChar[0].isSpace())
lookForSpaces = true;
found = false;
while(!found && col < offset)
{
if((lookForEntity && curChar == ";") ||
!(lookForSpaces || lookForEntity))
found = true;
else if(lookForSpaces && !curChar[0].isSpace())//curChar != " ")
break;
col++;
curChar = textNode.mid(col, 1);
}
#ifdef HEAVY_DEBUG
//kdDebug(25001)<< "endpos at " << line << ":" << col << endl;
#endif
domNodeOffset++;
}
}
else if(node && node->rootNode())
{
domNode = *node->rootNode();
domNodeOffset = 0;//shoud we select?
}
else
{
domNode = DOM::Node();
domNodeOffset = 0;
}
}
void KafkaDocument::translateNodeIntoQuantaCursorPosition(Node *node, int offset, uint &line, uint &col)
{
int curCol, curLine, curOffset;
node->tag->beginPos(curLine, curCol);
line = curLine;
col = curCol;
curOffset = offset;
while(curOffset > 0)
{
if(node->tag->tagStr()[offset - curOffset] == '\n')
line++;
else
col++;
curOffset--;
}
}
bool KafkaDocument::insertDomNode(DOM::Node node, DOM::Node parent,
DOM::Node nextSibling, DOM::Node rootNode)
{
DOM::Node siblingNSpecial;
//First insert the node
if(!kafkaCommon::insertDomNode(node, parent, nextSibling, rootNode))
return false;
//Then unEnhance and reEnhance the nearest non special nodes so that everything's fine.
siblingNSpecial = getPrevSiblingNSpecial(node);
if(!siblingNSpecial.isNull())
{
mainEnhancer->postUnenhanceNode(siblingNSpecial);
mainEnhancer->postEnhanceNode(siblingNSpecial);
}
siblingNSpecial = getNextSiblingNSpecial(node);
if(!siblingNSpecial.isNull())
{
mainEnhancer->postUnenhanceNode(siblingNSpecial);
mainEnhancer->postEnhanceNode(siblingNSpecial);
}
mainEnhancer->postUnenhanceNode(node.parentNode());
mainEnhancer->postEnhanceNode(node.parentNode());
mainEnhancer->postEnhanceNode(node);
return true;
}
bool KafkaDocument::removeDomNode(DOM::Node node)
{
DOM::Node nextSiblingNSpecial, prevSiblingNSpecial, parent;
//First remove the node
prevSiblingNSpecial = getPrevSiblingNSpecial(node);
nextSiblingNSpecial = getNextSiblingNSpecial(node);
parent = node.parentNode();
if(!kafkaCommon::removeDomNode(node))
return false;
//Then unEnhance and reEnhance the nearest non special nodes so that everything's fine.
if(!prevSiblingNSpecial.isNull())
{
mainEnhancer->postUnenhanceNode(prevSiblingNSpecial);
mainEnhancer->postEnhanceNode(prevSiblingNSpecial);
}
if(!nextSiblingNSpecial.isNull())
{
mainEnhancer->postUnenhanceNode(nextSiblingNSpecial);
mainEnhancer->postEnhanceNode(nextSiblingNSpecial);
}
mainEnhancer->postUnenhanceNode(parent);
mainEnhancer->postEnhanceNode(parent);
return true;
}
DOM::Node KafkaDocument::getPrevSiblingNSpecial(DOM::Node domNode)
{
kNodeAttrs *attrs;
if(domNode.isNull())
return DOM::Node();
domNode = domNode.previousSibling();
while(!domNode.isNull())
{
attrs = getAttrs(domNode);
if(!attrs)
return DOM::Node();
if(attrs->specialBehavior() == kNodeAttrs::none)
return domNode;
domNode = domNode.previousSibling();
}
return DOM::Node();
}
DOM::Node KafkaDocument::getNextSiblingNSpecial(DOM::Node domNode)
{
kNodeAttrs *attrs;
if(domNode.isNull())
return DOM::Node();
domNode = domNode.nextSibling();
while(!domNode.isNull())
{
attrs = getAttrs(domNode);
if(!attrs)
return DOM::Node();
if(attrs->specialBehavior() == kNodeAttrs::none)
return domNode;
domNode = domNode.nextSibling();
}
return DOM::Node();
}
void KafkaDocument::readConfig(KConfig *m_config)
{
#ifdef LIGHT_DEBUG
kdDebug(25001)<< "KafkaDocument::readConfig()" << endl;
#endif
//for each nodeEnhancer
if(mainEnhancer)
mainEnhancer->readConfig(m_config);
//reload the document
reloadDocument();
}
const DTDStruct* KafkaDocument::defaultDTD()
{
return m_currentDoc->defaultDTD();
}
#ifdef HEAVY_DEBUG
void KafkaDocument::coutLinkTree(Node *node, int indent)
#else
void KafkaDocument::coutLinkTree(Node *, int)
#endif
{
#ifdef HEAVY_DEBUG
TQString output, dots;
DOM::Node domNode;
Node *n = 0L;
if(!node)
kdDebug(25001)<< "kafkaDocument::coutTree() - bad node!" << endl;
while (node)
{
dots = "";
dots.fill('*', indent);
output = dots;
if (node->tag->type != Tag::Text)
output += node->tag->name.replace('\n'," ");
else
output+= node->tag->tagStr().replace('\n'," ");
output += " (";
output += node->tag->type;
output += ") ";
n = 0L;
if(node->rootNode())
{
domNode = *node->rootNode();
n = getNode(domNode);
}
kdDebug(25001) << output <<" (" << node << ") " << domNode.handle() << " - " << n <<endl;
if (node->child)
coutLinkTree(node->child, indent + 4);
node = node->next;
}
#endif
}
void KafkaDocument::slotDomNodeInserted(DOM::Node domNode, bool insertChilds, NodeModifsSet* modifs)
{
#ifdef LIGHT_DEBUG
if(!domNode.isNull())
kdDebug(25001)<< "KafkaDocument::slotDomNodeInserted() - DOM::Node: " <<
domNode.nodeName().string() << endl;
else
kdDebug(25001)<< "KafkaDocument::slotDomNodeInserted() - DOM::Node: NULL" << endl;
#endif
Node *_nodeParent = 0L, *nodeNext = 0L, *_node = 0L;
DOM::Node tmpDomNode, nextDomNode;
bool b = false;
#ifdef LIGHT_DEBUG
TQTime t;
t.start();
#endif
_nodeParent = getNode(domNode.parentNode());
if(!_nodeParent)
{//DOM::Node not found, strange...
kdDebug(25001)<< "KafkaDocument::slotDomNodeInserted() - *ERROR* the" <<
" corresponding DOM::Node is not found!" << endl;
return;
}
nextDomNode = getNextSiblingNSpecial(domNode);
if(!nextDomNode.isNull())
{
nodeNext = getNode(nextDomNode);
if(!nodeNext)
{
kdDebug(25001)<< "KafkaDocument::slotDomNodeInserted() - *ERROR2*" <<
" the corresponding DOM::Node is not found!" << endl;
return;
}
}
_node = buildNodeFromKafkaNode(domNode, _nodeParent, nodeNext, 0, 0L, 0, modifs);
if(insertChilds && domNode.hasChildNodes())
{
//TODO: check if it is working
tmpDomNode = domNode.firstChild();
while(!tmpDomNode.isNull())
{
buildNodeFromKafkaNode(tmpDomNode,
getNode(tmpDomNode.parentNode()), 0L, 0, 0L, 0, modifs);
tmpDomNode = kafkaCommon::getNextDomNode(tmpDomNode, b, false, domNode);
}
}
#ifdef LIGHT_DEBUG
kdDebug(25001)<< "KafkaDocument::slotDomNodeInserted() in " << t.elapsed() <<
" ms only!" << endl;
#endif
#ifdef HEAVY_DEBUG
kafkaCommon::coutTree(baseNode, 2);
#endif
}
void KafkaDocument::slotDomNodeModified(DOM::Node domNode, NodeModifsSet* modifs)
{
#ifdef LIGHT_DEBUG
if(!domNode.isNull())
kdDebug(25001)<< "KafkaDocument::slotDomNodeModified() - DOM::Node: " <<
domNode.nodeName().string() << endl;
else
kdDebug(25001)<< "KafkaDocument::slotDomNodeModfied() - DOM::Node: NULL" << endl;
#endif
Node *node = 0L;
NodeModif *modif;
kNodeAttrs *props, *newProps;
DOM::Node newDomNode, parentDomNode, nextSiblingDomNode;
DOM::Node *ptDomNode;
QTag *qTag;
#ifdef LIGHT_DEBUG
TQTime t;
t.start();
#endif
//gets the DOM::Node's kNodeAttrs
props = domNodeProps[domNode.handle()];
if(!props)
{
kdDebug(25001)<< "KafkaDocument::slotDomNodeModified - *ERROR " <<
" missing kNodeAttrs for a DOM::Node!!!" << endl;
return;
}
//First look if domNode has a corresponding Node.
if(props->isLinkedToNode())
{
//Look which Node correspond to this DOM::Node
node = props->getNode();
if(!node)
{//DOM::Node not found, weird...
kdDebug(25001)<< "KafkaDocument::slotDomNodeModified() - *ERROR* the" <<
" corresponding DOM::Node is not found!" << endl;
return;
}
modif = new NodeModif();
modif->setType(NodeModif::NodeModified);
modif->setTag(new Tag(*(node->tag)));
modif->setLocation(kafkaCommon::getLocation(node));
buildNodeFromKafkaNode(node, domNode);
if (!modifs)
modifs = new NodeModifsSet();
modifs->addNodeModif(modif);
}
else
{
//no corresponding Node, we are in a special case with a special behavior.
qTag = QuantaCommon::tagFromDTD(getNode(domNode.parentNode()));
if(((!domNode.parentNode().isNull() && domNode.parentNode().nodeName() == "#document") ||
qTag) && (
props->specialBehavior() == kNodeAttrs::emptyTextSurroundingBlockElementAtTheLeft ||
props->specialBehavior() == kNodeAttrs::emptyTextSurroundingBlockElementAtTheRight ||
props->specialBehavior() == kNodeAttrs::emptyTextAsChildOfAChildlessElement))
{
//let's create the corresponding Text Node and the P tag only if necessary
modifs = new NodeModifsSet();
modif = new NodeModif();
parentDomNode = domNode.parentNode();
nextSiblingDomNode = domNode.nextSibling();
if(!qTag->isChild("#text", false))
{
newDomNode = kafkaCommon::createDomNode("p",
getNode(domNode.parentNode())->tag->dtd(), m_kafkaPart->document());
kafkaCommon::removeDomNode(domNode);
kafkaCommon::insertDomNode(newDomNode, parentDomNode, nextSiblingDomNode);
kafkaCommon::insertDomNode(domNode, newDomNode);
node = kafkaCommon::createNode("p", "", Tag::XmlTag, m_currentDoc);
newProps = connectDomNodeToQuantaNode(newDomNode, node);
ptDomNode = new DOM::Node(newDomNode);
node->setRootNode(ptDomNode);
ptDomNode = new DOM::Node(newDomNode);
node->setLeafNode(ptDomNode);
node = kafkaCommon::insertNode(node, getNode(parentDomNode),
getNode(nextSiblingDomNode), getNode(nextSiblingDomNode), modifs);
newProps->setNode(node);
modifs->addNodeModif(modif);
parentDomNode = newDomNode;
nextSiblingDomNode = DOM::Node();
modif = new NodeModif();
}
node = kafkaCommon::createNode("", domNode.nodeValue().string(), Tag::Text,
m_currentDoc);
ptDomNode = new DOM::Node(domNode);
node->setRootNode(ptDomNode);
ptDomNode = new DOM::Node(domNode);
node->setLeafNode(ptDomNode);
//avoid the merging of Text Nodes
node = kafkaCommon::insertNode(node, getNode(parentDomNode),
getNode(nextSiblingDomNode), modifs, false);
props->setNode(node);
modifs->addNodeModif(modif);
//If there is a empty Node after of before the text, remove them
if(node->prev && node->prev->tag->type == Tag::Empty)
kafkaCommon::extractNode(node->prev, modifs);
if(node->next && node->next->tag->type == Tag::Empty)
kafkaCommon::extractNode(node->next, modifs);
//Log the changes
m_currentDoc->docUndoRedo->addNewModifsSet(modifs, undoRedo::KafkaModif);
}
props->setIsLinkedToNode(true);
props->setSpecialBehavior(kNodeAttrs::none);
}
#ifdef LIGHT_DEBUG
kdDebug(25001)<< "KafkaDocument::slotDomNodeModified() in " << t.elapsed() <<
" ms only!" << endl;
#endif
#ifdef HEAVY_DEBUG
kafkaCommon::coutTree(baseNode, 2);
#endif
}
void KafkaDocument::slotDomNodeAboutToBeRemoved(DOM::Node _domNode, bool deleteChilds, NodeModifsSet* modifs)
{
#ifdef LIGHT_DEBUG
if(!_domNode.isNull())
kdDebug(25001)<< "KafkaDocument::slotDomNodeAboutToBeRemoved() - DOM::Node: " <<
_domNode.nodeName().string() << "(" << _domNode.handle() << ")"<< " bool :" <<
deleteChilds << endl;
else
kdDebug(25001)<< "KafkaDocument::slotDomNodeAboutToBeRemoved() - DOM::Node: NULL bool :" <<
deleteChilds << endl;
#endif
Node *_node = 0L, *_nodeNext = 0L, *_tmpNode = 0L, *n = 0L;
int i, bLine, bCol, eLine, eCol, bLine2, bCol2;
bool hasClosingNode = false, b;
NodeModif *modif;
#ifdef LIGHT_DEBUG
TQTime t;
t.start();
#endif
_node = getNode(_domNode);
if(!_node)
{
kdDebug(25001)<<"KafkaDocument::slotDomNodeAboutToBeRemoved() - *ERROR* the" <<
" corresponding DOM::Node is not found!" << endl;
return;
}
//If we are deleting a PHP Node which is embedded into a tag e.g. <a <? echo boo; ?> >
//We must regenerate the <a> tag string.
if(_node->tag->type == Tag::ScriptTag && _node->parent)
{
_node->parent->tag->beginPos(bLine, bCol);
_node->parent->tag->endPos(eLine, eCol);
_node->tag->beginPos(bLine2, bCol2);
if(QuantaCommon::isBetween(bLine2, bCol2, bLine, bCol, eLine,eCol) == 0)
_node->parent->tag->setCleanStrBuilt(false);
}
if(_node->prev)
{
//delete the previous empty tag if present
_nodeNext = _node;
_node = _node->prev;
if(_node && _node->tag->type == Tag::Empty)
{
modif = new NodeModif();
modif->setType(NodeModif::NodeRemoved);
modif->setLocation(kafkaCommon::getLocation(_node));
if(_node->parent && _node->parent->child == _node)
_node->parent->child = _node->next;
if(_node->prev)
_node->prev->next = _node->next;
if(_node->next)
_node->next->prev = _node->prev;
if(_node == baseNode)
baseNode = _node->next;
_node->parent = 0L;
_node->prev = 0L;
_node->next = 0L;
_node->child = 0L;
modif->setNode(_node);
modifs->addNodeModif(modif);
}
_node = _nodeNext;
}
//delete the Node
modif = new NodeModif();
if(deleteChilds)
modif->setType(NodeModif::NodeAndChildsRemoved);
else
modif->setType(NodeModif::NodeRemoved);
modif->setLocation(kafkaCommon::getLocation(_node));
if(_node->getClosingNode())
hasClosingNode = true;
else
hasClosingNode = false;
//_node->removeAll = false;
if(_node->parent && _node->parent->child == _node)
_node->parent->child = _node->next;
if(_node->next)
_node->next->prev = _node->prev;
if(_node->prev)
_node->prev->next = _node->next;
i = 0;
if(_node->child && deleteChilds)
{
_tmpNode = _node->child;
b = false;
while(_tmpNode)
{
if(_tmpNode->rootNode())
disconnectDomNodeFromQuantaNode(*_tmpNode->rootNode());
if(_tmpNode->leafNode())
disconnectDomNodeFromQuantaNode(*_tmpNode->leafNode());
_tmpNode = kafkaCommon::getNextNode(_tmpNode, b, _node);
}
//delete _node->child;
}
else if(_node->child)// && !deleteChilds
{
if(_node->parent && !_node->parent->child)
_node->parent->child = _node->child;
if(_node->prev)
_node->prev->next = _node->child;
_tmpNode = _node->child;
while(_tmpNode)
{
i++;
_tmpNode->parent = _node->parent;
n = _tmpNode;
_tmpNode = _tmpNode->next;
if(!_tmpNode)
{
n->next = _node->next;
if(_node->next)
_node->next->prev = n;
}
}
}
if(_node == baseNode)
baseNode = _node->next;
if(_node->rootNode())
disconnectDomNodeFromQuantaNode(*_node->rootNode());
if(_node->leafNode())
disconnectDomNodeFromQuantaNode(*_node->leafNode());
_node->parent = 0L;
_nodeNext = _node->next;
_node->next = 0L;
_node->prev = 0L;
_node->child = 0L;
modif->setNode(_node);
//delete _node;
modif->setChildrenMovedUp(i);
modifs->addNodeModif(modif);
_node = _nodeNext;
if(hasClosingNode)
{
//delete the closing Node if present
if(_node->parent && _node->parent->child == _node)
_node->parent->child = _node->next;
if(_node->prev)
_node->prev->next = _node->next;
if(_node->next)
_node->next->prev = _node->prev;
_node->parent = 0L;
_nodeNext = _node->next;
_node->next = 0L;
_node->prev = 0L;
_node->child = 0L;
modif = new NodeModif();
modif->setType(NodeModif::NodeRemoved);
modif->setLocation(kafkaCommon::getLocation(_node));
modif->setNode(_node);
modifs->addNodeModif(modif);
//delete _node;
_node = _nodeNext;
}
if(_node && _node->tag->type == Tag::Empty)
{
//delete the next empty tag if present
modif = new NodeModif();
modif->setType(NodeModif::NodeRemoved);
modif->setLocation(kafkaCommon::getLocation(_node));
if(_node->parent && _node->parent->child == _node)
_node->parent->child = _node->next;
if(_node->prev)
_node->prev->next = _node->next;
if(_node->next)
_node->next->prev = _node->prev;
_node->parent = 0L;
_nodeNext = _node->next;
_node->prev = 0L;
_node->next = 0L;
_node->child = 0L;
modif->setNode(_node);
modifs->addNodeModif(modif);
_node = _nodeNext;
}
//NO NORMALIZATION!! It is KafkaWidget::normalize()'s job!
#ifdef LIGHT_DEBUG
kdDebug(25001)<< "KafkaDocument::slotDomNodeDeleted() in " << t.elapsed() <<
" ms only!" << endl;
#endif
#ifdef HEAVY_DEBUG
kafkaCommon::coutTree(baseNode, 2);
#endif
}
void KafkaDocument::slotDomNodeIsAboutToBeMoved(DOM::Node domNode, DOM::Node newParent, DOM::Node before, NodeModifsSet* modifs)
{
Node *node, *parent, *nextSibling, *closingNode;
if(domNode.isNull())
return;
node = getNode(domNode);
parent = getNode(newParent);
nextSibling = getNode(before);
if(!node)
return;
closingNode = node->getClosingNode();
kafkaCommon::moveNode(node, parent, nextSibling, modifs, false);
if(closingNode)
kafkaCommon::moveNode(closingNode, parent, nextSibling, modifs, false);
}
void KafkaDocument::slotdomNodeNewCursorPos(DOM::Node, int)
{
#ifdef LIGHT_DEBUG
kdDebug(25001)<< "KafkaDocument::slotdomNodeNewCursorPos()" << endl;
#endif
//int line, col;
//dont calculate cursor pos until the next view update
//getQuantaCursorPosition(line, col);
//emit newCursorPosition(line, col);
}
void KafkaDocument::slotCut()
{
TQString text = m_kafkaPart->selectedText();
NodeSelectionInd selection_ind;
selection_ind.fillWithVPLCursorSelection();
int startOffset = selection_ind.cursorOffset();
int endOffset = selection_ind.cursorOffsetEndSel();
Node* startNode = kafkaCommon::getNodeFromLocation(selection_ind.cursorNode());
Node* endNode = kafkaCommon::getNodeFromLocation(selection_ind.cursorNodeEndSel());
DOM::Node cursorDomNode;
long cursorOffset;
m_kafkaPart->getCurrentNode(cursorDomNode, cursorOffset);
Node* cursorNode = getNode(cursorDomNode);
slotCut(startNode, startOffset, endNode, endOffset, &cursorNode, cursorOffset, text);
}
void KafkaDocument::slotCut(Node* startNode, int startOffset, Node* endNode, int endOffset,
Node** cursorNode, long cursorOffset, TQString const& text)
{
if(!startNode || !endNode)
return;
NodeModifsSet *modifs = new NodeModifsSet();
Node* subtree_root = kafkaCommon::DTDExtractNodeSubtree(startNode, startOffset, endNode, endOffset,
cursorNode, cursorOffset, modifs);
m_currentDoc->docUndoRedo->addNewModifsSet(modifs, undoRedo::NodeTreeModif);
//Now update the VPL cursor position
kafkaWidget->setCurrentNode(startNode, startOffset);
if(subtree_root)
{
KafkaDragObject* node_drag = new KafkaDragObject(subtree_root);
TQTextDrag* text_drag = new TQTextDrag(text);
KMultipleDrag* drag_object = new KMultipleDrag();
drag_object->addDragObject(node_drag);
drag_object->addDragObject(text_drag);
TQApplication::clipboard()->setData(drag_object);
#ifdef LIGHT_DEBUG
kafkaCommon::coutTree(subtree_root, 3);
#endif
}
}
void KafkaDocument::slotCopy()
{
TQString text = m_kafkaPart->selectedText();
NodeSelectionInd selection_ind;
selection_ind.fillWithVPLCursorSelection();
int startOffset = selection_ind.cursorOffset();
int endOffset = selection_ind.cursorOffsetEndSel();
Node* startNode = kafkaCommon::getNodeFromLocation(selection_ind.cursorNode());
Node* endNode = kafkaCommon::getNodeFromLocation(selection_ind.cursorNodeEndSel());
slotCopy(startNode, startOffset, endNode, endOffset, text);
}
void KafkaDocument::slotCopy(Node* startNode, int startOffset, Node* endNode, int endOffset, TQString const& text)
{
if(!startNode || !endNode)
return;
Node* subtree_root = kafkaCommon::getNodeSubtree(startNode, startOffset, endNode, endOffset);
if(subtree_root)
{
KafkaDragObject* node_drag = new KafkaDragObject(subtree_root);
TQTextDrag* text_drag = new TQTextDrag(text);
KMultipleDrag* drag_object = new KMultipleDrag();
drag_object->addDragObject(node_drag);
drag_object->addDragObject(text_drag);
TQApplication::clipboard()->setData(drag_object);
// FIXME delete the subtree
#ifdef LIGHT_DEBUG
kafkaCommon::coutTree(subtree_root, 3);
#endif
}
}
void KafkaDocument::slotPaste()
{
QClipboard *cb = TQApplication::clipboard();
TQMimeSource* e = cb->data();
Node* node = new Node(0);
if(KafkaDragObject::decode(e, node))
{
bool go_up = false;
for(Node* aux = node; aux; aux = kafkaCommon::getNextNode(aux, go_up))
kafkaCommon::restorePastedNode(aux, getCurrentDoc());
NodeSelectionInd selection_ind;
selection_ind.fillWithVPLCursorSelection();
Node* cursorNode = kafkaCommon::getNodeFromLocation(selection_ind.cursorNode());
long cursorOffset = selection_ind.cursorOffset();
NodeModifsSet *modifs = new NodeModifsSet();
if(selection_ind.hasSelection())
kafkaCommon::DTDRemoveSelection(selection_ind, &cursorNode, cursorOffset, modifs);
else
cursorNode = 0; // use selection_ind
kafkaCommon::DTDInsertNodeSubtree(node, selection_ind, &cursorNode, cursorOffset, modifs);
m_currentDoc->docUndoRedo->addNewModifsSet(modifs, undoRedo::NodeTreeModif, 0, false);
//Now update the VPL cursor position
kafkaWidget->setCurrentNode(cursorNode, cursorOffset);
}
}
|