Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
| 3 | * (C) 1999 Antti Koivisto (koivisto@kde.org) |
| 4 | * (C) 2001 Peter Kelly (pmk@post.com) |
| 5 | * (C) 2001 Dirk Mueller (mueller@kde.org) |
| 6 | * (C) 2007 David Smith (catfish.man@gmail.com) |
| 7 | * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012, 2013 Apple Inc. All rights reserved. |
| 8 | * (C) 2007 Eric Seidel (eric@webkit.org) |
| 9 | * |
| 10 | * This library is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU Library General Public |
| 12 | * License as published by the Free Software Foundation; either |
| 13 | * version 2 of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This library is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * Library General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Library General Public License |
| 21 | * along with this library; see the file COPYING.LIB. If not, write to |
| 22 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 23 | * Boston, MA 02110-1301, USA. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "core/dom/Element.h" |
| 28 | |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 29 | #include "CSSPropertyNames.h" |
| 30 | #include "CSSValueKeywords.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 31 | #include "HTMLNames.h" |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 32 | #include "RuntimeEnabledFeatures.h" |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 33 | #include "SVGNames.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 34 | #include "XMLNames.h" |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 35 | #include "bindings/v8/ExceptionState.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 36 | #include "core/accessibility/AXObjectCache.h" |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 37 | #include "core/animation/DocumentTimeline.h" |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 38 | #include "core/css/CSSParser.h" |
| 39 | #include "core/css/CSSStyleSheet.h" |
| 40 | #include "core/css/CSSValuePool.h" |
| 41 | #include "core/css/PropertySetCSSStyleDeclaration.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 42 | #include "core/css/StylePropertySet.h" |
Torne (Richard Coles) | 81a5157 | 2013-05-13 16:52:28 +0100 | [diff] [blame] | 43 | #include "core/css/resolver/StyleResolver.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 44 | #include "core/dom/Attr.h" |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 45 | #include "core/dom/Attribute.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 46 | #include "core/dom/ClientRect.h" |
| 47 | #include "core/dom/ClientRectList.h" |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 48 | #include "core/dom/CustomElement.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 49 | #include "core/dom/CustomElementRegistrationContext.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 50 | #include "core/dom/DatasetDOMStringMap.h" |
| 51 | #include "core/dom/Document.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 52 | #include "core/dom/DocumentSharedObjectPool.h" |
| 53 | #include "core/dom/ElementRareData.h" |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 54 | #include "core/dom/EventDispatcher.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 55 | #include "core/dom/ExceptionCode.h" |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 56 | #include "core/dom/FocusEvent.h" |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 57 | #include "core/dom/FullscreenElementStack.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 58 | #include "core/dom/MutationObserverInterestGroup.h" |
| 59 | #include "core/dom/MutationRecord.h" |
| 60 | #include "core/dom/NamedNodeMap.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 61 | #include "core/dom/NodeRenderStyle.h" |
| 62 | #include "core/dom/NodeRenderingContext.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 63 | #include "core/dom/PseudoElement.h" |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 64 | #include "core/dom/ScriptableDocumentParser.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 65 | #include "core/dom/SelectorQuery.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 66 | #include "core/dom/Text.h" |
Torne (Richard Coles) | e524955 | 2013-05-15 11:35:13 +0100 | [diff] [blame] | 67 | #include "core/dom/shadow/InsertionPoint.h" |
| 68 | #include "core/dom/shadow/ShadowRoot.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 69 | #include "core/editing/FrameSelection.h" |
| 70 | #include "core/editing/TextIterator.h" |
| 71 | #include "core/editing/htmlediting.h" |
| 72 | #include "core/html/ClassList.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 73 | #include "core/html/HTMLCollection.h" |
| 74 | #include "core/html/HTMLDocument.h" |
| 75 | #include "core/html/HTMLElement.h" |
| 76 | #include "core/html/HTMLFormControlsCollection.h" |
| 77 | #include "core/html/HTMLFrameOwnerElement.h" |
| 78 | #include "core/html/HTMLLabelElement.h" |
| 79 | #include "core/html/HTMLOptionsCollection.h" |
| 80 | #include "core/html/HTMLTableRowsCollection.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 81 | #include "core/html/parser/HTMLParserIdioms.h" |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 82 | #include "core/page/ContentSecurityPolicy.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 83 | #include "core/page/FocusController.h" |
| 84 | #include "core/page/Frame.h" |
| 85 | #include "core/page/FrameView.h" |
| 86 | #include "core/page/Page.h" |
| 87 | #include "core/page/PointerLockController.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 88 | #include "core/rendering/FlowThreadController.h" |
| 89 | #include "core/rendering/RenderRegion.h" |
| 90 | #include "core/rendering/RenderView.h" |
| 91 | #include "core/rendering/RenderWidget.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 92 | #include "core/svg/SVGDocumentExtensions.h" |
| 93 | #include "core/svg/SVGElement.h" |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 94 | #include "wtf/BitVector.h" |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 95 | #include "wtf/HashFunctions.h" |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 96 | #include "wtf/text/CString.h" |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 97 | #include "wtf/text/TextPosition.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 98 | |
| 99 | namespace WebCore { |
| 100 | |
| 101 | using namespace HTMLNames; |
| 102 | using namespace XMLNames; |
| 103 | |
| 104 | static inline bool shouldIgnoreAttributeCase(const Element* e) |
| 105 | { |
| 106 | return e && e->document()->isHTMLDocument() && e->isHTMLElement(); |
| 107 | } |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 108 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 109 | class StyleResolverParentPusher { |
| 110 | public: |
| 111 | StyleResolverParentPusher(Element* parent) |
| 112 | : m_parent(parent) |
| 113 | , m_pushedStyleResolver(0) |
| 114 | { |
| 115 | } |
| 116 | void push() |
| 117 | { |
| 118 | if (m_pushedStyleResolver) |
| 119 | return; |
| 120 | m_pushedStyleResolver = m_parent->document()->styleResolver(); |
| 121 | m_pushedStyleResolver->pushParentElement(m_parent); |
| 122 | } |
| 123 | ~StyleResolverParentPusher() |
| 124 | { |
| 125 | |
| 126 | if (!m_pushedStyleResolver) |
| 127 | return; |
| 128 | |
| 129 | // This tells us that our pushed style selector is in a bad state, |
| 130 | // so we should just bail out in that scenario. |
| 131 | ASSERT(m_pushedStyleResolver == m_parent->document()->styleResolver()); |
| 132 | if (m_pushedStyleResolver != m_parent->document()->styleResolver()) |
| 133 | return; |
| 134 | |
| 135 | m_pushedStyleResolver->popParentElement(m_parent); |
| 136 | } |
| 137 | |
| 138 | private: |
| 139 | Element* m_parent; |
| 140 | StyleResolver* m_pushedStyleResolver; |
| 141 | }; |
| 142 | |
| 143 | typedef Vector<RefPtr<Attr> > AttrNodeList; |
| 144 | typedef HashMap<Element*, OwnPtr<AttrNodeList> > AttrNodeListMap; |
| 145 | |
| 146 | static AttrNodeListMap& attrNodeListMap() |
| 147 | { |
| 148 | DEFINE_STATIC_LOCAL(AttrNodeListMap, map, ()); |
| 149 | return map; |
| 150 | } |
| 151 | |
| 152 | static AttrNodeList* attrNodeListForElement(Element* element) |
| 153 | { |
| 154 | if (!element->hasSyntheticAttrChildNodes()) |
| 155 | return 0; |
| 156 | ASSERT(attrNodeListMap().contains(element)); |
| 157 | return attrNodeListMap().get(element); |
| 158 | } |
| 159 | |
| 160 | static AttrNodeList* ensureAttrNodeListForElement(Element* element) |
| 161 | { |
| 162 | if (element->hasSyntheticAttrChildNodes()) { |
| 163 | ASSERT(attrNodeListMap().contains(element)); |
| 164 | return attrNodeListMap().get(element); |
| 165 | } |
| 166 | ASSERT(!attrNodeListMap().contains(element)); |
| 167 | element->setHasSyntheticAttrChildNodes(true); |
| 168 | AttrNodeListMap::AddResult result = attrNodeListMap().add(element, adoptPtr(new AttrNodeList)); |
| 169 | return result.iterator->value.get(); |
| 170 | } |
| 171 | |
| 172 | static void removeAttrNodeListForElement(Element* element) |
| 173 | { |
| 174 | ASSERT(element->hasSyntheticAttrChildNodes()); |
| 175 | ASSERT(attrNodeListMap().contains(element)); |
| 176 | attrNodeListMap().remove(element); |
| 177 | element->setHasSyntheticAttrChildNodes(false); |
| 178 | } |
| 179 | |
| 180 | static Attr* findAttrNodeInList(AttrNodeList* attrNodeList, const QualifiedName& name) |
| 181 | { |
| 182 | for (unsigned i = 0; i < attrNodeList->size(); ++i) { |
| 183 | if (attrNodeList->at(i)->qualifiedName() == name) |
| 184 | return attrNodeList->at(i).get(); |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 189 | PassRefPtr<Element> Element::create(const QualifiedName& tagName, Document* document) |
| 190 | { |
| 191 | return adoptRef(new Element(tagName, document, CreateElement)); |
| 192 | } |
| 193 | |
| 194 | Element::~Element() |
| 195 | { |
| 196 | #ifndef NDEBUG |
| 197 | if (document() && document()->renderer()) { |
| 198 | // When the document is not destroyed, an element that was part of a named flow |
| 199 | // content nodes should have been removed from the content nodes collection |
| 200 | // and the inNamedFlow flag reset. |
| 201 | ASSERT(!inNamedFlow()); |
| 202 | } |
| 203 | #endif |
| 204 | |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 205 | if (PropertySetCSSStyleDeclaration* cssomWrapper = inlineStyleCSSOMWrapper()) |
| 206 | cssomWrapper->clearParentElement(); |
| 207 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 208 | if (hasRareData()) { |
| 209 | ElementRareData* data = elementRareData(); |
| 210 | data->setPseudoElement(BEFORE, 0); |
| 211 | data->setPseudoElement(AFTER, 0); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 212 | data->setPseudoElement(BACKDROP, 0); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 213 | data->clearShadow(); |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 214 | |
| 215 | if (RuntimeEnabledFeatures::webAnimationsCSSEnabled()) { |
| 216 | if (ActiveAnimations* activeAnimations = data->activeAnimations()) |
| 217 | activeAnimations->cssAnimations()->cancel(); |
| 218 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 219 | } |
| 220 | |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 221 | if (isCustomElement()) |
| 222 | CustomElement::wasDestroyed(this); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 223 | |
| 224 | if (hasSyntheticAttrChildNodes()) |
| 225 | detachAllAttrNodesFromElement(); |
| 226 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 227 | if (hasPendingResources()) { |
| 228 | document()->accessSVGExtensions()->removeElementFromPendingResources(this); |
| 229 | ASSERT(!hasPendingResources()); |
| 230 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | inline ElementRareData* Element::elementRareData() const |
| 234 | { |
| 235 | ASSERT(hasRareData()); |
| 236 | return static_cast<ElementRareData*>(rareData()); |
| 237 | } |
| 238 | |
| 239 | inline ElementRareData* Element::ensureElementRareData() |
| 240 | { |
| 241 | return static_cast<ElementRareData*>(ensureRareData()); |
| 242 | } |
| 243 | |
| 244 | void Element::clearTabIndexExplicitlyIfNeeded() |
| 245 | { |
| 246 | if (hasRareData()) |
| 247 | elementRareData()->clearTabIndexExplicitly(); |
| 248 | } |
| 249 | |
| 250 | void Element::setTabIndexExplicitly(short tabIndex) |
| 251 | { |
| 252 | ensureElementRareData()->setTabIndexExplicitly(tabIndex); |
| 253 | } |
| 254 | |
| 255 | bool Element::supportsFocus() const |
| 256 | { |
| 257 | return hasRareData() && elementRareData()->tabIndexSetExplicitly(); |
| 258 | } |
| 259 | |
| 260 | short Element::tabIndex() const |
| 261 | { |
| 262 | return hasRareData() ? elementRareData()->tabIndex() : 0; |
| 263 | } |
| 264 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 265 | bool Element::rendererIsFocusable() const |
| 266 | { |
| 267 | // Elements in canvas fallback content are not rendered, but they are allowed to be |
| 268 | // focusable as long as their canvas is displayed and visible. |
| 269 | if (isInCanvasSubtree()) { |
| 270 | const Element* e = this; |
| 271 | while (e && !e->hasLocalName(canvasTag)) |
| 272 | e = e->parentElement(); |
| 273 | ASSERT(e); |
| 274 | return e->renderer() && e->renderer()->style()->visibility() == VISIBLE; |
| 275 | } |
| 276 | |
| 277 | // FIXME: These asserts should be in Node::isFocusable, but there are some |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 278 | // callsites like Document::setFocusedElement that would currently fail on |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 279 | // them. See crbug.com/251163 |
| 280 | if (renderer()) { |
| 281 | ASSERT(!renderer()->needsLayout()); |
| 282 | } else { |
| 283 | // We can't just use needsStyleRecalc() because if the node is in a |
| 284 | // display:none tree it might say it needs style recalc but the whole |
| 285 | // document is actually up to date. |
| 286 | ASSERT(!document()->childNeedsStyleRecalc()); |
| 287 | } |
| 288 | |
| 289 | // FIXME: Even if we are not visible, we might have a child that is visible. |
| 290 | // Hyatt wants to fix that some day with a "has visible content" flag or the like. |
| 291 | if (!renderer() || renderer()->style()->visibility() != VISIBLE) |
| 292 | return false; |
| 293 | |
| 294 | return true; |
| 295 | } |
| 296 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 297 | DEFINE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(Element, blur); |
| 298 | DEFINE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(Element, error); |
| 299 | DEFINE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(Element, focus); |
| 300 | DEFINE_VIRTUAL_ATTRIBUTE_EVENT_LISTENER(Element, load); |
| 301 | |
| 302 | PassRefPtr<Node> Element::cloneNode(bool deep) |
| 303 | { |
| 304 | return deep ? cloneElementWithChildren() : cloneElementWithoutChildren(); |
| 305 | } |
| 306 | |
| 307 | PassRefPtr<Element> Element::cloneElementWithChildren() |
| 308 | { |
| 309 | RefPtr<Element> clone = cloneElementWithoutChildren(); |
| 310 | cloneChildNodes(clone.get()); |
| 311 | return clone.release(); |
| 312 | } |
| 313 | |
| 314 | PassRefPtr<Element> Element::cloneElementWithoutChildren() |
| 315 | { |
| 316 | RefPtr<Element> clone = cloneElementWithoutAttributesAndChildren(); |
| 317 | // This will catch HTML elements in the wrong namespace that are not correctly copied. |
| 318 | // This is a sanity check as HTML overloads some of the DOM methods. |
| 319 | ASSERT(isHTMLElement() == clone->isHTMLElement()); |
| 320 | |
| 321 | clone->cloneDataFromElement(*this); |
| 322 | return clone.release(); |
| 323 | } |
| 324 | |
| 325 | PassRefPtr<Element> Element::cloneElementWithoutAttributesAndChildren() |
| 326 | { |
| 327 | return document()->createElement(tagQName(), false); |
| 328 | } |
| 329 | |
| 330 | PassRefPtr<Attr> Element::detachAttribute(size_t index) |
| 331 | { |
| 332 | ASSERT(elementData()); |
Torne (Richard Coles) | e524955 | 2013-05-15 11:35:13 +0100 | [diff] [blame] | 333 | const Attribute* attribute = elementData()->attributeItem(index); |
| 334 | RefPtr<Attr> attrNode = attrIfExists(attribute->name()); |
| 335 | if (attrNode) |
| 336 | detachAttrNodeAtIndex(attrNode.get(), index); |
| 337 | else { |
| 338 | attrNode = Attr::create(document(), attribute->name(), attribute->value()); |
| 339 | removeAttributeInternal(index, NotInSynchronizationOfLazyAttribute); |
| 340 | } |
| 341 | return attrNode.release(); |
| 342 | } |
| 343 | |
| 344 | void Element::detachAttrNodeAtIndex(Attr* attr, size_t index) |
| 345 | { |
| 346 | ASSERT(attr); |
| 347 | ASSERT(elementData()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 348 | |
| 349 | const Attribute* attribute = elementData()->attributeItem(index); |
| 350 | ASSERT(attribute); |
Torne (Richard Coles) | e524955 | 2013-05-15 11:35:13 +0100 | [diff] [blame] | 351 | ASSERT(attribute->name() == attr->qualifiedName()); |
| 352 | detachAttrNodeFromElementWithValue(attr, attribute->value()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 353 | removeAttributeInternal(index, NotInSynchronizationOfLazyAttribute); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | void Element::removeAttribute(const QualifiedName& name) |
| 357 | { |
| 358 | if (!elementData()) |
| 359 | return; |
| 360 | |
| 361 | size_t index = elementData()->getAttributeItemIndex(name); |
| 362 | if (index == notFound) |
| 363 | return; |
| 364 | |
| 365 | removeAttributeInternal(index, NotInSynchronizationOfLazyAttribute); |
| 366 | } |
| 367 | |
| 368 | void Element::setBooleanAttribute(const QualifiedName& name, bool value) |
| 369 | { |
| 370 | if (value) |
| 371 | setAttribute(name, emptyAtom); |
| 372 | else |
| 373 | removeAttribute(name); |
| 374 | } |
| 375 | |
| 376 | NamedNodeMap* Element::attributes() const |
| 377 | { |
| 378 | ElementRareData* rareData = const_cast<Element*>(this)->ensureElementRareData(); |
| 379 | if (NamedNodeMap* attributeMap = rareData->attributeMap()) |
| 380 | return attributeMap; |
| 381 | |
| 382 | rareData->setAttributeMap(NamedNodeMap::create(const_cast<Element*>(this))); |
| 383 | return rareData->attributeMap(); |
| 384 | } |
| 385 | |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 386 | ActiveAnimations* Element::activeAnimations() const |
Torne (Richard Coles) | 81a5157 | 2013-05-13 16:52:28 +0100 | [diff] [blame] | 387 | { |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 388 | if (hasActiveAnimations()) |
| 389 | return elementRareData()->activeAnimations(); |
| 390 | return 0; |
Torne (Richard Coles) | 81a5157 | 2013-05-13 16:52:28 +0100 | [diff] [blame] | 391 | } |
| 392 | |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 393 | ActiveAnimations* Element::ensureActiveAnimations() |
Torne (Richard Coles) | 81a5157 | 2013-05-13 16:52:28 +0100 | [diff] [blame] | 394 | { |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 395 | ElementRareData* rareData = ensureElementRareData(); |
| 396 | if (!elementRareData()->activeAnimations()) |
| 397 | rareData->setActiveAnimations(adoptPtr(new ActiveAnimations())); |
| 398 | return rareData->activeAnimations(); |
Torne (Richard Coles) | 81a5157 | 2013-05-13 16:52:28 +0100 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | bool Element::hasActiveAnimations() const |
| 402 | { |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 403 | if (!RuntimeEnabledFeatures::webAnimationsEnabled()) |
| 404 | return false; |
Torne (Richard Coles) | 81a5157 | 2013-05-13 16:52:28 +0100 | [diff] [blame] | 405 | |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 406 | if (!hasRareData()) |
| 407 | return false; |
| 408 | |
| 409 | ActiveAnimations* activeAnimations = elementRareData()->activeAnimations(); |
| 410 | return activeAnimations && !activeAnimations->isEmpty(); |
Torne (Richard Coles) | 81a5157 | 2013-05-13 16:52:28 +0100 | [diff] [blame] | 411 | } |
| 412 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 413 | Node::NodeType Element::nodeType() const |
| 414 | { |
| 415 | return ELEMENT_NODE; |
| 416 | } |
| 417 | |
| 418 | bool Element::hasAttribute(const QualifiedName& name) const |
| 419 | { |
| 420 | return hasAttributeNS(name.namespaceURI(), name.localName()); |
| 421 | } |
| 422 | |
| 423 | void Element::synchronizeAllAttributes() const |
| 424 | { |
| 425 | if (!elementData()) |
| 426 | return; |
| 427 | if (elementData()->m_styleAttributeIsDirty) { |
| 428 | ASSERT(isStyledElement()); |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 429 | synchronizeStyleAttributeInternal(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 430 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 431 | if (elementData()->m_animatedSVGAttributesAreDirty) { |
| 432 | ASSERT(isSVGElement()); |
| 433 | toSVGElement(this)->synchronizeAnimatedSVGAttribute(anyQName()); |
| 434 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | inline void Element::synchronizeAttribute(const QualifiedName& name) const |
| 438 | { |
| 439 | if (!elementData()) |
| 440 | return; |
| 441 | if (UNLIKELY(name == styleAttr && elementData()->m_styleAttributeIsDirty)) { |
| 442 | ASSERT(isStyledElement()); |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 443 | synchronizeStyleAttributeInternal(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 444 | return; |
| 445 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 446 | if (UNLIKELY(elementData()->m_animatedSVGAttributesAreDirty)) { |
| 447 | ASSERT(isSVGElement()); |
| 448 | toSVGElement(this)->synchronizeAnimatedSVGAttribute(name); |
| 449 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | inline void Element::synchronizeAttribute(const AtomicString& localName) const |
| 453 | { |
| 454 | // This version of synchronizeAttribute() is streamlined for the case where you don't have a full QualifiedName, |
| 455 | // e.g when called from DOM API. |
| 456 | if (!elementData()) |
| 457 | return; |
| 458 | if (elementData()->m_styleAttributeIsDirty && equalPossiblyIgnoringCase(localName, styleAttr.localName(), shouldIgnoreAttributeCase(this))) { |
| 459 | ASSERT(isStyledElement()); |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 460 | synchronizeStyleAttributeInternal(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 461 | return; |
| 462 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 463 | if (elementData()->m_animatedSVGAttributesAreDirty) { |
| 464 | // We're not passing a namespace argument on purpose. SVGNames::*Attr are defined w/o namespaces as well. |
| 465 | ASSERT(isSVGElement()); |
| 466 | static_cast<const SVGElement*>(this)->synchronizeAnimatedSVGAttribute(QualifiedName(nullAtom, localName, nullAtom)); |
| 467 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | const AtomicString& Element::getAttribute(const QualifiedName& name) const |
| 471 | { |
| 472 | if (!elementData()) |
| 473 | return nullAtom; |
| 474 | synchronizeAttribute(name); |
| 475 | if (const Attribute* attribute = getAttributeItem(name)) |
| 476 | return attribute->value(); |
| 477 | return nullAtom; |
| 478 | } |
| 479 | |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 480 | void Element::scrollIntoView(bool alignToTop) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 481 | { |
| 482 | document()->updateLayoutIgnorePendingStylesheets(); |
| 483 | |
| 484 | if (!renderer()) |
| 485 | return; |
| 486 | |
| 487 | LayoutRect bounds = boundingBox(); |
| 488 | // Align to the top / bottom and to the closest edge. |
| 489 | if (alignToTop) |
| 490 | renderer()->scrollRectToVisible(bounds, ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignTopAlways); |
| 491 | else |
| 492 | renderer()->scrollRectToVisible(bounds, ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignBottomAlways); |
| 493 | } |
| 494 | |
| 495 | void Element::scrollIntoViewIfNeeded(bool centerIfNeeded) |
| 496 | { |
| 497 | document()->updateLayoutIgnorePendingStylesheets(); |
| 498 | |
| 499 | if (!renderer()) |
| 500 | return; |
| 501 | |
| 502 | LayoutRect bounds = boundingBox(); |
| 503 | if (centerIfNeeded) |
| 504 | renderer()->scrollRectToVisible(bounds, ScrollAlignment::alignCenterIfNeeded, ScrollAlignment::alignCenterIfNeeded); |
| 505 | else |
| 506 | renderer()->scrollRectToVisible(bounds, ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignToEdgeIfNeeded); |
| 507 | } |
| 508 | |
| 509 | void Element::scrollByUnits(int units, ScrollGranularity granularity) |
| 510 | { |
| 511 | document()->updateLayoutIgnorePendingStylesheets(); |
| 512 | |
| 513 | if (!renderer()) |
| 514 | return; |
| 515 | |
| 516 | if (!renderer()->hasOverflowClip()) |
| 517 | return; |
| 518 | |
| 519 | ScrollDirection direction = ScrollDown; |
| 520 | if (units < 0) { |
| 521 | direction = ScrollUp; |
| 522 | units = -units; |
| 523 | } |
| 524 | Node* stopNode = this; |
| 525 | toRenderBox(renderer())->scroll(direction, granularity, units, &stopNode); |
| 526 | } |
| 527 | |
| 528 | void Element::scrollByLines(int lines) |
| 529 | { |
| 530 | scrollByUnits(lines, ScrollByLine); |
| 531 | } |
| 532 | |
| 533 | void Element::scrollByPages(int pages) |
| 534 | { |
| 535 | scrollByUnits(pages, ScrollByPage); |
| 536 | } |
| 537 | |
| 538 | static float localZoomForRenderer(RenderObject* renderer) |
| 539 | { |
| 540 | // FIXME: This does the wrong thing if two opposing zooms are in effect and canceled each |
| 541 | // other out, but the alternative is that we'd have to crawl up the whole render tree every |
| 542 | // time (or store an additional bit in the RenderStyle to indicate that a zoom was specified). |
| 543 | float zoomFactor = 1; |
| 544 | if (renderer->style()->effectiveZoom() != 1) { |
| 545 | // Need to find the nearest enclosing RenderObject that set up |
| 546 | // a differing zoom, and then we divide our result by it to eliminate the zoom. |
| 547 | RenderObject* prev = renderer; |
| 548 | for (RenderObject* curr = prev->parent(); curr; curr = curr->parent()) { |
| 549 | if (curr->style()->effectiveZoom() != prev->style()->effectiveZoom()) { |
| 550 | zoomFactor = prev->style()->zoom(); |
| 551 | break; |
| 552 | } |
| 553 | prev = curr; |
| 554 | } |
| 555 | if (prev->isRenderView()) |
| 556 | zoomFactor = prev->style()->zoom(); |
| 557 | } |
| 558 | return zoomFactor; |
| 559 | } |
| 560 | |
| 561 | static int adjustForLocalZoom(LayoutUnit value, RenderObject* renderer) |
| 562 | { |
| 563 | float zoomFactor = localZoomForRenderer(renderer); |
| 564 | if (zoomFactor == 1) |
| 565 | return value; |
| 566 | return lroundf(value / zoomFactor); |
| 567 | } |
| 568 | |
| 569 | int Element::offsetLeft() |
| 570 | { |
| 571 | document()->updateLayoutIgnorePendingStylesheets(); |
| 572 | if (RenderBoxModelObject* renderer = renderBoxModelObject()) |
| 573 | return adjustForLocalZoom(renderer->pixelSnappedOffsetLeft(), renderer); |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | int Element::offsetTop() |
| 578 | { |
| 579 | document()->updateLayoutIgnorePendingStylesheets(); |
| 580 | if (RenderBoxModelObject* renderer = renderBoxModelObject()) |
| 581 | return adjustForLocalZoom(renderer->pixelSnappedOffsetTop(), renderer); |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | int Element::offsetWidth() |
| 586 | { |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 587 | document()->updateStyleForNodeIfNeeded(this); |
| 588 | |
| 589 | if (RenderBox* renderer = renderBox()) { |
| 590 | if (!renderer->requiresLayoutToDetermineWidth()) |
| 591 | return adjustLayoutUnitForAbsoluteZoom(renderer->fixedOffsetWidth(), renderer).round(); |
| 592 | } |
| 593 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 594 | document()->updateLayoutIgnorePendingStylesheets(); |
| 595 | if (RenderBoxModelObject* renderer = renderBoxModelObject()) |
| 596 | return adjustLayoutUnitForAbsoluteZoom(renderer->pixelSnappedOffsetWidth(), renderer).round(); |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | int Element::offsetHeight() |
| 601 | { |
| 602 | document()->updateLayoutIgnorePendingStylesheets(); |
| 603 | if (RenderBoxModelObject* renderer = renderBoxModelObject()) |
| 604 | return adjustLayoutUnitForAbsoluteZoom(renderer->pixelSnappedOffsetHeight(), renderer).round(); |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | Element* Element::bindingsOffsetParent() |
| 609 | { |
| 610 | Element* element = offsetParent(); |
| 611 | if (!element || !element->isInShadowTree()) |
| 612 | return element; |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 613 | return element->containingShadowRoot()->shouldExposeToBindings() ? element : 0; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | Element* Element::offsetParent() |
| 617 | { |
| 618 | document()->updateLayoutIgnorePendingStylesheets(); |
| 619 | if (RenderObject* renderer = this->renderer()) |
| 620 | return renderer->offsetParent(); |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | int Element::clientLeft() |
| 625 | { |
| 626 | document()->updateLayoutIgnorePendingStylesheets(); |
| 627 | |
| 628 | if (RenderBox* renderer = renderBox()) |
| 629 | return adjustForAbsoluteZoom(roundToInt(renderer->clientLeft()), renderer); |
| 630 | return 0; |
| 631 | } |
| 632 | |
| 633 | int Element::clientTop() |
| 634 | { |
| 635 | document()->updateLayoutIgnorePendingStylesheets(); |
| 636 | |
| 637 | if (RenderBox* renderer = renderBox()) |
| 638 | return adjustForAbsoluteZoom(roundToInt(renderer->clientTop()), renderer); |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | int Element::clientWidth() |
| 643 | { |
| 644 | document()->updateLayoutIgnorePendingStylesheets(); |
| 645 | |
| 646 | // When in strict mode, clientWidth for the document element should return the width of the containing frame. |
| 647 | // When in quirks mode, clientWidth for the body element should return the width of the containing frame. |
| 648 | bool inQuirksMode = document()->inQuirksMode(); |
| 649 | if ((!inQuirksMode && document()->documentElement() == this) || |
| 650 | (inQuirksMode && isHTMLElement() && document()->body() == this)) { |
| 651 | if (FrameView* view = document()->view()) { |
| 652 | if (RenderView* renderView = document()->renderView()) |
| 653 | return adjustForAbsoluteZoom(view->layoutWidth(), renderView); |
| 654 | } |
| 655 | } |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 656 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 657 | if (RenderBox* renderer = renderBox()) |
| 658 | return adjustLayoutUnitForAbsoluteZoom(renderer->pixelSnappedClientWidth(), renderer).round(); |
| 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | int Element::clientHeight() |
| 663 | { |
| 664 | document()->updateLayoutIgnorePendingStylesheets(); |
| 665 | |
| 666 | // When in strict mode, clientHeight for the document element should return the height of the containing frame. |
| 667 | // When in quirks mode, clientHeight for the body element should return the height of the containing frame. |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 668 | bool inQuirksMode = document()->inQuirksMode(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 669 | |
| 670 | if ((!inQuirksMode && document()->documentElement() == this) || |
| 671 | (inQuirksMode && isHTMLElement() && document()->body() == this)) { |
| 672 | if (FrameView* view = document()->view()) { |
| 673 | if (RenderView* renderView = document()->renderView()) |
| 674 | return adjustForAbsoluteZoom(view->layoutHeight(), renderView); |
| 675 | } |
| 676 | } |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 677 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 678 | if (RenderBox* renderer = renderBox()) |
| 679 | return adjustLayoutUnitForAbsoluteZoom(renderer->pixelSnappedClientHeight(), renderer).round(); |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | int Element::scrollLeft() |
| 684 | { |
| 685 | document()->updateLayoutIgnorePendingStylesheets(); |
| 686 | if (RenderBox* rend = renderBox()) |
| 687 | return adjustForAbsoluteZoom(rend->scrollLeft(), rend); |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | int Element::scrollTop() |
| 692 | { |
| 693 | document()->updateLayoutIgnorePendingStylesheets(); |
| 694 | if (RenderBox* rend = renderBox()) |
| 695 | return adjustForAbsoluteZoom(rend->scrollTop(), rend); |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | void Element::setScrollLeft(int newLeft) |
| 700 | { |
| 701 | document()->updateLayoutIgnorePendingStylesheets(); |
| 702 | if (RenderBox* rend = renderBox()) |
| 703 | rend->setScrollLeft(static_cast<int>(newLeft * rend->style()->effectiveZoom())); |
| 704 | } |
| 705 | |
| 706 | void Element::setScrollTop(int newTop) |
| 707 | { |
| 708 | document()->updateLayoutIgnorePendingStylesheets(); |
| 709 | if (RenderBox* rend = renderBox()) |
| 710 | rend->setScrollTop(static_cast<int>(newTop * rend->style()->effectiveZoom())); |
| 711 | } |
| 712 | |
| 713 | int Element::scrollWidth() |
| 714 | { |
| 715 | document()->updateLayoutIgnorePendingStylesheets(); |
| 716 | if (RenderBox* rend = renderBox()) |
| 717 | return adjustForAbsoluteZoom(rend->scrollWidth(), rend); |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | int Element::scrollHeight() |
| 722 | { |
| 723 | document()->updateLayoutIgnorePendingStylesheets(); |
| 724 | if (RenderBox* rend = renderBox()) |
| 725 | return adjustForAbsoluteZoom(rend->scrollHeight(), rend); |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | IntRect Element::boundsInRootViewSpace() |
| 730 | { |
| 731 | document()->updateLayoutIgnorePendingStylesheets(); |
| 732 | |
| 733 | FrameView* view = document()->view(); |
| 734 | if (!view) |
| 735 | return IntRect(); |
| 736 | |
| 737 | Vector<FloatQuad> quads; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 738 | if (isSVGElement() && renderer()) { |
| 739 | // Get the bounding rectangle from the SVG model. |
| 740 | SVGElement* svgElement = toSVGElement(this); |
| 741 | FloatRect localRect; |
| 742 | if (svgElement->getBoundingBox(localRect)) |
| 743 | quads.append(renderer()->localToAbsoluteQuad(localRect)); |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 744 | } else { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 745 | // Get the bounding rectangle from the box model. |
| 746 | if (renderBoxModelObject()) |
| 747 | renderBoxModelObject()->absoluteQuads(quads); |
| 748 | } |
| 749 | |
| 750 | if (quads.isEmpty()) |
| 751 | return IntRect(); |
| 752 | |
| 753 | IntRect result = quads[0].enclosingBoundingBox(); |
| 754 | for (size_t i = 1; i < quads.size(); ++i) |
| 755 | result.unite(quads[i].enclosingBoundingBox()); |
| 756 | |
| 757 | result = view->contentsToRootView(result); |
| 758 | return result; |
| 759 | } |
| 760 | |
| 761 | PassRefPtr<ClientRectList> Element::getClientRects() |
| 762 | { |
| 763 | document()->updateLayoutIgnorePendingStylesheets(); |
| 764 | |
| 765 | RenderBoxModelObject* renderBoxModelObject = this->renderBoxModelObject(); |
| 766 | if (!renderBoxModelObject) |
| 767 | return ClientRectList::create(); |
| 768 | |
| 769 | // FIXME: Handle SVG elements. |
| 770 | // FIXME: Handle table/inline-table with a caption. |
| 771 | |
| 772 | Vector<FloatQuad> quads; |
| 773 | renderBoxModelObject->absoluteQuads(quads); |
| 774 | document()->adjustFloatQuadsForScrollAndAbsoluteZoom(quads, renderBoxModelObject); |
| 775 | return ClientRectList::create(quads); |
| 776 | } |
| 777 | |
| 778 | PassRefPtr<ClientRect> Element::getBoundingClientRect() |
| 779 | { |
| 780 | document()->updateLayoutIgnorePendingStylesheets(); |
| 781 | |
| 782 | Vector<FloatQuad> quads; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 783 | if (isSVGElement() && renderer() && !renderer()->isSVGRoot()) { |
| 784 | // Get the bounding rectangle from the SVG model. |
| 785 | SVGElement* svgElement = toSVGElement(this); |
| 786 | FloatRect localRect; |
| 787 | if (svgElement->getBoundingBox(localRect)) |
| 788 | quads.append(renderer()->localToAbsoluteQuad(localRect)); |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 789 | } else { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 790 | // Get the bounding rectangle from the box model. |
| 791 | if (renderBoxModelObject()) |
| 792 | renderBoxModelObject()->absoluteQuads(quads); |
| 793 | } |
| 794 | |
| 795 | if (quads.isEmpty()) |
| 796 | return ClientRect::create(); |
| 797 | |
| 798 | FloatRect result = quads[0].boundingBox(); |
| 799 | for (size_t i = 1; i < quads.size(); ++i) |
| 800 | result.unite(quads[i].boundingBox()); |
| 801 | |
| 802 | document()->adjustFloatRectForScrollAndAbsoluteZoom(result, renderer()); |
| 803 | return ClientRect::create(result); |
| 804 | } |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 805 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 806 | IntRect Element::screenRect() const |
| 807 | { |
| 808 | if (!renderer()) |
| 809 | return IntRect(); |
| 810 | // FIXME: this should probably respect transforms |
| 811 | return document()->view()->contentsToScreen(renderer()->absoluteBoundingBoxRectIgnoringTransforms()); |
| 812 | } |
| 813 | |
| 814 | const AtomicString& Element::getAttribute(const AtomicString& localName) const |
| 815 | { |
| 816 | if (!elementData()) |
| 817 | return nullAtom; |
| 818 | synchronizeAttribute(localName); |
| 819 | if (const Attribute* attribute = elementData()->getAttributeItem(localName, shouldIgnoreAttributeCase(this))) |
| 820 | return attribute->value(); |
| 821 | return nullAtom; |
| 822 | } |
| 823 | |
| 824 | const AtomicString& Element::getAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName) const |
| 825 | { |
| 826 | return getAttribute(QualifiedName(nullAtom, localName, namespaceURI)); |
| 827 | } |
| 828 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 829 | void Element::setAttribute(const AtomicString& localName, const AtomicString& value, ExceptionState& es) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 830 | { |
| 831 | if (!Document::isValidName(localName)) { |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 832 | es.throwDOMException(InvalidCharacterError); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 833 | return; |
| 834 | } |
| 835 | |
| 836 | synchronizeAttribute(localName); |
| 837 | const AtomicString& caseAdjustedLocalName = shouldIgnoreAttributeCase(this) ? localName.lower() : localName; |
| 838 | |
| 839 | size_t index = elementData() ? elementData()->getAttributeItemIndex(caseAdjustedLocalName, false) : notFound; |
| 840 | const QualifiedName& qName = index != notFound ? attributeItem(index)->name() : QualifiedName(nullAtom, caseAdjustedLocalName, nullAtom); |
| 841 | setAttributeInternal(index, qName, value, NotInSynchronizationOfLazyAttribute); |
| 842 | } |
| 843 | |
| 844 | void Element::setAttribute(const QualifiedName& name, const AtomicString& value) |
| 845 | { |
| 846 | synchronizeAttribute(name); |
| 847 | size_t index = elementData() ? elementData()->getAttributeItemIndex(name) : notFound; |
| 848 | setAttributeInternal(index, name, value, NotInSynchronizationOfLazyAttribute); |
| 849 | } |
| 850 | |
| 851 | void Element::setSynchronizedLazyAttribute(const QualifiedName& name, const AtomicString& value) |
| 852 | { |
| 853 | size_t index = elementData() ? elementData()->getAttributeItemIndex(name) : notFound; |
| 854 | setAttributeInternal(index, name, value, InSynchronizationOfLazyAttribute); |
| 855 | } |
| 856 | |
| 857 | inline void Element::setAttributeInternal(size_t index, const QualifiedName& name, const AtomicString& newValue, SynchronizationOfLazyAttribute inSynchronizationOfLazyAttribute) |
| 858 | { |
| 859 | if (newValue.isNull()) { |
| 860 | if (index != notFound) |
| 861 | removeAttributeInternal(index, inSynchronizationOfLazyAttribute); |
| 862 | return; |
| 863 | } |
| 864 | |
| 865 | if (index == notFound) { |
| 866 | addAttributeInternal(name, newValue, inSynchronizationOfLazyAttribute); |
| 867 | return; |
| 868 | } |
| 869 | |
| 870 | if (!inSynchronizationOfLazyAttribute) |
| 871 | willModifyAttribute(name, attributeItem(index)->value(), newValue); |
| 872 | |
| 873 | if (newValue != attributeItem(index)->value()) { |
| 874 | // If there is an Attr node hooked to this attribute, the Attr::setValue() call below |
| 875 | // will write into the ElementData. |
| 876 | // FIXME: Refactor this so it makes some sense. |
| 877 | if (RefPtr<Attr> attrNode = inSynchronizationOfLazyAttribute ? 0 : attrIfExists(name)) |
| 878 | attrNode->setValue(newValue); |
| 879 | else |
| 880 | ensureUniqueElementData()->attributeItem(index)->setValue(newValue); |
| 881 | } |
| 882 | |
| 883 | if (!inSynchronizationOfLazyAttribute) |
| 884 | didModifyAttribute(name, newValue); |
| 885 | } |
| 886 | |
| 887 | static inline AtomicString makeIdForStyleResolution(const AtomicString& value, bool inQuirksMode) |
| 888 | { |
| 889 | if (inQuirksMode) |
| 890 | return value.lower(); |
| 891 | return value; |
| 892 | } |
| 893 | |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 894 | static bool checkNeedsStyleInvalidationForIdChange(const AtomicString& oldId, const AtomicString& newId, const RuleFeatureSet& features) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 895 | { |
| 896 | ASSERT(newId != oldId); |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 897 | if (!oldId.isEmpty() && features.hasSelectorForId(oldId)) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 898 | return true; |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 899 | if (!newId.isEmpty() && features.hasSelectorForId(newId)) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 900 | return true; |
| 901 | return false; |
| 902 | } |
| 903 | |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 904 | void Element::attributeChanged(const QualifiedName& name, const AtomicString& newValue, AttributeModificationReason reason) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 905 | { |
| 906 | if (ElementShadow* parentElementShadow = shadowOfParentForDistribution(this)) { |
| 907 | if (shouldInvalidateDistributionWhenAttributeChanged(parentElementShadow, name, newValue)) |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 908 | parentElementShadow->setNeedsDistributionRecalc(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | parseAttribute(name, newValue); |
| 912 | |
| 913 | document()->incDOMTreeVersion(); |
| 914 | |
| 915 | StyleResolver* styleResolver = document()->styleResolverIfExists(); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 916 | bool testShouldInvalidateStyle = attached() && styleResolver && styleChangeType() < SubtreeStyleChange; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 917 | bool shouldInvalidateStyle = false; |
| 918 | |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 919 | if (isStyledElement() && name == styleAttr) { |
| 920 | styleAttributeChanged(newValue, reason); |
| 921 | } else if (isStyledElement() && isPresentationAttribute(name)) { |
| 922 | elementData()->m_presentationAttributeStyleIsDirty = true; |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 923 | setNeedsStyleRecalc(LocalStyleChange); |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 924 | } |
| 925 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 926 | if (isIdAttributeName(name)) { |
| 927 | AtomicString oldId = elementData()->idForStyleResolution(); |
| 928 | AtomicString newId = makeIdForStyleResolution(newValue, document()->inQuirksMode()); |
| 929 | if (newId != oldId) { |
| 930 | elementData()->setIdForStyleResolution(newId); |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 931 | shouldInvalidateStyle = testShouldInvalidateStyle && checkNeedsStyleInvalidationForIdChange(oldId, newId, styleResolver->ruleFeatureSet()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 932 | } |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 933 | } else if (name == classAttr) { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 934 | classAttributeChanged(newValue); |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 935 | } else if (name == HTMLNames::nameAttr) { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 936 | setHasName(!newValue.isNull()); |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 937 | } else if (name == HTMLNames::pseudoAttr) { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 938 | shouldInvalidateStyle |= testShouldInvalidateStyle && isInShadowTree(); |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 939 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 940 | |
| 941 | invalidateNodeListCachesInAncestors(&name, this); |
| 942 | |
| 943 | // If there is currently no StyleResolver, we can't be sure that this attribute change won't affect style. |
| 944 | shouldInvalidateStyle |= !styleResolver; |
| 945 | |
| 946 | if (shouldInvalidateStyle) |
| 947 | setNeedsStyleRecalc(); |
| 948 | |
| 949 | if (AXObjectCache* cache = document()->existingAXObjectCache()) |
| 950 | cache->handleAttributeChanged(name, this); |
| 951 | } |
| 952 | |
| 953 | inline void Element::attributeChangedFromParserOrByCloning(const QualifiedName& name, const AtomicString& newValue, AttributeModificationReason reason) |
| 954 | { |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 955 | if (name == isAttr) |
Ben Murdoch | 0019e4e | 2013-07-18 11:57:54 +0100 | [diff] [blame] | 956 | CustomElementRegistrationContext::setTypeExtension(this, newValue); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 957 | attributeChanged(name, newValue, reason); |
| 958 | } |
| 959 | |
| 960 | template <typename CharacterType> |
| 961 | static inline bool classStringHasClassName(const CharacterType* characters, unsigned length) |
| 962 | { |
| 963 | ASSERT(length > 0); |
| 964 | |
| 965 | unsigned i = 0; |
| 966 | do { |
| 967 | if (isNotHTMLSpace(characters[i])) |
| 968 | break; |
| 969 | ++i; |
| 970 | } while (i < length); |
| 971 | |
| 972 | return i < length; |
| 973 | } |
| 974 | |
| 975 | static inline bool classStringHasClassName(const AtomicString& newClassString) |
| 976 | { |
| 977 | unsigned length = newClassString.length(); |
| 978 | |
| 979 | if (!length) |
| 980 | return false; |
| 981 | |
| 982 | if (newClassString.is8Bit()) |
| 983 | return classStringHasClassName(newClassString.characters8(), length); |
| 984 | return classStringHasClassName(newClassString.characters16(), length); |
| 985 | } |
| 986 | |
| 987 | template<typename Checker> |
| 988 | static bool checkSelectorForClassChange(const SpaceSplitString& changedClasses, const Checker& checker) |
| 989 | { |
| 990 | unsigned changedSize = changedClasses.size(); |
| 991 | for (unsigned i = 0; i < changedSize; ++i) { |
| 992 | if (checker.hasSelectorForClass(changedClasses[i])) |
| 993 | return true; |
| 994 | } |
| 995 | return false; |
| 996 | } |
| 997 | |
| 998 | template<typename Checker> |
| 999 | static bool checkSelectorForClassChange(const SpaceSplitString& oldClasses, const SpaceSplitString& newClasses, const Checker& checker) |
| 1000 | { |
| 1001 | unsigned oldSize = oldClasses.size(); |
| 1002 | if (!oldSize) |
| 1003 | return checkSelectorForClassChange(newClasses, checker); |
| 1004 | BitVector remainingClassBits; |
| 1005 | remainingClassBits.ensureSize(oldSize); |
| 1006 | // Class vectors tend to be very short. This is faster than using a hash table. |
| 1007 | unsigned newSize = newClasses.size(); |
| 1008 | for (unsigned i = 0; i < newSize; ++i) { |
| 1009 | for (unsigned j = 0; j < oldSize; ++j) { |
| 1010 | if (newClasses[i] == oldClasses[j]) { |
| 1011 | remainingClassBits.quickSet(j); |
| 1012 | continue; |
| 1013 | } |
| 1014 | } |
| 1015 | if (checker.hasSelectorForClass(newClasses[i])) |
| 1016 | return true; |
| 1017 | } |
| 1018 | for (unsigned i = 0; i < oldSize; ++i) { |
| 1019 | // If the bit is not set the the corresponding class has been removed. |
| 1020 | if (remainingClassBits.quickGet(i)) |
| 1021 | continue; |
| 1022 | if (checker.hasSelectorForClass(oldClasses[i])) |
| 1023 | return true; |
| 1024 | } |
| 1025 | return false; |
| 1026 | } |
| 1027 | |
| 1028 | void Element::classAttributeChanged(const AtomicString& newClassString) |
| 1029 | { |
| 1030 | StyleResolver* styleResolver = document()->styleResolverIfExists(); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 1031 | bool testShouldInvalidateStyle = attached() && styleResolver && styleChangeType() < SubtreeStyleChange; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1032 | bool shouldInvalidateStyle = false; |
| 1033 | |
| 1034 | if (classStringHasClassName(newClassString)) { |
| 1035 | const bool shouldFoldCase = document()->inQuirksMode(); |
| 1036 | const SpaceSplitString oldClasses = elementData()->classNames(); |
| 1037 | elementData()->setClass(newClassString, shouldFoldCase); |
| 1038 | const SpaceSplitString& newClasses = elementData()->classNames(); |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 1039 | shouldInvalidateStyle = testShouldInvalidateStyle && checkSelectorForClassChange(oldClasses, newClasses, styleResolver->ruleFeatureSet()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1040 | } else { |
| 1041 | const SpaceSplitString& oldClasses = elementData()->classNames(); |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 1042 | shouldInvalidateStyle = testShouldInvalidateStyle && checkSelectorForClassChange(oldClasses, styleResolver->ruleFeatureSet()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1043 | elementData()->clearClass(); |
| 1044 | } |
| 1045 | |
| 1046 | if (hasRareData()) |
| 1047 | elementRareData()->clearClassListValueForQuirksMode(); |
| 1048 | |
| 1049 | if (shouldInvalidateStyle) |
| 1050 | setNeedsStyleRecalc(); |
| 1051 | } |
| 1052 | |
| 1053 | bool Element::shouldInvalidateDistributionWhenAttributeChanged(ElementShadow* elementShadow, const QualifiedName& name, const AtomicString& newValue) |
| 1054 | { |
| 1055 | ASSERT(elementShadow); |
| 1056 | const SelectRuleFeatureSet& featureSet = elementShadow->distributor().ensureSelectFeatureSet(elementShadow); |
| 1057 | |
| 1058 | if (isIdAttributeName(name)) { |
| 1059 | AtomicString oldId = elementData()->idForStyleResolution(); |
| 1060 | AtomicString newId = makeIdForStyleResolution(newValue, document()->inQuirksMode()); |
| 1061 | if (newId != oldId) { |
| 1062 | if (!oldId.isEmpty() && featureSet.hasSelectorForId(oldId)) |
| 1063 | return true; |
| 1064 | if (!newId.isEmpty() && featureSet.hasSelectorForId(newId)) |
| 1065 | return true; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | if (name == HTMLNames::classAttr) { |
| 1070 | const AtomicString& newClassString = newValue; |
| 1071 | if (classStringHasClassName(newClassString)) { |
| 1072 | const bool shouldFoldCase = document()->inQuirksMode(); |
| 1073 | const SpaceSplitString& oldClasses = elementData()->classNames(); |
| 1074 | const SpaceSplitString newClasses(newClassString, shouldFoldCase); |
| 1075 | if (checkSelectorForClassChange(oldClasses, newClasses, featureSet)) |
| 1076 | return true; |
| 1077 | } else { |
| 1078 | const SpaceSplitString& oldClasses = elementData()->classNames(); |
| 1079 | if (checkSelectorForClassChange(oldClasses, featureSet)) |
| 1080 | return true; |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | return featureSet.hasSelectorForAttribute(name.localName()); |
| 1085 | } |
| 1086 | |
| 1087 | // Returns true is the given attribute is an event handler. |
| 1088 | // We consider an event handler any attribute that begins with "on". |
| 1089 | // It is a simple solution that has the advantage of not requiring any |
| 1090 | // code or configuration change if a new event handler is defined. |
| 1091 | |
| 1092 | static inline bool isEventHandlerAttribute(const Attribute& attribute) |
| 1093 | { |
| 1094 | return attribute.name().namespaceURI().isNull() && attribute.name().localName().startsWith("on"); |
| 1095 | } |
| 1096 | |
| 1097 | bool Element::isJavaScriptURLAttribute(const Attribute& attribute) const |
| 1098 | { |
| 1099 | return isURLAttribute(attribute) && protocolIsJavaScript(stripLeadingAndTrailingHTMLSpaces(attribute.value())); |
| 1100 | } |
| 1101 | |
| 1102 | void Element::stripScriptingAttributes(Vector<Attribute>& attributeVector) const |
| 1103 | { |
| 1104 | size_t destination = 0; |
| 1105 | for (size_t source = 0; source < attributeVector.size(); ++source) { |
| 1106 | if (isEventHandlerAttribute(attributeVector[source]) |
| 1107 | || isJavaScriptURLAttribute(attributeVector[source]) |
| 1108 | || isHTMLContentAttribute(attributeVector[source])) |
| 1109 | continue; |
| 1110 | |
| 1111 | if (source != destination) |
| 1112 | attributeVector[destination] = attributeVector[source]; |
| 1113 | |
| 1114 | ++destination; |
| 1115 | } |
| 1116 | attributeVector.shrink(destination); |
| 1117 | } |
| 1118 | |
| 1119 | void Element::parserSetAttributes(const Vector<Attribute>& attributeVector) |
| 1120 | { |
| 1121 | ASSERT(!inDocument()); |
| 1122 | ASSERT(!parentNode()); |
| 1123 | ASSERT(!m_elementData); |
| 1124 | |
| 1125 | if (attributeVector.isEmpty()) |
| 1126 | return; |
| 1127 | |
| 1128 | if (document() && document()->sharedObjectPool()) |
| 1129 | m_elementData = document()->sharedObjectPool()->cachedShareableElementDataWithAttributes(attributeVector); |
| 1130 | else |
| 1131 | m_elementData = ShareableElementData::createWithAttributes(attributeVector); |
| 1132 | |
| 1133 | // Use attributeVector instead of m_elementData because attributeChanged might modify m_elementData. |
| 1134 | for (unsigned i = 0; i < attributeVector.size(); ++i) |
| 1135 | attributeChangedFromParserOrByCloning(attributeVector[i].name(), attributeVector[i].value(), ModifiedDirectly); |
| 1136 | } |
| 1137 | |
| 1138 | bool Element::hasAttributes() const |
| 1139 | { |
| 1140 | synchronizeAllAttributes(); |
| 1141 | return elementData() && elementData()->length(); |
| 1142 | } |
| 1143 | |
| 1144 | bool Element::hasEquivalentAttributes(const Element* other) const |
| 1145 | { |
| 1146 | synchronizeAllAttributes(); |
| 1147 | other->synchronizeAllAttributes(); |
| 1148 | if (elementData() == other->elementData()) |
| 1149 | return true; |
| 1150 | if (elementData()) |
| 1151 | return elementData()->isEquivalent(other->elementData()); |
| 1152 | if (other->elementData()) |
| 1153 | return other->elementData()->isEquivalent(elementData()); |
| 1154 | return true; |
| 1155 | } |
| 1156 | |
| 1157 | String Element::nodeName() const |
| 1158 | { |
| 1159 | return m_tagName.toString(); |
| 1160 | } |
| 1161 | |
| 1162 | String Element::nodeNamePreservingCase() const |
| 1163 | { |
| 1164 | return m_tagName.toString(); |
| 1165 | } |
| 1166 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1167 | void Element::setPrefix(const AtomicString& prefix, ExceptionState& es) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1168 | { |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1169 | es.clearException(); |
| 1170 | checkSetPrefix(prefix, es); |
| 1171 | if (es.hadException()) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1172 | return; |
| 1173 | |
| 1174 | m_tagName.setPrefix(prefix.isEmpty() ? AtomicString() : prefix); |
| 1175 | } |
| 1176 | |
| 1177 | KURL Element::baseURI() const |
| 1178 | { |
| 1179 | const AtomicString& baseAttribute = getAttribute(baseAttr); |
| 1180 | KURL base(KURL(), baseAttribute); |
| 1181 | if (!base.protocol().isEmpty()) |
| 1182 | return base; |
| 1183 | |
| 1184 | ContainerNode* parent = parentNode(); |
| 1185 | if (!parent) |
| 1186 | return base; |
| 1187 | |
| 1188 | const KURL& parentBase = parent->baseURI(); |
| 1189 | if (parentBase.isNull()) |
| 1190 | return base; |
| 1191 | |
| 1192 | return KURL(parentBase, baseAttribute); |
| 1193 | } |
| 1194 | |
| 1195 | const AtomicString& Element::imageSourceURL() const |
| 1196 | { |
| 1197 | return getAttribute(srcAttr); |
| 1198 | } |
| 1199 | |
| 1200 | bool Element::rendererIsNeeded(const NodeRenderingContext& context) |
| 1201 | { |
| 1202 | return context.style()->display() != NONE; |
| 1203 | } |
| 1204 | |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 1205 | RenderObject* Element::createRenderer(RenderStyle* style) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1206 | { |
| 1207 | return RenderObject::createObject(this, style); |
| 1208 | } |
| 1209 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1210 | bool Element::isInert() const |
| 1211 | { |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 1212 | const Element* dialog = document()->activeModalDialog(); |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 1213 | if (dialog && !containsIncludingShadowDOM(dialog) && !dialog->containsIncludingShadowDOM(this)) |
| 1214 | return true; |
| 1215 | return document()->ownerElement() && document()->ownerElement()->isInert(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | Node::InsertionNotificationRequest Element::insertedInto(ContainerNode* insertionPoint) |
| 1219 | { |
| 1220 | // need to do superclass processing first so inDocument() is true |
| 1221 | // by the time we reach updateId |
| 1222 | ContainerNode::insertedInto(insertionPoint); |
| 1223 | |
| 1224 | if (containsFullScreenElement() && parentElement() && !parentElement()->containsFullScreenElement()) |
| 1225 | setContainsFullScreenElementOnAncestorsCrossingFrameBoundaries(true); |
| 1226 | |
| 1227 | if (Element* before = pseudoElement(BEFORE)) |
| 1228 | before->insertedInto(insertionPoint); |
| 1229 | |
| 1230 | if (Element* after = pseudoElement(AFTER)) |
| 1231 | after->insertedInto(insertionPoint); |
| 1232 | |
| 1233 | if (!insertionPoint->isInTreeScope()) |
| 1234 | return InsertionDone; |
| 1235 | |
| 1236 | if (hasRareData()) |
| 1237 | elementRareData()->clearClassListValueForQuirksMode(); |
| 1238 | |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 1239 | if (isUpgradedCustomElement() && inDocument()) |
| 1240 | CustomElement::didEnterDocument(this, document()); |
| 1241 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1242 | TreeScope* scope = insertionPoint->treeScope(); |
| 1243 | if (scope != treeScope()) |
| 1244 | return InsertionDone; |
| 1245 | |
| 1246 | const AtomicString& idValue = getIdAttribute(); |
| 1247 | if (!idValue.isNull()) |
| 1248 | updateId(scope, nullAtom, idValue); |
| 1249 | |
| 1250 | const AtomicString& nameValue = getNameAttribute(); |
| 1251 | if (!nameValue.isNull()) |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1252 | updateName(nullAtom, nameValue); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1253 | |
| 1254 | if (hasTagName(labelTag)) { |
| 1255 | if (scope->shouldCacheLabelsByForAttribute()) |
| 1256 | updateLabel(scope, nullAtom, fastGetAttribute(forAttr)); |
| 1257 | } |
| 1258 | |
| 1259 | return InsertionDone; |
| 1260 | } |
| 1261 | |
| 1262 | void Element::removedFrom(ContainerNode* insertionPoint) |
| 1263 | { |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 1264 | bool wasInDocument = insertionPoint->inDocument(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1265 | |
| 1266 | if (Element* before = pseudoElement(BEFORE)) |
| 1267 | before->removedFrom(insertionPoint); |
| 1268 | |
| 1269 | if (Element* after = pseudoElement(AFTER)) |
| 1270 | after->removedFrom(insertionPoint); |
| 1271 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 1272 | if (Element* backdrop = pseudoElement(BACKDROP)) |
| 1273 | backdrop->removedFrom(insertionPoint); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1274 | document()->removeFromTopLayer(this); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 1275 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1276 | if (containsFullScreenElement()) |
| 1277 | setContainsFullScreenElementOnAncestorsCrossingFrameBoundaries(false); |
| 1278 | |
| 1279 | if (document()->page()) |
| 1280 | document()->page()->pointerLockController()->elementRemoved(this); |
| 1281 | |
| 1282 | setSavedLayerScrollOffset(IntSize()); |
| 1283 | |
| 1284 | if (insertionPoint->isInTreeScope() && treeScope() == document()) { |
| 1285 | const AtomicString& idValue = getIdAttribute(); |
| 1286 | if (!idValue.isNull()) |
| 1287 | updateId(insertionPoint->treeScope(), idValue, nullAtom); |
| 1288 | |
| 1289 | const AtomicString& nameValue = getNameAttribute(); |
| 1290 | if (!nameValue.isNull()) |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1291 | updateName(nameValue, nullAtom); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1292 | |
| 1293 | if (hasTagName(labelTag)) { |
| 1294 | TreeScope* treeScope = insertionPoint->treeScope(); |
| 1295 | if (treeScope->shouldCacheLabelsByForAttribute()) |
| 1296 | updateLabel(treeScope, fastGetAttribute(forAttr), nullAtom); |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | ContainerNode::removedFrom(insertionPoint); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 1301 | if (wasInDocument) { |
| 1302 | if (hasPendingResources()) |
| 1303 | document()->accessSVGExtensions()->removeElementFromPendingResources(this); |
| 1304 | |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 1305 | if (isUpgradedCustomElement()) |
Ben Murdoch | fff8884 | 2013-07-30 15:20:09 +0100 | [diff] [blame] | 1306 | CustomElement::didLeaveDocument(this, insertionPoint->document()); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 1307 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1308 | } |
| 1309 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 1310 | void Element::createRendererIfNeeded(const AttachContext& context) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1311 | { |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 1312 | NodeRenderingContext(this, context.resolvedStyle).createRendererForElementIfNeeded(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1313 | } |
| 1314 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 1315 | void Element::attach(const AttachContext& context) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1316 | { |
| 1317 | PostAttachCallbackDisabler callbackDisabler(this); |
| 1318 | StyleResolverParentPusher parentPusher(this); |
| 1319 | WidgetHierarchyUpdatesSuspensionScope suspendWidgetHierarchyUpdates; |
| 1320 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 1321 | createRendererIfNeeded(context); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1322 | |
| 1323 | if (parentElement() && parentElement()->isInCanvasSubtree()) |
| 1324 | setIsInCanvasSubtree(true); |
| 1325 | |
| 1326 | createPseudoElementIfNeeded(BEFORE); |
| 1327 | |
| 1328 | // When a shadow root exists, it does the work of attaching the children. |
| 1329 | if (ElementShadow* shadow = this->shadow()) { |
| 1330 | parentPusher.push(); |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 1331 | shadow->attach(context); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1332 | } else if (firstChild()) |
| 1333 | parentPusher.push(); |
| 1334 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 1335 | ContainerNode::attach(context); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1336 | |
| 1337 | createPseudoElementIfNeeded(AFTER); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 1338 | createPseudoElementIfNeeded(BACKDROP); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1339 | |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 1340 | if (hasRareData()) { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1341 | ElementRareData* data = elementRareData(); |
| 1342 | if (data->needsFocusAppearanceUpdateSoonAfterAttach()) { |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 1343 | if (isFocusable() && document()->focusedElement() == this) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1344 | document()->updateFocusAppearanceSoon(false /* don't restore selection */); |
| 1345 | data->setNeedsFocusAppearanceUpdateSoonAfterAttach(false); |
| 1346 | } |
| 1347 | } |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 1348 | |
| 1349 | // FIXME: It doesn't appear safe to call didRecalculateStyleForElement when |
| 1350 | // not in a Document::recalcStyle. Since we're hopefully going to always |
| 1351 | // lazyAttach in the future that problem should go away. |
| 1352 | if (document()->inStyleRecalc()) |
| 1353 | InspectorInstrumentation::didRecalculateStyleForElement(this); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | void Element::unregisterNamedFlowContentNode() |
| 1357 | { |
| 1358 | if (RuntimeEnabledFeatures::cssRegionsEnabled() && inNamedFlow() && document()->renderView()) |
| 1359 | document()->renderView()->flowThreadController()->unregisterNamedFlowContentNode(this); |
| 1360 | } |
| 1361 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 1362 | void Element::detach(const AttachContext& context) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1363 | { |
| 1364 | WidgetHierarchyUpdatesSuspensionScope suspendWidgetHierarchyUpdates; |
| 1365 | unregisterNamedFlowContentNode(); |
| 1366 | cancelFocusAppearanceUpdate(); |
| 1367 | if (hasRareData()) { |
| 1368 | ElementRareData* data = elementRareData(); |
| 1369 | data->setPseudoElement(BEFORE, 0); |
| 1370 | data->setPseudoElement(AFTER, 0); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 1371 | data->setPseudoElement(BACKDROP, 0); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1372 | data->setIsInCanvasSubtree(false); |
| 1373 | data->resetComputedStyle(); |
| 1374 | data->resetDynamicRestyleObservations(); |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 1375 | data->setIsInsideRegion(false); |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 1376 | |
| 1377 | if (RuntimeEnabledFeatures::webAnimationsCSSEnabled() && !context.performingReattach) { |
| 1378 | if (ActiveAnimations* activeAnimations = data->activeAnimations()) |
| 1379 | activeAnimations->cssAnimations()->cancel(); |
| 1380 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1381 | } |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 1382 | if (ElementShadow* shadow = this->shadow()) |
| 1383 | shadow->detach(context); |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 1384 | ContainerNode::detach(context); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | bool Element::pseudoStyleCacheIsInvalid(const RenderStyle* currentStyle, RenderStyle* newStyle) |
| 1388 | { |
| 1389 | ASSERT(currentStyle == renderStyle()); |
| 1390 | ASSERT(renderer()); |
| 1391 | |
| 1392 | if (!currentStyle) |
| 1393 | return false; |
| 1394 | |
| 1395 | const PseudoStyleCache* pseudoStyleCache = currentStyle->cachedPseudoStyles(); |
| 1396 | if (!pseudoStyleCache) |
| 1397 | return false; |
| 1398 | |
| 1399 | size_t cacheSize = pseudoStyleCache->size(); |
| 1400 | for (size_t i = 0; i < cacheSize; ++i) { |
| 1401 | RefPtr<RenderStyle> newPseudoStyle; |
| 1402 | PseudoId pseudoId = pseudoStyleCache->at(i)->styleType(); |
| 1403 | if (pseudoId == FIRST_LINE || pseudoId == FIRST_LINE_INHERITED) |
| 1404 | newPseudoStyle = renderer()->uncachedFirstLineStyle(newStyle); |
| 1405 | else |
| 1406 | newPseudoStyle = renderer()->getUncachedPseudoStyle(PseudoStyleRequest(pseudoId), newStyle, newStyle); |
| 1407 | if (!newPseudoStyle) |
| 1408 | return true; |
| 1409 | if (*newPseudoStyle != *pseudoStyleCache->at(i)) { |
| 1410 | if (pseudoId < FIRST_INTERNAL_PSEUDOID) |
| 1411 | newStyle->setHasPseudoStyle(pseudoId); |
| 1412 | newStyle->addCachedPseudoStyle(newPseudoStyle); |
| 1413 | if (pseudoId == FIRST_LINE || pseudoId == FIRST_LINE_INHERITED) { |
| 1414 | // FIXME: We should do an actual diff to determine whether a repaint vs. layout |
| 1415 | // is needed, but for now just assume a layout will be required. The diff code |
| 1416 | // in RenderObject::setStyle would need to be factored out so that it could be reused. |
| 1417 | renderer()->setNeedsLayoutAndPrefWidthsRecalc(); |
| 1418 | } |
| 1419 | return true; |
| 1420 | } |
| 1421 | } |
| 1422 | return false; |
| 1423 | } |
| 1424 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 1425 | PassRefPtr<RenderStyle> Element::styleForRenderer() |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1426 | { |
| 1427 | if (hasCustomStyleCallbacks()) { |
| 1428 | if (RefPtr<RenderStyle> style = customStyleForRenderer()) |
| 1429 | return style.release(); |
| 1430 | } |
| 1431 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 1432 | return originalStyleForRenderer(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1433 | } |
| 1434 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 1435 | PassRefPtr<RenderStyle> Element::originalStyleForRenderer() |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 1436 | { |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 1437 | return document()->styleResolver()->styleForElement(this); |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 1438 | } |
| 1439 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 1440 | bool Element::recalcStyle(StyleChange change) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1441 | { |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 1442 | ASSERT(document()->inStyleRecalc()); |
| 1443 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1444 | if (hasCustomStyleCallbacks()) |
| 1445 | willRecalcStyle(change); |
| 1446 | |
| 1447 | // Ref currentStyle in case it would otherwise be deleted when setting the new style in the renderer. |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 1448 | RefPtr<RenderStyle> currentStyle = renderStyle(); |
| 1449 | bool hasParentStyle = static_cast<bool>(parentRenderStyle()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1450 | bool hasDirectAdjacentRules = childrenAffectedByDirectAdjacentRules(); |
| 1451 | bool hasIndirectAdjacentRules = childrenAffectedByForwardPositionalRules(); |
| 1452 | |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 1453 | if (change > NoChange || needsStyleRecalc()) { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1454 | if (hasRareData()) |
| 1455 | elementRareData()->resetComputedStyle(); |
| 1456 | } |
| 1457 | if (hasParentStyle && (change >= Inherit || needsStyleRecalc())) { |
| 1458 | StyleChange localChange = Detach; |
| 1459 | RefPtr<RenderStyle> newStyle; |
| 1460 | if (currentStyle) { |
| 1461 | // FIXME: This still recalcs style twice when changing display types, but saves |
| 1462 | // us from recalcing twice when going from none -> anything else which is more |
| 1463 | // common, especially during lazy attach. |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 1464 | newStyle = styleForRenderer(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1465 | localChange = Node::diff(currentStyle.get(), newStyle.get(), document()); |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 1466 | } else if (attached() && isActiveInsertionPoint(this)) { |
| 1467 | // Active InsertionPoints will never have renderers so there's no reason to |
| 1468 | // reattach them repeatedly once they're already attached. |
| 1469 | localChange = change; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1470 | } |
| 1471 | if (localChange == Detach) { |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 1472 | AttachContext reattachContext; |
| 1473 | reattachContext.resolvedStyle = newStyle.get(); |
| 1474 | reattach(reattachContext); |
| 1475 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1476 | // attach recalculates the style for all children. No need to do it twice. |
| 1477 | clearNeedsStyleRecalc(); |
| 1478 | clearChildNeedsStyleRecalc(); |
| 1479 | |
| 1480 | if (hasCustomStyleCallbacks()) |
| 1481 | didRecalcStyle(change); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 1482 | return true; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1483 | } |
| 1484 | |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 1485 | InspectorInstrumentation::didRecalculateStyleForElement(this); |
| 1486 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1487 | if (RenderObject* renderer = this->renderer()) { |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 1488 | if (localChange != NoChange || pseudoStyleCacheIsInvalid(currentStyle.get(), newStyle.get()) || (change == Force && renderer->requiresForcedStyleRecalcPropagation()) || shouldNotifyRendererWithIdenticalStyles()) { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1489 | renderer->setAnimatableStyle(newStyle.get()); |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 1490 | } else if (needsStyleRecalc()) { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1491 | // Although no change occurred, we use the new style so that the cousin style sharing code won't get |
| 1492 | // fooled into believing this style is the same. |
| 1493 | renderer->setStyleInternal(newStyle.get()); |
| 1494 | } |
| 1495 | } |
| 1496 | |
| 1497 | // If "rem" units are used anywhere in the document, and if the document element's font size changes, then go ahead and force font updating |
| 1498 | // all the way down the tree. This is simpler than having to maintain a cache of objects (and such font size changes should be rare anyway). |
| 1499 | if (document()->styleSheetCollection()->usesRemUnits() && document()->documentElement() == this && localChange != NoChange && currentStyle && newStyle && currentStyle->fontSize() != newStyle->fontSize()) { |
| 1500 | // Cached RenderStyles may depend on the re units. |
| 1501 | document()->styleResolver()->invalidateMatchedPropertiesCache(); |
| 1502 | change = Force; |
| 1503 | } |
| 1504 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1505 | if (styleChangeType() >= SubtreeStyleChange) |
Torne (Richard Coles) | e524955 | 2013-05-15 11:35:13 +0100 | [diff] [blame] | 1506 | change = Force; |
| 1507 | else if (change != Force) |
| 1508 | change = localChange; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1509 | } |
| 1510 | StyleResolverParentPusher parentPusher(this); |
| 1511 | |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 1512 | for (ShadowRoot* root = youngestShadowRoot(); root; root = root->olderShadowRoot()) { |
| 1513 | if (shouldRecalcStyle(change, root)) { |
| 1514 | parentPusher.push(); |
| 1515 | root->recalcStyle(change); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | if (shouldRecalcStyle(change, this)) |
| 1520 | updatePseudoElement(BEFORE, change); |
| 1521 | |
| 1522 | // FIXME: This check is good enough for :hover + foo, but it is not good enough for :hover + foo + bar. |
| 1523 | // For now we will just worry about the common case, since it's a lot trickier to get the second case right |
| 1524 | // without doing way too much re-resolution. |
| 1525 | bool forceCheckOfNextElementSibling = false; |
| 1526 | bool forceCheckOfAnyElementSibling = false; |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 1527 | bool forceReattachOfAnyWhitespaceSibling = false; |
| 1528 | for (Node* child = firstChild(); child; child = child->nextSibling()) { |
| 1529 | bool didReattach = false; |
| 1530 | |
| 1531 | if (child->renderer()) |
| 1532 | forceReattachOfAnyWhitespaceSibling = false; |
| 1533 | |
| 1534 | if (child->isTextNode()) { |
| 1535 | if (forceReattachOfAnyWhitespaceSibling && toText(child)->containsOnlyWhitespace()) |
| 1536 | child->reattach(); |
| 1537 | else |
| 1538 | didReattach = toText(child)->recalcTextStyle(change); |
| 1539 | } else if (child->isElementNode()) { |
| 1540 | Element* element = toElement(child); |
| 1541 | |
| 1542 | if (forceCheckOfNextElementSibling || forceCheckOfAnyElementSibling) |
| 1543 | element->setNeedsStyleRecalc(); |
| 1544 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1545 | bool childRulesChanged = element->needsStyleRecalc() && element->styleChangeType() >= SubtreeStyleChange; |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 1546 | forceCheckOfNextElementSibling = childRulesChanged && hasDirectAdjacentRules; |
| 1547 | forceCheckOfAnyElementSibling = forceCheckOfAnyElementSibling || (childRulesChanged && hasIndirectAdjacentRules); |
| 1548 | |
| 1549 | if (shouldRecalcStyle(change, element)) { |
| 1550 | parentPusher.push(); |
| 1551 | didReattach = element->recalcStyle(change); |
| 1552 | } |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 1553 | } |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 1554 | |
| 1555 | forceReattachOfAnyWhitespaceSibling = didReattach || forceReattachOfAnyWhitespaceSibling; |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 1556 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1557 | |
| 1558 | if (shouldRecalcStyle(change, this)) |
| 1559 | updatePseudoElement(AFTER, change); |
| 1560 | |
| 1561 | clearNeedsStyleRecalc(); |
| 1562 | clearChildNeedsStyleRecalc(); |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 1563 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1564 | if (hasCustomStyleCallbacks()) |
| 1565 | didRecalcStyle(change); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 1566 | return false; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | ElementShadow* Element::shadow() const |
| 1570 | { |
| 1571 | return hasRareData() ? elementRareData()->shadow() : 0; |
| 1572 | } |
| 1573 | |
| 1574 | ElementShadow* Element::ensureShadow() |
| 1575 | { |
| 1576 | return ensureElementRareData()->ensureShadow(); |
| 1577 | } |
| 1578 | |
| 1579 | void Element::didAffectSelector(AffectedSelectorMask mask) |
| 1580 | { |
| 1581 | setNeedsStyleRecalc(); |
| 1582 | if (ElementShadow* elementShadow = shadowOfParentForDistribution(this)) |
| 1583 | elementShadow->didAffectSelector(mask); |
| 1584 | } |
| 1585 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1586 | PassRefPtr<ShadowRoot> Element::createShadowRoot(ExceptionState& es) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1587 | { |
| 1588 | if (alwaysCreateUserAgentShadowRoot()) |
| 1589 | ensureUserAgentShadowRoot(); |
| 1590 | |
| 1591 | if (RuntimeEnabledFeatures::authorShadowDOMForAnyElementEnabled()) |
| 1592 | return ensureShadow()->addShadowRoot(this, ShadowRoot::AuthorShadowRoot); |
| 1593 | |
| 1594 | // Since some elements recreates shadow root dynamically, multiple shadow |
| 1595 | // subtrees won't work well in that element. Until they are fixed, we disable |
| 1596 | // adding author shadow root for them. |
| 1597 | if (!areAuthorShadowsAllowed()) { |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1598 | es.throwDOMException(HierarchyRequestError); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1599 | return 0; |
| 1600 | } |
| 1601 | return ensureShadow()->addShadowRoot(this, ShadowRoot::AuthorShadowRoot); |
| 1602 | } |
| 1603 | |
| 1604 | ShadowRoot* Element::shadowRoot() const |
| 1605 | { |
| 1606 | ElementShadow* elementShadow = shadow(); |
| 1607 | if (!elementShadow) |
| 1608 | return 0; |
| 1609 | ShadowRoot* shadowRoot = elementShadow->youngestShadowRoot(); |
| 1610 | if (shadowRoot->type() == ShadowRoot::AuthorShadowRoot) |
| 1611 | return shadowRoot; |
| 1612 | return 0; |
| 1613 | } |
| 1614 | |
| 1615 | ShadowRoot* Element::userAgentShadowRoot() const |
| 1616 | { |
| 1617 | if (ElementShadow* elementShadow = shadow()) { |
| 1618 | if (ShadowRoot* shadowRoot = elementShadow->oldestShadowRoot()) { |
| 1619 | ASSERT(shadowRoot->type() == ShadowRoot::UserAgentShadowRoot); |
| 1620 | return shadowRoot; |
| 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | return 0; |
| 1625 | } |
| 1626 | |
| 1627 | ShadowRoot* Element::ensureUserAgentShadowRoot() |
| 1628 | { |
| 1629 | if (ShadowRoot* shadowRoot = userAgentShadowRoot()) |
| 1630 | return shadowRoot; |
| 1631 | ShadowRoot* shadowRoot = ensureShadow()->addShadowRoot(this, ShadowRoot::UserAgentShadowRoot); |
| 1632 | didAddUserAgentShadowRoot(shadowRoot); |
| 1633 | return shadowRoot; |
| 1634 | } |
| 1635 | |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 1636 | // FIXME: After replacing all internal shadowPseudoId with shadowPartId, remove this method. |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1637 | const AtomicString& Element::shadowPseudoId() const |
| 1638 | { |
| 1639 | return pseudo(); |
| 1640 | } |
| 1641 | |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 1642 | const AtomicString& Element::shadowPartId() const |
| 1643 | { |
| 1644 | return part(); |
| 1645 | } |
| 1646 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1647 | bool Element::childTypeAllowed(NodeType type) const |
| 1648 | { |
| 1649 | switch (type) { |
| 1650 | case ELEMENT_NODE: |
| 1651 | case TEXT_NODE: |
| 1652 | case COMMENT_NODE: |
| 1653 | case PROCESSING_INSTRUCTION_NODE: |
| 1654 | case CDATA_SECTION_NODE: |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1655 | return true; |
| 1656 | default: |
| 1657 | break; |
| 1658 | } |
| 1659 | return false; |
| 1660 | } |
| 1661 | |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 1662 | static void inline checkForEmptyStyleChange(Element* element, RenderStyle* style) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1663 | { |
| 1664 | if (!style && !element->styleAffectedByEmpty()) |
| 1665 | return; |
| 1666 | |
| 1667 | if (!style || (element->styleAffectedByEmpty() && (!style->emptyState() || element->hasChildNodes()))) |
| 1668 | element->setNeedsStyleRecalc(); |
| 1669 | } |
| 1670 | |
| 1671 | static void checkForSiblingStyleChanges(Element* e, RenderStyle* style, bool finishedParsingCallback, |
| 1672 | Node* beforeChange, Node* afterChange, int childCountDelta) |
| 1673 | { |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1674 | if (!e->attached() || e->document()->hasPendingForcedStyleRecalc() || e->styleChangeType() >= SubtreeStyleChange) |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 1675 | return; |
| 1676 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1677 | // :empty selector. |
| 1678 | checkForEmptyStyleChange(e, style); |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 1679 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1680 | if (!style || (e->needsStyleRecalc() && e->childrenAffectedByPositionalRules())) |
| 1681 | return; |
| 1682 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1683 | // Forward positional selectors include the ~ selector, nth-child, nth-of-type, first-of-type and only-of-type. |
| 1684 | // Backward positional selectors include nth-last-child, nth-last-of-type, last-of-type and only-of-type. |
| 1685 | // We have to invalidate everything following the insertion point in the forward case, and everything before the insertion point in the |
| 1686 | // backward case. |
| 1687 | // |afterChange| is 0 in the parser callback case, so we won't do any work for the forward case if we don't have to. |
| 1688 | // For performance reasons we just mark the parent node as changed, since we don't want to make childrenChanged O(n^2) by crawling all our kids |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 1689 | // here. recalcStyle will then force a walk of the children when it sees that this has happened. |
| 1690 | if ((e->childrenAffectedByForwardPositionalRules() && afterChange) || (e->childrenAffectedByBackwardPositionalRules() && beforeChange)) { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1691 | e->setNeedsStyleRecalc(); |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 1692 | return; |
| 1693 | } |
| 1694 | |
| 1695 | // :first-child. In the parser callback case, we don't have to check anything, since we were right the first time. |
| 1696 | // In the DOM case, we only need to do something if |afterChange| is not 0. |
| 1697 | // |afterChange| is 0 in the parser case, so it works out that we'll skip this block. |
| 1698 | if (e->childrenAffectedByFirstChildRules() && afterChange) { |
| 1699 | // Find our new first child. |
| 1700 | Node* newFirstChild = e->firstElementChild(); |
| 1701 | RenderStyle* newFirstChildStyle = newFirstChild ? newFirstChild->renderStyle() : 0; |
| 1702 | |
| 1703 | // Find the first element node following |afterChange| |
| 1704 | Node* firstElementAfterInsertion = afterChange->isElementNode() ? afterChange : afterChange->nextElementSibling(); |
| 1705 | RenderStyle* firstElementAfterInsertionStyle = firstElementAfterInsertion ? firstElementAfterInsertion->renderStyle() : 0; |
| 1706 | |
| 1707 | // This is the insert/append case. |
| 1708 | if (newFirstChild != firstElementAfterInsertion && firstElementAfterInsertionStyle && firstElementAfterInsertionStyle->firstChildState()) |
| 1709 | firstElementAfterInsertion->setNeedsStyleRecalc(); |
| 1710 | |
| 1711 | // We also have to handle node removal. |
| 1712 | if (childCountDelta < 0 && newFirstChild == firstElementAfterInsertion && newFirstChild && (!newFirstChildStyle || !newFirstChildStyle->firstChildState())) |
| 1713 | newFirstChild->setNeedsStyleRecalc(); |
| 1714 | } |
| 1715 | |
| 1716 | // :last-child. In the parser callback case, we don't have to check anything, since we were right the first time. |
| 1717 | // In the DOM case, we only need to do something if |afterChange| is not 0. |
| 1718 | if (e->childrenAffectedByLastChildRules() && beforeChange) { |
| 1719 | // Find our new last child. |
| 1720 | Node* newLastChild = e->lastElementChild(); |
| 1721 | RenderStyle* newLastChildStyle = newLastChild ? newLastChild->renderStyle() : 0; |
| 1722 | |
| 1723 | // Find the last element node going backwards from |beforeChange| |
| 1724 | Node* lastElementBeforeInsertion = beforeChange->isElementNode() ? beforeChange : beforeChange->previousElementSibling(); |
| 1725 | RenderStyle* lastElementBeforeInsertionStyle = lastElementBeforeInsertion ? lastElementBeforeInsertion->renderStyle() : 0; |
| 1726 | |
| 1727 | if (newLastChild != lastElementBeforeInsertion && lastElementBeforeInsertionStyle && lastElementBeforeInsertionStyle->lastChildState()) |
| 1728 | lastElementBeforeInsertion->setNeedsStyleRecalc(); |
| 1729 | |
| 1730 | // We also have to handle node removal. The parser callback case is similar to node removal as well in that we need to change the last child |
| 1731 | // to match now. |
| 1732 | if ((childCountDelta < 0 || finishedParsingCallback) && newLastChild == lastElementBeforeInsertion && newLastChild && (!newLastChildStyle || !newLastChildStyle->lastChildState())) |
| 1733 | newLastChild->setNeedsStyleRecalc(); |
| 1734 | } |
| 1735 | |
| 1736 | // The + selector. We need to invalidate the first element following the insertion point. It is the only possible element |
| 1737 | // that could be affected by this DOM change. |
| 1738 | if (e->childrenAffectedByDirectAdjacentRules() && afterChange) { |
| 1739 | if (Node* firstElementAfterInsertion = afterChange->nextElementSibling()) |
| 1740 | firstElementAfterInsertion->setNeedsStyleRecalc(); |
| 1741 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | void Element::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta) |
| 1745 | { |
| 1746 | ContainerNode::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta); |
| 1747 | if (changedByParser) |
| 1748 | checkForEmptyStyleChange(this, renderStyle()); |
| 1749 | else |
| 1750 | checkForSiblingStyleChanges(this, renderStyle(), false, beforeChange, afterChange, childCountDelta); |
| 1751 | |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 1752 | if (ElementShadow* shadow = this->shadow()) |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 1753 | shadow->setNeedsDistributionRecalc(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | void Element::removeAllEventListeners() |
| 1757 | { |
| 1758 | ContainerNode::removeAllEventListeners(); |
| 1759 | if (ElementShadow* shadow = this->shadow()) |
| 1760 | shadow->removeAllEventListeners(); |
| 1761 | } |
| 1762 | |
| 1763 | void Element::beginParsingChildren() |
| 1764 | { |
| 1765 | clearIsParsingChildrenFinished(); |
| 1766 | StyleResolver* styleResolver = document()->styleResolverIfExists(); |
| 1767 | if (styleResolver && attached()) |
| 1768 | styleResolver->pushParentElement(this); |
| 1769 | } |
| 1770 | |
| 1771 | void Element::finishParsingChildren() |
| 1772 | { |
| 1773 | ContainerNode::finishParsingChildren(); |
| 1774 | setIsParsingChildrenFinished(); |
| 1775 | checkForSiblingStyleChanges(this, renderStyle(), true, lastChild(), 0, 0); |
| 1776 | if (StyleResolver* styleResolver = document()->styleResolverIfExists()) |
| 1777 | styleResolver->popParentElement(this); |
| 1778 | } |
| 1779 | |
| 1780 | #ifndef NDEBUG |
| 1781 | void Element::formatForDebugger(char* buffer, unsigned length) const |
| 1782 | { |
| 1783 | StringBuilder result; |
| 1784 | String s; |
| 1785 | |
| 1786 | result.append(nodeName()); |
| 1787 | |
| 1788 | s = getIdAttribute(); |
| 1789 | if (s.length() > 0) { |
| 1790 | if (result.length() > 0) |
| 1791 | result.appendLiteral("; "); |
| 1792 | result.appendLiteral("id="); |
| 1793 | result.append(s); |
| 1794 | } |
| 1795 | |
| 1796 | s = getAttribute(classAttr); |
| 1797 | if (s.length() > 0) { |
| 1798 | if (result.length() > 0) |
| 1799 | result.appendLiteral("; "); |
| 1800 | result.appendLiteral("class="); |
| 1801 | result.append(s); |
| 1802 | } |
| 1803 | |
| 1804 | strncpy(buffer, result.toString().utf8().data(), length - 1); |
| 1805 | } |
| 1806 | #endif |
| 1807 | |
| 1808 | const Vector<RefPtr<Attr> >& Element::attrNodeList() |
| 1809 | { |
| 1810 | ASSERT(hasSyntheticAttrChildNodes()); |
| 1811 | return *attrNodeListForElement(this); |
| 1812 | } |
| 1813 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1814 | PassRefPtr<Attr> Element::setAttributeNode(Attr* attrNode, ExceptionState& es) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1815 | { |
| 1816 | if (!attrNode) { |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1817 | es.throwDOMException(TypeMismatchError); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1818 | return 0; |
| 1819 | } |
| 1820 | |
| 1821 | RefPtr<Attr> oldAttrNode = attrIfExists(attrNode->qualifiedName()); |
| 1822 | if (oldAttrNode.get() == attrNode) |
| 1823 | return attrNode; // This Attr is already attached to the element. |
| 1824 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 1825 | // InUseAttributeError: Raised if node is an Attr that is already an attribute of another Element object. |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1826 | // The DOM user must explicitly clone Attr nodes to re-use them in other elements. |
| 1827 | if (attrNode->ownerElement()) { |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1828 | es.throwDOMException(InUseAttributeError); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1829 | return 0; |
| 1830 | } |
| 1831 | |
| 1832 | synchronizeAllAttributes(); |
| 1833 | UniqueElementData* elementData = ensureUniqueElementData(); |
| 1834 | |
| 1835 | size_t index = elementData->getAttributeItemIndex(attrNode->qualifiedName()); |
| 1836 | if (index != notFound) { |
| 1837 | if (oldAttrNode) |
| 1838 | detachAttrNodeFromElementWithValue(oldAttrNode.get(), elementData->attributeItem(index)->value()); |
| 1839 | else |
| 1840 | oldAttrNode = Attr::create(document(), attrNode->qualifiedName(), elementData->attributeItem(index)->value()); |
| 1841 | } |
| 1842 | |
| 1843 | setAttributeInternal(index, attrNode->qualifiedName(), attrNode->value(), NotInSynchronizationOfLazyAttribute); |
| 1844 | |
| 1845 | attrNode->attachToElement(this); |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 1846 | treeScope()->adoptIfNeeded(attrNode); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1847 | ensureAttrNodeListForElement(this)->append(attrNode); |
| 1848 | |
| 1849 | return oldAttrNode.release(); |
| 1850 | } |
| 1851 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1852 | PassRefPtr<Attr> Element::setAttributeNodeNS(Attr* attr, ExceptionState& es) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1853 | { |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1854 | return setAttributeNode(attr, es); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1855 | } |
| 1856 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1857 | PassRefPtr<Attr> Element::removeAttributeNode(Attr* attr, ExceptionState& es) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1858 | { |
| 1859 | if (!attr) { |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1860 | es.throwDOMException(TypeMismatchError); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1861 | return 0; |
| 1862 | } |
| 1863 | if (attr->ownerElement() != this) { |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1864 | es.throwDOMException(NotFoundError); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1865 | return 0; |
| 1866 | } |
| 1867 | |
| 1868 | ASSERT(document() == attr->document()); |
| 1869 | |
| 1870 | synchronizeAttribute(attr->qualifiedName()); |
| 1871 | |
Torne (Richard Coles) | e524955 | 2013-05-15 11:35:13 +0100 | [diff] [blame] | 1872 | size_t index = elementData()->getAttrIndex(attr); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1873 | if (index == notFound) { |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1874 | es.throwDOMException(NotFoundError); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1875 | return 0; |
| 1876 | } |
| 1877 | |
Torne (Richard Coles) | e524955 | 2013-05-15 11:35:13 +0100 | [diff] [blame] | 1878 | RefPtr<Attr> guard(attr); |
| 1879 | detachAttrNodeAtIndex(attr, index); |
| 1880 | return guard.release(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1881 | } |
| 1882 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1883 | bool Element::parseAttributeName(QualifiedName& out, const AtomicString& namespaceURI, const AtomicString& qualifiedName, ExceptionState& es) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1884 | { |
| 1885 | String prefix, localName; |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1886 | if (!Document::parseQualifiedName(qualifiedName, prefix, localName, es)) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1887 | return false; |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1888 | ASSERT(!es.hadException()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1889 | |
| 1890 | QualifiedName qName(prefix, localName, namespaceURI); |
| 1891 | |
| 1892 | if (!Document::hasValidNamespaceForAttributes(qName)) { |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1893 | es.throwDOMException(NamespaceError); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1894 | return false; |
| 1895 | } |
| 1896 | |
| 1897 | out = qName; |
| 1898 | return true; |
| 1899 | } |
| 1900 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1901 | void Element::setAttributeNS(const AtomicString& namespaceURI, const AtomicString& qualifiedName, const AtomicString& value, ExceptionState& es) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1902 | { |
| 1903 | QualifiedName parsedName = anyName; |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 1904 | if (!parseAttributeName(parsedName, namespaceURI, qualifiedName, es)) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1905 | return; |
| 1906 | setAttribute(parsedName, value); |
| 1907 | } |
| 1908 | |
| 1909 | void Element::removeAttributeInternal(size_t index, SynchronizationOfLazyAttribute inSynchronizationOfLazyAttribute) |
| 1910 | { |
| 1911 | ASSERT_WITH_SECURITY_IMPLICATION(index < attributeCount()); |
| 1912 | |
| 1913 | UniqueElementData* elementData = ensureUniqueElementData(); |
| 1914 | |
| 1915 | QualifiedName name = elementData->attributeItem(index)->name(); |
| 1916 | AtomicString valueBeingRemoved = elementData->attributeItem(index)->value(); |
| 1917 | |
| 1918 | if (!inSynchronizationOfLazyAttribute) { |
| 1919 | if (!valueBeingRemoved.isNull()) |
| 1920 | willModifyAttribute(name, valueBeingRemoved, nullAtom); |
| 1921 | } |
| 1922 | |
| 1923 | if (RefPtr<Attr> attrNode = attrIfExists(name)) |
| 1924 | detachAttrNodeFromElementWithValue(attrNode.get(), elementData->attributeItem(index)->value()); |
| 1925 | |
| 1926 | elementData->removeAttribute(index); |
| 1927 | |
| 1928 | if (!inSynchronizationOfLazyAttribute) |
| 1929 | didRemoveAttribute(name); |
| 1930 | } |
| 1931 | |
| 1932 | void Element::addAttributeInternal(const QualifiedName& name, const AtomicString& value, SynchronizationOfLazyAttribute inSynchronizationOfLazyAttribute) |
| 1933 | { |
| 1934 | if (!inSynchronizationOfLazyAttribute) |
| 1935 | willModifyAttribute(name, nullAtom, value); |
| 1936 | ensureUniqueElementData()->addAttribute(name, value); |
| 1937 | if (!inSynchronizationOfLazyAttribute) |
| 1938 | didAddAttribute(name, value); |
| 1939 | } |
| 1940 | |
| 1941 | void Element::removeAttribute(const AtomicString& name) |
| 1942 | { |
| 1943 | if (!elementData()) |
| 1944 | return; |
| 1945 | |
| 1946 | AtomicString localName = shouldIgnoreAttributeCase(this) ? name.lower() : name; |
| 1947 | size_t index = elementData()->getAttributeItemIndex(localName, false); |
| 1948 | if (index == notFound) { |
| 1949 | if (UNLIKELY(localName == styleAttr) && elementData()->m_styleAttributeIsDirty && isStyledElement()) |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 1950 | removeAllInlineStyleProperties(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1951 | return; |
| 1952 | } |
| 1953 | |
| 1954 | removeAttributeInternal(index, NotInSynchronizationOfLazyAttribute); |
| 1955 | } |
| 1956 | |
| 1957 | void Element::removeAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName) |
| 1958 | { |
| 1959 | removeAttribute(QualifiedName(nullAtom, localName, namespaceURI)); |
| 1960 | } |
| 1961 | |
| 1962 | PassRefPtr<Attr> Element::getAttributeNode(const AtomicString& localName) |
| 1963 | { |
| 1964 | if (!elementData()) |
| 1965 | return 0; |
| 1966 | synchronizeAttribute(localName); |
| 1967 | const Attribute* attribute = elementData()->getAttributeItem(localName, shouldIgnoreAttributeCase(this)); |
| 1968 | if (!attribute) |
| 1969 | return 0; |
| 1970 | return ensureAttr(attribute->name()); |
| 1971 | } |
| 1972 | |
| 1973 | PassRefPtr<Attr> Element::getAttributeNodeNS(const AtomicString& namespaceURI, const AtomicString& localName) |
| 1974 | { |
| 1975 | if (!elementData()) |
| 1976 | return 0; |
| 1977 | QualifiedName qName(nullAtom, localName, namespaceURI); |
| 1978 | synchronizeAttribute(qName); |
| 1979 | const Attribute* attribute = elementData()->getAttributeItem(qName); |
| 1980 | if (!attribute) |
| 1981 | return 0; |
| 1982 | return ensureAttr(attribute->name()); |
| 1983 | } |
| 1984 | |
| 1985 | bool Element::hasAttribute(const AtomicString& localName) const |
| 1986 | { |
| 1987 | if (!elementData()) |
| 1988 | return false; |
| 1989 | synchronizeAttribute(localName); |
| 1990 | return elementData()->getAttributeItem(shouldIgnoreAttributeCase(this) ? localName.lower() : localName, false); |
| 1991 | } |
| 1992 | |
| 1993 | bool Element::hasAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName) const |
| 1994 | { |
| 1995 | if (!elementData()) |
| 1996 | return false; |
| 1997 | QualifiedName qName(nullAtom, localName, namespaceURI); |
| 1998 | synchronizeAttribute(qName); |
| 1999 | return elementData()->getAttributeItem(qName); |
| 2000 | } |
| 2001 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2002 | void Element::focus(bool restorePreviousSelection, FocusDirection direction) |
| 2003 | { |
| 2004 | if (!inDocument()) |
| 2005 | return; |
| 2006 | |
| 2007 | Document* doc = document(); |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 2008 | if (doc->focusedElement() == this) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2009 | return; |
| 2010 | |
| 2011 | // If the stylesheets have already been loaded we can reliably check isFocusable. |
| 2012 | // If not, we continue and set the focused node on the focus controller below so |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 2013 | // that it can be updated soon after attach. |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2014 | if (doc->haveStylesheetsLoaded()) { |
| 2015 | doc->updateLayoutIgnorePendingStylesheets(); |
| 2016 | if (!isFocusable()) |
| 2017 | return; |
| 2018 | } |
| 2019 | |
| 2020 | if (!supportsFocus()) |
| 2021 | return; |
| 2022 | |
| 2023 | RefPtr<Node> protect; |
| 2024 | if (Page* page = doc->page()) { |
| 2025 | // Focus and change event handlers can cause us to lose our last ref. |
| 2026 | // If a focus event handler changes the focus to a different node it |
| 2027 | // does not make sense to continue and update appearence. |
| 2028 | protect = this; |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 2029 | if (!page->focusController()->setFocusedElement(this, doc->frame(), direction)) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2030 | return; |
| 2031 | } |
| 2032 | |
| 2033 | // Setting the focused node above might have invalidated the layout due to scripts. |
| 2034 | doc->updateLayoutIgnorePendingStylesheets(); |
| 2035 | |
| 2036 | if (!isFocusable()) { |
| 2037 | ensureElementRareData()->setNeedsFocusAppearanceUpdateSoonAfterAttach(true); |
| 2038 | return; |
| 2039 | } |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 2040 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2041 | cancelFocusAppearanceUpdate(); |
| 2042 | updateFocusAppearance(restorePreviousSelection); |
| 2043 | } |
| 2044 | |
| 2045 | void Element::updateFocusAppearance(bool /*restorePreviousSelection*/) |
| 2046 | { |
| 2047 | if (isRootEditableElement()) { |
| 2048 | Frame* frame = document()->frame(); |
| 2049 | if (!frame) |
| 2050 | return; |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 2051 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2052 | // When focusing an editable element in an iframe, don't reset the selection if it already contains a selection. |
| 2053 | if (this == frame->selection()->rootEditableElement()) |
| 2054 | return; |
| 2055 | |
| 2056 | // FIXME: We should restore the previous selection if there is one. |
| 2057 | VisibleSelection newSelection = VisibleSelection(firstPositionInOrBeforeNode(this), DOWNSTREAM); |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 2058 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2059 | if (frame->selection()->shouldChangeSelection(newSelection)) { |
| 2060 | frame->selection()->setSelection(newSelection); |
| 2061 | frame->selection()->revealSelection(); |
| 2062 | } |
| 2063 | } else if (renderer() && !renderer()->isWidget()) |
| 2064 | renderer()->scrollRectToVisible(boundingBox()); |
| 2065 | } |
| 2066 | |
| 2067 | void Element::blur() |
| 2068 | { |
| 2069 | cancelFocusAppearanceUpdate(); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 2070 | if (treeScope()->adjustedFocusedElement() == this) { |
| 2071 | Document* doc = document(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2072 | if (doc->frame()) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 2073 | doc->frame()->page()->focusController()->setFocusedElement(0, doc->frame()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2074 | else |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 2075 | doc->setFocusedElement(0); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2076 | } |
| 2077 | } |
| 2078 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 2079 | bool Element::isFocusable() const |
| 2080 | { |
| 2081 | return inDocument() && supportsFocus() && !isInert() && rendererIsFocusable(); |
| 2082 | } |
| 2083 | |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 2084 | bool Element::isKeyboardFocusable() const |
| 2085 | { |
| 2086 | return isFocusable() && tabIndex() >= 0; |
| 2087 | } |
| 2088 | |
| 2089 | bool Element::isMouseFocusable() const |
| 2090 | { |
| 2091 | return isFocusable(); |
| 2092 | } |
| 2093 | |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 2094 | void Element::dispatchFocusEvent(Element* oldFocusedElement, FocusDirection) |
| 2095 | { |
| 2096 | RefPtr<FocusEvent> event = FocusEvent::create(eventNames().focusEvent, false, false, document()->defaultView(), 0, oldFocusedElement); |
| 2097 | EventDispatcher::dispatchEvent(this, FocusEventDispatchMediator::create(event.release())); |
| 2098 | } |
| 2099 | |
| 2100 | void Element::dispatchBlurEvent(Element* newFocusedElement) |
| 2101 | { |
| 2102 | RefPtr<FocusEvent> event = FocusEvent::create(eventNames().blurEvent, false, false, document()->defaultView(), 0, newFocusedElement); |
| 2103 | EventDispatcher::dispatchEvent(this, BlurEventDispatchMediator::create(event.release())); |
| 2104 | } |
| 2105 | |
| 2106 | void Element::dispatchFocusInEvent(const AtomicString& eventType, Element* oldFocusedElement) |
| 2107 | { |
| 2108 | ASSERT(!NoEventDispatchAssertion::isEventDispatchForbidden()); |
| 2109 | ASSERT(eventType == eventNames().focusinEvent || eventType == eventNames().DOMFocusInEvent); |
| 2110 | dispatchScopedEventDispatchMediator(FocusInEventDispatchMediator::create(FocusEvent::create(eventType, true, false, document()->defaultView(), 0, oldFocusedElement))); |
| 2111 | } |
| 2112 | |
| 2113 | void Element::dispatchFocusOutEvent(const AtomicString& eventType, Element* newFocusedElement) |
| 2114 | { |
| 2115 | ASSERT(!NoEventDispatchAssertion::isEventDispatchForbidden()); |
| 2116 | ASSERT(eventType == eventNames().focusoutEvent || eventType == eventNames().DOMFocusOutEvent); |
| 2117 | dispatchScopedEventDispatchMediator(FocusOutEventDispatchMediator::create(FocusEvent::create(eventType, true, false, document()->defaultView(), 0, newFocusedElement))); |
| 2118 | } |
| 2119 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2120 | String Element::innerText() |
| 2121 | { |
| 2122 | // We need to update layout, since plainText uses line boxes in the render tree. |
| 2123 | document()->updateLayoutIgnorePendingStylesheets(); |
| 2124 | |
| 2125 | if (!renderer()) |
| 2126 | return textContent(true); |
| 2127 | |
| 2128 | return plainText(rangeOfContents(const_cast<Element*>(this)).get()); |
| 2129 | } |
| 2130 | |
| 2131 | String Element::outerText() |
| 2132 | { |
| 2133 | // Getting outerText is the same as getting innerText, only |
| 2134 | // setting is different. You would think this should get the plain |
| 2135 | // text for the outer range, but this is wrong, <br> for instance |
| 2136 | // would return different values for inner and outer text by such |
| 2137 | // a rule, but it doesn't in WinIE, and we want to match that. |
| 2138 | return innerText(); |
| 2139 | } |
| 2140 | |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 2141 | String Element::textFromChildren() |
| 2142 | { |
| 2143 | Text* firstTextNode = 0; |
| 2144 | bool foundMultipleTextNodes = false; |
| 2145 | unsigned totalLength = 0; |
| 2146 | |
| 2147 | for (Node* child = firstChild(); child; child = child->nextSibling()) { |
| 2148 | if (!child->isTextNode()) |
| 2149 | continue; |
| 2150 | Text* text = toText(child); |
| 2151 | if (!firstTextNode) |
| 2152 | firstTextNode = text; |
| 2153 | else |
| 2154 | foundMultipleTextNodes = true; |
| 2155 | unsigned length = text->data().length(); |
| 2156 | if (length > std::numeric_limits<unsigned>::max() - totalLength) |
| 2157 | return emptyString(); |
| 2158 | totalLength += length; |
| 2159 | } |
| 2160 | |
| 2161 | if (!firstTextNode) |
| 2162 | return emptyString(); |
| 2163 | |
| 2164 | if (firstTextNode && !foundMultipleTextNodes) { |
| 2165 | firstTextNode->atomize(); |
| 2166 | return firstTextNode->data(); |
| 2167 | } |
| 2168 | |
| 2169 | StringBuilder content; |
| 2170 | content.reserveCapacity(totalLength); |
| 2171 | for (Node* child = firstTextNode; child; child = child->nextSibling()) { |
| 2172 | if (!child->isTextNode()) |
| 2173 | continue; |
| 2174 | content.append(toText(child)->data()); |
| 2175 | } |
| 2176 | |
| 2177 | ASSERT(content.length() == totalLength); |
| 2178 | return content.toString(); |
| 2179 | } |
| 2180 | |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 2181 | // FIXME: pseudo should be deprecated after all pseudo is replaced with ::part. |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2182 | const AtomicString& Element::pseudo() const |
| 2183 | { |
| 2184 | return getAttribute(pseudoAttr); |
| 2185 | } |
| 2186 | |
| 2187 | void Element::setPseudo(const AtomicString& value) |
| 2188 | { |
| 2189 | setAttribute(pseudoAttr, value); |
| 2190 | } |
| 2191 | |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 2192 | const AtomicString& Element::part() const |
| 2193 | { |
| 2194 | return getAttribute(partAttr); |
| 2195 | } |
| 2196 | |
| 2197 | void Element::setPart(const AtomicString& value) |
| 2198 | { |
| 2199 | setAttribute(partAttr, value); |
| 2200 | } |
| 2201 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2202 | LayoutSize Element::minimumSizeForResizing() const |
| 2203 | { |
| 2204 | return hasRareData() ? elementRareData()->minimumSizeForResizing() : defaultMinimumSizeForResizing(); |
| 2205 | } |
| 2206 | |
| 2207 | void Element::setMinimumSizeForResizing(const LayoutSize& size) |
| 2208 | { |
| 2209 | if (!hasRareData() && size == defaultMinimumSizeForResizing()) |
| 2210 | return; |
| 2211 | ensureElementRareData()->setMinimumSizeForResizing(size); |
| 2212 | } |
| 2213 | |
| 2214 | RenderStyle* Element::computedStyle(PseudoId pseudoElementSpecifier) |
| 2215 | { |
| 2216 | if (PseudoElement* element = pseudoElement(pseudoElementSpecifier)) |
| 2217 | return element->computedStyle(); |
| 2218 | |
| 2219 | // FIXME: Find and use the renderer from the pseudo element instead of the actual element so that the 'length' |
| 2220 | // properties, which are only known by the renderer because it did the layout, will be correct and so that the |
| 2221 | // values returned for the ":selection" pseudo-element will be correct. |
| 2222 | if (RenderStyle* usedStyle = renderStyle()) { |
| 2223 | if (pseudoElementSpecifier) { |
| 2224 | RenderStyle* cachedPseudoStyle = usedStyle->getCachedPseudoStyle(pseudoElementSpecifier); |
| 2225 | return cachedPseudoStyle ? cachedPseudoStyle : usedStyle; |
| 2226 | } else |
| 2227 | return usedStyle; |
| 2228 | } |
| 2229 | |
| 2230 | if (!attached()) |
| 2231 | // FIXME: Try to do better than this. Ensure that styleForElement() works for elements that are not in the |
| 2232 | // document tree and figure out when to destroy the computed style for such elements. |
| 2233 | return 0; |
| 2234 | |
| 2235 | ElementRareData* data = ensureElementRareData(); |
| 2236 | if (!data->computedStyle()) |
| 2237 | data->setComputedStyle(document()->styleForElementIgnoringPendingStylesheets(this)); |
| 2238 | return pseudoElementSpecifier ? data->computedStyle()->getCachedPseudoStyle(pseudoElementSpecifier) : data->computedStyle(); |
| 2239 | } |
| 2240 | |
| 2241 | void Element::setStyleAffectedByEmpty() |
| 2242 | { |
| 2243 | ensureElementRareData()->setStyleAffectedByEmpty(true); |
| 2244 | } |
| 2245 | |
| 2246 | void Element::setChildrenAffectedByHover(bool value) |
| 2247 | { |
| 2248 | if (value || hasRareData()) |
| 2249 | ensureElementRareData()->setChildrenAffectedByHover(value); |
| 2250 | } |
| 2251 | |
| 2252 | void Element::setChildrenAffectedByActive(bool value) |
| 2253 | { |
| 2254 | if (value || hasRareData()) |
| 2255 | ensureElementRareData()->setChildrenAffectedByActive(value); |
| 2256 | } |
| 2257 | |
| 2258 | void Element::setChildrenAffectedByDrag(bool value) |
| 2259 | { |
| 2260 | if (value || hasRareData()) |
| 2261 | ensureElementRareData()->setChildrenAffectedByDrag(value); |
| 2262 | } |
| 2263 | |
| 2264 | void Element::setChildrenAffectedByFirstChildRules() |
| 2265 | { |
| 2266 | ensureElementRareData()->setChildrenAffectedByFirstChildRules(true); |
| 2267 | } |
| 2268 | |
| 2269 | void Element::setChildrenAffectedByLastChildRules() |
| 2270 | { |
| 2271 | ensureElementRareData()->setChildrenAffectedByLastChildRules(true); |
| 2272 | } |
| 2273 | |
| 2274 | void Element::setChildrenAffectedByDirectAdjacentRules() |
| 2275 | { |
| 2276 | ensureElementRareData()->setChildrenAffectedByDirectAdjacentRules(true); |
| 2277 | } |
| 2278 | |
| 2279 | void Element::setChildrenAffectedByForwardPositionalRules() |
| 2280 | { |
| 2281 | ensureElementRareData()->setChildrenAffectedByForwardPositionalRules(true); |
| 2282 | } |
| 2283 | |
| 2284 | void Element::setChildrenAffectedByBackwardPositionalRules() |
| 2285 | { |
| 2286 | ensureElementRareData()->setChildrenAffectedByBackwardPositionalRules(true); |
| 2287 | } |
| 2288 | |
| 2289 | void Element::setChildIndex(unsigned index) |
| 2290 | { |
| 2291 | ElementRareData* rareData = ensureElementRareData(); |
| 2292 | if (RenderStyle* style = renderStyle()) |
| 2293 | style->setUnique(); |
| 2294 | rareData->setChildIndex(index); |
| 2295 | } |
| 2296 | |
| 2297 | bool Element::hasFlagsSetDuringStylingOfChildren() const |
| 2298 | { |
| 2299 | if (!hasRareData()) |
| 2300 | return false; |
| 2301 | return rareDataChildrenAffectedByHover() |
| 2302 | || rareDataChildrenAffectedByActive() |
| 2303 | || rareDataChildrenAffectedByDrag() |
| 2304 | || rareDataChildrenAffectedByFirstChildRules() |
| 2305 | || rareDataChildrenAffectedByLastChildRules() |
| 2306 | || rareDataChildrenAffectedByDirectAdjacentRules() |
| 2307 | || rareDataChildrenAffectedByForwardPositionalRules() |
| 2308 | || rareDataChildrenAffectedByBackwardPositionalRules(); |
| 2309 | } |
| 2310 | |
| 2311 | bool Element::rareDataStyleAffectedByEmpty() const |
| 2312 | { |
| 2313 | ASSERT(hasRareData()); |
| 2314 | return elementRareData()->styleAffectedByEmpty(); |
| 2315 | } |
| 2316 | |
| 2317 | bool Element::rareDataChildrenAffectedByHover() const |
| 2318 | { |
| 2319 | ASSERT(hasRareData()); |
| 2320 | return elementRareData()->childrenAffectedByHover(); |
| 2321 | } |
| 2322 | |
| 2323 | bool Element::rareDataChildrenAffectedByActive() const |
| 2324 | { |
| 2325 | ASSERT(hasRareData()); |
| 2326 | return elementRareData()->childrenAffectedByActive(); |
| 2327 | } |
| 2328 | |
| 2329 | bool Element::rareDataChildrenAffectedByDrag() const |
| 2330 | { |
| 2331 | ASSERT(hasRareData()); |
| 2332 | return elementRareData()->childrenAffectedByDrag(); |
| 2333 | } |
| 2334 | |
| 2335 | bool Element::rareDataChildrenAffectedByFirstChildRules() const |
| 2336 | { |
| 2337 | ASSERT(hasRareData()); |
| 2338 | return elementRareData()->childrenAffectedByFirstChildRules(); |
| 2339 | } |
| 2340 | |
| 2341 | bool Element::rareDataChildrenAffectedByLastChildRules() const |
| 2342 | { |
| 2343 | ASSERT(hasRareData()); |
| 2344 | return elementRareData()->childrenAffectedByLastChildRules(); |
| 2345 | } |
| 2346 | |
| 2347 | bool Element::rareDataChildrenAffectedByDirectAdjacentRules() const |
| 2348 | { |
| 2349 | ASSERT(hasRareData()); |
| 2350 | return elementRareData()->childrenAffectedByDirectAdjacentRules(); |
| 2351 | } |
| 2352 | |
| 2353 | bool Element::rareDataChildrenAffectedByForwardPositionalRules() const |
| 2354 | { |
| 2355 | ASSERT(hasRareData()); |
| 2356 | return elementRareData()->childrenAffectedByForwardPositionalRules(); |
| 2357 | } |
| 2358 | |
| 2359 | bool Element::rareDataChildrenAffectedByBackwardPositionalRules() const |
| 2360 | { |
| 2361 | ASSERT(hasRareData()); |
| 2362 | return elementRareData()->childrenAffectedByBackwardPositionalRules(); |
| 2363 | } |
| 2364 | |
| 2365 | unsigned Element::rareDataChildIndex() const |
| 2366 | { |
| 2367 | ASSERT(hasRareData()); |
| 2368 | return elementRareData()->childIndex(); |
| 2369 | } |
| 2370 | |
| 2371 | void Element::setIsInCanvasSubtree(bool isInCanvasSubtree) |
| 2372 | { |
| 2373 | ensureElementRareData()->setIsInCanvasSubtree(isInCanvasSubtree); |
| 2374 | } |
| 2375 | |
| 2376 | bool Element::isInCanvasSubtree() const |
| 2377 | { |
| 2378 | return hasRareData() && elementRareData()->isInCanvasSubtree(); |
| 2379 | } |
| 2380 | |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 2381 | void Element::setIsInsideRegion(bool value) |
| 2382 | { |
| 2383 | if (value == isInsideRegion()) |
| 2384 | return; |
| 2385 | |
| 2386 | ensureElementRareData()->setIsInsideRegion(value); |
| 2387 | } |
| 2388 | |
| 2389 | bool Element::isInsideRegion() const |
| 2390 | { |
| 2391 | return hasRareData() ? elementRareData()->isInsideRegion() : false; |
| 2392 | } |
| 2393 | |
| 2394 | void Element::setRegionOversetState(RegionOversetState state) |
| 2395 | { |
| 2396 | ensureElementRareData()->setRegionOversetState(state); |
| 2397 | } |
| 2398 | |
| 2399 | RegionOversetState Element::regionOversetState() const |
| 2400 | { |
| 2401 | return hasRareData() ? elementRareData()->regionOversetState() : RegionUndefined; |
| 2402 | } |
| 2403 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2404 | AtomicString Element::computeInheritedLanguage() const |
| 2405 | { |
| 2406 | const Node* n = this; |
| 2407 | AtomicString value; |
| 2408 | // The language property is inherited, so we iterate over the parents to find the first language. |
| 2409 | do { |
| 2410 | if (n->isElementNode()) { |
| 2411 | if (const ElementData* elementData = toElement(n)->elementData()) { |
| 2412 | // Spec: xml:lang takes precedence -- http://www.w3.org/TR/xhtml1/#C_7 |
| 2413 | if (const Attribute* attribute = elementData->getAttributeItem(XMLNames::langAttr)) |
| 2414 | value = attribute->value(); |
| 2415 | else if (const Attribute* attribute = elementData->getAttributeItem(HTMLNames::langAttr)) |
| 2416 | value = attribute->value(); |
| 2417 | } |
| 2418 | } else if (n->isDocumentNode()) { |
| 2419 | // checking the MIME content-language |
| 2420 | value = toDocument(n)->contentLanguage(); |
| 2421 | } |
| 2422 | |
| 2423 | n = n->parentNode(); |
| 2424 | } while (n && value.isNull()); |
| 2425 | |
| 2426 | return value; |
| 2427 | } |
| 2428 | |
| 2429 | Locale& Element::locale() const |
| 2430 | { |
| 2431 | return document()->getCachedLocale(computeInheritedLanguage()); |
| 2432 | } |
| 2433 | |
| 2434 | void Element::cancelFocusAppearanceUpdate() |
| 2435 | { |
| 2436 | if (hasRareData()) |
| 2437 | elementRareData()->setNeedsFocusAppearanceUpdateSoonAfterAttach(false); |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 2438 | if (document()->focusedElement() == this) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2439 | document()->cancelFocusAppearanceUpdate(); |
| 2440 | } |
| 2441 | |
| 2442 | void Element::normalizeAttributes() |
| 2443 | { |
| 2444 | if (!hasAttributes()) |
| 2445 | return; |
| 2446 | for (unsigned i = 0; i < attributeCount(); ++i) { |
| 2447 | if (RefPtr<Attr> attr = attrIfExists(attributeItem(i)->name())) |
| 2448 | attr->normalize(); |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | void Element::updatePseudoElement(PseudoId pseudoId, StyleChange change) |
| 2453 | { |
| 2454 | PseudoElement* element = pseudoElement(pseudoId); |
| 2455 | if (element && (needsStyleRecalc() || shouldRecalcStyle(change, element))) { |
| 2456 | // PseudoElement styles hang off their parent element's style so if we needed |
| 2457 | // a style recalc we should Force one on the pseudo. |
| 2458 | element->recalcStyle(needsStyleRecalc() ? Force : change); |
| 2459 | |
| 2460 | // Wait until our parent is not displayed or pseudoElementRendererIsNeeded |
| 2461 | // is false, otherwise we could continously create and destroy PseudoElements |
| 2462 | // when RenderObject::isChildAllowed on our parent returns false for the |
| 2463 | // PseudoElement's renderer for each style recalc. |
| 2464 | if (!renderer() || !pseudoElementRendererIsNeeded(renderer()->getCachedPseudoStyle(pseudoId))) |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 2465 | elementRareData()->setPseudoElement(pseudoId, 0); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2466 | } else if (change >= Inherit || needsStyleRecalc()) |
| 2467 | createPseudoElementIfNeeded(pseudoId); |
| 2468 | } |
| 2469 | |
| 2470 | void Element::createPseudoElementIfNeeded(PseudoId pseudoId) |
| 2471 | { |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 2472 | if ((pseudoId == BEFORE || pseudoId == AFTER) && !document()->styleSheetCollection()->usesBeforeAfterRules()) |
| 2473 | return; |
| 2474 | |
| 2475 | if (pseudoId == BACKDROP && !isInTopLayer()) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2476 | return; |
| 2477 | |
| 2478 | if (!renderer() || !pseudoElementRendererIsNeeded(renderer()->getCachedPseudoStyle(pseudoId))) |
| 2479 | return; |
| 2480 | |
| 2481 | if (!renderer()->canHaveGeneratedChildren()) |
| 2482 | return; |
| 2483 | |
| 2484 | ASSERT(!isPseudoElement()); |
| 2485 | RefPtr<PseudoElement> element = PseudoElement::create(this, pseudoId); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 2486 | if (pseudoId == BACKDROP) |
| 2487 | document()->addToTopLayer(element.get(), this); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2488 | element->attach(); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 2489 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 2490 | ensureElementRareData()->setPseudoElement(pseudoId, element.release()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | PseudoElement* Element::pseudoElement(PseudoId pseudoId) const |
| 2494 | { |
| 2495 | return hasRareData() ? elementRareData()->pseudoElement(pseudoId) : 0; |
| 2496 | } |
| 2497 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2498 | RenderObject* Element::pseudoElementRenderer(PseudoId pseudoId) const |
| 2499 | { |
| 2500 | if (PseudoElement* element = pseudoElement(pseudoId)) |
| 2501 | return element->renderer(); |
| 2502 | return 0; |
| 2503 | } |
| 2504 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 2505 | bool Element::webkitMatchesSelector(const String& selector, ExceptionState& es) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2506 | { |
| 2507 | if (selector.isEmpty()) { |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 2508 | es.throwDOMException(SyntaxError); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2509 | return false; |
| 2510 | } |
| 2511 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 2512 | SelectorQuery* selectorQuery = document()->selectorQueryCache()->add(selector, document(), es); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2513 | if (!selectorQuery) |
| 2514 | return false; |
| 2515 | return selectorQuery->matches(this); |
| 2516 | } |
| 2517 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2518 | DOMTokenList* Element::classList() |
| 2519 | { |
| 2520 | ElementRareData* data = ensureElementRareData(); |
| 2521 | if (!data->classList()) |
| 2522 | data->setClassList(ClassList::create(this)); |
| 2523 | return data->classList(); |
| 2524 | } |
| 2525 | |
| 2526 | DOMStringMap* Element::dataset() |
| 2527 | { |
| 2528 | ElementRareData* data = ensureElementRareData(); |
| 2529 | if (!data->dataset()) |
| 2530 | data->setDataset(DatasetDOMStringMap::create(this)); |
| 2531 | return data->dataset(); |
| 2532 | } |
| 2533 | |
| 2534 | KURL Element::getURLAttribute(const QualifiedName& name) const |
| 2535 | { |
| 2536 | #if !ASSERT_DISABLED |
| 2537 | if (elementData()) { |
| 2538 | if (const Attribute* attribute = getAttributeItem(name)) |
| 2539 | ASSERT(isURLAttribute(*attribute)); |
| 2540 | } |
| 2541 | #endif |
| 2542 | return document()->completeURL(stripLeadingAndTrailingHTMLSpaces(getAttribute(name))); |
| 2543 | } |
| 2544 | |
| 2545 | KURL Element::getNonEmptyURLAttribute(const QualifiedName& name) const |
| 2546 | { |
| 2547 | #if !ASSERT_DISABLED |
| 2548 | if (elementData()) { |
| 2549 | if (const Attribute* attribute = getAttributeItem(name)) |
| 2550 | ASSERT(isURLAttribute(*attribute)); |
| 2551 | } |
| 2552 | #endif |
| 2553 | String value = stripLeadingAndTrailingHTMLSpaces(getAttribute(name)); |
| 2554 | if (value.isEmpty()) |
| 2555 | return KURL(); |
| 2556 | return document()->completeURL(value); |
| 2557 | } |
| 2558 | |
| 2559 | int Element::getIntegralAttribute(const QualifiedName& attributeName) const |
| 2560 | { |
| 2561 | return getAttribute(attributeName).string().toInt(); |
| 2562 | } |
| 2563 | |
| 2564 | void Element::setIntegralAttribute(const QualifiedName& attributeName, int value) |
| 2565 | { |
| 2566 | // FIXME: Need an AtomicString version of String::number. |
| 2567 | setAttribute(attributeName, String::number(value)); |
| 2568 | } |
| 2569 | |
| 2570 | unsigned Element::getUnsignedIntegralAttribute(const QualifiedName& attributeName) const |
| 2571 | { |
| 2572 | return getAttribute(attributeName).string().toUInt(); |
| 2573 | } |
| 2574 | |
| 2575 | void Element::setUnsignedIntegralAttribute(const QualifiedName& attributeName, unsigned value) |
| 2576 | { |
| 2577 | // FIXME: Need an AtomicString version of String::number. |
| 2578 | setAttribute(attributeName, String::number(value)); |
| 2579 | } |
| 2580 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2581 | bool Element::childShouldCreateRenderer(const NodeRenderingContext& childContext) const |
| 2582 | { |
| 2583 | // Only create renderers for SVG elements whose parents are SVG elements, or for proper <svg xmlns="svgNS"> subdocuments. |
| 2584 | if (childContext.node()->isSVGElement()) |
| 2585 | return childContext.node()->hasTagName(SVGNames::svgTag) || isSVGElement(); |
| 2586 | |
| 2587 | return ContainerNode::childShouldCreateRenderer(childContext); |
| 2588 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2589 | |
| 2590 | void Element::webkitRequestFullscreen() |
| 2591 | { |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 2592 | FullscreenElementStack::from(document())->requestFullScreenForElement(this, ALLOW_KEYBOARD_INPUT, FullscreenElementStack::EnforceIFrameAllowFullScreenRequirement); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2593 | } |
| 2594 | |
| 2595 | void Element::webkitRequestFullScreen(unsigned short flags) |
| 2596 | { |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 2597 | FullscreenElementStack::from(document())->requestFullScreenForElement(this, (flags | LEGACY_MOZILLA_REQUEST), FullscreenElementStack::EnforceIFrameAllowFullScreenRequirement); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2598 | } |
| 2599 | |
| 2600 | bool Element::containsFullScreenElement() const |
| 2601 | { |
| 2602 | return hasRareData() && elementRareData()->containsFullScreenElement(); |
| 2603 | } |
| 2604 | |
| 2605 | void Element::setContainsFullScreenElement(bool flag) |
| 2606 | { |
| 2607 | ensureElementRareData()->setContainsFullScreenElement(flag); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 2608 | setNeedsStyleRecalc(SubtreeStyleChange); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2609 | } |
| 2610 | |
| 2611 | static Element* parentCrossingFrameBoundaries(Element* element) |
| 2612 | { |
| 2613 | ASSERT(element); |
| 2614 | return element->parentElement() ? element->parentElement() : element->document()->ownerElement(); |
| 2615 | } |
| 2616 | |
| 2617 | void Element::setContainsFullScreenElementOnAncestorsCrossingFrameBoundaries(bool flag) |
| 2618 | { |
| 2619 | Element* element = this; |
| 2620 | while ((element = parentCrossingFrameBoundaries(element))) |
| 2621 | element->setContainsFullScreenElement(flag); |
| 2622 | } |
| 2623 | |
| 2624 | bool Element::isInTopLayer() const |
| 2625 | { |
| 2626 | return hasRareData() && elementRareData()->isInTopLayer(); |
| 2627 | } |
| 2628 | |
| 2629 | void Element::setIsInTopLayer(bool inTopLayer) |
| 2630 | { |
| 2631 | if (isInTopLayer() == inTopLayer) |
| 2632 | return; |
| 2633 | ensureElementRareData()->setIsInTopLayer(inTopLayer); |
| 2634 | |
| 2635 | // We must ensure a reattach occurs so the renderer is inserted in the correct sibling order under RenderView according to its |
| 2636 | // top layer position, or in its usual place if not in the top layer. |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 2637 | lazyReattachIfAttached(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2638 | } |
| 2639 | |
| 2640 | void Element::webkitRequestPointerLock() |
| 2641 | { |
| 2642 | if (document()->page()) |
| 2643 | document()->page()->pointerLockController()->requestPointerLock(this); |
| 2644 | } |
| 2645 | |
| 2646 | SpellcheckAttributeState Element::spellcheckAttributeState() const |
| 2647 | { |
| 2648 | const AtomicString& value = getAttribute(HTMLNames::spellcheckAttr); |
| 2649 | if (value == nullAtom) |
| 2650 | return SpellcheckAttributeDefault; |
| 2651 | if (equalIgnoringCase(value, "true") || equalIgnoringCase(value, "")) |
| 2652 | return SpellcheckAttributeTrue; |
| 2653 | if (equalIgnoringCase(value, "false")) |
| 2654 | return SpellcheckAttributeFalse; |
| 2655 | |
| 2656 | return SpellcheckAttributeDefault; |
| 2657 | } |
| 2658 | |
| 2659 | bool Element::isSpellCheckingEnabled() const |
| 2660 | { |
| 2661 | for (const Element* element = this; element; element = element->parentOrShadowHostElement()) { |
| 2662 | switch (element->spellcheckAttributeState()) { |
| 2663 | case SpellcheckAttributeTrue: |
| 2664 | return true; |
| 2665 | case SpellcheckAttributeFalse: |
| 2666 | return false; |
| 2667 | case SpellcheckAttributeDefault: |
| 2668 | break; |
| 2669 | } |
| 2670 | } |
| 2671 | |
| 2672 | return true; |
| 2673 | } |
| 2674 | |
| 2675 | RenderRegion* Element::renderRegion() const |
| 2676 | { |
| 2677 | if (renderer() && renderer()->isRenderRegion()) |
| 2678 | return toRenderRegion(renderer()); |
| 2679 | |
| 2680 | return 0; |
| 2681 | } |
| 2682 | |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 2683 | bool Element::shouldMoveToFlowThread(RenderStyle* styleToUse) const |
| 2684 | { |
| 2685 | ASSERT(styleToUse); |
| 2686 | |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 2687 | if (FullscreenElementStack::isActiveFullScreenElement(this)) |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 2688 | return false; |
| 2689 | |
| 2690 | if (isInShadowTree()) |
| 2691 | return false; |
| 2692 | |
| 2693 | if (styleToUse->flowThread().isEmpty()) |
| 2694 | return false; |
| 2695 | |
| 2696 | return !isRegisteredWithNamedFlow(); |
| 2697 | } |
| 2698 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2699 | const AtomicString& Element::webkitRegionOverset() const |
| 2700 | { |
| 2701 | document()->updateLayoutIgnorePendingStylesheets(); |
| 2702 | |
| 2703 | DEFINE_STATIC_LOCAL(AtomicString, undefinedState, ("undefined", AtomicString::ConstructFromLiteral)); |
| 2704 | if (!RuntimeEnabledFeatures::cssRegionsEnabled() || !renderRegion()) |
| 2705 | return undefinedState; |
| 2706 | |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 2707 | switch (renderRegion()->regionOversetState()) { |
| 2708 | case RegionFit: { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2709 | DEFINE_STATIC_LOCAL(AtomicString, fitState, ("fit", AtomicString::ConstructFromLiteral)); |
| 2710 | return fitState; |
| 2711 | } |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 2712 | case RegionEmpty: { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2713 | DEFINE_STATIC_LOCAL(AtomicString, emptyState, ("empty", AtomicString::ConstructFromLiteral)); |
| 2714 | return emptyState; |
| 2715 | } |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 2716 | case RegionOverset: { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2717 | DEFINE_STATIC_LOCAL(AtomicString, overflowState, ("overset", AtomicString::ConstructFromLiteral)); |
| 2718 | return overflowState; |
| 2719 | } |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 2720 | case RegionUndefined: |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2721 | return undefinedState; |
| 2722 | } |
| 2723 | |
| 2724 | ASSERT_NOT_REACHED(); |
| 2725 | return undefinedState; |
| 2726 | } |
| 2727 | |
| 2728 | Vector<RefPtr<Range> > Element::webkitGetRegionFlowRanges() const |
| 2729 | { |
| 2730 | document()->updateLayoutIgnorePendingStylesheets(); |
| 2731 | |
| 2732 | Vector<RefPtr<Range> > rangeObjects; |
| 2733 | if (RuntimeEnabledFeatures::cssRegionsEnabled() && renderer() && renderer()->isRenderRegion()) { |
| 2734 | RenderRegion* region = toRenderRegion(renderer()); |
| 2735 | if (region->isValid()) |
| 2736 | region->getRanges(rangeObjects); |
| 2737 | } |
| 2738 | |
| 2739 | return rangeObjects; |
| 2740 | } |
| 2741 | |
| 2742 | #ifndef NDEBUG |
| 2743 | bool Element::fastAttributeLookupAllowed(const QualifiedName& name) const |
| 2744 | { |
| 2745 | if (name == HTMLNames::styleAttr) |
| 2746 | return false; |
| 2747 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2748 | if (isSVGElement()) |
| 2749 | return !static_cast<const SVGElement*>(this)->isAnimatableAttribute(name); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2750 | |
| 2751 | return true; |
| 2752 | } |
| 2753 | #endif |
| 2754 | |
| 2755 | #ifdef DUMP_NODE_STATISTICS |
| 2756 | bool Element::hasNamedNodeMap() const |
| 2757 | { |
| 2758 | return hasRareData() && elementRareData()->attributeMap(); |
| 2759 | } |
| 2760 | #endif |
| 2761 | |
| 2762 | inline void Element::updateName(const AtomicString& oldName, const AtomicString& newName) |
| 2763 | { |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 2764 | if (!inDocument() || isInShadowTree()) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2765 | return; |
| 2766 | |
| 2767 | if (oldName == newName) |
| 2768 | return; |
| 2769 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 2770 | if (shouldRegisterAsNamedItem()) |
| 2771 | updateNamedItemRegistration(oldName, newName); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2772 | } |
| 2773 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 2774 | inline void Element::updateId(const AtomicString& oldId, const AtomicString& newId) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2775 | { |
| 2776 | if (!isInTreeScope()) |
| 2777 | return; |
| 2778 | |
| 2779 | if (oldId == newId) |
| 2780 | return; |
| 2781 | |
| 2782 | updateId(treeScope(), oldId, newId); |
| 2783 | } |
| 2784 | |
| 2785 | inline void Element::updateId(TreeScope* scope, const AtomicString& oldId, const AtomicString& newId) |
| 2786 | { |
| 2787 | ASSERT(isInTreeScope()); |
| 2788 | ASSERT(oldId != newId); |
| 2789 | |
| 2790 | if (!oldId.isEmpty()) |
| 2791 | scope->removeElementById(oldId, this); |
| 2792 | if (!newId.isEmpty()) |
| 2793 | scope->addElementById(newId, this); |
| 2794 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 2795 | if (shouldRegisterAsExtraNamedItem()) |
| 2796 | updateExtraNamedItemRegistration(oldId, newId); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2797 | } |
| 2798 | |
| 2799 | void Element::updateLabel(TreeScope* scope, const AtomicString& oldForAttributeValue, const AtomicString& newForAttributeValue) |
| 2800 | { |
| 2801 | ASSERT(hasTagName(labelTag)); |
| 2802 | |
| 2803 | if (!inDocument()) |
| 2804 | return; |
| 2805 | |
| 2806 | if (oldForAttributeValue == newForAttributeValue) |
| 2807 | return; |
| 2808 | |
| 2809 | if (!oldForAttributeValue.isEmpty()) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 2810 | scope->removeLabel(oldForAttributeValue, toHTMLLabelElement(this)); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2811 | if (!newForAttributeValue.isEmpty()) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 2812 | scope->addLabel(newForAttributeValue, toHTMLLabelElement(this)); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2813 | } |
| 2814 | |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 2815 | static bool hasSelectorForAttribute(Document* document, const AtomicString& localName) |
| 2816 | { |
| 2817 | return document->styleResolver() && document->styleResolver()->ruleFeatureSet().hasSelectorForAttribute(localName); |
| 2818 | } |
| 2819 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2820 | void Element::willModifyAttribute(const QualifiedName& name, const AtomicString& oldValue, const AtomicString& newValue) |
| 2821 | { |
| 2822 | if (isIdAttributeName(name)) |
| 2823 | updateId(oldValue, newValue); |
| 2824 | else if (name == HTMLNames::nameAttr) |
| 2825 | updateName(oldValue, newValue); |
| 2826 | else if (name == HTMLNames::forAttr && hasTagName(labelTag)) { |
| 2827 | TreeScope* scope = treeScope(); |
| 2828 | if (scope->shouldCacheLabelsByForAttribute()) |
| 2829 | updateLabel(scope, oldValue, newValue); |
| 2830 | } |
| 2831 | |
| 2832 | if (oldValue != newValue) { |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 2833 | if (attached() && hasSelectorForAttribute(document(), name.localName())) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2834 | setNeedsStyleRecalc(); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 2835 | |
| 2836 | if (isUpgradedCustomElement()) |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 2837 | CustomElement::attributeDidChange(this, name.localName(), oldValue, newValue); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | if (OwnPtr<MutationObserverInterestGroup> recipients = MutationObserverInterestGroup::createForAttributesMutation(this, name)) |
| 2841 | recipients->enqueueMutationRecord(MutationRecord::createAttributes(this, name, oldValue)); |
| 2842 | |
| 2843 | InspectorInstrumentation::willModifyDOMAttr(document(), this, oldValue, newValue); |
| 2844 | } |
| 2845 | |
| 2846 | void Element::didAddAttribute(const QualifiedName& name, const AtomicString& value) |
| 2847 | { |
| 2848 | attributeChanged(name, value); |
| 2849 | InspectorInstrumentation::didModifyDOMAttr(document(), this, name.localName(), value); |
| 2850 | dispatchSubtreeModifiedEvent(); |
| 2851 | } |
| 2852 | |
| 2853 | void Element::didModifyAttribute(const QualifiedName& name, const AtomicString& value) |
| 2854 | { |
| 2855 | attributeChanged(name, value); |
| 2856 | InspectorInstrumentation::didModifyDOMAttr(document(), this, name.localName(), value); |
| 2857 | // Do not dispatch a DOMSubtreeModified event here; see bug 81141. |
| 2858 | } |
| 2859 | |
| 2860 | void Element::didRemoveAttribute(const QualifiedName& name) |
| 2861 | { |
| 2862 | attributeChanged(name, nullAtom); |
| 2863 | InspectorInstrumentation::didRemoveDOMAttr(document(), this, name.localName()); |
| 2864 | dispatchSubtreeModifiedEvent(); |
| 2865 | } |
| 2866 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 2867 | void Element::didMoveToNewDocument(Document* oldDocument) |
| 2868 | { |
| 2869 | Node::didMoveToNewDocument(oldDocument); |
| 2870 | |
| 2871 | // If the documents differ by quirks mode then they differ by case sensitivity |
| 2872 | // for class and id names so we need to go through the attribute change logic |
| 2873 | // to pick up the new casing in the ElementData. |
| 2874 | if (oldDocument->inQuirksMode() != document()->inQuirksMode()) { |
| 2875 | if (hasID()) |
| 2876 | setIdAttribute(getIdAttribute()); |
| 2877 | if (hasClass()) |
| 2878 | setAttribute(HTMLNames::classAttr, getClassAttribute()); |
| 2879 | } |
| 2880 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2881 | |
Ben Murdoch | df95704 | 2013-08-06 11:01:27 +0100 | [diff] [blame^] | 2882 | void Element::updateNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName) |
| 2883 | { |
| 2884 | if (!document()->isHTMLDocument()) |
| 2885 | return; |
| 2886 | |
| 2887 | if (!oldName.isEmpty()) |
| 2888 | toHTMLDocument(document())->removeNamedItem(oldName); |
| 2889 | |
| 2890 | if (!newName.isEmpty()) |
| 2891 | toHTMLDocument(document())->addNamedItem(newName); |
| 2892 | } |
| 2893 | |
| 2894 | void Element::updateExtraNamedItemRegistration(const AtomicString& oldId, const AtomicString& newId) |
| 2895 | { |
| 2896 | if (!document()->isHTMLDocument()) |
| 2897 | return; |
| 2898 | |
| 2899 | if (!oldId.isEmpty()) |
| 2900 | toHTMLDocument(document())->removeExtraNamedItem(oldId); |
| 2901 | |
| 2902 | if (!newId.isEmpty()) |
| 2903 | toHTMLDocument(document())->addExtraNamedItem(newId); |
| 2904 | } |
| 2905 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2906 | PassRefPtr<HTMLCollection> Element::ensureCachedHTMLCollection(CollectionType type) |
| 2907 | { |
| 2908 | if (HTMLCollection* collection = cachedHTMLCollection(type)) |
| 2909 | return collection; |
| 2910 | |
| 2911 | RefPtr<HTMLCollection> collection; |
| 2912 | if (type == TableRows) { |
| 2913 | ASSERT(hasTagName(tableTag)); |
| 2914 | return ensureRareData()->ensureNodeLists()->addCacheWithAtomicName<HTMLTableRowsCollection>(this, type); |
| 2915 | } else if (type == SelectOptions) { |
| 2916 | ASSERT(hasTagName(selectTag)); |
| 2917 | return ensureRareData()->ensureNodeLists()->addCacheWithAtomicName<HTMLOptionsCollection>(this, type); |
| 2918 | } else if (type == FormControls) { |
| 2919 | ASSERT(hasTagName(formTag) || hasTagName(fieldsetTag)); |
| 2920 | return ensureRareData()->ensureNodeLists()->addCacheWithAtomicName<HTMLFormControlsCollection>(this, type); |
| 2921 | } |
| 2922 | return ensureRareData()->ensureNodeLists()->addCacheWithAtomicName<HTMLCollection>(this, type); |
| 2923 | } |
| 2924 | |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 2925 | static void scheduleLayerUpdateCallback(Node* node) |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 2926 | { |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 2927 | // Notify the renderer even is the styles are identical since it may need to |
| 2928 | // create or destroy a RenderLayer. |
| 2929 | node->setNeedsStyleRecalc(LocalStyleChange, StyleChangeFromRenderer); |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 2930 | } |
| 2931 | |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 2932 | void Element::scheduleLayerUpdate() |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 2933 | { |
| 2934 | if (postAttachCallbacksAreSuspended()) |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 2935 | queuePostAttachCallback(scheduleLayerUpdateCallback, this); |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 2936 | else |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 2937 | scheduleLayerUpdateCallback(this); |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 2938 | } |
| 2939 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 2940 | HTMLCollection* Element::cachedHTMLCollection(CollectionType type) |
| 2941 | { |
| 2942 | return hasRareData() && rareData()->nodeLists() ? rareData()->nodeLists()->cacheWithAtomicName<HTMLCollection>(type) : 0; |
| 2943 | } |
| 2944 | |
| 2945 | IntSize Element::savedLayerScrollOffset() const |
| 2946 | { |
| 2947 | return hasRareData() ? elementRareData()->savedLayerScrollOffset() : IntSize(); |
| 2948 | } |
| 2949 | |
| 2950 | void Element::setSavedLayerScrollOffset(const IntSize& size) |
| 2951 | { |
| 2952 | if (size.isZero() && !hasRareData()) |
| 2953 | return; |
| 2954 | ensureElementRareData()->setSavedLayerScrollOffset(size); |
| 2955 | } |
| 2956 | |
| 2957 | PassRefPtr<Attr> Element::attrIfExists(const QualifiedName& name) |
| 2958 | { |
| 2959 | if (AttrNodeList* attrNodeList = attrNodeListForElement(this)) |
| 2960 | return findAttrNodeInList(attrNodeList, name); |
| 2961 | return 0; |
| 2962 | } |
| 2963 | |
| 2964 | PassRefPtr<Attr> Element::ensureAttr(const QualifiedName& name) |
| 2965 | { |
| 2966 | AttrNodeList* attrNodeList = ensureAttrNodeListForElement(this); |
| 2967 | RefPtr<Attr> attrNode = findAttrNodeInList(attrNodeList, name); |
| 2968 | if (!attrNode) { |
| 2969 | attrNode = Attr::create(this, name); |
| 2970 | treeScope()->adoptIfNeeded(attrNode.get()); |
| 2971 | attrNodeList->append(attrNode); |
| 2972 | } |
| 2973 | return attrNode.release(); |
| 2974 | } |
| 2975 | |
| 2976 | void Element::detachAttrNodeFromElementWithValue(Attr* attrNode, const AtomicString& value) |
| 2977 | { |
| 2978 | ASSERT(hasSyntheticAttrChildNodes()); |
| 2979 | attrNode->detachFromElementWithValue(value); |
| 2980 | |
| 2981 | AttrNodeList* attrNodeList = attrNodeListForElement(this); |
| 2982 | for (unsigned i = 0; i < attrNodeList->size(); ++i) { |
| 2983 | if (attrNodeList->at(i)->qualifiedName() == attrNode->qualifiedName()) { |
| 2984 | attrNodeList->remove(i); |
| 2985 | if (attrNodeList->isEmpty()) |
| 2986 | removeAttrNodeListForElement(this); |
| 2987 | return; |
| 2988 | } |
| 2989 | } |
| 2990 | ASSERT_NOT_REACHED(); |
| 2991 | } |
| 2992 | |
| 2993 | void Element::detachAllAttrNodesFromElement() |
| 2994 | { |
| 2995 | AttrNodeList* attrNodeList = attrNodeListForElement(this); |
| 2996 | ASSERT(attrNodeList); |
| 2997 | |
| 2998 | for (unsigned i = 0; i < attributeCount(); ++i) { |
| 2999 | const Attribute* attribute = attributeItem(i); |
| 3000 | if (RefPtr<Attr> attrNode = findAttrNodeInList(attrNodeList, attribute->name())) |
| 3001 | attrNode->detachFromElementWithValue(attribute->value()); |
| 3002 | } |
| 3003 | |
| 3004 | removeAttrNodeListForElement(this); |
| 3005 | } |
| 3006 | |
| 3007 | void Element::willRecalcStyle(StyleChange) |
| 3008 | { |
| 3009 | ASSERT(hasCustomStyleCallbacks()); |
| 3010 | } |
| 3011 | |
| 3012 | void Element::didRecalcStyle(StyleChange) |
| 3013 | { |
| 3014 | ASSERT(hasCustomStyleCallbacks()); |
| 3015 | } |
| 3016 | |
| 3017 | |
| 3018 | PassRefPtr<RenderStyle> Element::customStyleForRenderer() |
| 3019 | { |
| 3020 | ASSERT(hasCustomStyleCallbacks()); |
| 3021 | return 0; |
| 3022 | } |
| 3023 | |
| 3024 | void Element::cloneAttributesFromElement(const Element& other) |
| 3025 | { |
| 3026 | if (hasSyntheticAttrChildNodes()) |
| 3027 | detachAllAttrNodesFromElement(); |
| 3028 | |
| 3029 | other.synchronizeAllAttributes(); |
| 3030 | if (!other.m_elementData) { |
| 3031 | m_elementData.clear(); |
| 3032 | return; |
| 3033 | } |
| 3034 | |
| 3035 | const AtomicString& oldID = getIdAttribute(); |
| 3036 | const AtomicString& newID = other.getIdAttribute(); |
| 3037 | |
| 3038 | if (!oldID.isNull() || !newID.isNull()) |
| 3039 | updateId(oldID, newID); |
| 3040 | |
| 3041 | const AtomicString& oldName = getNameAttribute(); |
| 3042 | const AtomicString& newName = other.getNameAttribute(); |
| 3043 | |
| 3044 | if (!oldName.isNull() || !newName.isNull()) |
| 3045 | updateName(oldName, newName); |
| 3046 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 3047 | // Quirks mode makes class and id not case sensitive. We can't share the ElementData |
| 3048 | // if the idForStyleResolution and the className need different casing. |
| 3049 | bool ownerDocumentsHaveDifferentCaseSensitivity = false; |
| 3050 | if (other.hasClass() || other.hasID()) |
| 3051 | ownerDocumentsHaveDifferentCaseSensitivity = other.document()->inQuirksMode() != document()->inQuirksMode(); |
| 3052 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 3053 | // If 'other' has a mutable ElementData, convert it to an immutable one so we can share it between both elements. |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 3054 | // We can only do this if there is no CSSOM wrapper for other's inline style, and there are no presentation attributes, |
| 3055 | // and sharing the data won't result in different case sensitivity of class or id. |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 3056 | if (other.m_elementData->isUnique() |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 3057 | && !ownerDocumentsHaveDifferentCaseSensitivity |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 3058 | && !other.m_elementData->presentationAttributeStyle() |
| 3059 | && (!other.m_elementData->inlineStyle() || !other.m_elementData->inlineStyle()->hasCSSOMWrapper())) |
| 3060 | const_cast<Element&>(other).m_elementData = static_cast<const UniqueElementData*>(other.m_elementData.get())->makeShareableCopy(); |
| 3061 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 3062 | if (!other.m_elementData->isUnique() && !ownerDocumentsHaveDifferentCaseSensitivity) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 3063 | m_elementData = other.m_elementData; |
| 3064 | else |
| 3065 | m_elementData = other.m_elementData->makeUniqueCopy(); |
| 3066 | |
| 3067 | for (unsigned i = 0; i < m_elementData->length(); ++i) { |
| 3068 | const Attribute* attribute = const_cast<const ElementData*>(m_elementData.get())->attributeItem(i); |
| 3069 | attributeChangedFromParserOrByCloning(attribute->name(), attribute->value(), ModifiedByCloning); |
| 3070 | } |
| 3071 | } |
| 3072 | |
| 3073 | void Element::cloneDataFromElement(const Element& other) |
| 3074 | { |
| 3075 | cloneAttributesFromElement(other); |
| 3076 | copyNonAttributePropertiesFromElement(other); |
| 3077 | } |
| 3078 | |
| 3079 | void Element::createUniqueElementData() |
| 3080 | { |
| 3081 | if (!m_elementData) |
| 3082 | m_elementData = UniqueElementData::create(); |
| 3083 | else { |
| 3084 | ASSERT(!m_elementData->isUnique()); |
| 3085 | m_elementData = static_cast<ShareableElementData*>(m_elementData.get())->makeUniqueCopy(); |
| 3086 | } |
| 3087 | } |
| 3088 | |
Torne (Richard Coles) | 81a5157 | 2013-05-13 16:52:28 +0100 | [diff] [blame] | 3089 | InputMethodContext* Element::getInputContext() |
| 3090 | { |
| 3091 | return ensureElementRareData()->ensureInputMethodContext(toHTMLElement(this)); |
| 3092 | } |
| 3093 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 3094 | bool Element::hasPendingResources() const |
| 3095 | { |
| 3096 | return hasRareData() && elementRareData()->hasPendingResources(); |
| 3097 | } |
| 3098 | |
| 3099 | void Element::setHasPendingResources() |
| 3100 | { |
| 3101 | ensureElementRareData()->setHasPendingResources(true); |
| 3102 | } |
| 3103 | |
| 3104 | void Element::clearHasPendingResources() |
| 3105 | { |
| 3106 | ensureElementRareData()->setHasPendingResources(false); |
| 3107 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 3108 | |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 3109 | struct PresentationAttributeCacheKey { |
| 3110 | PresentationAttributeCacheKey() : tagName(0) { } |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 3111 | StringImpl* tagName; |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 3112 | // Only the values need refcounting. |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 3113 | Vector<pair<StringImpl*, AtomicString>, 3> attributesAndValues; |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 3114 | }; |
| 3115 | |
| 3116 | struct PresentationAttributeCacheEntry { |
| 3117 | WTF_MAKE_FAST_ALLOCATED; |
| 3118 | public: |
| 3119 | PresentationAttributeCacheKey key; |
| 3120 | RefPtr<StylePropertySet> value; |
| 3121 | }; |
| 3122 | |
| 3123 | typedef HashMap<unsigned, OwnPtr<PresentationAttributeCacheEntry>, AlreadyHashed> PresentationAttributeCache; |
| 3124 | |
| 3125 | static bool operator!=(const PresentationAttributeCacheKey& a, const PresentationAttributeCacheKey& b) |
| 3126 | { |
| 3127 | if (a.tagName != b.tagName) |
| 3128 | return true; |
| 3129 | return a.attributesAndValues != b.attributesAndValues; |
| 3130 | } |
| 3131 | |
| 3132 | static PresentationAttributeCache& presentationAttributeCache() |
| 3133 | { |
| 3134 | DEFINE_STATIC_LOCAL(PresentationAttributeCache, cache, ()); |
| 3135 | return cache; |
| 3136 | } |
| 3137 | |
| 3138 | class PresentationAttributeCacheCleaner { |
| 3139 | WTF_MAKE_NONCOPYABLE(PresentationAttributeCacheCleaner); WTF_MAKE_FAST_ALLOCATED; |
| 3140 | public: |
| 3141 | PresentationAttributeCacheCleaner() |
| 3142 | : m_hitCount(0) |
| 3143 | , m_cleanTimer(this, &PresentationAttributeCacheCleaner::cleanCache) |
| 3144 | { |
| 3145 | } |
| 3146 | |
| 3147 | void didHitPresentationAttributeCache() |
| 3148 | { |
| 3149 | if (presentationAttributeCache().size() < minimumPresentationAttributeCacheSizeForCleaning) |
| 3150 | return; |
| 3151 | |
| 3152 | m_hitCount++; |
| 3153 | |
| 3154 | if (!m_cleanTimer.isActive()) |
| 3155 | m_cleanTimer.startOneShot(presentationAttributeCacheCleanTimeInSeconds); |
| 3156 | } |
| 3157 | |
| 3158 | private: |
| 3159 | static const unsigned presentationAttributeCacheCleanTimeInSeconds = 60; |
| 3160 | static const int minimumPresentationAttributeCacheSizeForCleaning = 100; |
| 3161 | static const unsigned minimumPresentationAttributeCacheHitCountPerMinute = (100 * presentationAttributeCacheCleanTimeInSeconds) / 60; |
| 3162 | |
| 3163 | void cleanCache(Timer<PresentationAttributeCacheCleaner>* timer) |
| 3164 | { |
| 3165 | ASSERT_UNUSED(timer, timer == &m_cleanTimer); |
| 3166 | unsigned hitCount = m_hitCount; |
| 3167 | m_hitCount = 0; |
| 3168 | if (hitCount > minimumPresentationAttributeCacheHitCountPerMinute) |
| 3169 | return; |
| 3170 | presentationAttributeCache().clear(); |
| 3171 | } |
| 3172 | |
| 3173 | unsigned m_hitCount; |
| 3174 | Timer<PresentationAttributeCacheCleaner> m_cleanTimer; |
| 3175 | }; |
| 3176 | |
| 3177 | static PresentationAttributeCacheCleaner& presentationAttributeCacheCleaner() |
| 3178 | { |
| 3179 | DEFINE_STATIC_LOCAL(PresentationAttributeCacheCleaner, cleaner, ()); |
| 3180 | return cleaner; |
| 3181 | } |
| 3182 | |
| 3183 | void Element::synchronizeStyleAttributeInternal() const |
| 3184 | { |
| 3185 | ASSERT(isStyledElement()); |
| 3186 | ASSERT(elementData()); |
| 3187 | ASSERT(elementData()->m_styleAttributeIsDirty); |
| 3188 | elementData()->m_styleAttributeIsDirty = false; |
| 3189 | if (const StylePropertySet* inlineStyle = this->inlineStyle()) |
| 3190 | const_cast<Element*>(this)->setSynchronizedLazyAttribute(styleAttr, inlineStyle->asText()); |
| 3191 | } |
| 3192 | |
| 3193 | CSSStyleDeclaration* Element::style() |
| 3194 | { |
| 3195 | if (!isStyledElement()) |
| 3196 | return 0; |
| 3197 | return ensureMutableInlineStyle()->ensureInlineCSSStyleDeclaration(this); |
| 3198 | } |
| 3199 | |
| 3200 | MutableStylePropertySet* Element::ensureMutableInlineStyle() |
| 3201 | { |
| 3202 | ASSERT(isStyledElement()); |
| 3203 | RefPtr<StylePropertySet>& inlineStyle = ensureUniqueElementData()->m_inlineStyle; |
| 3204 | if (!inlineStyle) |
| 3205 | inlineStyle = MutableStylePropertySet::create(strictToCSSParserMode(isHTMLElement() && !document()->inQuirksMode())); |
| 3206 | else if (!inlineStyle->isMutable()) |
| 3207 | inlineStyle = inlineStyle->mutableCopy(); |
| 3208 | ASSERT(inlineStyle->isMutable()); |
| 3209 | return static_cast<MutableStylePropertySet*>(inlineStyle.get()); |
| 3210 | } |
| 3211 | |
| 3212 | PropertySetCSSStyleDeclaration* Element::inlineStyleCSSOMWrapper() |
| 3213 | { |
| 3214 | if (!inlineStyle() || !inlineStyle()->hasCSSOMWrapper()) |
| 3215 | return 0; |
| 3216 | PropertySetCSSStyleDeclaration* cssomWrapper = ensureMutableInlineStyle()->cssStyleDeclaration(); |
| 3217 | ASSERT(cssomWrapper && cssomWrapper->parentElement() == this); |
| 3218 | return cssomWrapper; |
| 3219 | } |
| 3220 | |
| 3221 | inline void Element::setInlineStyleFromString(const AtomicString& newStyleString) |
| 3222 | { |
| 3223 | ASSERT(isStyledElement()); |
| 3224 | RefPtr<StylePropertySet>& inlineStyle = elementData()->m_inlineStyle; |
| 3225 | |
| 3226 | // Avoid redundant work if we're using shared attribute data with already parsed inline style. |
| 3227 | if (inlineStyle && !elementData()->isUnique()) |
| 3228 | return; |
| 3229 | |
| 3230 | // We reconstruct the property set instead of mutating if there is no CSSOM wrapper. |
| 3231 | // This makes wrapperless property sets immutable and so cacheable. |
| 3232 | if (inlineStyle && !inlineStyle->isMutable()) |
| 3233 | inlineStyle.clear(); |
| 3234 | |
| 3235 | if (!inlineStyle) { |
| 3236 | inlineStyle = CSSParser::parseInlineStyleDeclaration(newStyleString, this); |
| 3237 | } else { |
| 3238 | ASSERT(inlineStyle->isMutable()); |
| 3239 | static_pointer_cast<MutableStylePropertySet>(inlineStyle)->parseDeclaration(newStyleString, document()->elementSheet()->contents()); |
| 3240 | } |
| 3241 | } |
| 3242 | |
| 3243 | void Element::styleAttributeChanged(const AtomicString& newStyleString, AttributeModificationReason modificationReason) |
| 3244 | { |
| 3245 | ASSERT(isStyledElement()); |
| 3246 | WTF::OrdinalNumber startLineNumber = WTF::OrdinalNumber::beforeFirst(); |
| 3247 | if (document() && document()->scriptableDocumentParser() && !document()->isInDocumentWrite()) |
| 3248 | startLineNumber = document()->scriptableDocumentParser()->lineNumber(); |
| 3249 | |
| 3250 | if (newStyleString.isNull()) { |
| 3251 | if (PropertySetCSSStyleDeclaration* cssomWrapper = inlineStyleCSSOMWrapper()) |
| 3252 | cssomWrapper->clearParentElement(); |
| 3253 | ensureUniqueElementData()->m_inlineStyle.clear(); |
| 3254 | } else if (modificationReason == ModifiedByCloning || document()->contentSecurityPolicy()->allowInlineStyle(document()->url(), startLineNumber)) { |
| 3255 | setInlineStyleFromString(newStyleString); |
| 3256 | } |
| 3257 | |
| 3258 | elementData()->m_styleAttributeIsDirty = false; |
| 3259 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 3260 | setNeedsStyleRecalc(LocalStyleChange); |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 3261 | InspectorInstrumentation::didInvalidateStyleAttr(document(), this); |
| 3262 | } |
| 3263 | |
| 3264 | void Element::inlineStyleChanged() |
| 3265 | { |
| 3266 | ASSERT(isStyledElement()); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 3267 | setNeedsStyleRecalc(LocalStyleChange); |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 3268 | ASSERT(elementData()); |
| 3269 | elementData()->m_styleAttributeIsDirty = true; |
| 3270 | InspectorInstrumentation::didInvalidateStyleAttr(document(), this); |
| 3271 | } |
| 3272 | |
| 3273 | bool Element::setInlineStyleProperty(CSSPropertyID propertyID, CSSValueID identifier, bool important) |
| 3274 | { |
| 3275 | ASSERT(isStyledElement()); |
| 3276 | ensureMutableInlineStyle()->setProperty(propertyID, cssValuePool().createIdentifierValue(identifier), important); |
| 3277 | inlineStyleChanged(); |
| 3278 | return true; |
| 3279 | } |
| 3280 | |
| 3281 | bool Element::setInlineStyleProperty(CSSPropertyID propertyID, CSSPropertyID identifier, bool important) |
| 3282 | { |
| 3283 | ASSERT(isStyledElement()); |
| 3284 | ensureMutableInlineStyle()->setProperty(propertyID, cssValuePool().createIdentifierValue(identifier), important); |
| 3285 | inlineStyleChanged(); |
| 3286 | return true; |
| 3287 | } |
| 3288 | |
| 3289 | bool Element::setInlineStyleProperty(CSSPropertyID propertyID, double value, CSSPrimitiveValue::UnitTypes unit, bool important) |
| 3290 | { |
| 3291 | ASSERT(isStyledElement()); |
| 3292 | ensureMutableInlineStyle()->setProperty(propertyID, cssValuePool().createValue(value, unit), important); |
| 3293 | inlineStyleChanged(); |
| 3294 | return true; |
| 3295 | } |
| 3296 | |
| 3297 | bool Element::setInlineStyleProperty(CSSPropertyID propertyID, const String& value, bool important) |
| 3298 | { |
| 3299 | ASSERT(isStyledElement()); |
| 3300 | bool changes = ensureMutableInlineStyle()->setProperty(propertyID, value, important, document()->elementSheet()->contents()); |
| 3301 | if (changes) |
| 3302 | inlineStyleChanged(); |
| 3303 | return changes; |
| 3304 | } |
| 3305 | |
| 3306 | bool Element::removeInlineStyleProperty(CSSPropertyID propertyID) |
| 3307 | { |
| 3308 | ASSERT(isStyledElement()); |
| 3309 | if (!inlineStyle()) |
| 3310 | return false; |
| 3311 | bool changes = ensureMutableInlineStyle()->removeProperty(propertyID); |
| 3312 | if (changes) |
| 3313 | inlineStyleChanged(); |
| 3314 | return changes; |
| 3315 | } |
| 3316 | |
| 3317 | void Element::removeAllInlineStyleProperties() |
| 3318 | { |
| 3319 | ASSERT(isStyledElement()); |
| 3320 | if (!inlineStyle() || inlineStyle()->isEmpty()) |
| 3321 | return; |
| 3322 | ensureMutableInlineStyle()->clear(); |
| 3323 | inlineStyleChanged(); |
| 3324 | } |
| 3325 | |
| 3326 | void Element::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const |
| 3327 | { |
| 3328 | ASSERT(isStyledElement()); |
| 3329 | if (const StylePropertySet* inlineStyle = elementData() ? elementData()->inlineStyle() : 0) |
| 3330 | inlineStyle->addSubresourceStyleURLs(urls, document()->elementSheet()->contents()); |
| 3331 | } |
| 3332 | |
Ben Murdoch | 8375017 | 2013-07-24 10:36:59 +0100 | [diff] [blame] | 3333 | static inline bool attributeNameSort(const pair<StringImpl*, AtomicString>& p1, const pair<StringImpl*, AtomicString>& p2) |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 3334 | { |
| 3335 | // Sort based on the attribute name pointers. It doesn't matter what the order is as long as it is always the same. |
| 3336 | return p1.first < p2.first; |
| 3337 | } |
| 3338 | |
| 3339 | void Element::makePresentationAttributeCacheKey(PresentationAttributeCacheKey& result) const |
| 3340 | { |
| 3341 | ASSERT(isStyledElement()); |
| 3342 | // FIXME: Enable for SVG. |
| 3343 | if (namespaceURI() != xhtmlNamespaceURI) |
| 3344 | return; |
| 3345 | // Interpretation of the size attributes on <input> depends on the type attribute. |
| 3346 | if (hasTagName(inputTag)) |
| 3347 | return; |
| 3348 | unsigned size = attributeCount(); |
| 3349 | for (unsigned i = 0; i < size; ++i) { |
| 3350 | const Attribute* attribute = attributeItem(i); |
| 3351 | if (!isPresentationAttribute(attribute->name())) |
| 3352 | continue; |
| 3353 | if (!attribute->namespaceURI().isNull()) |
| 3354 | return; |
| 3355 | // FIXME: Background URL may depend on the base URL and can't be shared. Disallow caching. |
| 3356 | if (attribute->name() == backgroundAttr) |
| 3357 | return; |
| 3358 | result.attributesAndValues.append(std::make_pair(attribute->localName().impl(), attribute->value())); |
| 3359 | } |
| 3360 | if (result.attributesAndValues.isEmpty()) |
| 3361 | return; |
| 3362 | // Attribute order doesn't matter. Sort for easy equality comparison. |
| 3363 | std::sort(result.attributesAndValues.begin(), result.attributesAndValues.end(), attributeNameSort); |
| 3364 | // The cache key is non-null when the tagName is set. |
| 3365 | result.tagName = localName().impl(); |
| 3366 | } |
| 3367 | |
| 3368 | static unsigned computePresentationAttributeCacheHash(const PresentationAttributeCacheKey& key) |
| 3369 | { |
| 3370 | if (!key.tagName) |
| 3371 | return 0; |
| 3372 | ASSERT(key.attributesAndValues.size()); |
| 3373 | unsigned attributeHash = StringHasher::hashMemory(key.attributesAndValues.data(), key.attributesAndValues.size() * sizeof(key.attributesAndValues[0])); |
| 3374 | return WTF::pairIntHash(key.tagName->existingHash(), attributeHash); |
| 3375 | } |
| 3376 | |
| 3377 | void Element::rebuildPresentationAttributeStyle() |
| 3378 | { |
| 3379 | ASSERT(isStyledElement()); |
| 3380 | PresentationAttributeCacheKey cacheKey; |
| 3381 | makePresentationAttributeCacheKey(cacheKey); |
| 3382 | |
| 3383 | unsigned cacheHash = computePresentationAttributeCacheHash(cacheKey); |
| 3384 | |
| 3385 | PresentationAttributeCache::iterator cacheIterator; |
| 3386 | if (cacheHash) { |
| 3387 | cacheIterator = presentationAttributeCache().add(cacheHash, nullptr).iterator; |
| 3388 | if (cacheIterator->value && cacheIterator->value->key != cacheKey) |
| 3389 | cacheHash = 0; |
| 3390 | } else { |
| 3391 | cacheIterator = presentationAttributeCache().end(); |
| 3392 | } |
| 3393 | |
| 3394 | RefPtr<StylePropertySet> style; |
| 3395 | if (cacheHash && cacheIterator->value) { |
| 3396 | style = cacheIterator->value->value; |
| 3397 | presentationAttributeCacheCleaner().didHitPresentationAttributeCache(); |
| 3398 | } else { |
| 3399 | style = MutableStylePropertySet::create(isSVGElement() ? SVGAttributeMode : CSSQuirksMode); |
| 3400 | unsigned size = attributeCount(); |
| 3401 | for (unsigned i = 0; i < size; ++i) { |
| 3402 | const Attribute* attribute = attributeItem(i); |
| 3403 | collectStyleForPresentationAttribute(attribute->name(), attribute->value(), static_cast<MutableStylePropertySet*>(style.get())); |
| 3404 | } |
| 3405 | } |
| 3406 | |
| 3407 | // ShareableElementData doesn't store presentation attribute style, so make sure we have a UniqueElementData. |
| 3408 | UniqueElementData* elementData = ensureUniqueElementData(); |
| 3409 | |
| 3410 | elementData->m_presentationAttributeStyleIsDirty = false; |
| 3411 | elementData->m_presentationAttributeStyle = style->isEmpty() ? 0 : style; |
| 3412 | |
| 3413 | if (!cacheHash || cacheIterator->value) |
| 3414 | return; |
| 3415 | |
| 3416 | OwnPtr<PresentationAttributeCacheEntry> newEntry = adoptPtr(new PresentationAttributeCacheEntry); |
| 3417 | newEntry->key = cacheKey; |
| 3418 | newEntry->value = style.release(); |
| 3419 | |
| 3420 | static const int presentationAttributeCacheMaximumSize = 4096; |
| 3421 | if (presentationAttributeCache().size() > presentationAttributeCacheMaximumSize) { |
| 3422 | // Start building from scratch if the cache ever gets big. |
| 3423 | presentationAttributeCache().clear(); |
| 3424 | presentationAttributeCache().set(cacheHash, newEntry.release()); |
| 3425 | } else { |
| 3426 | cacheIterator->value = newEntry.release(); |
| 3427 | } |
| 3428 | } |
| 3429 | |
| 3430 | void Element::addPropertyToPresentationAttributeStyle(MutableStylePropertySet* style, CSSPropertyID propertyID, CSSValueID identifier) |
| 3431 | { |
| 3432 | ASSERT(isStyledElement()); |
| 3433 | style->setProperty(propertyID, cssValuePool().createIdentifierValue(identifier)); |
| 3434 | } |
| 3435 | |
| 3436 | void Element::addPropertyToPresentationAttributeStyle(MutableStylePropertySet* style, CSSPropertyID propertyID, double value, CSSPrimitiveValue::UnitTypes unit) |
| 3437 | { |
| 3438 | ASSERT(isStyledElement()); |
| 3439 | style->setProperty(propertyID, cssValuePool().createValue(value, unit)); |
| 3440 | } |
| 3441 | |
| 3442 | void Element::addPropertyToPresentationAttributeStyle(MutableStylePropertySet* style, CSSPropertyID propertyID, const String& value) |
| 3443 | { |
| 3444 | ASSERT(isStyledElement()); |
| 3445 | style->setProperty(propertyID, value, false, document()->elementSheet()->contents()); |
| 3446 | } |
| 3447 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 3448 | void ElementData::deref() |
| 3449 | { |
| 3450 | if (!derefBase()) |
| 3451 | return; |
| 3452 | |
| 3453 | if (m_isUnique) |
| 3454 | delete static_cast<UniqueElementData*>(this); |
| 3455 | else |
| 3456 | delete static_cast<ShareableElementData*>(this); |
| 3457 | } |
| 3458 | |
| 3459 | ElementData::ElementData() |
| 3460 | : m_isUnique(true) |
| 3461 | , m_arraySize(0) |
| 3462 | , m_presentationAttributeStyleIsDirty(false) |
| 3463 | , m_styleAttributeIsDirty(false) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 3464 | , m_animatedSVGAttributesAreDirty(false) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 3465 | { |
| 3466 | } |
| 3467 | |
| 3468 | ElementData::ElementData(unsigned arraySize) |
| 3469 | : m_isUnique(false) |
| 3470 | , m_arraySize(arraySize) |
| 3471 | , m_presentationAttributeStyleIsDirty(false) |
| 3472 | , m_styleAttributeIsDirty(false) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 3473 | , m_animatedSVGAttributesAreDirty(false) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 3474 | { |
| 3475 | } |
| 3476 | |
| 3477 | struct SameSizeAsElementData : public RefCounted<SameSizeAsElementData> { |
| 3478 | unsigned bitfield; |
| 3479 | void* refPtrs[3]; |
| 3480 | }; |
| 3481 | |
| 3482 | COMPILE_ASSERT(sizeof(ElementData) == sizeof(SameSizeAsElementData), element_attribute_data_should_stay_small); |
| 3483 | |
| 3484 | static size_t sizeForShareableElementDataWithAttributeCount(unsigned count) |
| 3485 | { |
| 3486 | return sizeof(ShareableElementData) + sizeof(Attribute) * count; |
| 3487 | } |
| 3488 | |
| 3489 | PassRefPtr<ShareableElementData> ShareableElementData::createWithAttributes(const Vector<Attribute>& attributes) |
| 3490 | { |
| 3491 | void* slot = WTF::fastMalloc(sizeForShareableElementDataWithAttributeCount(attributes.size())); |
| 3492 | return adoptRef(new (slot) ShareableElementData(attributes)); |
| 3493 | } |
| 3494 | |
| 3495 | PassRefPtr<UniqueElementData> UniqueElementData::create() |
| 3496 | { |
| 3497 | return adoptRef(new UniqueElementData); |
| 3498 | } |
| 3499 | |
| 3500 | ShareableElementData::ShareableElementData(const Vector<Attribute>& attributes) |
| 3501 | : ElementData(attributes.size()) |
| 3502 | { |
| 3503 | for (unsigned i = 0; i < m_arraySize; ++i) |
| 3504 | new (&m_attributeArray[i]) Attribute(attributes[i]); |
| 3505 | } |
| 3506 | |
| 3507 | ShareableElementData::~ShareableElementData() |
| 3508 | { |
| 3509 | for (unsigned i = 0; i < m_arraySize; ++i) |
| 3510 | m_attributeArray[i].~Attribute(); |
| 3511 | } |
| 3512 | |
| 3513 | ShareableElementData::ShareableElementData(const UniqueElementData& other) |
| 3514 | : ElementData(other, false) |
| 3515 | { |
| 3516 | ASSERT(!other.m_presentationAttributeStyle); |
| 3517 | |
| 3518 | if (other.m_inlineStyle) { |
| 3519 | ASSERT(!other.m_inlineStyle->hasCSSOMWrapper()); |
| 3520 | m_inlineStyle = other.m_inlineStyle->immutableCopyIfNeeded(); |
| 3521 | } |
| 3522 | |
| 3523 | for (unsigned i = 0; i < m_arraySize; ++i) |
| 3524 | new (&m_attributeArray[i]) Attribute(other.m_attributeVector.at(i)); |
| 3525 | } |
| 3526 | |
| 3527 | ElementData::ElementData(const ElementData& other, bool isUnique) |
| 3528 | : m_isUnique(isUnique) |
| 3529 | , m_arraySize(isUnique ? 0 : other.length()) |
| 3530 | , m_presentationAttributeStyleIsDirty(other.m_presentationAttributeStyleIsDirty) |
| 3531 | , m_styleAttributeIsDirty(other.m_styleAttributeIsDirty) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 3532 | , m_animatedSVGAttributesAreDirty(other.m_animatedSVGAttributesAreDirty) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 3533 | , m_classNames(other.m_classNames) |
| 3534 | , m_idForStyleResolution(other.m_idForStyleResolution) |
| 3535 | { |
| 3536 | // NOTE: The inline style is copied by the subclass copy constructor since we don't know what to do with it here. |
| 3537 | } |
| 3538 | |
| 3539 | UniqueElementData::UniqueElementData() |
| 3540 | { |
| 3541 | } |
| 3542 | |
| 3543 | UniqueElementData::UniqueElementData(const UniqueElementData& other) |
| 3544 | : ElementData(other, true) |
| 3545 | , m_presentationAttributeStyle(other.m_presentationAttributeStyle) |
| 3546 | , m_attributeVector(other.m_attributeVector) |
| 3547 | { |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 3548 | m_inlineStyle = other.m_inlineStyle ? other.m_inlineStyle->mutableCopy() : 0; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | UniqueElementData::UniqueElementData(const ShareableElementData& other) |
| 3552 | : ElementData(other, true) |
| 3553 | { |
| 3554 | // An ShareableElementData should never have a mutable inline StylePropertySet attached. |
| 3555 | ASSERT(!other.m_inlineStyle || !other.m_inlineStyle->isMutable()); |
| 3556 | m_inlineStyle = other.m_inlineStyle; |
| 3557 | |
| 3558 | m_attributeVector.reserveCapacity(other.length()); |
| 3559 | for (unsigned i = 0; i < other.length(); ++i) |
| 3560 | m_attributeVector.uncheckedAppend(other.m_attributeArray[i]); |
| 3561 | } |
| 3562 | |
| 3563 | PassRefPtr<UniqueElementData> ElementData::makeUniqueCopy() const |
| 3564 | { |
| 3565 | if (isUnique()) |
| 3566 | return adoptRef(new UniqueElementData(static_cast<const UniqueElementData&>(*this))); |
| 3567 | return adoptRef(new UniqueElementData(static_cast<const ShareableElementData&>(*this))); |
| 3568 | } |
| 3569 | |
| 3570 | PassRefPtr<ShareableElementData> UniqueElementData::makeShareableCopy() const |
| 3571 | { |
| 3572 | void* slot = WTF::fastMalloc(sizeForShareableElementDataWithAttributeCount(m_attributeVector.size())); |
| 3573 | return adoptRef(new (slot) ShareableElementData(*this)); |
| 3574 | } |
| 3575 | |
| 3576 | void UniqueElementData::addAttribute(const QualifiedName& attributeName, const AtomicString& value) |
| 3577 | { |
| 3578 | m_attributeVector.append(Attribute(attributeName, value)); |
| 3579 | } |
| 3580 | |
| 3581 | void UniqueElementData::removeAttribute(size_t index) |
| 3582 | { |
| 3583 | ASSERT_WITH_SECURITY_IMPLICATION(index < length()); |
| 3584 | m_attributeVector.remove(index); |
| 3585 | } |
| 3586 | |
| 3587 | bool ElementData::isEquivalent(const ElementData* other) const |
| 3588 | { |
| 3589 | if (!other) |
| 3590 | return isEmpty(); |
| 3591 | |
| 3592 | unsigned len = length(); |
| 3593 | if (len != other->length()) |
| 3594 | return false; |
| 3595 | |
| 3596 | for (unsigned i = 0; i < len; i++) { |
| 3597 | const Attribute* attribute = attributeItem(i); |
| 3598 | const Attribute* otherAttr = other->getAttributeItem(attribute->name()); |
| 3599 | if (!otherAttr || attribute->value() != otherAttr->value()) |
| 3600 | return false; |
| 3601 | } |
| 3602 | |
| 3603 | return true; |
| 3604 | } |
| 3605 | |
Torne (Richard Coles) | e524955 | 2013-05-15 11:35:13 +0100 | [diff] [blame] | 3606 | size_t ElementData::getAttrIndex(Attr* attr) const |
| 3607 | { |
| 3608 | // This relies on the fact that Attr's QualifiedName == the Attribute's name. |
| 3609 | for (unsigned i = 0; i < length(); ++i) { |
| 3610 | if (attributeItem(i)->name() == attr->qualifiedName()) |
| 3611 | return i; |
| 3612 | } |
| 3613 | return notFound; |
| 3614 | } |
| 3615 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 3616 | size_t ElementData::getAttributeItemIndexSlowCase(const AtomicString& name, bool shouldIgnoreAttributeCase) const |
| 3617 | { |
| 3618 | // Continue to checking case-insensitively and/or full namespaced names if necessary: |
| 3619 | for (unsigned i = 0; i < length(); ++i) { |
| 3620 | const Attribute* attribute = attributeItem(i); |
| 3621 | if (!attribute->name().hasPrefix()) { |
| 3622 | if (shouldIgnoreAttributeCase && equalIgnoringCase(name, attribute->localName())) |
| 3623 | return i; |
| 3624 | } else { |
| 3625 | // FIXME: Would be faster to do this comparison without calling toString, which |
| 3626 | // generates a temporary string by concatenation. But this branch is only reached |
| 3627 | // if the attribute name has a prefix, which is rare in HTML. |
| 3628 | if (equalPossiblyIgnoringCase(name, attribute->name().toString(), shouldIgnoreAttributeCase)) |
| 3629 | return i; |
| 3630 | } |
| 3631 | } |
| 3632 | return notFound; |
| 3633 | } |
| 3634 | |
| 3635 | Attribute* UniqueElementData::getAttributeItem(const QualifiedName& name) |
| 3636 | { |
| 3637 | for (unsigned i = 0; i < length(); ++i) { |
| 3638 | if (m_attributeVector.at(i).name().matches(name)) |
| 3639 | return &m_attributeVector.at(i); |
| 3640 | } |
| 3641 | return 0; |
| 3642 | } |
| 3643 | |
| 3644 | Attribute* UniqueElementData::attributeItem(unsigned index) |
| 3645 | { |
| 3646 | ASSERT_WITH_SECURITY_IMPLICATION(index < length()); |
| 3647 | return &m_attributeVector.at(index); |
| 3648 | } |
| 3649 | |
| 3650 | } // namespace WebCore |