原创!转载请注明出自http://blog.csdn.net/lichwei1983
生成DOM Tree的过程中,HTMLTreeBuilder会经常检查看看自己是否必须暂停不可,以便执行缺省格式的脚本比如<script>,比如:
HTMLTreeBuilder处理token的时候,如果发现有<script>标签--token.name() == scriptTag,就会停下解析token的流程,转而运行这个脚本--通过runScriptsForPausedTreeBuilder,还记得缺省的测试程序jsc吗,她也是这个函数runScripts。
HTMLTreeBuilder.cpp:
HTMLTreeBuilder::processEndTag(AtomicHTMLToken& token){
case TextMode:
if (token.name() == scriptTag) {
// 设置停止标记,以便处理下一个词语(Token)时停止DOM树的构建,直到JS脚本完全被TreeBuilder处理完:
m_isPaused = true;
.......
}
bool HTMLDocumentParser::canTakeNextToken(SynchronousMode mode, PumpSession& session)
{
//处理每一个词语,都需要检查看看DOM Tree Builder是否因为之前执行脚本等事情,而停下来:
if(m_treeBuilder->isPaused()) {
// If we're paused waiting for a script, we try to execute scripts before continuing.
bool shouldContinueParsing = runScriptsForPausedTreeBuilder();
m_treeBuilder->setPaused(!shouldContinueParsing);
if (!shouldContinueParsing || isStopped())
return false;
}
.........
}
。。。
}
又比如加载cachedResource时候也需要检查:是否导致HTMLTreeBuilder process token的流程中断。
bool shouldContinueParsing = m_scriptRunner->executeScriptsWaitingForLoad(cachedResource);
m_treeBuilder->setPaused(!shouldContinueParsing);
或者StyeSheet的加载需要中断:
void HTMLDocumentParser::executeScriptsWaitingForStylesheets()
{
m_treeBuilder->setPaused(false);
bool shouldContinueParsing = m_scriptRunner->executeScriptsWaitingForStylesheets();
m_treeBuilder->setPaused(!shouldContinueParsing);
}