#include "config.h"
#include "Document.h"
#include "AnimationController.h"
#include "WebKitAnimationEvent.h"
#include "AXObjectCache.h"
#include "CDATASection.h"
#include "CSSHelper.h"
#include "CSSStyleSelector.h"
#include "CSSStyleSheet.h"
#include "CSSValueKeywords.h"
#include "CString.h"
#include "ClassNodeList.h"
#include "Comment.h"
#include "CookieJar.h"
#include "Database.h"
#include "DOMImplementation.h"
#include "DocLoader.h"
#include "DocumentFragment.h"
#include "DocumentLoader.h"
#include "DocumentType.h"
#include "EditingText.h"
#include "Editor.h"
#include "EditorClient.h"
#include "EntityReference.h"
#include "Event.h"
#include "EventHandler.h"
#include "EventListener.h"
#include "EventNames.h"
#include "ExceptionCode.h"
#include "FocusController.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameTree.h"
#include "FrameView.h"
#include "HTMLBodyElement.h"
#include "HTMLDocument.h"
#include "HTMLElementFactory.h"
#include "HTMLFrameOwnerElement.h"
#include "HTMLHeadElement.h"
#include "HTMLImageLoader.h"
#include "HTMLInputElement.h"
#include "HTMLLinkElement.h"
#include "HTMLMapElement.h"
#include "HTMLNameCollection.h"
#include "HTMLNames.h"
#include "HTMLStyleElement.h"
#include "HTMLTitleElement.h"
#include "HTTPParsers.h"
#include "HistoryItem.h"
#include "HitTestRequest.h"
#include "HitTestResult.h"
#include "KeyboardEvent.h"
#include "Logging.h"
#include "MouseEvent.h"
#include "MouseEventWithHitTestResults.h"
#include "MutationEvent.h"
#include "NameNodeList.h"
#include "NodeFilter.h"
#include "NodeIterator.h"
#include "OverflowEvent.h"
#include "Page.h"
#include "PlatformKeyboardEvent.h"
#include "ProcessingInstruction.h"
#include "ProgressEvent.h"
#include "RegisteredEventListener.h"
#include "RegularExpression.h"
#include "RenderArena.h"
#include "RenderView.h"
#include "RenderWidget.h"
#include "SecurityOrigin.h"
#include "SegmentedString.h"
#include "SelectionController.h"
#include "Settings.h"
#include "StringHash.h"
#include "StyleSheetList.h"
#include "SystemTime.h"
#include "TextEvent.h"
#include "TextIterator.h"
#include "TextResourceDecoder.h"
#include "TreeWalker.h"
#include "UIEvent.h"
#include "WheelEvent.h"
#include "XMLHttpRequest.h"
#include "XMLTokenizer.h"
#include "kjs_binding.h"
#include "kjs_proxy.h"
#if ENABLE(DATABASE)
#include "DatabaseThread.h"
#endif
#if ENABLE(XPATH)
#include "XPathEvaluator.h"
#include "XPathExpression.h"
#include "XPathNSResolver.h"
#include "XPathResult.h"
#endif
#if ENABLE(XSLT)
#include "XSLTProcessor.h"
#endif
#if ENABLE(XBL)
#include "XBLBindingManager.h"
#endif
#if ENABLE(SVG)
#include "SVGDocumentExtensions.h"
#include "SVGElementFactory.h"
#include "SVGZoomEvent.h"
#include "SVGStyleElement.h"
#include "TimeScheduler.h"
#endif
#if ENABLE(TOUCH_EVENTS)
#include "GestureEvent.h"
#include "TouchEvent.h"
#endif
#include "Element.h"
#include "Page.h"
#include "HTMLTokenizer.h"
#include "HTMLParserErrorCodes.h"
#include "WKUtilities.h"
#include "CachedImage.h"
using namespace std;
using namespace WTF;
using namespace Unicode;
namespace WebCore {
using namespace EventNames;
using namespace HTMLNames;
const int cLayoutScheduleThreshold = 250;
static HTMLFormElement* const defaultForm = reinterpret_cast<HTMLFormElement*>(1);
static const unsigned PHI = 0x9e3779b9U;
static inline bool isValidNameStart(UChar32 c)
{
if ((c >= 0x02BB && c <= 0x02C1) || c == 0x559 || c == 0x6E5 || c == 0x6E6)
return true;
if (c == ':' || c == '_')
return true;
const uint32_t nameStartMask = Letter_Lowercase | Letter_Uppercase | Letter_Other | Letter_Titlecase | Number_Letter;
if (!(Unicode::category(c) & nameStartMask))
return false;
if (c >= 0xF900 && c < 0xFFFE)
return false;
DecompositionType decompType = decompositionType(c);
if (decompType == DecompositionFont || decompType == DecompositionCompat)
return false;
return true;
}
static inline bool isValidNamePart(UChar32 c)
{
if (isValidNameStart(c))
return true;
if (c == 0x00B7 || c == 0x0387)
return true;
if (c == '-' || c == '.')
return true;
const uint32_t otherNamePartMask = Mark_NonSpacing | Mark_Enclosing | Mark_SpacingCombining | Letter_Modifier | Number_DecimalDigit;
if (!(Unicode::category(c) & otherNamePartMask))
return false;
if (c >= 0xF900 && c < 0xFFFE)
return false;
DecompositionType decompType = decompositionType(c);
if (decompType == DecompositionFont || decompType == DecompositionCompat)
return false;
return true;
}
static Widget* widgetForNode(Node* focusedNode)
{
if (!focusedNode)
return 0;
RenderObject* renderer = focusedNode->renderer();
if (!renderer || !renderer->isWidget())
return 0;
return static_cast<RenderWidget*>(renderer)->widget();
}
static bool acceptsEditingFocus(Node *node)
{
ASSERT(node);
ASSERT(node->isContentEditable());
Node *root = node->rootEditableElement();
Frame* frame = node->document()->frame();
if (!frame || !root)
return false;
return frame->editor()->shouldBeginEditing(rangeOfContents(root).get());
}
DeprecatedPtrList<Document>* Document::changedDocuments = 0;
Document::Document(DOMImplementation* impl, Frame* frame, bool isXHTML)
: ContainerNode(0)
#if ENABLE(TOUCH_EVENTS)
, m_inTouchEventHandling(false)
, m_touchEventRegionsDirty(false)
, m_touchEventsChangedTimer(this, &Document::touchEventsChangedTimerFired)
#endif
, m_implementation(impl)
, m_domtree_version(0)
, m_styleSheets(new StyleSheetList(this))
, m_title("")
, m_titleSetExplicitly(false)
, m_imageLoadEventTimer(this, &Document::imageLoadEventTimerFired)
, m_updateFocusAppearanceTimer(this, &Document::updateFocusAppearanceTimerFired)
#if ENABLE(XSLT)
, m_transformSource(0)
#endif
, m_xmlVersion("1.0")
, m_xmlStandalone(false)
#if ENABLE(XBL)
, m_bindingManager(new XBLBindingManager(this))
#endif
, m_savedRenderer(0)
, m_secureForms(0)
, m_designMode(inherit)
, m_selfOnlyRefCount(0)
#if ENABLE(SVG)
, m_svgExtensions(0)
#endif
#if ENABLE(DASHBOARD_SUPPORT)
, m_hasDashboardRegions(false)
, m_dashboardRegionsDirty(false)
#endif
, m_accessKeyMapValid(false)
, m_createRenderers(true)
, m_inPageCache(false)
, m_isAllowedToLoadLocalResources(false)
, m_useSecureKeyboardEntryWhenActive(false)
, m_isXHTML(isXHTML)
, m_numNodeListCaches(0)
#if ENABLE(DATABASE)
, m_hasOpenDatabases(false)
#endif
#if USE(LOW_BANDWIDTH_DISPLAY)
, m_inLowBandwidthDisplay(false)
#endif
, m_loadComplete(false)
, m_scrollEventListenerCount(0)
, m_totalImageDataSize(0)
, m_animatedImageDataCount(0)
{
m_document.resetSkippingRef(this);
m_printing = false;
m_frame = frame;
m_renderArena = 0;
m_docLoader = new DocLoader(this);
visuallyOrdered = false;
m_bParsing = false;
m_docChanged = false;
m_tokenizer = 0;
m_wellFormed = false;
pMode = Strict;
hMode = XHtml;
m_textColor = Color::black;
m_listenerTypes = 0;
m_inDocument = true;
m_inStyleRecalc = false;
m_closeAfterStyleRecalc = false;
m_usesDescendantRules = false;
m_usesSiblingRules = false;
m_usesFirstLineRules = false;
m_usesFirstLetterRules = false;
m_gotoAnchorNeededAfterStylesheetsLoad = false;
bool matchAuthorAndUserStyles = true;
if (Settings* settings = this->settings())
matchAuthorAndUserStyles = settings->authorAndUserStylesEnabled();
m_styleSelector = new CSSStyleSelector(this, userStyleSheet(), m_styleSheets.get(), m_mappedElementSheet.get(), !inCompatMode(), matchAuthorAndUserStyles);
m_didCalculateStyleSelector = false;
m_pendingStylesheets = 0;
m_ignorePendingStylesheets = false;
m_hasNodesWithPlaceholderStyle = false;
m_pendingSheetLayout = NoLayoutWithPendingSheets;
m_cssTarget = 0;
resetLinkColor();
resetVisitedLinkColor();
resetActiveLinkColor();
m_processingLoadEvent = false;
m_startTime = currentTime();
m_overMinimumLayoutThreshold = false;
initSecurityOrigin();
static int docID = 0;
m_docID = docID++;
m_telephoneNumberParsingEnabled = false;
if (m_frame)
m_telephoneNumberParsingEnabled = m_frame->isTelephoneNumberParsingEnabled();
}
void Document::removedLastRef()
{
ASSERT(!m_deletionHasBegun);
if (m_selfOnlyRefCount) {
DocPtr<Document> guard(this);
m_docType = 0;
m_focusedNode = 0;
m_hoverNode = 0;
m_activeNode = 0;
m_titleElement = 0;
m_documentElement = 0;
m_documentImages.clear();
removeAllChildren();
deleteAllValues(m_markers);
m_markers.clear();
delete m_tokenizer;
m_tokenizer = 0;
#ifndef NDEBUG
m_inRemovedLastRefFunction = false;
#endif
} else {
#ifndef NDEBUG
m_deletionHasBegun = true;
#endif
delete this;
}
}
Document::~Document()
{
ASSERT(!renderer());
ASSERT(!m_inPageCache);
ASSERT(!m_savedRenderer);
removeAllEventListeners();
#if ENABLE(SVG)
delete m_svgExtensions;
#endif
XMLHttpRequest::detachRequests(this);
{
KJS::JSLock lock;
KJS::ScriptInterpreter::forgetAllDOMNodesForDocument(this);
}
if (m_docChanged && changedDocuments)
changedDocuments->remove(this);
delete m_tokenizer;
m_document.resetSkippingRef(0);
delete m_styleSelector;
delete m_docLoader;
if (m_renderArena) {
delete m_renderArena;
m_renderArena = 0;
}
#if ENABLE(XSLT)
xmlFreeDoc((xmlDocPtr)m_transformSource);
#endif
#if ENABLE(XBL)
delete m_bindingManager;
#endif
deleteAllValues(m_markers);
m_decoder = 0;
unsigned count = sizeof(m_nameCollectionInfo) / sizeof(m_nameCollectionInfo[0]);
for (unsigned i = 0; i < count; i++)
deleteAllValues(m_nameCollectionInfo[i]);
#if ENABLE(DATABASE)
if (m_databaseThread) {
ASSERT(m_databaseThread->terminationRequested());
m_databaseThread = 0;
}
#endif
if (m_styleSheets)
m_styleSheets->documentDestroyed();
m_document = 0;
}
void Document::resetLinkColor()
{
m_linkColor = Color(0, 0, 238);
}
void Document::resetVisitedLinkColor()
{
m_visitedLinkColor = Color(85, 26, 139);
}
void Document::resetActiveLinkColor()
{
m_activeLinkColor.setNamedColor("red");
}
void Document::setDocType(PassRefPtr<DocumentType> docType)
{
m_docType = docType;
if (Frame* f = frame())
f->didReceiveDocType();
}
DocumentType *Document::doctype() const
{
return m_docType.get();
}
DOMImplementation* Document::implementation() const
{
return m_implementation.get();
}
void Document::childrenChanged(bool changedByParser)
{
ContainerNode::childrenChanged(changedByParser);
m_documentElement = 0;
}
Element* Document::documentElement() const
{
if (!m_documentElement) {
Node* n = firstChild();
while (n && !n->isElementNode())
n = n->nextSibling();
m_documentElement = static_cast<Element*>(n);
}
return m_documentElement.get();
}
PassRefPtr<Element> Document::createElement(const String &name, ExceptionCode& ec)
{
if (m_isXHTML) {
if (!isValidName(name)) {
ec = INVALID_CHARACTER_ERR;
return 0;
}
return HTMLElementFactory::createHTMLElement(AtomicString(name), this, 0, false);
} else
return createElementNS(nullAtom, name, ec);
}
PassRefPtr<DocumentFragment> Document::createDocumentFragment()
{
return new DocumentFragment(document());
}
PassRefPtr<Text> Document::createTextNode(const String &data)
{
return new Text(this, data);
}
PassRefPtr<Comment> Document::createComment (const String &data)
{
return new Comment(this, data);
}
PassRefPtr<CDATASection> Document::createCDATASection(const String &data, ExceptionCode& ec)
{
if (isHTMLDocument()) {
ec = NOT_SUPPORTED_ERR;
return 0;
}
return new CDATASection(this, data);
}
PassRefPtr<ProcessingInstruction> Document::createProcessingInstruction(const String &target, const String &data, ExceptionCode& ec)
{
if (!isValidName(target)) {
ec = INVALID_CHARACTER_ERR;
return 0;
}
if (isHTMLDocument()) {
ec = NOT_SUPPORTED_ERR;
return 0;
}
return new ProcessingInstruction(this, target, data);
}
PassRefPtr<EntityReference> Document::createEntityReference(const String &name, ExceptionCode& ec)
{
if (!isValidName(name)) {
ec = INVALID_CHARACTER_ERR;
return 0;
}
if (isHTMLDocument()) {
ec = NOT_SUPPORTED_ERR;
return 0;
}
return new EntityReference(this, name);
}
PassRefPtr<EditingText> Document::createEditingTextNode(const String &text)
{
return new EditingText(this, text);
}
PassRefPtr<CSSStyleDeclaration> Document::createCSSStyleDeclaration()
{
return new CSSMutableStyleDeclaration;
}
PassRefPtr<Node> Document::importNode(Node* importedNode, bool deep, ExceptionCode& ec)
{
ec = 0;
if (!importedNode
#if ENABLE(SVG) && ENABLE(DASHBOARD_SUPPORT)
|| (importedNode->isSVGElement() && page() && page()->settings()->usesDashboardBackwardCompatibilityMode())
#endif
) {
ec = NOT_SUPPORTED_ERR;
return 0;
}
switch (importedNode->nodeType()) {
case TEXT_NODE:
return createTextNode(importedNode->nodeValue());
case CDATA_SECTION_NODE:
return createCDATASection(importedNode->nodeValue(), ec);
case ENTITY_REFERENCE_NODE:
return createEntityReference(importedNode->nodeName(), ec);
case PROCESSING_INSTRUCTION_NODE:
return createProcessingInstruction(importedNode->nodeName(), importedNode->nodeValue(), ec);
case COMMENT_NODE:
return createComment(importedNode->nodeValue());
case ELEMENT_NODE: {
Element* oldElement = static_cast<Element*>(importedNode);
RefPtr<Element> newElement = createElementNS(oldElement->namespaceURI(), oldElement->tagQName().toString(), ec);
if (ec)
return 0;
NamedAttrMap* attrs = oldElement->attributes(true);
if (attrs) {
unsigned length = attrs->length();
for (unsigned i = 0; i < length; i++) {
Attribute* attr = attrs->attributeItem(i);
newElement->setAttribute(attr->name(), attr->value().impl(), ec);
if (ec)
return 0;
}
}
newElement->copyNonAttributeProperties(oldElement);
if (deep) {
for (Node* oldChild = oldElement->firstChild(); oldChild; oldChild = oldChild->nextSibling()) {
RefPtr<Node> newChild = importNode(oldChild, true, ec);
if (ec)
return 0;
newElement->appendChild(newChild.release(), ec);
if (ec)
return 0;
}
}
return newElement.release();
}
case ATTRIBUTE_NODE: {
RefPtr<Attr> newAttr = new Attr(0, this, static_cast<Attr*>(importedNode)->attr()->clone());
newAttr->createTextChild();
return newAttr.release();
}
case DOCUMENT_FRAGMENT_NODE: {
DocumentFragment* oldFragment = static_cast<DocumentFragment*>(importedNode);
RefPtr<DocumentFragment> newFragment = createDocumentFragment();
if (deep) {
for (Node* oldChild = oldFragment->firstChild(); oldChild; oldChild = oldChild->nextSibling()) {
RefPtr<Node> newChild = importNode(oldChild, true, ec);
if (ec)
return 0;
newFragment->appendChild(newChild.release(), ec);
if (ec)
return 0;
}
}
return newFragment.release();
}
case ENTITY_NODE:
case NOTATION_NODE:
case DOCUMENT_NODE:
case DOCUMENT_TYPE_NODE:
case XPATH_NAMESPACE_NODE:
break;
}
ec = NOT_SUPPORTED_ERR;
return 0;
}
PassRefPtr<Node> Document::adoptNode(PassRefPtr<Node> source, ExceptionCode& ec)
{
if (!source) {
ec = NOT_SUPPORTED_ERR;
return 0;
}
switch (source->nodeType()) {
case ENTITY_NODE:
case NOTATION_NODE:
case DOCUMENT_NODE:
case DOCUMENT_TYPE_NODE:
case XPATH_NAMESPACE_NODE:
ec = NOT_SUPPORTED_ERR;
return 0;
case ATTRIBUTE_NODE: {
Attr* attr = static_cast<Attr*>(source.get());
if (attr->ownerElement())
attr->ownerElement()->removeAttributeNode(attr, ec);
attr->m_attrWasSpecifiedOrElementHasRareData = true;
break;
}
default:
if (source->parentNode())
source->parentNode()->removeChild(source.get(), ec);
}
for (Node* node = source.get(); node; node = node->traverseNextNode(source.get()))
node->setDocument(this);
return source;
}
PassRefPtr<Element> Document::createElement(const QualifiedName& qName, bool createdByParser, ExceptionCode& ec)
{
RefPtr<Element> e;
if (qName.namespaceURI() == xhtmlNamespaceURI)
e = HTMLElementFactory::createHTMLElement(qName.localName(), this, 0, createdByParser);
#if ENABLE(SVG)
else if (qName.namespaceURI() == SVGNames::svgNamespaceURI)
e = SVGElementFactory::createSVGElement(qName, this, createdByParser);
#endif
if (!e)
e = new Element(qName, document());
if (e && !qName.prefix().isNull()) {
ec = 0;
e->setPrefix(qName.prefix(), ec);
if (ec)
return 0;
}
return e.release();
}
PassRefPtr<Element> Document::createElementNS(const String &_namespaceURI, const String &qualifiedName, ExceptionCode& ec)
{
String prefix, localName;
if (!parseQualifiedName(qualifiedName, prefix, localName)) {
ec = INVALID_CHARACTER_ERR;
return 0;
}
RefPtr<Element> e;
QualifiedName qName = QualifiedName(AtomicString(prefix), AtomicString(localName), AtomicString(_namespaceURI));
return createElement(qName, false, ec);
}
Element *Document::getElementById(const AtomicString& elementId) const
{
if (elementId.length() == 0)
return 0;
Element *element = m_elementsById.get(elementId.impl());
if (element)
return element;
if (m_duplicateIds.contains(elementId.impl())) {
for (Node *n = traverseNextNode(); n != 0; n = n->traverseNextNode()) {
if (n->isElementNode()) {
element = static_cast<Element*>(n);
if (element->hasID() && element->getAttribute(idAttr) == elementId) {
m_duplicateIds.remove(elementId.impl());
m_elementsById.set(elementId.impl(), element);
return element;
}
}
}
ASSERT_NOT_REACHED();
}
return 0;
}
String Document::readyState() const
{
if (Frame* f = frame()) {
if (f->loader()->isComplete())
return "complete";
if (parsing())
return "loading";
return "loaded";
}
return String();
}
String Document::inputEncoding() const
{
if (TextResourceDecoder* d = decoder())
return d->encoding().name();
return String();
}
String Document::defaultCharset() const
{
if (Settings* settings = this->settings())
return settings->defaultTextEncodingName();
return String();
}
void Document::setCharset(const String& charset)
{
if (!decoder())
return;
decoder()->setEncoding(charset, TextResourceDecoder::UserChosenEncoding);
}
void Document::setXMLVersion(const String& version, ExceptionCode& ec)
{
if (!implementation()->hasFeature("XML", String())) {
ec = NOT_SUPPORTED_ERR;
return;
}
m_xmlVersion = version;
}
void Document::setXMLStandalone(bool standalone, ExceptionCode& ec)
{
if (!implementation()->hasFeature("XML", String())) {
ec = NOT_SUPPORTED_ERR;
return;
}
m_xmlStandalone = standalone;
}
String Document::documentURI() const
{
return m_baseURL;
}
void Document::setDocumentURI(const String &uri)
{
m_baseURL = uri.deprecatedString();
}
String Document::baseURI() const
{
return documentURI();
}
Element* Document::elementFromPoint(int x, int y) const
{
if (!renderer())
return 0;
HitTestRequest request(true, true);
HitTestResult result(IntPoint(x, y));
renderer()->layer()->hitTest(request, result);
Node* n = result.innerNode();
while (n && !n->isElementNode())
n = n->parentNode();
if (n)
n = n->shadowAncestorNode();
return static_cast<Element*>(n);
}
void Document::addElementById(const AtomicString& elementId, Element* element)
{
typedef HashMap<AtomicStringImpl*, Element*>::iterator iterator;
if (!m_duplicateIds.contains(elementId.impl())) {
pair<iterator, bool> addResult = m_elementsById.add(elementId.impl(), element);
if (addResult.second)
return;
m_elementsById.remove(addResult.first);
m_duplicateIds.add(elementId.impl());
} else {
iterator cachedItem = m_elementsById.find(elementId.impl());
if (cachedItem != m_elementsById.end()) {
m_elementsById.remove(cachedItem);
m_duplicateIds.add(elementId.impl());
}
}
m_duplicateIds.add(elementId.impl());
}
void Document::removeElementById(const AtomicString& elementId, Element* element)
{
if (m_elementsById.get(elementId.impl()) == element)
m_elementsById.remove(elementId.impl());
else
m_duplicateIds.remove(elementId.impl());
}
Element* Document::getElementByAccessKey(const String& key) const
{
if (key.isEmpty())
return 0;
if (!m_accessKeyMapValid) {
for (Node* n = firstChild(); n; n = n->traverseNextNode()) {
if (!n->isElementNode())
continue;
Element* element = static_cast<Element*>(n);
const AtomicString& accessKey = element->getAttribute(accesskeyAttr);
if (!accessKey.isEmpty())
m_elementsByAccessKey.set(accessKey.impl(), element);
}
m_accessKeyMapValid = true;
}
return m_elementsByAccessKey.get(key.impl());
}
void Document::updateTitle()
{
if (Frame* f = frame())
f->loader()->setTitle(m_title);
}
void Document::setTitle(const String& title, Element* titleElement)
{
if (!titleElement) {
m_titleSetExplicitly = true;
if (!isHTMLDocument())
m_titleElement = 0;
else if (!m_titleElement) {
if (HTMLElement* headElement = head()) {
ExceptionCode ec = 0;
m_titleElement = createElement("title", ec);
ASSERT(!ec);
headElement->appendChild(m_titleElement, ec);
ASSERT(!ec);
}
}
} else if (titleElement != m_titleElement) {
if (m_titleElement || m_titleSetExplicitly)
return;
m_titleElement = titleElement;
}
if (m_title == title)
return;
m_title = title;
updateTitle();
if (m_titleSetExplicitly && m_titleElement && m_titleElement->hasTagName(titleTag))
static_cast<HTMLTitleElement*>(m_titleElement.get())->setText(m_title);
}
void Document::removeTitle(Element* titleElement)
{
if (m_titleElement != titleElement)
return;
m_titleElement = 0;
m_titleSetExplicitly = false;
if (HTMLElement* headElement = head()) {
for (Node* e = headElement->firstChild(); e; e = e->nextSibling())
if (e->hasTagName(titleTag)) {
HTMLTitleElement* titleElement = static_cast<HTMLTitleElement*>(e);
setTitle(titleElement->text(), titleElement);
break;
}
}
if (!m_titleElement && !m_title.isEmpty()) {
m_title = "";
updateTitle();
}
}
String Document::nodeName() const
{
return "#document";
}
Node::NodeType Document::nodeType() const
{
return DOCUMENT_NODE;
}
FrameView* Document::view() const
{
return m_frame ? m_frame->view() : 0;
}
Page* Document::page() const
{
return m_frame ? m_frame->page() : 0;
}
Settings* Document::settings() const
{
return m_frame ? m_frame->settings() : 0;
}
PassRefPtr<Range> Document::createRange()
{
return new Range(this);
}
PassRefPtr<NodeIterator> Document::createNodeIterator(Node* root, unsigned whatToShow,
NodeFilter* filter, bool expandEntityReferences, ExceptionCode& ec)
{
if (!root) {
ec = NOT_SUPPORTED_ERR;
return 0;
}
return new NodeIterator(root, whatToShow, filter, expandEntityReferences);
}
PassRefPtr<TreeWalker> Document::createTreeWalker(Node *root, unsigned whatToShow,
NodeFilter* filter, bool expandEntityReferences, ExceptionCode& ec)
{
if (!root) {
ec = NOT_SUPPORTED_ERR;
return 0;
}
return new TreeWalker(root, whatToShow, filter, expandEntityReferences);
}
void Document::setDocumentChanged(bool b)
{
if (b) {
if (!m_docChanged) {
if (!changedDocuments)
changedDocuments = new DeprecatedPtrList<Document>;
changedDocuments->append(this);
}
if (m_accessKeyMapValid) {
m_accessKeyMapValid = false;
m_elementsByAccessKey.clear();
}
} else {
if (m_docChanged && changedDocuments)
changedDocuments->remove(this);
}
m_docChanged = b;
}
void Document::recalcStyle(StyleChange change)
{
if (frame() && frame()->isPainting()) {
ASSERT(!frame()->isPainting());
return;
}
if (m_inStyleRecalc)
return;
m_inStyleRecalc = true;
suspendPostAttachCallbacks();
ASSERT(!renderer() || renderArena());
if (!renderer() || !renderArena())
goto bail_out;
if (change == Force) {
m_hasNodesWithPlaceholderStyle = false;
RenderStyle* oldStyle = renderer()->style();
if (oldStyle)
oldStyle->ref();
RenderStyle* _style = new (m_renderArena) RenderStyle();
_style->ref();
_style->setDisplay(BLOCK);
_style->setVisuallyOrdered(visuallyOrdered);
FontDescription fontDescription;
fontDescription.setUsePrinterFont(printing());
if (Settings* settings = this->settings()) {
fontDescription.setRenderingMode(settings->fontRenderingMode());
if (printing() && !settings->shouldPrintBackgrounds())
_style->setForceBackgroundsToWhite(true);
const AtomicString& stdfont = settings->standardFontFamily();
if (!stdfont.isEmpty()) {
fontDescription.firstFamily().setFamily(stdfont);
fontDescription.firstFamily().appendFamily(0);
}
fontDescription.setKeywordSize(CSS_VAL_MEDIUM - CSS_VAL_XX_SMALL + 1);
m_styleSelector->setFontSize(fontDescription, m_styleSelector->fontSizeForKeyword(CSS_VAL_MEDIUM, inCompatMode(), false));
}
_style->setFontDescription(fontDescription);
_style->font().update(m_styleSelector->fontSelector());
if (inCompatMode())
_style->setHtmlHacks(true);
StyleChange ch = diff(_style, oldStyle);
if (renderer() && ch != NoChange)
renderer()->setStyle(_style);
if (change != Force)
change = ch;
_style->deref(m_renderArena);
if (oldStyle)
oldStyle->deref(m_renderArena);
}
for (Node* n = firstChild(); n; n = n->nextSibling())
if (change >= Inherit || n->hasChangedChild() || n->changed())
n->recalcStyle(change);
if (changed() && view())
view()->layout();
bail_out:
setChanged(NoStyleChange);
setHasChangedChild(false);
setDocumentChanged(false);
resumePostAttachCallbacks();
m_inStyleRecalc = false;
if (m_closeAfterStyleRecalc) {
m_closeAfterStyleRecalc = false;
implicitClose();
}
}
void Document::updateRendering()
{
if (hasChangedChild())
recalcStyle(NoChange);
#if ENABLE(HW_COMP)
FrameView* v = view();
if (v && renderer() && v->layerUpdatePending())
v->updateLayers();
if (m_frame)
m_frame->animationController()->styleIsSetup();
#endif
}
void Document::updateDocumentsRendering()
{
if (!changedDocuments)
return;
while (Document* doc = changedDocuments->take()) {
doc->m_docChanged = false;
doc->updateRendering();
}
}
void Document::updateLayout()
{
if (Element* oe = ownerElement())
oe->document()->updateLayout();
updateRendering();
FrameView* v = view();
if (v && renderer() && (v->layoutPending() || renderer()->needsLayout()))
v->layout();
}
void Document::updateLayoutIgnorePendingStylesheets()
{
bool oldIgnore = m_ignorePendingStylesheets;
if (!haveStylesheetsLoaded()) {
m_ignorePendingStylesheets = true;
if (body() && !body()->renderer() && m_pendingSheetLayout == NoLayoutWithPendingSheets) {
m_pendingSheetLayout = DidLayoutWithPendingSheets;
updateStyleSelector();
} else if (m_hasNodesWithPlaceholderStyle)
recalcStyle(Force);
}
updateLayout();
m_ignorePendingStylesheets = oldIgnore;
}
void Document::attach()
{
ASSERT(!attached());
ASSERT(!m_inPageCache);
if (!m_renderArena)
m_renderArena = new RenderArena();
setRenderer(new (m_renderArena) RenderView(this, view()));
#if ENABLE(HW_COMP)
{
RenderView* rv = static_cast<RenderView*>(renderer());
rv->wasAttached();
}
#endif
recalcStyle(Force);
RenderObject* render = renderer();
setRenderer(0);
ContainerNode::attach();
setRenderer(render);
}
void Document::detach()
{
ASSERT(attached());
ASSERT(!m_inPageCache);
if (m_focusedNode && m_focusedNode->isElementNode())
frame()->formElementDidBlur(static_cast<Element *>(m_focusedNode.get()));
RenderObject* render = renderer();
#if ENABLE(HW_COMP)
if (render) {
RenderView* rv = static_cast<RenderView*>(render);
rv->willBeDetached();
}
#endif
setRenderer(0);
m_imageLoadEventDispatchSoonList.clear();
m_imageLoadEventDispatchingList.clear();
m_hoverNode = 0;
m_focusedNode = 0;
m_activeNode = 0;
ContainerNode::detach();
if (render)
render->destroy();
if (m_styleSelector)
m_styleSelector->arenaDestroy();
clearFramePointer();
m_textAutoSizedNodes.clear();
if (m_renderArena) {
delete m_renderArena;
m_renderArena = 0;
}
}
void Document::clearFramePointer()
{
m_frame = 0;
}
void Document::removeAllEventListenersFromAllNodes()
{
m_windowEventListeners.clear();
setTouchEventListenersDirty(true);
m_touchEventListeners.clear();
removeAllDisconnectedNodeEventListeners();
for (Node *n = this; n; n = n->traverseNextNode()) {
if (!n->isEventTargetNode())
continue;
EventTargetNodeCast(n)->removeAllEventListeners();
}
}
void Document::registerDisconnectedNodeWithEventListeners(Node* node)
{
m_disconnectedNodesWithEventListeners.add(node);
}
void Document::unregisterDisconnectedNodeWithEventListeners(Node* node)
{
m_disconnectedNodesWithEventListeners.remove(node);
}
void Document::removeAllDisconnectedNodeEventListeners()
{
HashSet<Node*>::iterator end = m_disconnectedNodesWithEventListeners.end();
for (HashSet<Node*>::iterator i = m_disconnectedNodesWithEventListeners.begin(); i != end; ++i)
EventTargetNodeCast(*i)->removeAllEventListeners();
m_disconnectedNodesWithEventListeners.clear();
}
void Document::setVisuallyOrdered()
{
visuallyOrdered = true;
if (renderer())
renderer()->style()->setVisuallyOrdered(true);
}
Tokenizer* Document::createTokenizer()
{
return new XMLTokenizer(this, view());
}
void Document::open()
{
if (Document *parent = parentDocument()) {
if (m_url.isEmpty() || m_url == "about:blank")
setURL(parent->baseURL());
if (m_baseURL.isEmpty() || m_baseURL == "about:blank")
setBaseURL(parent->baseURL());
}
if (m_frame) {
if (m_frame->loader()->isLoadingMainResource() || (tokenizer() && tokenizer()->executingScript()))
return;
if (m_frame->loader()->state() == FrameStateProvisional)
m_frame->loader()->stopAllLoaders();
}
implicitOpen();
if (m_frame)
m_frame->loader()->didExplicitOpen();
}
void Document::cancelParsing()
{
if (m_tokenizer) {
delete m_tokenizer;
m_tokenizer = 0;
close();
}
}
void Document::implicitOpen()
{
cancelParsing();
clear();
m_tokenizer = createTokenizer();
setParsing(true);
}
HTMLElement* Document::body()
{
Node* de = documentElement();
if (!de)
return 0;
Node* body = 0;
for (Node* i = de->firstChild(); i; i = i->nextSibling()) {
if (i->hasTagName(framesetTag))
return static_cast<HTMLElement*>(i);
if (i->hasTagName(bodyTag))
body = i;
}
return static_cast<HTMLElement*>(body);
}
void Document::setBody(PassRefPtr<HTMLElement> newBody, ExceptionCode& ec)
{
if (!newBody) {
ec = HIERARCHY_REQUEST_ERR;
return;
}
HTMLElement* b = body();
if (!b)
documentElement()->appendChild(newBody, ec);
else
documentElement()->replaceChild(newBody, b, ec);
}
HTMLHeadElement* Document::head()
{
Node* de = documentElement();
if (!de)
return 0;
for (Node* e = de->firstChild(); e; e = e->nextSibling())
if (e->hasTagName(headTag))
return static_cast<HTMLHeadElement*>(e);
return 0;
}
void Document::close()
{
Frame* frame = this->frame();
if (frame) {
FrameLoader* frameLoader = frame->loader();
frameLoader->endIfNotLoadingMainResource();
frameLoader->checkCompleted();
} else {
implicitClose();
}
}
void Document::implicitClose()
{
if (m_inStyleRecalc) {
m_closeAfterStyleRecalc = true;
return;
}
bool wasLocationChangePending = frame() && frame()->loader()->isScheduledLocationChangePending();
bool doload = !parsing() && m_tokenizer && !m_processingLoadEvent && !wasLocationChangePending;
if (!doload)
return;
m_processingLoadEvent = true;
m_wellFormed = m_tokenizer && m_tokenizer->wellFormed();
delete m_tokenizer;
m_tokenizer = 0;
m_docLoader->clearPreloads();
if (!this->body() && isHTMLDocument()) {
if (Node* documentElement = this->documentElement()) {
ExceptionCode ec = 0;
documentElement->appendChild(new HTMLBodyElement(this), ec);
ASSERT(!ec);
}
}
m_loadComplete = true;
if (frame())
frame()->animationController()->resumeAnimations(this);
dispatchImageLoadEventsNow();
this->dispatchWindowEvent(loadEvent, false, false);
if (Frame* f = frame())
f->loader()->handledOnloadEvents();
#ifdef INSTRUMENT_LAYOUT_SCHEDULING
if (!ownerElement())
printf("onload fired at %d\n", elapsedTime());
#endif
m_processingLoadEvent = false;
if (!frame())
return;
if (frame()->loader()->isScheduledLocationChangePending() && elapsedTime() < frame()->layoutInterval())
{
view()->unscheduleRelayout();
#if ENABLE(HW_COMP)
view()->unscheduleLayerUpdate();
#endif // ENABLE(HW_COMP)
return;
}
frame()->loader()->checkCallImplicitClose();
if (!ownerElement() || (ownerElement()->renderer() && !ownerElement()->renderer()->needsLayout())) {
updateRendering();
if (view() && renderer() && (!renderer()->firstChild() || renderer()->needsLayout()))
view()->layout();
#if !PLATFORM(MAC)
if (view() && !ownerElement())
view()->update();
#endif
}
#if ENABLE(SVG)
if (svgExtensions())
accessSVGExtensions()->startAnimations();
#endif
}
void Document::setParsing(bool b)
{
m_bParsing = b;
if (!m_bParsing && view())
view()->scheduleRelayout();
#ifdef INSTRUMENT_LAYOUT_SCHEDULING
if (!ownerElement() && !m_bParsing)
printf("Parsing finished at %d\n", elapsedTime());
#endif
}
bool Document::shouldScheduleLayout()
{
return (renderer() && renderer()->needsLayout() && haveStylesheetsLoaded() &&
documentElement() && documentElement()->renderer() &&
(!documentElement()->hasTagName(htmlTag) || body()));
}
int Document::minimumLayoutDelay()
{
if (m_overMinimumLayoutThreshold)
return 0;
int cLayoutScheduleThreshold = frame()->layoutInterval();
int elapsed = elapsedTime();
m_overMinimumLayoutThreshold = elapsed > cLayoutScheduleThreshold;
return max(0, cLayoutScheduleThreshold - elapsed);
}
int Document::elapsedTime() const
{
return static_cast<int>((currentTime() - m_startTime) * 1000);
}
void Document::write(const String& text)
{
#ifdef INSTRUMENT_LAYOUT_SCHEDULING
if (!ownerElement())
printf("Beginning a document.write at %d\n", elapsedTime());
#endif
if (!m_tokenizer) {
open();
ASSERT(m_tokenizer);
if (!m_tokenizer)
return;
write("<html>");
}
m_tokenizer->write(text, false);
#ifdef INSTRUMENT_LAYOUT_SCHEDULING
if (!ownerElement())
printf("Ending a document.write at %d\n", elapsedTime());
#endif
}
void Document::writeln(const String& text)
{
write(text);
write("\n");
}
void Document::finishParsing()
{
#ifdef INSTRUMENT_LAYOUT_SCHEDULING
if (!ownerElement())
printf("Received all data at %d\n", elapsedTime());
#endif
if (m_tokenizer)
m_tokenizer->finish();
}
void Document::clear()
{
delete m_tokenizer;
m_tokenizer = 0;
removeChildren();
m_windowEventListeners.clear();
setTouchEventListenersDirty(true);
m_touchEventListeners.clear();
}
void Document::setURL(const DeprecatedString& url)
{
if (url == m_url)
return;
m_url = url;
if (m_styleSelector)
m_styleSelector->setEncodedURL(m_url);
m_isAllowedToLoadLocalResources = shouldBeAllowedToLoadLocalResources();
}
bool Document::shouldBeAllowedToLoadLocalResources() const
{
if (FrameLoader::shouldTreatURLAsLocal(m_url))
return true;
Frame* frame = this->frame();
if (!frame)
return false;
DocumentLoader* documentLoader = frame->loader()->documentLoader();
if (!documentLoader)
return false;
if (m_url == "about:blank" && frame->loader()->opener() && frame->loader()->opener()->document()->isAllowedToLoadLocalResources())
return true;
return documentLoader->substituteData().isValid();
}
void Document::setBaseURL(const DeprecatedString& baseURL)
{
m_baseURL = baseURL;
if (m_elemSheet)
m_elemSheet->setHref(m_baseURL);
}
void Document::setCSSStyleSheet(const String &url, const String& charset, const String &sheet)
{
m_sheet = new CSSStyleSheet(this, url, charset);
m_sheet->parseString(sheet);
updateStyleSelector();
}
#if FRAME_LOADS_USER_STYLESHEET
void Document::setUserStyleSheet(const String& sheet)
{
if (m_usersheet != sheet) {
m_usersheet = sheet;
updateStyleSelector();
}
}
#endif
String Document::userStyleSheet() const
{
#if FRAME_LOADS_USER_STYLESHEET
return m_usersheet;
#else
Page* page = this->page();
if (!page)
return String();
return page->userStyleSheet();
#endif
}
CSSStyleSheet* Document::elementSheet()
{
if (!m_elemSheet)
m_elemSheet = new CSSStyleSheet(this, baseURL());
return m_elemSheet.get();
}
CSSStyleSheet* Document::mappedElementSheet()
{
if (!m_mappedElementSheet)
m_mappedElementSheet = new CSSStyleSheet(this, baseURL());
return m_mappedElementSheet.get();
}
void Document::determineParseMode(const String&)
{
pMode = Strict;
hMode = XHtml;
}
static Node* nextNodeWithExactTabIndex(Node* start, int tabIndex, KeyboardEvent* event)
{
for (Node* n = start; n; n = n->traverseNextNode())
if (n->isKeyboardFocusable(event) && n->tabIndex() == tabIndex)
return n;
return 0;
}
static Node* previousNodeWithExactTabIndex(Node* start, int tabIndex, KeyboardEvent* event)
{
for (Node* n = start; n; n = n->traversePreviousNode())
if (n->isKeyboardFocusable(event) && n->tabIndex() == tabIndex)
return n;
return 0;
}
static Node* nextNodeWithGreaterTabIndex(Node* start, int tabIndex, KeyboardEvent* event)
{
int winningTabIndex = SHRT_MAX + 1;
Node* winner = 0;
for (Node* n = start; n; n = n->traverseNextNode())
if (n->isKeyboardFocusable(event) && n->tabIndex() > tabIndex && n->tabIndex() < winningTabIndex) {
winner = n;
winningTabIndex = n->tabIndex();
}
return winner;
}
static Node* previousNodeWithLowerTabIndex(Node* start, int tabIndex, KeyboardEvent* event)
{
int winningTabIndex = 0;
Node* winner = 0;
for (Node* n = start; n; n = n->traversePreviousNode())
if (n->isKeyboardFocusable(event) && n->tabIndex() < tabIndex && n->tabIndex() > winningTabIndex) {
winner = n;
winningTabIndex = n->tabIndex();
}
return winner;
}
Node* Document::nextFocusableNode(Node* start, KeyboardEvent* event)
{
if (start) {
if (Node* winner = nextNodeWithExactTabIndex(start->traverseNextNode(), start->tabIndex(), event))
return winner;
if (start->tabIndex() == 0)
return 0;
}
if (Node* winner = nextNodeWithGreaterTabIndex(this, start ? start->tabIndex() : 0, event))
return winner;
return nextNodeWithExactTabIndex(this, 0, event);
}
Node* Document::previousFocusableNode(Node* start, KeyboardEvent* event)
{
Node* last;
for (last = this; last->lastChild(); last = last->lastChild())
;
Node* startingNode;
int startingTabIndex;
if (start) {
startingNode = start->traversePreviousNode();
startingTabIndex = start->tabIndex();
} else {
startingNode = last;
startingTabIndex = 0;
}
if (Node* winner = previousNodeWithExactTabIndex(startingNode, startingTabIndex, event))
return winner;
startingTabIndex = (start && start->tabIndex()) ? start->tabIndex() : SHRT_MAX;
return previousNodeWithLowerTabIndex(last, startingTabIndex, event);
}
int Document::nodeAbsIndex(Node *node)
{
ASSERT(node->document() == this);
int absIndex = 0;
for (Node *n = node; n && n != this; n = n->traversePreviousNode())
absIndex++;
return absIndex;
}
Node *Document::nodeWithAbsIndex(int absIndex)
{
Node *n = this;
for (int i = 0; n && (i < absIndex); i++) {
n = n->traverseNextNode();
}
return n;
}
void Document::processHttpEquiv(const String &equiv, const String &content)
{
ASSERT(!equiv.isNull() && !content.isNull());
Frame *frame = this->frame();
if (equalIgnoringCase(equiv, "default-style")) {
m_selectedStylesheetSet = content;
m_preferredStylesheetSet = content;
updateStyleSelector();
} else if (equalIgnoringCase(equiv, "refresh")) {
double delay;
String url;
if (frame && parseHTTPRefresh(content, true, delay, url)) {
if (url.isEmpty())
url = frame->loader()->url().string();
else
url = completeURL(url);
frame->loader()->scheduleHTTPRedirection(delay, url);
}
} else if (equalIgnoringCase(equiv, "set-cookie")) {
if (isHTMLDocument())
static_cast<HTMLDocument*>(this)->setCookie(content);
} else if (equalIgnoringCase(equiv, "content-language"))
setContentLanguage(content);
}
static void reportViewportWarning(Document * aDocument, ViewportErrorCode anErrorCode, const String& aReplacement)
{
Tokenizer * tokenizer = aDocument->tokenizer();
Frame * frame = aDocument->frame();
if (!frame)
return;
Page * page = frame->page();
if (page) {
String message = viewportErrorMessageTemplate(anErrorCode);
message.replace("%replacement", aReplacement);
page->chrome()->addMessageToConsole(HTMLMessageSource, viewportErrorMessageLevel(anErrorCode), message, tokenizer ? tokenizer->lineNumber() + 1 : 0, aDocument->url());
}
}
static void setViewportFeature(const String& keyString, const String& valueString, Document * aDocument, void* userData)
{
ViewportArguments &arguments = *((ViewportArguments*)userData);
float value = -1;
bool didUseConstants = false;
if (equalIgnoringCase(valueString, "yes"))
value = 1;
else if (equalIgnoringCase(valueString, "device-width")) {
didUseConstants = true;
value = GSMainScreenSize().width;
}
else if (equalIgnoringCase(valueString, "device-height")) {
didUseConstants = true;
value = GSMainScreenSize().height;
}
else if (equalIgnoringCase(valueString, "default"))
value = -2;
else if (valueString.length() != 0) value = valueString.deprecatedString().toDouble();
if (keyString == "initial-scale")
arguments.initialScale = value;
else if (keyString == "minimum-scale")
arguments.minimumScale = value;
else if (keyString == "maximum-scale")
{
arguments.maximumScale = value;
if (value > 10.0)
reportViewportWarning(aDocument, MaximumScaleTooLargeError, keyString);
}
else if (keyString == "user-scalable")
arguments.userScalable = value;
else if (keyString == "width") {
if (value == GSMainScreenSize().width && !didUseConstants)
reportViewportWarning(aDocument, DeviceWidthShouldBeUsedWarning, keyString);
else if (value == GSMainScreenSize().height && !didUseConstants)
reportViewportWarning(aDocument, DeviceHeightShouldBeUsedWarning, keyString);
arguments.width = value;
}
else if (keyString == "height") {
if (value == GSMainScreenSize().width && !didUseConstants)
reportViewportWarning(aDocument, DeviceWidthShouldBeUsedWarning, keyString);
else if (value == GSMainScreenSize().height && !didUseConstants)
reportViewportWarning(aDocument, DeviceHeightShouldBeUsedWarning, keyString);
arguments.height = value;
}
else
reportViewportWarning(aDocument, UnrecognizedViewportArgumentError, keyString);
}
static void setParserFeature(const String& keyString, const String& valueString, Document * aDocument, void* userData)
{
Document* document = (Document *)userData;
if ((keyString == "telephone") && equalIgnoringCase(valueString, "no")) {
document->setTelephoneNumberParsingEnabled(false);
}
}
static bool isSeparator(::UChar c)
{
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '=' || c == ',' || c == '\0';
}
void Document::processArguments(const String & features, void *userData, void (*argumentsCallback)(const String& keyString, const String& valueString, Document * aDocument, void* userData))
{
int keyBegin, keyEnd;
int valueBegin, valueEnd;
int i = 0;
int length = features.length();
String buffer = features.lower();
while (i < length) {
while (isSeparator(buffer[i])) {
if (i >= length)
break;
i++;
}
keyBegin = i;
while (!isSeparator(buffer[i]))
i++;
keyEnd = i;
while (buffer[i] != '=') {
if (buffer[i] == ',' || i >= length)
break;
i++;
}
while (isSeparator(buffer[i])) {
if (buffer[i] == ',' || i >= length)
break;
i++;
}
valueBegin = i;
while (!isSeparator(buffer[i]))
i++;
valueEnd = i;
assert(i <= length);
String keyString(buffer.substring(keyBegin, keyEnd - keyBegin));
String valueString(buffer.substring(valueBegin, valueEnd - valueBegin));
argumentsCallback(keyString, valueString, this, userData);
}
}
void Document::processViewport(const String & features)
{
assert(!features.isNull());
Frame *frame = this->frame();
if (!frame)
return;
ViewportArguments arguments;
processArguments(features, &arguments, setViewportFeature);
frame->didReceiveViewportArguments(arguments);
}
void Document::processFormatDetection(const String & features)
{
assert(!features.isNull());
processArguments(features, this, setParserFeature);
}
MouseEventWithHitTestResults Document::prepareMouseEvent(const HitTestRequest& request, const IntPoint& documentPoint, const PlatformMouseEvent& event)
{
ASSERT(!renderer() || renderer()->isRenderView());
if (!renderer())
return MouseEventWithHitTestResults(event, HitTestResult(IntPoint()));
HitTestResult result(documentPoint);
renderer()->layer()->hitTest(request, result);
if (!request.readonly)
updateRendering();
return MouseEventWithHitTestResults(event, result);
}
bool Document::childTypeAllowed(NodeType type)
{
switch (type) {
case ATTRIBUTE_NODE:
case CDATA_SECTION_NODE:
case DOCUMENT_FRAGMENT_NODE:
case DOCUMENT_NODE:
case ENTITY_NODE:
case ENTITY_REFERENCE_NODE:
case NOTATION_NODE:
case TEXT_NODE:
case XPATH_NAMESPACE_NODE:
return false;
case COMMENT_NODE:
case PROCESSING_INSTRUCTION_NODE:
return true;
case DOCUMENT_TYPE_NODE:
case ELEMENT_NODE:
for (Node* c = firstChild(); c; c = c->nextSibling())
if (c->nodeType() == type)
return false;
return true;
}
return false;
}
bool Document::canReplaceChild(Node* newChild, Node* oldChild)
{
if (!oldChild)
return true;
if (oldChild->nodeType() == newChild->nodeType())
return true;
int numDoctypes = 0;
int numElements = 0;
for (Node* c = firstChild(); c; c = c->nextSibling()) {
if (c == oldChild)
continue;
switch (c->nodeType()) {
case DOCUMENT_TYPE_NODE:
numDoctypes++;
break;
case ELEMENT_NODE:
numElements++;
break;
default:
break;
}
}
if (newChild->nodeType() == DOCUMENT_FRAGMENT_NODE) {
for (Node* c = firstChild(); c; c = c->nextSibling()) {
switch (c->nodeType()) {
case ATTRIBUTE_NODE:
case CDATA_SECTION_NODE:
case DOCUMENT_FRAGMENT_NODE:
case DOCUMENT_NODE:
case ENTITY_NODE:
case ENTITY_REFERENCE_NODE:
case NOTATION_NODE:
case TEXT_NODE:
case XPATH_NAMESPACE_NODE:
return false;
case COMMENT_NODE:
case PROCESSING_INSTRUCTION_NODE:
break;
case DOCUMENT_TYPE_NODE:
numDoctypes++;
break;
case ELEMENT_NODE:
numElements++;
break;
}
}
} else {
switch (newChild->nodeType()) {
case ATTRIBUTE_NODE:
case CDATA_SECTION_NODE:
case DOCUMENT_FRAGMENT_NODE:
case DOCUMENT_NODE:
case ENTITY_NODE:
case ENTITY_REFERENCE_NODE:
case NOTATION_NODE:
case TEXT_NODE:
case XPATH_NAMESPACE_NODE:
return false;
case COMMENT_NODE:
case PROCESSING_INSTRUCTION_NODE:
return true;
case DOCUMENT_TYPE_NODE:
numDoctypes++;
break;
case ELEMENT_NODE:
numElements++;
break;
}
}
if (numElements > 1 || numDoctypes > 1)
return false;
return true;
}
PassRefPtr<Node> Document::cloneNode(bool )
{
return 0;
}
StyleSheetList* Document::styleSheets()
{
return m_styleSheets.get();
}
String Document::preferredStylesheetSet() const
{
return m_preferredStylesheetSet;
}
String Document::selectedStylesheetSet() const
{
return m_selectedStylesheetSet;
}
void Document::setSelectedStylesheetSet(const String& aString)
{
m_selectedStylesheetSet = aString;
updateStyleSelector();
if (renderer())
renderer()->repaint();
}
void Document::removePendingSheet()
{
ASSERT(m_pendingStylesheets > 0);
m_pendingStylesheets--;
#ifdef INSTRUMENT_LAYOUT_SCHEDULING
if (!ownerElement())
printf("Stylesheet loaded at time %d. %d stylesheets still remain.\n", elapsedTime(), m_pendingStylesheets);
#endif
updateStyleSelector();
if (!m_pendingStylesheets && m_tokenizer)
m_tokenizer->executeScriptsWaitingForStylesheets();
if (!m_pendingStylesheets && m_gotoAnchorNeededAfterStylesheetsLoad && m_frame)
m_frame->loader()->gotoAnchor();
}
void Document::updateStyleSelector()
{
if (!m_didCalculateStyleSelector && !haveStylesheetsLoaded())
return;
if (didLayoutWithPendingStylesheets() && m_pendingStylesheets <= 0) {
m_pendingSheetLayout = IgnoreLayoutWithPendingSheets;
if (renderer())
renderer()->repaint();
}
#ifdef INSTRUMENT_LAYOUT_SCHEDULING
if (!ownerElement())
printf("Beginning update of style selector at time %d.\n", elapsedTime());
#endif
recalcStyleSelector();
recalcStyle(Force);
#ifdef INSTRUMENT_LAYOUT_SCHEDULING
if (!ownerElement())
printf("Finished update of style selector at time %d\n", elapsedTime());
#endif
if (renderer()) {
renderer()->setNeedsLayoutAndPrefWidthsRecalc();
if (view())
view()->scheduleRelayout();
}
}
void Document::recalcStyleSelector()
{
if (!renderer() || !attached())
return;
DeprecatedPtrList<StyleSheet> oldStyleSheets = m_styleSheets->styleSheets;
m_styleSheets->styleSheets.clear();
bool matchAuthorAndUserStyles = true;
if (Settings* settings = this->settings())
matchAuthorAndUserStyles = settings->authorAndUserStylesEnabled();
Node* n = matchAuthorAndUserStyles ? this : 0;
for ( ; n; n = n->traverseNextNode()) {
StyleSheet* sheet = 0;
if (n->nodeType() == PROCESSING_INSTRUCTION_NODE) {
ProcessingInstruction* pi = static_cast<ProcessingInstruction*>(n);
sheet = pi->sheet();
#if ENABLE(XSLT)
if (pi->isXSL() && !transformSourceDocument()) {
if (!parsing())
applyXSLTransform(pi);
return;
}
#endif
if (!sheet && !pi->localHref().isEmpty()) {
Element* elem = getElementById(pi->localHref().impl());
if (elem) {
String sheetText("");
for (Node* c = elem->firstChild(); c; c = c->nextSibling()) {
if (c->nodeType() == TEXT_NODE || c->nodeType() == CDATA_SECTION_NODE)
sheetText += c->nodeValue();
}
CSSStyleSheet* cssSheet = new CSSStyleSheet(this);
cssSheet->parseString(sheetText);
pi->setCSSStyleSheet(cssSheet);
sheet = cssSheet;
}
}
} else if (n->isHTMLElement() && (n->hasTagName(linkTag) || n->hasTagName(styleTag))
#if ENABLE(SVG)
|| (n->isSVGElement() && n->hasTagName(SVGNames::styleTag))
#endif
) {
Element* e = static_cast<Element*>(n);
AtomicString title = e->getAttribute(titleAttr);
bool enabledViaScript = false;
if (e->hasLocalName(linkTag)) {
HTMLLinkElement* l = static_cast<HTMLLinkElement*>(n);
if (l->isDisabled())
continue;
enabledViaScript = l->isEnabledViaScript();
if (l->isLoading()) {
if (!enabledViaScript && !title.isEmpty() && m_preferredStylesheetSet.isEmpty()) {
const AtomicString& rel = e->getAttribute(relAttr);
if (!rel.domString().contains("alternate")) {
m_preferredStylesheetSet = title;
m_selectedStylesheetSet = title;
}
}
continue;
}
if (!l->sheet())
title = nullAtom;
}
#if ENABLE(SVG)
if (n->isSVGElement() && n->hasTagName(SVGNames::styleTag))
sheet = static_cast<SVGStyleElement*>(n)->sheet();
else
#endif
if (e->hasLocalName(linkTag))
sheet = static_cast<HTMLLinkElement*>(n)->sheet();
else
sheet = static_cast<HTMLStyleElement*>(n)->sheet();
if (!enabledViaScript && !title.isEmpty()) {
if (m_preferredStylesheetSet.isEmpty()) {
AtomicString rel = e->getAttribute(relAttr);
if (e->hasLocalName(styleTag) || !rel.contains("alternate"))
m_preferredStylesheetSet = m_selectedStylesheetSet = title;
}
if (title != m_preferredStylesheetSet)
sheet = 0;
#if ENABLE(SVG)
if (!n->isHTMLElement())
title = title.deprecatedString().replace('&', "&&");
#endif
}
}
if (sheet) {
sheet->ref();
m_styleSheets->styleSheets.append(sheet);
}
if (isHTMLDocument() && n->hasTagName(bodyTag))
break;
}
DeprecatedPtrListIterator<StyleSheet> it(oldStyleSheets);
for (; it.current(); ++it)
it.current()->deref();
delete m_styleSelector;
m_styleSelector = new CSSStyleSelector(this, userStyleSheet(), m_styleSheets.get(), m_mappedElementSheet.get(), !inCompatMode(), matchAuthorAndUserStyles);
m_styleSelector->setEncodedURL(m_url);
m_didCalculateStyleSelector = true;
}
void Document::setHoverNode(PassRefPtr<Node> newHoverNode)
{
m_hoverNode = newHoverNode;
}
void Document::setActiveNode(PassRefPtr<Node> newActiveNode)
{
m_activeNode = newActiveNode;
}
void Document::focusedNodeRemoved()
{
setFocusedNode(0);
}
void Document::removeFocusedNodeOfSubtree(Node* node, bool amongChildrenOnly)
{
if (!m_focusedNode || this->inPageCache()) return;
bool nodeInSubtree = false;
if (amongChildrenOnly)
nodeInSubtree = m_focusedNode->isDescendantOf(node);
else
nodeInSubtree = (m_focusedNode == node) || m_focusedNode->isDescendantOf(node);
if (nodeInSubtree)
document()->focusedNodeRemoved();
}
void Document::hoveredNodeDetached(Node* node)
{
if (!m_hoverNode || (node != m_hoverNode && (!m_hoverNode->isTextNode() || node != m_hoverNode->parent())))
return;
m_hoverNode = node->parent();
while (m_hoverNode && !m_hoverNode->renderer())
m_hoverNode = m_hoverNode->parent();
if (frame())
frame()->eventHandler()->scheduleHoverStateUpdate();
}
void Document::activeChainNodeDetached(Node* node)
{
if (!m_activeNode || (node != m_activeNode && (!m_activeNode->isTextNode() || node != m_activeNode->parent())))
return;
m_activeNode = node->parent();
while (m_activeNode && !m_activeNode->renderer())
m_activeNode = m_activeNode->parent();
}
#if ENABLE(DASHBOARD_SUPPORT)
const Vector<DashboardRegionValue>& Document::dashboardRegions() const
{
return m_dashboardRegions;
}
void Document::setDashboardRegions(const Vector<DashboardRegionValue>& regions)
{
m_dashboardRegions = regions;
setDashboardRegionsDirty(false);
}
#endif
bool Document::setFocusedNode(PassRefPtr<Node> newFocusedNode)
{
if (newFocusedNode && (newFocusedNode->document() != this))
return true;
if (m_focusedNode == newFocusedNode)
return true;
if (m_inPageCache)
return false;
bool focusChangeBlocked = false;
RefPtr<Node> oldFocusedNode = m_focusedNode;
m_focusedNode = 0;
if (oldFocusedNode && !oldFocusedNode->m_inDetach) {
if (oldFocusedNode->active())
oldFocusedNode->setActive(false);
oldFocusedNode->setFocus(false);
RenderObject *r = static_cast<RenderObject*>(oldFocusedNode.get()->renderer());
if (r && (r->isTextArea() || r->isTextField()) && r->isEdited()) {
EventTargetNodeCast(oldFocusedNode.get())->dispatchHTMLEvent(changeEvent, true, false);
if ((r = static_cast<RenderObject*>(oldFocusedNode.get()->renderer())))
r->setEdited(false);
}
EventTargetNodeCast(oldFocusedNode.get())->dispatchBlurEvent();
if (m_focusedNode) {
focusChangeBlocked = true;
newFocusedNode = 0;
}
EventTargetNodeCast(oldFocusedNode.get())->dispatchUIEvent(DOMFocusOutEvent);
if (m_focusedNode) {
focusChangeBlocked = true;
newFocusedNode = 0;
}
if ((oldFocusedNode.get() == this) && oldFocusedNode->hasOneRef())
return true;
if (oldFocusedNode.get() == oldFocusedNode->rootEditableElement())
frame()->editor()->didEndEditing();
}
if (newFocusedNode) {
if (newFocusedNode == newFocusedNode->rootEditableElement() && !acceptsEditingFocus(newFocusedNode.get())) {
focusChangeBlocked = true;
goto SetFocusedNodeDone;
}
m_focusedNode = newFocusedNode.get();
EventTargetNodeCast(m_focusedNode.get())->dispatchFocusEvent();
if (m_focusedNode != newFocusedNode) {
focusChangeBlocked = true;
goto SetFocusedNodeDone;
}
EventTargetNodeCast(m_focusedNode.get())->dispatchUIEvent(DOMFocusInEvent);
if (m_focusedNode != newFocusedNode) {
focusChangeBlocked = true;
goto SetFocusedNodeDone;
}
m_focusedNode->setFocus();
if (m_focusedNode.get() == m_focusedNode->rootEditableElement())
frame()->editor()->didBeginEditing();
if (view()) {
Widget *focusWidget = widgetForNode(m_focusedNode.get());
if (focusWidget) {
updateLayout();
focusWidget = widgetForNode(m_focusedNode.get());
}
if (focusWidget)
focusWidget->setFocus();
else
view()->setFocus();
}
}
SetFocusedNodeDone:
updateRendering();
return !focusChangeBlocked;
}
void Document::setCSSTarget(Node* n)
{
if (m_cssTarget)
m_cssTarget->setChanged();
m_cssTarget = n;
if (n)
n->setChanged();
}
Node* Document::getCSSTarget() const
{
return m_cssTarget;
}
void Document::attachNodeIterator(NodeIterator *ni)
{
m_nodeIterators.add(ni);
}
void Document::detachNodeIterator(NodeIterator *ni)
{
m_nodeIterators.remove(ni);
}
void Document::notifyBeforeNodeRemoval(Node *n)
{
if (Frame* f = frame()) {
f->selectionController()->nodeWillBeRemoved(n);
f->dragCaretController()->nodeWillBeRemoved(n);
}
HashSet<NodeIterator*>::const_iterator end = m_nodeIterators.end();
for (HashSet<NodeIterator*>::const_iterator it = m_nodeIterators.begin(); it != end; ++it)
(*it)->notifyBeforeNodeRemoval(n);
}
DOMWindow* Document::defaultView() const
{
if (!frame())
return 0;
return frame()->domWindow();
}
PassRefPtr<Event> Document::createEvent(const String &eventType, ExceptionCode& ec)
{
if (eventType == "UIEvents" || eventType == "UIEvent")
return new UIEvent;
if (eventType == "MouseEvents" || eventType == "MouseEvent")
return new MouseEvent;
if (eventType == "MutationEvents" || eventType == "MutationEvent")
return new MutationEvent;
if (eventType == "KeyboardEvents" || eventType == "KeyboardEvent")
return new KeyboardEvent;
if (eventType == "HTMLEvents" || eventType == "Event" || eventType == "Events")
return new Event;
if (eventType == "ProgressEvent")
return new ProgressEvent;
if (eventType == "TextEvent")
return new TextEvent;
if (eventType == "OverflowEvent")
return new OverflowEvent;
if (eventType == "WheelEvent")
return new WheelEvent;
#if ENABLE(SVG)
if (eventType == "SVGEvents")
return new Event;
if (eventType == "SVGZoomEvents")
return new SVGZoomEvent;
#endif
#if ENABLE(TOUCH_EVENTS)
if (eventType == "TouchEvent")
return new TouchEvent;
if (eventType == "GestureEvent")
return new GestureEvent;
#endif
if (eventType == "WebKitAnimationEvent")
return new WebKitAnimationEvent;
ec = NOT_SUPPORTED_ERR;
return 0;
}
CSSStyleDeclaration *Document::getOverrideStyle(Element *, const String &)
{
return 0; }
void Document::handleWindowEvent(Event *evt, bool useCapture)
{
if (m_windowEventListeners.isEmpty())
return;
RegisteredEventListenerList listenersCopy = m_windowEventListeners;
RegisteredEventListenerList::iterator it = listenersCopy.begin();
for (; it != listenersCopy.end(); ++it)
if ((*it)->eventType() == evt->type() && (*it)->useCapture() == useCapture && !(*it)->removed())
(*it)->listener()->handleEvent(evt, true);
}
void Document::setHTMLWindowEventListener(const AtomicString &eventType, PassRefPtr<EventListener> listener)
{
removeHTMLWindowEventListener(eventType);
if (listener)
addWindowEventListener(eventType, listener, false);
}
EventListener *Document::getHTMLWindowEventListener(const AtomicString &eventType)
{
RegisteredEventListenerList::iterator it = m_windowEventListeners.begin();
for (; it != m_windowEventListeners.end(); ++it)
if ( (*it)->eventType() == eventType && (*it)->listener()->isHTMLEventListener())
return (*it)->listener();
return 0;
}
void Document::removeHTMLWindowEventListener(const AtomicString &eventType)
{
RegisteredEventListenerList::iterator it = m_windowEventListeners.begin();
for (; it != m_windowEventListeners.end(); ++it)
if ( (*it)->eventType() == eventType && (*it)->listener()->isHTMLEventListener()) {
m_windowEventListeners.remove(it);
if (eventType == scrollEvent)
decrementScrollEventListenersCount();
return;
}
}
void Document::incrementScrollEventListenersCount()
{
if (++m_scrollEventListenerCount == 1 && this == topDocument())
frame()->setNeedsScrollNotifications(true);
}
void Document::decrementScrollEventListenersCount()
{
if (--m_scrollEventListenerCount == 0 && this == topDocument())
frame()->setNeedsScrollNotifications(false);
}
void Document::addWindowEventListener(const AtomicString &eventType, PassRefPtr<EventListener> listener, bool useCapture)
{
removeWindowEventListener(eventType, listener.get(), useCapture);
m_windowEventListeners.append(new RegisteredEventListener(eventType, listener, useCapture));
if (eventType == scrollEvent)
incrementScrollEventListenersCount();
}
void Document::removeWindowEventListener(const AtomicString &eventType, EventListener *listener, bool useCapture)
{
RegisteredEventListener rl(eventType, listener, useCapture);
RegisteredEventListenerList::iterator it = m_windowEventListeners.begin();
for (; it != m_windowEventListeners.end(); ++it)
if (*(*it) == rl) {
m_windowEventListeners.remove(it);
if (eventType == scrollEvent)
decrementScrollEventListenersCount();
return;
}
}
bool Document::hasWindowEventListener(const AtomicString &eventType)
{
RegisteredEventListenerList::iterator it = m_windowEventListeners.begin();
for (; it != m_windowEventListeners.end(); ++it)
if ((*it)->eventType() == eventType) {
return true;
}
return false;
}
PassRefPtr<EventListener> Document::createHTMLEventListener(const String& functionName, const String& code, Node *node)
{
if (Frame* frm = frame())
if (frm->scriptProxy()->isEnabled())
return frm->scriptProxy()->createHTMLEventHandler(functionName, code, node);
return 0;
}
void Document::setHTMLWindowEventListener(const AtomicString& eventType, Attribute* attr)
{
setHTMLWindowEventListener(eventType,
createHTMLEventListener(attr->localName().domString(), attr->value(), 0));
}
void Document::dispatchImageLoadEventSoon(HTMLImageLoader *image)
{
m_imageLoadEventDispatchSoonList.append(image);
if (!m_imageLoadEventTimer.isActive())
m_imageLoadEventTimer.startOneShot(0);
}
void Document::removeImage(HTMLImageLoader* image)
{
while (m_imageLoadEventDispatchSoonList.removeRef(image)) { }
while (m_imageLoadEventDispatchingList.removeRef(image)) { }
if (m_imageLoadEventDispatchSoonList.isEmpty())
m_imageLoadEventTimer.stop();
}
void Document::dispatchImageLoadEventsNow()
{
if (!m_imageLoadEventDispatchingList.isEmpty()) {
return;
}
m_imageLoadEventTimer.stop();
m_imageLoadEventDispatchingList = m_imageLoadEventDispatchSoonList;
m_imageLoadEventDispatchSoonList.clear();
for (DeprecatedPtrListIterator<HTMLImageLoader> it(m_imageLoadEventDispatchingList); it.current();) {
HTMLImageLoader* image = it.current();
++it;
image->dispatchLoadEvent();
}
m_imageLoadEventDispatchingList.clear();
}
void Document::imageLoadEventTimerFired(Timer<Document>*)
{
dispatchImageLoadEventsNow();
}
Element* Document::ownerElement() const
{
if (!frame())
return 0;
return frame()->ownerElement();
}
String Document::cookie() const
{
return cookies(this, url());
}
void Document::setCookie(const String& value)
{
setCookies(this, url(), policyBaseURL().deprecatedString(), value);
}
String Document::referrer() const
{
if (frame())
return frame()->loader()->referrer();
return String();
}
String Document::domain() const
{
return m_securityOrigin->host();
}
void Document::setDomain(const String& newDomain)
{
if (equalIgnoringCase(domain(), newDomain)) {
m_securityOrigin->setDomainFromDOM(newDomain);
return;
}
int oldLength = domain().length();
int newLength = newDomain.length();
if (newLength >= oldLength)
return;
String test = domain();
if (test[oldLength - newLength - 1] != '.')
return;
test.remove(0, oldLength - newLength);
if (test != newDomain)
return;
m_securityOrigin->setDomainFromDOM(newDomain);
}
String Document::lastModified() const
{
Frame* f = frame();
if (!f)
return String();
DocumentLoader* loader = f->loader()->documentLoader();
if (!loader)
return String();
return loader->response().httpHeaderField("Last-Modified");
}
bool Document::isValidName(const String &name)
{
const UChar* s = reinterpret_cast<const UChar*>(name.characters());
unsigned length = name.length();
if (length == 0)
return false;
unsigned i = 0;
UChar32 c;
U16_NEXT(s, i, length, c)
if (!isValidNameStart(c))
return false;
while (i < length) {
U16_NEXT(s, i, length, c)
if (!isValidNamePart(c))
return false;
}
return true;
}
bool Document::parseQualifiedName(const String &qualifiedName, String &prefix, String &localName)
{
unsigned length = qualifiedName.length();
if (length == 0)
return false;
bool nameStart = true;
bool sawColon = false;
int colonPos = 0;
const UChar* s = reinterpret_cast<const UChar*>(qualifiedName.characters());
for (unsigned i = 0; i < length;) {
UChar32 c;
U16_NEXT(s, i, length, c)
if (c == ':') {
if (sawColon)
return false; nameStart = true;
sawColon = true;
colonPos = i - 1;
} else if (nameStart) {
if (!isValidNameStart(c))
return false;
nameStart = false;
} else {
if (!isValidNamePart(c))
return false;
}
}
if (!sawColon) {
prefix = String();
localName = qualifiedName;
} else {
prefix = qualifiedName.substring(0, colonPos);
localName = qualifiedName.substring(colonPos + 1);
}
return true;
}
void Document::addImageMap(HTMLMapElement* imageMap)
{
const AtomicString& name = imageMap->getName();
if (!name.impl())
return;
m_imageMapsByName.add(name.impl(), imageMap);
}
void Document::removeImageMap(HTMLMapElement* imageMap)
{
const AtomicString& name = imageMap->getName();
if (!name.impl())
return;
ImageMapsByName::iterator it = m_imageMapsByName.find(name.impl());
if (it != m_imageMapsByName.end() && it->second == imageMap)
m_imageMapsByName.remove(it);
}
HTMLMapElement *Document::getImageMap(const String& url) const
{
if (url.isNull())
return 0;
int hashPos = url.find('#');
String name = (hashPos < 0 ? url : url.substring(hashPos + 1)).impl();
AtomicString mapName = hMode == XHtml ? name : name.lower();
return m_imageMapsByName.get(mapName.impl());
}
void Document::setDecoder(TextResourceDecoder *decoder)
{
m_decoder = decoder;
}
UChar Document::backslashAsCurrencySymbol() const
{
if (!m_decoder)
return '\\';
return m_decoder->encoding().backslashAsCurrencySymbol();
}
DeprecatedString Document::completeURL(const DeprecatedString& url)
{
if (m_url.isEmpty() && m_baseURL.isEmpty())
return url;
if (!m_decoder)
return KURL(baseURL(), url).deprecatedString();
return KURL(baseURL(), url, m_decoder->encoding()).deprecatedString();
}
String Document::completeURL(const String& url)
{
if (url.isNull())
return url;
return completeURL(url.deprecatedString());
}
bool Document::inPageCache()
{
return m_inPageCache;
}
void Document::setInPageCache(bool flag)
{
if (m_inPageCache == flag)
return;
m_inPageCache = flag;
if (flag) {
ASSERT(m_savedRenderer == 0);
m_savedRenderer = renderer();
if (FrameView* v = view())
v->resetScrollbars();
} else {
ASSERT(renderer() == 0 || renderer() == m_savedRenderer);
ASSERT(m_renderArena);
setRenderer(m_savedRenderer);
m_savedRenderer = 0;
}
}
void Document::willSaveToCache()
{
#if ENABLE(HW_COMP)
RenderView* rv = static_cast<RenderView*>(renderer());
if (rv) rv->willBeDetached();
#endif // ENABLE(HW_COMP)
HashSet<Element*>::iterator end = m_pageCacheCallbackElements.end();
for (HashSet<Element*>::iterator i = m_pageCacheCallbackElements.begin(); i != end; ++i)
(*i)->willSaveToCache();
}
void Document::didRestoreFromCache()
{
HashSet<Element*>::iterator end = m_pageCacheCallbackElements.end();
for (HashSet<Element*>::iterator i = m_pageCacheCallbackElements.begin(); i != end; ++i)
(*i)->didRestoreFromCache();
if (m_frame && m_scrollEventListenerCount > 0)
m_frame->setNeedsScrollNotifications(true);
#if ENABLE(HW_COMP)
RenderView* rv = static_cast<RenderView*>(renderer());
if (rv) rv->wasAttached();
#endif // ENABLE(HW_COMP)
}
void Document::registerForCacheCallbacks(Element* e)
{
m_pageCacheCallbackElements.add(e);
}
void Document::unregisterForCacheCallbacks(Element* e)
{
m_pageCacheCallbackElements.remove(e);
}
void Document::setShouldCreateRenderers(bool f)
{
m_createRenderers = f;
}
bool Document::shouldCreateRenderers()
{
return m_createRenderers;
}
String Document::toString() const
{
String result;
for (Node *child = firstChild(); child != NULL; child = child->nextSibling()) {
result += child->toString();
}
return result;
}
static Editor::Command command(Document* document, const String& commandName, bool userInterface = false)
{
Frame* frame = document->frame();
if (!frame || frame->document() != document)
return Editor::Command();
return frame->editor()->command(commandName,
userInterface ? CommandFromDOMWithUserInterface : CommandFromDOM);
}
bool Document::execCommand(const String& commandName, bool userInterface, const String& value)
{
return command(this, commandName, userInterface).execute(value);
}
bool Document::queryCommandEnabled(const String& commandName)
{
return command(this, commandName).isEnabled();
}
bool Document::queryCommandIndeterm(const String& commandName)
{
return command(this, commandName).state() == MixedTriState;
}
bool Document::queryCommandState(const String& commandName)
{
return command(this, commandName).state() != FalseTriState;
}
bool Document::queryCommandSupported(const String& commandName)
{
return command(this, commandName).isSupported();
}
String Document::queryCommandValue(const String& commandName)
{
return command(this, commandName).value();
}
static IntRect placeholderRectForMarker()
{
return IntRect(-1, -1, -1, -1);
}
void Document::addMarker(Range *range, DocumentMarker::MarkerType type, String description)
{
for (TextIterator markedText(range); !markedText.atEnd(); markedText.advance()) {
RefPtr<Range> textPiece = markedText.range();
int exception = 0;
DocumentMarker marker = {type, textPiece->startOffset(exception), textPiece->endOffset(exception), description};
addMarker(textPiece->startContainer(exception), marker);
}
}
void Document::removeMarkers(Range* range, DocumentMarker::MarkerType markerType)
{
if (m_markers.isEmpty())
return;
ExceptionCode ec = 0;
Node* startContainer = range->startContainer(ec);
Node* endContainer = range->endContainer(ec);
Node* pastEndNode = range->pastEndNode();
for (Node* node = range->startNode(); node != pastEndNode; node = node->traverseNextNode()) {
int startOffset = node == startContainer ? range->startOffset(ec) : 0;
int endOffset = node == endContainer ? range->endOffset(ec) : INT_MAX;
int length = endOffset - startOffset;
removeMarkers(node, startOffset, length, markerType);
}
}
void Document::addMarker(Node *node, DocumentMarker newMarker)
{
ASSERT(newMarker.endOffset >= newMarker.startOffset);
if (newMarker.endOffset == newMarker.startOffset)
return;
MarkerMapVectorPair* vectorPair = m_markers.get(node);
if (!vectorPair) {
vectorPair = new MarkerMapVectorPair;
vectorPair->first.append(newMarker);
vectorPair->second.append(placeholderRectForMarker());
m_markers.set(node, vectorPair);
} else {
Vector<DocumentMarker>& markers = vectorPair->first;
Vector<IntRect>& rects = vectorPair->second;
ASSERT(markers.size() == rects.size());
size_t i;
for (i = 0; i != markers.size();) {
DocumentMarker marker = markers[i];
if (newMarker.endOffset < marker.startOffset+1) {
break;
} else if (newMarker.startOffset > marker.endOffset) {
i++;
} else if (newMarker == marker) {
return;
} else {
newMarker.startOffset = min(newMarker.startOffset, marker.startOffset);
newMarker.endOffset = max(newMarker.endOffset, marker.endOffset);
markers.remove(i);
rects.remove(i);
}
}
markers.insert(i, newMarker);
rects.insert(i, placeholderRectForMarker());
}
if (node->renderer())
node->renderer()->repaint();
}
void Document::copyMarkers(Node *srcNode, unsigned startOffset, int length, Node *dstNode, int delta, DocumentMarker::MarkerType markerType)
{
if (length <= 0)
return;
MarkerMapVectorPair* vectorPair = m_markers.get(srcNode);
if (!vectorPair)
return;
ASSERT(vectorPair->first.size() == vectorPair->second.size());
bool docDirty = false;
unsigned endOffset = startOffset + length - 1;
Vector<DocumentMarker>& markers = vectorPair->first;
for (size_t i = 0; i != markers.size(); ++i) {
DocumentMarker marker = markers[i];
if (marker.startOffset > endOffset)
break;
if (marker.endOffset < startOffset || (marker.type != markerType && markerType != DocumentMarker::AllMarkers))
continue;
docDirty = true;
if (marker.startOffset < startOffset)
marker.startOffset = startOffset;
if (marker.endOffset > endOffset)
marker.endOffset = endOffset;
marker.startOffset += delta;
marker.endOffset += delta;
addMarker(dstNode, marker);
}
if (docDirty && dstNode->renderer())
dstNode->renderer()->repaint();
}
void Document::removeMarkers(Node* node, unsigned startOffset, int length, DocumentMarker::MarkerType markerType)
{
if (length <= 0)
return;
MarkerMapVectorPair* vectorPair = m_markers.get(node);
if (!vectorPair)
return;
Vector<DocumentMarker>& markers = vectorPair->first;
Vector<IntRect>& rects = vectorPair->second;
ASSERT(markers.size() == rects.size());
bool docDirty = false;
unsigned endOffset = startOffset + length;
for (size_t i = 0; i < markers.size();) {
DocumentMarker marker = markers[i];
if (marker.startOffset >= endOffset)
break;
if (marker.endOffset < startOffset || (marker.type != markerType && markerType != DocumentMarker::AllMarkers)) {
i++;
continue;
}
docDirty = true;
markers.remove(i);
rects.remove(i);
if (startOffset > marker.startOffset) {
DocumentMarker newLeft = marker;
newLeft.endOffset = startOffset;
markers.insert(i, newLeft);
rects.insert(i, placeholderRectForMarker());
i++;
}
if (marker.endOffset > endOffset) {
DocumentMarker newRight = marker;
newRight.startOffset = endOffset;
markers.insert(i, newRight);
rects.insert(i, placeholderRectForMarker());
i++;
}
}
if (markers.isEmpty()) {
ASSERT(rects.isEmpty());
m_markers.remove(node);
delete vectorPair;
}
if (docDirty && node->renderer())
node->renderer()->repaint();
}
DocumentMarker* Document::markerContainingPoint(const IntPoint& point, DocumentMarker::MarkerType markerType)
{
MarkerMap::iterator end = m_markers.end();
for (MarkerMap::iterator nodeIterator = m_markers.begin(); nodeIterator != end; ++nodeIterator) {
MarkerMapVectorPair* vectorPair = nodeIterator->second;
Vector<DocumentMarker>& markers = vectorPair->first;
Vector<IntRect>& rects = vectorPair->second;
ASSERT(markers.size() == rects.size());
unsigned markerCount = markers.size();
for (unsigned markerIndex = 0; markerIndex < markerCount; ++markerIndex) {
DocumentMarker& marker = markers[markerIndex];
if (marker.type != markerType && markerType != DocumentMarker::AllMarkers)
continue;
IntRect& r = rects[markerIndex];
if (r == placeholderRectForMarker())
continue;
if (r.contains(point))
return ▮
}
}
return 0;
}
Vector<DocumentMarker> Document::markersForNode(Node* node)
{
MarkerMapVectorPair* vectorPair = m_markers.get(node);
if (vectorPair)
return vectorPair->first;
return Vector<DocumentMarker>();
}
Vector<IntRect> Document::renderedRectsForMarkers(DocumentMarker::MarkerType markerType)
{
Vector<IntRect> result;
MarkerMap::iterator end = m_markers.end();
for (MarkerMap::iterator nodeIterator = m_markers.begin(); nodeIterator != end; ++nodeIterator) {
MarkerMapVectorPair* vectorPair = nodeIterator->second;
Vector<DocumentMarker>& markers = vectorPair->first;
Vector<IntRect>& rects = vectorPair->second;
ASSERT(markers.size() == rects.size());
unsigned markerCount = markers.size();
for (unsigned markerIndex = 0; markerIndex < markerCount; ++markerIndex) {
DocumentMarker marker = markers[markerIndex];
if (marker.type != markerType && markerType != DocumentMarker::AllMarkers)
continue;
IntRect r = rects[markerIndex];
if (r == placeholderRectForMarker())
continue;
result.append(r);
}
}
return result;
}
void Document::removeMarkers(Node* node)
{
MarkerMap::iterator i = m_markers.find(node);
if (i != m_markers.end()) {
delete i->second;
m_markers.remove(i);
if (RenderObject* renderer = node->renderer())
renderer->repaint();
}
}
void Document::removeMarkers(DocumentMarker::MarkerType markerType)
{
MarkerMap markerMapCopy = m_markers;
MarkerMap::iterator end = markerMapCopy.end();
for (MarkerMap::iterator i = markerMapCopy.begin(); i != end; ++i) {
Node* node = i->first.get();
bool nodeNeedsRepaint = false;
MarkerMapVectorPair* vectorPair = i->second;
Vector<DocumentMarker>& markers = vectorPair->first;
Vector<IntRect>& rects = vectorPair->second;
ASSERT(markers.size() == rects.size());
for (size_t i = 0; i != markers.size();) {
DocumentMarker marker = markers[i];
if (marker.type != markerType && markerType != DocumentMarker::AllMarkers) {
++i;
continue;
}
markers.remove(i);
rects.remove(i);
nodeNeedsRepaint = true;
}
if (nodeNeedsRepaint) {
RenderObject* renderer = node->renderer();
if (renderer)
renderer->repaint();
}
if (markers.isEmpty()) {
ASSERT(rects.isEmpty());
m_markers.remove(node);
delete vectorPair;
}
}
}
void Document::repaintMarkers(DocumentMarker::MarkerType markerType)
{
MarkerMap::iterator end = m_markers.end();
for (MarkerMap::iterator i = m_markers.begin(); i != end; ++i) {
Node* node = i->first.get();
MarkerMapVectorPair* vectorPair = i->second;
Vector<DocumentMarker>& markers = vectorPair->first;
bool nodeNeedsRepaint = false;
for (size_t i = 0; i != markers.size(); ++i) {
DocumentMarker marker = markers[i];
if (marker.type == markerType || markerType == DocumentMarker::AllMarkers) {
nodeNeedsRepaint = true;
break;
}
}
if (!nodeNeedsRepaint)
continue;
if (RenderObject* renderer = node->renderer())
renderer->repaint();
}
}
void Document::setRenderedRectForMarker(Node* node, DocumentMarker marker, const IntRect& r)
{
MarkerMapVectorPair* vectorPair = m_markers.get(node);
if (!vectorPair) {
ASSERT_NOT_REACHED(); return;
}
Vector<DocumentMarker>& markers = vectorPair->first;
ASSERT(markers.size() == vectorPair->second.size());
unsigned markerCount = markers.size();
for (unsigned markerIndex = 0; markerIndex < markerCount; ++markerIndex) {
DocumentMarker m = markers[markerIndex];
if (m == marker) {
vectorPair->second[markerIndex] = r;
return;
}
}
ASSERT_NOT_REACHED(); }
void Document::invalidateRenderedRectsForMarkersInRect(const IntRect& r)
{
MarkerMap::iterator end = m_markers.end();
for (MarkerMap::iterator i = m_markers.begin(); i != end; ++i) {
MarkerMapVectorPair* vectorPair = i->second;
Vector<IntRect>& rects = vectorPair->second;
unsigned rectCount = rects.size();
for (unsigned rectIndex = 0; rectIndex < rectCount; ++rectIndex)
if (rects[rectIndex].intersects(r))
rects[rectIndex] = placeholderRectForMarker();
}
}
void Document::shiftMarkers(Node *node, unsigned startOffset, int delta, DocumentMarker::MarkerType markerType)
{
MarkerMapVectorPair* vectorPair = m_markers.get(node);
if (!vectorPair)
return;
Vector<DocumentMarker>& markers = vectorPair->first;
Vector<IntRect>& rects = vectorPair->second;
ASSERT(markers.size() == rects.size());
bool docDirty = false;
for (size_t i = 0; i != markers.size(); ++i) {
DocumentMarker &marker = markers[i];
if (marker.startOffset >= startOffset && (markerType == DocumentMarker::AllMarkers || marker.type == markerType)) {
ASSERT((int)marker.startOffset + delta >= 0);
marker.startOffset += delta;
marker.endOffset += delta;
docDirty = true;
rects[i] = placeholderRectForMarker();
}
}
if (docDirty && node->renderer())
node->renderer()->repaint();
}
#if ENABLE(XSLT)
void Document::applyXSLTransform(ProcessingInstruction* pi)
{
RefPtr<XSLTProcessor> processor = new XSLTProcessor;
processor->setXSLStylesheet(static_cast<XSLStyleSheet*>(pi->sheet()));
String resultMIMEType;
String newSource;
String resultEncoding;
if (!processor->transformToString(this, resultMIMEType, newSource, resultEncoding))
return;
processor->createDocumentFromSource(newSource, resultEncoding, resultMIMEType, this, frame());
}
void Document::setTransformSource(void* doc)
{
if (doc == m_transformSource)
return;
xmlFreeDoc((xmlDocPtr)m_transformSource);
m_transformSource = doc;
}
#endif
void Document::setDesignMode(InheritedBool value)
{
m_designMode = value;
}
Document::InheritedBool Document::getDesignMode() const
{
return m_designMode;
}
bool Document::inDesignMode() const
{
for (const Document* d = this; d; d = d->parentDocument()) {
if (d->m_designMode != inherit)
return d->m_designMode;
}
return false;
}
Document *Document::parentDocument() const
{
Frame *childPart = frame();
if (!childPart)
return 0;
Frame *parent = childPart->tree()->parent();
if (!parent)
return 0;
return parent->document();
}
Document *Document::topDocument() const
{
Document *doc = const_cast<Document *>(this);
Element *element;
while ((element = doc->ownerElement()))
doc = element->document();
return doc;
}
PassRefPtr<Attr> Document::createAttributeNS(const String &namespaceURI, const String &qualifiedName, ExceptionCode& ec)
{
if (qualifiedName.isNull()) {
ec = NAMESPACE_ERR;
return 0;
}
String localName = qualifiedName;
String prefix;
int colonpos;
if ((colonpos = qualifiedName.find(':')) >= 0) {
prefix = qualifiedName.substring(0, colonpos);
localName = qualifiedName.substring(colonpos + 1);
}
if (!isValidName(localName)) {
ec = INVALID_CHARACTER_ERR;
return 0;
}
return new Attr(0, this, new MappedAttribute(QualifiedName(prefix, localName, namespaceURI), StringImpl::empty()));
}
#if ENABLE(SVG)
const SVGDocumentExtensions* Document::svgExtensions()
{
return m_svgExtensions;
}
SVGDocumentExtensions* Document::accessSVGExtensions()
{
if (!m_svgExtensions)
m_svgExtensions = new SVGDocumentExtensions(this);
return m_svgExtensions;
}
#endif
PassRefPtr<HTMLCollection> Document::images()
{
return new HTMLCollection(this, HTMLCollection::DocImages);
}
PassRefPtr<HTMLCollection> Document::applets()
{
return new HTMLCollection(this, HTMLCollection::DocApplets);
}
PassRefPtr<HTMLCollection> Document::embeds()
{
return new HTMLCollection(this, HTMLCollection::DocEmbeds);
}
PassRefPtr<HTMLCollection> Document::plugins()
{
return new HTMLCollection(this, HTMLCollection::DocEmbeds);
}
PassRefPtr<HTMLCollection> Document::objects()
{
return new HTMLCollection(this, HTMLCollection::DocObjects);
}
PassRefPtr<HTMLCollection> Document::scripts()
{
return new HTMLCollection(this, HTMLCollection::DocScripts);
}
PassRefPtr<HTMLCollection> Document::links()
{
return new HTMLCollection(this, HTMLCollection::DocLinks);
}
PassRefPtr<HTMLCollection> Document::forms()
{
return new HTMLCollection(this, HTMLCollection::DocForms);
}
PassRefPtr<HTMLCollection> Document::anchors()
{
return new HTMLCollection(this, HTMLCollection::DocAnchors);
}
PassRefPtr<HTMLCollection> Document::all()
{
return new HTMLCollection(this, HTMLCollection::DocAll);
}
PassRefPtr<HTMLCollection> Document::windowNamedItems(const String &name)
{
return new HTMLNameCollection(this, HTMLCollection::WindowNamedItems, name);
}
PassRefPtr<HTMLCollection> Document::documentNamedItems(const String &name)
{
return new HTMLNameCollection(this, HTMLCollection::DocumentNamedItems, name);
}
HTMLCollection::CollectionInfo* Document::nameCollectionInfo(HTMLCollection::Type type, const AtomicString& name)
{
ASSERT(type >= HTMLCollection::FirstNamedDocumentCachedType);
unsigned index = type - HTMLCollection::FirstNamedDocumentCachedType;
ASSERT(index < HTMLCollection::NumNamedDocumentCachedTypes);
NamedCollectionMap& map = m_nameCollectionInfo[index];
NamedCollectionMap::iterator iter = map.find(name.impl());
if (iter == map.end())
iter = map.add(name.impl(), new HTMLCollection::CollectionInfo).first;
return iter->second;
}
void Document::finishedParsing()
{
setParsing(false);
ExceptionCode ec = 0;
dispatchEvent(new Event(DOMContentLoadedEvent, true, false), ec);
if (Frame* f = frame())
f->loader()->finishedParsing();
}
Vector<String> Document::formElementsState() const
{
Vector<String> stateVector;
stateVector.reserveCapacity(m_formElementsWithState.size() * 3);
typedef ListHashSet<HTMLFormControlElementWithState*>::const_iterator Iterator;
Iterator end = m_formElementsWithState.end();
for (Iterator it = m_formElementsWithState.begin(); it != end; ++it) {
HTMLFormControlElementWithState* e = *it;
String value;
if (e->saveState(value)) {
stateVector.append(e->name().domString());
stateVector.append(e->type().domString());
stateVector.append(value);
}
}
return stateVector;
}
void Document::setTelephoneNumberParsingEnabled(bool enabled)
{
m_telephoneNumberParsingEnabled = enabled;
}
bool Document::isTelephoneNumberParsingEnabled()
{
return m_telephoneNumberParsingEnabled;
}
unsigned Document::formElementsCharacterCount() const
{
typedef ListHashSet<HTMLFormControlElementWithState*>::const_iterator Iterator;
Iterator end = m_formElementsWithState.end();
unsigned charCount = 0;
for (Iterator it = m_formElementsWithState.begin(); it != end; ++it) {
HTMLGenericFormElement* e = *it;
if (e->hasLocalName(inputTag)) {
HTMLInputElement *input = static_cast<HTMLInputElement*>(e);
if (input->isTextField()) {
charCount += input->value().length();
}
}
}
return charCount;
}
#if ENABLE(XPATH)
PassRefPtr<XPathExpression> Document::createExpression(const String& expression,
XPathNSResolver* resolver,
ExceptionCode& ec)
{
if (!m_xpathEvaluator)
m_xpathEvaluator = new XPathEvaluator;
return m_xpathEvaluator->createExpression(expression, resolver, ec);
}
PassRefPtr<XPathNSResolver> Document::createNSResolver(Node* nodeResolver)
{
if (!m_xpathEvaluator)
m_xpathEvaluator = new XPathEvaluator;
return m_xpathEvaluator->createNSResolver(nodeResolver);
}
PassRefPtr<XPathResult> Document::evaluate(const String& expression,
Node* contextNode,
XPathNSResolver* resolver,
unsigned short type,
XPathResult* result,
ExceptionCode& ec)
{
if (!m_xpathEvaluator)
m_xpathEvaluator = new XPathEvaluator;
return m_xpathEvaluator->evaluate(expression, contextNode, resolver, type, result, ec);
}
#endif // ENABLE(XPATH)
void Document::setStateForNewFormElements(const Vector<String>& stateVector)
{
typedef FormElementStateMap::iterator Iterator;
m_formElementsWithState.clear();
for (size_t i = stateVector.size() / 3 * 3; i; i -= 3) {
AtomicString a = stateVector[i - 3];
AtomicString b = stateVector[i - 2];
const String& c = stateVector[i - 1];
FormElementKey key(a.impl(), b.impl());
Iterator it = m_stateForNewFormElements.find(key);
if (it != m_stateForNewFormElements.end())
it->second.append(c);
else {
Vector<String> v(1);
v[0] = c;
m_stateForNewFormElements.set(key, v);
}
}
}
bool Document::hasStateForNewFormElements() const
{
return !m_stateForNewFormElements.isEmpty();
}
bool Document::takeStateForFormElement(AtomicStringImpl* name, AtomicStringImpl* type, String& state)
{
typedef FormElementStateMap::iterator Iterator;
Iterator it = m_stateForNewFormElements.find(FormElementKey(name, type));
if (it == m_stateForNewFormElements.end())
return false;
ASSERT(it->second.size());
state = it->second.last();
if (it->second.size() > 1)
it->second.removeLast();
else
m_stateForNewFormElements.remove(it);
return true;
}
FormElementKey::FormElementKey(AtomicStringImpl* name, AtomicStringImpl* type)
: m_name(name), m_type(type)
{
ref();
}
FormElementKey::~FormElementKey()
{
deref();
}
FormElementKey::FormElementKey(const FormElementKey& other)
: m_name(other.name()), m_type(other.type())
{
ref();
}
FormElementKey& FormElementKey::operator=(const FormElementKey& other)
{
other.ref();
deref();
m_name = other.name();
m_type = other.type();
return *this;
}
void FormElementKey::ref() const
{
if (name() && name() != HashTraits<AtomicStringImpl*>::deletedValue())
name()->ref();
if (type())
type()->ref();
}
void FormElementKey::deref() const
{
if (name() && name() != HashTraits<AtomicStringImpl*>::deletedValue())
name()->deref();
if (type())
type()->deref();
}
unsigned FormElementKeyHash::hash(const FormElementKey& k)
{
ASSERT(sizeof(k) % (sizeof(uint16_t) * 2) == 0);
unsigned l = sizeof(k) / (sizeof(uint16_t) * 2);
const uint16_t* s = reinterpret_cast<const uint16_t*>(&k);
uint32_t hash = PHI;
for (; l > 0; l--) {
hash += s[0];
uint32_t tmp = (s[1] << 11) ^ hash;
hash = (hash << 16) ^ tmp;
s += 2;
hash += hash >> 11;
}
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 2;
hash += hash >> 15;
hash ^= hash << 10;
if (hash == 0)
hash = 0x80000000;
return hash;
}
FormElementKey FormElementKeyHashTraits::deletedValue()
{
return HashTraits<AtomicStringImpl*>::deletedValue();
}
String Document::iconURL()
{
return m_iconURL;
}
void Document::setIconURL(const String& iconURL, const String& type)
{
if (m_iconURL.isEmpty())
m_iconURL = iconURL;
else if (!type.isEmpty())
m_iconURL = iconURL;
}
void Document::setUseSecureKeyboardEntryWhenActive(bool usesSecureKeyboard)
{
if (m_useSecureKeyboardEntryWhenActive == usesSecureKeyboard)
return;
m_useSecureKeyboardEntryWhenActive = usesSecureKeyboard;
m_frame->updateSecureKeyboardEntryIfActive();
}
bool Document::useSecureKeyboardEntryWhenActive() const
{
return m_useSecureKeyboardEntryWhenActive;
}
void Document::initSecurityOrigin()
{
if (m_securityOrigin && !m_securityOrigin->isEmpty())
return;
m_securityOrigin = SecurityOrigin::createForFrame(m_frame);
}
void Document::setSecurityOrigin(SecurityOrigin* securityOrigin)
{
m_securityOrigin = securityOrigin;
}
void Document::updateFocusAppearanceSoon()
{
if (!m_updateFocusAppearanceTimer.isActive())
m_updateFocusAppearanceTimer.startOneShot(0);
}
void Document::cancelFocusAppearanceUpdate()
{
m_updateFocusAppearanceTimer.stop();
}
void Document::updateFocusAppearanceTimerFired(Timer<Document>*)
{
Node* node = focusedNode();
if (!node)
return;
if (!node->isElementNode())
return;
updateLayout();
Element* element = static_cast<Element*>(node);
if (element->isFocusable())
element->updateFocusAppearance(false);
}
#if ENABLE(DATABASE)
void Document::addOpenDatabase(Database* database)
{
if (!m_openDatabaseSet)
m_openDatabaseSet.set(new DatabaseSet);
ASSERT(!m_openDatabaseSet->contains(database));
m_openDatabaseSet->add(database);
}
void Document::removeOpenDatabase(Database* database)
{
ASSERT(m_openDatabaseSet && m_openDatabaseSet->contains(database));
if (!m_openDatabaseSet)
return;
m_openDatabaseSet->remove(database);
}
DatabaseThread* Document::databaseThread()
{
if (!m_databaseThread && !m_hasOpenDatabases) {
m_databaseThread = new DatabaseThread(this);
if (!m_databaseThread->start())
m_databaseThread = 0;
}
return m_databaseThread.get();
}
void Document::stopDatabases()
{
if (m_openDatabaseSet) {
DatabaseSet::iterator i = m_openDatabaseSet->begin();
DatabaseSet::iterator end = m_openDatabaseSet->end();
for (; i != end; ++i) {
(*i)->stop();
if (m_databaseThread)
m_databaseThread->unscheduleDatabaseTasks(*i);
}
}
if (m_databaseThread)
m_databaseThread->requestTermination();
}
#endif
TextAutoSizingKey::TextAutoSizingKey() : m_style(0), m_doc(0)
{
}
TextAutoSizingKey::TextAutoSizingKey(RenderStyle* style, Document* doc)
: m_style(style), m_doc(doc)
{
ref();
}
TextAutoSizingKey::TextAutoSizingKey(const TextAutoSizingKey& other)
: m_style(other.m_style), m_doc(other.m_doc)
{
ref();
}
TextAutoSizingKey::~TextAutoSizingKey()
{
deref();
}
TextAutoSizingKey& TextAutoSizingKey::operator=(const TextAutoSizingKey& other)
{
other.ref();
deref();
m_style = other.m_style;
m_doc = other.m_doc;
return *this;
}
void TextAutoSizingKey::ref() const
{
if (isValidStyle())
m_style->ref();
}
void TextAutoSizingKey::deref() const
{
if (isValidStyle() && isValidDoc() && m_doc->renderArena())
m_style->deref(m_doc->renderArena());
}
TextAutoSizingKey TextAutoSizingTraits::deletedValue()
{
return TextAutoSizingKey(TextAutoSizingKey::deletedKeyStyle(), TextAutoSizingKey::deletedKeyDoc());
}
int TextAutoSizingValue::numNodes() const
{
return m_autoSizedNodes.size();
}
void TextAutoSizingValue::addNode(Node* node, float size)
{
ASSERT(node);
ASSERT(node->renderer());
RenderText* renderText = static_cast<RenderText*>(node->renderer());
renderText->setCandidateComputedTextSize(size);
m_autoSizedNodes.add(node);
}
#define MAX_SCALE_INCREASE 1.7
bool TextAutoSizingValue::adjustNodeSizes()
{
bool objectsRemoved = false;
Vector<RefPtr<Node> > nodesForRemoval;
HashSet<RefPtr<Node> >::iterator end = m_autoSizedNodes.end();
for (HashSet<RefPtr<Node> >::iterator i = m_autoSizedNodes.begin(); i != end; ++i) {
RefPtr<Node> autoSizingNode = *i;
RenderText* text = static_cast<RenderText*>(autoSizingNode->renderer());
if (!text || (text->style() && !text->style()->textSizeAdjust().isAuto()) || !text->candidateComputedTextSize()) {
nodesForRemoval.append(autoSizingNode);
objectsRemoved = true;
}
}
unsigned count = nodesForRemoval.size();
for (unsigned i = 0; i < count; i++)
m_autoSizedNodes.remove(nodesForRemoval[i]);
if (m_autoSizedNodes.size() <= 1)
return objectsRemoved;
float cumulativeSize = 0;
end = m_autoSizedNodes.end();
for (HashSet<RefPtr<Node> >::iterator i = m_autoSizedNodes.begin(); i != end; ++i) {
RefPtr<Node> autoSizingNode = *i;
RenderText* renderText = static_cast<RenderText*>(autoSizingNode->renderer());
cumulativeSize += renderText->candidateComputedTextSize();
}
float averageSize = roundf(cumulativeSize / m_autoSizedNodes.size());
bool firstPass = true;
end = m_autoSizedNodes.end();
for (HashSet<RefPtr<Node> >::iterator i = m_autoSizedNodes.begin(); i != end; ++i) {
RefPtr<Node>& autoSizingNode = *i;
RenderText* text = static_cast<RenderText*>(autoSizingNode->renderer());
if (text && text->style()->fontDescription().computedSize() != averageSize) {
float specifiedSize = text->style()->fontDescription().specifiedSize();
float scaleChange = averageSize / specifiedSize;
if (scaleChange > MAX_SCALE_INCREASE && firstPass) {
firstPass = false;
averageSize = roundf(specifiedSize * MAX_SCALE_INCREASE);
scaleChange = averageSize / specifiedSize;
}
RenderStyle* style = new (text->renderArena()) RenderStyle(*text->style());
FontDescription fontDescription = style->fontDescription();
fontDescription.setComputedSize(averageSize);
style->setFontDescription(fontDescription);
style->font().update(autoSizingNode->document()->styleSelector()->fontSelector());
text->setStyle(style);
RenderObject* parentRenderer = text->parent();
const RenderStyle* parentStyle = parentRenderer->style();
Length lineHeightLength = parentStyle->specifiedLineHeight();
int specifiedLineHeight = 0;
if (lineHeightLength.isPercent())
specifiedLineHeight = lineHeightLength.calcMinValue(fontDescription.specifiedSize());
else
specifiedLineHeight = lineHeightLength.value();
int lineHeight = specifiedLineHeight * scaleChange;
if (!lineHeightLength.isFixed() || lineHeightLength.value() != lineHeight) {
RenderStyle* newParentStyle = new (text->renderArena()) RenderStyle(*parentStyle);
newParentStyle->setLineHeight(Length(lineHeight, Fixed));
newParentStyle->setSpecifiedLineHeight(lineHeightLength);
newParentStyle->setFontDescription(fontDescription);
newParentStyle->font().update(autoSizingNode->document()->styleSelector()->fontSelector());
parentRenderer->setStyle(newParentStyle);
}
}
}
return objectsRemoved;
}
void TextAutoSizingValue::reset()
{
HashSet<RefPtr<Node> >::iterator end = m_autoSizedNodes.end();
for (HashSet<RefPtr<Node> >::iterator i = m_autoSizedNodes.begin(); i != end; ++i) {
RefPtr<Node>& autoSizingNode = *i;
RenderText* text = static_cast<RenderText*>(autoSizingNode->renderer());
if (!text)
continue;
FontDescription fontDescription = text->style()->fontDescription();
float originalSize = fontDescription.specifiedSize();
if (fontDescription.computedSize() != originalSize) {
fontDescription.setComputedSize(originalSize);
RenderStyle* style = new (text->renderArena()) RenderStyle(*text->style());
style->setFontDescription(fontDescription);
style->font().update(autoSizingNode->document()->styleSelector()->fontSelector());
text->setStyle(style);
}
RenderObject* parentRenderer = text->parent();
if (!parentRenderer)
continue;
const RenderStyle* parentStyle = parentRenderer->style();
Length originalLineHeight = parentStyle->specifiedLineHeight();
if (originalLineHeight != parentStyle->lineHeight()) {
RenderStyle* newParentStyle = new (text->renderArena()) RenderStyle(*parentStyle);
newParentStyle->setLineHeight(originalLineHeight);
newParentStyle->setFontDescription(fontDescription);
newParentStyle->font().update(autoSizingNode->document()->styleSelector()->fontSelector());
parentRenderer->setStyle(newParentStyle);
}
}
}
void Document::addAutoSizingNode(Node* node, float candidateSize)
{
TextAutoSizingKey key(node->renderer()->style(), document());
RefPtr<TextAutoSizingValue> value;
if (m_textAutoSizedNodes.contains(key))
value = m_textAutoSizedNodes.get(key);
else {
value = new TextAutoSizingValue();
m_textAutoSizedNodes.set(key, value);
}
value->addNode(node, candidateSize);
}
void Document::validateAutoSizingNodes ()
{
Vector<TextAutoSizingKey> nodesForRemoval;
TextAutoSizingMap::iterator end = m_textAutoSizedNodes.end();
for (TextAutoSizingMap::iterator i = m_textAutoSizedNodes.begin(); i != end; ++i) {
TextAutoSizingKey key = i->first;
RefPtr<TextAutoSizingValue> value = i->second;
if (!value)
continue;
value->adjustNodeSizes();
if (!value->numNodes())
nodesForRemoval.append(key);
}
unsigned count = nodesForRemoval.size();
for (unsigned i = 0; i < count; i++)
m_textAutoSizedNodes.remove(nodesForRemoval[i]);
}
void Document::resetAutoSizingNodes()
{
TextAutoSizingMap::iterator end = m_textAutoSizedNodes.end();
for (TextAutoSizingMap::iterator i = m_textAutoSizedNodes.begin(); i != end; ++i) {
RefPtr<TextAutoSizingValue> value = i->second;
if (value)
value->reset();
}
m_textAutoSizedNodes.clear();
}
void Document::incrementTotalImageDataSize(CachedImage* image)
{
if (m_documentImages.add(image).second)
m_totalImageDataSize += image->encodedSize();
}
void Document::decrementTotalImageDataSize(CachedImage* image)
{
if (m_documentImages.isEmpty())
return;
HashCountedSet<CachedImage*>::iterator it = m_documentImages.find(image);
if (it == m_documentImages.end())
return;
if (it->second == 1)
m_totalImageDataSize -= image->encodedSize();
m_documentImages.remove(it);
}
unsigned long Document::totalImageDataSize()
{
return m_totalImageDataSize;
}
void Document::incrementAnimatedImageDataCount(unsigned count)
{
m_animatedImageDataCount += count;
}
unsigned long Document::animatedImageDataCount()
{
return m_animatedImageDataCount;
}
}