OLD | NEW |
1 /* | 1 /* |
2 * (C) 1999-2003 Lars Knoll (knoll@kde.org) | 2 * (C) 1999-2003 Lars Knoll (knoll@kde.org) |
3 * Copyright (C) 2004, 2006, 2010, 2012 Apple Inc. All rights reserved. | 3 * Copyright (C) 2004, 2006, 2010, 2012 Apple Inc. All rights reserved. |
4 * | 4 * |
5 * This library is free software; you can redistribute it and/or | 5 * This library is free software; you can redistribute it and/or |
6 * modify it under the terms of the GNU Library General Public | 6 * modify it under the terms of the GNU Library General Public |
7 * License as published by the Free Software Foundation; either | 7 * License as published by the Free Software Foundation; either |
8 * version 2 of the License, or (at your option) any later version. | 8 * version 2 of the License, or (at your option) any later version. |
9 * | 9 * |
10 * This library is distributed in the hope that it will be useful, | 10 * This library is distributed in the hope that it will be useful, |
(...skipping 20 matching lines...) Expand all Loading... |
31 #include "core/page/DOMWindow.h" | 31 #include "core/page/DOMWindow.h" |
32 #include "wtf/MemoryInstrumentationVector.h" | 32 #include "wtf/MemoryInstrumentationVector.h" |
33 #include "wtf/text/StringBuilder.h" | 33 #include "wtf/text/StringBuilder.h" |
34 | 34 |
35 namespace WebCore { | 35 namespace WebCore { |
36 | 36 |
37 /* MediaList is used to store 3 types of media related entities which mean the s
ame: | 37 /* MediaList is used to store 3 types of media related entities which mean the s
ame: |
38 * | 38 * |
39 * Media Queries, Media Types and Media Descriptors. | 39 * Media Queries, Media Types and Media Descriptors. |
40 * | 40 * |
41 * Slight problem with syntax error handling: | 41 * Media queries, as described in the Media Queries Level 3 specification, build
on |
| 42 * the mechanism outlined in HTML4. The syntax of media queries fit into the med
ia |
| 43 * type syntax reserved in HTML4. The media attribute of HTML4 also exists in XH
TML |
| 44 * and generic XML. The same syntax can also be used inside the @media and @impo
rt |
| 45 * rules of CSS. |
| 46 * |
| 47 * However, the parsing rules for media queries are incompatible with those of H
TML4 |
| 48 * and are consistent with those of media queries used in CSS. |
| 49 * |
| 50 * HTML5 (at the moment of writing still work in progress) references the Media
Queries |
| 51 * specification directly and thus updates the rules for HTML. |
| 52 * |
42 * CSS 2.1 Spec (http://www.w3.org/TR/CSS21/media.html) | 53 * CSS 2.1 Spec (http://www.w3.org/TR/CSS21/media.html) |
43 * specifies that failing media type parsing is a syntax error | |
44 * CSS 3 Media Queries Spec (http://www.w3.org/TR/css3-mediaqueries/) | 54 * CSS 3 Media Queries Spec (http://www.w3.org/TR/css3-mediaqueries/) |
45 * specifies that failing media query is a syntax error | |
46 * HTML 4.01 spec (http://www.w3.org/TR/REC-html40/present/styles.html#adef-medi
a) | |
47 * specifies that Media Descriptors should be parsed with forward-compatible syn
tax | |
48 * DOM Level 2 Style Sheet spec (http://www.w3.org/TR/DOM-Level-2-Style/) | |
49 * talks about MediaList.mediaText and refers | |
50 * - to Media Descriptors of HTML 4.0 in context of StyleSheet | |
51 * - to Media Types of CSS 2.0 in context of CSSMediaRule and CSSImportRule | |
52 * | |
53 * These facts create situation where same (illegal) media specification may res
ult in | |
54 * different parses depending on whether it is media attr of style element or pa
rt of | |
55 * css @media rule. | |
56 * <style media="screen and resolution > 40dpi"> ..</style> will be enabled on s
creen devices where as | |
57 * @media screen and resolution > 40dpi {..} will not. | |
58 * This gets more counter-intuitive in JavaScript: | |
59 * document.styleSheets[0].media.mediaText = "screen and resolution > 40dpi" wil
l be ok and | |
60 * enabled, while | |
61 * document.styleSheets[0].cssRules[0].media.mediaText = "screen and resolution
> 40dpi" will | |
62 * throw SYNTAX_ERR exception. | |
63 */ | 55 */ |
64 | 56 |
65 MediaQuerySet::MediaQuerySet() | 57 MediaQuerySet::MediaQuerySet() |
66 : m_parserMode(MediaQueryNormalMode) | 58 : m_parserMode(MediaQueryNormalMode) |
67 , m_lastLine(0) | 59 , m_lastLine(0) |
68 { | 60 { |
69 } | 61 } |
70 | 62 |
71 MediaQuerySet::MediaQuerySet(const String& mediaString, MediaQueryParserMode mod
e) | 63 MediaQuerySet::MediaQuerySet(const String& mediaString, MediaQueryParserMode mod
e) |
72 : m_parserMode(mode) | 64 : m_parserMode(mode) |
73 , m_lastLine(0) | 65 , m_lastLine(0) |
74 { | 66 { |
75 set(mediaString); | 67 set(mediaString); |
76 } | 68 } |
77 | 69 |
78 MediaQuerySet::MediaQuerySet(const MediaQuerySet& o) | 70 MediaQuerySet::MediaQuerySet(const MediaQuerySet& o) |
79 : RefCounted<MediaQuerySet>() | 71 : RefCounted<MediaQuerySet>() |
80 , m_parserMode(o.m_parserMode) | 72 , m_parserMode(o.m_parserMode) |
81 , m_lastLine(o.m_lastLine) | 73 , m_lastLine(o.m_lastLine) |
82 , m_queries(o.m_queries.size()) | 74 , m_queries(o.m_queries.size()) |
83 { | 75 { |
84 for (unsigned i = 0; i < m_queries.size(); ++i) | 76 for (unsigned i = 0; i < m_queries.size(); ++i) |
85 m_queries[i] = o.m_queries[i]->copy(); | 77 m_queries[i] = o.m_queries[i]->copy(); |
86 } | 78 } |
87 | 79 |
88 MediaQuerySet::~MediaQuerySet() | 80 MediaQuerySet::~MediaQuerySet() |
89 { | 81 { |
90 } | 82 } |
91 | 83 |
92 static String parseMediaDescriptor(const String& string) | |
93 { | |
94 // http://www.w3.org/TR/REC-html40/types.html#type-media-descriptors | |
95 // "Each entry is truncated just before the first character that isn't a | |
96 // US ASCII letter [a-zA-Z] (ISO 10646 hex 41-5a, 61-7a), digit [0-9] (hex 3
0-39), | |
97 // or hyphen (hex 2d)." | |
98 unsigned length = string.length(); | |
99 unsigned i = 0; | |
100 for (; i < length; ++i) { | |
101 unsigned short c = string[i]; | |
102 if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '1' && c
<= '9') || (c == '-'))) | |
103 break; | |
104 } | |
105 return string.left(i); | |
106 } | |
107 | |
108 PassOwnPtr<MediaQuery> MediaQuerySet::parseMediaQuery(const String& queryString,
MediaQueryParserMode mode) | 84 PassOwnPtr<MediaQuery> MediaQuerySet::parseMediaQuery(const String& queryString,
MediaQueryParserMode mode) |
109 { | 85 { |
110 CSSParser parser(CSSStrictMode); | 86 CSSParser parser(CSSStrictMode); |
111 OwnPtr<MediaQuery> parsedQuery = parser.parseMediaQuery(queryString); | 87 OwnPtr<MediaQuery> parsedQuery = parser.parseMediaQuery(queryString); |
112 | 88 |
113 if (parsedQuery) | 89 if (parsedQuery) |
114 return parsedQuery.release(); | 90 return parsedQuery.release(); |
115 | 91 |
116 switch (mode) { | 92 switch (mode) { |
117 case MediaQueryForwardCompatibleSyntaxMode: { | |
118 String medium = parseMediaDescriptor(queryString); | |
119 if (!medium.isNull()) | |
120 return adoptPtr(new MediaQuery(MediaQuery::None, medium, nullptr)); | |
121 // Fall through. | |
122 } | |
123 case MediaQueryNormalMode: | 93 case MediaQueryNormalMode: |
124 return adoptPtr(new MediaQuery(MediaQuery::None, "not all", nullptr)); | 94 return adoptPtr(new MediaQuery(MediaQuery::None, "not all", nullptr)); |
125 case MediaQueryStrictMode: | 95 case MediaQueryStrictMode: |
126 break; | 96 break; |
127 default: | 97 default: |
128 ASSERT_NOT_REACHED(); | 98 ASSERT_NOT_REACHED(); |
129 break; | 99 break; |
130 } | 100 } |
131 return nullptr; | 101 return nullptr; |
132 } | 102 } |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(cssV
alue); | 347 CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(cssV
alue); |
378 if (primitiveValue->isDotsPerInch() || primitiveValue->isDot
sPerCentimeter()) | 348 if (primitiveValue->isDotsPerInch() || primitiveValue->isDot
sPerCentimeter()) |
379 addResolutionWarningMessageToConsole(document, mediaQuer
ySet->mediaText(), primitiveValue); | 349 addResolutionWarningMessageToConsole(document, mediaQuer
ySet->mediaText(), primitiveValue); |
380 } | 350 } |
381 } | 351 } |
382 } | 352 } |
383 } | 353 } |
384 } | 354 } |
385 | 355 |
386 } | 356 } |
OLD | NEW |