This source file includes following definitions.
- tokenExitsForeignContent
- tokenExitsSVG
- tokenExitsMath
- stateFor
- simulate
#include "config.h"
#include "core/html/parser/HTMLTreeBuilderSimulator.h"
#include "HTMLNames.h"
#include "MathMLNames.h"
#include "SVGNames.h"
#include "core/html/parser/HTMLParserIdioms.h"
#include "core/html/parser/HTMLTokenizer.h"
#include "core/html/parser/HTMLTreeBuilder.h"
namespace WebCore {
using namespace HTMLNames;
static bool tokenExitsForeignContent(const CompactHTMLToken& token)
{
const String& tagName = token.data();
return threadSafeMatch(tagName, bTag)
|| threadSafeMatch(tagName, bigTag)
|| threadSafeMatch(tagName, blockquoteTag)
|| threadSafeMatch(tagName, bodyTag)
|| threadSafeMatch(tagName, brTag)
|| threadSafeMatch(tagName, centerTag)
|| threadSafeMatch(tagName, codeTag)
|| threadSafeMatch(tagName, ddTag)
|| threadSafeMatch(tagName, divTag)
|| threadSafeMatch(tagName, dlTag)
|| threadSafeMatch(tagName, dtTag)
|| threadSafeMatch(tagName, emTag)
|| threadSafeMatch(tagName, embedTag)
|| threadSafeMatch(tagName, h1Tag)
|| threadSafeMatch(tagName, h2Tag)
|| threadSafeMatch(tagName, h3Tag)
|| threadSafeMatch(tagName, h4Tag)
|| threadSafeMatch(tagName, h5Tag)
|| threadSafeMatch(tagName, h6Tag)
|| threadSafeMatch(tagName, headTag)
|| threadSafeMatch(tagName, hrTag)
|| threadSafeMatch(tagName, iTag)
|| threadSafeMatch(tagName, imgTag)
|| threadSafeMatch(tagName, liTag)
|| threadSafeMatch(tagName, listingTag)
|| threadSafeMatch(tagName, menuTag)
|| threadSafeMatch(tagName, metaTag)
|| threadSafeMatch(tagName, nobrTag)
|| threadSafeMatch(tagName, olTag)
|| threadSafeMatch(tagName, pTag)
|| threadSafeMatch(tagName, preTag)
|| threadSafeMatch(tagName, rubyTag)
|| threadSafeMatch(tagName, sTag)
|| threadSafeMatch(tagName, smallTag)
|| threadSafeMatch(tagName, spanTag)
|| threadSafeMatch(tagName, strongTag)
|| threadSafeMatch(tagName, strikeTag)
|| threadSafeMatch(tagName, subTag)
|| threadSafeMatch(tagName, supTag)
|| threadSafeMatch(tagName, tableTag)
|| threadSafeMatch(tagName, ttTag)
|| threadSafeMatch(tagName, uTag)
|| threadSafeMatch(tagName, ulTag)
|| threadSafeMatch(tagName, varTag)
|| (threadSafeMatch(tagName, fontTag) && (token.getAttributeItem(colorAttr) || token.getAttributeItem(faceAttr) || token.getAttributeItem(sizeAttr)));
}
static bool tokenExitsSVG(const CompactHTMLToken& token)
{
return equalIgnoringCaseNonNull(token.data().impl(), SVGNames::foreignObjectTag.localName().impl());
}
static bool tokenExitsMath(const CompactHTMLToken& token)
{
const String& tagName = token.data();
return threadSafeMatch(tagName, MathMLNames::miTag)
|| threadSafeMatch(tagName, MathMLNames::moTag)
|| threadSafeMatch(tagName, MathMLNames::mnTag)
|| threadSafeMatch(tagName, MathMLNames::msTag)
|| threadSafeMatch(tagName, MathMLNames::mtextTag);
}
HTMLTreeBuilderSimulator::HTMLTreeBuilderSimulator(const HTMLParserOptions& options)
: m_options(options)
{
m_namespaceStack.append(HTML);
}
HTMLTreeBuilderSimulator::State HTMLTreeBuilderSimulator::stateFor(HTMLTreeBuilder* treeBuilder)
{
ASSERT(isMainThread());
State namespaceStack;
for (HTMLElementStack::ElementRecord* record = treeBuilder->openElements()->topRecord(); record; record = record->next()) {
Namespace currentNamespace = HTML;
if (record->namespaceURI() == SVGNames::svgNamespaceURI)
currentNamespace = SVG;
else if (record->namespaceURI() == MathMLNames::mathmlNamespaceURI)
currentNamespace = MathML;
if (namespaceStack.isEmpty() || namespaceStack.last() != currentNamespace)
namespaceStack.append(currentNamespace);
}
namespaceStack.reverse();
return namespaceStack;
}
bool HTMLTreeBuilderSimulator::simulate(const CompactHTMLToken& token, HTMLTokenizer* tokenizer)
{
if (token.type() == HTMLToken::StartTag) {
const String& tagName = token.data();
if (threadSafeMatch(tagName, SVGNames::svgTag))
m_namespaceStack.append(SVG);
if (threadSafeMatch(tagName, MathMLNames::mathTag))
m_namespaceStack.append(MathML);
if (inForeignContent() && tokenExitsForeignContent(token))
m_namespaceStack.removeLast();
if ((m_namespaceStack.last() == SVG && tokenExitsSVG(token))
|| (m_namespaceStack.last() == MathML && tokenExitsMath(token)))
m_namespaceStack.append(HTML);
if (!inForeignContent()) {
if (threadSafeMatch(tagName, textareaTag) || threadSafeMatch(tagName, titleTag))
tokenizer->setState(HTMLTokenizer::RCDATAState);
else if (threadSafeMatch(tagName, plaintextTag))
tokenizer->setState(HTMLTokenizer::PLAINTEXTState);
else if (threadSafeMatch(tagName, scriptTag))
tokenizer->setState(HTMLTokenizer::ScriptDataState);
else if (threadSafeMatch(tagName, styleTag)
|| threadSafeMatch(tagName, iframeTag)
|| threadSafeMatch(tagName, xmpTag)
|| (threadSafeMatch(tagName, noembedTag) && m_options.pluginsEnabled)
|| threadSafeMatch(tagName, noframesTag)
|| (threadSafeMatch(tagName, noscriptTag) && m_options.scriptEnabled))
tokenizer->setState(HTMLTokenizer::RAWTEXTState);
}
}
if (token.type() == HTMLToken::EndTag) {
const String& tagName = token.data();
if ((m_namespaceStack.last() == SVG && threadSafeMatch(tagName, SVGNames::svgTag))
|| (m_namespaceStack.last() == MathML && threadSafeMatch(tagName, MathMLNames::mathTag))
|| (m_namespaceStack.contains(SVG) && m_namespaceStack.last() == HTML && tokenExitsSVG(token))
|| (m_namespaceStack.contains(MathML) && m_namespaceStack.last() == HTML && tokenExitsMath(token)))
m_namespaceStack.removeLast();
if (threadSafeMatch(tagName, scriptTag)) {
if (!inForeignContent())
tokenizer->setState(HTMLTokenizer::DataState);
return false;
}
}
tokenizer->setForceNullCharacterReplacement(inForeignContent());
tokenizer->setShouldAllowCDATA(inForeignContent());
return true;
}
}