/* 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 #include #include #include #include #include #include #include #include #include #include #include #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 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 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 "<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 "<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;indexitem(vID,vResIndex,&pDispatch); if(hRes!=S_OK||!pDispatch) { LOG_DEBUG(L"Could not retreave item "<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 "<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 "<addAttribute(L#attrName,tempBSTR);\ SysFreeString(tempBSTR);\ tempBSTR=NULL;\ } else {\ LOG_DEBUG(L"Failed to get "<get_##styleName(&tempVar);\ if(tempVar.vt==VT_BSTR && tempVar.bstrVal) {\ LOG_DEBUG(L"Got "<addAttribute(L#attrName,tempVar.bstrVal);\ VariantClear(&tempVar);\ } else {\ LOG_DEBUG(L"Failed to get "<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& 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(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(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::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::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& attribsMap) { map::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<curRowNumber; tableInfo->curColumnNumber = 0; } if(tableInfo&&(nodeName.compare(L"TD")==0||nodeName.compare(L"TH")==0)) { ++tableInfo->curColumnNumber; handleColsSpannedByPrevRows(*tableInfo); tempStringStream.str(L""); tempStringStream<tableID; attribsMap[L"table-id"]=tempStringStream.str(); tempStringStream.str(L""); tempStringStream<curRowNumber; attribsMap[L"table-rownumber"]=tempStringStream.str(); int startCol = tableInfo->curColumnNumber; tempStringStream.str(L""); tempStringStream<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::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& 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 "<