#include "config.h"
#include "HTMLDocument.h"
#include "CSSPropertyNames.h"
#include "CookieJar.h"
#include "DocumentType.h"
#include "ExceptionCode.h"
#include "Frame.h"
#include "HTMLElement.h"
#include "HTMLElementFactory.h"
#include "HTMLNames.h"
#include "HTMLTokenizer.h"
#include "cssstyleselector.h"
#include "DocTypeStrings.cpp"
namespace WebCore {
using namespace HTMLNames;
HTMLDocument::HTMLDocument(DOMImplementation *_implementation, FrameView *v)
: Document(_implementation, v)
{
bodyElement = 0;
htmlElement = 0;
}
HTMLDocument::~HTMLDocument()
{
}
String HTMLDocument::lastModified() const
{
if ( frame() )
return frame()->lastModified();
return String();
}
String HTMLDocument::cookie() const
{
return cookies(URL());
}
void HTMLDocument::setCookie(const String& value)
{
setCookies(URL(), m_policyBaseURL.deprecatedString(), value);
}
void HTMLDocument::setBody(HTMLElement *_body, ExceptionCode& ec)
{
if (!_body) {
ec = HIERARCHY_REQUEST_ERR;
return;
}
HTMLElement* b = body();
if (!b)
documentElement()->appendChild(_body, ec);
else
documentElement()->replaceChild(_body, b, ec);
}
Tokenizer *HTMLDocument::createTokenizer()
{
return new HTMLTokenizer(this, true);
}
bool HTMLDocument::childAllowed( Node *newChild )
{
return newChild->hasTagName(htmlTag) || newChild->isCommentNode();
}
PassRefPtr<Element> HTMLDocument::createElement(const String &name, ExceptionCode& ec)
{
String lowerName(name.lower());
if (!isValidName(lowerName)) {
ec = INVALID_CHARACTER_ERR;
return 0;
}
return HTMLElementFactory::createHTMLElement(AtomicString(lowerName), this, 0, false);
}
static void addItemToMap(HTMLDocument::NameCountMap& map, const String& name)
{
if (name.length() == 0)
return;
HTMLDocument::NameCountMap::iterator it = map.find(name.impl());
if (it == map.end())
map.set(name.impl(), 1);
else
++(it->second);
}
static void removeItemFromMap(HTMLDocument::NameCountMap& map, const String& name)
{
if (name.length() == 0)
return;
HTMLDocument::NameCountMap::iterator it = map.find(name.impl());
if (it == map.end())
return;
int oldVal = it->second;
assert(oldVal != 0);
int newVal = oldVal - 1;
if (newVal == 0)
map.remove(it);
else
it->second = newVal;
}
void HTMLDocument::addNamedItem(const String& name)
{
addItemToMap(namedItemCounts, name);
}
void HTMLDocument::removeNamedItem(const String &name)
{
removeItemFromMap(namedItemCounts, name);
}
bool HTMLDocument::hasNamedItem(const String& name)
{
return namedItemCounts.get(name.impl()) != 0;
}
void HTMLDocument::addDocExtraNamedItem(const String& name)
{
addItemToMap(docExtraNamedItemCounts, name);
}
void HTMLDocument::removeDocExtraNamedItem(const String& name)
{
removeItemFromMap(docExtraNamedItemCounts, name);
}
bool HTMLDocument::hasDocExtraNamedItem(const String& name)
{
return docExtraNamedItemCounts.get(name.impl()) != 0;
}
const int PARSEMODE_HAVE_DOCTYPE = (1<<0);
const int PARSEMODE_HAVE_PUBLIC_ID = (1<<1);
const int PARSEMODE_HAVE_SYSTEM_ID = (1<<2);
const int PARSEMODE_HAVE_INTERNAL = (1<<3);
static int parseDocTypePart(const DeprecatedString& buffer, int index)
{
while (true) {
DeprecatedChar ch = buffer[index];
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
++index;
else if (ch == '-') {
int tmpIndex=index;
if (buffer[index+1] == '-' &&
((tmpIndex=buffer.find("--", index+2)) != -1))
index = tmpIndex+2;
else
return index;
}
else
return index;
}
}
static bool containsString(const char* str, const DeprecatedString& buffer, int offset)
{
DeprecatedString startString(str);
if (offset + startString.length() > buffer.length())
return false;
DeprecatedString bufferString = buffer.mid(offset, startString.length()).lower();
DeprecatedString lowerStart = startString.lower();
return bufferString.startsWith(lowerStart);
}
static bool parseDocTypeDeclaration(const DeprecatedString& buffer,
int* resultFlags,
DeprecatedString& name,
DeprecatedString& publicID,
DeprecatedString& systemID)
{
bool haveDocType = false;
*resultFlags = 0;
int index = 0;
do {
index = buffer.find('<', index);
if (index == -1) break;
DeprecatedChar nextChar = buffer[index+1];
if (nextChar == '!') {
if (containsString("doctype", buffer, index+2)) {
haveDocType = true;
index += 9; break;
}
index = parseDocTypePart(buffer,index);
index = buffer.find('>', index);
}
else if (nextChar == '?')
index = buffer.find('>', index);
else
break;
} while (index != -1);
if (!haveDocType)
return true;
*resultFlags |= PARSEMODE_HAVE_DOCTYPE;
index = parseDocTypePart(buffer, index);
if (!containsString("html", buffer, index))
return false;
name = buffer.mid(index, 4);
index = parseDocTypePart(buffer, index+4);
bool hasPublic = containsString("public", buffer, index);
if (hasPublic) {
index = parseDocTypePart(buffer, index+6);
DeprecatedChar theChar = buffer[index];
if (theChar != '\"' && theChar != '\'')
return false;
int publicIDStart = index+1;
int publicIDEnd = buffer.find(theChar, publicIDStart);
if (publicIDEnd == -1)
return false;
index = parseDocTypePart(buffer, publicIDEnd+1);
DeprecatedChar next = buffer[index];
if (next == '>') {
}
else if (next == '\"' || next == '\'') {
*resultFlags |= PARSEMODE_HAVE_SYSTEM_ID;
int systemIDStart = index+1;
int systemIDEnd = buffer.find(next, systemIDStart);
if (systemIDEnd == -1)
return false;
systemID = buffer.mid(systemIDStart, systemIDEnd - systemIDStart);
}
else if (next == '[') {
*resultFlags |= PARSEMODE_HAVE_INTERNAL;
}
else
return false;
publicID = buffer.mid(publicIDStart, publicIDEnd - publicIDStart);
publicID = publicID.stripWhiteSpace();
*resultFlags |= PARSEMODE_HAVE_PUBLIC_ID;
} else {
if (containsString("system", buffer, index)) {
*resultFlags |= PARSEMODE_HAVE_SYSTEM_ID;
index = parseDocTypePart(buffer, index+6);
DeprecatedChar next = buffer[index];
if (next != '\"' && next != '\'')
return false;
int systemIDStart = index+1;
int systemIDEnd = buffer.find(next, systemIDStart);
if (systemIDEnd == -1)
return false;
systemID = buffer.mid(systemIDStart, systemIDEnd - systemIDStart);
index = parseDocTypePart(buffer, systemIDEnd+1);
}
DeprecatedChar nextChar = buffer[index];
if (nextChar == '[')
*resultFlags |= PARSEMODE_HAVE_INTERNAL;
else if (nextChar != '>')
return false;
}
return true;
}
void HTMLDocument::determineParseMode( const DeprecatedString &str )
{
DeprecatedString name, systemID, publicID;
int resultFlags = 0;
if (parseDocTypeDeclaration(str, &resultFlags, name, publicID, systemID)) {
if (resultFlags & PARSEMODE_HAVE_DOCTYPE)
setDocType(new DocumentType(this, name, publicID, systemID));
if (!(resultFlags & PARSEMODE_HAVE_DOCTYPE)) {
pMode = Compat;
hMode = Html4;
}
else if ((resultFlags & PARSEMODE_HAVE_INTERNAL) ||
!(resultFlags & PARSEMODE_HAVE_PUBLIC_ID)) {
pMode = Strict;
hMode = Html4;
}
else {
DeprecatedString lowerPubID = publicID.lower();
const char* pubIDStr = lowerPubID.latin1();
const PubIDInfo* doctypeEntry = findDoctypeEntry(pubIDStr, publicID.length());
if (!doctypeEntry) {
pMode = Strict;
hMode = Html4;
return;
}
switch ((resultFlags & PARSEMODE_HAVE_SYSTEM_ID) ?
doctypeEntry->mode_if_sysid :
doctypeEntry->mode_if_no_sysid)
{
case PubIDInfo::eQuirks3:
pMode = Compat;
hMode = Html3;
break;
case PubIDInfo::eQuirks:
pMode = Compat;
hMode = Html4;
break;
case PubIDInfo::eAlmostStandards:
pMode = AlmostStrict;
hMode = Html4;
break;
default:
assert(false);
}
}
}
else {
pMode = Compat;
hMode = Html3;
}
m_styleSelector->strictParsing = !inCompatMode();
}
}