Giter Club home page Giter Club logo

node-cssjanus's Introduction

npm Tested with QUnit

CSSJanus

Convert CSS stylesheets between left-to-right and right-to-left.

Based the original Google project.

See Interactive demo.

Install

npm install cssjanus

Usage

var cssjanus = require( 'cssjanus' );
var rtlCss = cssjanus.transform( ltrCss );
transform( string css [, Object options ] ) : string

Parameters:

  • css Stylesheet to transform
  • options: Options object (optional)
  • options.transformDirInUrl (Boolean): Transform directions in URLs, such as ltr to rtl. Default: false.
  • options.transformEdgeInUrl (Boolean): Transform edges in URLs, such as left to right. Default: false.

Preventing flipping

If a rule is not meant to be flipped by CSSJanus, use a /* @noflip */ comment to protect the rule.

.rule1 {
  /* Will be converted to margin-right */
  margin-left: 1em;
}
/* @noflip */
.rule2 {
  /* Will be preserved as margin-left */
  margin-left: 1em;
}

Integrations

Who uses CSSJanus?

See also

Contribute

node-cssjanus's People

Contributors

catrope avatar dd32 avatar edg2s avatar jdforrester avatar krinkle avatar mainframe98 avatar moabualruz avatar mohammadyounes avatar ocean90 avatar ricordisamoa avatar scriby avatar tomyam1-personal avatar trevorparscal avatar volker-e avatar yairrand avatar yoavf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-cssjanus's Issues

Gradients in 'background' flipped incorrectly

Hi,

First of all let me thank you for doing a great work. You project made implementing an RTL version of our project much easier. But I did encounter a small issue when flipping gradients: for some reason if the start position is set to "0%" it is changed to "100%". Just "0" remains unchanged as expected.

Here's an example https://cssjanus.github.io/#input/div%20%7B%0A%20%20background%3A%20linear-gradient(to%20left%2C%20%2300ff00%200%25%2C%20%23ff0000%20100%25)%3B%0A%20%20background%3A%20linear-gradient(to%20left%2C%20%2300ff00%200%2C%20%23ff0000%20100%25)%3B%0A%7D

CSSJanus changes right to left in selectors when using the general sibling combinator

When given

#mirage-right-rail-checkbox:checked ~ #mw-navigation #mirage-right-rail-button:before {
  transform: rotate(180deg);
}

CSSJanus outputs

#mirage-left-rail-checkbox:checked ~ #mw-navigation #mirage-right-rail-button:before {
  transform: rotate(180deg);
}

instead of keeping it as is. CSSJanus shouldn't change the id.

It appears this only occurs when the ~ operator is involved. Replacing it with > causes the correct behaviour.

Example:
cssjanus.github.io with the above

Related: #5 and #35.

Rule name gets flipped

LTR CSS
.content > .right:not(.ignore) {
  display:block;
}
Expected:
.content > .right:not(.ignore) {
  display:block;
}
Result:
.content > .left:not(.ignore) {
  display:block;
}

Support values ending in !important

Input:

.foo {
  left: 10px !important;
  float: left !important;
  padding: 1px 2px 3px 4px !important;
}

Expected:

.foo {
  right: 10px !important;
  float: right !important;
  padding: 1px 4px 3px 2px !important;
}

Actual:

.foo {
  right: 10px !important;
  float: right !important;
  padding: 1px 2px 3px 4px !important;
}

The padding rule isn't flipped.

/* @noflip */ doesn't work.

Hi, I'm using stylis-plugin-rtl plugin with StyleSheetManager and i'm trying to exclude some rule but it doesn't work

/* @noflip */
.stx_yaxis {
  /* y-axis price styles */
  font-size: 12px;
  font-family: Helvetica, sans-serif;
  color: #333;
  background-color: #003eb9;
  direction: ltr !important;
}

am I missing something?

mirroring is not enough

Some rules need to be ADDED in addition to just flipping, in order to "zero out" the previous rule.

Example:

.class {
position: absolute;
left:0;
}
.class2 {
margin-left:20px;
}

Should be transformed to:

.class {
position: absolute;
right:0;
left:auto //addition
}
.class2 {
margin-right:20px;
margin-left:auto; //addition
}

License has generic copyright

I'm updating CSSJanus in our codebase to the newest version (thank you!) and noticed the LICENSE.txt file has a generic copyright text 'Copyright [yyyy] [name of copyright owner]'.

