Skip to content

Commit

Permalink
do not inject important for declarations whose ancestor is an atrule. #…
Browse files Browse the repository at this point in the history
  • Loading branch information
ankit committed Jul 29, 2020
1 parent b19a961 commit fcb8a1f
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 18 deletions.
16 changes: 4 additions & 12 deletions src/background/styles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as postcss from 'postcss';
import { appendImportantToDeclarations } from '@stylebot/css';

import BackgroundPageUtils from './utils';

import {
Expand Down Expand Up @@ -63,7 +65,7 @@ class BackgroundPageStyles {
const { styles, defaultStyle } = this.getStylesForPage(tab.url);
this.updateIcon(tab, styles, defaultStyle);

const css = this.addImportantToCss(this.styles[url].css);
const css = appendImportantToDeclarations(this.styles[url].css);
const message: EnableStyleForTab = {
name: 'EnableStyleForTab',
url,
Expand Down Expand Up @@ -161,7 +163,7 @@ class BackgroundPageStyles {

if (matches && this.styles[url]) {
const css = important
? this.addImportantToCss(this.styles[url].css)
? appendImportantToDeclarations(this.styles[url].css)
: this.styles[url].css;

const { enabled, readability } = this.styles[url];
Expand All @@ -182,16 +184,6 @@ class BackgroundPageStyles {
return { styles, defaultStyle };
}

addImportantToCss(css: string): string {
const root = postcss.parse(css);

root.walkDecls(decl => {
decl.important = true;
});

return root.toString();
}

getImportCss(url: string): Promise<string> {
return new Promise(resolve => {
const xhr = new XMLHttpRequest();
Expand Down
88 changes: 87 additions & 1 deletion src/css/__tests__/declaration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const dedent = require('dedent');

import 'jest-fetch-mock';
import { addDeclaration } from '../declaration';
import { addDeclaration, appendImportantToDeclarations } from '../declaration';

describe('declaration', () => {
describe('addDeclaration', () => {
Expand Down Expand Up @@ -141,4 +141,90 @@ describe('declaration', () => {
expect(output).toBe('');
});
});

describe('appendImportantToDeclarations', () => {
it('correctly appends !important to declarations', () => {
const css = dedent`
* {
font-family: Helvetica;
}
a {
color: red;
}
`;

const output = appendImportantToDeclarations(css);
expect(output).toEqual(dedent`
* {
font-family: Helvetica !important;
}
a {
color: red !important;
}
`);
});

it('does not append !important to declarations whose ancestor is an atrule', () => {
const css = dedent`
@font-face {
font-family: 'MS PGothic';
src: local('Meiryo');
}
@page {
margin: 1cm;
}
@keyframes slidein {
from {
transform: translateX(0%);
}
to {
transform: translateX(100%);
}
}
* {
font-family: Helvetica;
}
a {
color: red;
}
`;

const output = appendImportantToDeclarations(css);
expect(output).toEqual(dedent`
@font-face {
font-family: 'MS PGothic';
src: local('Meiryo');
}
@page {
margin: 1cm;
}
@keyframes slidein {
from {
transform: translateX(0%);
}
to {
transform: translateX(100%);
}
}
* {
font-family: Helvetica !important;
}
a {
color: red !important;
}
`);
});
});
});
22 changes: 22 additions & 0 deletions src/css/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,25 @@ export const addDeclaration = (

return css;
};

export const appendImportantToDeclarations = (css: string): string => {
const root = postcss.parse(css);

const isAncestorAnAtRule = (node: postcss.Node): boolean => {
if (node.type === 'atrule') {
return true;
} else if (node.type === 'decl' || node.type === 'rule') {
return isAncestorAnAtRule(node.parent);
} else {
return false;
}
};

root.walkDecls(decl => {
if (!isAncestorAnAtRule(decl)) {
decl.important = true;
}
});

return root.toString();
};
2 changes: 1 addition & 1 deletion src/css/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ export {

export { addGoogleWebFont, cleanGoogleWebFonts } from './webfont';

export { addDeclaration } from './declaration';
export { addDeclaration, appendImportantToDeclarations } from './declaration';
6 changes: 2 additions & 4 deletions src/css/inject-style.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as postcss from 'postcss';
import { appendImportantToDeclarations } from './declaration';
import { getCssWithExpandedImports } from './import';

const getStylesheetId = (id: string) => {
Expand Down Expand Up @@ -32,10 +33,7 @@ export const injectRootIntoDocument = (
root: postcss.Root,
id: string
): void => {
const rootWithImportant = root.clone();
rootWithImportant.walkDecls(decl => (decl.important = true));

const css = rootWithImportant.toString();
const css = appendImportantToDeclarations(root.toString());
injectCSSIntoDocument(css, id);
};

Expand Down

0 comments on commit fcb8a1f

Please sign in to comment.