📄 html.cpp
字号:
return;}CNcbiOstream& CHTMLInlineElement::PrintEnd(CNcbiOstream& out, TMode mode){ if ( mode != ePlainText ) { out << "</" << m_Name << '>'; CHECK_STREAM_WRITE(out); } return out;}CHTMLElement::~CHTMLElement(void){ return;}CNcbiOstream& CHTMLElement::PrintEnd(CNcbiOstream& out, TMode mode){ CParent::PrintEnd(out, mode); if ( mode != ePlainText ) { const TMode* previous = mode.GetPreviousContext(); if ( previous ) { CNCBINode* parent = previous->GetNode(); if ( parent && parent->HaveChildren() && parent->Children().size() > 1 ) out << '\n'; // separate child nodes by newline } else { out << '\n'; } } CHECK_STREAM_WRITE(out); return out;}CHTMLBlockElement::~CHTMLBlockElement(void){ return;}CNcbiOstream& CHTMLBlockElement::PrintEnd(CNcbiOstream& out, TMode mode){ CParent::PrintEnd(out, mode); if ( mode == ePlainText ) { // Add a newline iff no node on the path to the last descendant // is also a block element. We only need one break. CNCBINode* node = this; while (node->HaveChildren()) { node = node->Children().back(); if (dynamic_cast<CHTMLBlockElement*>(node)) { return out; } } out << '\n'; CHECK_STREAM_WRITE(out); } return out;}// HTML comment.const char CHTMLComment::sm_TagName[] = "comment";CHTMLComment::~CHTMLComment(void){ return;}CNcbiOstream& CHTMLComment::Print(CNcbiOstream& out, TMode mode){ if (mode == ePlainText) { return out; } return CParent::Print(out, mode);}CNcbiOstream& CHTMLComment::PrintBegin(CNcbiOstream& out, TMode mode){ if (mode == eHTML) { out << "<!--"; CHECK_STREAM_WRITE(out); } return out;}CNcbiOstream& CHTMLComment::PrintEnd(CNcbiOstream& out, TMode mode){ if (mode == eHTML) { return out << "-->"; } CHECK_STREAM_WRITE(out); return out;}CHTMLListElement::~CHTMLListElement(void){ return;}CHTMLListElement* CHTMLListElement::SetType(const char* type){ SetAttribute("type", type); return this;}CHTMLListElement* CHTMLListElement::SetType(const string& type){ SetAttribute("type", type); return this;}CHTMLListElement* CHTMLListElement::SetCompact(void){ SetOptionalAttribute("compact", true); return this;}CNcbiOstream& CHTMLListElement::PrintChildren(CNcbiOstream& out, TMode mode){ if (mode == ePlainText) { CIndentingOstream out2(out); CHTMLElement::PrintChildren(out2, mode); } else { CHTMLElement::PrintChildren(out, mode); } return out;}// Special char.CHTMLSpecialChar::CHTMLSpecialChar(const char* html, const char* plain, int count) : CParent("", plain){ m_Name = s_GenerateNodeInternalName("specialchar", html); m_Html = html; m_Count = count;}CHTMLSpecialChar::~CHTMLSpecialChar(void){ return;}CNcbiOstream& CHTMLSpecialChar::PrintChildren(CNcbiOstream& out, TMode mode){ if ( mode == ePlainText ) { for ( int i = 0; i < m_Count; i++ ) { out << m_Plain; CHECK_STREAM_WRITE(out); } } else { for ( int i = 0; i < m_Count; i++ ) { out << "&" << m_Html << ";"; CHECK_STREAM_WRITE(out); } } return out;}// <html> tag.const char CHTML_html::sm_TagName[] = "html";CHTML_html::~CHTML_html(void){ return;}void CHTML_html::Init(void){ return;}void CHTML_html::EnablePopupMenu(CHTMLPopupMenu::EType type, const string& menu_script_url, bool use_dynamic_menu){ SPopupMenuInfo info(menu_script_url, use_dynamic_menu); m_PopupMenus[type] = info;}static bool s_CheckUsePopupMenus(const CNCBINode* node, CHTMLPopupMenu::EType type){ if ( !node || !node->HaveChildren() ) { return false; } ITERATE ( CNCBINode::TChildren, i, node->Children() ) { const CNCBINode* cnode = node->Node(i); if ( dynamic_cast<const CHTMLPopupMenu*>(cnode) ) { const CHTMLPopupMenu* menu = dynamic_cast<const CHTMLPopupMenu*>(cnode); if ( menu->GetType() == type ) return true; } if ( cnode->HaveChildren() && s_CheckUsePopupMenus(cnode, type)) { return true; } } return false;}CNcbiOstream& CHTML_html::PrintChildren(CNcbiOstream& out, TMode mode){ // Check mode if ( mode != eHTML ) { return CParent::PrintChildren(out, mode); } // Check for use popup menus bool use_popup_menus = false; for (int t = CHTMLPopupMenu::ePMFirst; t <= CHTMLPopupMenu::ePMLast; t++ ) { CHTMLPopupMenu::EType type = (CHTMLPopupMenu::EType)t; if ( m_PopupMenus.find(type) == m_PopupMenus.end() ) { if ( s_CheckUsePopupMenus(this, type) ) { EnablePopupMenu(type); use_popup_menus = true; } } else { use_popup_menus = true; } } if ( !use_popup_menus || !HaveChildren() ) { return CParent::PrintChildren(out, mode); } NON_CONST_ITERATE ( TChildren, i, Children() ) { if ( dynamic_cast<CHTML_head*>(Node(i)) ) { for (int t = CHTMLPopupMenu::ePMFirst; t <= CHTMLPopupMenu::ePMLast; t++ ) { CHTMLPopupMenu::EType type = (CHTMLPopupMenu::EType)t; TPopupMenus::const_iterator info = m_PopupMenus.find(type); if ( info != m_PopupMenus.end() ) { Node(i)->AppendChild(new CHTMLText( CHTMLPopupMenu::GetCodeHead(type,info->second.m_Url))); } } } else if ( dynamic_cast<CHTML_body*>(Node(i)) ) { for (int t = CHTMLPopupMenu::ePMFirst; t <= CHTMLPopupMenu::ePMLast; t++ ) { CHTMLPopupMenu::EType type = (CHTMLPopupMenu::EType)t; TPopupMenus::const_iterator info = m_PopupMenus.find(type); if ( info != m_PopupMenus.end() ) { Node(i)->AppendChild(new CHTMLText( CHTMLPopupMenu::GetCodeBody(type, info->second.m_UseDynamicMenu))); } } } } return CParent::PrintChildren(out, mode);}// Table element.class CHTML_tc_Cache{public: CHTML_tc_Cache(void) : m_Used(false), m_Node(0) { return; } bool IsUsed(void) const { return m_Used; } bool IsNode(void) const { return m_Node != 0; } CHTML_tc* GetCellNode(void) const { return m_Node; } void SetUsed(void); void SetCellNode(CHTML_tc* node);private: bool m_Used; CHTML_tc* m_Node;};class CHTML_tr_Cache{public: typedef CHTML_table::TIndex TIndex; CHTML_tr_Cache(void) : m_Node(0), m_CellCount(0), m_CellsSize(0), m_Cells(0), m_FilledCellCount(0) { return; } ~CHTML_tr_Cache(void) { delete[] m_Cells; } CHTML_tr* GetRowNode(void) const { return m_Node; } void SetRowNode(CHTML_tr* rowNode) { _ASSERT(!m_Node && rowNode); m_Node = rowNode; } TIndex GetCellCount(void) const { return m_CellCount; } CHTML_tc_Cache& GetCellCache(TIndex col); void AppendCell(CHTML_tr* rowNode, TIndex col, CHTML_tc* cellNode, TIndex colSpan); void SetUsedCells(TIndex colBegin, TIndex colEnd); void SetUsedCells(CHTML_tc* cellNode, TIndex colBegin, TIndex colEnd);private: CHTML_tr_Cache(const CHTML_tr_Cache&); CHTML_tr_Cache& operator=(const CHTML_tr_Cache&); CHTML_tr* m_Node; TIndex m_CellCount; TIndex m_CellsSize; CHTML_tc_Cache* m_Cells; TIndex m_FilledCellCount;};class CHTML_table_Cache{public: typedef CHTML_table::TIndex TIndex; CHTML_table_Cache(CHTML_table* table); ~CHTML_table_Cache(void); TIndex GetRowCount(void) const { return m_RowCount; } CHTML_tr_Cache& GetRowCache(TIndex row); CHTML_tr* GetRowNode(TIndex row); CHTML_tc* GetCellNode(TIndex row, TIndex col, CHTML_table::ECellType type); CHTML_tc* GetCellNode(TIndex row, TIndex col, CHTML_table::ECellType type, TIndex rowSpan, TIndex colSpan); void InitRow(TIndex row, CHTML_tr* rowNode); void SetUsedCells(TIndex rowBegin, TIndex rowEnd, TIndex colBegin, TIndex colEnd);private: CHTML_table* m_Node; TIndex m_RowCount; TIndex m_RowsSize; CHTML_tr_Cache** m_Rows; TIndex m_FilledRowCount; CHTML_table_Cache(const CHTML_table_Cache&); CHTML_table_Cache& operator=(const CHTML_table_Cache&);};CHTML_tr::CHTML_tr(void) : CParent("tr"), m_Parent(0){ return;}CHTML_tr::CHTML_tr(CNCBINode* node) : CParent("tr", node), m_Parent(0){ return;}CHTML_tr::CHTML_tr(const string& text) : CParent("tr", text), m_Parent(0){ return;}void CHTML_tr::DoAppendChild(CNCBINode* node){ CHTML_tc* cell = dynamic_cast<CHTML_tc*>(node); if ( cell ) { // Adding new cell _ASSERT(!cell->m_Parent); ResetTableCache(); cell->m_Parent = this; } CParent::DoAppendChild(node);}void CHTML_tr::AppendCell(CHTML_tc* cell){ _ASSERT(!cell->m_Parent); cell->m_Parent = this; CParent::DoAppendChild(cell);}void CHTML_tr::ResetTableCache(void){ if ( m_Parent ) m_Parent->ResetTableCache();}CNcbiOstream& CHTML_tr::PrintEnd(CNcbiOstream& out, TMode mode){ CParent::PrintEnd(out, mode); if ( mode == ePlainText ) { out << CHTMLHelper::GetNL(); if (m_Parent->m_IsRowSep == CHTML_table::ePrintRowSep) { out << string(GetTextLength(mode), m_Parent->m_RowSepChar) << CHTMLHelper::GetNL(); } CHECK_STREAM_WRITE(out); } return out;}CNcbiOstream& CHTML_tr::PrintChildren(CNcbiOstream& out, TMode mode){ if ( !HaveChildren() ) { return out; } if ( mode != ePlainText ) { return CParent::PrintChildren(out, mode); } out << m_Parent->m_ColSepL; NON_CONST_ITERATE ( TChildren, i, Children() ) { if ( i != Children().begin() ) { out << m_Parent->m_ColSepM; CHECK_STREAM_WRITE(out); } Node(i)->Print(out, mode); } out << m_Parent->m_ColSepR; CHECK_STREAM_WRITE(out); return out;}SIZE_TYPE CHTML_tr::GetTextLength(TMode mode){ if ( !HaveChildren() ) { return 0; } CNcbiOstrstream sout; SIZE_TYPE cols = 0; NON_CONST_ITERATE ( TChildren, i, Children() ) { Node(i)->Print(sout, mode); cols++; } sout.put('\0'); SIZE_TYPE textlen = strlen(sout.str()); if ( mode == ePlainText ) { textlen += m_Parent->m_ColSepL.length() + m_Parent->m_ColSepR.length(); if ( cols ) { textlen += m_Parent->m_ColSepM.length() * (cols - 1); } } return textlen;}CHTML_tc::~CHTML_tc(void){ return;}CHTML_tc* CHTML_tc::SetRowSpan(TIndex span){ SetAttribute("rowspan", span); return this;}CHTML_tc* CHTML_tc::SetColSpan(TIndex span){ SetAttribute("colspan", span); return this;}static CHTML_table::TIndex x_GetSpan(const CHTML_tc* node, const string& attributeName){ if ( !node->HaveAttribute(attributeName) ) { return 1; } const string& value = node->GetAttribute(attributeName); try { CHTML_table::TIndex span = NStr::StringToUInt(value); if ( span > 0 ) { return span; } } catch ( exception& ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -