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

Using RTLCSS plugin instead of CSSJanus - Adding more tests #1

Open
wants to merge 3 commits 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
74 changes: 60 additions & 14 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@
"jest": "^23.5.0"
},
"dependencies": {
"cssjanus": "^1.3.0"
"rtlcss": "^2.4.0"
}
}
14 changes: 8 additions & 6 deletions src/stylis-rtl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow

import cssjanus from 'cssjanus'
// https://rtlcss.com/
import rtlcss from 'rtlcss';

// https://github.com/thysultan/stylis.js#plugins
const STYLIS_CONTEXTS = {
Expand All @@ -10,13 +11,14 @@ 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>;
// Using PREPARATION because comments are getting removed at the PROPERTY level.
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 rtlcss.process(content);
}
}
};
41 changes: 28 additions & 13 deletions src/stylis-rtl.test.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,48 @@
// @flow

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

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

it('allows you to replace a value via inline comments', () => {
expect(
stylisRtlPlugin(STYLIS_PROPERTY_CONTEXT, 'font-size: 16px/*rtl:14px*/;')
).toEqual('font-size: 14px;');
});

it('allows you to skip rules via comments', () => {
const input = `
margin: 0 2px 0 1px;
/* @noflip */
margin: 0 1px 0 2px;
/*rtl:begin:ignore*/
text-align:left;
/*rtl:end:ignore*/
/* just a regular comment */
margin: 0 2px 0 1px;
`
`;

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

it('allows you to prepend a value via comments', () => {
expect(
stylisRtlPlugin(
STYLIS_PROPERTY_CONTEXT,
'font-family: "Droid Sans", sans-serif/*rtl:prepend:"Droid Arabic Kufi",*/;'
)
).toEqual('font-family: "Droid Arabic Kufi","Droid Sans", sans-serif;');
});
});