blob: 46a60530d0b130782ebda7e27959f8d543530c1a [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
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 Murdoch591b9582013-07-10 11:41:44 +010029#include "CSSValueKeywords.h"
Ben Murdoch7757ec22013-07-23 11:17:36 +010030#include "RuntimeEnabledFeatures.h"
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010031#include "SVGNames.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010032#include "XMLNames.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +000033#include "bindings/v8/Dictionary.h"
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000034#include "bindings/v8/ExceptionMessages.h"
Ben Murdochdf957042013-08-06 11:01:27 +010035#include "bindings/v8/ExceptionState.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010036#include "core/accessibility/AXObjectCache.h"
Ben Murdoch83750172013-07-24 10:36:59 +010037#include "core/animation/DocumentTimeline.h"
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +010038#include "core/animation/css/CSSAnimations.h"
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000039#include "core/css/CSSImageValue.h"
Ben Murdoch591b9582013-07-10 11:41:44 +010040#include "core/css/CSSStyleSheet.h"
41#include "core/css/CSSValuePool.h"
42#include "core/css/PropertySetCSSStyleDeclaration.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010043#include "core/css/StylePropertySet.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +000044#include "core/css/parser/BisonCSSParser.h"
Torne (Richard Coles)81a51572013-05-13 16:52:28 +010045#include "core/css/resolver/StyleResolver.h"
Ben Murdoch07a852d2014-03-31 11:51:52 +010046#include "core/css/resolver/StyleResolverParentScope.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010047#include "core/dom/Attr.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010048#include "core/dom/CSSSelectorWatch.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010049#include "core/dom/ClientRect.h"
50#include "core/dom/ClientRectList.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010051#include "core/dom/DatasetDOMStringMap.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +000052#include "core/dom/ElementDataCache.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010053#include "core/dom/ElementRareData.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +000054#include "core/dom/ElementTraversal.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010055#include "core/dom/ExceptionCode.h"
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +010056#include "core/dom/FullscreenElementStack.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010057#include "core/dom/MutationObserverInterestGroup.h"
58#include "core/dom/MutationRecord.h"
59#include "core/dom/NamedNodeMap.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010060#include "core/dom/NodeRenderStyle.h"
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +010061#include "core/dom/PresentationAttributeStyle.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010062#include "core/dom/PseudoElement.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +000063#include "core/dom/RenderTreeBuilder.h"
Ben Murdoch591b9582013-07-10 11:41:44 +010064#include "core/dom/ScriptableDocumentParser.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010065#include "core/dom/SelectorQuery.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010066#include "core/dom/Text.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010067#include "core/dom/custom/CustomElement.h"
68#include "core/dom/custom/CustomElementRegistrationContext.h"
Torne (Richard Coles)e5249552013-05-15 11:35:13 +010069#include "core/dom/shadow/InsertionPoint.h"
70#include "core/dom/shadow/ShadowRoot.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010071#include "core/editing/FrameSelection.h"
72#include "core/editing/TextIterator.h"
73#include "core/editing/htmlediting.h"
Torne (Richard Coles)19cde672013-11-06 12:28:04 +000074#include "core/editing/markup.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010075#include "core/events/EventDispatcher.h"
76#include "core/events/FocusEvent.h"
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +010077#include "core/frame/FrameView.h"
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000078#include "core/frame/LocalFrame.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +000079#include "core/frame/UseCounter.h"
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000080#include "core/frame/csp/ContentSecurityPolicy.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010081#include "core/html/ClassList.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010082#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)43e75022014-03-21 14:26:12 +000086#include "core/html/HTMLFrameElementBase.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010087#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)19cde672013-11-06 12:28:04 +000091#include "core/html/HTMLTemplateElement.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010092#include "core/html/parser/HTMLParserIdioms.h"
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000093#include "core/inspector/InspectorInstrumentation.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010094#include "core/page/FocusController.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010095#include "core/page/Page.h"
96#include "core/page/PointerLockController.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +000097#include "core/rendering/RenderLayer.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010098#include "core/rendering/RenderView.h"
99#include "core/rendering/RenderWidget.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100100#include "core/svg/SVGDocumentExtensions.h"
101#include "core/svg/SVGElement.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000102#include "platform/scroll/ScrollableArea.h"
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100103#include "wtf/BitVector.h"
Ben Murdoch591b9582013-07-10 11:41:44 +0100104#include "wtf/HashFunctions.h"
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100105#include "wtf/text/CString.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000106#include "wtf/text/StringBuilder.h"
Ben Murdoch591b9582013-07-10 11:41:44 +0100107#include "wtf/text/TextPosition.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100108
109namespace WebCore {
110
111using namespace HTMLNames;
112using namespace XMLNames;
113
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100114typedef Vector<RefPtr<Attr> > AttrNodeList;
115typedef HashMap<Element*, OwnPtr<AttrNodeList> > AttrNodeListMap;
116
117static AttrNodeListMap& attrNodeListMap()
118{
119 DEFINE_STATIC_LOCAL(AttrNodeListMap, map, ());
120 return map;
121}
122
123static 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)bfe35902013-10-22 16:41:51 +0100131static AttrNodeList& ensureAttrNodeListForElement(Element* element)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100132{
133 if (element->hasSyntheticAttrChildNodes()) {
134 ASSERT(attrNodeListMap().contains(element));
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +0100135 return *attrNodeListMap().get(element);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100136 }
137 ASSERT(!attrNodeListMap().contains(element));
138 element->setHasSyntheticAttrChildNodes(true);
139 AttrNodeListMap::AddResult result = attrNodeListMap().add(element, adoptPtr(new AttrNodeList));
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000140 return *result.storedValue->value;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100141}
142
143static 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)51b29062013-11-28 11:56:03 +0000151static Attr* findAttrNodeInList(const AttrNodeList& attrNodeList, const QualifiedName& name)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100152{
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000153 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)53e740f2013-05-09 18:38:43 +0100157 }
158 return 0;
159}
160
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100161PassRefPtr<Element> Element::create(const QualifiedName& tagName, Document* document)
162{
163 return adoptRef(new Element(tagName, document, CreateElement));
164}
165
166Element::~Element()
167{
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000168 ASSERT(needsAttach());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100169
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000170 if (hasRareData())
171 elementRareData()->clearShadow();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100172
Ben Murdoch83750172013-07-24 10:36:59 +0100173 if (isCustomElement())
174 CustomElement::wasDestroyed(this);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100175
176 if (hasSyntheticAttrChildNodes())
177 detachAllAttrNodesFromElement();
178
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100179 if (hasPendingResources()) {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000180 document().accessSVGExtensions().removeElementFromPendingResources(this);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100181 ASSERT(!hasPendingResources());
182 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100183}
184
185inline ElementRareData* Element::elementRareData() const
186{
187 ASSERT(hasRareData());
188 return static_cast<ElementRareData*>(rareData());
189}
190
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +0100191inline ElementRareData& Element::ensureElementRareData()
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100192{
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +0100193 return static_cast<ElementRareData&>(ensureRareData());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100194}
195
Torne (Richard Coles)43e75022014-03-21 14:26:12 +0000196bool Element::hasElementFlagInternal(ElementFlags mask) const
197{
Ben Murdoch07a852d2014-03-31 11:51:52 +0100198 return elementRareData()->hasElementFlag(mask);
Torne (Richard Coles)43e75022014-03-21 14:26:12 +0000199}
200
201void Element::setElementFlag(ElementFlags mask, bool value)
202{
203 if (!hasRareData() && !value)
204 return;
Ben Murdoch07a852d2014-03-31 11:51:52 +0100205 ensureElementRareData().setElementFlag(mask, value);
Torne (Richard Coles)43e75022014-03-21 14:26:12 +0000206}
207
208void Element::clearElementFlag(ElementFlags mask)
209{
210 if (!hasRareData())
211 return;
Ben Murdoch07a852d2014-03-31 11:51:52 +0100212 elementRareData()->clearElementFlag(mask);
Torne (Richard Coles)43e75022014-03-21 14:26:12 +0000213}
214
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100215void Element::clearTabIndexExplicitlyIfNeeded()
216{
217 if (hasRareData())
218 elementRareData()->clearTabIndexExplicitly();
219}
220
221void Element::setTabIndexExplicitly(short tabIndex)
222{
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +0100223 ensureElementRareData().setTabIndexExplicitly(tabIndex);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100224}
225
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100226short Element::tabIndex() const
227{
228 return hasRareData() ? elementRareData()->tabIndex() : 0;
229}
230
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100231bool 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 Murdoche69819b2013-07-17 14:56:49 +0100244 // callsites like Document::setFocusedElement that would currently fail on
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100245 // them. See crbug.com/251163
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000246 if (!renderer()) {
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100247 // 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)8abfc582013-09-12 12:10:38 +0100250 ASSERT(!document().childNeedsStyleRecalc());
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100251 }
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)53e740f2013-05-09 18:38:43 +0100261PassRefPtr<Node> Element::cloneNode(bool deep)
262{
263 return deep ? cloneElementWithChildren() : cloneElementWithoutChildren();
264}
265
266PassRefPtr<Element> Element::cloneElementWithChildren()
267{
268 RefPtr<Element> clone = cloneElementWithoutChildren();
269 cloneChildNodes(clone.get());
270 return clone.release();
271}
272
273PassRefPtr<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
284PassRefPtr<Element> Element::cloneElementWithoutAttributesAndChildren()
285{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100286 return document().createElement(tagQName(), false);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100287}
288
289PassRefPtr<Attr> Element::detachAttribute(size_t index)
290{
291 ASSERT(elementData());
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000292 const Attribute& attribute = elementData()->attributeItem(index);
293 RefPtr<Attr> attrNode = attrIfExists(attribute.name());
Torne (Richard Coles)e5249552013-05-15 11:35:13 +0100294 if (attrNode)
295 detachAttrNodeAtIndex(attrNode.get(), index);
296 else {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000297 attrNode = Attr::create(document(), attribute.name(), attribute.value());
Torne (Richard Coles)e5249552013-05-15 11:35:13 +0100298 removeAttributeInternal(index, NotInSynchronizationOfLazyAttribute);
299 }
300 return attrNode.release();
301}
302
303void Element::detachAttrNodeAtIndex(Attr* attr, size_t index)
304{
305 ASSERT(attr);
306 ASSERT(elementData());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100307
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000308 const Attribute& attribute = elementData()->attributeItem(index);
309 ASSERT(attribute.name() == attr->qualifiedName());
310 detachAttrNodeFromElementWithValue(attr, attribute.value());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100311 removeAttributeInternal(index, NotInSynchronizationOfLazyAttribute);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100312}
313
314void Element::removeAttribute(const QualifiedName& name)
315{
316 if (!elementData())
317 return;
318
319 size_t index = elementData()->getAttributeItemIndex(name);
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +0100320 if (index == kNotFound)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100321 return;
322
323 removeAttributeInternal(index, NotInSynchronizationOfLazyAttribute);
324}
325
326void Element::setBooleanAttribute(const QualifiedName& name, bool value)
327{
328 if (value)
329 setAttribute(name, emptyAtom);
330 else
331 removeAttribute(name);
332}
333
334NamedNodeMap* Element::attributes() const
335{
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +0100336 ElementRareData& rareData = const_cast<Element*>(this)->ensureElementRareData();
337 if (NamedNodeMap* attributeMap = rareData.attributeMap())
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100338 return attributeMap;
339
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +0100340 rareData.setAttributeMap(NamedNodeMap::create(const_cast<Element*>(this)));
341 return rareData.attributeMap();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100342}
343
Ben Murdoch83750172013-07-24 10:36:59 +0100344ActiveAnimations* Element::activeAnimations() const
Torne (Richard Coles)81a51572013-05-13 16:52:28 +0100345{
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000346 if (hasRareData())
Ben Murdoch83750172013-07-24 10:36:59 +0100347 return elementRareData()->activeAnimations();
348 return 0;
Torne (Richard Coles)81a51572013-05-13 16:52:28 +0100349}
350
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000351ActiveAnimations& Element::ensureActiveAnimations()
Torne (Richard Coles)81a51572013-05-13 16:52:28 +0100352{
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +0100353 ElementRareData& rareData = ensureElementRareData();
354 if (!rareData.activeAnimations())
Ben Murdoch07a852d2014-03-31 11:51:52 +0100355 rareData.setActiveAnimations(adoptPtrWillBeNoop(new ActiveAnimations()));
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000356 return *rareData.activeAnimations();
Torne (Richard Coles)81a51572013-05-13 16:52:28 +0100357}
358
359bool Element::hasActiveAnimations() const
360{
Ben Murdoch83750172013-07-24 10:36:59 +0100361 if (!hasRareData())
362 return false;
363
364 ActiveAnimations* activeAnimations = elementRareData()->activeAnimations();
365 return activeAnimations && !activeAnimations->isEmpty();
Torne (Richard Coles)81a51572013-05-13 16:52:28 +0100366}
367
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100368Node::NodeType Element::nodeType() const
369{
370 return ELEMENT_NODE;
371}
372
373bool Element::hasAttribute(const QualifiedName& name) const
374{
375 return hasAttributeNS(name.namespaceURI(), name.localName());
376}
377
378void Element::synchronizeAllAttributes() const
379{
380 if (!elementData())
381 return;
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000382 // 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)53e740f2013-05-09 18:38:43 +0100385 if (elementData()->m_styleAttributeIsDirty) {
386 ASSERT(isStyledElement());
Ben Murdoch591b9582013-07-10 11:41:44 +0100387 synchronizeStyleAttributeInternal();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100388 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100389 if (elementData()->m_animatedSVGAttributesAreDirty) {
390 ASSERT(isSVGElement());
391 toSVGElement(this)->synchronizeAnimatedSVGAttribute(anyQName());
392 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100393}
394
395inline 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 Murdoch591b9582013-07-10 11:41:44 +0100401 synchronizeStyleAttributeInternal();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100402 return;
403 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100404 if (UNLIKELY(elementData()->m_animatedSVGAttributesAreDirty)) {
405 ASSERT(isSVGElement());
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000406 // See comment in the AtomicString version of synchronizeAttribute()
407 // also.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100408 toSVGElement(this)->synchronizeAnimatedSVGAttribute(name);
409 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100410}
411
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000412void Element::synchronizeAttribute(const AtomicString& localName) const
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100413{
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)bfe35902013-10-22 16:41:51 +0100418 if (elementData()->m_styleAttributeIsDirty && equalPossiblyIgnoringCase(localName, styleAttr.localName(), shouldIgnoreAttributeCase())) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100419 ASSERT(isStyledElement());
Ben Murdoch591b9582013-07-10 11:41:44 +0100420 synchronizeStyleAttributeInternal();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100421 return;
422 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100423 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)09380292014-02-21 12:17:33 +0000425
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)53e740f2013-05-09 18:38:43 +0100436 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100437}
438
439const 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 Murdoch591b9582013-07-10 11:41:44 +0100449void Element::scrollIntoView(bool alignToTop)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100450{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100451 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100452
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
464void Element::scrollIntoViewIfNeeded(bool centerIfNeeded)
465{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100466 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100467
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
478void Element::scrollByUnits(int units, ScrollGranularity granularity)
479{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100480 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100481
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)a854de02013-12-18 16:25:25 +0000493 toRenderBox(renderer())->scroll(direction, granularity, units);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100494}
495
496void Element::scrollByLines(int lines)
497{
498 scrollByUnits(lines, ScrollByLine);
499}
500
501void Element::scrollByPages(int pages)
502{
503 scrollByUnits(pages, ScrollByPage);
504}
505
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000506static float localZoomForRenderer(RenderObject& renderer)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100507{
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)51b29062013-11-28 11:56:03 +0000512 if (renderer.style()->effectiveZoom() != 1) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100513 // 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)51b29062013-11-28 11:56:03 +0000515 RenderObject* prev = &renderer;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100516 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)51b29062013-11-28 11:56:03 +0000529static int adjustForLocalZoom(LayoutUnit value, RenderObject& renderer)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100530{
531 float zoomFactor = localZoomForRenderer(renderer);
532 if (zoomFactor == 1)
533 return value;
534 return lroundf(value / zoomFactor);
535}
536
537int Element::offsetLeft()
538{
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000539 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100540 if (RenderBoxModelObject* renderer = renderBoxModelObject())
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000541 return adjustForLocalZoom(renderer->pixelSnappedOffsetLeft(), *renderer);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100542 return 0;
543}
544
545int Element::offsetTop()
546{
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000547 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100548 if (RenderBoxModelObject* renderer = renderBoxModelObject())
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000549 return adjustForLocalZoom(renderer->pixelSnappedOffsetTop(), *renderer);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100550 return 0;
551}
552
553int Element::offsetWidth()
554{
Ben Murdoch07a852d2014-03-31 11:51:52 +0100555 document().updateRenderTreeForNodeIfNeeded(this);
Ben Murdoch591b9582013-07-10 11:41:44 +0100556
557 if (RenderBox* renderer = renderBox()) {
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100558 if (renderer->canDetermineWidthWithoutLayout())
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000559 return adjustLayoutUnitForAbsoluteZoom(renderer->fixedOffsetWidth(), *renderer).round();
Ben Murdoch591b9582013-07-10 11:41:44 +0100560 }
561
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000562 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100563 if (RenderBoxModelObject* renderer = renderBoxModelObject())
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000564 return adjustLayoutUnitForAbsoluteZoom(renderer->pixelSnappedOffsetWidth(), *renderer).round();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100565 return 0;
566}
567
568int Element::offsetHeight()
569{
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000570 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100571 if (RenderBoxModelObject* renderer = renderBoxModelObject())
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000572 return adjustLayoutUnitForAbsoluteZoom(renderer->pixelSnappedOffsetHeight(), *renderer).round();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100573 return 0;
574}
575
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000576Element* Element::offsetParentForBindings()
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100577{
578 Element* element = offsetParent();
579 if (!element || !element->isInShadowTree())
580 return element;
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100581 return element->containingShadowRoot()->shouldExposeToBindings() ? element : 0;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100582}
583
584Element* Element::offsetParent()
585{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100586 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100587 if (RenderObject* renderer = this->renderer())
588 return renderer->offsetParent();
589 return 0;
590}
591
592int Element::clientLeft()
593{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100594 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100595
596 if (RenderBox* renderer = renderBox())
597 return adjustForAbsoluteZoom(roundToInt(renderer->clientLeft()), renderer);
598 return 0;
599}
600
601int Element::clientTop()
602{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100603 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100604
605 if (RenderBox* renderer = renderBox())
606 return adjustForAbsoluteZoom(roundToInt(renderer->clientTop()), renderer);
607 return 0;
608}
609
610int Element::clientWidth()
611{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100612 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100613
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)8abfc582013-09-12 12:10:38 +0100616 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)bfe35902013-10-22 16:41:51 +0100621 return adjustForAbsoluteZoom(view->layoutSize().width(), renderView);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100622 }
623 }
Ben Murdoch591b9582013-07-10 11:41:44 +0100624
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100625 if (RenderBox* renderer = renderBox())
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000626 return adjustLayoutUnitForAbsoluteZoom(renderer->pixelSnappedClientWidth(), *renderer).round();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100627 return 0;
628}
629
630int Element::clientHeight()
631{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100632 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100633
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)8abfc582013-09-12 12:10:38 +0100636 bool inQuirksMode = document().inQuirksMode();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100637
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100638 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)bfe35902013-10-22 16:41:51 +0100642 return adjustForAbsoluteZoom(view->layoutSize().height(), renderView);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100643 }
644 }
Ben Murdoch591b9582013-07-10 11:41:44 +0100645
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100646 if (RenderBox* renderer = renderBox())
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000647 return adjustLayoutUnitForAbsoluteZoom(renderer->pixelSnappedClientHeight(), *renderer).round();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100648 return 0;
649}
650
651int Element::scrollLeft()
652{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100653 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100654
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000655 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)1e202182013-10-18 15:46:42 +0100662 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)53e740f2013-05-09 18:38:43 +0100671 return 0;
672}
673
674int Element::scrollTop()
675{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100676 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100677
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000678 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)1e202182013-10-18 15:46:42 +0100685 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)53e740f2013-05-09 18:38:43 +0100694 return 0;
695}
696
697void Element::setScrollLeft(int newLeft)
698{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100699 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100700
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000701 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)1e202182013-10-18 15:46:42 +0100708 if (document().inQuirksMode())
709 return;
710
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000711 LocalFrame* frame = document().frame();
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100712 if (!frame)
713 return;
714 FrameView* view = frame->view();
715 if (!view)
716 return;
717
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100718 view->setScrollPosition(IntPoint(static_cast<int>(newLeft * frame->pageZoomFactor()), view->scrollY()));
719 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100720}
721
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000722void 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)53e740f2013-05-09 18:38:43 +0100743void Element::setScrollTop(int newTop)
744{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100745 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100746
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000747 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)1e202182013-10-18 15:46:42 +0100754 if (document().inQuirksMode())
755 return;
756
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000757 LocalFrame* frame = document().frame();
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100758 if (!frame)
759 return;
760 FrameView* view = frame->view();
761 if (!view)
762 return;
763
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100764 view->setScrollPosition(IntPoint(view->scrollX(), static_cast<int>(newTop * frame->pageZoomFactor())));
765 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100766}
767
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000768void 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)53e740f2013-05-09 18:38:43 +0100789int Element::scrollWidth()
790{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100791 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100792 if (RenderBox* rend = renderBox())
793 return adjustForAbsoluteZoom(rend->scrollWidth(), rend);
794 return 0;
795}
796
797int Element::scrollHeight()
798{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100799 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100800 if (RenderBox* rend = renderBox())
801 return adjustForAbsoluteZoom(rend->scrollHeight(), rend);
802 return 0;
803}
804
805IntRect Element::boundsInRootViewSpace()
806{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100807 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100808
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100809 FrameView* view = document().view();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100810 if (!view)
811 return IntRect();
812
813 Vector<FloatQuad> quads;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100814 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)93ac45c2013-05-29 14:40:20 +0100820 } else {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100821 // 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
837PassRefPtr<ClientRectList> Element::getClientRects()
838{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100839 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100840
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)51b29062013-11-28 11:56:03 +0000850 document().adjustFloatQuadsForScrollAndAbsoluteZoom(quads, *renderBoxModelObject);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100851 return ClientRectList::create(quads);
852}
853
854PassRefPtr<ClientRect> Element::getBoundingClientRect()
855{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100856 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100857
858 Vector<FloatQuad> quads;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100859 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)93ac45c2013-05-29 14:40:20 +0100865 } else {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100866 // 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)51b29062013-11-28 11:56:03 +0000878 ASSERT(renderer());
879 document().adjustFloatRectForScrollAndAbsoluteZoom(result, *renderer());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100880 return ClientRect::create(result);
881}
Ben Murdoch591b9582013-07-10 11:41:44 +0100882
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100883IntRect Element::screenRect() const
884{
885 if (!renderer())
886 return IntRect();
887 // FIXME: this should probably respect transforms
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100888 return document().view()->contentsToScreen(renderer()->absoluteBoundingBoxRectIgnoringTransforms());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100889}
890
891const AtomicString& Element::getAttribute(const AtomicString& localName) const
892{
893 if (!elementData())
894 return nullAtom;
895 synchronizeAttribute(localName);
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +0100896 if (const Attribute* attribute = elementData()->getAttributeItem(localName, shouldIgnoreAttributeCase()))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100897 return attribute->value();
898 return nullAtom;
899}
900
901const AtomicString& Element::getAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName) const
902{
903 return getAttribute(QualifiedName(nullAtom, localName, namespaceURI));
904}
905
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000906void Element::setAttribute(const AtomicString& localName, const AtomicString& value, ExceptionState& exceptionState)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100907{
908 if (!Document::isValidName(localName)) {
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000909 exceptionState.throwDOMException(InvalidCharacterError, "'" + localName + "' is not a valid attribute name.");
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100910 return;
911 }
912
913 synchronizeAttribute(localName);
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +0100914 const AtomicString& caseAdjustedLocalName = shouldIgnoreAttributeCase() ? localName.lower() : localName;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100915
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +0100916 size_t index = elementData() ? elementData()->getAttributeItemIndex(caseAdjustedLocalName, false) : kNotFound;
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000917 const QualifiedName& qName = index != kNotFound ? attributeItem(index).name() : QualifiedName(nullAtom, caseAdjustedLocalName, nullAtom);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100918 setAttributeInternal(index, qName, value, NotInSynchronizationOfLazyAttribute);
919}
920
921void Element::setAttribute(const QualifiedName& name, const AtomicString& value)
922{
923 synchronizeAttribute(name);
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +0100924 size_t index = elementData() ? elementData()->getAttributeItemIndex(name) : kNotFound;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100925 setAttributeInternal(index, name, value, NotInSynchronizationOfLazyAttribute);
926}
927
928void Element::setSynchronizedLazyAttribute(const QualifiedName& name, const AtomicString& value)
929{
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +0100930 size_t index = elementData() ? elementData()->getAttributeItemIndex(name) : kNotFound;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100931 setAttributeInternal(index, name, value, InSynchronizationOfLazyAttribute);
932}
933
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000934ALWAYS_INLINE void Element::setAttributeInternal(size_t index, const QualifiedName& name, const AtomicString& newValue, SynchronizationOfLazyAttribute inSynchronizationOfLazyAttribute)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100935{
936 if (newValue.isNull()) {
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +0100937 if (index != kNotFound)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100938 removeAttributeInternal(index, inSynchronizationOfLazyAttribute);
939 return;
940 }
941
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +0100942 if (index == kNotFound) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100943 addAttributeInternal(name, newValue, inSynchronizationOfLazyAttribute);
944 return;
945 }
946
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000947 const Attribute& existingAttribute = attributeItem(index);
948 QualifiedName existingAttributeName = existingAttribute.name();
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100949
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100950 if (!inSynchronizationOfLazyAttribute)
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000951 willModifyAttribute(existingAttributeName, existingAttribute.value(), newValue);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100952
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000953 if (newValue != existingAttribute.value()) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100954 // 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)d5428f32014-03-18 10:21:16 +0000957 if (RefPtr<Attr> attrNode = inSynchronizationOfLazyAttribute ? nullptr : attrIfExists(existingAttributeName))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100958 attrNode->setValue(newValue);
959 else
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000960 ensureUniqueElementData().attributeItem(index).setValue(newValue);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100961 }
962
963 if (!inSynchronizationOfLazyAttribute)
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100964 didModifyAttribute(existingAttributeName, newValue);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100965}
966
967static inline AtomicString makeIdForStyleResolution(const AtomicString& value, bool inQuirksMode)
968{
969 if (inQuirksMode)
970 return value.lower();
971 return value;
972}
973
Ben Murdoch7757ec22013-07-23 11:17:36 +0100974static bool checkNeedsStyleInvalidationForIdChange(const AtomicString& oldId, const AtomicString& newId, const RuleFeatureSet& features)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100975{
976 ASSERT(newId != oldId);
Ben Murdoch7757ec22013-07-23 11:17:36 +0100977 if (!oldId.isEmpty() && features.hasSelectorForId(oldId))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100978 return true;
Ben Murdoch7757ec22013-07-23 11:17:36 +0100979 if (!newId.isEmpty() && features.hasSelectorForId(newId))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100980 return true;
981 return false;
982}
983
Ben Murdoch591b9582013-07-10 11:41:44 +0100984void Element::attributeChanged(const QualifiedName& name, const AtomicString& newValue, AttributeModificationReason reason)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100985{
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000986 if (ElementShadow* parentElementShadow = shadowWhereNodeCanBeDistributed(*this)) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100987 if (shouldInvalidateDistributionWhenAttributeChanged(parentElementShadow, name, newValue))
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +0100988 parentElementShadow->setNeedsDistributionRecalc();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100989 }
990
991 parseAttribute(name, newValue);
992
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100993 document().incDOMTreeVersion();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100994
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000995 StyleResolver* styleResolver = document().styleResolver();
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +0000996 bool testShouldInvalidateStyle = inActiveDocument() && styleResolver && styleChangeType() < SubtreeStyleChange;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100997 bool shouldInvalidateStyle = false;
998
Ben Murdoch591b9582013-07-10 11:41:44 +0100999 if (isStyledElement() && name == styleAttr) {
1000 styleAttributeChanged(newValue, reason);
1001 } else if (isStyledElement() && isPresentationAttribute(name)) {
1002 elementData()->m_presentationAttributeStyleIsDirty = true;
Ben Murdoche69819b2013-07-17 14:56:49 +01001003 setNeedsStyleRecalc(LocalStyleChange);
Ben Murdoch591b9582013-07-10 11:41:44 +01001004 }
1005
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001006 if (isIdAttributeName(name)) {
1007 AtomicString oldId = elementData()->idForStyleResolution();
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01001008 AtomicString newId = makeIdForStyleResolution(newValue, document().inQuirksMode());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001009 if (newId != oldId) {
1010 elementData()->setIdForStyleResolution(newId);
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00001011 shouldInvalidateStyle = testShouldInvalidateStyle && checkNeedsStyleInvalidationForIdChange(oldId, newId, styleResolver->ensureUpdatedRuleFeatureSet());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001012 }
Ben Murdoch591b9582013-07-10 11:41:44 +01001013 } else if (name == classAttr) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001014 classAttributeChanged(newValue);
Ben Murdoch591b9582013-07-10 11:41:44 +01001015 } else if (name == HTMLNames::nameAttr) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001016 setHasName(!newValue.isNull());
Ben Murdoch591b9582013-07-10 11:41:44 +01001017 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001018
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)09380292014-02-21 12:17:33 +00001025 setNeedsStyleRecalc(SubtreeStyleChange);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001026
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01001027 if (AXObjectCache* cache = document().existingAXObjectCache())
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001028 cache->handleAttributeChanged(name, this);
1029}
1030
Torne (Richard Coles)43e75022014-03-21 14:26:12 +00001031bool Element::hasLegalLinkAttribute(const QualifiedName&) const
1032{
1033 return false;
1034}
1035
1036const QualifiedName& Element::subResourceAttributeName() const
1037{
1038 return nullQName();
1039}
1040
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001041inline void Element::attributeChangedFromParserOrByCloning(const QualifiedName& name, const AtomicString& newValue, AttributeModificationReason reason)
1042{
Ben Murdoche69819b2013-07-17 14:56:49 +01001043 if (name == isAttr)
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001044 CustomElementRegistrationContext::setTypeExtension(this, newValue);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001045 attributeChanged(name, newValue, reason);
1046}
1047
1048template <typename CharacterType>
1049static inline bool classStringHasClassName(const CharacterType* characters, unsigned length)
1050{
1051 ASSERT(length > 0);
1052
1053 unsigned i = 0;
1054 do {
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +01001055 if (isNotHTMLSpace<CharacterType>(characters[i]))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001056 break;
1057 ++i;
1058 } while (i < length);
1059
1060 return i < length;
1061}
1062
1063static 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)53e740f2013-05-09 18:38:43 +01001075void Element::classAttributeChanged(const AtomicString& newClassString)
1076{
Torne (Richard Coles)a854de02013-12-18 16:25:25 +00001077 StyleResolver* styleResolver = document().styleResolver();
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +00001078 bool testShouldInvalidateStyle = inActiveDocument() && styleResolver && styleChangeType() < SubtreeStyleChange;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001079
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00001080 ASSERT(elementData());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001081 if (classStringHasClassName(newClassString)) {
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01001082 const bool shouldFoldCase = document().inQuirksMode();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001083 const SpaceSplitString oldClasses = elementData()->classNames();
1084 elementData()->setClass(newClassString, shouldFoldCase);
1085 const SpaceSplitString& newClasses = elementData()->classNames();
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001086 if (testShouldInvalidateStyle)
Ben Murdochaafa69c2014-04-03 12:30:15 +01001087 styleResolver->ensureUpdatedRuleFeatureSet().scheduleStyleInvalidationForClassChange(oldClasses, newClasses, *this);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001088 } else {
1089 const SpaceSplitString& oldClasses = elementData()->classNames();
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001090 if (testShouldInvalidateStyle)
Ben Murdochaafa69c2014-04-03 12:30:15 +01001091 styleResolver->ensureUpdatedRuleFeatureSet().scheduleStyleInvalidationForClassChange(oldClasses, *this);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001092 elementData()->clearClass();
1093 }
1094
1095 if (hasRareData())
1096 elementRareData()->clearClassListValueForQuirksMode();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001097}
1098
1099bool Element::shouldInvalidateDistributionWhenAttributeChanged(ElementShadow* elementShadow, const QualifiedName& name, const AtomicString& newValue)
1100{
1101 ASSERT(elementShadow);
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +01001102 const SelectRuleFeatureSet& featureSet = elementShadow->ensureSelectFeatureSet();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001103
1104 if (isIdAttributeName(name)) {
1105 AtomicString oldId = elementData()->idForStyleResolution();
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01001106 AtomicString newId = makeIdForStyleResolution(newValue, document().inQuirksMode());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001107 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)8abfc582013-09-12 12:10:38 +01001118 const bool shouldFoldCase = document().inQuirksMode();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001119 const SpaceSplitString& oldClasses = elementData()->classNames();
1120 const SpaceSplitString newClasses(newClassString, shouldFoldCase);
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001121 if (featureSet.checkSelectorsForClassChange(oldClasses, newClasses))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001122 return true;
1123 } else {
1124 const SpaceSplitString& oldClasses = elementData()->classNames();
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001125 if (featureSet.checkSelectorsForClassChange(oldClasses))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001126 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
1138static inline bool isEventHandlerAttribute(const Attribute& attribute)
1139{
1140 return attribute.name().namespaceURI().isNull() && attribute.name().localName().startsWith("on");
1141}
1142
1143bool Element::isJavaScriptURLAttribute(const Attribute& attribute) const
1144{
1145 return isURLAttribute(attribute) && protocolIsJavaScript(stripLeadingAndTrailingHTMLSpaces(attribute.value()));
1146}
1147
1148void 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
1165void 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)09380292014-02-21 12:17:33 +00001174 if (document().elementDataCache())
1175 m_elementData = document().elementDataCache()->cachedShareableElementDataWithAttributes(attributeVector);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001176 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
1184bool Element::hasAttributes() const
1185{
1186 synchronizeAllAttributes();
1187 return elementData() && elementData()->length();
1188}
1189
1190bool 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
1203String Element::nodeName() const
1204{
1205 return m_tagName.toString();
1206}
1207
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001208void Element::setPrefix(const AtomicString& prefix, ExceptionState& exceptionState)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001209{
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001210 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)51b29062013-11-28 11:56:03 +00001230 if (exceptionState.hadException())
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001231 return;
1232
1233 m_tagName.setPrefix(prefix.isEmpty() ? AtomicString() : prefix);
1234}
1235
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001236const AtomicString& Element::locateNamespacePrefix(const AtomicString& namespaceToLocate) const
1237{
1238 if (!prefix().isNull() && namespaceURI() == namespaceToLocate)
1239 return prefix();
1240
1241 if (hasAttributes()) {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00001242 unsigned attributeCount = this->attributeCount();
1243 for (unsigned i = 0; i < attributeCount; ++i) {
1244 const Attribute& attr = attributeItem(i);
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001245
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00001246 if (attr.prefix() == xmlnsAtom && attr.value() == namespaceToLocate)
1247 return attr.localName();
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001248 }
1249 }
1250
1251 if (Element* parent = parentElement())
1252 return parent->locateNamespacePrefix(namespaceToLocate);
1253
1254 return nullAtom;
1255}
1256
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001257KURL Element::baseURI() const
1258{
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001259 const AtomicString& baseAttribute = fastGetAttribute(baseAttr);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001260 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)8abfc582013-09-12 12:10:38 +01001275const AtomicString Element::imageSourceURL() const
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001276{
1277 return getAttribute(srcAttr);
1278}
1279
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01001280bool Element::rendererIsNeeded(const RenderStyle& style)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001281{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01001282 return style.display() != NONE;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001283}
1284
Ben Murdoch591b9582013-07-10 11:41:44 +01001285RenderObject* Element::createRenderer(RenderStyle* style)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001286{
1287 return RenderObject::createObject(this, style);
1288}
1289
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001290Node::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)19cde672013-11-06 12:28:04 +00001299 ASSERT(!hasRareData() || !elementRareData()->hasPseudoElements());
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +01001300
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001301 if (!insertionPoint->isInTreeScope())
1302 return InsertionDone;
1303
1304 if (hasRareData())
1305 elementRareData()->clearClassListValueForQuirksMode();
1306
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +01001307 if (isUpgradedCustomElement() && inDocument())
1308 CustomElement::didEnterDocument(this, document());
1309
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01001310 TreeScope& scope = insertionPoint->treeScope();
Torne (Richard Coles)1e202182013-10-18 15:46:42 +01001311 if (scope != treeScope())
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001312 return InsertionDone;
1313
1314 const AtomicString& idValue = getIdAttribute();
Ben Murdoch00d3faa2013-08-14 11:52:03 +01001315 if (!idValue.isNull())
1316 updateId(scope, nullAtom, idValue);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001317
1318 const AtomicString& nameValue = getNameAttribute();
Ben Murdoch00d3faa2013-08-14 11:52:03 +01001319 if (!nameValue.isNull())
1320 updateName(nullAtom, nameValue);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001321
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00001322 if (isHTMLLabelElement(*this)) {
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01001323 if (scope.shouldCacheLabelsByForAttribute())
Ben Murdoch00d3faa2013-08-14 11:52:03 +01001324 updateLabel(scope, nullAtom, fastGetAttribute(forAttr));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001325 }
1326
Ben Murdoch3c9e4ae2013-08-12 14:20:44 +01001327 if (parentElement() && parentElement()->isInCanvasSubtree())
1328 setIsInCanvasSubtree(true);
1329
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001330 return InsertionDone;
1331}
1332
1333void Element::removedFrom(ContainerNode* insertionPoint)
1334{
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +01001335 bool wasInDocument = insertionPoint->inDocument();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001336
Torne (Richard Coles)19cde672013-11-06 12:28:04 +00001337 ASSERT(!hasRareData() || !elementRareData()->hasPseudoElements());
Ben Murdoche69819b2013-07-17 14:56:49 +01001338
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001339 if (containsFullScreenElement())
1340 setContainsFullScreenElementOnAncestorsCrossingFrameBoundaries(false);
1341
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01001342 if (document().page())
1343 document().page()->pointerLockController().elementRemoved(this);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001344
1345 setSavedLayerScrollOffset(IntSize());
1346
Torne (Richard Coles)1e202182013-10-18 15:46:42 +01001347 if (insertionPoint->isInTreeScope() && treeScope() == document()) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001348 const AtomicString& idValue = getIdAttribute();
Ben Murdoch00d3faa2013-08-14 11:52:03 +01001349 if (!idValue.isNull())
1350 updateId(insertionPoint->treeScope(), idValue, nullAtom);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001351
1352 const AtomicString& nameValue = getNameAttribute();
Ben Murdoch00d3faa2013-08-14 11:52:03 +01001353 if (!nameValue.isNull())
1354 updateName(nameValue, nullAtom);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001355
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00001356 if (isHTMLLabelElement(*this)) {
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01001357 TreeScope& treeScope = insertionPoint->treeScope();
1358 if (treeScope.shouldCacheLabelsByForAttribute())
Ben Murdoch00d3faa2013-08-14 11:52:03 +01001359 updateLabel(treeScope, fastGetAttribute(forAttr), nullAtom);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001360 }
1361 }
1362
1363 ContainerNode::removedFrom(insertionPoint);
Ben Murdoche69819b2013-07-17 14:56:49 +01001364 if (wasInDocument) {
1365 if (hasPendingResources())
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00001366 document().accessSVGExtensions().removeElementFromPendingResources(this);
Ben Murdoche69819b2013-07-17 14:56:49 +01001367
Ben Murdoch83750172013-07-24 10:36:59 +01001368 if (isUpgradedCustomElement())
Ben Murdochfff88842013-07-30 15:20:09 +01001369 CustomElement::didLeaveDocument(this, insertionPoint->document());
Ben Murdoche69819b2013-07-17 14:56:49 +01001370 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001371
Torne (Richard Coles)19cde672013-11-06 12:28:04 +00001372 document().removeFromTopLayer(this);
1373
Torne (Richard Coles)43e75022014-03-21 14:26:12 +00001374 clearElementFlag(IsInCanvasSubtree);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001375}
1376
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +01001377void Element::attach(const AttachContext& context)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001378{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01001379 ASSERT(document().inStyleRecalc());
1380
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001381 // We've already been through detach when doing an attach, but we might
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +01001382 // need to clear any state that's been added since then.
Torne (Richard Coles)1e202182013-10-18 15:46:42 +01001383 if (hasRareData() && styleChangeType() == NeedsReattachStyleChange) {
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +01001384 ElementRareData* data = elementRareData();
1385 data->clearComputedStyle();
Ben Murdoch07a852d2014-03-31 11:51:52 +01001386 data->clearRestyleFlags();
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001387 // Only clear the style state if we're not going to reuse the style from recalcStyle.
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +01001388 if (!context.resolvedStyle)
1389 data->resetStyleState();
1390 }
1391
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001392 RenderTreeBuilder(this, context.resolvedStyle).createRendererForElementIfNeeded();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001393
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001394 addCallbackSelectors();
Torne (Richard Coles)1e202182013-10-18 15:46:42 +01001395
Ben Murdoch07a852d2014-03-31 11:51:52 +01001396 StyleResolverParentScope parentScope(*this);
1397
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001398 createPseudoElementIfNeeded(BEFORE);
1399
1400 // When a shadow root exists, it does the work of attaching the children.
Ben Murdoch07a852d2014-03-31 11:51:52 +01001401 if (ElementShadow* shadow = this->shadow())
Ben Murdoch591b9582013-07-10 11:41:44 +01001402 shadow->attach(context);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001403
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +01001404 ContainerNode::attach(context);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001405
1406 createPseudoElementIfNeeded(AFTER);
Ben Murdoche69819b2013-07-17 14:56:49 +01001407 createPseudoElementIfNeeded(BACKDROP);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001408
Ben Murdoch591b9582013-07-10 11:41:44 +01001409 if (hasRareData()) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001410 ElementRareData* data = elementRareData();
Ben Murdoch07a852d2014-03-31 11:51:52 +01001411 if (data->hasElementFlag(NeedsFocusAppearanceUpdateSoonAfterAttach)) {
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01001412 if (isFocusable() && document().focusedElement() == this)
1413 document().updateFocusAppearanceSoon(false /* don't restore selection */);
Ben Murdoch07a852d2014-03-31 11:51:52 +01001414 data->clearElementFlag(NeedsFocusAppearanceUpdateSoonAfterAttach);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001415 }
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001416 if (!renderer()) {
1417 if (ActiveAnimations* activeAnimations = data->activeAnimations()) {
1418 activeAnimations->cssAnimations().cancel();
1419 activeAnimations->setAnimationStyleChange(false);
1420 }
1421 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001422 }
Ben Murdoch591b9582013-07-10 11:41:44 +01001423
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01001424 InspectorInstrumentation::didRecalculateStyleForElement(this);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001425}
1426
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +01001427void Element::detach(const AttachContext& context)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001428{
Torne (Richard Coles)1e202182013-10-18 15:46:42 +01001429 RenderWidget::UpdateSuspendScope suspendWidgetHierarchyUpdates;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001430 cancelFocusAppearanceUpdate();
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001431 removeCallbackSelectors();
Ben Murdoch07a852d2014-03-31 11:51:52 +01001432 if (needsLayerUpdate())
1433 document().unscheduleLayerUpdate(*this);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001434 if (hasRareData()) {
1435 ElementRareData* data = elementRareData();
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001436 data->clearPseudoElements();
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +01001437
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001438 // attach() will perform the below steps for us when inside recalcStyle.
1439 if (!document().inStyleRecalc()) {
Ben Murdoch3c9e4ae2013-08-12 14:20:44 +01001440 data->resetStyleState();
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001441 data->clearComputedStyle();
Ben Murdoch07a852d2014-03-31 11:51:52 +01001442 data->clearRestyleFlags();
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001443 }
Ben Murdoch3c9e4ae2013-08-12 14:20:44 +01001444
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001445 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)51b29062013-11-28 11:56:03 +00001456 }
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +01001457 }
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001458
1459 if (ElementShadow* shadow = data->shadow())
1460 shadow->detach(context);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001461 }
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +01001462 ContainerNode::detach(context);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001463}
1464
1465bool 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)51b29062013-11-28 11:56:03 +00001493 // is needed, but for now just assume a layout will be required. The diff code
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001494 // 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)521d96e2013-06-19 11:58:24 +01001503PassRefPtr<RenderStyle> Element::styleForRenderer()
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001504{
Torne (Richard Coles)a854de02013-12-18 16:25:25 +00001505 ASSERT(document().inStyleRecalc());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001506
Torne (Richard Coles)a854de02013-12-18 16:25:25 +00001507 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)53e740f2013-05-09 18:38:43 +01001525}
1526
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +01001527PassRefPtr<RenderStyle> Element::originalStyleForRenderer()
Torne (Richard Coles)5267f702013-06-11 10:57:24 +01001528{
Torne (Richard Coles)a854de02013-12-18 16:25:25 +00001529 ASSERT(document().inStyleRecalc());
1530 return document().ensureStyleResolver().styleForElement(this);
Torne (Richard Coles)5267f702013-06-11 10:57:24 +01001531}
1532
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001533void Element::recalcStyle(StyleRecalcChange change, Text* nextTextSibling)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001534{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01001535 ASSERT(document().inStyleRecalc());
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001536 ASSERT(!parentOrShadowHostNode()->needsStyleRecalc());
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +01001537
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01001538 if (hasCustomStyleCallbacks())
1539 willRecalcStyle(change);
1540
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001541 if (change >= Inherit || needsStyleRecalc()) {
1542 if (hasRareData()) {
1543 ElementRareData* data = elementRareData();
1544 data->resetStyleState();
1545 data->clearComputedStyle();
Torne (Richard Coles)a854de02013-12-18 16:25:25 +00001546
1547 if (change >= Inherit) {
1548 if (ActiveAnimations* activeAnimations = data->activeAnimations())
1549 activeAnimations->setAnimationStyleChange(false);
1550 }
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001551 }
1552 if (parentRenderStyle())
1553 change = recalcOwnStyle(change);
1554 clearNeedsStyleRecalc();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001555 }
Ben Murdoch3c9e4ae2013-08-12 14:20:44 +01001556
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +01001557 // If we reattached we don't need to recalc the style of our descendants anymore.
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001558 if ((change >= UpdatePseudoElements && change < Reattach) || childNeedsStyleRecalc()) {
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +01001559 recalcChildStyle(change);
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001560 clearChildNeedsStyleRecalc();
1561 }
Ben Murdoch591b9582013-07-10 11:41:44 +01001562
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01001563 if (hasCustomStyleCallbacks())
1564 didRecalcStyle(change);
1565
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001566 if (change == Reattach)
1567 reattachWhitespaceSiblings(nextTextSibling);
Torne (Richard Coles)1e202182013-10-18 15:46:42 +01001568}
1569
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01001570StyleRecalcChange Element::recalcOwnStyle(StyleRecalcChange change)
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +01001571{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01001572 ASSERT(document().inStyleRecalc());
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001573 ASSERT(!parentOrShadowHostNode()->needsStyleRecalc());
1574 ASSERT(change >= Inherit || needsStyleRecalc());
1575 ASSERT(parentRenderStyle());
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +01001576
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +01001577 RefPtr<RenderStyle> oldStyle = renderStyle();
Torne (Richard Coles)a854de02013-12-18 16:25:25 +00001578 RefPtr<RenderStyle> newStyle = styleForRenderer();
Ben Murdoch07a852d2014-03-31 11:51:52 +01001579 StyleRecalcChange localChange = RenderStyle::stylePropagationDiff(oldStyle.get(), newStyle.get());
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +01001580
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001581 ASSERT(newStyle);
1582
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +01001583 if (localChange == Reattach) {
1584 AttachContext reattachContext;
1585 reattachContext.resolvedStyle = newStyle.get();
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00001586 bool rendererWillChange = needsAttach() || renderer();
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +01001587 reattach(reattachContext);
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00001588 if (rendererWillChange || renderer())
1589 return Reattach;
1590 return ReattachNoRenderer;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001591 }
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +01001592
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001593 ASSERT(oldStyle);
1594
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +01001595 InspectorInstrumentation::didRecalculateStyleForElement(this);
1596
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001597 if (localChange != NoChange)
Torne (Richard Coles)1e202182013-10-18 15:46:42 +01001598 updateCallbackSelectors(oldStyle.get(), newStyle.get());
1599
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +01001600 if (RenderObject* renderer = this->renderer()) {
Ben Murdoch07a852d2014-03-31 11:51:52 +01001601 if (localChange != NoChange || pseudoStyleCacheIsInvalid(oldStyle.get(), newStyle.get()) || needsLayerUpdate()) {
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001602 renderer->setStyle(newStyle.get());
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +00001603 } else {
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +01001604 // 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)f79f16f2013-10-31 11:16:44 +00001606 // FIXME: We may be able to remove this hack, see discussion in
1607 // https://codereview.chromium.org/30453002/
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +01001608 renderer->setStyleInternal(newStyle.get());
1609 }
1610 }
1611
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +01001612 if (styleChangeType() >= SubtreeStyleChange)
1613 return Force;
1614
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001615 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)c0e19a62013-08-30 15:15:11 +01001622}
1623
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01001624void Element::recalcChildStyle(StyleRecalcChange change)
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +01001625{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01001626 ASSERT(document().inStyleRecalc());
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001627 ASSERT(change >= UpdatePseudoElements || childNeedsStyleRecalc());
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001628 ASSERT(!needsStyleRecalc());
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +01001629
Ben Murdoch07a852d2014-03-31 11:51:52 +01001630 StyleResolverParentScope parentScope(*this);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001631
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001632 updatePseudoElement(BEFORE, change);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001633
Ben Murdoch07a852d2014-03-31 11:51:52 +01001634 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)51b29062013-11-28 11:56:03 +00001641 if (change < Force && hasRareData() && childNeedsStyleRecalc())
Ben Murdoch07a852d2014-03-31 11:51:52 +01001642 checkForChildrenAdjacentRuleChanges();
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +00001643
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001644 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 Murdoch07a852d2014-03-31 11:51:52 +01001657 if (element->shouldCallRecalcStyle(change))
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001658 element->recalcStyle(change, lastTextNode);
Ben Murdoch07a852d2014-03-31 11:51:52 +01001659 else if (element->supportsStyleSharing())
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001660 styleResolver.addToStyleSharingList(*element);
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001661 if (element->renderer())
1662 lastTextNode = 0;
Ben Murdoche69819b2013-07-17 14:56:49 +01001663 }
Ben Murdoch591b9582013-07-10 11:41:44 +01001664 }
Torne (Richard Coles)5267f702013-06-11 10:57:24 +01001665 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001666
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001667 updatePseudoElement(AFTER, change);
1668 updatePseudoElement(BACKDROP, change);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001669}
1670
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001671void 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
1682void Element::addCallbackSelectors()
1683{
1684 updateCallbackSelectors(0, renderStyle());
1685}
1686
1687void Element::removeCallbackSelectors()
1688{
1689 updateCallbackSelectors(renderStyle(), 0);
1690}
1691
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001692ElementShadow* Element::shadow() const
1693{
1694 return hasRareData() ? elementRareData()->shadow() : 0;
1695}
1696
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01001697ElementShadow& Element::ensureShadow()
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001698{
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01001699 return ensureElementRareData().ensureShadow();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001700}
1701
1702void Element::didAffectSelector(AffectedSelectorMask mask)
1703{
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001704 setNeedsStyleRecalc(SubtreeStyleChange);
Torne (Richard Coles)19cde672013-11-06 12:28:04 +00001705 if (ElementShadow* elementShadow = shadowWhereNodeCanBeDistributed(*this))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001706 elementShadow->didAffectSelector(mask);
1707}
1708
Torne (Richard Coles)a854de02013-12-18 16:25:25 +00001709void Element::setAnimationStyleChange(bool animationStyleChange)
1710{
1711 if (ActiveAnimations* activeAnimations = elementRareData()->activeAnimations())
1712 activeAnimations->setAnimationStyleChange(animationStyleChange);
1713}
1714
1715void Element::setNeedsAnimationStyleRecalc()
1716{
1717 if (styleChangeType() != NoStyleChange)
1718 return;
1719
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001720 setNeedsStyleRecalc(LocalStyleChange);
Torne (Richard Coles)a854de02013-12-18 16:25:25 +00001721 setAnimationStyleChange(true);
1722}
1723
Torne (Richard Coles)43e75022014-03-21 14:26:12 +00001724void Element::setCustomElementDefinition(PassRefPtr<CustomElementDefinition> definition)
1725{
1726 if (!hasRareData() && !definition)
1727 return;
1728 ASSERT(!customElementDefinition());
1729 ensureElementRareData().setCustomElementDefinition(definition);
1730}
1731
1732CustomElementDefinition* Element::customElementDefinition() const
1733{
1734 if (hasRareData())
1735 return elementRareData()->customElementDefinition();
1736 return 0;
1737}
1738
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001739PassRefPtr<ShadowRoot> Element::createShadowRoot(ExceptionState& exceptionState)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001740{
1741 if (alwaysCreateUserAgentShadowRoot())
1742 ensureUserAgentShadowRoot();
1743
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001744 // 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)d5428f32014-03-18 10:21:16 +00001749 return nullptr;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001750 }
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001751
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001752 return PassRefPtr<ShadowRoot>(ensureShadow().addShadowRoot(*this, ShadowRoot::AuthorShadowRoot));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001753}
1754
1755ShadowRoot* 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)8abfc582013-09-12 12:10:38 +01001766void Element::didAddShadowRoot(ShadowRoot&)
1767{
1768}
1769
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001770ShadowRoot* 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)51b29062013-11-28 11:56:03 +00001782ShadowRoot& Element::ensureUserAgentShadowRoot()
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001783{
1784 if (ShadowRoot* shadowRoot = userAgentShadowRoot())
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001785 return *shadowRoot;
1786 ShadowRoot& shadowRoot = ensureShadow().addShadowRoot(*this, ShadowRoot::UserAgentShadowRoot);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001787 didAddUserAgentShadowRoot(shadowRoot);
1788 return shadowRoot;
1789}
1790
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001791bool 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)53e740f2013-05-09 18:38:43 +01001799 return true;
1800 default:
1801 break;
1802 }
1803 return false;
1804}
1805
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001806void Element::checkForEmptyStyleChange(RenderStyle* style)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001807{
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001808 if (!style && !styleAffectedByEmpty())
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001809 return;
1810
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00001811 if (!style || (styleAffectedByEmpty() && (!style->emptyState() || hasChildren())))
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001812 setNeedsStyleRecalc(SubtreeStyleChange);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001813}
1814
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001815void Element::checkForSiblingStyleChanges(bool finishedParsingCallback, Node* beforeChange, Node* afterChange, int childCountDelta)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001816{
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001817 if (!inActiveDocument() || document().hasPendingForcedStyleRecalc() || styleChangeType() >= SubtreeStyleChange)
Ben Murdoch83750172013-07-24 10:36:59 +01001818 return;
1819
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001820 RenderStyle* style = renderStyle();
Ben Murdoch591b9582013-07-10 11:41:44 +01001821
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001822 // :empty selector.
1823 checkForEmptyStyleChange(style);
1824
1825 if (!style || (needsStyleRecalc() && childrenAffectedByPositionalRules()))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001826 return;
1827
Torne (Richard Coles)43e75022014-03-21 14:26:12 +00001828 // 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)53e740f2013-05-09 18:38:43 +01001830 // Backward positional selectors include nth-last-child, nth-last-of-type, last-of-type and only-of-type.
Torne (Richard Coles)43e75022014-03-21 14:26:12 +00001831 // 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)53e740f2013-05-09 18:38:43 +01001833 // |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 Murdoch83750172013-07-24 10:36:59 +01001835 // here. recalcStyle will then force a walk of the children when it sees that this has happened.
Torne (Richard Coles)43e75022014-03-21 14:26:12 +00001836 if (((childrenAffectedByForwardPositionalRules() || childrenAffectedByIndirectAdjacentRules()) && afterChange)
1837 || (childrenAffectedByBackwardPositionalRules() && beforeChange)) {
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001838 setNeedsStyleRecalc(SubtreeStyleChange);
Ben Murdoch83750172013-07-24 10:36:59 +01001839 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)51b29062013-11-28 11:56:03 +00001845 if (childrenAffectedByFirstChildRules() && afterChange) {
Ben Murdoch83750172013-07-24 10:36:59 +01001846 // Find our new first child.
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001847 Element* newFirstChild = ElementTraversal::firstWithin(*this);
Ben Murdoch83750172013-07-24 10:36:59 +01001848 RenderStyle* newFirstChildStyle = newFirstChild ? newFirstChild->renderStyle() : 0;
1849
1850 // Find the first element node following |afterChange|
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001851 Node* firstElementAfterInsertion = afterChange->isElementNode() ? afterChange : ElementTraversal::nextSibling(*afterChange);
Ben Murdoch83750172013-07-24 10:36:59 +01001852 RenderStyle* firstElementAfterInsertionStyle = firstElementAfterInsertion ? firstElementAfterInsertion->renderStyle() : 0;
1853
1854 // This is the insert/append case.
1855 if (newFirstChild != firstElementAfterInsertion && firstElementAfterInsertionStyle && firstElementAfterInsertionStyle->firstChildState())
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001856 firstElementAfterInsertion->setNeedsStyleRecalc(SubtreeStyleChange);
Ben Murdoch83750172013-07-24 10:36:59 +01001857
1858 // We also have to handle node removal.
1859 if (childCountDelta < 0 && newFirstChild == firstElementAfterInsertion && newFirstChild && (!newFirstChildStyle || !newFirstChildStyle->firstChildState()))
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001860 newFirstChild->setNeedsStyleRecalc(SubtreeStyleChange);
Ben Murdoch83750172013-07-24 10:36:59 +01001861 }
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)51b29062013-11-28 11:56:03 +00001865 if (childrenAffectedByLastChildRules() && beforeChange) {
Ben Murdoch83750172013-07-24 10:36:59 +01001866 // Find our new last child.
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001867 Node* newLastChild = ElementTraversal::lastWithin(*this);
Ben Murdoch83750172013-07-24 10:36:59 +01001868 RenderStyle* newLastChildStyle = newLastChild ? newLastChild->renderStyle() : 0;
1869
1870 // Find the last element node going backwards from |beforeChange|
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001871 Node* lastElementBeforeInsertion = beforeChange->isElementNode() ? beforeChange : ElementTraversal::previousSibling(*beforeChange);
Ben Murdoch83750172013-07-24 10:36:59 +01001872 RenderStyle* lastElementBeforeInsertionStyle = lastElementBeforeInsertion ? lastElementBeforeInsertion->renderStyle() : 0;
1873
1874 if (newLastChild != lastElementBeforeInsertion && lastElementBeforeInsertionStyle && lastElementBeforeInsertionStyle->lastChildState())
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001875 lastElementBeforeInsertion->setNeedsStyleRecalc(SubtreeStyleChange);
Ben Murdoch83750172013-07-24 10:36:59 +01001876
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)09380292014-02-21 12:17:33 +00001880 newLastChild->setNeedsStyleRecalc(SubtreeStyleChange);
Ben Murdoch83750172013-07-24 10:36:59 +01001881 }
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)51b29062013-11-28 11:56:03 +00001885 if (childrenAffectedByDirectAdjacentRules() && afterChange) {
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001886 if (Node* firstElementAfterInsertion = afterChange->isElementNode() ? afterChange : ElementTraversal::nextSibling(*afterChange))
1887 firstElementAfterInsertion->setNeedsStyleRecalc(SubtreeStyleChange);
Ben Murdoch83750172013-07-24 10:36:59 +01001888 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001889}
1890
1891void Element::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
1892{
1893 ContainerNode::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
1894 if (changedByParser)
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001895 checkForEmptyStyleChange(renderStyle());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001896 else
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001897 checkForSiblingStyleChanges(false, beforeChange, afterChange, childCountDelta);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001898
Ben Murdoch83750172013-07-24 10:36:59 +01001899 if (ElementShadow* shadow = this->shadow())
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +01001900 shadow->setNeedsDistributionRecalc();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001901}
1902
1903void Element::removeAllEventListeners()
1904{
1905 ContainerNode::removeAllEventListeners();
1906 if (ElementShadow* shadow = this->shadow())
1907 shadow->removeAllEventListeners();
1908}
1909
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001910void Element::finishParsingChildren()
1911{
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001912 setIsFinishedParsingChildren(true);
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001913 checkForSiblingStyleChanges(this, lastChild(), 0, 0);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001914}
1915
1916#ifndef NDEBUG
1917void 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
1944const Vector<RefPtr<Attr> >& Element::attrNodeList()
1945{
1946 ASSERT(hasSyntheticAttrChildNodes());
1947 return *attrNodeListForElement(this);
1948}
1949
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001950PassRefPtr<Attr> Element::setAttributeNode(Attr* attrNode, ExceptionState& exceptionState)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001951{
1952 if (!attrNode) {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00001953 exceptionState.throwDOMException(TypeMismatchError, ExceptionMessages::argumentNullOrIncorrectType(1, "Attr"));
1954 return nullptr;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001955 }
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 Murdoche69819b2013-07-17 14:56:49 +01001961 // InUseAttributeError: Raised if node is an Attr that is already an attribute of another Element object.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001962 // The DOM user must explicitly clone Attr nodes to re-use them in other elements.
1963 if (attrNode->ownerElement()) {
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001964 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)d5428f32014-03-18 10:21:16 +00001965 return nullptr;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001966 }
1967
1968 synchronizeAllAttributes();
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00001969 UniqueElementData& elementData = ensureUniqueElementData();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001970
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00001971 size_t index = elementData.getAttributeItemIndex(attrNode->qualifiedName(), shouldIgnoreAttributeCase());
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +01001972 if (index != kNotFound) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001973 if (oldAttrNode)
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00001974 detachAttrNodeFromElementWithValue(oldAttrNode.get(), elementData.attributeItem(index).value());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001975 else
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00001976 oldAttrNode = Attr::create(document(), attrNode->qualifiedName(), elementData.attributeItem(index).value());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001977 }
1978
1979 setAttributeInternal(index, attrNode->qualifiedName(), attrNode->value(), NotInSynchronizationOfLazyAttribute);
1980
1981 attrNode->attachToElement(this);
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +00001982 treeScope().adoptIfNeeded(*attrNode);
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01001983 ensureAttrNodeListForElement(this).append(attrNode);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001984
1985 return oldAttrNode.release();
1986}
1987
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00001988PassRefPtr<Attr> Element::removeAttributeNode(Attr* attr, ExceptionState& exceptionState)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001989{
1990 if (!attr) {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00001991 exceptionState.throwDOMException(TypeMismatchError, ExceptionMessages::argumentNullOrIncorrectType(1, "Attr"));
1992 return nullptr;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001993 }
1994 if (attr->ownerElement() != this) {
Torne (Richard Coles)09380292014-02-21 12:17:33 +00001995 exceptionState.throwDOMException(NotFoundError, "The node provided is owned by another element.");
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00001996 return nullptr;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001997 }
1998
Torne (Richard Coles)1e202182013-10-18 15:46:42 +01001999 ASSERT(document() == attr->document());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002000
2001 synchronizeAttribute(attr->qualifiedName());
2002
Torne (Richard Coles)e5249552013-05-15 11:35:13 +01002003 size_t index = elementData()->getAttrIndex(attr);
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +01002004 if (index == kNotFound) {
Torne (Richard Coles)09380292014-02-21 12:17:33 +00002005 exceptionState.throwDOMException(NotFoundError, "The attribute was not found on this element.");
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002006 return nullptr;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002007 }
2008
Torne (Richard Coles)e5249552013-05-15 11:35:13 +01002009 RefPtr<Attr> guard(attr);
2010 detachAttrNodeAtIndex(attr, index);
2011 return guard.release();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002012}
2013
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00002014bool Element::parseAttributeName(QualifiedName& out, const AtomicString& namespaceURI, const AtomicString& qualifiedName, ExceptionState& exceptionState)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002015{
Torne (Richard Coles)a854de02013-12-18 16:25:25 +00002016 AtomicString prefix, localName;
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00002017 if (!Document::parseQualifiedName(qualifiedName, prefix, localName, exceptionState))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002018 return false;
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00002019 ASSERT(!exceptionState.hadException());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002020
2021 QualifiedName qName(prefix, localName, namespaceURI);
2022
2023 if (!Document::hasValidNamespaceForAttributes(qName)) {
Torne (Richard Coles)09380292014-02-21 12:17:33 +00002024 exceptionState.throwDOMException(NamespaceError, "'" + namespaceURI + "' is an invalid namespace for attributes.");
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002025 return false;
2026 }
2027
2028 out = qName;
2029 return true;
2030}
2031
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00002032void Element::setAttributeNS(const AtomicString& namespaceURI, const AtomicString& qualifiedName, const AtomicString& value, ExceptionState& exceptionState)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002033{
2034 QualifiedName parsedName = anyName;
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00002035 if (!parseAttributeName(parsedName, namespaceURI, qualifiedName, exceptionState))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002036 return;
2037 setAttribute(parsedName, value);
2038}
2039
2040void Element::removeAttributeInternal(size_t index, SynchronizationOfLazyAttribute inSynchronizationOfLazyAttribute)
2041{
2042 ASSERT_WITH_SECURITY_IMPLICATION(index < attributeCount());
2043
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002044 UniqueElementData& elementData = ensureUniqueElementData();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002045
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002046 QualifiedName name = elementData.attributeItem(index).name();
2047 AtomicString valueBeingRemoved = elementData.attributeItem(index).value();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002048
2049 if (!inSynchronizationOfLazyAttribute) {
2050 if (!valueBeingRemoved.isNull())
2051 willModifyAttribute(name, valueBeingRemoved, nullAtom);
2052 }
2053
2054 if (RefPtr<Attr> attrNode = attrIfExists(name))
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002055 detachAttrNodeFromElementWithValue(attrNode.get(), elementData.attributeItem(index).value());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002056
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002057 elementData.removeAttribute(index);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002058
2059 if (!inSynchronizationOfLazyAttribute)
2060 didRemoveAttribute(name);
2061}
2062
2063void Element::addAttributeInternal(const QualifiedName& name, const AtomicString& value, SynchronizationOfLazyAttribute inSynchronizationOfLazyAttribute)
2064{
2065 if (!inSynchronizationOfLazyAttribute)
2066 willModifyAttribute(name, nullAtom, value);
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002067 ensureUniqueElementData().addAttribute(name, value);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002068 if (!inSynchronizationOfLazyAttribute)
2069 didAddAttribute(name, value);
2070}
2071
2072void Element::removeAttribute(const AtomicString& name)
2073{
2074 if (!elementData())
2075 return;
2076
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01002077 AtomicString localName = shouldIgnoreAttributeCase() ? name.lower() : name;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002078 size_t index = elementData()->getAttributeItemIndex(localName, false);
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +01002079 if (index == kNotFound) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002080 if (UNLIKELY(localName == styleAttr) && elementData()->m_styleAttributeIsDirty && isStyledElement())
Ben Murdoch591b9582013-07-10 11:41:44 +01002081 removeAllInlineStyleProperties();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002082 return;
2083 }
2084
2085 removeAttributeInternal(index, NotInSynchronizationOfLazyAttribute);
2086}
2087
2088void Element::removeAttributeNS(const AtomicString& namespaceURI, const AtomicString& localName)
2089{
2090 removeAttribute(QualifiedName(nullAtom, localName, namespaceURI));
2091}
2092
2093PassRefPtr<Attr> Element::getAttributeNode(const AtomicString& localName)
2094{
2095 if (!elementData())
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002096 return nullptr;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002097 synchronizeAttribute(localName);
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01002098 const Attribute* attribute = elementData()->getAttributeItem(localName, shouldIgnoreAttributeCase());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002099 if (!attribute)
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002100 return nullptr;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002101 return ensureAttr(attribute->name());
2102}
2103
2104PassRefPtr<Attr> Element::getAttributeNodeNS(const AtomicString& namespaceURI, const AtomicString& localName)
2105{
2106 if (!elementData())
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002107 return nullptr;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002108 QualifiedName qName(nullAtom, localName, namespaceURI);
2109 synchronizeAttribute(qName);
2110 const Attribute* attribute = elementData()->getAttributeItem(qName);
2111 if (!attribute)
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002112 return nullptr;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002113 return ensureAttr(attribute->name());
2114}
2115
2116bool Element::hasAttribute(const AtomicString& localName) const
2117{
2118 if (!elementData())
2119 return false;
2120 synchronizeAttribute(localName);
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01002121 return elementData()->getAttributeItem(shouldIgnoreAttributeCase() ? localName.lower() : localName, false);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002122}
2123
2124bool 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)09380292014-02-21 12:17:33 +00002133void Element::focus(bool restorePreviousSelection, FocusType type)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002134{
2135 if (!inDocument())
2136 return;
2137
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002138 Document& doc = document();
2139 if (doc.focusedElement() == this)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002140 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 Murdoch591b9582013-07-10 11:41:44 +01002144 // that it can be updated soon after attach.
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002145 if (doc.haveStylesheetsLoaded()) {
2146 doc.updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002147 if (!isFocusable())
2148 return;
2149 }
2150
2151 if (!supportsFocus())
2152 return;
2153
2154 RefPtr<Node> protect;
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002155 if (Page* page = doc.page()) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002156 // 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)09380292014-02-21 12:17:33 +00002160 if (!page->focusController().setFocusedElement(this, doc.frame(), type))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002161 return;
2162 }
2163
2164 // Setting the focused node above might have invalidated the layout due to scripts.
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002165 doc.updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002166
2167 if (!isFocusable()) {
Torne (Richard Coles)43e75022014-03-21 14:26:12 +00002168 setElementFlag(NeedsFocusAppearanceUpdateSoonAfterAttach);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002169 return;
2170 }
Ben Murdoch591b9582013-07-10 11:41:44 +01002171
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002172 cancelFocusAppearanceUpdate();
2173 updateFocusAppearance(restorePreviousSelection);
2174}
2175
2176void Element::updateFocusAppearance(bool /*restorePreviousSelection*/)
2177{
2178 if (isRootEditableElement()) {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002179 LocalFrame* frame = document().frame();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002180 if (!frame)
2181 return;
Ben Murdoch591b9582013-07-10 11:41:44 +01002182
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002183 // When focusing an editable element in an iframe, don't reset the selection if it already contains a selection.
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002184 if (this == frame->selection().rootEditableElement())
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002185 return;
2186
2187 // FIXME: We should restore the previous selection if there is one.
2188 VisibleSelection newSelection = VisibleSelection(firstPositionInOrBeforeNode(this), DOWNSTREAM);
Torne (Richard Coles)1e202182013-10-18 15:46:42 +01002189 frame->selection().setSelection(newSelection);
2190 frame->selection().revealSelection();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002191 } else if (renderer() && !renderer()->isWidget())
2192 renderer()->scrollRectToVisible(boundingBox());
2193}
2194
2195void Element::blur()
2196{
2197 cancelFocusAppearanceUpdate();
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002198 if (treeScope().adjustedFocusedElement() == this) {
2199 Document& doc = document();
2200 if (doc.page())
2201 doc.page()->focusController().setFocusedElement(0, doc.frame());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002202 else
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002203 doc.setFocusedElement(nullptr);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002204 }
2205}
2206
Ben Murdochdf957042013-08-06 11:01:27 +01002207bool Element::isFocusable() const
2208{
2209 return inDocument() && supportsFocus() && !isInert() && rendererIsFocusable();
2210}
2211
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +01002212bool Element::isKeyboardFocusable() const
2213{
2214 return isFocusable() && tabIndex() >= 0;
2215}
2216
2217bool Element::isMouseFocusable() const
2218{
2219 return isFocusable();
2220}
2221
Torne (Richard Coles)09380292014-02-21 12:17:33 +00002222void Element::dispatchFocusEvent(Element* oldFocusedElement, FocusType)
Ben Murdoch02772c62013-07-26 10:21:05 +01002223{
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01002224 RefPtr<FocusEvent> event = FocusEvent::create(EventTypeNames::focus, false, false, document().domWindow(), 0, oldFocusedElement);
Ben Murdoch02772c62013-07-26 10:21:05 +01002225 EventDispatcher::dispatchEvent(this, FocusEventDispatchMediator::create(event.release()));
2226}
2227
2228void Element::dispatchBlurEvent(Element* newFocusedElement)
2229{
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01002230 RefPtr<FocusEvent> event = FocusEvent::create(EventTypeNames::blur, false, false, document().domWindow(), 0, newFocusedElement);
Ben Murdoch02772c62013-07-26 10:21:05 +01002231 EventDispatcher::dispatchEvent(this, BlurEventDispatchMediator::create(event.release()));
2232}
2233
2234void Element::dispatchFocusInEvent(const AtomicString& eventType, Element* oldFocusedElement)
2235{
2236 ASSERT(!NoEventDispatchAssertion::isEventDispatchForbidden());
Torne (Richard Coles)1e202182013-10-18 15:46:42 +01002237 ASSERT(eventType == EventTypeNames::focusin || eventType == EventTypeNames::DOMFocusIn);
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01002238 dispatchScopedEventDispatchMediator(FocusInEventDispatchMediator::create(FocusEvent::create(eventType, true, false, document().domWindow(), 0, oldFocusedElement)));
Ben Murdoch02772c62013-07-26 10:21:05 +01002239}
2240
2241void Element::dispatchFocusOutEvent(const AtomicString& eventType, Element* newFocusedElement)
2242{
2243 ASSERT(!NoEventDispatchAssertion::isEventDispatchForbidden());
Torne (Richard Coles)1e202182013-10-18 15:46:42 +01002244 ASSERT(eventType == EventTypeNames::focusout || eventType == EventTypeNames::DOMFocusOut);
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01002245 dispatchScopedEventDispatchMediator(FocusOutEventDispatchMediator::create(FocusEvent::create(eventType, true, false, document().domWindow(), 0, newFocusedElement)));
Ben Murdoch02772c62013-07-26 10:21:05 +01002246}
2247
Torne (Richard Coles)19cde672013-11-06 12:28:04 +00002248String Element::innerHTML() const
2249{
2250 return createMarkup(this, ChildrenOnly);
2251}
2252
2253String Element::outerHTML() const
2254{
2255 return createMarkup(this);
2256}
2257
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00002258void Element::setInnerHTML(const String& html, ExceptionState& exceptionState)
Torne (Richard Coles)19cde672013-11-06 12:28:04 +00002259{
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00002260 if (RefPtr<DocumentFragment> fragment = createFragmentForInnerOuterHTML(html, this, AllowScriptingContent, "innerHTML", exceptionState)) {
Torne (Richard Coles)19cde672013-11-06 12:28:04 +00002261 ContainerNode* container = this;
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002262 if (isHTMLTemplateElement(*this))
Torne (Richard Coles)19cde672013-11-06 12:28:04 +00002263 container = toHTMLTemplateElement(this)->content();
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00002264 replaceChildrenWithFragment(container, fragment.release(), exceptionState);
Torne (Richard Coles)19cde672013-11-06 12:28:04 +00002265 }
2266}
2267
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00002268void Element::setOuterHTML(const String& html, ExceptionState& exceptionState)
Torne (Richard Coles)19cde672013-11-06 12:28:04 +00002269{
2270 Node* p = parentNode();
Torne (Richard Coles)09380292014-02-21 12:17:33 +00002271 if (!p) {
2272 exceptionState.throwDOMException(NoModificationAllowedError, "This element has no parent node.");
Torne (Richard Coles)19cde672013-11-06 12:28:04 +00002273 return;
2274 }
Torne (Richard Coles)09380292014-02-21 12:17:33 +00002275 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)19cde672013-11-06 12:28:04 +00002280 RefPtr<Element> parent = toElement(p);
2281 RefPtr<Node> prev = previousSibling();
2282 RefPtr<Node> next = nextSibling();
2283
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00002284 RefPtr<DocumentFragment> fragment = createFragmentForInnerOuterHTML(html, parent.get(), AllowScriptingContent, "outerHTML", exceptionState);
2285 if (exceptionState.hadException())
Torne (Richard Coles)19cde672013-11-06 12:28:04 +00002286 return;
2287
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00002288 parent->replaceChild(fragment.release(), this, exceptionState);
Torne (Richard Coles)19cde672013-11-06 12:28:04 +00002289 RefPtr<Node> node = next ? next->previousSibling() : 0;
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00002290 if (!exceptionState.hadException() && node && node->isTextNode())
2291 mergeWithNextTextNode(node.release(), exceptionState);
Torne (Richard Coles)19cde672013-11-06 12:28:04 +00002292
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00002293 if (!exceptionState.hadException() && prev && prev->isTextNode())
2294 mergeWithNextTextNode(prev.release(), exceptionState);
Torne (Richard Coles)19cde672013-11-06 12:28:04 +00002295}
2296
Torne (Richard Coles)a854de02013-12-18 16:25:25 +00002297Node* 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()
2332static 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)09380292014-02-21 12:17:33 +00002348Element* 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
2360void 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)a854de02013-12-18 16:25:25 +00002366void 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)53e740f2013-05-09 18:38:43 +01002378String Element::innerText()
2379{
2380 // We need to update layout, since plainText uses line boxes in the render tree.
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002381 document().updateLayoutIgnorePendingStylesheets();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002382
2383 if (!renderer())
2384 return textContent(true);
2385
2386 return plainText(rangeOfContents(const_cast<Element*>(this)).get());
2387}
2388
2389String 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 Murdoch591b9582013-07-10 11:41:44 +01002399String 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)09380292014-02-21 12:17:33 +00002439const AtomicString& Element::shadowPseudoId() const
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002440{
Ben Murdoch07a852d2014-03-31 11:51:52 +01002441 if (ShadowRoot* root = containingShadowRoot()) {
2442 if (root->type() == ShadowRoot::UserAgentShadowRoot)
2443 return fastGetAttribute(pseudoAttr);
2444 }
2445 return nullAtom;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002446}
2447
Torne (Richard Coles)09380292014-02-21 12:17:33 +00002448void Element::setShadowPseudoId(const AtomicString& id)
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +01002449{
Ben Murdoch07a852d2014-03-31 11:51:52 +01002450 ASSERT(CSSSelector::parsePseudoType(id) == CSSSelector::PseudoWebKitCustomElement || CSSSelector::parsePseudoType(id) == CSSSelector::PseudoUserAgentCustomElement);
Torne (Richard Coles)09380292014-02-21 12:17:33 +00002451 setAttribute(pseudoAttr, id);
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +01002452}
2453
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00002454bool Element::isInDescendantTreeOf(const Element* shadowHost) const
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +01002455{
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00002456 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)f5e4ad52013-08-05 13:57:57 +01002467}
2468
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002469LayoutSize Element::minimumSizeForResizing() const
2470{
2471 return hasRareData() ? elementRareData()->minimumSizeForResizing() : defaultMinimumSizeForResizing();
2472}
2473
2474void Element::setMinimumSizeForResizing(const LayoutSize& size)
2475{
2476 if (!hasRareData() && size == defaultMinimumSizeForResizing())
2477 return;
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01002478 ensureElementRareData().setMinimumSizeForResizing(size);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002479}
2480
2481RenderStyle* 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)f79f16f2013-10-31 11:16:44 +00002497 if (!inActiveDocument())
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002498 // 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)bfe35902013-10-22 16:41:51 +01002502 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)53e740f2013-05-09 18:38:43 +01002506}
2507
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002508void Element::setChildIndex(unsigned index)
2509{
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01002510 ElementRareData& rareData = ensureElementRareData();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002511 if (RenderStyle* style = renderStyle())
2512 style->setUnique();
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01002513 rareData.setChildIndex(index);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002514}
2515
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002516unsigned Element::rareDataChildIndex() const
2517{
2518 ASSERT(hasRareData());
2519 return elementRareData()->childIndex();
2520}
2521
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002522AtomicString 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
2547Locale& Element::locale() const
2548{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002549 return document().getCachedLocale(computeInheritedLanguage());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002550}
2551
2552void Element::cancelFocusAppearanceUpdate()
2553{
2554 if (hasRareData())
Torne (Richard Coles)43e75022014-03-21 14:26:12 +00002555 clearElementFlag(NeedsFocusAppearanceUpdateSoonAfterAttach);
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002556 if (document().focusedElement() == this)
2557 document().cancelFocusAppearanceUpdate();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002558}
2559
2560void Element::normalizeAttributes()
2561{
2562 if (!hasAttributes())
2563 return;
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002564 // attributeCount() cannot be cached before the loop because the attributes
2565 // list is altered while iterating.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002566 for (unsigned i = 0; i < attributeCount(); ++i) {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002567 if (RefPtr<Attr> attr = attrIfExists(attributeItem(i).name()))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002568 attr->normalize();
2569 }
2570}
2571
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002572void Element::updatePseudoElement(PseudoId pseudoId, StyleRecalcChange change)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002573{
Torne (Richard Coles)09380292014-02-21 12:17:33 +00002574 ASSERT(!needsStyleRecalc());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002575 PseudoElement* element = pseudoElement(pseudoId);
Torne (Richard Coles)09380292014-02-21 12:17:33 +00002576 if (element && (change == UpdatePseudoElements || element->shouldCallRecalcStyle(change))) {
2577
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00002578 // 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)53e740f2013-05-09 18:38:43 +01002583 // 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)51b29062013-11-28 11:56:03 +00002585 // FIXME: We should figure out the right text sibling to pass.
Torne (Richard Coles)09380292014-02-21 12:17:33 +00002586 element->recalcStyle(change == UpdatePseudoElements ? Force : change);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002587
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)d5428f32014-03-18 10:21:16 +00002593 elementRareData()->setPseudoElement(pseudoId, nullptr);
Torne (Richard Coles)09380292014-02-21 12:17:33 +00002594 } else if (change >= UpdatePseudoElements) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002595 createPseudoElementIfNeeded(pseudoId);
Torne (Richard Coles)09380292014-02-21 12:17:33 +00002596 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002597}
2598
2599void Element::createPseudoElementIfNeeded(PseudoId pseudoId)
2600{
Torne (Richard Coles)09380292014-02-21 12:17:33 +00002601 if (isPseudoElement())
2602 return;
Torne (Richard Coles)9bbd2f52013-09-19 22:37:05 +01002603
Ben Murdoch07a852d2014-03-31 11:51:52 +01002604 // 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)09380292014-02-21 12:17:33 +00002607 if (!element)
2608 return;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002609
Ben Murdoche69819b2013-07-17 14:56:49 +01002610 if (pseudoId == BACKDROP)
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002611 document().addToTopLayer(element.get(), this);
Torne (Richard Coles)19cde672013-11-06 12:28:04 +00002612 element->insertedInto(this);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002613 element->attach();
Ben Murdoche69819b2013-07-17 14:56:49 +01002614
Torne (Richard Coles)19cde672013-11-06 12:28:04 +00002615 InspectorInstrumentation::pseudoElementCreated(element.get());
2616
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01002617 ensureElementRareData().setPseudoElement(pseudoId, element.release());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002618}
2619
2620PseudoElement* Element::pseudoElement(PseudoId pseudoId) const
2621{
2622 return hasRareData() ? elementRareData()->pseudoElement(pseudoId) : 0;
2623}
2624
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002625RenderObject* Element::pseudoElementRenderer(PseudoId pseudoId) const
2626{
2627 if (PseudoElement* element = pseudoElement(pseudoId))
2628 return element->renderer();
2629 return 0;
2630}
2631
Torne (Richard Coles)09380292014-02-21 12:17:33 +00002632bool Element::matches(const String& selectors, ExceptionState& exceptionState)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002633{
Torne (Richard Coles)09380292014-02-21 12:17:33 +00002634 SelectorQuery* selectorQuery = document().selectorQueryCache().add(AtomicString(selectors), document(), exceptionState);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002635 if (!selectorQuery)
2636 return false;
Torne (Richard Coles)19cde672013-11-06 12:28:04 +00002637 return selectorQuery->matches(*this);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002638}
2639
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002640DOMTokenList& Element::classList()
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002641{
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01002642 ElementRareData& rareData = ensureElementRareData();
2643 if (!rareData.classList())
2644 rareData.setClassList(ClassList::create(this));
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002645 return *rareData.classList();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002646}
2647
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002648DOMStringMap& Element::dataset()
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002649{
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01002650 ElementRareData& rareData = ensureElementRareData();
2651 if (!rareData.dataset())
2652 rareData.setDataset(DatasetDOMStringMap::create(this));
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002653 return *rareData.dataset();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002654}
2655
2656KURL 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)8abfc582013-09-12 12:10:38 +01002664 return document().completeURL(stripLeadingAndTrailingHTMLSpaces(getAttribute(name)));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002665}
2666
2667KURL 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)8abfc582013-09-12 12:10:38 +01002678 return document().completeURL(value);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002679}
2680
2681int Element::getIntegralAttribute(const QualifiedName& attributeName) const
2682{
2683 return getAttribute(attributeName).string().toInt();
2684}
2685
2686void Element::setIntegralAttribute(const QualifiedName& attributeName, int value)
2687{
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00002688 setAttribute(attributeName, AtomicString::number(value));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002689}
2690
2691unsigned Element::getUnsignedIntegralAttribute(const QualifiedName& attributeName) const
2692{
2693 return getAttribute(attributeName).string().toUInt();
2694}
2695
2696void Element::setUnsignedIntegralAttribute(const QualifiedName& attributeName, unsigned value)
2697{
Torne (Richard Coles)09380292014-02-21 12:17:33 +00002698 // 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)51b29062013-11-28 11:56:03 +00002703 setAttribute(attributeName, AtomicString::number(value));
2704}
2705
2706double Element::getFloatingPointAttribute(const QualifiedName& attributeName, double fallbackValue) const
2707{
2708 return parseToDoubleForNumberType(getAttribute(attributeName), fallbackValue);
2709}
2710
2711void Element::setFloatingPointAttribute(const QualifiedName& attributeName, double value)
2712{
2713 setAttribute(attributeName, AtomicString::number(value));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002714}
2715
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002716void Element::webkitRequestFullscreen()
2717{
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002718 FullscreenElementStack::from(document()).requestFullScreenForElement(this, ALLOW_KEYBOARD_INPUT, FullscreenElementStack::EnforceIFrameAllowFullScreenRequirement);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002719}
2720
2721void Element::webkitRequestFullScreen(unsigned short flags)
2722{
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002723 FullscreenElementStack::from(document()).requestFullScreenForElement(this, (flags | LEGACY_MOZILLA_REQUEST), FullscreenElementStack::EnforceIFrameAllowFullScreenRequirement);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002724}
2725
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002726void Element::setContainsFullScreenElement(bool flag)
2727{
Torne (Richard Coles)43e75022014-03-21 14:26:12 +00002728 setElementFlag(ContainsFullScreenElement, flag);
Ben Murdoche69819b2013-07-17 14:56:49 +01002729 setNeedsStyleRecalc(SubtreeStyleChange);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002730}
2731
2732static Element* parentCrossingFrameBoundaries(Element* element)
2733{
2734 ASSERT(element);
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002735 return element->parentElement() ? element->parentElement() : element->document().ownerElement();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002736}
2737
2738void Element::setContainsFullScreenElementOnAncestorsCrossingFrameBoundaries(bool flag)
2739{
2740 Element* element = this;
2741 while ((element = parentCrossingFrameBoundaries(element)))
2742 element->setContainsFullScreenElement(flag);
2743}
2744
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002745void Element::setIsInTopLayer(bool inTopLayer)
2746{
2747 if (isInTopLayer() == inTopLayer)
2748 return;
Torne (Richard Coles)43e75022014-03-21 14:26:12 +00002749 setElementFlag(IsInTopLayer, inTopLayer);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002750
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)93ac45c2013-05-29 14:40:20 +01002753 lazyReattachIfAttached();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002754}
2755
2756void Element::webkitRequestPointerLock()
2757{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002758 if (document().page())
2759 document().page()->pointerLockController().requestPointerLock(this);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002760}
2761
2762SpellcheckAttributeState Element::spellcheckAttributeState() const
2763{
Torne (Richard Coles)09380292014-02-21 12:17:33 +00002764 const AtomicString& value = fastGetAttribute(spellcheckAttr);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002765 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
2775bool 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)53e740f2013-05-09 18:38:43 +01002791#ifndef NDEBUG
2792bool Element::fastAttributeLookupAllowed(const QualifiedName& name) const
2793{
2794 if (name == HTMLNames::styleAttr)
2795 return false;
2796
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002797 if (isSVGElement())
Torne (Richard Coles)09380292014-02-21 12:17:33 +00002798 return !toSVGElement(this)->isAnimatableAttribute(name);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002799
2800 return true;
2801}
2802#endif
2803
2804#ifdef DUMP_NODE_STATISTICS
2805bool Element::hasNamedNodeMap() const
2806{
2807 return hasRareData() && elementRareData()->attributeMap();
2808}
2809#endif
2810
Ben Murdoch00d3faa2013-08-14 11:52:03 +01002811inline void Element::updateName(const AtomicString& oldName, const AtomicString& newName)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002812{
Ben Murdoch00d3faa2013-08-14 11:52:03 +01002813 if (!inDocument() || isInShadowTree())
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002814 return;
2815
2816 if (oldName == newName)
2817 return;
2818
Ben Murdochdf957042013-08-06 11:01:27 +01002819 if (shouldRegisterAsNamedItem())
2820 updateNamedItemRegistration(oldName, newName);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002821}
2822
Ben Murdoch00d3faa2013-08-14 11:52:03 +01002823inline void Element::updateId(const AtomicString& oldId, const AtomicString& newId)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002824{
2825 if (!isInTreeScope())
2826 return;
2827
2828 if (oldId == newId)
2829 return;
2830
Ben Murdoch00d3faa2013-08-14 11:52:03 +01002831 updateId(treeScope(), oldId, newId);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002832}
2833
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002834inline void Element::updateId(TreeScope& scope, const AtomicString& oldId, const AtomicString& newId)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002835{
2836 ASSERT(isInTreeScope());
2837 ASSERT(oldId != newId);
2838
2839 if (!oldId.isEmpty())
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002840 scope.removeElementById(oldId, this);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002841 if (!newId.isEmpty())
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002842 scope.addElementById(newId, this);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002843
Ben Murdochdf957042013-08-06 11:01:27 +01002844 if (shouldRegisterAsExtraNamedItem())
2845 updateExtraNamedItemRegistration(oldId, newId);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002846}
2847
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002848void Element::updateLabel(TreeScope& scope, const AtomicString& oldForAttributeValue, const AtomicString& newForAttributeValue)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002849{
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002850 ASSERT(isHTMLLabelElement(this));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002851
2852 if (!inDocument())
2853 return;
2854
2855 if (oldForAttributeValue == newForAttributeValue)
2856 return;
2857
2858 if (!oldForAttributeValue.isEmpty())
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002859 scope.removeLabel(oldForAttributeValue, toHTMLLabelElement(this));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002860 if (!newForAttributeValue.isEmpty())
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002861 scope.addLabel(newForAttributeValue, toHTMLLabelElement(this));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002862}
2863
2864void Element::willModifyAttribute(const QualifiedName& name, const AtomicString& oldValue, const AtomicString& newValue)
2865{
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002866 if (isIdAttributeName(name)) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002867 updateId(oldValue, newValue);
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002868 } else if (name == HTMLNames::nameAttr) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002869 updateName(oldValue, newValue);
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002870 } else if (name == HTMLNames::forAttr && isHTMLLabelElement(*this)) {
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002871 TreeScope& scope = treeScope();
2872 if (scope.shouldCacheLabelsByForAttribute())
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002873 updateLabel(scope, oldValue, newValue);
2874 }
2875
2876 if (oldValue != newValue) {
Ben Murdoch07a852d2014-03-31 11:51:52 +01002877 if (inActiveDocument())
Ben Murdochaafa69c2014-04-03 12:30:15 +01002878 document().ensureStyleResolver().ensureUpdatedRuleFeatureSet().scheduleStyleInvalidationForAttributeChange(name, *this);
Ben Murdoche69819b2013-07-17 14:56:49 +01002879
2880 if (isUpgradedCustomElement())
Ben Murdoch83750172013-07-24 10:36:59 +01002881 CustomElement::attributeDidChange(this, name.localName(), oldValue, newValue);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002882 }
2883
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +00002884 if (OwnPtr<MutationObserverInterestGroup> recipients = MutationObserverInterestGroup::createForAttributesMutation(*this, name))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002885 recipients->enqueueMutationRecord(MutationRecord::createAttributes(this, name, oldValue));
2886
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01002887 InspectorInstrumentation::willModifyDOMAttr(this, oldValue, newValue);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002888}
2889
2890void Element::didAddAttribute(const QualifiedName& name, const AtomicString& value)
2891{
2892 attributeChanged(name, value);
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01002893 InspectorInstrumentation::didModifyDOMAttr(this, name.localName(), value);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002894 dispatchSubtreeModifiedEvent();
2895}
2896
2897void Element::didModifyAttribute(const QualifiedName& name, const AtomicString& value)
2898{
2899 attributeChanged(name, value);
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01002900 InspectorInstrumentation::didModifyDOMAttr(this, name.localName(), value);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002901 // Do not dispatch a DOMSubtreeModified event here; see bug 81141.
2902}
2903
2904void Element::didRemoveAttribute(const QualifiedName& name)
2905{
2906 attributeChanged(name, nullAtom);
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01002907 InspectorInstrumentation::didRemoveDOMAttr(this, name.localName());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002908 dispatchSubtreeModifiedEvent();
2909}
2910
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002911static 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
2928static 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)1e202182013-10-18 15:46:42 +01002938void Element::didMoveToNewDocument(Document& oldDocument)
Ben Murdoche69819b2013-07-17 14:56:49 +01002939{
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)1e202182013-10-18 15:46:42 +01002945 if (oldDocument.inQuirksMode() != document().inQuirksMode()) {
Ben Murdoche69819b2013-07-17 14:56:49 +01002946 if (hasID())
2947 setIdAttribute(getIdAttribute());
2948 if (hasClass())
2949 setAttribute(HTMLNames::classAttr, getClassAttribute());
2950 }
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002951
2952 if (needsURLResolutionForInlineStyle(*this, oldDocument, document()))
2953 reResolveURLsInInlineStyle(document(), ensureMutableInlineStyle());
Ben Murdoche69819b2013-07-17 14:56:49 +01002954}
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002955
Ben Murdochdf957042013-08-06 11:01:27 +01002956void Element::updateNamedItemRegistration(const AtomicString& oldName, const AtomicString& newName)
2957{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002958 if (!document().isHTMLDocument())
Ben Murdoch00d3faa2013-08-14 11:52:03 +01002959 return;
Ben Murdochdf957042013-08-06 11:01:27 +01002960
2961 if (!oldName.isEmpty())
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002962 toHTMLDocument(document()).removeNamedItem(oldName);
Ben Murdochdf957042013-08-06 11:01:27 +01002963
2964 if (!newName.isEmpty())
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002965 toHTMLDocument(document()).addNamedItem(newName);
Ben Murdochdf957042013-08-06 11:01:27 +01002966}
2967
2968void Element::updateExtraNamedItemRegistration(const AtomicString& oldId, const AtomicString& newId)
2969{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002970 if (!document().isHTMLDocument())
Ben Murdoch00d3faa2013-08-14 11:52:03 +01002971 return;
Ben Murdochdf957042013-08-06 11:01:27 +01002972
2973 if (!oldId.isEmpty())
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002974 toHTMLDocument(document()).removeExtraNamedItem(oldId);
Ben Murdochdf957042013-08-06 11:01:27 +01002975
2976 if (!newId.isEmpty())
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01002977 toHTMLDocument(document()).addExtraNamedItem(newId);
Ben Murdochdf957042013-08-06 11:01:27 +01002978}
2979
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002980PassRefPtr<HTMLCollection> Element::ensureCachedHTMLCollection(CollectionType type)
2981{
2982 if (HTMLCollection* collection = cachedHTMLCollection(type))
2983 return collection;
2984
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002985 if (type == TableRows) {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002986 ASSERT(isHTMLTableElement(this));
2987 return ensureRareData().ensureNodeLists().addCache<HTMLTableRowsCollection>(*this, type);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002988 } else if (type == SelectOptions) {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002989 ASSERT(isHTMLSelectElement(this));
2990 return ensureRareData().ensureNodeLists().addCache<HTMLOptionsCollection>(*this, type);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002991 } else if (type == FormControls) {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002992 ASSERT(isHTMLFormElement(this) || isHTMLFieldSetElement(this));
2993 return ensureRareData().ensureNodeLists().addCache<HTMLFormControlsCollection>(*this, type);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002994 }
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00002995 return ensureRareData().ensureNodeLists().addCache<HTMLCollection>(*this, type);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01002996}
2997
Ben Murdoch591b9582013-07-10 11:41:44 +01002998void Element::scheduleLayerUpdate()
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +01002999{
Ben Murdoch07a852d2014-03-31 11:51:52 +01003000 document().scheduleLayerUpdate(*this);
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +01003001}
3002
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01003003HTMLCollection* Element::cachedHTMLCollection(CollectionType type)
3004{
Torne (Richard Coles)09380292014-02-21 12:17:33 +00003005 return hasRareData() && rareData()->nodeLists() ? rareData()->nodeLists()->cached<HTMLCollection>(type) : 0;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01003006}
3007
3008IntSize Element::savedLayerScrollOffset() const
3009{
3010 return hasRareData() ? elementRareData()->savedLayerScrollOffset() : IntSize();
3011}
3012
3013void Element::setSavedLayerScrollOffset(const IntSize& size)
3014{
3015 if (size.isZero() && !hasRareData())
3016 return;
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01003017 ensureElementRareData().setSavedLayerScrollOffset(size);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01003018}
3019
3020PassRefPtr<Attr> Element::attrIfExists(const QualifiedName& name)
3021{
3022 if (AttrNodeList* attrNodeList = attrNodeListForElement(this))
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01003023 return findAttrNodeInList(*attrNodeList, name);
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003024 return nullptr;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01003025}
3026
3027PassRefPtr<Attr> Element::ensureAttr(const QualifiedName& name)
3028{
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01003029 AttrNodeList& attrNodeList = ensureAttrNodeListForElement(this);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01003030 RefPtr<Attr> attrNode = findAttrNodeInList(attrNodeList, name);
3031 if (!attrNode) {
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01003032 attrNode = Attr::create(*this, name);
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +00003033 treeScope().adoptIfNeeded(*attrNode);
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01003034 attrNodeList.append(attrNode);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01003035 }
3036 return attrNode.release();
3037}
3038
3039void 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
3056void Element::detachAllAttrNodesFromElement()
3057{
3058 AttrNodeList* attrNodeList = attrNodeListForElement(this);
3059 ASSERT(attrNodeList);
3060
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003061 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)53e740f2013-05-09 18:38:43 +01003066 }
3067
3068 removeAttrNodeListForElement(this);
3069}
3070
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01003071void Element::willRecalcStyle(StyleRecalcChange)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01003072{
3073 ASSERT(hasCustomStyleCallbacks());
3074}
3075
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01003076void Element::didRecalcStyle(StyleRecalcChange)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01003077{
3078 ASSERT(hasCustomStyleCallbacks());
3079}
3080
3081
3082PassRefPtr<RenderStyle> Element::customStyleForRenderer()
3083{
3084 ASSERT(hasCustomStyleCallbacks());
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003085 return nullptr;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01003086}
3087
3088void 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 Murdoche69819b2013-07-17 14:56:49 +01003111 // 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)8abfc582013-09-12 12:10:38 +01003115 ownerDocumentsHaveDifferentCaseSensitivity = other.document().inQuirksMode() != document().inQuirksMode();
Ben Murdoche69819b2013-07-17 14:56:49 +01003116
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01003117 // If 'other' has a mutable ElementData, convert it to an immutable one so we can share it between both elements.
Torne (Richard Coles)09380292014-02-21 12:17:33 +00003118 // 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)53e740f2013-05-09 18:38:43 +01003119 if (other.m_elementData->isUnique()
Ben Murdoche69819b2013-07-17 14:56:49 +01003120 && !ownerDocumentsHaveDifferentCaseSensitivity
Torne (Richard Coles)09380292014-02-21 12:17:33 +00003121 && !other.m_elementData->presentationAttributeStyle())
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01003122 const_cast<Element&>(other).m_elementData = static_cast<const UniqueElementData*>(other.m_elementData.get())->makeShareableCopy();
3123
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003124 if (!other.m_elementData->isUnique() && !ownerDocumentsHaveDifferentCaseSensitivity && !needsURLResolutionForInlineStyle(other, other.document(), document()))
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01003125 m_elementData = other.m_elementData;
3126 else
3127 m_elementData = other.m_elementData->makeUniqueCopy();
3128
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003129 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)53e740f2013-05-09 18:38:43 +01003133 }
3134}
3135
3136void Element::cloneDataFromElement(const Element& other)
3137{
3138 cloneAttributesFromElement(other);
3139 copyNonAttributePropertiesFromElement(other);
3140}
3141
3142void 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)d5428f32014-03-18 10:21:16 +00003152InputMethodContext& Element::inputMethodContext()
Torne (Richard Coles)81a51572013-05-13 16:52:28 +01003153{
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01003154 return ensureElementRareData().ensureInputMethodContext(toHTMLElement(this));
Torne (Richard Coles)81a51572013-05-13 16:52:28 +01003155}
3156
Torne (Richard Coles)19cde672013-11-06 12:28:04 +00003157bool Element::hasInputMethodContext() const
3158{
3159 return hasRareData() && elementRareData()->hasInputMethodContext();
3160}
3161
Ben Murdoch591b9582013-07-10 11:41:44 +01003162void Element::synchronizeStyleAttributeInternal() const
3163{
3164 ASSERT(isStyledElement());
3165 ASSERT(elementData());
3166 ASSERT(elementData()->m_styleAttributeIsDirty);
3167 elementData()->m_styleAttributeIsDirty = false;
Torne (Richard Coles)09380292014-02-21 12:17:33 +00003168 const StylePropertySet* inlineStyle = this->inlineStyle();
3169 const_cast<Element*>(this)->setSynchronizedLazyAttribute(styleAttr,
3170 inlineStyle ? AtomicString(inlineStyle->asText()) : nullAtom);
Ben Murdoch591b9582013-07-10 11:41:44 +01003171}
3172
3173CSSStyleDeclaration* Element::style()
3174{
3175 if (!isStyledElement())
3176 return 0;
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003177 return &ensureElementRareData().ensureInlineCSSStyleDeclaration(this);
Ben Murdoch591b9582013-07-10 11:41:44 +01003178}
3179
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003180MutableStylePropertySet& Element::ensureMutableInlineStyle()
Ben Murdoch591b9582013-07-10 11:41:44 +01003181{
3182 ASSERT(isStyledElement());
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003183 RefPtr<StylePropertySet>& inlineStyle = ensureUniqueElementData().m_inlineStyle;
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +00003184 if (!inlineStyle) {
3185 CSSParserMode mode = (!isHTMLElement() || document().inQuirksMode()) ? HTMLQuirksMode : HTMLStandardMode;
3186 inlineStyle = MutableStylePropertySet::create(mode);
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00003187 } else if (!inlineStyle->isMutable()) {
Ben Murdoch591b9582013-07-10 11:41:44 +01003188 inlineStyle = inlineStyle->mutableCopy();
Torne (Richard Coles)51b29062013-11-28 11:56:03 +00003189 }
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003190 return *toMutableStylePropertySet(inlineStyle);
Ben Murdoch591b9582013-07-10 11:41:44 +01003191}
3192
Torne (Richard Coles)09380292014-02-21 12:17:33 +00003193void Element::clearMutableInlineStyleIfEmpty()
Ben Murdoch591b9582013-07-10 11:41:44 +01003194{
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003195 if (ensureMutableInlineStyle().isEmpty()) {
3196 ensureUniqueElementData().m_inlineStyle.clear();
Torne (Richard Coles)09380292014-02-21 12:17:33 +00003197 }
Ben Murdoch591b9582013-07-10 11:41:44 +01003198}
3199
3200inline 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)09380292014-02-21 12:17:33 +00003215 inlineStyle = BisonCSSParser::parseInlineStyleDeclaration(newStyleString, this);
Ben Murdoch591b9582013-07-10 11:41:44 +01003216 } else {
3217 ASSERT(inlineStyle->isMutable());
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003218 static_pointer_cast<MutableStylePropertySet>(inlineStyle)->parseDeclaration(newStyleString, document().elementSheet().contents());
Ben Murdoch591b9582013-07-10 11:41:44 +01003219 }
3220}
3221
3222void Element::styleAttributeChanged(const AtomicString& newStyleString, AttributeModificationReason modificationReason)
3223{
3224 ASSERT(isStyledElement());
3225 WTF::OrdinalNumber startLineNumber = WTF::OrdinalNumber::beforeFirst();
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01003226 if (document().scriptableDocumentParser() && !document().isInDocumentWrite())
3227 startLineNumber = document().scriptableDocumentParser()->lineNumber();
Ben Murdoch591b9582013-07-10 11:41:44 +01003228
3229 if (newStyleString.isNull()) {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003230 ensureUniqueElementData().m_inlineStyle.clear();
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +01003231 } else if (modificationReason == ModifiedByCloning || document().contentSecurityPolicy()->allowInlineStyle(document().url(), startLineNumber)) {
Ben Murdoch591b9582013-07-10 11:41:44 +01003232 setInlineStyleFromString(newStyleString);
3233 }
3234
3235 elementData()->m_styleAttributeIsDirty = false;
3236
Ben Murdoche69819b2013-07-17 14:56:49 +01003237 setNeedsStyleRecalc(LocalStyleChange);
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01003238 InspectorInstrumentation::didInvalidateStyleAttr(this);
Ben Murdoch591b9582013-07-10 11:41:44 +01003239}
3240
3241void Element::inlineStyleChanged()
3242{
3243 ASSERT(isStyledElement());
Ben Murdoche69819b2013-07-17 14:56:49 +01003244 setNeedsStyleRecalc(LocalStyleChange);
Ben Murdoch591b9582013-07-10 11:41:44 +01003245 ASSERT(elementData());
3246 elementData()->m_styleAttributeIsDirty = true;
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01003247 InspectorInstrumentation::didInvalidateStyleAttr(this);
Ben Murdoch591b9582013-07-10 11:41:44 +01003248}
3249
3250bool Element::setInlineStyleProperty(CSSPropertyID propertyID, CSSValueID identifier, bool important)
3251{
3252 ASSERT(isStyledElement());
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003253 ensureMutableInlineStyle().setProperty(propertyID, cssValuePool().createIdentifierValue(identifier), important);
Ben Murdoch591b9582013-07-10 11:41:44 +01003254 inlineStyleChanged();
3255 return true;
3256}
3257
3258bool Element::setInlineStyleProperty(CSSPropertyID propertyID, CSSPropertyID identifier, bool important)
3259{
3260 ASSERT(isStyledElement());
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003261 ensureMutableInlineStyle().setProperty(propertyID, cssValuePool().createIdentifierValue(identifier), important);
Ben Murdoch591b9582013-07-10 11:41:44 +01003262 inlineStyleChanged();
3263 return true;
3264}
3265
3266bool Element::setInlineStyleProperty(CSSPropertyID propertyID, double value, CSSPrimitiveValue::UnitTypes unit, bool important)
3267{
3268 ASSERT(isStyledElement());
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003269 ensureMutableInlineStyle().setProperty(propertyID, cssValuePool().createValue(value, unit), important);
Ben Murdoch591b9582013-07-10 11:41:44 +01003270 inlineStyleChanged();
3271 return true;
3272}
3273
3274bool Element::setInlineStyleProperty(CSSPropertyID propertyID, const String& value, bool important)
3275{
3276 ASSERT(isStyledElement());
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003277 bool changes = ensureMutableInlineStyle().setProperty(propertyID, value, important, document().elementSheet().contents());
Ben Murdoch591b9582013-07-10 11:41:44 +01003278 if (changes)
3279 inlineStyleChanged();
3280 return changes;
3281}
3282
3283bool Element::removeInlineStyleProperty(CSSPropertyID propertyID)
3284{
3285 ASSERT(isStyledElement());
3286 if (!inlineStyle())
3287 return false;
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003288 bool changes = ensureMutableInlineStyle().removeProperty(propertyID);
Ben Murdoch591b9582013-07-10 11:41:44 +01003289 if (changes)
3290 inlineStyleChanged();
3291 return changes;
3292}
3293
3294void Element::removeAllInlineStyleProperties()
3295{
3296 ASSERT(isStyledElement());
Torne (Richard Coles)09380292014-02-21 12:17:33 +00003297 if (!inlineStyle())
Ben Murdoch591b9582013-07-10 11:41:44 +01003298 return;
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003299 ensureMutableInlineStyle().clear();
Ben Murdoch591b9582013-07-10 11:41:44 +01003300 inlineStyleChanged();
3301}
3302
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01003303void Element::updatePresentationAttributeStyle()
Ben Murdoch591b9582013-07-10 11:41:44 +01003304{
Ben Murdoch591b9582013-07-10 11:41:44 +01003305 // ShareableElementData doesn't store presentation attribute style, so make sure we have a UniqueElementData.
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003306 UniqueElementData& elementData = ensureUniqueElementData();
3307 elementData.m_presentationAttributeStyleIsDirty = false;
3308 elementData.m_presentationAttributeStyle = computePresentationAttributeStyle(*this);
Ben Murdoch591b9582013-07-10 11:41:44 +01003309}
3310
3311void Element::addPropertyToPresentationAttributeStyle(MutableStylePropertySet* style, CSSPropertyID propertyID, CSSValueID identifier)
3312{
3313 ASSERT(isStyledElement());
3314 style->setProperty(propertyID, cssValuePool().createIdentifierValue(identifier));
3315}
3316
3317void 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
3323void Element::addPropertyToPresentationAttributeStyle(MutableStylePropertySet* style, CSSPropertyID propertyID, const String& value)
3324{
3325 ASSERT(isStyledElement());
Torne (Richard Coles)1e202182013-10-18 15:46:42 +01003326 style->setProperty(propertyID, value, false);
Ben Murdoch591b9582013-07-10 11:41:44 +01003327}
3328
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01003329bool Element::supportsStyleSharing() const
3330{
Torne (Richard Coles)09380292014-02-21 12:17:33 +00003331 if (!isStyledElement() || !parentOrShadowHostElement())
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01003332 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)a854de02013-12-18 16:25:25 +00003339 if (hasID() && document().ensureStyleResolver().hasRulesForId(idForStyleResolution()))
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01003340 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)09380292014-02-21 12:17:33 +00003349 if (!parentOrShadowHostElement()->childrenSupportStyleSharing())
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01003350 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)bfe35902013-10-22 16:41:51 +01003359 // 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)43e75022014-03-21 14:26:12 +00003362 if (isHTMLFrameElementBase(*this)
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +00003363 || isHTMLEmbedElement(*this)
3364 || isHTMLObjectElement(*this)
3365 || isHTMLAppletElement(*this)
3366 || isHTMLCanvasElement(*this))
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01003367 return false;
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +01003368 if (FullscreenElementStack::isActiveFullScreenElement(this))
3369 return false;
3370 return true;
3371}
3372
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01003373} // namespace WebCore