| 1 | /* |
| 2 | * Copyright (C) 2007, 2008, 2012, 2014 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 "CSSKeyframeRule.h" |
| 28 | |
| 29 | #include "CSSKeyframesRule.h" |
| 30 | #include "CSSParser.h" |
| 31 | #include "PropertySetCSSStyleDeclaration.h" |
| 32 | #include "StyleProperties.h" |
| 33 | #include <wtf/text/StringBuilder.h> |
| 34 | |
| 35 | namespace WebCore { |
| 36 | |
| 37 | StyleRuleKeyframe::StyleRuleKeyframe(Ref<StyleProperties>&& properties) |
| 38 | : StyleRuleBase(Keyframe) |
| 39 | , m_properties(WTFMove(properties)) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | StyleRuleKeyframe::StyleRuleKeyframe(std::unique_ptr<Vector<double>> keys, Ref<StyleProperties>&& properties) |
| 44 | : StyleRuleBase(Keyframe) |
| 45 | , m_properties(WTFMove(properties)) |
| 46 | , m_keys(*keys) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | StyleRuleKeyframe::~StyleRuleKeyframe() = default; |
| 51 | |
| 52 | MutableStyleProperties& StyleRuleKeyframe::mutableProperties() |
| 53 | { |
| 54 | if (!is<MutableStyleProperties>(m_properties.get())) |
| 55 | m_properties = m_properties->mutableCopy(); |
| 56 | return downcast<MutableStyleProperties>(m_properties.get()); |
| 57 | } |
| 58 | |
| 59 | String StyleRuleKeyframe::keyText() const |
| 60 | { |
| 61 | StringBuilder keyText; |
| 62 | |
| 63 | for (size_t i = 0; i < m_keys.size(); ++i) { |
| 64 | if (i) |
| 65 | keyText.append(','); |
| 66 | keyText.appendFixedPrecisionNumber(m_keys.at(i) * 100); |
| 67 | keyText.append('%'); |
| 68 | } |
| 69 | |
| 70 | return keyText.toString(); |
| 71 | } |
| 72 | |
| 73 | bool StyleRuleKeyframe::setKeyText(const String& keyText) |
| 74 | { |
| 75 | ASSERT(!keyText.isNull()); |
| 76 | auto keys = CSSParser::parseKeyframeKeyList(keyText); |
| 77 | if (!keys || keys->isEmpty()) |
| 78 | return false; |
| 79 | m_keys = *keys; |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | String StyleRuleKeyframe::cssText() const |
| 84 | { |
| 85 | StringBuilder result; |
| 86 | result.append(keyText()); |
| 87 | result.appendLiteral(" { " ); |
| 88 | String decls = m_properties->asText(); |
| 89 | result.append(decls); |
| 90 | if (!decls.isEmpty()) |
| 91 | result.append(' '); |
| 92 | result.append('}'); |
| 93 | return result.toString(); |
| 94 | } |
| 95 | |
| 96 | CSSKeyframeRule::CSSKeyframeRule(StyleRuleKeyframe& keyframe, CSSKeyframesRule* parent) |
| 97 | : CSSRule(0) |
| 98 | , m_keyframe(keyframe) |
| 99 | { |
| 100 | setParentRule(parent); |
| 101 | } |
| 102 | |
| 103 | CSSKeyframeRule::~CSSKeyframeRule() |
| 104 | { |
| 105 | if (m_propertiesCSSOMWrapper) |
| 106 | m_propertiesCSSOMWrapper->clearParentRule(); |
| 107 | } |
| 108 | |
| 109 | CSSStyleDeclaration& CSSKeyframeRule::style() |
| 110 | { |
| 111 | if (!m_propertiesCSSOMWrapper) |
| 112 | m_propertiesCSSOMWrapper = StyleRuleCSSStyleDeclaration::create(m_keyframe->mutableProperties(), *this); |
| 113 | return *m_propertiesCSSOMWrapper; |
| 114 | } |
| 115 | |
| 116 | void CSSKeyframeRule::reattach(StyleRuleBase&) |
| 117 | { |
| 118 | // No need to reattach, the underlying data is shareable on mutation. |
| 119 | ASSERT_NOT_REACHED(); |
| 120 | } |
| 121 | |
| 122 | } // namespace WebCore |
| 123 | |