1 | /*
|
---|
2 | * Copyright (C) 2007-2022 Apple Inc. All rights reserved.
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without
|
---|
5 | * modification, are permitted provided that the following conditions
|
---|
6 | * are met:
|
---|
7 | * 1. Redistributions of source code must retain the above copyright
|
---|
8 | * notice, this list of conditions and the following disclaimer.
|
---|
9 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer in the
|
---|
11 | * documentation and/or other materials provided with the distribution.
|
---|
12 | *
|
---|
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
|
---|
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
|
---|
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
---|
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include "config.h"
|
---|
27 | #include "CSSKeyframesRule.h"
|
---|
28 |
|
---|
29 | #include "CSSKeyframeRule.h"
|
---|
30 | #include "CSSParser.h"
|
---|
31 | #include "CSSRuleList.h"
|
---|
32 | #include "CSSStyleSheet.h"
|
---|
33 | #include "Document.h"
|
---|
34 | #include <wtf/text/StringBuilder.h>
|
---|
35 |
|
---|
36 | namespace WebCore {
|
---|
37 |
|
---|
38 | StyleRuleKeyframes::StyleRuleKeyframes(const AtomString& name)
|
---|
39 | : StyleRuleBase(StyleRuleType::Keyframes)
|
---|
40 | , m_name(name)
|
---|
41 | {
|
---|
42 | }
|
---|
43 |
|
---|
44 | StyleRuleKeyframes::StyleRuleKeyframes(const StyleRuleKeyframes& o)
|
---|
45 | : StyleRuleBase(o)
|
---|
46 | , m_keyframes(o.keyframes())
|
---|
47 | , m_name(o.m_name)
|
---|
48 | {
|
---|
49 | }
|
---|
50 |
|
---|
51 | Ref<StyleRuleKeyframes> StyleRuleKeyframes::create(const AtomString& name)
|
---|
52 | {
|
---|
53 | return adoptRef(*new StyleRuleKeyframes(name));
|
---|
54 | }
|
---|
55 |
|
---|
56 | StyleRuleKeyframes::~StyleRuleKeyframes() = default;
|
---|
57 |
|
---|
58 | const Vector<Ref<StyleRuleKeyframe>>& StyleRuleKeyframes::keyframes() const
|
---|
59 | {
|
---|
60 | return m_keyframes;
|
---|
61 | }
|
---|
62 |
|
---|
63 | void StyleRuleKeyframes::parserAppendKeyframe(RefPtr<StyleRuleKeyframe>&& keyframe)
|
---|
64 | {
|
---|
65 | if (!keyframe)
|
---|
66 | return;
|
---|
67 | m_keyframes.append(keyframe.releaseNonNull());
|
---|
68 | }
|
---|
69 |
|
---|
70 | void StyleRuleKeyframes::wrapperAppendKeyframe(Ref<StyleRuleKeyframe>&& keyframe)
|
---|
71 | {
|
---|
72 | m_keyframes.append(WTFMove(keyframe));
|
---|
73 | }
|
---|
74 |
|
---|
75 | void StyleRuleKeyframes::wrapperRemoveKeyframe(unsigned index)
|
---|
76 | {
|
---|
77 | m_keyframes.remove(index);
|
---|
78 | }
|
---|
79 |
|
---|
80 | std::optional<size_t> StyleRuleKeyframes::findKeyframeIndex(const String& key) const
|
---|
81 | {
|
---|
82 | auto keys = CSSParser::parseKeyframeKeyList(key);
|
---|
83 | if (keys.isEmpty())
|
---|
84 | return std::nullopt;
|
---|
85 | for (auto i = m_keyframes.size(); i--; ) {
|
---|
86 | if (m_keyframes[i]->keys() == keys)
|
---|
87 | return i;
|
---|
88 | }
|
---|
89 | return std::nullopt;
|
---|
90 | }
|
---|
91 |
|
---|
92 | void StyleRuleKeyframes::shrinkToFit()
|
---|
93 | {
|
---|
94 | m_keyframes.shrinkToFit();
|
---|
95 | }
|
---|
96 |
|
---|
97 | CSSKeyframesRule::CSSKeyframesRule(StyleRuleKeyframes& keyframesRule, CSSStyleSheet* parent)
|
---|
98 | : CSSRule(parent)
|
---|
99 | , m_keyframesRule(keyframesRule)
|
---|
100 | , m_childRuleCSSOMWrappers(keyframesRule.keyframes().size())
|
---|
101 | {
|
---|
102 | }
|
---|
103 |
|
---|
104 | CSSKeyframesRule::~CSSKeyframesRule()
|
---|
105 | {
|
---|
106 | ASSERT(m_childRuleCSSOMWrappers.size() == m_keyframesRule->keyframes().size());
|
---|
107 |
|
---|
108 | for (unsigned i = 0; i < m_childRuleCSSOMWrappers.size(); ++i) {
|
---|
109 | if (m_childRuleCSSOMWrappers[i])
|
---|
110 | m_childRuleCSSOMWrappers[i]->setParentRule(0);
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | void CSSKeyframesRule::setName(const AtomString& name)
|
---|
115 | {
|
---|
116 | CSSStyleSheet::RuleMutationScope mutationScope(this);
|
---|
117 |
|
---|
118 | m_keyframesRule->setName(name);
|
---|
119 | }
|
---|
120 |
|
---|
121 | void CSSKeyframesRule::appendRule(const String& ruleText)
|
---|
122 | {
|
---|
123 | ASSERT(m_childRuleCSSOMWrappers.size() == m_keyframesRule->keyframes().size());
|
---|
124 |
|
---|
125 | CSSParser parser(parserContext());
|
---|
126 | RefPtr<StyleRuleKeyframe> keyframe = parser.parseKeyframeRule(ruleText);
|
---|
127 | if (!keyframe)
|
---|
128 | return;
|
---|
129 |
|
---|
130 | CSSStyleSheet::RuleMutationScope mutationScope(this);
|
---|
131 |
|
---|
132 | m_keyframesRule->wrapperAppendKeyframe(keyframe.releaseNonNull());
|
---|
133 |
|
---|
134 | m_childRuleCSSOMWrappers.grow(length());
|
---|
135 | }
|
---|
136 |
|
---|
137 | void CSSKeyframesRule::insertRule(const String& ruleText)
|
---|
138 | {
|
---|
139 | if (CSSStyleSheet* parent = parentStyleSheet()) {
|
---|
140 | if (Document* ownerDocument = parent->ownerDocument())
|
---|
141 | ownerDocument->addConsoleMessage(MessageSource::JS, MessageLevel::Warning, "CSSKeyframesRule 'insertRule' function is deprecated. Use 'appendRule' instead."_s);
|
---|
142 | }
|
---|
143 | appendRule(ruleText);
|
---|
144 | }
|
---|
145 |
|
---|
146 | void CSSKeyframesRule::deleteRule(const String& s)
|
---|
147 | {
|
---|
148 | ASSERT(m_childRuleCSSOMWrappers.size() == m_keyframesRule->keyframes().size());
|
---|
149 |
|
---|
150 | auto i = m_keyframesRule->findKeyframeIndex(s);
|
---|
151 | if (!i)
|
---|
152 | return;
|
---|
153 |
|
---|
154 | CSSStyleSheet::RuleMutationScope mutationScope(this);
|
---|
155 |
|
---|
156 | m_keyframesRule->wrapperRemoveKeyframe(*i);
|
---|
157 |
|
---|
158 | if (m_childRuleCSSOMWrappers[*i])
|
---|
159 | m_childRuleCSSOMWrappers[*i]->setParentRule(nullptr);
|
---|
160 | m_childRuleCSSOMWrappers.remove(*i);
|
---|
161 | }
|
---|
162 |
|
---|
163 | CSSKeyframeRule* CSSKeyframesRule::findRule(const String& s)
|
---|
164 | {
|
---|
165 | auto i = m_keyframesRule->findKeyframeIndex(s);
|
---|
166 | return i ? item(*i) : nullptr;
|
---|
167 | }
|
---|
168 |
|
---|
169 | String CSSKeyframesRule::cssText() const
|
---|
170 | {
|
---|
171 | StringBuilder result;
|
---|
172 | result.append("@-webkit-keyframes ", name(), " { \n");
|
---|
173 | for (unsigned i = 0, size = length(); i < size; ++i)
|
---|
174 | result.append(" ", m_keyframesRule->keyframes()[i]->cssText(), '\n');
|
---|
175 | result.append('}');
|
---|
176 | return result.toString();
|
---|
177 | }
|
---|
178 |
|
---|
179 | unsigned CSSKeyframesRule::length() const
|
---|
180 | {
|
---|
181 | return m_keyframesRule->keyframes().size();
|
---|
182 | }
|
---|
183 |
|
---|
184 | CSSKeyframeRule* CSSKeyframesRule::item(unsigned index) const
|
---|
185 | {
|
---|
186 | if (index >= length())
|
---|
187 | return nullptr;
|
---|
188 | ASSERT(m_childRuleCSSOMWrappers.size() == m_keyframesRule->keyframes().size());
|
---|
189 | auto& rule = m_childRuleCSSOMWrappers[index];
|
---|
190 | if (!rule)
|
---|
191 | rule = adoptRef(*new CSSKeyframeRule(m_keyframesRule->keyframes()[index], const_cast<CSSKeyframesRule*>(this)));
|
---|
192 | return rule.get();
|
---|
193 | }
|
---|
194 |
|
---|
195 | CSSRuleList& CSSKeyframesRule::cssRules()
|
---|
196 | {
|
---|
197 | if (!m_ruleListCSSOMWrapper)
|
---|
198 | m_ruleListCSSOMWrapper = makeUnique<LiveCSSRuleList<CSSKeyframesRule>>(*this);
|
---|
199 | return *m_ruleListCSSOMWrapper;
|
---|
200 | }
|
---|
201 |
|
---|
202 | void CSSKeyframesRule::reattach(StyleRuleBase& rule)
|
---|
203 | {
|
---|
204 | ASSERT_WITH_SECURITY_IMPLICATION(rule.isKeyframesRule());
|
---|
205 | m_keyframesRule = static_cast<StyleRuleKeyframes&>(rule);
|
---|
206 | }
|
---|
207 |
|
---|
208 | } // namespace WebCore
|
---|