Home | History | Annotate | Download | only in css
      1 /*
      2  * Copyright (C) 2007, 2008 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 COMPUTER, 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 COMPUTER, 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 "WebKitCSSKeyframesRule.h"
     28 
     29 #include "CSSMutableStyleDeclaration.h"
     30 #include "CSSParser.h"
     31 #include "CSSRuleList.h"
     32 #include "StyleSheet.h"
     33 #include "WebKitCSSKeyframeRule.h"
     34 
     35 namespace WebCore {
     36 
     37 WebKitCSSKeyframesRule::WebKitCSSKeyframesRule(CSSStyleSheet* parent)
     38     : CSSRule(parent)
     39     , m_lstCSSRules(CSSRuleList::create())
     40 {
     41 }
     42 
     43 WebKitCSSKeyframesRule::~WebKitCSSKeyframesRule()
     44 {
     45     int length = m_lstCSSRules->length();
     46     if (length == 0)
     47         return;
     48 
     49     for (int i = 0; i < length; i++) {
     50         if (m_lstCSSRules->item(i)->isKeyframeRule()) {
     51             if (CSSMutableStyleDeclaration* style = static_cast<WebKitCSSKeyframeRule*>(m_lstCSSRules->item(i))->style())
     52                 style->setParent(0);
     53         }
     54         m_lstCSSRules->item(i)->setParent(0);
     55     }
     56 }
     57 
     58 String WebKitCSSKeyframesRule::name() const
     59 {
     60     return m_name;
     61 }
     62 
     63 void WebKitCSSKeyframesRule::setName(const String& name)
     64 {
     65     m_name = name;
     66 
     67     // Since the name is used in the keyframe map list in CSSStyleSelector, we need
     68     // to recompute the style sheet to get the updated name.
     69     if (stylesheet())
     70         stylesheet()->styleSheetChanged();
     71 }
     72 
     73 unsigned WebKitCSSKeyframesRule::length() const
     74 {
     75     return m_lstCSSRules.get()->length();
     76 }
     77 
     78 WebKitCSSKeyframeRule* WebKitCSSKeyframesRule::item(unsigned index)
     79 {
     80     CSSRule* rule = m_lstCSSRules.get()->item(index);
     81     return (rule && rule->isKeyframeRule()) ? static_cast<WebKitCSSKeyframeRule*>(rule) : 0;
     82 }
     83 
     84 const WebKitCSSKeyframeRule* WebKitCSSKeyframesRule::item(unsigned index) const
     85 {
     86     CSSRule* rule = m_lstCSSRules.get()->item(index);
     87     return (rule && rule->isKeyframeRule()) ? static_cast<const WebKitCSSKeyframeRule*>(rule) : 0;
     88 }
     89 
     90 void WebKitCSSKeyframesRule::append(WebKitCSSKeyframeRule* rule)
     91 {
     92     if (!rule)
     93         return;
     94 
     95     m_lstCSSRules->append(rule);
     96     rule->setParent(this);
     97 
     98     if (CSSMutableStyleDeclaration* style = rule->style())
     99         style->setParent(this);
    100 }
    101 
    102 void WebKitCSSKeyframesRule::insertRule(const String& rule)
    103 {
    104     CSSParser p(useStrictParsing());
    105     RefPtr<CSSRule> newRule = p.parseKeyframeRule(parentStyleSheet(), rule);
    106     if (newRule.get() && newRule.get()->isKeyframeRule())
    107         append(static_cast<WebKitCSSKeyframeRule*>(newRule.get()));
    108 }
    109 
    110 void WebKitCSSKeyframesRule::deleteRule(const String& s)
    111 {
    112     int i = findRuleIndex(s);
    113     if (i >= 0)
    114         m_lstCSSRules.get()->deleteRule(i);
    115 }
    116 
    117 WebKitCSSKeyframeRule* WebKitCSSKeyframesRule::findRule(const String& s)
    118 {
    119     int i = findRuleIndex(s);
    120     return (i >= 0) ? item(i) : 0;
    121 }
    122 
    123 int WebKitCSSKeyframesRule::findRuleIndex(const String& key) const
    124 {
    125     String percentageString;
    126     if (equalIgnoringCase(key, "from"))
    127         percentageString = "0%";
    128     else if (equalIgnoringCase(key, "to"))
    129         percentageString = "100%";
    130     else
    131         percentageString = key;
    132 
    133     for (unsigned i = 0; i < length(); ++i) {
    134         if (item(i)->keyText() == percentageString)
    135             return i;
    136     }
    137 
    138     return -1;
    139 }
    140 
    141 String WebKitCSSKeyframesRule::cssText() const
    142 {
    143     String result = "@-webkit-keyframes ";
    144     result += m_name;
    145     result += " { \n";
    146 
    147     if (m_lstCSSRules) {
    148         unsigned len = m_lstCSSRules->length();
    149         for (unsigned i = 0; i < len; i++) {
    150             result += "  ";
    151             result += m_lstCSSRules->item(i)->cssText();
    152             result += "\n";
    153         }
    154     }
    155 
    156     result += "}";
    157     return result;
    158 }
    159 
    160 } // namespace WebCore
    161