📄 markup.cpp
字号:
bool CMarkup::x_FindAttrib( CMarkup::TokenPos& token, MCD_PCSZ szAttrib, int n/*=0*/ )
{
// Return true if found, otherwise false and token.nNext is new insertion point
// If szAttrib is NULL find attrib n and leave token at attrib name
// If szAttrib is given, find matching attrib and leave token at value
// support non-well-formed attributes e.g. href=/advanced_search?hl=en, nowrap
// token also holds start and length of preceeding whitespace to support remove
//
int nPreSpaceStart;
int nPreSpaceLength;
int nChar;
MCD_CHAR cFirstChar;
MCD_PCSZ szDoc = token.szDoc;
int nAttrib = -1; // starts at tag name
int nFoundAttribNameR = 0;
bool bAfterEqual = false;
while ( 1 )
{
// Starting at token.nNext, bypass whitespace and find the next token
nChar = token.nNext;
nPreSpaceStart = nChar;
if ( ! x_FindAny(szDoc,nChar) )
break;
nPreSpaceLength = nChar - nPreSpaceStart;
// Is it an opening quote?
cFirstChar = szDoc[nChar];
if ( cFirstChar == _T('\"') || cFirstChar == _T('\'') )
{
token.nTokenFlags |= MNF_QUOTED;
// Move past opening quote
++nChar;
token.nL = nChar;
// Look for closing quote
while ( szDoc[nChar] && szDoc[nChar] != cFirstChar )
nChar += MCD_CLEN( &szDoc[nChar] );
// Set right to before closing quote
token.nR = nChar - 1;
// Set nChar past closing quote unless at end of document
if ( szDoc[nChar] )
++nChar;
}
else
{
token.nTokenFlags &= ~MNF_QUOTED;
// Go until special char or whitespace
token.nL = nChar;
if ( bAfterEqual )
{
while ( szDoc[nChar] && ! MCD_PSZCHR(_T(" \t\n\r>"),szDoc[nChar]) )
nChar += MCD_CLEN( &szDoc[nChar] );
}
else
{
while ( szDoc[nChar] && ! MCD_PSZCHR(_T("= \t\n\r>/?"),szDoc[nChar]) )
nChar += MCD_CLEN( &szDoc[nChar] );
}
// Adjust end position if it is one special char
if ( nChar == token.nL )
++nChar; // it is a special char
token.nR = nChar - 1;
}
// nNext points to one past last char of token
token.nNext = nChar;
if ( ! bAfterEqual && ! (token.nTokenFlags&MNF_QUOTED) )
{
// Is it an equal sign?
MCD_CHAR cChar = szDoc[token.nL];
if ( cChar == _T('=') )
{
bAfterEqual = true;
continue;
}
// Is it the right angle bracket?
if ( cChar == _T('>') || cChar == _T('/') || cChar == _T('?') )
{
token.nNext = nPreSpaceStart;
break; // attrib not found
}
if ( nFoundAttribNameR )
break;
// Attribute name
if ( nAttrib != -1 )
{
if ( ! szAttrib )
{
if ( nAttrib == n )
return true; // found by number
}
else if ( token.Match(szAttrib) )
{
// Matched attrib name, go forward to value
nFoundAttribNameR = token.nR;
token.nPreSpaceStart = nPreSpaceStart;
token.nPreSpaceLength = nPreSpaceLength;
}
}
++nAttrib;
}
else if ( nFoundAttribNameR )
break;
bAfterEqual = false;
}
if ( nFoundAttribNameR )
{
if ( ! bAfterEqual )
{
// when attribute has no value the value is the attribute name
token.nL = token.nPreSpaceStart + token.nPreSpaceLength;
token.nR = nFoundAttribNameR;
token.nNext = nFoundAttribNameR + 1;
}
return true; // found by name
}
return false; // not found
}
MCD_STR CMarkup::x_GetAttrib( int iPos, MCD_PCSZ szAttrib ) const
{
// Return the value of the attrib
TokenPos token( m_strDoc, m_nFlags );
if ( iPos && m_nNodeType == MNT_ELEMENT )
token.nNext = m_aPos[iPos].nStart + 1;
else if ( iPos == m_iPos && m_nNodeLength && m_nNodeType == MNT_PROCESSING_INSTRUCTION )
token.nNext = m_nNodeOffset + 2;
else
return _T("");
if ( szAttrib && x_FindAttrib( token, szAttrib ) )
return UnescapeText( &token.szDoc[token.nL], token.Length() );
return _T("");
}
bool CMarkup::x_SetAttrib( int iPos, MCD_PCSZ szAttrib, int nValue )
{
// Convert integer to string
MCD_CHAR szVal[25];
MCD_SPRINTF( szVal, _T("%d"), nValue );
return x_SetAttrib( iPos, szAttrib, szVal );
}
bool CMarkup::x_SetAttrib( int iPos, MCD_PCSZ szAttrib, MCD_PCSZ szValue )
{
// Set attribute in iPos element
TokenPos token( m_strDoc, m_nFlags );
if ( iPos && m_nNodeType == MNT_ELEMENT )
token.nNext = m_aPos[iPos].nStart + 1;
else if ( iPos == m_iPos && m_nNodeLength && m_nNodeType == MNT_PROCESSING_INSTRUCTION )
token.nNext = m_nNodeOffset + 2;
else
return false;
// Create insertion text depending on whether attribute already exists
// Decision: for empty value leaving attrib="" instead of removing attrib
int nReplace = 0;
int nInsertAt;
MCD_STR strInsert;
strInsert += x_ATTRIBQUOTE;
strInsert += EscapeText( szValue, MNF_ESCAPEQUOTES );
strInsert += x_ATTRIBQUOTE;
if ( x_FindAttrib( token, szAttrib ) )
{
// Replace value
nInsertAt = token.nL - ((token.nTokenFlags&MNF_QUOTED)?1:0);
nReplace = token.Length() + ((token.nTokenFlags&MNF_QUOTED)?2:0);
}
else
{
// Insert string name value pair
MCD_STR strFormat;
strFormat = _T(" ");
strFormat += szAttrib;
strFormat += _T("=");
strFormat += strInsert;
strInsert = strFormat;
nInsertAt = token.nNext;
}
x_DocChange( nInsertAt, nReplace, strInsert );
int nAdjust = MCD_STRLENGTH(strInsert) - nReplace;
if ( m_nNodeType == MNT_PROCESSING_INSTRUCTION )
{
x_AdjustForNode( m_iPosParent, m_iPos, nAdjust );
m_nNodeLength += nAdjust;
MARKUP_SETDEBUGSTATE;
return true;
}
m_aPos[iPos].AdjustStartTagLen( nAdjust );
m_aPos[iPos].nLength += nAdjust;
x_Adjust( iPos, nAdjust );
MARKUP_SETDEBUGSTATE;
return true;
}
bool CMarkup::x_CreateNode( MCD_STR& strNode, int nNodeType, MCD_PCSZ szText )
{
// Set strNode based on nNodeType and szData
// Return false if szData would jeopardize well-formed document
//
switch ( nNodeType )
{
case MNT_PROCESSING_INSTRUCTION:
strNode = _T("<?");
strNode += szText;
strNode += _T("?>");
break;
case MNT_COMMENT:
strNode = _T("<!--");
strNode += szText;
strNode += _T("-->");
break;
case MNT_ELEMENT:
strNode = _T("<");
strNode += szText;
strNode += _T("/>");
break;
case MNT_TEXT:
case MNT_WHITESPACE:
strNode = EscapeText( szText );
break;
case MNT_DOCUMENT_TYPE:
strNode = szText;
break;
case MNT_LONE_END_TAG:
return false;
case MNT_CDATA_SECTION:
if ( MCD_PSZSTR(szText,_T("]]>")) != NULL )
return false;
strNode = _T("<![CDATA[");
strNode += szText;
strNode += _T("]]>");
break;
}
return true;
}
MCD_STR CMarkup::x_EncodeCDATASection( MCD_PCSZ szData )
{
// Split CDATA Sections if there are any end delimiters
MCD_STR strData = _T("<![CDATA[");
MCD_PCSZ pszNextStart = szData;
MCD_PCSZ pszEnd = MCD_PSZSTR( szData, _T("]]>") );
while ( pszEnd )
{
strData += MCD_STR( pszNextStart, (int)(pszEnd - pszNextStart) );
strData += _T("]]]]><![CDATA[>");
pszNextStart = pszEnd + 3;
pszEnd = MCD_PSZSTR( pszNextStart, _T("]]>") );
}
strData += pszNextStart;
strData += _T("]]>");
return strData;
}
bool CMarkup::x_SetData( int iPos, int nValue )
{
// Convert integer to string
MCD_CHAR szVal[25];
MCD_SPRINTF( szVal, _T("%d"), nValue );
return x_SetData( iPos, szVal, 0 );
}
bool CMarkup::x_SetData( int iPos, MCD_PCSZ szData, int nFlags )
{
// Set data at specified position
// if nFlags==1, set content of element to a CDATA Section
MCD_STR strInsert;
if ( iPos == m_iPos && m_nNodeLength )
{
// Not an element
if ( ! x_CreateNode(strInsert, m_nNodeType, szData) )
return false;
x_DocChange( m_nNodeOffset, m_nNodeLength, strInsert );
x_AdjustForNode( m_iPosParent, iPos, MCD_STRLENGTH(strInsert) - m_nNodeLength );
m_nNodeLength = MCD_STRLENGTH(strInsert);
MARKUP_SETDEBUGSTATE;
return true;
}
// Set data in iPos element
if ( ! iPos || m_aPos[iPos].iElemChild )
return false;
// Build strInsert from szData based on nFlags
if ( nFlags & MNF_WITHCDATA )
strInsert = x_EncodeCDATASection( szData );
else
strInsert = EscapeText( szData, nFlags );
// Insert
NodePos node( MNF_WITHNOLINES|MNF_REPLACE );
node.strMeta = strInsert;
int iPosBefore = 0;
int nReplace = x_InsertNew( iPos, iPosBefore, node );
int nAdjust = MCD_STRLENGTH(node.strMeta) - nReplace;
x_Adjust( iPos, nAdjust );
m_aPos[iPos].nLength += nAdjust;
if ( m_aPos[iPos].nFlags & MNF_ILLDATA )
m_aPos[iPos].nFlags &= ~MNF_ILLDATA;
MARKUP_SETDEBUGSTATE;
return true;
}
MCD_STR CMarkup::x_GetData( int iPos ) const
{
if ( iPos == m_iPos && m_nNodeLength )
{
if ( m_nNodeType == MNT_COMMENT )
return MCD_STRMID( m_strDoc, m_nNodeOffset+4, m_nNodeLength-7 );
else if ( m_nNodeType == MNT_PROCESSING_INSTRUCTION )
return MCD_STRMID( m_strDoc, m_nNodeOffset+2, m_nNodeLength-4 );
else if ( m_nNodeType == MNT_CDATA_SECTION )
return MCD_STRMID( m_strDoc, m_nNodeOffset+9, m_nNodeLength-12 );
else if ( m_nNodeType == MNT_TEXT )
return UnescapeText( &(MCD_2PCSZ(m_strDoc))[m_nNodeOffset], m_nNodeLength );
else if ( m_nNodeType == MNT_LONE_END_TAG )
return MCD_STRMID( m_strDoc, m_nNodeOffset+2, m_nNodeLength-3 );
else
return MCD_STRMID( m_strDoc, m_nNodeOffset, m_nNodeLength );
}
// Return a string representing data between start and end tag
// Return empty string if there are any children elements
MCD_STR strData;
if ( ! m_aPos[iPos].iElemChild && ! m_aPos[iPos].IsEmptyElement() )
{
// Quick scan for any tags inside content
int nContentLen = m_aPos[iPos].ContentLen();
int nStartContent = m_aPos[iPos].StartContent();
MCD_PCSZ pszContent = &(MCD_2PCSZ(m_strDoc))[nStartContent];
MCD_PCSZ pszTag = MCD_PSZCHR( pszContent, _T('<') );
if ( pszTag && ((int)(pszTag-pszContent) < nContentLen) )
{
// Concatenate all CDATA Sections and text nodes, ignore other nodes
TokenPos token( m_strDoc, m_nFlags );
token.nNext = nStartContent;
NodePos node;
while ( token.nNext < nStartContent + nContentLen )
{
x_ParseNode( token, node );
if ( node.nNodeType == MNT_TEXT )
strData += UnescapeText( &token.szDoc[node.nStart], node.nLength );
else if ( node.nNodeType == MNT_CDATA_SECTION )
strData += MCD_STRMID( m_strDoc, node.nStart+9, node.nLength-12 );
}
}
else // no tags
strData = UnescapeText( &(MCD_2PCSZ(m_strDoc))[nStartContent], nContentLen );
}
return strData;
}
MCD_STR CMarkup::x_GetElemContent( int iPos ) const
{
if ( iPos && m_aPos[iPos].ContentLen() )
return MCD_STRMID( m_strDoc, m_aPos[iPos].StartContent(), m_aPos[iPos].ContentLen() );
return _T("");
}
bool CMarkup::x_SetElemContent( MCD_PCSZ szContent )
{
// Set data in iPos element only
if ( ! m_iPos )
return false;
if ( m_nNodeLength )
return false; // not an element
// Unlink all children
int iPos = m_iPos;
int iPosChild = m_aPos[iPos].iElemChild;
bool bHadChild = (iPosChild != 0);
while ( iPosChild )
iPosChild = x_ReleaseSubDoc( iPosChild );
if ( bHadChild )
x_CheckSavedPos();
// Parse content
bool bWellFormed = true;
TokenPos token( szContent, m_nFlags );
int iPosVirtual = x_GetFreePos();
m_aPos[iPosVirtual].ClearVirtualParent();
m_aPos[iPosVirtual].SetLevel( m_aPos[iPos].Level() + 1 );
iPosChild = x_ParseElem( iPosVirtual, token );
if ( m_aPos[iPosVirtual].nFlags & MNF_ILLFORMED )
bWellFormed = false;
m_aPos[iPos].nFlags = (m_aPos[iPos].nFlags & ~MNF_ILLDATA) | (m_aPos[iPosVirtual].nFlags & MNF_ILLDATA);
// Prepare insert and adjust offsets
NodePos node( MNF_WITHNOLINES|MNF_REPLACE );
node.strMeta = szContent;
int iPosBefore = 0;
int nReplace = x_InsertNew( iPos, iPosBefore, node );
// Adjust and link in the inserted elements
x_Adjust( iPosChild, node.nStart );
m_aPos[iPosChild].nStart += node.nStart;
m_aPos[iPos].iElemChild = iPosChild;
while ( iPosChild )
{
m_aPos[iPosChild].iElemParent = iPos;
iPosChild = m_aPos[iPosChild].iElemNext;
}
x_ReleasePos( iPosVirtual );
int nAdjust = MCD_STRLENGTH(node.strMeta) - nReplace;
x_Adjust( iPos, nAdjust, true );
m_aPos[iPos].nLength += nAdjust;
x_SetPos( m_iPosParent, m_iPos, 0 );
return bWellFormed;
}
void CMarkup::x_DocChange( int nLeft, int nReplace, const MCD_STR& strInsert )
{
// Insert strInsert int m_strDoc at nLeft replacing nReplace chars
// When creating a document, reduce reallocs by reserving string space
// If realloc needed, allow for 1.5 times the new length
//
int nDocLength = MCD_STRLENGTH(m_strDoc);
int nInsLength = MCD_STRLENGTH(strInsert);
int nNewLength = nInsLength + nDocLength - nReplace;
int nAllocLen = MCD_STRCAPACITY(m_strDoc);
#ifdef MCD_STRINSERTREPLACE // (STL)
if ( nNewLength > nAllocLen )
MCD_BLDRESERVE( m_strDoc, (nNewLength + nNewLength/2 + 128) );
MCD_STRINSERTREPLACE( m_strDoc, nLeft, nReplace, strInsert );
#else // (MFC)
int nBufferLen = nNewLength;
if ( nNewLength > nAllocLen )
nBufferLen += nBufferLen/2 + 128;
MCD_CHAR* pDoc = MCD_GETBUFFER( m_strDoc, nBufferLen );
if ( nLeft+nReplace < nDocLength )
memmove( &pDoc[nLeft+nInsLength], &pDoc[nLeft+nReplace], (nDocLength-nLeft-nReplace)*sizeof(MCD_CHAR) );
memcpy( &pDoc[nLeft], strInsert, nInsLength*sizeof(MCD_CHAR) );
MCD_RELEASEBUFFER( m_strDoc, pDoc, nNewLength );
#endif
}
void CMarkup::x_Adjust( int iPos, int nShift, bool bAfterPos /*=false*/ )
{
// Loop through affected elements and adjust indexes
// Algorithm:
// 1. update children unless bAfterPos
// (if no children or bAfterPos is true, length of iPos not affected)
// 2. update starts of next siblings and their children
// 3. go up until there is a next sibling of a pa
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -