Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a964c6d7b59518528f884c5741020a5ba2c8303d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*******************************************************************************
 * Copyright (c) 2009, 2014 EclipseSource and others.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which accompanies this distribution,
t https://www.eclipse.org/legal/epl-2.0/
t
t SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   EclipseSource - initial API and implementation
 *   Lars Vogel <Lars.Vogel@gmail.com> - Bug 430468
 *   Stefan Winkler <stefan@winklerweb.net> - Bug 458342
 ******************************************************************************/
package org.eclipse.e4.ui.tests.css.core.parser;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;

import org.eclipse.e4.ui.css.core.impl.dom.DocumentCSSImpl;
import org.eclipse.e4.ui.css.core.impl.dom.ViewCSSImpl;
import org.eclipse.e4.ui.css.swt.engine.CSSSWTEngineImpl;
import org.eclipse.e4.ui.tests.css.core.util.ParserTestUtil;
import org.eclipse.e4.ui.tests.css.core.util.TestElement;
import org.eclipse.swt.widgets.Display;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.w3c.dom.css.CSSStyleDeclaration;
import org.w3c.dom.css.CSSStyleSheet;
import org.w3c.dom.css.ViewCSS;

/**
 * Test to ensure the that CSS honors the CSS cascading rules, i.e.:
 *
 * <p>
 * !important after CSS properties.
 * </p>
 * <p>
 * Specificity of CSS rule selectors
 * </p>
 * <p>
 * Sequence of declaration.
 * </p>
 *
 **/

public class CascadeTest {

	private Display display;
	private CSSSWTEngineImpl engine;

	@BeforeEach
	public void setUp() throws Exception {
		display = Display.getDefault();
		engine = new CSSSWTEngineImpl(display);
	}

	@Test
	public void testPosition() throws Exception {
		// Two rules with the same specificity, the second rule should take
		// precedence because of its position in the stylesheet
		String css = "Button { color: blue; font-weight: bold; }\n"
				+ "Button { color: black }\n";
		ViewCSS viewCSS = createViewCss(css);

		TestElement button = new TestElement("Button", engine);
		CSSStyleDeclaration style = viewCSS.getComputedStyle(button, null);
		assertEquals("black", style.getPropertyCSSValue("color").getCssText());
		assertEquals("bold", style.getPropertyCSSValue("font-weight").getCssText());
	}

	@Test
	public void testSpecificity() throws Exception {
		// Two rules with different specificity, the first should take
		// precedence because of its higher specificity
		String css = "Label, Button.special { color: black; }\n"
				+ "Button { color: blue; font-weight: bold; }\n";
		ViewCSS viewCSS = createViewCss(css);

		TestElement button = new TestElement("Button", engine);
		CSSStyleDeclaration style = viewCSS.getComputedStyle(button, null);
		assertEquals("blue", style.getPropertyCSSValue("color").getCssText());

		button.setClass("special");
		style = viewCSS.getComputedStyle(button, null);
		assertEquals("black", style.getPropertyCSSValue("color").getCssText());
		assertEquals("bold", style.getPropertyCSSValue("font-weight")
				.getCssText());
	}

	@Test
	public void ensureThatClassAndIdareConsideredIfOnTheSameLevel() throws Exception {
		// Rules for elements with children. The first should take
		// precedence because of its higher specificity
		String css = "CTabFolder > Composite > Toolbar { color: black; }\n"
				+ "CTabFolder > Composite > .special { color: blue; font-weight: bold; }\n"
				+ "CTabFolder > Composite > #special { color: red; font-weight: bold; }\n";
		ViewCSS viewCSS = createViewCss(css);

		TestElement tabFolder = new TestElement("CTabFolder", engine);
		TestElement composite = new TestElement("Composite", tabFolder, engine);
		TestElement toolbar = new TestElement("Toolbar", composite, engine);

		CSSStyleDeclaration style = viewCSS.getComputedStyle(toolbar, null);
		assertEquals("black", style.getPropertyCSSValue("color").getCssText());

		toolbar.setClass("special");
		style = viewCSS.getComputedStyle(toolbar, null);
		assertEquals("blue", style.getPropertyCSSValue("color").getCssText());

		toolbar.setId("special");
		style = viewCSS.getComputedStyle(toolbar, null);
		assertEquals("red", style.getPropertyCSSValue("color").getCssText());

	}



