Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
387 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# media-prefers-color-scheme | ||
|
||
Require implementation of certain styles for selectors with colors. | ||
|
||
**Sources:** | ||
|
||
- [Docs](https://drafts4.csswg.org/mediaqueries-5/#prefers-color-scheme) | ||
- [less mixin](https://brehaut.net/blog/2018/a_dark_mode_less_mixin) | ||
- [Webkit](https://trac.webkit.org/changeset/237156/webkit) | ||
- [Safari TP](https://webkit.org/blog/8475/release-notes-for-safari-technology-preview-68) | ||
- [Mojave](https://www.apple.com/lae/macos/mojave) | ||
|
||
## Options | ||
|
||
### true | ||
|
||
The following pattern are considered violations: | ||
|
||
```css | ||
.foo { | ||
color: red; | ||
} | ||
``` | ||
|
||
```css | ||
.bar { | ||
color: red; | ||
} | ||
.baz { | ||
background-color: red; | ||
} | ||
@media screen and (prefers-color-scheme: dark) { | ||
.baz { | ||
background-color: white; | ||
} | ||
} | ||
``` | ||
|
||
```css | ||
.foo { | ||
color: red; | ||
} | ||
@media screen and (prefers-color-scheme: dark) { | ||
.foo { | ||
background-color: red; | ||
} | ||
} | ||
``` | ||
|
||
The following patterns are _not_ considered violations: | ||
|
||
```css | ||
.foo { | ||
color: red; | ||
} | ||
@media screen and (prefers-color-scheme: dark) { | ||
.foo { | ||
color: white; | ||
} | ||
} | ||
``` | ||
|
||
```css | ||
.bar { | ||
background-color: white; | ||
} | ||
@media screen and (prefers-color-scheme: dark) { | ||
.bar { | ||
background-color: gray; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import rule, { messages, ruleName } from '../index'; | ||
|
||
testRule(rule, { | ||
ruleName, | ||
config: [true], | ||
|
||
accept: [ | ||
{ | ||
code: 'a { }', | ||
}, | ||
{ | ||
code: | ||
'.foo { color: red } @media screen and (prefers-color-scheme: dark) { .foo { color: blue } }', | ||
}, | ||
{ | ||
code: | ||
'.bar { background-color: red } @media screen and (prefers-color-scheme: dark) { .bar { background-color: blue } }', | ||
}, | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: 'a { color: red; }', | ||
message: messages.expected('a'), | ||
line: 1, | ||
column: 4, | ||
}, | ||
{ | ||
code: | ||
'a { color: red; } @media screen and (prefers-color-scheme: dark) { a { background-color: red; } }', | ||
message: messages.expected('a'), | ||
line: 1, | ||
column: 4, | ||
}, | ||
{ | ||
code: '.foo { background-color: red;}', | ||
message: messages.expected('.foo'), | ||
line: 1, | ||
column: 4, | ||
}, | ||
{ | ||
code: | ||
'.bar { color: red; } .baz { background-color: red; } @media screen and (prefers-color-scheme: dark) { .baz { color: blue; } }', | ||
message: messages.expected('.bar'), | ||
line: 1, | ||
column: 4, | ||
}, | ||
{ | ||
code: | ||
'.foo { background-color: red; } @media screen and (prefers-color-scheme) { .foo { color: red; } }', | ||
message: messages.expected('.foo'), | ||
line: 1, | ||
column: 4, | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { utils } from 'stylelint'; | ||
import isStandardSyntaxRule from 'stylelint/lib/utils/isStandardSyntaxRule'; | ||
import isStandardSyntaxSelector from 'stylelint/lib/utils/isStandardSyntaxSelector'; | ||
import isStandardSyntaxAtRule from 'stylelint/lib/utils/isStandardSyntaxAtRule'; | ||
import isCustomSelector from 'stylelint/lib/utils/isCustomSelector'; | ||
|
||
export const ruleName = 'a11y/media-prefers-color-scheme'; | ||
|
||
export const messages = utils.ruleMessages(ruleName, { | ||
expected: selector => `Expected ${selector} is used with @media (prefers-color-scheme)`, | ||
}); | ||
const targetProperties = ['background-color', 'color']; | ||
|
||
function check(selector, node) { | ||
const declarations = node.nodes; | ||
const params = node.parent.params; | ||
const parentNodes = node.parent.nodes; | ||
|
||
if (!declarations) return true; | ||
|
||
if (!isStandardSyntaxSelector(selector)) { | ||
return true; | ||
} | ||
|
||
if (isCustomSelector(selector)) { | ||
return true; | ||
} | ||
|
||
let currentSelector = null; | ||
|
||
const declarationsIsMatched = declarations.some(declaration => { | ||
const noMatchedParams = !params || params.indexOf('prefers-color-scheme') === -1; | ||
const index = targetProperties.indexOf(declaration.prop); | ||
currentSelector = targetProperties[index]; | ||
|
||
return index >= 0 && noMatchedParams; | ||
}); | ||
|
||
if (!declarationsIsMatched) return true; | ||
|
||
if (declarationsIsMatched) { | ||
const parentMatchedNode = parentNodes.some(parentNode => { | ||
if (!parentNode || !parentNode.nodes) return; | ||
return parentNode.nodes.some(childrenNode => { | ||
const childrenNodes = childrenNode.nodes; | ||
|
||
if ( | ||
!parentNode.params || | ||
!Array.isArray(childrenNodes) || | ||
selector !== childrenNode.selector | ||
) | ||
return false; | ||
|
||
const matchedChildrenNodes = childrenNodes.some(declaration => { | ||
const index = targetProperties.indexOf(declaration.prop); | ||
if (currentSelector !== targetProperties[index]) return false; | ||
|
||
return index >= 0 && parentNode.params.indexOf('prefers-color-scheme') >= 0; | ||
}); | ||
|
||
return matchedChildrenNodes; | ||
}); | ||
}); | ||
|
||
if (!parentMatchedNode) return false; | ||
|
||
return true; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
export default function(actual) { | ||
return (root, result) => { | ||
const validOptions = utils.validateOptions(result, ruleName, { actual }); | ||
|
||
if (!validOptions || !actual) { | ||
return; | ||
} | ||
|
||
root.walk(node => { | ||
let selector = null; | ||
|
||
if (node.type === 'rule') { | ||
if (!isStandardSyntaxRule(node)) { | ||
return; | ||
} | ||
|
||
selector = node.selector; | ||
} else if (node.type === 'atrule' && node.name === 'page' && node.params) { | ||
if (!isStandardSyntaxAtRule(node)) { | ||
return; | ||
} | ||
|
||
selector = node.params; | ||
} | ||
|
||
if (!selector) { | ||
return; | ||
} | ||
|
||
const isAccepted = check(selector, node); | ||
|
||
if (!isAccepted) { | ||
utils.report({ | ||
index: node.lastEach, | ||
message: messages.expected(selector), | ||
node, | ||
ruleName, | ||
result, | ||
}); | ||
} | ||
}); | ||
}; | ||
} |
Oops, something went wrong.