📄 htmltokenizer.cpp
字号:
/* This file is part of the KDE libraries Copyright (C) 1997 Martin Jones (mjones@kde.org) (C) 1997 Torben Weis (weis@kde.org) (C) 1998 Waldo Bastian (bastian@kde.org) (C) 1999 Lars Knoll (knoll@kde.org) (C) 1999 Antti Koivisto (koivisto@kde.org) (C) 2001 Dirk Mueller (mueller@kde.org) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*///----------------------------------------------------------------------------//// KDE HTML Widget - Tokenizers// $Id: htmltokenizer.cpp,v 1.193.2.7 2001/11/26 16:37:25 mueller Exp $//#define TOKEN_DEBUG 1//#define TOKEN_DEBUG 2#ifdef HAVE_CONFIG_H#include "config.h"#endif#include <string.h>#include "htmltokenizer.h"#include "misc/loader.h"#include "khtmlview.h"#include "khtml_part.h"#include "htmlparser.h"#include "html_documentimpl.h"#include "css/csshelper.h"#include "dtd.h"#include "htmlhashes.h"#include <kcharsets.h>#include <kglobal.h>#include <ctype.h>#include <assert.h>#include <kdebug.h>#include <stdlib.h>#include "kjs.h"#include "kentities.c"using namespace khtml;static const QChar commentStart [] = { '<','!','-','-', QChar::null };static const char scriptEnd [] = "</script";static const char listingEnd [] = "</listing";static const char styleEnd [] = "</style";static const char textareaEnd [] = "</textarea";#define KHTML_ALLOC_QCHAR_VEC( N ) (QChar*) malloc( sizeof(QChar)*( N ) )#define KHTML_REALLOC_QCHAR_VEC(P, N ) (QChar*) P = realloc(p, sizeof(QChar)*( N ))#define KHTML_DELETE_QCHAR_VEC( P ) free((char*)( P ))// Partial support for MS Windows Latin-1 extensions// full list http://www.bbsinc.com/iso8859.html// There may be better equivalents#define fixUpChar(x) \ if (!(x).row() ) { \ switch ((x).cell()) \ { \ case 0x82: (x) = ','; break; \ case 0x84: (x) = '"'; break; \ case 0x8b: (x) = '<'; break; \ case 0x9b: (x) = '>'; break; \ case 0x91: (x) = '\''; break; \ case 0x92: (x) = '\''; break; \ case 0x93: (x) = '"'; break; \ case 0x94: (x) = '"'; break; \ case 0x95: (x) = '*'; break; \ case 0x96: (x) = '-'; break; \ case 0x97: (x) = '-'; break; \ case 0x98: (x) = '~'; break; \ case 0xb7: (x) = '*'; break; \ case 0x2014: (x) = '-'; break; \ case 0x2019: (x) = '\''; break; \ case 0x201c: (x) = '"'; break; \ case 0x201d: (x) = '"'; break; \ default: break; \ } \ }// ----------------------------------------------------------------------------HTMLTokenizer::HTMLTokenizer(DOM::DocumentPtr *_doc, KHTMLView *_view){ view = _view; buffer = 0; scriptCode = 0; scriptCodeSize = scriptCodeMaxSize = scriptCodeResync = 0; charsets = KGlobal::charsets(); parser = new KHTMLParser(_view, _doc); cachedScript = 0; m_executingScript = 0; loadingExtScript = false; onHold = false; reset();}HTMLTokenizer::HTMLTokenizer(DOM::DocumentPtr *_doc, DOM::DocumentFragmentImpl *i){ view = 0; buffer = 0; scriptCode = 0; scriptCodeSize = scriptCodeMaxSize = scriptCodeResync = 0; charsets = KGlobal::charsets(); parser = new KHTMLParser( i, _doc ); cachedScript = 0; m_executingScript = 0; loadingExtScript = false; onHold = false; reset();}void HTMLTokenizer::reset(){ assert(m_executingScript == 0); assert(onHold == false); if (cachedScript) cachedScript->deref(this); cachedScript = 0; if ( buffer ) KHTML_DELETE_QCHAR_VEC(buffer); buffer = dest = 0; size = 0; if ( scriptCode ) KHTML_DELETE_QCHAR_VEC(scriptCode); scriptCode = 0; scriptCodeSize = scriptCodeMaxSize = scriptCodeResync = 0; currToken.reset();}void HTMLTokenizer::begin(){ m_executingScript = 0; loadingExtScript = false; onHold = false; reset(); size = 254; buffer = KHTML_ALLOC_QCHAR_VEC( 255 ); dest = buffer; tag = NoTag; pending = NonePending; discard = NoneDiscard; pre = false; prePos = 0; plaintext = false; listing = false; processingInstruction = false; script = false; escaped = false; style = false; skipLF = false; select = false; comment = false; textarea = false; startTag = false; tquote = NoQuote; searchCount = 0; Entity = NoEntity; loadingExtScript = false; scriptSrc = ""; pendingSrc = ""; noMoreData = false; brokenComments = false;}void HTMLTokenizer::processListing(DOMStringIt list){ bool old_pre = pre; // This function adds the listing 'list' as // preformatted text-tokens to the token-collection // thereby converting TABs. if(!style) pre = true; prePos = 0; while ( list.length() ) { checkBuffer(3*TAB_SIZE); if (skipLF && ( *list != '\n' )) { skipLF = false; } if (skipLF) { skipLF = false; ++list; } else if (( *list == '\n' ) || ( *list == '\r' )) { if (discard == LFDiscard) { // Ignore this LF discard = NoneDiscard; // We have discarded 1 LF } else { // Process this LF if (pending) addPending(); pending = LFPending; } /* Check for MS-DOS CRLF sequence */ if (*list == '\r') { skipLF = true; } ++list; } else if (( *list == ' ' ) || ( *list == '\t')) { if (pending) addPending(); if (*list == ' ') pending = SpacePending; else pending = TabPending; ++list; } else { discard = NoneDiscard; if (pending) addPending(); prePos++; *dest++ = *list; ++list; } } if ((pending == SpacePending) || (pending == TabPending)) addPending(); pending = NonePending; prePos = 0; pre = old_pre;}void HTMLTokenizer::parseSpecial(DOMStringIt &src, bool begin){ assert( textarea || !Entity ); assert( !tag ); assert( listing+textarea+style+script == 1 ); if ( begin ) { if ( script ) { searchStopper = scriptEnd; } else if ( style ) { searchStopper = styleEnd; } else if ( textarea ) { searchStopper = textareaEnd; } else if ( listing ) { searchStopper = listingEnd; } searchStopperLen = strlen( searchStopper ); } if ( comment ) parseComment( src ); while ( src.length() ) { checkScriptBuffer(); unsigned char ch = src->latin1(); if ( !scriptCodeResync && !textarea && ch == '-' && scriptCodeSize >= 3 && !src.escaped() && QConstString( scriptCode+scriptCodeSize-3, 3 ).string() == "<!-" ) { comment = true; parseComment( src ); continue; } if ( scriptCodeResync && !tquote && ( ch == '>' ) ) { ++src; scriptCodeSize = scriptCodeResync-1; scriptCodeResync = 0; scriptCode[ scriptCodeSize ] = scriptCode[ scriptCodeSize + 1 ] = 0; if ( script ) scriptHandler(); else { processListing(DOMStringIt(scriptCode, scriptCodeSize)); processToken(); if ( style ) { currToken.id = ID_STYLE + ID_CLOSE_TAG; } else if ( textarea ) { currToken.id = ID_TEXTAREA + ID_CLOSE_TAG; } else if ( listing ) { currToken.id = ID_LISTING + ID_CLOSE_TAG; } processToken(); style = script = style = textarea = listing = false; scriptCodeSize = scriptCodeResync = 0; } return; } // possible end of tagname, lets check. if ( !scriptCodeResync && !escaped && !src.escaped() && ( ch == '>' || ch == '/' || ch <= ' ' ) && ch && scriptCodeSize >= searchStopperLen && !QConstString( scriptCode+scriptCodeSize-searchStopperLen, searchStopperLen+1 ).string().find( searchStopper, 0, false )) { scriptCodeResync = scriptCodeSize-searchStopperLen+1; tquote = NoQuote; continue; } if ( scriptCodeResync && !escaped ) { if(ch == '\"') tquote = (tquote == NoQuote) ? DoubleQuote : ((tquote == SingleQuote) ? SingleQuote : NoQuote); else if(ch == '\'') tquote = (tquote == NoQuote) ? SingleQuote : (tquote == DoubleQuote) ? DoubleQuote : NoQuote; else if (tquote != NoQuote && (ch == '\r' || ch == '\n')) tquote = NoQuote; } escaped = ( !escaped && ch == '\\' ); if (!scriptCodeResync && textarea && !src.escaped() && ch == '&') { QChar *scriptCodeDest = scriptCode+scriptCodeSize; ++src; parseEntity(src,scriptCodeDest,true); scriptCodeSize = scriptCodeDest-scriptCode; } else { scriptCode[ scriptCodeSize++ ] = *src; ++src; } }}void HTMLTokenizer::scriptHandler(){ // We are inside a <script> bool doScriptExec = false; if (!scriptSrc.isEmpty()) { // forget what we just got; load from src url instead if ( !parser->skipMode() ) cachedScript = parser->doc()->docLoader()->requestScript(scriptSrc, parser->doc()->baseURL(), scriptSrcCharset); scriptSrc=""; } else {#ifdef TOKEN_DEBUG kdDebug( 6036 ) << "---START SCRIPT---" << endl; kdDebug( 6036 ) << QString(scriptCode, scriptCodeSize) << endl; kdDebug( 6036 ) << "---END SCRIPT---" << endl;#endif // Parse scriptCode containing <script> info doScriptExec = true; } processListing(DOMStringIt(scriptCode, scriptCodeSize)); QString exScript( buffer, dest-buffer ); processToken(); currToken.id = ID_SCRIPT + ID_CLOSE_TAG; processToken(); QString prependingSrc; if ( !parser->skipMode() ) { if (cachedScript) {// qDebug( "cachedscript extern!" );// qDebug( "src: *%s*", QString( src.current(), src.length() ).latin1() );// qDebug( "pending: *%s*", pendingSrc.latin1() ); pendingSrc.prepend( QString(src.current(), src.length() ) ); _src = QString::null; src = DOMStringIt(_src); scriptCodeSize = scriptCodeResync = 0; cachedScript->ref(this); // will be 0 if script was already loaded and ref() executed it if (cachedScript) loadingExtScript = true; } else if (view && doScriptExec && javascript && !parser->skipMode()) { if ( !m_executingScript ) pendingSrc.prepend( QString( src.current(), src.length() ) ); // deep copy - again else prependingSrc = QString( src.current(), src.length() ); // deep copy _src = QString::null; src = DOMStringIt( _src ); scriptCodeSize = scriptCodeResync = 0; scriptExecution( exScript ); } } script = false; scriptCodeSize = scriptCodeResync = 0; if ( !parser->skipMode() ) { if ( !m_executingScript && !loadingExtScript ) addPendingSource(); else if ( !prependingSrc.isEmpty() ) write( prependingSrc, false ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -