Skip to content

Commit ad57db3

Browse files
committed
1 parent 5d3925b commit ad57db3

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// $Id$
2+
//
3+
// (c) COPYRIGHT MIT, ECRIM and Keio University, 2011
4+
// Please first read the full copyright statement in file COPYRIGHT.html
5+
6+
package org.w3c.css.atrules.css3.media;
7+
8+
import org.w3c.css.atrules.css.media.MediaFeature;
9+
import org.w3c.css.util.ApplContext;
10+
import org.w3c.css.util.InvalidParamException;
11+
import org.w3c.css.values.CssExpression;
12+
import org.w3c.css.values.CssIdent;
13+
import org.w3c.css.values.CssTypes;
14+
import org.w3c.css.values.CssValue;
15+
16+
/**
17+
* @spec https://www.w3.org/TR/2020/WD-mediaqueries-5-20200731/#descdef-media-prefers-reduced-motion
18+
*/
19+
public class MediaPrefersReducedMotion extends MediaFeature {
20+
21+
public static final CssIdent[] allowed_values;
22+
23+
static {
24+
String[] _allowed_values = {"no-preference", "reduce"};
25+
allowed_values = new CssIdent[_allowed_values.length];
26+
int i = 0;
27+
for (String s : _allowed_values) {
28+
allowed_values[i++] = CssIdent.getIdent(s);
29+
}
30+
}
31+
32+
public static CssIdent getAllowedIdent(CssIdent ident) {
33+
for (CssIdent id : allowed_values) {
34+
if (id.equals(ident)) {
35+
return id;
36+
}
37+
}
38+
return null;
39+
}
40+
41+
/**
42+
* Create a new MediaPrefersReducedMotion
43+
*/
44+
public MediaPrefersReducedMotion() {
45+
}
46+
47+
/**
48+
* Create a new MediaPrefersReducedMotion.
49+
*
50+
* @param expression The expression for this media feature
51+
* @throws InvalidParamException Values are incorrect
52+
*/
53+
public MediaPrefersReducedMotion(ApplContext ac, String modifier,
54+
CssExpression expression, boolean check)
55+
throws InvalidParamException {
56+
57+
if (modifier != null) {
58+
throw new InvalidParamException("nomodifiermedia",
59+
getFeatureName(), ac);
60+
}
61+
62+
if (expression != null) {
63+
if (expression.getCount() > 1) {
64+
throw new InvalidParamException("unrecognize", ac);
65+
}
66+
if (expression.getCount() == 0) {
67+
throw new InvalidParamException("few-value", getFeatureName(), ac);
68+
}
69+
CssValue val = expression.getValue();
70+
71+
switch (val.getType()) {
72+
case CssTypes.CSS_IDENT:
73+
if (getAllowedIdent(val.getIdent()) != null) {
74+
value = val;
75+
break;
76+
}
77+
// let it flow through the exception
78+
default:
79+
throw new InvalidParamException("value", expression.getValue(),
80+
getFeatureName(), ac);
81+
}
82+
} else {
83+
// TODO add a warning for value less mediafeature that makes no sense
84+
}
85+
}
86+
87+
public MediaPrefersReducedMotion(ApplContext ac, String modifier, CssExpression expression)
88+
throws InvalidParamException {
89+
this(ac, modifier, expression, false);
90+
}
91+
92+
// just in case someone wants to call it externally...
93+
public void setModifier(ApplContext ac, String modifier)
94+
throws InvalidParamException {
95+
throw new InvalidParamException("nomodifiermedia",
96+
getFeatureName(), ac);
97+
}
98+
99+
/**
100+
* Returns the value of this media feature.
101+
*/
102+
103+
public Object get() {
104+
return value;
105+
}
106+
107+
/**
108+
* Returns the name of this media feature.
109+
*/
110+
public final String getFeatureName() {
111+
return "prefers-reduced-motion";
112+
}
113+
114+
/**
115+
* Compares two media features for equality.
116+
*
117+
* @param other The other media features.
118+
*/
119+
public boolean equals(MediaFeature other) {
120+
try {
121+
MediaPrefersReducedMotion mo = (MediaPrefersReducedMotion) other;
122+
return (((value == null) && (mo.value == null)) || ((value != null) && value.equals(mo.value)));
123+
} catch (ClassCastException cce) {
124+
return false;
125+
}
126+
127+
}
128+
}

org/w3c/css/properties/CSS3Properties.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ mediafeature.orientation: org.w3c.css.atrules.css3.media.MediaOrie
544544
mediafeature.overflow-block: org.w3c.css.atrules.css3.media.MediaOverflowBlock
545545
mediafeature.overflow-inline: org.w3c.css.atrules.css3.media.MediaOverflowInline
546546
mediafeature.pointer: org.w3c.css.atrules.css3.media.MediaPointer
547+
mediafeature.prefers-reduced-motion: org.w3c.css.atrules.css3.media.MediaPrefersReducedMotion
547548
mediafeature.resolution: org.w3c.css.atrules.css3.media.MediaResolution
548549
mediafeature.scan: org.w3c.css.atrules.css3.media.MediaScan
549550
mediafeature.scripting: org.w3c.css.atrules.css3.media.MediaScripting

0 commit comments

Comments
 (0)