2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2006 Apple Computer, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 #include "MediaList.h"
23 #include "CSSImportRule.h"
24 #include "CSSParser.h"
25 #include "CSSStyleSheet.h"
26 #include "ExceptionCode.h"
27 #include "MediaQuery.h"
31 /* MediaList is used to store 3 types of media related entities which mean the same:
32 * Media Queries, Media Types and Media Descriptors.
33 * Currently MediaList always tries to parse media queries and if parsing fails,
34 * tries to fallback to Media Descriptors if m_fallback flag is set.
35 * Slight problem with syntax error handling:
36 * CSS 2.1 Spec (http://www.w3.org/TR/CSS21/media.html)
37 * specifies that failing media type parsing is a syntax error
38 * CSS 3 Media Queries Spec (http://www.w3.org/TR/css3-mediaqueries/)
39 * specifies that failing media query is a syntax error
40 * HTML 4.01 spec (http://www.w3.org/TR/REC-html40/present/styles.html#adef-media)
41 * specifies that Media Descriptors should be parsed with forward-compatible syntax
42 * DOM Level 2 Style Sheet spec (http://www.w3.org/TR/DOM-Level-2-Style/)
43 * talks about MediaList.mediaText and refers
44 * - to Media Descriptors of HTML 4.0 in context of StyleSheet
45 * - to Media Types of CSS 2.0 in context of CSSMediaRule and CSSImportRule
47 * These facts create situation where same (illegal) media specification may result in
48 * different parses depending on whether it is media attr of style element or part of
50 * <style media="screen and resolution > 40dpi"> ..</style> will be enabled on screen devices where as
51 * @media screen and resolution > 40dpi {..} will not.
52 * This gets more counter-intuitive in JavaScript:
53 * document.styleSheets[0].media.mediaText = "screen and resolution > 40dpi" will be ok and
55 * document.styleSheets[0].cssRules[0].media.mediaText = "screen and resolution > 40dpi" will
56 * throw SYNTAX_ERR exception.
59 MediaList::MediaList(CSSStyleSheet* parentSheet, bool fallbackToDescriptor)
60 : StyleBase(parentSheet)
61 , m_fallback(fallbackToDescriptor)
65 MediaList::MediaList(CSSStyleSheet* parentSheet, const String& media, bool fallbackToDescriptor)
66 : StyleBase(parentSheet)
67 , m_fallback(fallbackToDescriptor)
70 setMediaText(media, ec);
71 // FIXME: parsing can fail. The problem with failing constructor is that
72 // we would need additional flag saying MediaList is not valid
73 // Parse can fail only when fallbackToDescriptor == false, i.e when HTML4 media descriptor
74 // forward-compatible syntax is not in use.
75 // DOMImplementationCSS seems to mandate that media descriptors are used
76 // for both html and svg, even though svg:style doesn't use media descriptors
77 // Currently the only places where parsing can fail are
78 // creating <svg:style>, creating css media / import rules from js
80 setMediaText("invalid", ec);
83 MediaList::MediaList(CSSImportRule* parentRule, const String& media)
84 : StyleBase(parentRule)
88 setMediaText(media, ec);
90 setMediaText("invalid", ec);
93 MediaList::~MediaList()
95 deleteAllValues(m_queries);
98 static String parseMediaDescriptor(const String& s)
100 int len = s.length();
102 // http://www.w3.org/TR/REC-html40/types.html#type-media-descriptors
103 // "Each entry is truncated just before the first character that isn't a
104 // US ASCII letter [a-zA-Z] (ISO 10646 hex 41-5a, 61-7a), digit [0-9] (hex 30-39),
105 // or hyphen (hex 2d)."
108 for (i = 0; i < len; ++i) {
110 if (! ((c >= 'a' && c <= 'z')
111 || (c >= 'A' && c <= 'Z')
112 || (c >= '1' && c <= '9')
119 void MediaList::deleteMedium(const String& oldMedium, ExceptionCode& ec)
121 RefPtr<MediaList> tempMediaList = MediaList::create();
124 MediaQuery* oldQuery = 0;
125 bool deleteOldQuery = false;
127 if (p.parseMediaQuery(tempMediaList.get(), oldMedium)) {
128 if (tempMediaList->m_queries.size() > 0)
129 oldQuery = tempMediaList->m_queries[0];
130 } else if (m_fallback) {
131 String medium = parseMediaDescriptor(oldMedium);
132 if (!medium.isNull()) {
133 oldQuery = new MediaQuery(MediaQuery::None, medium, 0);
134 deleteOldQuery = true;
138 // DOM Style Sheets spec doesn't allow SYNTAX_ERR to be thrown in deleteMedium
142 for (size_t i = 0; i < m_queries.size(); ++i) {
143 MediaQuery* a = m_queries[i];
144 if (*a == *oldQuery) {
159 String MediaList::mediaText() const
164 for (size_t i = 0; i < m_queries.size(); ++i) {
169 text += m_queries[i]->cssText();
175 void MediaList::setMediaText(const String& value, ExceptionCode& ec)
177 RefPtr<MediaList> tempMediaList = MediaList::create();
181 value.split(',', list);
182 Vector<String>::const_iterator end = list.end();
183 for (Vector<String>::const_iterator it = list.begin(); it != end; ++it) {
184 String medium = (*it).stripWhiteSpace();
185 if (!medium.isEmpty()) {
186 if (!p.parseMediaQuery(tempMediaList.get(), medium)) {
188 String mediaDescriptor = parseMediaDescriptor(medium);
189 if (!mediaDescriptor.isNull())
190 tempMediaList->m_queries.append(new MediaQuery(MediaQuery::None, mediaDescriptor, 0));
196 } else if (!m_fallback) {
201 // ",,,," falls straight through, but is not valid unless fallback
202 if (!m_fallback && list.begin() == list.end()) {
203 String s = value.stripWhiteSpace();
211 deleteAllValues(m_queries);
212 m_queries = tempMediaList->m_queries;
213 tempMediaList->m_queries.clear();
217 String MediaList::item(unsigned index) const
219 if (index < m_queries.size()) {
220 MediaQuery* query = m_queries[index];
221 return query->cssText();
227 void MediaList::appendMedium(const String& newMedium, ExceptionCode& ec)
229 ec = INVALID_CHARACTER_ERR;
231 if (p.parseMediaQuery(this, newMedium)) {
233 } else if (m_fallback) {
234 String medium = parseMediaDescriptor(newMedium);
235 if (!medium.isNull()) {
236 m_queries.append(new MediaQuery(MediaQuery::None, medium, 0));
245 void MediaList::appendMediaQuery(MediaQuery* mediaQuery)
247 m_queries.append(mediaQuery);
250 void MediaList::notifyChanged()
252 for (StyleBase* p = parent(); p; p = p->parent()) {
253 if (p->isCSSStyleSheet())
254 return static_cast<CSSStyleSheet*>(p)->styleSheetChanged();