Skip to content

Commit ac34f4d

Browse files
committed
Migrate color expressions to variables + test to enforce these patterns.
1 parent 07942df commit ac34f4d

File tree

10 files changed

+144
-26
lines changed

10 files changed

+144
-26
lines changed

pkg/web_css/lib/src/_base.scss

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ pre {
339339
right: 0;
340340
z-index: 10000;
341341

342-
background: rgba(0, 0, 0, 0.2);
342+
background: var(--pub-spinner_frame-background-color);
343343

344344
display: flex;
345345
align-items: center;
@@ -361,7 +361,7 @@ pre {
361361
}
362362

363363
.hash-link {
364-
color: #ccc;
364+
color: var(--pub-hash_link-text-color);
365365
opacity: 0;
366366
visibility: hidden;
367367
transition: opacity 0.5s ease-in-out 0.1s;
@@ -387,7 +387,7 @@ pre {
387387
.announcement-banner {
388388
padding: 10px 0;
389389

390-
background: #e7f8ff;
390+
background: var(--pub-home_announcement-background-color);
391391
font-size: 16px;
392392

393393
text-align: center;
@@ -411,7 +411,7 @@ a.-x-ago {
411411
top: 0;
412412
left: 0;
413413
right: 0;
414-
background: #ffffaa;
414+
background: var(--pub-session_warning-background-color);
415415
padding: 12px 24px;
416416
text-align: center;
417417
font-size: 14px;

pkg/web_css/lib/src/_form.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@
9393
padding: 2px 8px;
9494

9595
&:hover {
96-
background: $color-input-danger;
97-
color: white;
96+
background: var(--pub-remove_button-background-color);
97+
color: var(--pub-remove_button-text-color);
9898
}
9999
}
100100

pkg/web_css/lib/src/_home.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
margin: 1em auto;
2424
max-width: 500px;
2525

26-
color: white;
26+
color: var(--pub-home_banner-text-color);
2727
font-size: 18px;
2828

2929
> a {
30-
color: #31b0fc;
30+
color: var(--pub-home_banner-link-color);
3131
}
3232
}
3333

pkg/web_css/lib/src/_list.scss

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
}
3737

3838
.count {
39-
background: rgba(218, 220, 224, 0.26);
39+
background: var(--pub-code-background-color);
4040
display: inline-block;
4141
font-size: 12px;
4242
font-family: monospace;
@@ -106,7 +106,7 @@
106106
top: 0px;
107107
z-index: 1;
108108
display: none;
109-
background: rgba(0,0,0,0.85);
109+
background: var(--pub-carousel-background-color);
110110

111111
.image-container {
112112
user-select: none;
@@ -128,15 +128,15 @@
128128
}
129129

130130
.carousel-nav {
131-
background-color: rgba(0,0,0,0.7);
131+
background-color: var(--pub-carousel_nav-background-color);
132132
position: fixed;
133133
top: 50%;
134134
transform: translateY(-50%);
135135
display: none;
136136
z-index: 2;
137137

138138
&:hover {
139-
background-color: #4285f4;
139+
background-color: var(--pub-carousel_nav_hover-background-color);
140140
}
141141
}
142142

@@ -151,11 +151,11 @@
151151
.screenshot-description {
152152
font-size: 16px;
153153
z-index: 2;
154-
color: white;
154+
color: var(--pub-carousel_screenshot-text-color);
155155
bottom: 0;
156156
left: 20px;
157157
position: absolute;
158-
background: rgba(0,0,0,0.7);
158+
background: var(--pub-carousel_screenshot-background-color);
159159
border-radius: 25px;
160160
max-width: 35%;
161161
padding:12px;
@@ -170,7 +170,7 @@
170170
height: 30px;
171171
width: 30px;
172172
opacity: 70%;
173-
background-color: #aeaeae;
173+
background-color: var(--pub-collections_icon-background-color);
174174
margin-top: auto;
175175
margin-left: -30px;
176176
}
@@ -181,7 +181,7 @@
181181
height: inherit;
182182
justify-content: center;
183183
align-items: center;
184-
background: white;
184+
background: var(--pub-thumbnail_container-background-color);
185185
}
186186

187187
.packages {
@@ -240,7 +240,7 @@
240240
}
241241

242242
.packages-recent {
243-
color: #6d7278;
243+
color: var(--pub-pkg_list_recent_item-text-color);
244244
font-family: var(--pub-default-text-font_family);
245245
font-size: 12px;
246246
margin: 0px 8px 0px 16px;
@@ -268,7 +268,6 @@
268268
}
269269

270270
.packages-api {
271-
color: #3c4043;
272271
font-size: 12px;
273272
margin-top: 6px;
274273
display: flex;

pkg/web_css/lib/src/_pkg.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,11 @@
221221
font-size: 22px;
222222

223223
&.-is-red {
224-
color: #e13701;
224+
color: var(--pub-report_header_score_error-text-color);
225225
}
226226

227227
&.-is-yellow {
228-
color: #FFA500;
228+
color: var(--pub-report_header_score_warning-text-color);
229229
}
230230

231231
.foldable-icon {

pkg/web_css/lib/src/_search.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
color: $color-searchbar-dark-input-fg;
3333

3434
&::placeholder {
35-
color: #888;
35+
color: var(--pub-input-placeholder-color);
3636
}
3737

3838
.home-banner & {

pkg/web_css/lib/src/_site_header.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
left: 0;
103103
bottom: 0;
104104
right: 0;
105-
background: rgba(0, 0, 0, 0.5);
105+
background: var(--pub-full_page_mask-background-color);
106106
z-index: $z-index-nav-mask;
107107

108108
&.-show {

pkg/web_css/lib/src/_tags.scss

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
&.legacy,
2121
&.retracted {
22-
background: #c0392b;
23-
color: #f8f8f8;
22+
background: var(--pub_tag_simplebadge_warning-background-color);
23+
color: var(--pub_tag_simplebadge_warning-text-color);
2424
}
2525
}
2626

@@ -45,8 +45,8 @@
4545

4646
/* Tag for discontinued packages. */
4747
.-pub-tag-discontinued {
48-
background: #c0392b;
49-
color: #f8f8f8;
48+
background: var(--pub_tag_simplebadge_warning-background-color);
49+
color: var(--pub_tag_simplebadge_warning-text-color);
5050
padding: 2px;
5151

5252
>.-discontinued-main {

pkg/web_css/lib/src/_variables.scss

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,24 @@
1414
--pub-code-text-font_family: "Google Sans Mono", "Roboto Mono", "Source Code Pro", Menlo, "Bitstream Vera Sans Mono", "DejaVu Sans Mono", Monaco, Consolas, monospace;
1515
--pub-badge-default-color: var(--pub-link-text-color);
1616
--pub-badge-red-color: #ff4242;
17+
--pub-carousel-background-color: rgba(0,0,0,0.85);
18+
--pub-carousel_nav-background-color: rgba(0,0,0,0.7);
19+
--pub-carousel_nav_hover-background-color: #4285f4;
20+
--pub-carousel_screenshot-background-color: rgba(0,0,0,0.7);
21+
--pub-carousel_screenshot-text-color: #ffffff;
22+
--pub-collections_icon-background-color: #aeaeae;
23+
--pub-thumbnail_container-background-color: #ffffff;
1724
--pub-copy_feedback-background-color: #fafaff;
1825
--pub-detail_tab-background-color: var(--pub-code-background-color);
1926
--pub-detail_tab-text-color: var(--pub-default-text-color);
2027
--pub-detail_tab-underline-color: #dddddd;
2128
--pub-detail_tab-active-color: #1967d2;
2229
--pub-detail_tab-admin-color: #990000;
30+
--pub-hash_link-text-color: #ccc;
2331
--pub-home_title-text-color: #254a76;
32+
--pub-home_announcement-background-color: #e7f8ff;
33+
--pub-home_banner-text-color: #ffffff;
34+
--pub-home_banner-link-color: #31b0fc;
2435
--pub-home_card-background-color: #ffffff;
2536
--pub-home_card-box_shadow-color: rgba(0, 0, 0, 0.3);
2637
--pub-home_card_fadeout-background-value: linear-gradient(transparent 90%, white);
@@ -31,22 +42,33 @@
3142
--pub-pagination-background-color: var(--pub-code-background-color);
3243
--pub-pagination-active-color: var(--pub-link-text-color);
3344
--pub-pagination-inactive-color: #aaaaaa;
45+
--pub-input-placeholder-color: #888;
3446
--pub-pkg_list_item_hover-background-color: #fafafa;
47+
--pub-pkg_list_recent_item-text-color: #6d7278;
48+
--pub-remove_button-background-color: #ff4242;
49+
--pub-remove_button-text-color: #ffffff;
50+
--pub-report_header_score_error-text-color: #e13701;
51+
--pub-report_header_score_warning-text-color: #ffa500;
3552
--pub-report-hover-background-color: #f0f0f0;
3653
--pub-score_label-text-color: #6d7278;
3754
--pub-score_value-text-color: var(--pub-link-text-color);
55+
--pub-session_warning-background-color: #ffffaa;
3856
--pub-sort_control-background-color: #ffffff;
3957
--pub-sort_control-text-color: var(--pub-default-text-color);
4058
--pub-sort_control_hover-background-color: #f5f5f7;
4159
--pub-sort_control_hover-text-color: var(--pub-default-text-color);
4260
--pub-sort_control_selected-background-color: #e7f8ff;
4361
--pub-sort_control_selected-text-color: var(--pub-default-text-color);
62+
--pub-spinner_frame-background-color: rgba(0, 0, 0, 0.2);
4463
--pub-summary_hover-background-color: color-mix(in srgb, var(--pub-link-text-color), var(--pub-default-background-color) 80%);
4564
--pub-tag_simplebadge-background-color: #f0f0f0;
4665
--pub-tag_simplebadge-text-color: #444444;
66+
--pub_tag_simplebadge_warning-background-color: #c0392b;
67+
--pub_tag_simplebadge_warning-text-color: #f8f8f8;
4768
--pub-tag_sdkbadge-background-color: #e7f8ff;
4869
--pub-tag_sdkbadge-separator-color: rgba(25, 103, 210, 0.5); // #1967d2 + 0.5 opacity;
4970
--pub-tag_sdkbadge-text-color: #1967d2;
71+
--pub-full_page_mask-background-color: rgba(0, 0, 0, 0.5); // Used to make the inaccessible UI parts fade into the background.
5072

5173
// Material Design theme customizations
5274
--mdc-theme-primary: #0175c2;
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:io';
6+
7+
import 'package:test/test.dart';
8+
9+
final _whitespace = RegExp(r'\s+');
10+
11+
const _colors = {
12+
'black',
13+
'red',
14+
'white',
15+
};
16+
17+
void main() {
18+
group('Color restrictions and rules', () {
19+
test('text and bg colors are only defined in _variables.scss', () {
20+
final files = Directory('lib')
21+
.listSync(recursive: true)
22+
.whereType<File>()
23+
.where((f) => f.path.endsWith('.scss'))
24+
// _variables.scss is not checked, this should be the place for all color definitions
25+
.where((f) => !f.path.endsWith('/_variables.scss'))
26+
27+
// _staging_ribbon.scss is only used on staging
28+
.where((f) => !f.path.endsWith('/_staging_ribbon.scss'))
29+
30+
// _footer.scss has only one color theme, skipping for now
31+
// TODO: migrate the variables in this file
32+
.where((f) => !f.path.endsWith('/_footer.scss'))
33+
34+
// dartdoc files are not checked
35+
.where((f) => !f.path.endsWith('/dartdoc.scss'))
36+
.toList();
37+
38+
final badExpressions = <String>[];
39+
40+
for (final file in files) {
41+
var content = file.readAsStringSync();
42+
// remove multi-line comment blocks
43+
content =
44+
content.replaceAll(RegExp(r'\/\*.*\*/', multiLine: true), ' ');
45+
final lines = content.split('\n');
46+
for (var i = 0; i < lines.length; i++) {
47+
final line = lines[i];
48+
// remove single-line comment
49+
final expr =
50+
line.split('//').first.replaceAll(_whitespace, ' ').trim();
51+
if (expr.isEmpty) continue;
52+
53+
// minimal parsing and sanity check
54+
final parts = expr.split(':');
55+
if (parts.length != 2) continue;
56+
final name = parts[0].trim();
57+
var value = parts[1].trim();
58+
59+
// remove known overlapping variable names from checking
60+
value =
61+
value.replaceAll('--pub-badge-red-color', '--pub-badge-color');
62+
63+
if (name.isEmpty || value.isEmpty) continue;
64+
65+
// local mdc overrides are exempted for now
66+
// TODO: move these values to _variables.scss
67+
if (name.startsWith('--mdc-theme-')) {
68+
continue;
69+
}
70+
71+
// border colors, box shadows and text-decorations are exempted for now
72+
// TODO: move these values to _variables.scss
73+
if (name == 'border' ||
74+
name.startsWith('border-') ||
75+
name == 'box-shadow' ||
76+
name == 'text-decoration') {
77+
continue;
78+
}
79+
80+
// detect color patterns
81+
final hasColor = value.contains('#') ||
82+
// TODO: also migrate color- variables
83+
// value.contains(r'$color-') ||
84+
value.contains('rgb(') ||
85+
value.contains('rgba(') ||
86+
value.contains('hsl(') ||
87+
_colors.any((c) => value.contains(c));
88+
if (!hasColor) continue;
89+
90+
badExpressions.add('${file.path} line #${i + 1}: `${expr.trim()}`');
91+
}
92+
}
93+
94+
expect(badExpressions, isEmpty);
95+
});
96+
});
97+
}

0 commit comments

Comments
 (0)