Skip to content

Commit

Permalink
fix(stylehacks): better handle uppercase (#704)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Jan 31, 2019
1 parent ceb24a1 commit 8d48058
Show file tree
Hide file tree
Showing 21 changed files with 166 additions and 4 deletions.
8 changes: 8 additions & 0 deletions packages/stylehacks/src/__tests__/bodyEmpty.js
Expand Up @@ -8,3 +8,11 @@ test(
'',
{target: 'firefox2', unaffected: 'chrome58'}
);

test(
'firefox empty body hack (uppercase)',
processCSS,
'BODY:EMPTY h1 { color: red }',
'',
{target: 'firefox2', unaffected: 'chrome58'}
);
Expand Up @@ -9,6 +9,14 @@ test(
{target: 'ie6', unaffected: 'ie8'}
);

test(
'html combinator comment body hack (uppercase)',
processCSS,
'HTML > /**/ BODY H1 { color: red }',
'',
{target: 'ie6', unaffected: 'ie8'}
);

test(
'html combinator comment body hack (2)',
processCSS,
Expand Down
8 changes: 8 additions & 0 deletions packages/stylehacks/src/__tests__/htmlFirstChild.js
Expand Up @@ -8,3 +8,11 @@ test(
'',
{target: 'opera9', unaffected: 'chrome58'}
);

test(
'opera html:first-child hack (uppercase)',
processCSS,
'HTML:FIRST-CHILD H1 { color: red }',
'',
{target: 'opera9', unaffected: 'chrome58'}
);
8 changes: 8 additions & 0 deletions packages/stylehacks/src/__tests__/important.js
Expand Up @@ -8,3 +8,11 @@ test(
'h1 { }',
{target: 'ie6', unaffected: 'ie8'}
);

test(
'ie 5.5-7 important hack (uppercase)',
processCSS,
'H1 { COLOR: RED !IE }',
'H1 { }',
{target: 'ie6', unaffected: 'ie8'}
);
8 changes: 8 additions & 0 deletions packages/stylehacks/src/__tests__/leadingStar.js
Expand Up @@ -38,3 +38,11 @@ test(
'h1 {}',
opts
);

test(
'should also handle @hacks (uppercase)',
processCSS,
'h1 { @COLOR: red }',
'h1 {}',
opts
);
34 changes: 34 additions & 0 deletions packages/stylehacks/src/__tests__/leadingUnderscore.js
Expand Up @@ -9,6 +9,14 @@ test(
{target: 'ie6', unaffected: 'ie7'}
);

test(
'ie 6 underscore hack (uppercase)',
processCSS,
'h1 { _COLOR: red }',
'h1 { }',
{target: 'ie6', unaffected: 'ie7'}
);

test(
'ie 6 hyphen hack',
processCSS,
Expand All @@ -17,6 +25,14 @@ test(
{target: 'ie6', unaffected: 'ie7'}
);

test(
'ie 6 hyphen hack (uppercase)',
processCSS,
'h1 { -COLOR: red }',
'h1 { }',
{target: 'ie6', unaffected: 'ie7'}
);

test(
'prefixed value must be ignored',
processCSS,
Expand All @@ -26,6 +42,15 @@ test(
0
);

test(
'prefixed value must be ignored (uppercase)',
processCSS,
'h1 { -MOZ-TAB-SIZE: 10px }',
'h1 { -MOZ-TAB-SIZE: 10px }',
{target: 'ie6', unaffected: 'ie7'},
0
);

test(
'custom property must be ignored',
processCSS,
Expand All @@ -34,3 +59,12 @@ test(
{target: 'ie6', unaffected: 'ie7'},
0
);

test(
'custom property must be ignored (uppercase)',
processCSS,
'h1 { --COLOR-FOOBAR: #000; }',
'h1 { --COLOR-FOOBAR: #000; }',
{target: 'ie6', unaffected: 'ie7'},
0
);
8 changes: 8 additions & 0 deletions packages/stylehacks/src/__tests__/mediaSlash0.js
Expand Up @@ -8,3 +8,11 @@ test(
'',
{target: 'ie8', unaffected: 'ie9'}
);

test(
'ie 8 media \\0screen hack (uppercase)',
processCSS,
'@MEDIA \\0SCREEN { h1 { color: red } }',
'',
{target: 'ie8', unaffected: 'ie9'}
);
8 changes: 8 additions & 0 deletions packages/stylehacks/src/__tests__/mediaSlash0Slash9.js
Expand Up @@ -8,3 +8,11 @@ test(
'',
{target: 'ie6', unaffected: 'ie9'}
);

test(
'ie <= 8 media \\0screen\\,screen\\9 hack (uppercase)',
processCSS,
'@MEDIA \\0SCREEN\\,SCREEN\\9 { h1 { color: red } }',
'',
{target: 'ie6', unaffected: 'ie9'}
);
8 changes: 8 additions & 0 deletions packages/stylehacks/src/__tests__/mediaSlash9.js
Expand Up @@ -8,3 +8,11 @@ test(
'',
{target: 'ie6', unaffected: 'ie8'}
);

test(
'ie 7 media screen\\9 hack (uppercase)',
processCSS,
'@MEDIA SCREEN\\9 { h1 { color: red } }',
'',
{target: 'ie6', unaffected: 'ie8'}
);
16 changes: 16 additions & 0 deletions packages/stylehacks/src/__tests__/slash9.js
Expand Up @@ -9,10 +9,26 @@ test(
{target: 'ie8', unaffected: 'chrome58'}
);

test(
'ie 6 underscore hack (uppercase)',
processCSS,
'h1 { MARGIN-TOP: 1PX\\9; }',
'h1 { }',
{target: 'ie8', unaffected: 'chrome58'}
);

test(
'ie 6 underscore hack',
processCSS,
'h1 { margin-top/*\\**/: 1px\\9; }',
'h1 { }',
{target: 'ie8', unaffected: 'chrome58'}
);

test(
'ie 6 underscore hack (uppercase)',
processCSS,
'h1 { MARGIN-TOP/*\\**/: 1PX\\9; }',
'h1 { }',
{target: 'ie8', unaffected: 'chrome58'}
);
8 changes: 8 additions & 0 deletions packages/stylehacks/src/__tests__/starHtml.js
Expand Up @@ -8,3 +8,11 @@ test(
'',
{target: 'ie6', unaffected: 'ie7'}
);

test(
'ie 5.5-6 * html hack (uppercase)',
processCSS,
'* HTML H1 { color: red }',
'',
{target: 'ie6', unaffected: 'ie7'}
);
16 changes: 16 additions & 0 deletions packages/stylehacks/src/__tests__/trailingSlashComma.js
Expand Up @@ -9,10 +9,26 @@ test(
{target: 'ie6', unaffected: 'ie8'}
);

test(
'ie 5.5-7 trailing comma hack (uppercase)',
processCSS,
'H1, { COLOR: RED }',
'',
{target: 'ie6', unaffected: 'ie8'}
);

test(
'ie 5.5-7 trailing slash hack',
processCSS,
'h1\\ { color: red }',
'',
{target: 'ie6', unaffected: 'ie8'}
);

test(
'ie 5.5-7 trailing slash hack (uppercase)',
processCSS,
'H1\\ { COLOR: RED }',
'',
{target: 'ie6', unaffected: 'ie8'}
);
3 changes: 2 additions & 1 deletion packages/stylehacks/src/exists.js
@@ -1,4 +1,5 @@
export default function exists (selector, index, value) {
const node = selector.at(index);
return node && node.value === value;

return node && node.value.toLowerCase() === value;
}
6 changes: 6 additions & 0 deletions packages/stylehacks/src/index.js
Expand Up @@ -15,19 +15,24 @@ const stylehacks = postcss.plugin('stylehacks', (opts = {}) => {
const applied = browsers.some(browser => {
return hack.targets.some(target => browser === target);
});

if (applied) {
return list;
}

return [...list, hack];
}, []);

css.walk(node => {
processors.forEach(proc => {
if (!~proc.nodeTypes.indexOf(node.type)) {
return;
}

if (opts.lint) {
return proc.detectAndWarn(node);
}

return proc.detectAndResolve(node);
});
});
Expand All @@ -37,6 +42,7 @@ const stylehacks = postcss.plugin('stylehacks', (opts = {}) => {
stylehacks.detect = node => {
return plugins.some(Plugin => {
const hack = new Plugin();

return hack.any(node);
});
};
Expand Down
2 changes: 2 additions & 0 deletions packages/stylehacks/src/isMixin.js
@@ -1,8 +1,10 @@
export default function isMixin (node) {
const {selector} = node;

// If the selector ends with a ':' it is likely a part of a custom mixin.
if (!selector || selector[selector.length - 1] === ':') {
return true;
}

return false;
}
8 changes: 8 additions & 0 deletions packages/stylehacks/src/plugin.js
Expand Up @@ -12,26 +12,33 @@ export default function plugin (targets, nodeTypes, detect) {
message: `Bad ${metadata.identifier}: ${metadata.hack}`,
browsers: this.targets,
});

this.nodes.push(node);
}

any (node) {
if (~this.nodeTypes.indexOf(node.type)) {
detect.apply(this, arguments);

return !!node._stylehacks;
}

return false;
}

detectAndResolve (...args) {
this.nodes = [];

detect.apply(this, args);

return this.resolve();
}

detectAndWarn (...args) {
this.nodes = [];

detect.apply(this, args);

return this.warn();
}

Expand All @@ -42,6 +49,7 @@ export default function plugin (targets, nodeTypes, detect) {
warn () {
return this.nodes.forEach(node => {
const {message, browsers, identifier, hack} = node._stylehacks;

return node.warn(this.result, message, {browsers, identifier, hack});
});
}
Expand Down
2 changes: 2 additions & 0 deletions packages/stylehacks/src/plugins/leadingUnderscore.js
Expand Up @@ -6,12 +6,14 @@ import {DECL} from '../dictionary/postcss';

export default plugin([IE_6], [DECL], function (decl) {
const {before} = decl.raws;

if (before && ~before.indexOf('_')) {
this.push(decl, {
identifier: PROPERTY,
hack: `${before.trim()}${decl.prop}`,
});
}

if (decl.prop[0] === '-' && decl.prop[1] !== '-' && postcss.vendor.prefix(decl.prop) === '') {
this.push(decl, {
identifier: PROPERTY,
Expand Down
3 changes: 2 additions & 1 deletion packages/stylehacks/src/plugins/mediaSlash0.js
Expand Up @@ -5,7 +5,8 @@ import {ATRULE} from '../dictionary/postcss';

export default plugin([IE_8], [ATRULE], function (rule) {
const params = rule.params.trim();
if (params === '\\0screen') {

if (params.toLowerCase() === '\\0screen') {
this.push(rule, {
identifier: MEDIA_QUERY,
hack: params,
Expand Down
3 changes: 2 additions & 1 deletion packages/stylehacks/src/plugins/mediaSlash0Slash9.js
Expand Up @@ -5,7 +5,8 @@ import {ATRULE} from '../dictionary/postcss';

export default plugin([IE_5_5, IE_6, IE_7, IE_8], [ATRULE], function (rule) {
const params = rule.params.trim();
if (params === '\\0screen\\,screen\\9') {

if (params.toLowerCase() === '\\0screen\\,screen\\9') {
this.push(rule, {
identifier: MEDIA_QUERY,
hack: params,
Expand Down
3 changes: 2 additions & 1 deletion packages/stylehacks/src/plugins/mediaSlash9.js
Expand Up @@ -5,7 +5,8 @@ import {ATRULE} from '../dictionary/postcss';

export default plugin([IE_5_5, IE_6, IE_7], [ATRULE], function (rule) {
const params = rule.params.trim();
if (params === 'screen\\9') {

if (params.toLowerCase() === 'screen\\9') {
this.push(rule, {
identifier: MEDIA_QUERY,
hack: params,
Expand Down
2 changes: 2 additions & 0 deletions packages/stylehacks/src/plugins/trailingSlashComma.js
Expand Up @@ -8,8 +8,10 @@ export default plugin([IE_5_5, IE_6, IE_7], [RULE], function (rule) {
if (isMixin(rule)) {
return;
}

const {selector} = rule;
const trim = selector.trim();

if (
trim.lastIndexOf(',') === selector.length - 1 ||
trim.lastIndexOf('\\') === selector.length - 1
Expand Down

0 comments on commit 8d48058

Please sign in to comment.