Skip to content

Commit

Permalink
fix: handle !important, fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Sep 30, 2019
1 parent 5af9b60 commit da5042a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
11 changes: 10 additions & 1 deletion __tests__/__snapshots__/ast.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ Object {
"id": 1,
"rules": Array [
Object {
"important": undefined,
"prop": "border",
"value": "1px solid",
},
Object {
"important": undefined,
"prop": "margin",
"value": "6px 10px",
},
Expand All @@ -58,14 +60,17 @@ Object {
"id": 2,
"rules": Array [
Object {
"important": true,
"prop": "display",
"value": "block",
},
Object {
"important": undefined,
"prop": "position",
"value": "relative",
},
Object {
"important": undefined,
"prop": "width",
"value": "calc(100% - 10px)",
},
Expand All @@ -83,6 +88,7 @@ Object {
"id": 3,
"rules": Array [
Object {
"important": undefined,
"prop": "position",
"value": "absolute",
},
Expand All @@ -100,6 +106,7 @@ Object {
"id": 4,
"rules": Array [
Object {
"important": undefined,
"prop": "position",
"value": "relative",
},
Expand All @@ -117,6 +124,7 @@ Object {
"id": 5,
"rules": Array [
Object {
"important": undefined,
"prop": "color",
"value": "rightColor",
},
Expand All @@ -134,6 +142,7 @@ Object {
"id": 6,
"rules": Array [
Object {
"important": undefined,
"prop": "color",
"value": "red",
},
Expand Down Expand Up @@ -249,7 +258,7 @@ margin: 6px 10px; }
`;

exports[`test ast should remap simple style 1`] = `
".d ~ .e:not(focused) { display: block;
".d ~ .e:not(focused) { display: block !important;
position: relative;
width: calc(100% - 10px); }
"
Expand Down
2 changes: 1 addition & 1 deletion __tests__/ast.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('test ast', () => {
}
.d ~ .e:not(focused){
display: block;
display: block !important;
position: relative;
width: calc(100% - 10px);
}
Expand Down
1 change: 1 addition & 0 deletions src/parser/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {CodeLocation} from "./ranges";
export interface StyleRule {
prop: string;
value: string;
important: boolean;
}

export interface StyleSelector {
Expand Down
11 changes: 7 additions & 4 deletions src/parser/toAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ const getPostfix = (rule: string) => {
let bodyCounter = 1;

const assignBody = (decl: StyleBody, bodies: StyleBodies): StyleBody => {
const d = Object.values(bodies).find(bodyDecl => rangesIntervalEqual(bodyDecl, decl));
const d = Object
.values(bodies)
.find(bodyDecl => rangesIntervalEqual(bodyDecl, decl));

if (d) {
return d;
}

decl.id = bodyCounter++;
bodies[decl.id] = decl;
return decl;
Expand All @@ -55,7 +59,6 @@ export const buildAst = (CSS: string, file: string = ''): SingleStyleAst => {
root.walkAtRules(rule => {
if (rule.name != 'media') {
atParents.add(rule);
//atRules[rule.params] = atRules[rule.params] || []
atRules/*[rule.params]*/.push({
kind: rule.name,
id: rule.params,
Expand Down Expand Up @@ -86,11 +89,11 @@ export const buildAst = (CSS: string, file: string = ''): SingleStyleAst => {
start: createRange(Infinity, Infinity),
end: createRange(0, 0),
};
rule.walkDecls(({prop, value, source}) => {
rule.walkDecls(({prop, value, source, important}) => {
delc.start = localRangeMin(delc.start, source.start);
delc.end = localRangeMax(delc.end, source.end);
delc.rules.push({
prop, value
prop, value, important
});
});

Expand Down

0 comments on commit da5042a

Please sign in to comment.