Is this intentional? We want to make sure we are respecting the copyright.

Add a flag to swap direction in selectors

When you work on projects that's primarily RTL and you depend on stylesheets from different vendors who code for LTR use mainly, you'll face a small confusing issue in selectors that have hardcoded direction in them like rtl, left, right etc. because when you convert the stylesheets to RTL those selectors will have an opposite effect of they appear to do. This issue might not be important for projects that is mainly LTR but in the other way it counts.

Let's take for example one of the common bootstrap classes that show this problem:

.pull-right {
    float: right
}

will be converted to

.pull-right {
    float: left
}

As you can see, it's a bit confusing when used in html markup with RTL as the primary direction.

What I'm proposing here is to allow changing directions in selectors as an option or two like swapLtrRtlInSelector and swapLeftRightInSelector that are disabled by default.

Abstract unit tests

For after #2, we'll want to abstract the unit tests so that we ensure feature parity and collective regression testing. That way when we tag a version, we can be sure they're compatible.

Top-to-bottom support

Janus phase II – not just LTRTTB -> RTLTTB flipping, but also TTBLTR or TTBRTL.

How to use this package with "create-react-app"

I'm using create-react-app and I'm taking the easy approach to flip the whole CSS file to RTL and load it based on a change in the state.

Changing the CSS file based on a prop in the state if the easy part, but I also want to convert the CSS file to RTL before that step, how to do that with this package?

Release v1.3.0

After #64 lands:

--- a/History.md
+++ b/History.md
@@ -1,5 +1,10 @@
 # Release History
 
+## v1.3.0 / !!TODAY!!
+
+* Drop support for Node.js 4; This release requires Node 6 (Timo Tijhof)
+* Fix unintended flipping of selectors containing a backslash (YairRand)
+* Make cssjanus.js compatible with Closure Compiler (Chris Scribner)
+

Inconsistent flipping behavior breaks custom CSS properties

When the word "right" or "left" is used as part of the name of a custom CSS property, the name is flipped by CSSJanus when the property is assigned, but not when the property is used, those breaking it's functionality.

Example input:

body.theme-dark {
    --right-rail-bg: blue;
}

.right-rail {
    background: var(--right-rail-bg);
}

CSSJanus output:

body.theme-dark {
    --left-rail-bg: blue;
}

.right-rail {
    background: var(--right-rail-bg); /* invalid */
}

It is probably better to not change custom property names at all, but it should at least be consistent.

Supporting testing border-radius with three values

For #20 I would like to have some test with only three values. Example:

[
    "border-radius: 5px 9px 7px",
    "border-radius: 9px 5px 9px 7px"
],
[
    "border-radius: 5px 9px 7px / 3px 4px",
    "border-radius: 9px 5px 9px 7px / 4px 3px"
]

But since cf8f1e0 tests are perfoming round-trip assertions too, which dosn't work for the cases above. Can we add a per-case option to disable these type of tests?

Any way to deal with negative `text-indent`?

Over many years a text-indent: -9999em or similar was a common solution for image widgets, where the text was only meant to be visible to screenreaders.
See for example: https://codepen.io/anon/pen/aqQWQJ

In RTL languages such property/value combinations could lead to unintended visibility of text.
It might be though to identify a clear path on when to switch it over.

Avoid flipping "ltr" or "left" in a data URI

Any URL that looks like a data URI should be skipped over. Otherwise image data will occasionally be corrupted, and in the case of the PHP port I've already seen the regular expressions engine simply blank out the entire stylesheet trying to operate on a data URI which was especially large.

Test case

It's not possible to flip flexbox designs

Shouldn't a class that goes like this:
.class { display: flex; flex-direction: row; justify-content: flex-start; }
be translated into:
.class { display: flex; flex-direction: row-reverse; justify-content: flex-start; }

As of the web demo, this doesn't happen.

Transition issue

Hi guys,

I'm facing a small issue in the following rule:
transform: scaleX(1);

