#include "config.h"
#include "HTMLDocument.h"
#include "CSSPropertyNames.h"
#include "CSSStyleSelector.h"
#include "CString.h"
#include "CookieJar.h"
#include "DocumentLoader.h"
#include "DocumentType.h"
#include "ExceptionCode.h"
#include "FocusController.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameTree.h"
#include "FrameView.h"
#include "HTMLBodyElement.h"
#include "HTMLElementFactory.h"
#include "HTMLNames.h"
#include "HTMLTokenizer.h"
#include "InspectorController.h"
#include "KURL.h"
#include "Page.h"
#include "DocTypeStrings.cpp"
namespace WebCore {
using namespace HTMLNames;
HTMLDocument::HTMLDocument(Frame* frame)
: Document(frame, false)
{
clearXMLVersion();
setParseMode(Compat);
}
HTMLDocument::~HTMLDocument()
{
}
int HTMLDocument::width()
{
updateLayoutIgnorePendingStylesheets();
FrameView* frameView = view();
return frameView ? frameView->contentsWidth() : 0;
}
int HTMLDocument::height()
{
updateLayoutIgnorePendingStylesheets();
FrameView* frameView = view();
return frameView ? frameView->contentsHeight() : 0;
}
String HTMLDocument::dir()
{
HTMLElement* b = body();
if (!b)
return String();
return b->dir();
}
void HTMLDocument::setDir(const String& value)
{
HTMLElement* b = body();
if (b)
b->setDir(value);
}
String HTMLDocument::designMode() const
{
return inDesignMode() ? "on" : "off";
}
void HTMLDocument::setDesignMode(const String& value)
{
InheritedBool mode;
if (equalIgnoringCase(value, "on"))
mode = on;
else if (equalIgnoringCase(value, "off"))
mode = off;
else
mode = inherit;
Document::setDesignMode(mode);
}
String HTMLDocument::compatMode() const
{
return inCompatMode() ? "BackCompat" : "CSS1Compat";
}
Element* HTMLDocument::activeElement()
{
if (Node* node = focusedNode())
if (node->isElementNode())
return static_cast<Element*>(node);
return body();
}
bool HTMLDocument::hasFocus()
{
Page* page = this->page();
if (!page)
return false;
if (!page->focusController()->isActive())
return false;
if (Frame* focusedFrame = page->focusController()->focusedFrame()) {
if (focusedFrame->tree()->isDescendantOf(frame()))
return true;
}
return false;
}
String HTMLDocument::bgColor()
{
HTMLElement* b = body();
HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
if (!bodyElement)
return String();
return bodyElement->bgColor();
}
void HTMLDocument::setBgColor(const String& value)
{
HTMLElement* b = body();
HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
if (bodyElement)
bodyElement->setBgColor(value);
}
String HTMLDocument::fgColor()
{
HTMLElement* b = body();
HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
if (!bodyElement)
return String();
return bodyElement->text();
}
void HTMLDocument::setFgColor(const String& value)
{
HTMLElement* b = body();
HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
if (bodyElement)
bodyElement->setText(value);
}
String HTMLDocument::alinkColor()
{
HTMLElement* b = body();
HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
if (!bodyElement)
return String();
return bodyElement->aLink();
}
void HTMLDocument::setAlinkColor(const String& value)
{
HTMLElement* b = body();
HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
if (bodyElement) {
if (bodyElement->aLink() != value)
bodyElement->setALink(value);
}
}
String HTMLDocument::linkColor()
{
HTMLElement* b = body();
HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
if (!bodyElement)
return String();
return bodyElement->link();
}
void HTMLDocument::setLinkColor(const String& value)
{
HTMLElement* b = body();
HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
if (bodyElement) {
if (bodyElement->link() != value)
bodyElement->setLink(value);
}
}
String HTMLDocument::vlinkColor()
{
HTMLElement* b = body();
HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
if (!bodyElement)
return String();
return bodyElement->vLink();
}
void HTMLDocument::setVlinkColor(const String& value)
{
HTMLElement* b = body();
HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
if (bodyElement) {
if (bodyElement->vLink() != value)
bodyElement->setVLink(value);
}
}
void HTMLDocument::captureEvents()
{
}
void HTMLDocument::releaseEvents()
{
}
Tokenizer *HTMLDocument::createTokenizer()
{
bool reportErrors = false;
if (Page* page = this->page())
reportErrors = page->inspectorController()->windowVisible();
return new HTMLTokenizer(this, reportErrors);
}
bool HTMLDocument::childAllowed(Node *newChild)
{
return newChild->hasTagName(htmlTag) || newChild->isCommentNode() || (newChild->nodeType() == DOCUMENT_TYPE_NODE && !doctype());
}
PassRefPtr<Element> HTMLDocument::createElement(const AtomicString& name, ExceptionCode& ec)
{
if (!isValidName(name)) {
ec = INVALID_CHARACTER_ERR;
return 0;
}
AtomicString lowerName = name.string().impl()->isLower() ? name : AtomicString(name.string().lower());
return HTMLElementFactory::createHTMLElement(QualifiedName(nullAtom, lowerName, xhtmlNamespaceURI), this, 0, false);
}
static void addItemToMap(HTMLDocument::NameCountMap& map, const AtomicString& name)
{
if (name.isEmpty())
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 AtomicString& name)
{
if (name.isEmpty())
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 AtomicString& name)
{
addItemToMap(m_namedItemCounts, name);
}
void HTMLDocument::removeNamedItem(const AtomicString &name)
{
removeItemFromMap(m_namedItemCounts, name);
}
void HTMLDocument::addExtraNamedItem(const AtomicString& name)
{
addItemToMap(m_extraNamedItemCounts, name);
}
void HTMLDocument::removeExtraNamedItem(const AtomicString& name)
{
removeItemFromMap(m_extraNamedItemCounts, name);
}
void HTMLDocument::determineParseMode()
{
bool wasInCompatMode = inCompatMode();
DocumentType* docType = doctype();
if (!docType || !equalIgnoringCase(docType->name(), "html"))
setParseMode(Compat);
else if (!doctype()->systemId().isEmpty() && equalIgnoringCase(docType->systemId(), "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd"))
setParseMode(Compat);
else if (docType->publicId().isEmpty())
setParseMode(Strict);
else {
String lowerPubID = docType->publicId().lower();
CString pubIDStr = lowerPubID.latin1();
const PubIDInfo* doctypeEntry = findDoctypeEntry(pubIDStr.data(), pubIDStr.length());
if (!doctypeEntry)
setParseMode(Strict);
else {
switch (docType->systemId().isEmpty() ?
doctypeEntry->mode_if_no_sysid :
doctypeEntry->mode_if_sysid) {
case PubIDInfo::eQuirks3:
case PubIDInfo::eQuirks:
setParseMode(Compat);
break;
case PubIDInfo::eAlmostStandards:
setParseMode(AlmostStrict);
break;
default:
ASSERT(false);
}
}
}
if (inCompatMode() != wasInCompatMode)
updateStyleSelector();
}
void HTMLDocument::clear()
{
}
bool HTMLDocument::isFrameSet() const
{
HTMLElement* bodyElement = body();
return bodyElement && bodyElement->renderer() && bodyElement->hasTagName(framesetTag);
}
}