#ifndef AtomicHTMLToken_h
#define AtomicHTMLToken_h
#include "HTMLElementLookupTrie.h"
#include "core/dom/Attribute.h"
#include "core/html/parser/CompactHTMLToken.h"
#include "core/html/parser/HTMLToken.h"
#include "wtf/RefCounted.h"
#include "wtf/RefPtr.h"
namespace WebCore {
class AtomicHTMLToken {
WTF_MAKE_NONCOPYABLE(AtomicHTMLToken);
public:
bool forceQuirks() const
{
ASSERT(m_type == HTMLToken::DOCTYPE);
return m_doctypeData->m_forceQuirks;
}
HTMLToken::Type type() const { return m_type; }
const AtomicString& name() const
{
ASSERT(usesName());
return m_name;
}
void setName(const AtomicString& name)
{
ASSERT(usesName());
m_name = name;
}
bool selfClosing() const
{
ASSERT(m_type == HTMLToken::StartTag || m_type == HTMLToken::EndTag);
return m_selfClosing;
}
Attribute* getAttributeItem(const QualifiedName& attributeName)
{
ASSERT(usesAttributes());
return findAttributeInVector(m_attributes, attributeName);
}
Vector<Attribute>& attributes()
{
ASSERT(usesAttributes());
return m_attributes;
}
const Vector<Attribute>& attributes() const
{
ASSERT(usesAttributes());
return m_attributes;
}
const String& characters() const
{
ASSERT(m_type == HTMLToken::Character);
return m_data;
}
const String& comment() const
{
ASSERT(m_type == HTMLToken::Comment);
return m_data;
}
Vector<UChar>& publicIdentifier() const
{
ASSERT(m_type == HTMLToken::DOCTYPE);
return m_doctypeData->m_publicIdentifier;
}
Vector<UChar>& systemIdentifier() const
{
ASSERT(m_type == HTMLToken::DOCTYPE);
return m_doctypeData->m_systemIdentifier;
}
explicit AtomicHTMLToken(HTMLToken& token)
: m_type(token.type())
{
switch (m_type) {
case HTMLToken::Uninitialized:
ASSERT_NOT_REACHED();
break;
case HTMLToken::DOCTYPE:
m_name = AtomicString(token.name());
m_doctypeData = token.releaseDoctypeData();
break;
case HTMLToken::EndOfFile:
break;
case HTMLToken::StartTag:
case HTMLToken::EndTag: {
m_selfClosing = token.selfClosing();
if (StringImpl* tagName = lookupHTMLTag(token.name().data(), token.name().size()))
m_name = AtomicString(tagName);
else
m_name = AtomicString(token.name());
initializeAttributes(token.attributes());
break;
}
case HTMLToken::Character:
case HTMLToken::Comment:
if (token.isAll8BitData())
m_data = String::make8BitFrom16BitSource(token.data());
else
m_data = String(token.data());
break;
}
}
explicit AtomicHTMLToken(const CompactHTMLToken& token)
: m_type(token.type())
{
switch (m_type) {
case HTMLToken::Uninitialized:
ASSERT_NOT_REACHED();
break;
case HTMLToken::DOCTYPE:
m_name = AtomicString(token.data());
m_doctypeData = adoptPtr(new DoctypeData());
m_doctypeData->m_hasPublicIdentifier = true;
append(m_doctypeData->m_publicIdentifier, token.publicIdentifier());
m_doctypeData->m_hasSystemIdentifier = true;
append(m_doctypeData->m_systemIdentifier, token.systemIdentifier());
m_doctypeData->m_forceQuirks = token.doctypeForcesQuirks();
break;
case HTMLToken::EndOfFile:
break;
case HTMLToken::StartTag:
m_attributes.reserveInitialCapacity(token.attributes().size());
for (Vector<CompactHTMLToken::Attribute>::const_iterator it = token.attributes().begin(); it != token.attributes().end(); ++it) {
QualifiedName name(nullAtom, AtomicString(it->name), nullAtom);
if (!findAttributeInVector(m_attributes, name))
m_attributes.append(Attribute(name, AtomicString(it->value)));
}
case HTMLToken::EndTag:
m_selfClosing = token.selfClosing();
m_name = AtomicString(token.data());
break;
case HTMLToken::Character:
case HTMLToken::Comment:
m_data = token.data();
break;
}
}
explicit AtomicHTMLToken(HTMLToken::Type type)
: m_type(type)
, m_selfClosing(false)
{
}
AtomicHTMLToken(HTMLToken::Type type, const AtomicString& name, const Vector<Attribute>& attributes = Vector<Attribute>())
: m_type(type)
, m_name(name)
, m_selfClosing(false)
, m_attributes(attributes)
{
ASSERT(usesName());
}
private:
HTMLToken::Type m_type;
void initializeAttributes(const HTMLToken::AttributeList& attributes);
QualifiedName nameForAttribute(const HTMLToken::Attribute&) const;
bool usesName() const;
bool usesAttributes() const;
AtomicString m_name;
String m_data;
OwnPtr<DoctypeData> m_doctypeData;
bool m_selfClosing;
Vector<Attribute> m_attributes;
};
inline void AtomicHTMLToken::initializeAttributes(const HTMLToken::AttributeList& attributes)
{
size_t size = attributes.size();
if (!size)
return;
m_attributes.clear();
m_attributes.reserveInitialCapacity(size);
for (size_t i = 0; i < size; ++i) {
const HTMLToken::Attribute& attribute = attributes[i];
if (attribute.name.isEmpty())
continue;
ASSERT(attribute.nameRange.end);
ASSERT(attribute.valueRange.start);
ASSERT(attribute.valueRange.end);
AtomicString value(attribute.value);
const QualifiedName& name = nameForAttribute(attribute);
if (!findAttributeInVector(m_attributes, name))
m_attributes.append(Attribute(name, value));
}
}
}
#endif