mshtml.cpp
51.4 KB
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
/*
This file is a part of the NVDA project.
URL: http://www.nvda-project.org/
Copyright 2006-2010 NVDA contributers.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2.0, as published by
the Free Software Foundation.
This program 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.
This license can be found at:
http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
#include <map>
#include <algorithm>
#include <windows.h>
#include <oleacc.h>
#include <oleidl.h>
#include <mshtml.h>
#include <set>
#include <string>
#include <sstream>
#include <vbufBase/backend.h>
#include <vbufBase/utils.h>
#include <common/log.h>
#include "node.h"
#include "mshtml.h"
using namespace std;
HINSTANCE backendLibHandle=NULL;
UINT WM_HTML_GETOBJECT;
BOOL WINAPI DllMain(HINSTANCE hModule,DWORD reason,LPVOID lpReserved) {
if(reason==DLL_PROCESS_ATTACH) {
_CrtSetReportHookW2(_CRT_RPTHOOK_INSTALL,(_CRT_REPORT_HOOKW)NVDALogCrtReportHook);
backendLibHandle=hModule;
WM_HTML_GETOBJECT=RegisterWindowMessage(L"WM_HTML_GETOBJECT");
}
return TRUE;
}
void incBackendLibRefCount() {
HMODULE h=NULL;
BOOL res=GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,(LPCTSTR)backendLibHandle,&h);
nhAssert(res); //Result of upping backend lib ref count
LOG_DEBUG(L"Increased backend lib ref count");
}
void decBackendLibRefCount() {
BOOL res=FreeLibrary(backendLibHandle);
nhAssert(res); //Result of freeing backend lib
LOG_DEBUG(L"Decreased backend lib ref count");
}
VBufStorage_controlFieldNode_t* MshtmlVBufBackend_t::getDeepestControlFieldNodeForHTMLElement(IHTMLElement* pHTMLElement) {
bool elementNeedsRelease=false;
while(pHTMLElement) {
IHTMLUniqueName* pHTMLUniqueName=NULL;
pHTMLElement->QueryInterface(IID_IHTMLUniqueName,(void**)&pHTMLUniqueName);
if(pHTMLUniqueName) {
int ID=0;
pHTMLUniqueName->get_uniqueNumber((long*)&ID);
pHTMLUniqueName->Release();
if(ID!=0) {
VBufStorage_controlFieldNode_t* node=this->getControlFieldNodeWithIdentifier(this->rootDocHandle,ID);
if(node) {
if(elementNeedsRelease) pHTMLElement->Release();
return node;
} else {
LOG_DEBUG(L"No node for element");
}
} else {
LOG_DEBUG(L"Could not get unique number from IHTMLUniqueName");
}
} else {
LOG_DEBUG(L"Could not queryInterface from IHTMLElement to IHTMLUniqueName");
}
IHTMLElement* parentPHTMLElement=NULL;
pHTMLElement->get_parentElement(&parentPHTMLElement);
if(elementNeedsRelease) pHTMLElement->Release();
pHTMLElement=parentPHTMLElement;
elementNeedsRelease=true;
}
return NULL;
}
/**
* A utility template function to queryService from a given IUnknown to the given service with the given service ID and interface eing returned.
* @param siid the service iid
*/
template<typename toInterface> inline HRESULT queryService(IUnknown* pUnknown, const IID& siid, toInterface** pIface) {
HRESULT hRes;
IServiceProvider* pServProv=NULL;
hRes=pUnknown->QueryInterface(IID_IServiceProvider,(void**)&pServProv);
if(hRes!=S_OK||!pServProv) {
LOG_DEBUG(L"Could not queryInterface to IServiceProvider");
return hRes;
}
hRes=pServProv->QueryService(siid,__uuidof(toInterface),(void**)pIface);
pServProv->Release();
if(hRes!=S_OK||!pIface) {
LOG_DEBUG(L"Could not get requested interface");
*pIface=NULL;
return hRes;
}
return hRes;
}
inline void getIAccessibleInfo(IAccessible* pacc, wstring* name, int* role, wstring* value, int* states, wstring* description, wstring* keyboardShortcut) {
*role=0;
*states=0;
int res=0;
VARIANT varChild;
varChild.vt=VT_I4;
varChild.lVal=0;
BSTR bstrVal=NULL;
if(pacc->get_accName(varChild,&bstrVal)==S_OK&&bstrVal!=NULL) {
name->append(bstrVal);
SysFreeString(bstrVal);
bstrVal=NULL;
} else {
LOG_DEBUG(L"IAccessible::get_accName failed");
}
VARIANT varRole;
VariantInit(&varRole);
res=pacc->get_accRole(varChild,&varRole);
if(res==S_OK&&varRole.vt==VT_I4) {
*role=varRole.lVal;
} else {
LOG_DEBUG(L"Failed to get role");
}
VariantClear(&varRole);
if(pacc->get_accValue(varChild,&bstrVal)==S_OK&&bstrVal!=NULL) {
value->append(bstrVal);
SysFreeString(bstrVal);
bstrVal=NULL;
} else {
LOG_DEBUG(L"IAccessible::get_accValue failed");
}
VARIANT varState;
VariantInit(&varState);
res=pacc->get_accState(varChild,&varState);
if(res==S_OK&&varState.vt==VT_I4) {
*states=varState.lVal;
} else {
LOG_DEBUG(L"Failed to get states");
}
VariantClear(&varState);
if(pacc->get_accDescription(varChild,&bstrVal)==S_OK&&bstrVal!=NULL) {
description->append(bstrVal);
SysFreeString(bstrVal);
bstrVal=NULL;
} else {
LOG_DEBUG(L"IAccessible::get_accDescription failed");
}
if(pacc->get_accKeyboardShortcut(varChild,&bstrVal)==S_OK&&bstrVal!=NULL) {
keyboardShortcut->append(bstrVal);
SysFreeString(bstrVal);
} else {
LOG_DEBUG(L"IAccessible::get_accKeyboardShortcut failed");
}
}
template<typename toInterface> inline HRESULT getHTMLSubdocumentBodyFromIAccessibleFrame(IAccessible* pacc, toInterface** pIface) {
HRESULT hRes=0;
VARIANT varChild;
varChild.vt=VT_I4;
varChild.lVal=1;
IDispatch* pDispatch=NULL;
if((hRes=pacc->get_accChild(varChild,&pDispatch))!=S_OK) {
LOG_DEBUG(L"IAccessible::accChild failed with return code "<<hRes);
return hRes;
}
hRes=queryService(pDispatch,IID_IHTMLElement,pIface);
pDispatch->Release();
return hRes;
}
IHTMLElement* LocateHTMLElementInDocument(IHTMLDocument3* pHTMLDocument3, const wstring& ID) {
HRESULT hRes;
IHTMLElement* pHTMLElement=NULL;
//First try getting the element directly from this document
hRes=pHTMLDocument3->getElementById((wchar_t*)(ID.c_str()),&pHTMLElement);
if(hRes==S_OK&&pHTMLElement) {
return pHTMLElement;
}
//As it was not in this document, we need to search for it in all subdocuments
//If the body is a frameset then we need to search all frames
//If the body is just body, we need to search all iframes
IHTMLDocument2* pHTMLDocument2=NULL;
hRes=pHTMLDocument3->QueryInterface(IID_IHTMLDocument2,(void**)&pHTMLDocument2);
if(hRes!=S_OK||!pHTMLDocument2) {
LOG_DEBUG(L"Could not get IHTMLDocument2");
return NULL;
}
hRes=pHTMLDocument2->get_body(&pHTMLElement);
pHTMLDocument2->Release();
if(hRes!=S_OK||!pHTMLElement) {
LOG_DEBUGWARNING(L"Could not get body element from IHTMLDocument2 at "<<pHTMLDocument2);
return NULL;
}
BSTR tagName=NULL;
hRes=pHTMLElement->get_tagName(&tagName);
wchar_t* embeddingTagName=(tagName&&(wcscmp(tagName,L"FRAMESET"))==0)?L"FRAME":L"IFRAME";
SysFreeString(tagName);
IHTMLElement2* pHTMLElement2=NULL;
hRes=pHTMLElement->QueryInterface(IID_IHTMLElement2,(void**)&pHTMLElement2);
pHTMLElement->Release();
if(hRes!=S_OK||!pHTMLElement2) {
LOG_DEBUG(L"Could not queryInterface to IHTMLElement2");
return NULL;
}
IHTMLElementCollection* pHTMLElementCollection=NULL;
hRes=pHTMLElement2->getElementsByTagName(embeddingTagName,&pHTMLElementCollection);
pHTMLElement2->Release();
if(hRes!=S_OK||!pHTMLElementCollection) {
LOG_DEBUG(L"Could not get collection from getElementsByName");
return NULL;
}
long numElements=0;
hRes=pHTMLElementCollection->get_length(&numElements);
if(hRes!=S_OK) {
LOG_DEBUG(L"Error getting length of collection");
numElements=0;
}
IHTMLElement* pHTMLElementChild=NULL;
for(long index=0;index<numElements;++index) {
IDispatch* pDispatch=NULL;
VARIANT vID;
vID.vt=VT_I4;
vID.lVal=index;
VARIANT vResIndex;
vResIndex.vt=VT_I4;
vResIndex.lVal=0;
hRes=pHTMLElementCollection->item(vID,vResIndex,&pDispatch);
if(hRes!=S_OK||!pDispatch) {
LOG_DEBUG(L"Could not retreave item "<<index<<L" from collection");
continue;
}
IAccessible* pacc=NULL;
queryService(pDispatch,IID_IAccessible,&pacc);
pDispatch->Release();
pDispatch=NULL;
if(!pacc) {
LOG_DEBUG(L"Could not queryService to IAccessible");
continue;
}
IHTMLElement* pHTMLElementSubBody=NULL;
getHTMLSubdocumentBodyFromIAccessibleFrame(pacc,&pHTMLElementSubBody);
pacc->Release();
if(!pHTMLElementSubBody) {
LOG_DEBUG(L"Could not get IHTMLElement body from frame's subdocument");
continue;
}
hRes=pHTMLElementSubBody->get_document(&pDispatch);
pHTMLElementSubBody->Release();
if(hRes!=S_OK||!pDispatch) {
LOG_DEBUG(L"Could not get document from IHTMLElement");
return NULL;
}
IHTMLDocument3* pHTMLDocument3sub=NULL;
hRes=pDispatch->QueryInterface(IID_IHTMLDocument3,(void**)&pHTMLDocument3sub);
pDispatch->Release();
if(hRes!=S_OK||!pHTMLDocument3sub) {
LOG_DEBUG(L"Could not queryInterface to IHTMLDocument3 for document");
continue;
}
pHTMLElementChild=LocateHTMLElementInDocument(pHTMLDocument3sub,ID);
pHTMLDocument3sub->Release();
if(pHTMLElementChild) {
break;
}
}
pHTMLElementCollection->Release();
return pHTMLElementChild;
}
inline int getIDFromHTMLDOMNode(IHTMLDOMNode* pHTMLDOMNode) {
int res;
IHTMLUniqueName* pHTMLUniqueName=NULL;
LOG_DEBUG(L"Try to get IHTMLUniqueName");
if(pHTMLDOMNode->QueryInterface(IID_IHTMLUniqueName,(void**)&pHTMLUniqueName)!=S_OK) {
LOG_DEBUG(L"Failed to get IHTMLUniqueName");
return 0;
}
LOG_DEBUG(L"Got IHTMLUniqueName");
int ID=0;
LOG_DEBUG(L"Getting IHTMLUniqueName::uniqueNumber");
res=pHTMLUniqueName->get_uniqueNumber((long*)&ID);
pHTMLUniqueName->Release();
if(res!=S_OK||!ID) {
LOG_DEBUG(L"Failed to get IHTMLUniqueName::uniqueNumber");
return 0;
}
LOG_DEBUG(L"Got uniqueNumber of "<<ID);
return ID;
}
inline wstring getTextFromHTMLDOMNode(IHTMLDOMNode* pHTMLDOMNode, bool allowPreformattedText, bool isStartOfBlock) {
int res=0;
IHTMLDOMTextNode* pHTMLDOMTextNode=NULL;
LOG_DEBUG(L"Trying to get an IHTMLDOMTextNode interface pointer");
if(pHTMLDOMNode->QueryInterface(IID_IHTMLDOMTextNode,(void**)&pHTMLDOMTextNode)!=S_OK) {
LOG_DEBUG(L"Not a text node");
return L"";
}
LOG_DEBUG(L"Fetch data of DOMTextNode");
BSTR data=NULL;
res=pHTMLDOMTextNode->get_data(&data);
pHTMLDOMTextNode->Release();
if(res!=S_OK||!data) {
LOG_DEBUG(L"Failed to get IHTMLDOMTextNode::data");
return L"";
}
LOG_DEBUG(L"Got data from IHTMLDOMTextNode");
wstring s;
bool notAllWhitespace=false;
if(allowPreformattedText) {
s.append(data);
} else {
bool lastNotWhitespace=false;
bool strippingLeft=isStartOfBlock;
for(wchar_t* c=data;*c;++c) {
if(!iswspace(*c)) {
s+=*c;
lastNotWhitespace=TRUE;
notAllWhitespace=true;
strippingLeft=false;
} else if(lastNotWhitespace||!strippingLeft) {
s+=L' ';
lastNotWhitespace=FALSE;
}
}
}
SysFreeString(data);
if(!allowPreformattedText&&!notAllWhitespace) {
return L"";
}
return s;
}
#define macro_addHTMLCurrentStyleToNodeAttrs(styleName,attrName,node,currentStyleObj,tempBSTR) {\
currentStyleObj->get_##styleName(&tempBSTR);\
if(tempBSTR) {\
LOG_DEBUG(L"Got "<<L#styleName);\
node->addAttribute(L#attrName,tempBSTR);\
SysFreeString(tempBSTR);\
tempBSTR=NULL;\
} else {\
LOG_DEBUG(L"Failed to get "<<L#styleName);\
}\
}
#define macro_addHTMLCurrentStyleToNodeAttrs_var(styleName,attrName,node,currentStyleObj,tempVar) {\
currentStyleObj->get_##styleName(&tempVar);\
if(tempVar.vt==VT_BSTR && tempVar.bstrVal) {\
LOG_DEBUG(L"Got "<<L#styleName);\
node->addAttribute(L#attrName,tempVar.bstrVal);\
VariantClear(&tempVar);\
} else {\
LOG_DEBUG(L"Failed to get "<<L#styleName);\
}\
}
inline void getCurrentStyleInfoFromHTMLDOMNode(IHTMLDOMNode* pHTMLDOMNode, bool& dontRender, bool& isBlock, bool& hidden, wstring& listStyle) {
BSTR tempBSTR=NULL;
IHTMLElement2* pHTMLElement2=NULL;
int res=pHTMLDOMNode->QueryInterface(IID_IHTMLElement2,(void**)&pHTMLElement2);
if(res!=S_OK||!pHTMLElement2) {
LOG_DEBUG(L"Could not get IHTMLElement2");
return;
}
IHTMLCurrentStyle* pHTMLCurrentStyle=NULL;
res=pHTMLElement2->get_currentStyle(&pHTMLCurrentStyle);
pHTMLElement2->Release();
if(res!=S_OK||!pHTMLCurrentStyle) {
LOG_DEBUG(L"Could not get IHTMLCurrentStyle");
return;
}
//get visibility
pHTMLCurrentStyle->get_visibility(&tempBSTR);
if(tempBSTR) {
LOG_DEBUG(L"Got visibility");
hidden=(_wcsicmp(tempBSTR,L"hidden")==0);
SysFreeString(tempBSTR);
tempBSTR=NULL;
} else {
LOG_DEBUG(L"Failed to get visibility");\
}
//get display
pHTMLCurrentStyle->get_display(&tempBSTR);
if(tempBSTR) {
LOG_DEBUG(L"Got display");
if (_wcsicmp(tempBSTR,L"none")==0) {
dontRender=true;
isBlock=false;
}
if (_wcsicmp(tempBSTR,L"inline")==0||_wcsicmp(tempBSTR,L"inline-block")==0) isBlock=false;
SysFreeString(tempBSTR);
tempBSTR=NULL;
} else {
LOG_DEBUG(L"Failed to get display");
}
BSTR _listStyle;
pHTMLCurrentStyle->get_listStyleType(&_listStyle);
if(_listStyle) {
listStyle.append(_listStyle);
SysFreeString(_listStyle);
}
if (pHTMLCurrentStyle) pHTMLCurrentStyle->Release();
}
#define macro_addHTMLAttributeToMap(attribName,allowEmpty,attribsObj,attribsMap,tempVar,tempAttrObj) {\
attribsObj->getNamedItem(attribName,&tempAttrObj);\
if(tempAttrObj) {\
VariantInit(&tempVar);\
tempAttrObj->get_nodeValue(&tempVar);\
if(tempVar.vt==VT_BSTR&&tempVar.bstrVal&&(allowEmpty||SysStringLen(tempVar.bstrVal)>0)) {\
attribsMap[L"HTMLAttrib::" attribName]=tempVar.bstrVal;\
} else if(tempVar.vt==VT_I2||tempVar.vt==VT_I4) {\
wostringstream* s=new wostringstream;\
(*s)<<((tempVar.vt==VT_I2)?tempVar.iVal:tempVar.lVal);\
attribsMap[L"HTMLAttrib::" attribName]=s->str();\
delete s;\
}\
VariantClear(&tempVar);\
tempAttrObj->Release();\
tempAttrObj=NULL;\
}\
}
inline void getAttributesFromHTMLDOMNode(IHTMLDOMNode* pHTMLDOMNode,wstring& nodeName, map<wstring,wstring>& attribsMap) {
int res=0;
IDispatch* pDispatch=NULL;
LOG_DEBUG(L"Getting IHTMLDOMNode::attributes");
if(pHTMLDOMNode->get_attributes(&pDispatch)!=S_OK||!pDispatch) {
LOG_DEBUG(L"pHTMLDOMNode->get_attributes failed");
return;
}
IHTMLAttributeCollection2* pHTMLAttributeCollection2=NULL;
res=pDispatch->QueryInterface(IID_IHTMLAttributeCollection2,(void**)&pHTMLAttributeCollection2);
pDispatch->Release();
if(res!=S_OK) {
LOG_DEBUG(L"Could not get IHTMLAttributesCollection2");
return;
}
IHTMLDOMAttribute* tempAttribNode=NULL;
VARIANT tempVar;
macro_addHTMLAttributeToMap(L"id",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
if(nodeName.compare(L"TABLE")==0) {
macro_addHTMLAttributeToMap(L"summary",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
} else if(nodeName.compare(L"A")==0) {
macro_addHTMLAttributeToMap(L"href",true,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
} else if(nodeName.compare(L"INPUT")==0) {
macro_addHTMLAttributeToMap(L"type",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"value",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
} else if(nodeName.compare(L"TD")==0||nodeName.compare(L"TH")==0) {
macro_addHTMLAttributeToMap(L"headers",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"colspan",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"rowspan",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"scope",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
}
macro_addHTMLAttributeToMap(L"longdesc",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"alt",true,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"title",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"src",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"onclick",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"onmousedown",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"onmouseup",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
//ARIA properties:
macro_addHTMLAttributeToMap(L"role",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"aria-valuenow",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"aria-sort",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"aria-labelledby",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"aria-describedby",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"aria-expanded",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"aria-selected",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"aria-level",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"aria-required",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"aria-dropeffect",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"aria-grabbed",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"aria-invalid",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"aria-multiline",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"aria-label",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"aria-hidden",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"aria-live",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"aria-relevant",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"aria-busy",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
macro_addHTMLAttributeToMap(L"aria-atomic",false,pHTMLAttributeCollection2,attribsMap,tempVar,tempAttribNode);
pHTMLAttributeCollection2->Release();
}
inline void fillTextFormatting_helper(IHTMLElement2* pHTMLElement2, VBufStorage_fieldNode_t* node) {
MshtmlVBufStorage_controlFieldNode_t* parentNode=static_cast<MshtmlVBufStorage_controlFieldNode_t*>(node->getParent());
if(parentNode&&!parentNode->language.empty()) {
node->addAttribute(L"language",parentNode->language);
}
wostringstream s;
s<<(parentNode->formatState);
node->addAttribute(L"formatState",s.str());
IHTMLCurrentStyle* pHTMLCurrentStyle=NULL;
if(pHTMLElement2->get_currentStyle(&pHTMLCurrentStyle)!=S_OK||!pHTMLCurrentStyle) {
LOG_DEBUG(L"Could not get IHTMLCurrentStyle");
return;
}
BSTR tempBSTR=NULL;
macro_addHTMLCurrentStyleToNodeAttrs(textAlign,text-align,node,pHTMLCurrentStyle,tempBSTR);
VARIANT tempVar;
macro_addHTMLCurrentStyleToNodeAttrs_var(fontSize,font-size,node,pHTMLCurrentStyle,tempVar);
macro_addHTMLCurrentStyleToNodeAttrs_var(verticalAlign,text-position,node,pHTMLCurrentStyle,tempVar);
macro_addHTMLCurrentStyleToNodeAttrs(fontFamily,font-family,node,pHTMLCurrentStyle,tempBSTR);
//font style
pHTMLCurrentStyle->get_fontStyle(&tempBSTR);
if(tempBSTR) {
LOG_DEBUG(L"Got fontStyle");
if (_wcsicmp(tempBSTR,L"normal")!=0) {
node->addAttribute((_wcsicmp(tempBSTR,L"oblique")!=0) ? tempBSTR : L"italic", L"1");
}
SysFreeString(tempBSTR);
tempBSTR=NULL;
} else {
LOG_DEBUG(L"Failed to get fontStyle");
}
//font weight
if (pHTMLCurrentStyle->get_fontWeight(&tempVar)==S_OK && tempVar.vt==VT_I4) {
LOG_DEBUG(L"Got fontWeight");
if (tempVar.lVal >=700) {
node->addAttribute(L"bold",L"1");
}
VariantClear(&tempVar);
} else {
LOG_DEBUG(L"Failed to get fontWeight");
}
//textDecoration
pHTMLCurrentStyle->get_textDecoration(&tempBSTR);
if(tempBSTR) {
LOG_DEBUG(L"Got textDecoration");
if (_wcsicmp(tempBSTR,L"none")!=0) {
// textDecoration may contain multiple values separated by spaces.
wchar_t *token, *tokenContext;
token = wcstok_s(tempBSTR, L" ", &tokenContext);
while (token) {
node->addAttribute((_wcsicmp(token,L"line-through")!=0) ? token : L"strikethrough", L"1");
token = wcstok_s(NULL, L" ", &tokenContext);
}
}
SysFreeString(tempBSTR);
tempBSTR=NULL;
} else {
LOG_DEBUG(L"Failed to get textDecoration");
}
pHTMLCurrentStyle->Release();
}
inline void fillTextFormattingForNode(IHTMLDOMNode* pHTMLDOMNode, VBufStorage_fieldNode_t* node) {
IHTMLElement2* pHTMLElement2=NULL;
int res=pHTMLDOMNode->QueryInterface(IID_IHTMLElement2,(void**)&pHTMLElement2);
if(res!=S_OK||!pHTMLElement2) {
LOG_DEBUG(L"Could not get IHTMLElement2");
return;
}
fillTextFormatting_helper(pHTMLElement2,node);
pHTMLElement2->Release();
}
inline void fillTextFormattingForTextNode(VBufStorage_controlFieldNode_t* parentNode, VBufStorage_textFieldNode_t* textNode)
//text nodes don't support IHTMLElement2 interface, so using style information from parent node
{
IHTMLElement2* pHTMLElement2=NULL;
static_cast<MshtmlVBufStorage_controlFieldNode_t*>(parentNode)->pHTMLDOMNode->QueryInterface(IID_IHTMLElement2,(void**)&pHTMLElement2);
if(pHTMLElement2) {
fillTextFormatting_helper(pHTMLElement2,textNode);
pHTMLElement2->Release();
}
}
const int TABLEHEADER_COLUMN = 0x1;
const int TABLEHEADER_ROW = 0x2;
inline void fillExplicitTableHeadersForCell(VBufStorage_controlFieldNode_t& cell, int docHandle, wstring& headersAttr, fillVBuf_tableInfo& tableInfo) {
wostringstream colHeaders, rowHeaders;
// The Headers attribute string is in the form "id id ..."
// Loop through all the ids.
size_t lastPos = headersAttr.length();
size_t startPos = 0;
while (startPos < lastPos) {
// Search for a space, which indicates the end of this id.
size_t endPos = headersAttr.find(L' ', startPos);
if (endPos == wstring::npos)
endPos=lastPos;
// headersAttr[startPos:endPos] is the id of a single header.
// Find the info for the header associated with this id string.
map<wstring, TableHeaderInfo>::const_iterator it = tableInfo.headersInfo.find(headersAttr.substr(startPos, endPos - startPos));
startPos = endPos + 1;
if (it == tableInfo.headersInfo.end())
continue;
if (it->second.type & TABLEHEADER_COLUMN)
colHeaders << docHandle << L"," << it->second.uniqueId << L";";
if (it->second.type & TABLEHEADER_ROW)
rowHeaders<< docHandle << L"," << it->second.uniqueId << L";";
}
if (colHeaders.tellp() > 0)
cell.addAttribute(L"table-columnheadercells", colHeaders.str());
if (rowHeaders.tellp() > 0)
cell.addAttribute(L"table-rowheadercells", rowHeaders.str());
}
/*
* Adjusts the current column number to skip past columns spanned by previous rows,
* decrementing row spans as they are encountered.
*/
inline void handleColsSpannedByPrevRows(fillVBuf_tableInfo& tableInfo) {
for (; ; ++tableInfo.curColumnNumber) {
map<int, int>::iterator it = tableInfo.columnRowSpans.find(tableInfo.curColumnNumber);
if (it == tableInfo.columnRowSpans.end()) {
// This column is not spanned by a previous row.
return;
}
nhAssert(it->second != 0); // 0 row span should never occur.
// This row has been covered, so decrement the row span.
--it->second;
if (it->second == 0)
tableInfo.columnRowSpans.erase(it);
}
nhAssert(false); // Code should never reach this point.
}
inline fillVBuf_tableInfo* fillVBuf_helper_collectAndUpdateTableInfo(VBufStorage_controlFieldNode_t* parentNode, wstring nodeName, int docHandle, int ID, fillVBuf_tableInfo* tableInfo, map<wstring,wstring>& attribsMap) {
map<wstring,wstring>::const_iterator tempIter;
wostringstream tempStringStream;
//Many in-table elements identify a data table
if((nodeName.compare(L"THEAD")==0||nodeName.compare(L"TFOOT")==0||nodeName.compare(L"TH")==0||nodeName.compare(L"CAPTION")==0||nodeName.compare(L"COLGROUP")==0||nodeName.compare(L"ROWGROUP")==0)) {
if(tableInfo) tableInfo->definitData=true;
}
if(nodeName.compare(L"TABLE")==0) {
tableInfo=new fillVBuf_tableInfo;
tableInfo->tableNode=parentNode;
tableInfo->tableID=ID;
tableInfo->curRowNumber=0;
tableInfo->curColumnNumber=0;
tableInfo->definitData=false;
//summary attribute suggests a data table
tempIter=attribsMap.find(L"HTMLAttrib::summary");
if(tempIter!=attribsMap.end()) {
tableInfo->definitData=true;
}
//Collect tableID, and row and column counts
tempStringStream.str(L"");
tempStringStream<<ID;
attribsMap[L"table-id"]=tempStringStream.str();
} else if(tableInfo&&nodeName.compare(L"TR")==0) {
++tableInfo->curRowNumber;
tableInfo->curColumnNumber = 0;
} if(tableInfo&&(nodeName.compare(L"TD")==0||nodeName.compare(L"TH")==0)) {
++tableInfo->curColumnNumber;
handleColsSpannedByPrevRows(*tableInfo);
tempStringStream.str(L"");
tempStringStream<<tableInfo->tableID;
attribsMap[L"table-id"]=tempStringStream.str();
tempStringStream.str(L"");
tempStringStream<<tableInfo->curRowNumber;
attribsMap[L"table-rownumber"]=tempStringStream.str();
int startCol = tableInfo->curColumnNumber;
tempStringStream.str(L"");
tempStringStream<<startCol;
attribsMap[L"table-columnnumber"]=tempStringStream.str();
tempIter=attribsMap.find(L"HTMLAttrib::headers");
if(tempIter!=attribsMap.end()) {
//A cell with the headers attribute is definitly a data table
tableInfo->definitData=true;
//Explicit headers must be recorded later as they may not have been rendered yet.
tableInfo->nodesWithExplicitHeaders.push_back(make_pair(parentNode, tempIter->second));
} else {
map<int, wstring>::const_iterator headersIt;
// Add implicit column headers for this cell.
if ((headersIt = tableInfo->columnHeaders.find(startCol)) != tableInfo->columnHeaders.end())
attribsMap[L"table-columnheadercells"]=headersIt->second;
// Add implicit row headers for this cell.
if ((headersIt = tableInfo->rowHeaders.find(tableInfo->curRowNumber)) != tableInfo->rowHeaders.end())
attribsMap[L"table-rowheadercells"]=headersIt->second;
}
// The last row spanned by this cell.
// This will be updated below if there is a row span.
int endRow = tableInfo->curRowNumber;
tempIter=attribsMap.find(L"HTMLAttrib::colspan");
if(tempIter!=attribsMap.end()) {
attribsMap[L"table-columnsspanned"]=tempIter->second;
tableInfo->curColumnNumber += max(_wtoi(tempIter->second.c_str()) - 1, 0);
}
tempIter=attribsMap.find(L"HTMLAttrib::rowspan");
if(tempIter!=attribsMap.end()) {
attribsMap[L"table-rowsspanned"]=tempIter->second;
// Keep trakc of how many rows after this one are spanned by this cell.
int span = _wtoi(tempIter->second.c_str()) - 1;
if (span > 0) {
// The row span needs to be recorded for each spanned column.
for (int col = startCol; col <= tableInfo->curColumnNumber; ++col)
tableInfo->columnRowSpans[col] = span;
endRow += span;
}
}
if(nodeName.compare(L"TH")==0) {
int headerType = 0;
tempIter=attribsMap.find(L"HTMLAttrib::scope");
if(tempIter!=attribsMap.end()) {
if (wcscmp(tempIter->second.c_str(), L"col") == 0)
headerType = TABLEHEADER_COLUMN;
else if (wcscmp(tempIter->second.c_str(), L"row") == 0)
headerType = TABLEHEADER_ROW;
else if (wcscmp(tempIter->second.c_str(), L"Both") == 0)
headerType = TABLEHEADER_COLUMN | TABLEHEADER_ROW;
}
if (!headerType) {
if(tableInfo->curColumnNumber==1) headerType=TABLEHEADER_ROW;
if(tableInfo->curRowNumber==1) headerType|=TABLEHEADER_COLUMN;
}
if (headerType & TABLEHEADER_COLUMN) {
// Record this as a column header for each spanned column.
tempStringStream.str(L"");
tempStringStream << docHandle << L"," << ID << L";";
for (int col = startCol; col <= tableInfo->curColumnNumber; ++col)
tableInfo->columnHeaders[col] += tempStringStream.str();
}
if (headerType & TABLEHEADER_ROW) {
// Record this as a row header for each spanned row.
tempStringStream.str(L"");
tempStringStream << docHandle << L"," << ID << L";";
for (int row = tableInfo->curRowNumber; row <= endRow; ++row)
tableInfo->rowHeaders[row] += tempStringStream.str();
}
tempIter=attribsMap.find(L"HTMLAttrib::id");
if(tempIter!=attribsMap.end()) {
// Record the id string and associated header info for use when handling explicitly defined headers.
TableHeaderInfo& headerInfo = tableInfo->headersInfo[tempIter->second];
headerInfo.uniqueId = ID;
headerInfo.type = headerType;
}
}
}
return tableInfo;
}
const unsigned int FORMATSTATE_INSERTED=1;
const unsigned int FORMATSTATE_DELETED=2;
const unsigned int FORMATSTATE_MARKED=4;
const unsigned int FORMATSTATE_STRONG=8;
const unsigned int FORMATSTATE_EMPH=16;
VBufStorage_fieldNode_t* MshtmlVBufBackend_t::fillVBuf(VBufStorage_buffer_t* buffer, VBufStorage_controlFieldNode_t* parentNode, VBufStorage_fieldNode_t* previousNode, VBufStorage_controlFieldNode_t* oldNode, IHTMLDOMNode* pHTMLDOMNode, int docHandle, fillVBuf_tableInfo* tableInfo, int* LIIndexPtr, bool ignoreInteractiveUnlabelledGraphics, bool allowPreformattedText, bool shouldSkipText, bool inNewSubtree,set<VBufStorage_controlFieldNode_t*>& atomicNodes) {
BSTR tempBSTR=NULL;
wostringstream tempStringStream;
//Handle text nodes
if(!shouldSkipText) {
wstring s=getTextFromHTMLDOMNode(pHTMLDOMNode,allowPreformattedText,(parentNode&&parentNode->isBlock&&!previousNode));
if(!s.empty()) {
LOG_DEBUG(L"Got text from node");
VBufStorage_textFieldNode_t* textNode=buffer->addTextFieldNode(parentNode,previousNode,s);
fillTextFormattingForTextNode(parentNode,textNode);
return textNode;
}
}
//Get node's ID
int ID=getIDFromHTMLDOMNode(pHTMLDOMNode);
if(ID==0) {
LOG_DEBUG(L"Could not get ID");
return NULL;
}
if(buffer->getControlFieldNodeWithIdentifier(docHandle,ID)!=NULL) {
LOG_DEBUG(L"Node already exists with docHandle "<<docHandle<<L" and ID "<<ID<<L", not adding to buffer");
return NULL;
}
//Find out block and visibility style
bool dontRender=false;
bool hidden=false;
bool isBlock=true;
wstring listStyle;
getCurrentStyleInfoFromHTMLDOMNode(pHTMLDOMNode, dontRender, isBlock,hidden,listStyle);
// #4031: nodes hidden due to style (not role="presentation") should have their direct text nodes skipped
if(hidden) shouldSkipText=true;
LOG_DEBUG(L"Trying to get IHTMLDOMNode::nodeName");
if(pHTMLDOMNode->get_nodeName(&tempBSTR)!=S_OK||!tempBSTR) {
LOG_DEBUG(L"Failed to get IHTMLDOMNode::nodeName");
return NULL;
}
wstring nodeName=tempBSTR;
SysFreeString(tempBSTR);
tempBSTR=NULL;
for(wstring::iterator i=nodeName.begin();i!=nodeName.end();++i)
*i=towupper(*i);
LOG_DEBUG(L"Got IHTMLDOMNode::nodeName of "<<nodeName);
//We can safely ignore script and comment tags
if(nodeName.compare(L"#COMMENT")==0||nodeName.compare(L"SCRIPT")==0) {
LOG_DEBUG(L"nodeName not supported");
return NULL;
}
//We only allow linebreaks for 'PRE' tags
if(nodeName.compare(L"PRE")==0) {
allowPreformattedText=TRUE;
}
map<wstring,wstring> attribsMap;
map<wstring,wstring>::const_iterator tempIter;
attribsMap[L"IHTMLDOMNode::nodeName"]=nodeName;
//Collect needed HTML attributes
getAttributesFromHTMLDOMNode(pHTMLDOMNode,nodeName,attribsMap);
//Find out if this node is editable
BOOL isEditable=false;
IHTMLElement3* pHTMLElement3=NULL;
if(pHTMLDOMNode->QueryInterface(IID_IHTMLElement3,(void**)&pHTMLElement3)==S_OK) {
VARIANT_BOOL varEditable=VARIANT_FALSE;
pHTMLElement3->get_isContentEditable(&varEditable);
isEditable=(varEditable==VARIANT_TRUE);
if(isEditable) attribsMap[L"IHTMLElement::isContentEditable"]=L"1";
}
if((tempIter=attribsMap.find(L"HTMLAttrib::aria-hidden"))!=attribsMap.end()&&tempIter->second==L"true") {
// aria-hidden
dontRender=true;
}
//input nodes of type hidden must be treeted as being dontRender.
if(!dontRender&&nodeName.compare(L"INPUT")==0) {
tempIter=attribsMap.find(L"HTMLAttrib::type");
if(tempIter!=attribsMap.end()&&tempIter->second.compare(L"hidden")==0) {
dontRender=true;
}
}
//Find out the language
wstring language=L"";
//Try getting it from this DOMNode,
//Else if this is the root of our buffer, then keep going up the actual DOM
//E.g. will hit HTML tag etc
IHTMLDOMNode* pHTMLDOMNodeTemp=pHTMLDOMNode;
pHTMLDOMNodeTemp->AddRef();
while(pHTMLDOMNodeTemp) {
IHTMLElement* pHTMLElement=NULL;
if(pHTMLDOMNodeTemp->QueryInterface(IID_IHTMLElement,(void**)&pHTMLElement)==S_OK&&pHTMLElement) {
VARIANT v;
if(pHTMLElement->getAttribute(L"lang",2,&v)==S_OK) {
if(v.vt==VT_BSTR&&v.bstrVal) {
language=v.bstrVal;
}
VariantClear(&v);
}
pHTMLElement->Release();
}
if(!parentNode&&language.empty()) {
IHTMLDOMNode* pHTMLDOMNodeTempParent=NULL;
if(pHTMLDOMNodeTemp->get_parentNode(&pHTMLDOMNodeTempParent)==S_OK&&pHTMLDOMNodeTempParent) {
pHTMLDOMNodeTemp->Release();
pHTMLDOMNodeTemp=pHTMLDOMNodeTempParent;
continue;
}
}
pHTMLDOMNodeTemp->Release();
pHTMLDOMNodeTemp=NULL;
}
if(parentNode&&language.empty()) {
language=static_cast<MshtmlVBufStorage_controlFieldNode_t*>(parentNode)->language;
}
// get parent's formatState
unsigned int formatState=0;
if(parentNode) {
formatState=((MshtmlVBufStorage_controlFieldNode_t*)parentNode)->formatState;
} else if(oldNode&&oldNode->getParent()) {
formatState=((MshtmlVBufStorage_controlFieldNode_t*)(oldNode->getParent()))->formatState;
}
if(!(formatState&FORMATSTATE_INSERTED)&&nodeName.compare(L"INS")==0) {
formatState|=FORMATSTATE_INSERTED;
}
if(!(formatState&FORMATSTATE_DELETED)&&nodeName.compare(L"DEL")==0) {
formatState|=FORMATSTATE_DELETED;
}
if(!(formatState&FORMATSTATE_MARKED)&&nodeName.compare(L"MARK")==0) {
formatState|=FORMATSTATE_MARKED;
}
if(!(formatState&FORMATSTATE_STRONG)&&nodeName.compare(L"STRONG")==0) {
formatState|=FORMATSTATE_STRONG;
}
if(!(formatState&FORMATSTATE_EMPH)&&nodeName.compare(L"EM")==0) {
formatState|=FORMATSTATE_EMPH;
}
bool isDocRoot=!parentNode&&(!oldNode||!oldNode->getParent());
VBufStorage_controlFieldNode_t* node=new MshtmlVBufStorage_controlFieldNode_t(docHandle,ID,isBlock,this,isDocRoot,pHTMLDOMNode,language);
((MshtmlVBufStorage_controlFieldNode_t*)node)->formatState=formatState;
((MshtmlVBufStorage_controlFieldNode_t*)node)->preProcessLiveRegion((MshtmlVBufStorage_controlFieldNode_t*)(oldNode?oldNode->getParent():parentNode),attribsMap);
bool wasInNewSubtree=inNewSubtree;
if(!wasInNewSubtree&&!oldNode) {
oldNode=this->getControlFieldNodeWithIdentifier(docHandle,ID);
if(oldNode&&oldNode->isHidden) oldNode=NULL;
inNewSubtree=!oldNode;
}
//Add the node to the buffer
parentNode=buffer->addControlFieldNode(parentNode,previousNode,node);
nhAssert(parentNode);
previousNode=NULL;
//All inner parts of a table (rows, cells etc) if they are changed must re-render the entire table.
//This must be done even for nodes with display:none.
if(tableInfo&&(nodeName.compare(L"THEAD")==0||nodeName.compare(L"TBODY")==0||nodeName.compare(L"TFOOT")==0||nodeName.compare(L"TR")==0||nodeName.compare(L"TH")==0||nodeName.compare(L"TD")==0)) {
parentNode->updateAncestor=tableInfo->tableNode;
}
//We do not want to render any content for dontRender nodes
if(dontRender) {
parentNode->isHidden=true;
return parentNode;
}
//Collect available IAccessible information
wstring IAName=L"";
int IARole=0;
wstring IAValue=L"";
int IAStates=0;
wstring IADescription=L"";
wstring IAKeyboardShortcut=L"";
IAccessible* pacc=NULL;
queryService(pHTMLDOMNode,IID_IAccessible,&pacc);
if(pacc) {
getIAccessibleInfo(pacc,&IAName,&IARole,&IAValue,&IAStates,&IADescription,&IAKeyboardShortcut);
}
//IE incorrectly places ROLE_SYSTEM_TEXT and no readonly state on unsupported or future tags that have an explicit ARIA role
//If this is the case then set the IARole to staticText so we don't class it as editable text.
if(IARole==ROLE_SYSTEM_TEXT&&!(IAStates&STATE_SYSTEM_READONLY)) {
tempIter=attribsMap.find(L"HTMLAttrib::role");
if(tempIter!=attribsMap.end()&&tempIter->second.compare(L"textbox")!=0) {
IARole=ROLE_SYSTEM_STATICTEXT;
}
}
//IE sometimes sets the readonly state on editable nodes, this does not make sence
if(isEditable&&IAStates&STATE_SYSTEM_READONLY) {
IAStates-=STATE_SYSTEM_READONLY;
}
wstring ariaRole;
tempIter=attribsMap.find(L"HTMLAttrib::role");
if(tempIter!=attribsMap.end()) {
ariaRole=tempIter->second;
if(ariaRole.compare(L"description")==0||ariaRole.compare(L"search")==0) {
//IE gives elements with an ARIA role of description and search a role of edit, probably should be staticText
IARole=ROLE_SYSTEM_STATICTEXT;
} else if(ariaRole.compare(L"list")==0) {
IARole=ROLE_SYSTEM_LIST;
IAStates|=STATE_SYSTEM_READONLY;
} else if(ariaRole.compare(L"slider")==0) {
IARole=ROLE_SYSTEM_SLIDER;
IAStates|=STATE_SYSTEM_FOCUSABLE;
tempIter=attribsMap.find(L"aria-valuenow");
if(tempIter!=attribsMap.end()) {
IAValue=tempIter->second;
}
} else if(ariaRole.compare(L"progressbar")==0) {
IARole=ROLE_SYSTEM_PROGRESSBAR;
tempIter=attribsMap.find(L"aria-valuenow");
if(tempIter!=attribsMap.end()) {
IAValue=tempIter->second;
}
} else if(ariaRole.compare(L"application")==0) {
IARole=ROLE_SYSTEM_APPLICATION;
} else if(ariaRole.compare(L"button")==0) {
//IE does not override accRole for links with an ARIA role of button (#2750)
IARole=ROLE_SYSTEM_PUSHBUTTON;
} else if(ariaRole.compare(L"dialog")==0) {
IARole=ROLE_SYSTEM_DIALOG;
} else if(!hidden&&ariaRole.compare(L"presentation")==0) {
hidden=true;
}
}
//IE doesn't seem to support aria-label yet so we want to override IAName with it
tempIter=attribsMap.find(L"HTMLAttrib::aria-label");
if(tempIter!=attribsMap.end()) {
IAName=tempIter->second;
}
//IE exposes state_linked for anchors with no href, this is wrong
if((nodeName.compare(L"A")==0)&&(attribsMap.find(L"HTMLAttrib::href")==attribsMap.end())) {
if(IAStates&STATE_SYSTEM_LINKED) IAStates-=STATE_SYSTEM_LINKED;
if(IAStates&STATE_SYSTEM_FOCUSABLE) IAStates-=STATE_SYSTEM_FOCUSABLE;
}
// Whether this is the root node.
bool isRoot=ID==this->rootID;
//Is this node interactive?
bool isInteractive=isEditable||(!isRoot&&IAStates&STATE_SYSTEM_FOCUSABLE&&nodeName!=L"BODY"&&nodeName!=L"IFRAME")||(IAStates&STATE_SYSTEM_LINKED)||(attribsMap.find(L"HTMLAttrib::onclick")!=attribsMap.end())||(attribsMap.find(L"HTMLAttrib::onmouseup")!=attribsMap.end())||(attribsMap.find(L"HTMLAttrib::onmousedown")!=attribsMap.end())||(attribsMap.find(L"HTMLAttrib::longdesc")!=attribsMap.end());
//Set up numbering for lists
int LIIndex=0;
if(nodeName.compare(L"OL")==0||nodeName.compare(L"UL")==0) {
//Ordered lists should number their list items
LIIndex=1;
LIIndexPtr=&LIIndex;
} else if(nodeName.compare(L"DL")==0) {
//Unordered lists should not be numbered
LIIndexPtr=NULL;
}
parentNode->isHidden=hidden;
if(!hidden) {
//Collect and update table information
tableInfo=fillVBuf_helper_collectAndUpdateTableInfo(parentNode, nodeName, docHandle,ID, tableInfo, attribsMap);
}
// Whether the name is the content of this node.
bool nameIsContent = (IARole == ROLE_SYSTEM_LINK || IARole == ROLE_SYSTEM_PUSHBUTTON || IARole == ROLE_SYSTEM_MENUITEM || IARole == ROLE_SYSTEM_GRAPHIC || IARole == ROLE_SYSTEM_PAGETAB
|| ariaRole == L"heading" || (nodeName[0] == L'H' && iswdigit(nodeName[1]))
|| nodeName == L"OBJECT" || nodeName == L"APPLET" || IARole == ROLE_SYSTEM_APPLICATION || IARole == ROLE_SYSTEM_DIALOG);
// True if the name definitely came from the author.
bool nameFromAuthor=false;
//Add opening quote for <Q> elements
if(nodeName.compare(L"Q")==0) {
VBufStorage_textFieldNode_t* textNode=buffer->addTextFieldNode(parentNode,previousNode,L"\x201c");
fillTextFormattingForNode(pHTMLDOMNode,textNode);
previousNode=textNode;
}
//Generate content for nodes
wstring contentString=L"";
bool renderChildren=false;
if (nameIsContent && (attribsMap.find(L"HTMLAttrib::aria-label") != attribsMap.end() || attribsMap.find(L"HTMLAttrib::aria-labelledby") != attribsMap.end())) {
// Explicitly override any content with aria-label(ledby).
contentString = IAName;
} else if (nodeName.compare(L"HR")==0) {
contentString=L" ";
isBlock=true;
IARole=ROLE_SYSTEM_SEPARATOR;
} else if(IARole==ROLE_SYSTEM_SLIDER||IARole==ROLE_SYSTEM_PROGRESSBAR) {
contentString=IAValue;
} else if ((nodeName.compare(L"OBJECT")==0 || nodeName.compare(L"APPLET")==0)) {
isBlock=true;
contentString=L" ";
} else if(nodeName.compare(L"LI")==0) {
renderChildren=true;
if(listStyle.compare(L"disc")==0||listStyle.compare(L"circle")==0||listStyle.compare(L"square")==0) {
tempStringStream.str(L"");
tempStringStream<<L"\x2022 "; //Bullet
contentString=tempStringStream.str();
} else if(LIIndexPtr!=NULL&&!listStyle.empty()&&listStyle.compare(L"none")!=0) {
tempStringStream.str(L"");
tempStringStream<<*LIIndexPtr<<L". ";
contentString=tempStringStream.str();
++(*LIIndexPtr);
}
} else if(nodeName.compare(L"TABLE")==0) {
renderChildren=true;
tempIter=attribsMap.find(L"HTMLAttrib::summary");
if(tempIter!=attribsMap.end()) {
contentString=tempIter->second;
}
} else if(nodeName.compare(L"IMG")==0) {
if ((tempIter = attribsMap.find(L"HTMLAttrib::alt")) != attribsMap.end()) {
if (tempIter->second.empty()) {
// alt="", so don't render this at all.
isInteractive = false;
} else {
// There is alt text, so use it.
contentString = tempIter->second;
}
} else if ((tempIter = attribsMap.find(L"HTMLAttrib::title")) != attribsMap.end()) {
// There is a title, so use it.
contentString = tempIter->second;
} else if (ignoreInteractiveUnlabelledGraphics)
isInteractive = false;
else if (isInteractive && !IAValue.empty()) {
// The graphic is unlabelled, but we should try to derive a name for it.
contentString=getNameForURL(IAValue);
}
} else if(nodeName.compare(L"INPUT")==0) {
tempIter=attribsMap.find(L"HTMLAttrib::type");
if(tempIter!=attribsMap.end()&&tempIter->second.compare(L"file")==0) {
contentString=IAValue;
contentString.append(L"...");
IARole=ROLE_SYSTEM_PUSHBUTTON;
} else if(IARole==ROLE_SYSTEM_TEXT) {
//Sometimes IAccessible::accValue can fail on protected fields, so fall back to value attribute if it exists.
tempIter=attribsMap.find(L"HTMLAttrib::value");
if(IAValue.empty()&&tempIter!=attribsMap.end()) {
contentString=tempIter->second;
} else {
contentString=IAValue;
}
if(IAStates&STATE_SYSTEM_PROTECTED) {
fill(contentString.begin(),contentString.end(),L'*');
}
nameFromAuthor=true;
} else if(IARole==ROLE_SYSTEM_PUSHBUTTON) {
contentString=IAName;
} else if(IARole==ROLE_SYSTEM_RADIOBUTTON||IARole==ROLE_SYSTEM_CHECKBUTTON) {
nameFromAuthor=true;
}
if(contentString.empty()) {
contentString=L" ";
}
} else if(nodeName.compare(L"SELECT")==0) {
if(!IAValue.empty()) {
contentString=IAValue;
} else {
contentString=L" ";
}
nameFromAuthor=true;
} else if(nodeName.compare(L"TEXTAREA")==0) {
isBlock=true;
if(!IAValue.empty()) {
contentString=IAValue;
} else {
contentString=L" ";
}
nameFromAuthor=true;
} else if(nodeName.compare(L"BR")==0) {
LOG_DEBUG(L"node is a br tag, adding a line feed as its text.");
contentString=L"\n";
} else if((!isRoot&&(IARole==ROLE_SYSTEM_APPLICATION||IARole==ROLE_SYSTEM_DIALOG))
||IARole==ROLE_SYSTEM_OUTLINE
||nodeName.compare(L"MATH")==0
) {
contentString=L" ";
} else {
renderChildren=true;
}
//If the name isn't being rendered as the content, add the name as a field attribute
// if it came from the author (not content).
if (!nameIsContent && !IAName.empty() && (nameFromAuthor || (
attribsMap.find(L"HTMLAttrib::aria-label") != attribsMap.end() || attribsMap.find(L"HTMLAttrib::aria-labelledby") != attribsMap.end()
|| attribsMap.find(L"HTMLAttrib::title") != attribsMap.end() || attribsMap.find(L"HTMLAttrib::alt") != attribsMap.end()
)))
attribsMap[L"name"]=IAName;
//Add a textNode to the buffer containing any special content retreaved
if(!hidden&&!contentString.empty()) {
previousNode=buffer->addTextFieldNode(parentNode,previousNode,contentString);
fillTextFormattingForNode(pHTMLDOMNode,previousNode);
}
//record IAccessible information as attributes
if(!IAKeyboardShortcut.empty()) {
attribsMap[L"keyboardShortcut"]=IAKeyboardShortcut;
}
tempStringStream.str(L"");
tempStringStream<<IARole;
attribsMap[L"IAccessible::role"]=tempStringStream.str();
for(int i=0;i<32;++i) {
int state=1<<i;
if(state&IAStates) {
tempStringStream.str(L"");
tempStringStream<<L"IAccessible::state_"<<state;
attribsMap[tempStringStream.str()]=L"1";
}
}
//Render content of children if we are allowed to
if(renderChildren) {
if (isInteractive && !ignoreInteractiveUnlabelledGraphics) {
// Don't render interactive unlabelled graphic descendants if this node has a name,
// as author supplied names are preferred.
ignoreInteractiveUnlabelledGraphics = !IAName.empty();
}
//For children of frames we must get the child document via IAccessible
if(nodeName.compare(L"FRAME")==0||nodeName.compare(L"IFRAME")==0) {
LOG_DEBUG(L"using getRoodDOMNodeOfHTMLFrame to get the frame's child");
if(pacc) {
IHTMLDOMNode* childPHTMLDOMNode=NULL;
getHTMLSubdocumentBodyFromIAccessibleFrame(pacc,&childPHTMLDOMNode);
if(childPHTMLDOMNode) {
previousNode=this->fillVBuf(buffer,parentNode,previousNode,NULL,childPHTMLDOMNode,docHandle,tableInfo,LIIndexPtr,ignoreInteractiveUnlabelledGraphics,allowPreformattedText,shouldSkipText,inNewSubtree,atomicNodes);
childPHTMLDOMNode->Release();
}
}
} else { //use childNodes
IHTMLDOMChildrenCollection* pHTMLDOMChildrenCollection=NULL;
LOG_DEBUG(L"Getting IHTMLDOMNode::childNodes");
IDispatch* pDispatch=NULL;
if(pHTMLDOMNode->get_childNodes(&pDispatch)==S_OK) {
IHTMLDOMChildrenCollection* pHTMLDOMChildrenCollection=NULL;
if(pDispatch->QueryInterface(IID_IHTMLDOMChildrenCollection,(void**)&pHTMLDOMChildrenCollection)==S_OK) {
LOG_DEBUG(L"Got IHTMLDOMNode::childNodes");
LOG_DEBUG(L"Getting IHTMLDOMChildrenCollection::length");
long length=0;
pHTMLDOMChildrenCollection->get_length(&length);
LOG_DEBUG(L"length "<<length);
for(int i=0;i<length;++i) {
LOG_DEBUG(L"Fetching child "<<i);
IDispatch* childPDispatch=NULL;
if(pHTMLDOMChildrenCollection->item(i,&childPDispatch)!=S_OK) {
continue;
}
IHTMLDOMNode* childPHTMLDOMNode=NULL;
if(childPDispatch->QueryInterface(IID_IHTMLDOMNode,(void**)&childPHTMLDOMNode)==S_OK) {
VBufStorage_fieldNode_t* tempNode=this->fillVBuf(buffer,parentNode,previousNode,NULL,childPHTMLDOMNode,docHandle,tableInfo,LIIndexPtr,ignoreInteractiveUnlabelledGraphics,allowPreformattedText,shouldSkipText,inNewSubtree,atomicNodes);
if(tempNode) {
previousNode=tempNode;
}
childPHTMLDOMNode->Release();
}
childPDispatch->Release();
}
pHTMLDOMChildrenCollection->Release();
}
pDispatch->Release();
}
}
//A node who's rendered children produces no content, or only a small amount of whitespace should render its title or URL
if(!hidden&&!nodeHasUsefulContent(parentNode)) {
contentString=L"";
if(!IAName.empty()) {
contentString=IAName;
}
if(contentString.empty()) {
tempIter=attribsMap.find(L"HTMLAttrib::title");
if(tempIter!=attribsMap.end()) {
contentString=tempIter->second;
}
}
if(contentString.empty()) {
tempIter=attribsMap.find(L"HTMLAttrib::href");
if(tempIter!=attribsMap.end()&&!tempIter->second.empty()) {
contentString=getNameForURL(tempIter->second);
}
}
if(!contentString.empty()) {
previousNode=buffer->addTextFieldNode(parentNode,NULL,contentString);
fillTextFormattingForNode(pHTMLDOMNode,previousNode);
}
}
}
//We no longer need the IAccessible
if(pacc) {
pacc->Release();
pacc=NULL;
}
//Update attributes with table info
if(!hidden&&nodeName.compare(L"TABLE")==0) {
nhAssert(tableInfo);
if(!tableInfo->definitData) {
attribsMap[L"table-layout"]=L"1";
}
for (list<pair<VBufStorage_controlFieldNode_t*, wstring>>::iterator it = tableInfo->nodesWithExplicitHeaders.begin(); it != tableInfo->nodesWithExplicitHeaders.end(); ++it)
fillExplicitTableHeadersForCell(*it->first, docHandle, it->second, *tableInfo);
wostringstream s;
s << tableInfo->curRowNumber;
parentNode->addAttribute(L"table-rowcount", s.str());
s.str(L"");
s << tableInfo->curColumnNumber;
parentNode->addAttribute(L"table-columncount", s.str());
delete tableInfo;
tableInfo=NULL;
}
if(!hidden) {
//Table cells should always be represented by at least a space, but if a space, then they should not be block.
if((nodeName.compare(L"TD")==0||nodeName.compare(L"TH")==0)) {
if(parentNode->getLength()==0) {
isBlock=false;
buffer->addTextFieldNode(parentNode,previousNode,L" ");
}
}
//If a node is interactive, and still has no content, add a space
if(isInteractive&&parentNode->getLength()==0) {
buffer->addTextFieldNode(parentNode,previousNode,L" ");
}
}
//Update block setting on node
parentNode->isBlock=isBlock;
//Add all the collected attributes to the node
for(tempIter=attribsMap.begin();tempIter!=attribsMap.end();++tempIter) {
parentNode->addAttribute(tempIter->first,tempIter->second);
}
// Closing quote for <Q> elements
if(nodeName.compare(L"Q")==0) {
VBufStorage_textFieldNode_t* textNode=buffer->addTextFieldNode(parentNode,previousNode,L"\x201d");
fillTextFormattingForNode(pHTMLDOMNode,textNode);
}
// Report any live region update for this node
if(!wasInNewSubtree&&!hidden) {
((MshtmlVBufStorage_controlFieldNode_t*)parentNode)->postProcessLiveRegion(oldNode,atomicNodes);
auto i=atomicNodes.find(parentNode);
if(i!=atomicNodes.end()) {
((MshtmlVBufStorage_controlFieldNode_t*)(*i))->reportLiveAddition();
atomicNodes.erase(i);
}
}
return parentNode;
}
void MshtmlVBufBackend_t::render(VBufStorage_buffer_t* buffer, int docHandle, int ID, VBufStorage_controlFieldNode_t* oldNode) {
LOG_DEBUG(L"Rendering from docHandle "<<docHandle<<L", ID "<<ID<<L", in to buffer at "<<buffer);
LOG_DEBUG(L"Getting document from window "<<docHandle);
LRESULT res=SendMessage((HWND)UlongToHandle(docHandle),WM_HTML_GETOBJECT,0,0);
if(res==0) {
LOG_DEBUG(L"Error getting document using WM_HTML_GETOBJECT");
return;
}
IHTMLDOMNode* pHTMLDOMNode=NULL;
if(oldNode!=NULL) {
pHTMLDOMNode=(static_cast<MshtmlVBufStorage_controlFieldNode_t*>(oldNode))->pHTMLDOMNode;
nhAssert(pHTMLDOMNode);
pHTMLDOMNode->AddRef();
} else {
IHTMLDocument3* pHTMLDocument3=NULL;
if(ObjectFromLresult(res,IID_IHTMLDocument3,0,(void**)&pHTMLDocument3)!=S_OK) {
LOG_DEBUG(L"Error in ObjectFromLresult");
return;
}
LOG_DEBUG(L"Locating DOM node with ID");
wostringstream s;
s<<L"ms__id"<<ID;
IHTMLElement* pHTMLElement=LocateHTMLElementInDocument(pHTMLDocument3,s.str());
pHTMLDocument3->Release();
if(!pHTMLElement) {
LOG_DEBUG(L"Could not locate HTML element in document");
return;
}
LOG_DEBUG(L"queryInterface to IHTMLDOMNode from IHTMLElement");
if(pHTMLElement->QueryInterface(IID_IHTMLDOMNode,(void**)&pHTMLDOMNode)!=S_OK) {
LOG_DEBUG(L"Could not get IHTMLDOMNode");
pHTMLElement->Release();
return;
}
pHTMLElement->Release();
}
nhAssert(pHTMLDOMNode);
set<VBufStorage_controlFieldNode_t*> atomicNodes;
this->fillVBuf(buffer,NULL,NULL,oldNode,pHTMLDOMNode,docHandle,NULL,NULL,false,false,false,false,atomicNodes);
for(auto& i: atomicNodes) {
((MshtmlVBufStorage_controlFieldNode_t*)i)->reportLiveAddition();
}
pHTMLDOMNode->Release();
}
MshtmlVBufBackend_t::MshtmlVBufBackend_t(int docHandle, int ID): VBufBackend_t(docHandle,ID) {
LOG_DEBUG(L"Mshtml backend constructor");
}
MshtmlVBufBackend_t::~MshtmlVBufBackend_t() {
LOG_DEBUG(L"Mshtml backend destructor");
}
extern "C" __declspec(dllexport) VBufBackend_t* VBufBackend_create(int docHandle, int ID) {
VBufBackend_t* backend=new MshtmlVBufBackend_t(docHandle,ID);
LOG_DEBUG(L"Created new backend at "<<backend);
return backend;
}