2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "core/dom/ElementData.h"
34 #include "core/css/StylePropertySet.h"
35 #include "core/dom/QualifiedName.h"
36 #include "wtf/Vector.h"
40 struct SameSizeAsElementData
: public RefCountedWillBeGarbageCollectedFinalized
<SameSizeAsElementData
> {
45 static_assert(sizeof(ElementData
) == sizeof(SameSizeAsElementData
), "ElementData should stay small");
47 static size_t sizeForShareableElementDataWithAttributeCount(unsigned count
)
49 return sizeof(ShareableElementData
) + sizeof(Attribute
) * count
;
52 ElementData::ElementData()
55 , m_presentationAttributeStyleIsDirty(false)
56 , m_styleAttributeIsDirty(false)
57 , m_animatedSVGAttributesAreDirty(false)
61 ElementData::ElementData(unsigned arraySize
)
63 , m_arraySize(arraySize
)
64 , m_presentationAttributeStyleIsDirty(false)
65 , m_styleAttributeIsDirty(false)
66 , m_animatedSVGAttributesAreDirty(false)
70 ElementData::ElementData(const ElementData
& other
, bool isUnique
)
71 : m_isUnique(isUnique
)
72 , m_arraySize(isUnique
? 0 : other
.attributes().size())
73 , m_presentationAttributeStyleIsDirty(other
.m_presentationAttributeStyleIsDirty
)
74 , m_styleAttributeIsDirty(other
.m_styleAttributeIsDirty
)
75 , m_animatedSVGAttributesAreDirty(other
.m_animatedSVGAttributesAreDirty
)
76 , m_classNames(other
.m_classNames
)
77 , m_idForStyleResolution(other
.m_idForStyleResolution
)
79 // NOTE: The inline style is copied by the subclass copy constructor since we don't know what to do with it here.
83 void ElementData::finalizeGarbageCollectedObject()
86 toUniqueElementData(this)->~UniqueElementData();
88 toShareableElementData(this)->~ShareableElementData();
91 void ElementData::destroy()
94 delete toUniqueElementData(this);
96 delete toShareableElementData(this);
100 PassRefPtrWillBeRawPtr
<UniqueElementData
> ElementData::makeUniqueCopy() const
103 return adoptRefWillBeNoop(new UniqueElementData(toUniqueElementData(*this)));
104 return adoptRefWillBeNoop(new UniqueElementData(toShareableElementData(*this)));
107 bool ElementData::isEquivalent(const ElementData
* other
) const
109 AttributeCollection attributes
= this->attributes();
111 return attributes
.isEmpty();
113 AttributeCollection otherAttributes
= other
->attributes();
114 if (attributes
.size() != otherAttributes
.size())
117 for (const Attribute
& attribute
: attributes
) {
118 const Attribute
* otherAttr
= otherAttributes
.find(attribute
.name());
119 if (!otherAttr
|| attribute
.value() != otherAttr
->value())
125 DEFINE_TRACE(ElementData
)
128 toUniqueElementData(this)->traceAfterDispatch(visitor
);
130 toShareableElementData(this)->traceAfterDispatch(visitor
);
133 DEFINE_TRACE_AFTER_DISPATCH(ElementData
)
135 visitor
->trace(m_inlineStyle
);
138 ShareableElementData::ShareableElementData(const Vector
<Attribute
>& attributes
)
139 : ElementData(attributes
.size())
141 for (unsigned i
= 0; i
< m_arraySize
; ++i
)
142 new (&m_attributeArray
[i
]) Attribute(attributes
[i
]);
145 ShareableElementData::~ShareableElementData()
147 for (unsigned i
= 0; i
< m_arraySize
; ++i
)
148 m_attributeArray
[i
].~Attribute();
151 ShareableElementData::ShareableElementData(const UniqueElementData
& other
)
152 : ElementData(other
, false)
154 ASSERT(!other
.m_presentationAttributeStyle
);
156 if (other
.m_inlineStyle
) {
157 m_inlineStyle
= other
.m_inlineStyle
->immutableCopyIfNeeded();
160 for (unsigned i
= 0; i
< m_arraySize
; ++i
)
161 new (&m_attributeArray
[i
]) Attribute(other
.m_attributeVector
.at(i
));
164 PassRefPtrWillBeRawPtr
<ShareableElementData
> ShareableElementData::createWithAttributes(const Vector
<Attribute
>& attributes
)
167 void* slot
= Heap::allocate
<ElementData
>(sizeForShareableElementDataWithAttributeCount(attributes
.size()));
169 void* slot
= WTF::fastMalloc(sizeForShareableElementDataWithAttributeCount(attributes
.size()));
171 return adoptRefWillBeNoop(new (slot
) ShareableElementData(attributes
));
174 UniqueElementData::UniqueElementData()
178 UniqueElementData::UniqueElementData(const UniqueElementData
& other
)
179 : ElementData(other
, true)
180 , m_presentationAttributeStyle(other
.m_presentationAttributeStyle
)
181 , m_attributeVector(other
.m_attributeVector
)
183 m_inlineStyle
= other
.m_inlineStyle
? other
.m_inlineStyle
->mutableCopy() : nullptr;
186 UniqueElementData::UniqueElementData(const ShareableElementData
& other
)
187 : ElementData(other
, true)
189 // An ShareableElementData should never have a mutable inline StylePropertySet attached.
190 ASSERT(!other
.m_inlineStyle
|| !other
.m_inlineStyle
->isMutable());
191 m_inlineStyle
= other
.m_inlineStyle
;
193 unsigned length
= other
.attributes().size();
194 m_attributeVector
.reserveCapacity(length
);
195 for (unsigned i
= 0; i
< length
; ++i
)
196 m_attributeVector
.uncheckedAppend(other
.m_attributeArray
[i
]);
199 PassRefPtrWillBeRawPtr
<UniqueElementData
> UniqueElementData::create()
201 return adoptRefWillBeNoop(new UniqueElementData
);
204 PassRefPtrWillBeRawPtr
<ShareableElementData
> UniqueElementData::makeShareableCopy() const
207 void* slot
= Heap::allocate
<ElementData
>(sizeForShareableElementDataWithAttributeCount(m_attributeVector
.size()));
209 void* slot
= WTF::fastMalloc(sizeForShareableElementDataWithAttributeCount(m_attributeVector
.size()));
211 return adoptRefWillBeNoop(new (slot
) ShareableElementData(*this));
214 DEFINE_TRACE_AFTER_DISPATCH(UniqueElementData
)
216 visitor
->trace(m_presentationAttributeStyle
);
217 ElementData::traceAfterDispatch(visitor
);