Home | History | Annotate | Download | only in css
      1 /*
      2  * CSS Media Query
      3  *
      4  * Copyright (C) 2006 Kimmo Kinnunen <kimmo.t.kinnunen (at) nokia.com>.
      5  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
     17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "config.h"
     30 #include "MediaQueryExp.h"
     31 
     32 #include "CSSParser.h"
     33 #include "CSSPrimitiveValue.h"
     34 #include "CSSValueList.h"
     35 #include <wtf/text/StringBuilder.h>
     36 
     37 namespace WebCore {
     38 
     39 inline MediaQueryExp::MediaQueryExp(const AtomicString& mediaFeature, CSSParserValueList* valueList)
     40     : m_mediaFeature(mediaFeature)
     41     , m_value(0)
     42     , m_isValid(true)
     43 {
     44     if (valueList) {
     45         if (valueList->size() == 1) {
     46             CSSParserValue* value = valueList->current();
     47 
     48             if (value->id != 0)
     49                 m_value = CSSPrimitiveValue::createIdentifier(value->id);
     50             else if (value->unit == CSSPrimitiveValue::CSS_STRING)
     51                 m_value = CSSPrimitiveValue::create(value->string, (CSSPrimitiveValue::UnitTypes) value->unit);
     52             else if (value->unit >= CSSPrimitiveValue::CSS_NUMBER &&
     53                       value->unit <= CSSPrimitiveValue::CSS_KHZ)
     54                 m_value = CSSPrimitiveValue::create(value->fValue, (CSSPrimitiveValue::UnitTypes) value->unit);
     55 
     56             valueList->next();
     57         } else if (valueList->size() > 1) {
     58             // create list of values
     59             // currently accepts only <integer>/<integer>
     60 
     61             RefPtr<CSSValueList> list = CSSValueList::createCommaSeparated();
     62             CSSParserValue* value = 0;
     63             bool isValid = true;
     64 
     65             while ((value = valueList->current()) && isValid) {
     66                 if (value->unit == CSSParserValue::Operator && value->iValue == '/')
     67                     list->append(CSSPrimitiveValue::create("/", CSSPrimitiveValue::CSS_STRING));
     68                 else if (value->unit == CSSPrimitiveValue::CSS_NUMBER)
     69                     list->append(CSSPrimitiveValue::create(value->fValue, CSSPrimitiveValue::CSS_NUMBER));
     70                 else
     71                     isValid = false;
     72 
     73                 value = valueList->next();
     74             }
     75 
     76             if (isValid)
     77                 m_value = list.release();
     78         }
     79         m_isValid = m_value;
     80     }
     81 }
     82 
     83 
     84 PassOwnPtr<MediaQueryExp> MediaQueryExp::create(const AtomicString& mediaFeature, CSSParserValueList* values)
     85 {
     86     return adoptPtr(new MediaQueryExp(mediaFeature, values));
     87 }
     88 
     89 MediaQueryExp::~MediaQueryExp()
     90 {
     91 }
     92 
     93 String MediaQueryExp::serialize() const
     94 {
     95     if (!m_serializationCache.isNull())
     96         return m_serializationCache;
     97 
     98     StringBuilder result;
     99     result.append("(");
    100     result.append(m_mediaFeature.lower());
    101     if (m_value) {
    102         result.append(": ");
    103         result.append(m_value->cssText());
    104     }
    105     result.append(")");
    106 
    107     const_cast<MediaQueryExp*>(this)->m_serializationCache = result.toString();
    108     return m_serializationCache;
    109 }
    110 
    111 } // namespace
    112