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