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