Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix shorthand flipping, handling of @noflip directives #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 46 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
"babel-preset-flow": "^6.23.0",
"flow-bin": "^0.78.0",
"flow-typed": "^2.5.1",
"jest": "^23.5.0"
"jest": "^23.5.0",
"prettier": "^1.19.1",
"stylis": "^3.5.4"
},
"dependencies": {
"cssjanus": "^1.3.0"
Expand Down
15 changes: 9 additions & 6 deletions src/stylis-rtl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow

import cssjanus from 'cssjanus'
import cssjanus from "cssjanus";

// https://github.com/thysultan/stylis.js#plugins
const STYLIS_CONTEXTS = {
Expand All @@ -10,13 +10,16 @@ const STYLIS_CONTEXTS = {
PROPERTY: 1,
SELECTOR_BLOCK: 2,
AT_RULE: 3
}
};

export type StylisContextType = $Values<typeof STYLIS_CONTEXTS>
export const STYLIS_PROPERTY_CONTEXT = STYLIS_CONTEXTS.PROPERTY
export type StylisContextType = $Values<typeof STYLIS_CONTEXTS>;

// We need to apply cssjanus as early as possible to capture the noflip directives if used
// (they are not present at the PROPERTY, SELECTOR_BLOCK, or POST_PROCESS steps)
export const STYLIS_PROPERTY_CONTEXT = STYLIS_CONTEXTS.PREPARATION;

export default (context: StylisContextType, content: string): ?string => {
if (context === STYLIS_PROPERTY_CONTEXT) {
return cssjanus.transform(content)
return cssjanus.transform(content);
}
}
};
73 changes: 60 additions & 13 deletions src/stylis-rtl.test.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,80 @@
// @flow

import stylisRtlPlugin, { STYLIS_PROPERTY_CONTEXT } from './stylis-rtl'
import Stylis from "stylis";
import stylisRtlPlugin, { STYLIS_PROPERTY_CONTEXT } from "./stylis-rtl";

describe('Stylis RTL Plugin', () => {
it('converts LTR to RTL', () => {
const stylis = new Stylis();

stylis.use(stylisRtlPlugin);

describe("Stylis RTL Plugin", () => {
it("converts LTR to RTL", () => {
expect(
stylisRtlPlugin(STYLIS_PROPERTY_CONTEXT, 'padding-left: 2px;')
).toEqual('padding-right: 2px;')
stylisRtlPlugin(STYLIS_PROPERTY_CONTEXT, "padding-left: 2px;")
).toEqual("padding-right: 2px;");
expect(
stylisRtlPlugin(STYLIS_PROPERTY_CONTEXT, 'margin: 0 1px 0 2px;')
).toEqual('margin: 0 2px 0 1px;')
})
stylisRtlPlugin(STYLIS_PROPERTY_CONTEXT, "margin: 0 1px 0 2px;")
).toEqual("margin: 0 2px 0 1px;");
});

it('allows you to skip rules via comments', () => {
it("allows you to skip rules via comments", () => {
const input = `
margin: 0 2px 0 1px;
/* @noflip */
margin: 0 1px 0 2px;
/* just a regular comment */
margin: 0 2px 0 1px;
`
`;

const output = `
margin: 0 1px 0 2px;
/* @noflip */
margin: 0 1px 0 2px;
/* just a regular comment */
margin: 0 1px 0 2px;
`;
expect(stylisRtlPlugin(STYLIS_PROPERTY_CONTEXT, input)).toEqual(output);
});
});

describe("integration test with stylis", () => {
it("flips simple rules", () => {
expect(
stylis(
".a",
`
padding-left: 5px;
margin-right: 5px;
border-left: 1px solid red;
`
)
).toMatchInlineSnapshot(
`".a{padding-right:5px;margin-left:5px;border-right:1px solid red;}"`
);
});

it("flips shorthands", () => {
expect(
stylis(
".a",
`
padding: 0 5px 0 0;
margin: 0 0 0 5px;
`
)
).toMatchInlineSnapshot(`".a{padding:0 0 0 5px;margin:0 5px 0 0;}"`);
});

it("handles noflip directives", () => {
expect(
stylis(
".a",
`
/* @noflip */
padding: 0 5px 0 0;
margin: 0 0 0 5px;
`
expect(stylisRtlPlugin(STYLIS_PROPERTY_CONTEXT, input)).toEqual(output)
})
})
)
).toMatchInlineSnapshot(`".a{padding:0 5px 0 0;margin:0 5px 0 0;}"`);
});
});