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