	@Test
	public void testSpecificities() throws Exception {
		// Different specificities
		String css = "* { color: black; }\n"
				+ "Button { color: blue; }\n"
				+ "Button[BORDER] { color: gray; }\n"
				+ "Button.special { color: green; }\n"
				+ "Button#myid { color: red; }\n";
		ViewCSS viewCSS = createViewCss(css);

		TestElement label = new TestElement("Label", engine);
		CSSStyleDeclaration style = viewCSS.getComputedStyle(label, null);
		assertEquals("black", style.getPropertyCSSValue("color").getCssText());

		TestElement button = new TestElement("Button", engine);
		style = viewCSS.getComputedStyle(button, null);
		assertEquals("blue", style.getPropertyCSSValue("color").getCssText());

		button.setAttribute("BORDER", "true");
		style = viewCSS.getComputedStyle(button, null);
		assertEquals("gray", style.getPropertyCSSValue("color").getCssText());

		button.setClass("special");
		style = viewCSS.getComputedStyle(button, null);
		assertEquals("green", style.getPropertyCSSValue("color").getCssText());

		button.setId("myid");
		style = viewCSS.getComputedStyle(button, null);
		assertEquals("red", style.getPropertyCSSValue("color").getCssText());
	}

	private static ViewCSS createViewCss(String... css) throws IOException {
		DocumentCSSImpl docCss = new DocumentCSSImpl();
		for (String cssString : css) {
			CSSStyleSheet styleSheet = ParserTestUtil.parseCss(cssString);
			docCss.addStyleSheet(styleSheet);
		}
		return new ViewCSSImpl(docCss);
	}

	//	public void testImportantRule() throws Exception {
	//		//Several rules for the same class, if one rule has ! important
	//		//it takes precedence over all other, if more than one
	//		//last one gets precedence
	//
	//		String css = "Button{color:red ! important;}\n"
	//			+"Button{ color: blue ! important;}\n"
	//			+ "Button { color: black }\n";
	//		ViewCSS viewCSS = createViewCss(css);
	//
	//		TestElement button = new TestElement("Button", engine);
	//		CSSStyleDeclaration style = viewCSS.getComputedStyle(button, null);
	//		assertEquals("blue", style.getPropertyCSSValue("color").getCssText());
	//	}

	@Test
	public void testBug261081() throws Exception{
		// Two rules with the same specificity, the second rule should take
		// precedence because of its position in the stylesheet
		String css = "Button, Label { color: blue; font-weight: bold; }\n"
				+ "Button { color: black }\n";
		ViewCSS viewCSS = createViewCss(css);

		TestElement button = new TestElement("Button", engine);
		CSSStyleDeclaration style = viewCSS.getComputedStyle(button, null);
		assertEquals("black", style.getPropertyCSSValue("color").getCssText());
		assertEquals("bold", style.getPropertyCSSValue("font-weight").getCssText());
	}

	@Test
	public void testBug458342_combine() throws Exception {
		// the rules of two stylesheets should be combined
		String css1 = "Button { color: blue; }";
		String css2 = "Button { font-weight: bold; }";

		ViewCSS viewCSS = createViewCss(css1, css2);

		TestElement button = new TestElement("Button", engine);
		CSSStyleDeclaration style = viewCSS.getComputedStyle(button, null);
		assertEquals("blue", style.getPropertyCSSValue("color").getCssText());
		assertEquals("bold", style.getPropertyCSSValue("font-weight").getCssText());
	}

	@Test
	public void testBug458342_override() throws Exception {
		// in case of two stylesheets, the second one should override properties
		// from the first one
		String css1 = "Button { color: blue; font-weight: bold; }";
		String css2 = "Button { color: black; }";

		ViewCSS viewCSS = createViewCss(css1, css2);

		TestElement button = new TestElement("Button", engine);
		CSSStyleDeclaration style = viewCSS.getComputedStyle(button, null);
		assertEquals("black", style.getPropertyCSSValue("color").getCssText());
		assertEquals("bold", style.getPropertyCSSValue("font-weight").getCssText());
	}
}

Back to the top