#include "config.h"
#include "Element.h"
#include "AXObjectCache.h"
#include "Attr.h"
#include "CSSParser.h"
#include "CSSSelectorList.h"
#include "CSSStyleSelector.h"
#include "ClassList.h"
#include "ClientRect.h"
#include "ClientRectList.h"
#include "DOMTokenList.h"
#include "DatasetDOMStringMap.h"
#include "Document.h"
#include "DocumentFragment.h"
#include "ElementRareData.h"
#include "ExceptionCode.h"
#include "FocusController.h"
#include "Frame.h"
#include "FrameView.h"
#include "HTMLElement.h"
#include "HTMLNames.h"
#include "HTMLParserIdioms.h"
#include "InspectorInstrumentation.h"
#include "NodeList.h"
#include "NodeRenderStyle.h"
#include "Page.h"
#include "RenderLayer.h"
#include "RenderView.h"
#include "RenderWidget.h"
#include "Settings.h"
#include "ShadowRoot.h"
#include "TextIterator.h"
#include "WebKitAnimationList.h"
#include "XMLNames.h"
#include "htmlediting.h"
#include <wtf/text/CString.h>
#if ENABLE(SVG)
#include "SVGElement.h"
#include "SVGNames.h"
#endif
namespace WebCore {
using namespace HTMLNames;
using namespace XMLNames;
class StyleSelectorParentPusher {
public:
StyleSelectorParentPusher(Element* parent)
: m_parent(parent)
, m_pushedStyleSelector(0)
{
}
void push()
{
if (m_pushedStyleSelector)
return;
m_pushedStyleSelector = m_parent->document()->styleSelector();
m_pushedStyleSelector->pushParent(m_parent);
}
~StyleSelectorParentPusher()
{
if (!m_pushedStyleSelector)
return;
ASSERT(m_pushedStyleSelector == m_parent->document()->styleSelector());
if (m_pushedStyleSelector != m_parent->document()->styleSelector())
return;
m_pushedStyleSelector->popParent(m_parent);
}
private:
Element* m_parent;
CSSStyleSelector* m_pushedStyleSelector;
};
PassRefPtr<Element> Element::create(const QualifiedName& tagName, Document* document)
{
return adoptRef(new Element(tagName, document, CreateElement));
}
Element::~Element()
{
removeShadowRoot();
if (m_attributeMap)
m_attributeMap->detachFromElement();
}
inline ElementRareData* Element::rareData() const
{
ASSERT(hasRareData());
return static_cast<ElementRareData*>(NodeRareData::rareDataFromMap(this));
}
inline ElementRareData* Element::ensureRareData()
{
return static_cast<ElementRareData*>(Node::ensureRareData());
}
NodeRareData* Element::createRareData()
{
return new ElementRareData;
}
DEFINE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(Element, blur);
DEFINE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(Element, error);
DEFINE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(Element, focus);
DEFINE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(Element, load);
PassRefPtr<DocumentFragment> Element::deprecatedCreateContextualFragment(const String& markup, FragmentScriptingPermission scriptingPermission)
{
RefPtr<DocumentFragment> fragment = document()->createDocumentFragment();
if (document()->isHTMLDocument())
fragment->parseHTML(markup, this, scriptingPermission);
else {
if (!fragment->parseXML(markup, this, scriptingPermission))
return 0;
}
ExceptionCode ignoredExceptionCode;
RefPtr<Node> nextNode;
for (RefPtr<Node> node = fragment->firstChild(); node; node = nextNode) {
nextNode = node->nextSibling();
if (node->hasTagName(htmlTag) || node->hasTagName(headTag) || node->hasTagName(bodyTag)) {
HTMLElement* element = toHTMLElement(node.get());
Node* firstChild = element->firstChild();
if (firstChild)
nextNode = firstChild;
RefPtr<Node> nextChild;
for (RefPtr<Node> child = firstChild; child; child = nextChild) {
nextChild = child->nextSibling();
element->removeChild(child.get(), ignoredExceptionCode);
ASSERT(!ignoredExceptionCode);
fragment->insertBefore(child, element, ignoredExceptionCode);
ASSERT(!ignoredExceptionCode);
}
fragment->removeChild(element, ignoredExceptionCode);
ASSERT(!ignoredExceptionCode);
}
}
return fragment.release();
}
PassRefPtr<Node> Element::cloneNode(bool deep)
{
return deep ? cloneElementWithChildren() : cloneElementWithoutChildren();
}
PassRefPtr<Element> Element::cloneElementWithChildren()
{
RefPtr<Element> clone = cloneElementWithoutChildren();
cloneChildNodes(clone.get());
return clone.release();
}
PassRefPtr<Element> Element::cloneElementWithoutChildren()
{
RefPtr<Element> clone = cloneElementWithoutAttributesAndChildren();
ASSERT(isHTMLElement() == clone->isHTMLElement());
if (NamedNodeMap* attributeMap = attributes(true))
clone->attributes()->setAttributes(*attributeMap);
clone->copyNonAttributeProperties(this);
return clone.release();
}
PassRefPtr<Element> Element::cloneElementWithoutAttributesAndChildren() const
{
return document()->createElement(tagQName(), false);
}
void Element::copyNonAttributeProperties(const Element*)
{
}
void Element::removeAttribute(const QualifiedName& name, ExceptionCode& ec)
{
if (m_attributeMap) {
ec = 0;
m_attributeMap->removeNamedItem(name, ec);
if (ec == NOT_FOUND_ERR)
ec = 0;
}
}
void Element::setAttribute(const QualifiedName& name, const AtomicString& value)
{
ExceptionCode ec;
setAttribute(name, value, ec);
}
void Element::setCStringAttribute(const QualifiedName& name, const char* cStringValue)
{
ExceptionCode ec;
setAttribute(name, AtomicString(cStringValue), ec);
}
void Element::setBooleanAttribute(const QualifiedName& name, bool b)
{
if (b)
setAttribute(name, emptyAtom);
else {
ExceptionCode ex;
removeAttribute(name, ex);
}
}
Node::NodeType Element::nodeType() const
{
return ELEMENT_NODE;
}
bool Element::hasAttribute(const QualifiedName& name) const
{
return hasAttributeNS(name.namespaceURI(), name.localName());
}
const AtomicString& Element::getAttribute(const QualifiedName& name) const
{
if (UNLIKELY(name == styleAttr) && !isStyleAttributeValid())
updateStyleAttribute();
#if ENABLE(SVG)
if (UNLIKELY(!areSVGAttributesValid()))
updateAnimatedSVGAttribute(name);
#endif
return fastGetAttribute(name);
}
void Element::scrollIntoView(bool alignToTop)
{
document()->updateLayoutIgnorePendingStylesheets();
IntRect bounds = getRect();
if (renderer()) {
if (alignToTop)
renderer()->enclosingLayer()->scrollRectToVisible(bounds, false, ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignTopAlways);
else
renderer()->enclosingLayer()->scrollRectToVisible(bounds, false, ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignBottomAlways);
}
}
void Element::scrollIntoViewIfNeeded(bool centerIfNeeded)
{
document()->updateLayoutIgnorePendingStylesheets();
IntRect bounds = getRect();
if (renderer()) {
if (centerIfNeeded)
renderer()->enclosingLayer()->scrollRectToVisible(bounds, false, ScrollAlignment::alignCenterIfNeeded, ScrollAlignment::alignCenterIfNeeded);
else
renderer()->enclosingLayer()->scrollRectToVisible(bounds, false, ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignToEdgeIfNeeded);
}
}
void Element::scrollByUnits(int units, ScrollGranularity granularity)
{
document()->updateLayoutIgnorePendingStylesheets();
if (RenderObject *rend = renderer()) {
if (rend->hasOverflowClip()) {
ScrollDirection direction = ScrollDown;
if (units < 0) {
direction = ScrollUp;
units = -units;
}
toRenderBox(rend)->layer()->scroll(direction, granularity, units);
}
}
}
void Element::scrollByLines(int lines)
{
scrollByUnits(lines, ScrollByLine);
}
void Element::scrollByPages(int pages)
{
scrollByUnits(pages, ScrollByPage);
}
static float localZoomForRenderer(RenderObject* renderer)
{
float zoomFactor = 1;
if (renderer->style()->effectiveZoom() != 1) {
RenderObject* prev = renderer;
for (RenderObject* curr = prev->parent(); curr; curr = curr->parent()) {
if (curr->style()->effectiveZoom() != prev->style()->effectiveZoom()) {
zoomFactor = prev->style()->zoom();
break;
}
prev = curr;
}
if (prev->isRenderView())
zoomFactor = prev->style()->zoom();
}
return zoomFactor;
}
int Element::adjustForLocalZoom(int value, RenderObject* renderer)
{
float zoomFactor = localZoomForRenderer(renderer);
if (zoomFactor == 1)
return value;
if (zoomFactor > 1)
value++;
return static_cast<int>(value / zoomFactor);
}
int Element::offsetLeft()
{
document()->updateLayoutIgnorePendingStylesheets();
if (RenderBoxModelObject* rend = renderBoxModelObject())
return adjustForLocalZoom(rend->offsetLeft(), rend);
return 0;
}
int Element::offsetTop()
{
document()->updateLayoutIgnorePendingStylesheets();
if (RenderBoxModelObject* rend = renderBoxModelObject())
return adjustForLocalZoom(rend->offsetTop(), rend);
return 0;
}
int Element::offsetWidth()
{
document()->updateLayoutIgnorePendingStylesheets();
if (RenderBoxModelObject* rend = renderBoxModelObject())
return adjustForAbsoluteZoom(rend->offsetWidth(), rend);
return 0;
}
int Element::offsetHeight()
{
document()->updateLayoutIgnorePendingStylesheets();
if (RenderBoxModelObject* rend = renderBoxModelObject())
return adjustForAbsoluteZoom(rend->offsetHeight(), rend);
return 0;
}
Element* Element::offsetParent()
{
document()->updateLayoutIgnorePendingStylesheets();
if (RenderObject* rend = renderer())
if (RenderObject* offsetParent = rend->offsetParent())
return static_cast<Element*>(offsetParent->node());
return 0;
}
int Element::clientLeft()
{
document()->updateLayoutIgnorePendingStylesheets();
if (RenderBox* rend = renderBox())
return adjustForAbsoluteZoom(rend->clientLeft(), rend);
return 0;
}
int Element::clientTop()
{
document()->updateLayoutIgnorePendingStylesheets();
if (RenderBox* rend = renderBox())
return adjustForAbsoluteZoom(rend->clientTop(), rend);
return 0;
}
int Element::clientWidth()
{
document()->updateLayoutIgnorePendingStylesheets();
bool inQuirksMode = document()->inQuirksMode();
if ((!inQuirksMode && document()->documentElement() == this) ||
(inQuirksMode && isHTMLElement() && document()->body() == this)) {
if (FrameView* view = document()->view()) {
if (RenderView* renderView = document()->renderView())
return adjustForAbsoluteZoom(view->layoutWidth(), renderView);
}
}
if (RenderBox* rend = renderBox())
return adjustForAbsoluteZoom(rend->clientWidth(), rend);
return 0;
}
int Element::clientHeight()
{
document()->updateLayoutIgnorePendingStylesheets();
bool inQuirksMode = document()->inQuirksMode();
if ((!inQuirksMode && document()->documentElement() == this) ||
(inQuirksMode && isHTMLElement() && document()->body() == this)) {
if (FrameView* view = document()->view()) {
if (RenderView* renderView = document()->renderView())
return adjustForAbsoluteZoom(view->layoutHeight(), renderView);
}
}
if (RenderBox* rend = renderBox())
return adjustForAbsoluteZoom(rend->clientHeight(), rend);
return 0;
}
int Element::scrollLeft() const
{
document()->updateLayoutIgnorePendingStylesheets();
if (RenderBox* rend = renderBox())
return adjustForAbsoluteZoom(rend->scrollLeft(), rend);
return 0;
}
int Element::scrollTop() const
{
document()->updateLayoutIgnorePendingStylesheets();
if (RenderBox* rend = renderBox())
return adjustForAbsoluteZoom(rend->scrollTop(), rend);
return 0;
}
void Element::setScrollLeft(int newLeft)
{
document()->updateLayoutIgnorePendingStylesheets();
if (RenderBox* rend = renderBox())
rend->setScrollLeft(static_cast<int>(newLeft * rend->style()->effectiveZoom()));
}
void Element::setScrollTop(int newTop)
{
document()->updateLayoutIgnorePendingStylesheets();
if (RenderBox* rend = renderBox())
rend->setScrollTop(static_cast<int>(newTop * rend->style()->effectiveZoom()));
}
int Element::scrollWidth() const
{
document()->updateLayoutIgnorePendingStylesheets();
if (RenderBox* rend = renderBox())
return adjustForAbsoluteZoom(rend->scrollWidth(), rend);
return 0;
}
int Element::scrollHeight() const
{
document()->updateLayoutIgnorePendingStylesheets();
if (RenderBox* rend = renderBox())
return adjustForAbsoluteZoom(rend->scrollHeight(), rend);
return 0;
}
IntRect Element::boundsInWindowSpace() const
{
document()->updateLayoutIgnorePendingStylesheets();
FrameView* view = document()->view();
if (!view)
return IntRect();
Vector<FloatQuad> quads;
#if ENABLE(SVG)
if (isSVGElement() && renderer()) {
const SVGElement* svgElement = static_cast<const SVGElement*>(this);
FloatRect localRect;
if (svgElement->boundingBox(localRect))
quads.append(renderer()->localToAbsoluteQuad(localRect));
} else
#endif
{
if (renderBoxModelObject())
renderBoxModelObject()->absoluteQuads(quads);
}
if (quads.isEmpty())
return IntRect();
IntRect result = quads[0].enclosingBoundingBox();
for (size_t i = 1; i < quads.size(); ++i)
result.unite(quads[i].enclosingBoundingBox());
result = view->contentsToWindow(result);
return result;
}
PassRefPtr<ClientRectList> Element::getClientRects() const
{
document()->updateLayoutIgnorePendingStylesheets();
RenderBoxModelObject* renderBoxModelObject = this->renderBoxModelObject();
if (!renderBoxModelObject)
return ClientRectList::create();
Vector<FloatQuad> quads;
renderBoxModelObject->absoluteQuads(quads);
float pageScale = 1;
if (Page* page = document()->page()) {
if (Frame* frame = page->mainFrame())
pageScale = frame->pageScaleFactor();
}
if (FrameView* view = document()->view()) {
IntRect visibleContentRect = view->visibleContentRect();
for (size_t i = 0; i < quads.size(); ++i) {
quads[i].move(-visibleContentRect.x(), -visibleContentRect.y());
adjustFloatQuadForAbsoluteZoom(quads[i], renderBoxModelObject);
if (pageScale != 1)
adjustFloatQuadForPageScale(quads[i], pageScale);
}
}
return ClientRectList::create(quads);
}
PassRefPtr<ClientRect> Element::getBoundingClientRect() const
{
document()->updateLayoutIgnorePendingStylesheets();
Vector<FloatQuad> quads;
#if ENABLE(SVG)
if (isSVGElement() && renderer()) {
const SVGElement* svgElement = static_cast<const SVGElement*>(this);
FloatRect localRect;
if (svgElement->boundingBox(localRect))
quads.append(renderer()->localToAbsoluteQuad(localRect));
} else
#endif
{
if (renderBoxModelObject())
renderBoxModelObject()->absoluteQuads(quads);
}
if (quads.isEmpty())
return ClientRect::create();
FloatRect result = quads[0].boundingBox();
for (size_t i = 1; i < quads.size(); ++i)
result.unite(quads[i].boundingBox());
if (FrameView* view = document()->view()) {
IntRect visibleContentRect = view->visibleContentRect();
result.move(-visibleContentRect.x(), -visibleContentRect.y());
}
adjustFloatRectForAbsoluteZoom(result, renderer());
if (Page* page = document()->page()) {
if (Frame* frame = page->mainFrame())
adjustFloatRectForPageScale(result, frame->pageScaleFactor());
}
return ClientRect::create(result);
}
IntRect Element::screenRect() const
{
if (!renderer())
return IntRect();
return renderer()->view()->frameView()->contentsToScreen(renderer()->absoluteBoundingBoxRect());
}
static inline bool shouldIgnoreAttributeCase(const Element* e)
{
return e && e->document()->isHTMLDocument() && e->isHTMLElement();
}
const AtomicString& Element::getAttribute(const String& name) const
{
bool ignoreCase = shouldIgnoreAttributeCase(this);
if (!isStyleAttributeValid() && equalPossiblyIgnoringCase(name, styleAttr.localName(), ignoreCase))
updateStyleAttribute();
#if ENABLE(SVG)
if (!areSVGAttributesValid()) {
updateAnimatedSVGAttribute(QualifiedName(nullAtom, name, nullAtom));
}
#endif
if (m_attributeMap) {
if (Attribute* attribute = m_attributeMap->getAttributeItem(name, ignoreCase))
return attribute->value();
}
return nullAtom;
}
const AtomicString& Element::getAttributeNS(const String& namespaceURI, const String& localName) const
{
return getAttribute(QualifiedName(nullAtom, localName, namespaceURI));
}
void Element::setAttribute(const AtomicString& name, const AtomicString& value, ExceptionCode& ec)
{
if (!Document::isValidName(name)) {
ec = INVALID_CHARACTER_ERR;
return;
}
#if ENABLE(INSPECTOR)
if (!isSynchronizingStyleAttribute())
InspectorInstrumentation::willModifyDOMAttr(document(), this);
#endif
const AtomicString& localName = shouldIgnoreAttributeCase(this) ? name.lower() : name;
QualifiedName attributeName(nullAtom, localName, nullAtom);
Attribute* old = attributes(false)->getAttributeItem(localName, false);
document()->incDOMTreeVersion();
if (isIdAttributeName(old ? old->name() : attributeName))
updateId(old ? old->value() : nullAtom, value);
if (old && value.isNull())
m_attributeMap->removeAttribute(old->name());
else if (!old && !value.isNull())
m_attributeMap->addAttribute(createAttribute(attributeName, value));
else if (old && !value.isNull()) {
if (Attr* attrNode = old->attr())
attrNode->setValue(value);
else
old->setValue(value);
attributeChanged(old);
}
#if ENABLE(INSPECTOR)
if (!isSynchronizingStyleAttribute())
InspectorInstrumentation::didModifyDOMAttr(document(), this);
#endif
}
void Element::setAttribute(const QualifiedName& name, const AtomicString& value, ExceptionCode&)
{
#if ENABLE(INSPECTOR)
if (!isSynchronizingStyleAttribute())
InspectorInstrumentation::willModifyDOMAttr(document(), this);
#endif
document()->incDOMTreeVersion();
Attribute* old = attributes(false)->getAttributeItem(name);
if (isIdAttributeName(name))
updateId(old ? old->value() : nullAtom, value);
if (old && value.isNull())
m_attributeMap->removeAttribute(name);
else if (!old && !value.isNull())
m_attributeMap->addAttribute(createAttribute(name, value));
else if (old) {
if (Attr* attrNode = old->attr())
attrNode->setValue(value);
else
old->setValue(value);
attributeChanged(old);
}
#if ENABLE(INSPECTOR)
if (!isSynchronizingStyleAttribute())
InspectorInstrumentation::didModifyDOMAttr(document(), this);
#endif
}
PassRefPtr<Attribute> Element::createAttribute(const QualifiedName& name, const AtomicString& value)
{
return Attribute::create(name, value);
}
void Element::attributeChanged(Attribute* attr, bool)
{
if (isIdAttributeName(attr->name()))
idAttributeChanged(attr);
recalcStyleIfNeededAfterAttributeChanged(attr);
updateAfterAttributeChanged(attr);
}
void Element::updateAfterAttributeChanged(Attribute* attr)
{
if (!AXObjectCache::accessibilityEnabled())
return;
const QualifiedName& attrName = attr->name();
if (attrName == aria_activedescendantAttr) {
document()->axObjectCache()->handleActiveDescendantChanged(renderer());
} else if (attrName == roleAttr) {
document()->axObjectCache()->handleAriaRoleChanged(renderer());
} else if (attrName == aria_valuenowAttr) {
document()->axObjectCache()->postNotification(renderer(), AXObjectCache::AXValueChanged, true);
} else if (attrName == aria_labelAttr || attrName == aria_labeledbyAttr || attrName == altAttr || attrName == titleAttr) {
document()->axObjectCache()->contentChanged(renderer());
} else if (attrName == aria_selectedAttr)
document()->axObjectCache()->selectedChildrenChanged(renderer());
else if (attrName == aria_expandedAttr)
document()->axObjectCache()->handleAriaExpandedChange(renderer());
else if (attrName == aria_hiddenAttr)
document()->axObjectCache()->childrenChanged(renderer());
else if (attrName == aria_invalidAttr)
document()->axObjectCache()->postNotification(renderer(), AXObjectCache::AXInvalidStatusChanged, true);
}
void Element::recalcStyleIfNeededAfterAttributeChanged(Attribute* attr)
{
if (document()->attached() && document()->styleSelector()->hasSelectorForAttribute(attr->name().localName()))
setNeedsStyleRecalc();
}
void Element::idAttributeChanged(Attribute* attr)
{
setHasID(!attr->isNull());
if (attributeMap()) {
if (attr->isNull())
attributeMap()->setIdForStyleResolution(nullAtom);
else if (document()->inQuirksMode())
attributeMap()->setIdForStyleResolution(attr->value().lower());
else
attributeMap()->setIdForStyleResolution(attr->value());
}
setNeedsStyleRecalc();
}
static bool isEventHandlerAttribute(const QualifiedName& name)
{
return name.namespaceURI().isNull() && name.localName().startsWith("on");
}
static bool isAttributeToRemove(const QualifiedName& name, const AtomicString& value)
{
return (name.localName().endsWith(hrefAttr.localName()) || name == srcAttr || name == actionAttr) && protocolIsJavaScript(stripLeadingAndTrailingHTMLSpaces(value));
}
void Element::setAttributeMap(PassRefPtr<NamedNodeMap> list, FragmentScriptingPermission scriptingPermission)
{
document()->incDOMTreeVersion();
const QualifiedName& idName = document()->idAttributeName();
Attribute* oldId = m_attributeMap ? m_attributeMap->getAttributeItem(idName) : 0;
Attribute* newId = list ? list->getAttributeItem(idName) : 0;
if (oldId || newId)
updateId(oldId ? oldId->value() : nullAtom, newId ? newId->value() : nullAtom);
if (m_attributeMap)
m_attributeMap->m_element = 0;
m_attributeMap = list;
if (m_attributeMap) {
m_attributeMap->m_element = this;
if (scriptingPermission == FragmentScriptingNotAllowed) {
unsigned i = 0;
while (i < m_attributeMap->length()) {
const QualifiedName& attributeName = m_attributeMap->m_attributes[i]->name();
if (isEventHandlerAttribute(attributeName)) {
m_attributeMap->m_attributes.remove(i);
continue;
}
if (isAttributeToRemove(attributeName, m_attributeMap->m_attributes[i]->value()))
m_attributeMap->m_attributes[i]->setValue(nullAtom);
i++;
}
}
Vector<RefPtr<Attribute> > attributes;
m_attributeMap->copyAttributesToVector(attributes);
for (Vector<RefPtr<Attribute> >::iterator iter = attributes.begin(); iter != attributes.end(); ++iter)
attributeChanged(iter->get());
}
}
bool Element::hasAttributes() const
{
if (!isStyleAttributeValid())
updateStyleAttribute();
#if ENABLE(SVG)
if (!areSVGAttributesValid())
updateAnimatedSVGAttribute(anyQName());
#endif
return m_attributeMap && m_attributeMap->length();
}
String Element::nodeName() const
{
return m_tagName.toString();
}
String Element::nodeNamePreservingCase() const
{
return m_tagName.toString();
}
void Element::setPrefix(const AtomicString& prefix, ExceptionCode& ec)
{
ec = 0;
checkSetPrefix(prefix, ec);
if (ec)
return;
m_tagName.setPrefix(prefix.isEmpty() ? AtomicString() : prefix);
}
KURL Element::baseURI() const
{
const AtomicString& baseAttribute = getAttribute(baseAttr);
KURL base(KURL(), baseAttribute);
if (!base.protocol().isEmpty())
return base;
ContainerNode* parent = parentNode();
if (!parent)
return base;
const KURL& parentBase = parent->baseURI();
if (parentBase.isNull())
return base;
return KURL(parentBase, baseAttribute);
}
void Element::createAttributeMap() const
{
m_attributeMap = NamedNodeMap::create(const_cast<Element*>(this));
}
bool Element::isURLAttribute(Attribute*) const
{
return false;
}
const QualifiedName& Element::imageSourceAttributeName() const
{
return srcAttr;
}
RenderObject* Element::createRenderer(RenderArena* arena, RenderStyle* style)
{
if (document()->documentElement() == this && style->display() == NONE) {
RenderBlock* result = new (arena) RenderBlock(this);
if (result)
result->setAnimatableStyle(style);
return result;
}
return RenderObject::createObject(this, style);
}
bool Element::wasChangedSinceLastFormControlChangeEvent() const
{
return false;
}
void Element::setChangedSinceLastFormControlChangeEvent(bool)
{
}
void Element::willRemove()
{
#if ENABLE(FULLSCREEN_API)
if (containsFullScreenElement() && parentElement())
document()->setContainsFullScreenElementRecursively(parentElement(), false);
#endif
ContainerNode::willRemove();
}
void Element::insertedIntoDocument()
{
ContainerNode::insertedIntoDocument();
if (ShadowRoot* shadow = shadowRoot())
shadow->insertedIntoDocument();
if (hasID()) {
if (m_attributeMap) {
Attribute* idItem = m_attributeMap->getAttributeItem(document()->idAttributeName());
if (idItem && !idItem->isNull())
updateId(nullAtom, idItem->value());
}
}
}
void Element::removedFromDocument()
{
if (hasID()) {
if (m_attributeMap) {
Attribute* idItem = m_attributeMap->getAttributeItem(document()->idAttributeName());
if (idItem && !idItem->isNull())
updateId(idItem->value(), nullAtom);
}
}
ContainerNode::removedFromDocument();
if (ShadowRoot* shadow = shadowRoot())
shadow->removedFromDocument();
}
void Element::insertedIntoTree(bool deep)
{
ContainerNode::insertedIntoTree(deep);
if (!deep)
return;
if (ShadowRoot* shadow = shadowRoot())
shadow->insertedIntoTree(true);
#if ENABLE(FULLSCREEN_API)
if (parentElement() && containsFullScreenElement() && !parentElement()->containsFullScreenElement())
document()->setContainsFullScreenElementRecursively(parentElement(), true);
#endif
}
void Element::removedFromTree(bool deep)
{
ContainerNode::removedFromTree(deep);
if (!deep)
return;
if (ShadowRoot* shadow = shadowRoot())
shadow->removedFromTree(true);
}
void Element::attach()
{
suspendPostAttachCallbacks();
RenderWidget::suspendWidgetHierarchyUpdates();
createRendererIfNeeded();
StyleSelectorParentPusher parentPusher(this);
if (firstChild())
parentPusher.push();
ContainerNode::attach();
if (Node* shadow = shadowRoot()) {
parentPusher.push();
shadow->attach();
}
if (hasRareData()) {
ElementRareData* data = rareData();
if (data->needsFocusAppearanceUpdateSoonAfterAttach()) {
if (isFocusable() && document()->focusedNode() == this)
document()->updateFocusAppearanceSoon(false );
data->setNeedsFocusAppearanceUpdateSoonAfterAttach(false);
}
}
RenderWidget::resumeWidgetHierarchyUpdates();
resumePostAttachCallbacks();
}
void Element::detach()
{
RenderWidget::suspendWidgetHierarchyUpdates();
cancelFocusAppearanceUpdate();
if (hasRareData())
rareData()->resetComputedStyle();
ContainerNode::detach();
if (ShadowRoot* shadow = shadowRoot())
shadow->detach();
RenderWidget::resumeWidgetHierarchyUpdates();
}
bool Element::pseudoStyleCacheIsInvalid(const RenderStyle* currentStyle, RenderStyle* newStyle)
{
ASSERT(currentStyle == renderStyle());
if (!renderer() || !currentStyle)
return false;
const PseudoStyleCache* pseudoStyleCache = currentStyle->cachedPseudoStyles();
if (!pseudoStyleCache)
return false;
size_t cacheSize = pseudoStyleCache->size();
for (size_t i = 0; i < cacheSize; ++i) {
RefPtr<RenderStyle> newPseudoStyle;
PseudoId pseudoId = pseudoStyleCache->at(i)->styleType();
if (pseudoId == VISITED_LINK) {
newPseudoStyle = newStyle->getCachedPseudoStyle(VISITED_LINK); if (!newPseudoStyle || *newPseudoStyle != *pseudoStyleCache->at(i))
return true;
} else if (pseudoId == FIRST_LINE || pseudoId == FIRST_LINE_INHERITED)
newPseudoStyle = renderer()->uncachedFirstLineStyle(newStyle);
else
newPseudoStyle = renderer()->getUncachedPseudoStyle(pseudoId, newStyle, newStyle);
if (!newPseudoStyle)
return true;
if (*newPseudoStyle != *pseudoStyleCache->at(i)) {
if (pseudoId < FIRST_INTERNAL_PSEUDOID)
newStyle->setHasPseudoStyle(pseudoId);
newStyle->addCachedPseudoStyle(newPseudoStyle);
if (pseudoId == FIRST_LINE || pseudoId == FIRST_LINE_INHERITED) {
renderer()->setNeedsLayoutAndPrefWidthsRecalc();
}
return true;
}
}
return false;
}
void Element::recalcStyle(StyleChange change)
{
RefPtr<RenderStyle> currentStyle(renderStyle());
bool hasParentStyle = parentNodeForRenderingAndStyle() ? parentNodeForRenderingAndStyle()->renderStyle() : false;
bool hasDirectAdjacentRules = currentStyle && currentStyle->childrenAffectedByDirectAdjacentRules();
bool hasIndirectAdjacentRules = currentStyle && currentStyle->childrenAffectedByForwardPositionalRules();
if ((change > NoChange || needsStyleRecalc())) {
if (hasRareData()) {
ElementRareData* data = rareData();
data->resetComputedStyle();
data->m_styleAffectedByEmpty = false;
}
}
if (hasParentStyle && (change >= Inherit || needsStyleRecalc())) {
RefPtr<RenderStyle> newStyle = document()->styleSelector()->styleForElement(this);
StyleChange ch = diff(currentStyle.get(), newStyle.get());
if (ch == Detach || !currentStyle) {
if (attached())
detach();
attach(); clearNeedsStyleRecalc();
clearChildNeedsStyleRecalc();
return;
}
if (currentStyle) {
if (currentStyle->affectedByHoverRules())
newStyle->setAffectedByHoverRules(true);
if (currentStyle->affectedByActiveRules())
newStyle->setAffectedByActiveRules(true);
if (currentStyle->affectedByDragRules())
newStyle->setAffectedByDragRules(true);
if (currentStyle->childrenAffectedByForwardPositionalRules())
newStyle->setChildrenAffectedByForwardPositionalRules();
if (currentStyle->childrenAffectedByBackwardPositionalRules())
newStyle->setChildrenAffectedByBackwardPositionalRules();
if (currentStyle->childrenAffectedByFirstChildRules())
newStyle->setChildrenAffectedByFirstChildRules();
if (currentStyle->childrenAffectedByLastChildRules())
newStyle->setChildrenAffectedByLastChildRules();
if (currentStyle->childrenAffectedByDirectAdjacentRules())
newStyle->setChildrenAffectedByDirectAdjacentRules();
}
if (ch != NoChange || pseudoStyleCacheIsInvalid(currentStyle.get(), newStyle.get()) || (change == Force && renderer() && renderer()->requiresForcedStyleRecalcPropagation())) {
setRenderStyle(newStyle);
} else if (needsStyleRecalc() && styleChangeType() != SyntheticStyleChange) {
if (renderer())
renderer()->setStyleInternal(newStyle.get());
else
setRenderStyle(newStyle);
} else if (styleChangeType() == SyntheticStyleChange)
setRenderStyle(newStyle);
if (change != Force) {
if (document()->usesRemUnits() && ch != NoChange && currentStyle && newStyle && currentStyle->fontSize() != newStyle->fontSize() && document()->documentElement() == this)
change = Force;
else if (styleChangeType() >= FullStyleChange)
change = Force;
else
change = ch;
}
}
StyleSelectorParentPusher parentPusher(this);
bool forceCheckOfNextElementSibling = false;
bool forceCheckOfAnyElementSibling = false;
for (Node *n = firstChild(); n; n = n->nextSibling()) {
bool childRulesChanged = n->needsStyleRecalc() && n->styleChangeType() == FullStyleChange;
if ((forceCheckOfNextElementSibling || forceCheckOfAnyElementSibling) && n->isElementNode())
n->setNeedsStyleRecalc();
if (change >= Inherit || n->isTextNode() || n->childNeedsStyleRecalc() || n->needsStyleRecalc()) {
parentPusher.push();
n->recalcStyle(change);
}
if (n->isElementNode()) {
forceCheckOfNextElementSibling = childRulesChanged && hasDirectAdjacentRules;
forceCheckOfAnyElementSibling = forceCheckOfAnyElementSibling || (childRulesChanged && hasIndirectAdjacentRules);
}
}
if (ShadowRoot* shadow = shadowRoot()) {
if (change >= Inherit || shadow->childNeedsStyleRecalc() || shadow->needsStyleRecalc()) {
parentPusher.push();
shadow->recalcStyle(change);
}
}
clearNeedsStyleRecalc();
clearChildNeedsStyleRecalc();
}
ShadowRoot* Element::shadowRoot() const
{
return hasRareData() ? rareData()->m_shadowRoot : 0;
}
ShadowRoot* Element::ensureShadowRoot()
{
if (ShadowRoot* existingRoot = shadowRoot())
return existingRoot;
RefPtr<ShadowRoot> newRoot = ShadowRoot::create(document());
ensureRareData()->m_shadowRoot = newRoot.get();
InspectorInstrumentation::willInsertDOMNode(document(), newRoot.get(), this);
newRoot->setShadowHost(this);
if (inDocument())
newRoot->insertedIntoDocument();
if (attached())
newRoot->lazyAttach();
InspectorInstrumentation::didInsertDOMNode(document(), newRoot.get());
return newRoot.get();
}
void Element::removeShadowRoot()
{
if (!hasRareData())
return;
ElementRareData* data = rareData();
if (RefPtr<Node> oldRoot = data->m_shadowRoot) {
InspectorInstrumentation::willRemoveDOMNode(document(), this);
data->m_shadowRoot = 0;
document()->removeFocusedNodeOfSubtree(oldRoot.get());
oldRoot->setShadowHost(0);
if (oldRoot->inDocument())
oldRoot->removedFromDocument();
else
oldRoot->removedFromTree(true);
}
}
bool Element::childTypeAllowed(NodeType type) const
{
switch (type) {
case ELEMENT_NODE:
case TEXT_NODE:
case COMMENT_NODE:
case PROCESSING_INSTRUCTION_NODE:
case CDATA_SECTION_NODE:
case ENTITY_REFERENCE_NODE:
return true;
default:
break;
}
return false;
}
static void checkForEmptyStyleChange(Element* element, RenderStyle* style)
{
if (!style && !element->styleAffectedByEmpty())
return;
if (!style || (style->affectedByEmpty() && (!style->emptyState() || element->hasChildNodes())))
element->setNeedsStyleRecalc();
}
static void checkForSiblingStyleChanges(Element* e, RenderStyle* style, bool finishedParsingCallback,
Node* beforeChange, Node* afterChange, int childCountDelta)
{
if (!style || (e->needsStyleRecalc() && style->childrenAffectedByPositionalRules()))
return;
if (style->childrenAffectedByFirstChildRules() && afterChange) {
Node* newFirstChild = 0;
for (newFirstChild = e->firstChild(); newFirstChild && !newFirstChild->isElementNode(); newFirstChild = newFirstChild->nextSibling()) {};
Node* firstElementAfterInsertion = 0;
for (firstElementAfterInsertion = afterChange;
firstElementAfterInsertion && !firstElementAfterInsertion->isElementNode();
firstElementAfterInsertion = firstElementAfterInsertion->nextSibling()) {};
if (newFirstChild != firstElementAfterInsertion && firstElementAfterInsertion && firstElementAfterInsertion->attached() &&
firstElementAfterInsertion->renderStyle() && firstElementAfterInsertion->renderStyle()->firstChildState())
firstElementAfterInsertion->setNeedsStyleRecalc();
if (childCountDelta < 0 && newFirstChild == firstElementAfterInsertion && newFirstChild && newFirstChild->renderStyle() && !newFirstChild->renderStyle()->firstChildState())
newFirstChild->setNeedsStyleRecalc();
}
if (style->childrenAffectedByLastChildRules() && beforeChange) {
Node* newLastChild = 0;
for (newLastChild = e->lastChild(); newLastChild && !newLastChild->isElementNode(); newLastChild = newLastChild->previousSibling()) {};
Node* lastElementBeforeInsertion = 0;
for (lastElementBeforeInsertion = beforeChange;
lastElementBeforeInsertion && !lastElementBeforeInsertion->isElementNode();
lastElementBeforeInsertion = lastElementBeforeInsertion->previousSibling()) {};
if (newLastChild != lastElementBeforeInsertion && lastElementBeforeInsertion && lastElementBeforeInsertion->attached() &&
lastElementBeforeInsertion->renderStyle() && lastElementBeforeInsertion->renderStyle()->lastChildState())
lastElementBeforeInsertion->setNeedsStyleRecalc();
if ((childCountDelta < 0 || finishedParsingCallback) && newLastChild == lastElementBeforeInsertion && newLastChild && newLastChild->renderStyle() && !newLastChild->renderStyle()->lastChildState())
newLastChild->setNeedsStyleRecalc();
}
if (style->childrenAffectedByDirectAdjacentRules() && afterChange) {
Node* firstElementAfterInsertion = 0;
for (firstElementAfterInsertion = afterChange;
firstElementAfterInsertion && !firstElementAfterInsertion->isElementNode();
firstElementAfterInsertion = firstElementAfterInsertion->nextSibling()) {};
if (firstElementAfterInsertion && firstElementAfterInsertion->attached())
firstElementAfterInsertion->setNeedsStyleRecalc();
}
if ((style->childrenAffectedByForwardPositionalRules() && afterChange) ||
(style->childrenAffectedByBackwardPositionalRules() && beforeChange))
e->setNeedsStyleRecalc();
checkForEmptyStyleChange(e, style);
}
void Element::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
{
ContainerNode::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
if (changedByParser)
checkForEmptyStyleChange(this, renderStyle());
else
checkForSiblingStyleChanges(this, renderStyle(), false, beforeChange, afterChange, childCountDelta);
}
void Element::beginParsingChildren()
{
clearIsParsingChildrenFinished();
CSSStyleSelector* styleSelector = document()->styleSelectorIfExists();
if (styleSelector && attached())
styleSelector->pushParent(this);
}
void Element::finishParsingChildren()
{
ContainerNode::finishParsingChildren();
setIsParsingChildrenFinished();
checkForSiblingStyleChanges(this, renderStyle(), true, lastChild(), 0, 0);
if (CSSStyleSelector* styleSelector = document()->styleSelectorIfExists())
styleSelector->popParent(this);
}
void Element::dispatchAttrRemovalEvent(Attribute*)
{
ASSERT(!eventDispatchForbidden());
#if 0
if (!document()->hasListenerType(Document::DOMATTRMODIFIED_LISTENER))
return;
ExceptionCode ec = 0;
dispatchScopedEvent(MutationEvent::create(DOMAttrModifiedEvent, true, attr, attr->value(),
attr->value(), document()->attrName(attr->id()), MutationEvent::REMOVAL), ec);
#endif
}
void Element::dispatchAttrAdditionEvent(Attribute*)
{
ASSERT(!eventDispatchForbidden());
#if 0
if (!document()->hasListenerType(Document::DOMATTRMODIFIED_LISTENER))
return;
ExceptionCode ec = 0;
dispatchScopedEvent(MutationEvent::create(DOMAttrModifiedEvent, true, attr, attr->value(),
attr->value(), document()->attrName(attr->id()), MutationEvent::ADDITION), ec);
#endif
}
String Element::openTagStartToString() const
{
String result = "<" + nodeName();
NamedNodeMap* attrMap = attributes(true);
if (attrMap) {
unsigned numAttrs = attrMap->length();
for (unsigned i = 0; i < numAttrs; i++) {
result += " ";
Attribute *attribute = attrMap->attributeItem(i);
result += attribute->name().toString();
if (!attribute->value().isNull()) {
result += "=\"";
result += attribute->value();
result += "\"";
}
}
}
return result;
}
#ifndef NDEBUG
void Element::formatForDebugger(char* buffer, unsigned length) const
{
String result;
String s;
s = nodeName();
if (s.length() > 0) {
result += s;
}
s = getIdAttribute();
if (s.length() > 0) {
if (result.length() > 0)
result += "; ";
result += "id=";
result += s;
}
s = getAttribute(classAttr);
if (s.length() > 0) {
if (result.length() > 0)
result += "; ";
result += "class=";
result += s;
}
strncpy(buffer, result.utf8().data(), length - 1);
}
#endif
PassRefPtr<Attr> Element::setAttributeNode(Attr* attr, ExceptionCode& ec)
{
if (!attr) {
ec = TYPE_MISMATCH_ERR;
return 0;
}
return static_pointer_cast<Attr>(attributes(false)->setNamedItem(attr, ec));
}
PassRefPtr<Attr> Element::setAttributeNodeNS(Attr* attr, ExceptionCode& ec)
{
if (!attr) {
ec = TYPE_MISMATCH_ERR;
return 0;
}
return static_pointer_cast<Attr>(attributes(false)->setNamedItem(attr, ec));
}
PassRefPtr<Attr> Element::removeAttributeNode(Attr* attr, ExceptionCode& ec)
{
if (!attr) {
ec = TYPE_MISMATCH_ERR;
return 0;
}
if (attr->ownerElement() != this) {
ec = NOT_FOUND_ERR;
return 0;
}
ASSERT(document() == attr->document());
NamedNodeMap* attrs = attributes(true);
if (!attrs)
return 0;
return static_pointer_cast<Attr>(attrs->removeNamedItem(attr->qualifiedName(), ec));
}
void Element::setAttributeNS(const AtomicString& namespaceURI, const AtomicString& qualifiedName, const AtomicString& value, ExceptionCode& ec, FragmentScriptingPermission scriptingPermission)
{
String prefix, localName;
if (!Document::parseQualifiedName(qualifiedName, prefix, localName, ec))
return;
if (namespaceURI.isNull() && !prefix.isNull()) {
ec = NAMESPACE_ERR;
return;
}
QualifiedName qName(prefix, localName, namespaceURI);
if (scriptingPermission == FragmentScriptingNotAllowed && (isEventHandlerAttribute(qName) || isAttributeToRemove(qName, value)))
return;
setAttribute(qName, value, ec);
}
void Element::removeAttribute(const String& name, ExceptionCode& ec)
{
InspectorInstrumentation::willModifyDOMAttr(document(), this);
String localName = shouldIgnoreAttributeCase(this) ? name.lower() : name;
if (m_attributeMap) {
m_attributeMap->removeNamedItem(localName, ec);
if (ec == NOT_FOUND_ERR)
ec = 0;
}
InspectorInstrumentation::didModifyDOMAttr(document(), this);
}
void Element::removeAttributeNS(const String& namespaceURI, const String& localName, ExceptionCode& ec)
{
removeAttribute(QualifiedName(nullAtom, localName, namespaceURI), ec);
}
PassRefPtr<Attr> Element::getAttributeNode(const String& name)
{
NamedNodeMap* attrs = attributes(true);
if (!attrs)
return 0;
String localName = shouldIgnoreAttributeCase(this) ? name.lower() : name;
return static_pointer_cast<Attr>(attrs->getNamedItem(localName));
}
PassRefPtr<Attr> Element::getAttributeNodeNS(const String& namespaceURI, const String& localName)
{
NamedNodeMap* attrs = attributes(true);
if (!attrs)
return 0;
return static_pointer_cast<Attr>(attrs->getNamedItem(QualifiedName(nullAtom, localName, namespaceURI)));
}
bool Element::hasAttribute(const String& name) const
{
NamedNodeMap* attrs = attributes(true);
if (!attrs)
return false;
String localName = shouldIgnoreAttributeCase(this) ? name.lower() : name;
return attrs->getAttributeItem(localName, false);
}
bool Element::hasAttributeNS(const String& namespaceURI, const String& localName) const
{
NamedNodeMap* attrs = attributes(true);
if (!attrs)
return false;
return attrs->getAttributeItem(QualifiedName(nullAtom, localName, namespaceURI));
}
CSSStyleDeclaration *Element::style()
{
return 0;
}
void Element::focus(bool restorePreviousSelection)
{
if (!inDocument())
return;
Document* doc = document();
if (doc->focusedNode() == this)
return;
if (doc->haveStylesheetsLoaded()) {
doc->updateLayoutIgnorePendingStylesheets();
if (!isFocusable())
return;
}
if (!supportsFocus())
return;
RefPtr<Node> protect;
if (Page* page = doc->page()) {
protect = this;
if (!page->focusController()->setFocusedNode(this, doc->frame()))
return;
}
doc->updateLayoutIgnorePendingStylesheets();
if (!isFocusable()) {
ensureRareData()->setNeedsFocusAppearanceUpdateSoonAfterAttach(true);
return;
}
cancelFocusAppearanceUpdate();
updateFocusAppearance(restorePreviousSelection);
}
void Element::updateFocusAppearance(bool )
{
if (this == rootEditableElement()) {
Frame* frame = document()->frame();
if (!frame)
return;
if (this == frame->selection()->rootEditableElement())
return;
VisibleSelection newSelection = VisibleSelection(firstPositionInOrBeforeNode(this), DOWNSTREAM);
if (frame->selection()->shouldChangeSelection(newSelection)) {
frame->selection()->setSelection(newSelection);
frame->selection()->revealSelection();
}
} else if (renderer() && !renderer()->isWidget())
renderer()->enclosingLayer()->scrollRectToVisible(getRect());
}
void Element::blur()
{
cancelFocusAppearanceUpdate();
Document* doc = document();
if (doc->focusedNode() == this) {
if (doc->frame())
doc->frame()->page()->focusController()->setFocusedNode(0, doc->frame());
else
doc->setFocusedNode(0);
}
}
String Element::innerText() const
{
document()->updateLayoutIgnorePendingStylesheets();
if (!renderer())
return textContent(true);
return plainText(rangeOfContents(const_cast<Element*>(this)).get());
}
String Element::outerText() const
{
return innerText();
}
String Element::title() const
{
return String();
}
IntSize Element::minimumSizeForResizing() const
{
return hasRareData() ? rareData()->m_minimumSizeForResizing : defaultMinimumSizeForResizing();
}
void Element::setMinimumSizeForResizing(const IntSize& size)
{
if (size == defaultMinimumSizeForResizing() && !hasRareData())
return;
ensureRareData()->m_minimumSizeForResizing = size;
}
RenderStyle* Element::computedStyle(PseudoId pseudoElementSpecifier)
{
if (RenderStyle* usedStyle = renderStyle())
return pseudoElementSpecifier ? usedStyle->getCachedPseudoStyle(pseudoElementSpecifier) : usedStyle;
if (!attached())
return 0;
ElementRareData* data = ensureRareData();
if (!data->m_computedStyle)
data->m_computedStyle = document()->styleForElementIgnoringPendingStylesheets(this);
return pseudoElementSpecifier ? data->m_computedStyle->getCachedPseudoStyle(pseudoElementSpecifier) : data->m_computedStyle.get();
}
void Element::setStyleAffectedByEmpty()
{
ElementRareData* data = ensureRareData();
data->m_styleAffectedByEmpty = true;
}
bool Element::styleAffectedByEmpty() const
{
return hasRareData() && rareData()->m_styleAffectedByEmpty;
}
AtomicString Element::computeInheritedLanguage() const
{
const Node* n = this;
AtomicString value;
while (n && value.isNull()) {
if (n->isElementNode()) {
value = static_cast<const Element*>(n)->fastGetAttribute(XMLNames::langAttr);
if (value.isNull())
value = static_cast<const Element*>(n)->fastGetAttribute(HTMLNames::langAttr);
} else if (n->isDocumentNode()) {
value = static_cast<const Document*>(n)->contentLanguage();
}
n = n->parentNode();
}
return value;
}
void Element::cancelFocusAppearanceUpdate()
{
if (hasRareData())
rareData()->setNeedsFocusAppearanceUpdateSoonAfterAttach(false);
if (document()->focusedNode() == this)
document()->cancelFocusAppearanceUpdate();
}
void Element::normalizeAttributes()
{
NamedNodeMap* attrs = attributes(true);
if (!attrs)
return;
if (attrs->isEmpty())
return;
Vector<RefPtr<Attribute> > attributeVector;
attrs->copyAttributesToVector(attributeVector);
size_t numAttrs = attributeVector.size();
for (size_t i = 0; i < numAttrs; ++i) {
if (Attr* attr = attributeVector[i]->attr())
attr->normalize();
}
}
Element* Element::firstElementChild() const
{
return WebCore::firstElementChild(this);
}
Element* Element::lastElementChild() const
{
Node* n = lastChild();
while (n && !n->isElementNode())
n = n->previousSibling();
return static_cast<Element*>(n);
}
Element* Element::previousElementSibling() const
{
Node* n = previousSibling();
while (n && !n->isElementNode())
n = n->previousSibling();
return static_cast<Element*>(n);
}
Element* Element::nextElementSibling() const
{
Node* n = nextSibling();
while (n && !n->isElementNode())
n = n->nextSibling();
return static_cast<Element*>(n);
}
unsigned Element::childElementCount() const
{
unsigned count = 0;
Node* n = firstChild();
while (n) {
count += n->isElementNode();
n = n->nextSibling();
}
return count;
}
bool Element::webkitMatchesSelector(const String& selector, ExceptionCode& ec)
{
if (selector.isEmpty()) {
ec = SYNTAX_ERR;
return false;
}
bool strictParsing = !document()->inQuirksMode();
CSSParser p(strictParsing);
CSSSelectorList selectorList;
p.parseSelector(selector, document(), selectorList);
if (!selectorList.first()) {
ec = SYNTAX_ERR;
return false;
}
if (selectorList.selectorsNeedNamespaceResolution()) {
ec = NAMESPACE_ERR;
return false;
}
CSSStyleSelector::SelectorChecker selectorChecker(document(), strictParsing);
for (CSSSelector* selector = selectorList.first(); selector; selector = CSSSelectorList::next(selector)) {
if (selectorChecker.checkSelector(selector, this))
return true;
}
return false;
}
DOMTokenList* Element::classList()
{
ElementRareData* data = ensureRareData();
if (!data->m_classList)
data->m_classList = ClassList::create(this);
return data->m_classList.get();
}
DOMTokenList* Element::optionalClassList() const
{
if (!hasRareData())
return 0;
return rareData()->m_classList.get();
}
DOMStringMap* Element::dataset()
{
ElementRareData* data = ensureRareData();
if (!data->m_datasetDOMStringMap)
data->m_datasetDOMStringMap = DatasetDOMStringMap::create(this);
return data->m_datasetDOMStringMap.get();
}
KURL Element::getURLAttribute(const QualifiedName& name) const
{
#if !ASSERT_DISABLED
if (m_attributeMap) {
if (Attribute* attribute = m_attributeMap->getAttributeItem(name))
ASSERT(isURLAttribute(attribute));
}
#endif
return document()->completeURL(stripLeadingAndTrailingHTMLSpaces(getAttribute(name)));
}
KURL Element::getNonEmptyURLAttribute(const QualifiedName& name) const
{
#if !ASSERT_DISABLED
if (m_attributeMap) {
if (Attribute* attribute = m_attributeMap->getAttributeItem(name))
ASSERT(isURLAttribute(attribute));
}
#endif
String value = stripLeadingAndTrailingHTMLSpaces(getAttribute(name));
if (value.isEmpty())
return KURL();
return document()->completeURL(value);
}
int Element::getIntegralAttribute(const QualifiedName& attributeName) const
{
return getAttribute(attributeName).string().toInt();
}
void Element::setIntegralAttribute(const QualifiedName& attributeName, int value)
{
ExceptionCode ec;
setAttribute(attributeName, String::number(value), ec);
}
unsigned Element::getUnsignedIntegralAttribute(const QualifiedName& attributeName) const
{
return getAttribute(attributeName).string().toUInt();
}
void Element::setUnsignedIntegralAttribute(const QualifiedName& attributeName, unsigned value)
{
ExceptionCode ec;
setAttribute(attributeName, String::number(value), ec);
}
#if ENABLE(SVG)
bool Element::childShouldCreateRenderer(Node* child) const
{
if (child->isSVGElement())
return child->hasTagName(SVGNames::svgTag) || isSVGElement();
return Node::childShouldCreateRenderer(child);
}
#endif
#if ENABLE(FULLSCREEN_API)
void Element::webkitRequestFullScreen(unsigned short flags)
{
document()->requestFullScreenForElement(this, flags, Document::EnforceIFrameAllowFulScreenRequirement);
}
bool Element::containsFullScreenElement() const
{
return hasRareData() ? rareData()->m_containsFullScreenElement : false;
}
void Element::setContainsFullScreenElement(bool flag)
{
ensureRareData()->m_containsFullScreenElement = flag;
setNeedsStyleRecalc(SyntheticStyleChange);
}
#endif
SpellcheckAttributeState Element::spellcheckAttributeState() const
{
if (!hasAttribute(HTMLNames::spellcheckAttr))
return SpellcheckAttributeDefault;
const AtomicString& value = getAttribute(HTMLNames::spellcheckAttr);
if (equalIgnoringCase(value, "true") || equalIgnoringCase(value, ""))
return SpellcheckAttributeTrue;
if (equalIgnoringCase(value, "false"))
return SpellcheckAttributeFalse;
return SpellcheckAttributeDefault;
}
bool Element::isSpellCheckingEnabled() const
{
const Element* element = this;
while (element) {
switch (element->spellcheckAttributeState()) {
case SpellcheckAttributeTrue:
return true;
case SpellcheckAttributeFalse:
return false;
case SpellcheckAttributeDefault:
break;
}
ContainerNode* parent = const_cast<Element*>(element)->parentOrHostNode();
element = (parent && parent->isElementNode()) ? toElement(parent) : 0;
}
return true;
}
PassRefPtr<WebKitAnimationList> Element::webkitGetAnimations() const
{
if (!renderer())
return 0;
AnimationController* animController = renderer()->animation();
if (!animController)
return 0;
return animController->animationsForRenderer(renderer());
}
}