When running it in the cssjanus there is nothing changed, maybe not all developers need to change this property, but some of them ( I'm one of these developers :) ) for sure need it to be changed to:
transform: scaleX(-1);

So I suggest if we can have a comment to be written beside the property and when running into cssjanus it convert it to the desired, something like that one /* @noflip */

It can be like:
transform: scaleX(1); /* @changeto 'transform: scaleX(-1);' */

License?

Can you provide a license for this code?

Words "left" and "right" flipped in selectors before a comma

Consider this example

[class*="span"].pull-right,
.row-fluid [class*="span"].pull-right {
  float: right;
}

the code doesn't take the "," as end of rule and therefor transform it as well.
I'm kindda new to Regex but I came up with this solution for now :

lookAheadNotLetterPattern = '(?![a-zA-Z]|[\\,])'

Flipping for `background` mistakes percentages in color functions for background-position

Consider the following definition:

background: hsl(150 30% 30%);

The percent values are about the saturation and lightness of the generated color and are unrelated to directions.

Yet, when passing it through CSSJanus you get:

background: hsl(150 70% 30%);

which is totally wrong.

Another example:

background: color-mix(in srgb, red 30%, blue);

turns into

background: color-mix(in srgb, red 70%, blue);

The root cause is probably this regex which tries to capture background-position values out of the background shorthand property.


(As a side note, I'm not sure how this is even supposed to work well for actual position values, considering that the default alignment direction is still "left". For example,

background-position: 30%;

turns into

background-position: 70%;

which seems off)

end event listener registration error

Hi all, I've been working on a tool to identify instances of events registered to the wrong object in uses of some JavaScript event-driven APIs, as part of a research project.
The tool flagged line 74 in test/bench.js, on the registration of the "end" event.

The reason I believe this is an error as follows (from looking at the nodejs http API documentation, particularly https://nodejs.org/api/http.html#http_http_get_options_callback):
The return of https.get(...) is an http.ClientRequest object, and res, as the argument to the callback handleResponse passed to get, is an http.IncomingMessage.
“end” is an event on http.IncomingMessage, and not on http.ClientRequest (you even have a registration of a listener for “end” on res, in the body of the handleResponse callback).

A common event indicative of an error to listen to on http.ClientRequest is “timeout”, so my guess is that maybe your registration on line 74 should be for “timeout” instead of “end”.

Translate RegExp's are too relaxed which can result in skipping replacements

It is probably easier to show in code. The following CSS snippet will not flip both transforms:

drawer {
  transform: translate(-100%)
}
drawer {
  transform: translate(100%)
}

This seems to be because the transform RegExp is selecting too much. It will work if both or even one of the rules have a semi-colon after it:

drawer {
  transform: translate(-100%);
}
drawer {
  transform: translate(100%);
}

I'm not sure if it affects any other rules. This seems more problematic for compilers such as Sass which automatically removes the last semi-colon for properties to save some space.

Support "/*!" syntax for @noflip

Other node tasks use special css comments as well, however those css comments usually are of the form
/*! comment */ while this project uses /* @noflip */ , it would be okay if also accepted /*! @noflip */

idea: flip css used in javascript/jquery.

like matching and changing
object.style.paddingRight=5;
object.css('padding-right',5);
object.css({'padding-right':5});
object.animate({'padding-right':5});
object.velocity({'padding-right':5});

Flip transform: scaleX(-1)

Hey!

Thanks for this amazing library.

One suggestion:

Should scaleX(1) be flipped to scaleX(-1) and vice versa?

My use-case is the arrow icon, that points to the left, but in RTL it's on the other side, and the direction of arrow doesn't make sense any more.

Support applying noflip to one consecutive section of the stylesheet

Is it possible to no-flip a set of selectors, for example:

/* rule1 and rule2 will be preserved
/* @noflipStart */
.rule1 {...}
.rule2 {...}
/* @noflipEnd */

My use case is that I put all CSS in one file and than pass it through cssjanus to get an RTL version of it. Some libraries already handle RTL themselves so I don't want to process them. Something like noflipStart and noflipEnd will allow to do that.

Maintain other implementations under the same GitHub organisation

How would you feel about moving the PHP port (from MediaWiki) and original (now abandoned) implementation in Python (from Google) to this repository. We could implement (partially?) shared test cases and other things, and a nice common versioning and release notes scheme.

The nodejs implementation can continue to be published on npm. We could consider publishing the PHP implementation on Composer.

Flip properties in CSS transitions

In CSS transitions, they use other properties for manipulation in order to create a nice, smooth effect. It means in the value, they will have properties like right, and left. They should be flipped too 👍

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.