Skip to content

Commit

Permalink
enhancements - support dot less rtl plugin like property names
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeapage committed Jul 24, 2015
1 parent 2fe6b89 commit 9b62113
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ node_modules

# Users Environment Variables
.lock-wscript

#Webstorm
.idea
45 changes: 36 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ function LessPluginRTL(options) {

LessPluginRTL.prototype = {
install: function(less, pluginManager) {
if (this.options.dir === "RTL") {
var RTLPlugin = getRTLPlugin(less);
pluginManager.addVisitor(new RTLPlugin());
var RTLPlugin = getRTLPlugin(less);
pluginManager.addVisitor(new RTLPlugin(this.options));

if (this.options.vars !== false) {
pluginManager.addPreProcessor(new RTLVariablePlugin(this.options.dir === "RTL"));
}
pluginManager.addPreProcessor(new RTLVariablePlugin(this.options.dir === "RTL"));
},
printUsage: function () {
console.log("RTL Plugin");
Expand All @@ -25,12 +26,38 @@ LessPluginRTL.prototype = {
options = {dir: "RTL"};
}
if (typeof options === "string") {
var args = options.match(/dir=(RTL|LTR)/);
if (!args) {
throw new Error("Invalid options for ");
}
options = {dir: args[1]};
var spaceSepOptions = options.split(" ");
options = { };
spaceSepOptions.forEach(function(arg) {
var match = arg.match(/^(--)?dir=(RTL|LTR)$/);
if (match) {
options.dir = match[1];
return;
}
match = arg.match(/^(--)?vars(=(true|false))?$/);
if (match) {
options.vars = match[1] !== false;
return;
}

match = arg.match(/^(--)?auto-reverse(=(true|false))?$/);
if (match) {
options.autoReverse = match[1] !== false;
return;
}

throw new Error("Invalid options - '" + arg + "'");
});
}

options.autoReverse = options.autoReverse !== false;
options.vars = options.vars !== false;
options.dir = options.dir || "LTR";

if (options.dir !== "LTR" && options.dir !== "RTL") {
throw new Error("Bad direction - use LTR or RTL");
}

return options;
},
minVersion: [2, 4, 0]
Expand Down
79 changes: 63 additions & 16 deletions lib/rtl-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module.exports = function(less) {
var shortHandProperties = ["margin", "border-width", "padding", "border-radius", "border", "border-style"];
var keywordProperties = ["float", "text-align"];

function RTLPlugin() {
function RTLPlugin(options) {
this._options = options;
this._visitor = new less.visitors.Visitor(this);
};

Expand All @@ -25,30 +26,76 @@ module.exports = function(less) {
return this._visitor.visit(root);
},
visitRule: function (ruleNode, visitArgs) {
if (!ruleNode.variable && ruleNode.name.match(/(^|-)(left|right)($|-)/)) {

if (ruleNode.variable) {
return ruleNode;
}

var nodeName = ruleNode.name,
wantedDir = this._options.dir,
doReverse = this._options.autoReverse && wantedDir === "RTL",
doRemove = false,
match;

if (match = nodeName.match(/^-rtl-reverse-(.*)$/)) {
doReverse = wantedDir === "RTL";
nodeName = match[1];
}
else if (match = nodeName.match(/^-ltr-reverse-(.*)$/)) {
doReverse = wantedDir === "LTR";
nodeName = match[1];
}
else if (match = nodeName.match(/^(-rtl-ltr-|-ltr-rtl-)(.*)$/)) {
doReverse = false;
nodeName = match[1];
}
else if (match = nodeName.match(/^-ltr-(.*)$/)) {
doRemove = wantedDir === "RTL";
nodeName = match[1];
}
else if (match = nodeName.match(/^-rtl-(.*)$/)) {
doRemove = wantedDir === "LTR";
nodeName = match[1];
}

if (doRemove) {
return;
}

if (!doReverse && !doRemove && nodeName === ruleNode.name) {
return ruleNode;
}

if (doReverse && nodeName.match(/(^|-)(left|right)($|-)/)) {
nodeName = nodeName.replace(/(^|-)(left|right)($|-)/, function(allOfMatch, leftPart, replacePart, rightPart) {
if (replacePart === "left") {
replacePart = "right";
} else {
replacePart = "left";
}
return leftPart + replacePart + rightPart;
});
}

if (doReverse && keywordProperties.indexOf(nodeName) >= 0) {
this._reverseKeywords = true;
}
else if (doReverse && shortHandProperties.indexOf(nodeName) >= 0) {
this._shortHandReorder = true;
}

if (nodeName !== ruleNode.name) {
return new less.tree.Rule(
ruleNode.name.replace(/(^|-)(left|right)($|-)/, function(allOfMatch, leftPart, replacePart, rightPart) {
if (replacePart === "left") {
replacePart = "right";
} else {
replacePart = "left";
}
return leftPart + replacePart + rightPart;
}),
nodeName,
ruleNode.value,
ruleNode.important,
ruleNode.merge,
ruleNode.index,
ruleNode.currentFileInfo,
ruleNode.inline,
ruleNode.variable);
} else
if (keywordProperties.indexOf(ruleNode.name) >= 0) {
this._reverseKeywords = true;
} else
if (shortHandProperties.indexOf(ruleNode.name) >= 0) {
this._shortHandReorder = true;
}

return ruleNode;
},
visitRuleOut: function () {
Expand Down
2 changes: 1 addition & 1 deletion lib/rtl-variable-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function RTLVariablePlugin(isRTL) {
}

RTLVariablePlugin.prototype.process = function(src, extra) {
var variable = "@rtl: " + String(this.isRTL) + "; @ltr: " + String(!this.isRTL) + ";\n";
var variable = "@rtl: " + String(this.isRTL) + "; @ltr: " + String(!this.isRTL) + ";\n" + "@dir:" + (this.isRTL ? "rtl" : "ltr") + ";\n";
var contentsIgnoredChars = extra.imports.contentsIgnoredChars;
var filename = extra.fileInfo.filename;
contentsIgnoredChars[filename] = contentsIgnoredChars[filename] || 0;
Expand Down
5 changes: 5 additions & 0 deletions test/css/ltr/test.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.reverse {
float: left;
float: right /*comment*/;
float: left !important;
margin-left: 5px;
padding-right: 3px;
font-size: 12px;
Expand All @@ -13,4 +14,8 @@
margin: 6px 7px;
margin: 1px 7px 3px;
margin: 1px 2px 3px 4px;
margin: 1px 2px 3px 4px !important;
}
.ltr-only {
padding: 4px;
}
27 changes: 27 additions & 0 deletions test/css/properties-rtl/test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.reverse {
float: right;
float: left;
float: left;
margin-right: 5px;
margin-left: 5px;
margin-left: 5px;
font-size: 12px;
/* unchanged*/
right: 3px;
left: 3px;
left: 3px;
}
.shorthands {
margin: 5px;
margin: 6px 7px;
margin: 1px 7px 3px;
margin: 1px 4px 3px 2px;
margin: 5px;
margin: 6px 7px;
margin: 1px 7px 3px;
margin: 1px 2px 3px 4px;
margin: 5px;
margin: 6px 7px;
margin: 1px 7px 3px;
margin: 1px 2px 3px 4px;
}
2 changes: 2 additions & 0 deletions test/css/rtl/test.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.reverse {
float: right;
float: left /*comment*/;
float: right !important;
margin-right: 5px;
padding-left: 3px;
font-size: 12px;
Expand All @@ -13,6 +14,7 @@
margin: 6px 7px;
margin: 1px 7px 3px;
margin: 1px 4px 3px 2px;
margin: 1px 4px 3px 2px !important;
}
.rtl-only {
margin: 3px;
Expand Down
10 changes: 8 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ var less = require("less"),
lessTester = lessTest(),
Plugin = require('../lib'),
rtlPlugin = new Plugin(),
ltrPlugin = new Plugin("dir=LTR"),
ltrPlugin = new Plugin("dir=LTR --auto-reverse vars=true"),
propertiesRtlPlugin = new Plugin("dir=RTL --auto-reverse=false"),
stylize = less.lesscHelper.stylize;

console.log("\n" + stylize("LESS - RTL", 'underline') + "\n");
Expand All @@ -13,7 +14,12 @@ lessTester.runTestSet(
"rtl/");
lessTester.runTestSet(
{strictMath: true, relativeUrls: true, silent: true, plugins: [ltrPlugin] },
"ltr/")
"ltr/");

lessTester.runTestSet(
{strictMath: true, relativeUrls: true, silent: true, plugins: [propertiesRtlPlugin] },
"properties-rtl/");


if (lessTester.finish) {
lessTester.finish();
Expand Down
7 changes: 7 additions & 0 deletions test/less/ltr/test.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.reverse {
float: left;
float: right/*comment*/;
float: left !important;
margin-left: 5px;
padding-right: 3px;
font-size: 12px; /* unchanged*/
Expand All @@ -20,9 +21,15 @@
margin: @vertical @horizontal;
margin: @top @horizontal @bottom;
margin: @top @right @bottom @left;
margin: @top @right @bottom @left !important;
}
& when (@rtl) {
.rtl-only {
margin: 3px;
}
}
& when (@dir = ltr) {
.ltr-only {
padding: 4px;
}
}
47 changes: 47 additions & 0 deletions test/less/properties-rtl/test.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
.reverse {
-ltr-reverse-float: left;
-ltr-float: left;
-rtl-reverse-float: left;
-rtl-float: left;

-ltr-reverse-margin-left: 5px;
-ltr-margin-left: 5px;
-rtl-reverse-margin-left: 5px;
-rtl-margin-left: 5px;

font-size: 12px; /* unchanged*/

-ltr-reverse-left: 3px;
-ltr-left: 3px;
-rtl-reverse-left: 3px;
-rtl-left: 3px;
}
.shorthands {
@top: 1px;
@right: 2px;
@bottom: 3px;
@left: 4px;
@all: 5px;
@vertical: 6px;
@horizontal: 7px;

-ltr-reverse-margin: @all;
-ltr-reverse-margin: @vertical @horizontal;
-ltr-reverse-margin: @top @horizontal @bottom;
-ltr-reverse-margin: @top @right @bottom @left;

-ltr-margin: @all;
-ltr-margin: @vertical @horizontal;
-ltr-margin: @top @horizontal @bottom;
-ltr-margin: @top @right @bottom @left;

-rtl-reverse-margin: @all;
-rtl-reverse-margin: @vertical @horizontal;
-rtl-reverse-margin: @top @horizontal @bottom;
-rtl-reverse-margin: @top @right @bottom @left;

-rtl-margin: @all;
-rtl-margin: @vertical @horizontal;
-rtl-margin: @top @horizontal @bottom;
-rtl-margin: @top @right @bottom @left;
}
9 changes: 8 additions & 1 deletion test/less/rtl/test.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.reverse {
float: left;
float: right/*comment*/;
float: right !important;
margin-left: 5px;
padding-right: 3px;
font-size: 12px; /* unchanged*/
Expand All @@ -20,9 +21,15 @@
margin: @vertical @horizontal;
margin: @top @horizontal @bottom;
margin: @top @right @bottom @left;
margin: @top @right @bottom @left !important;
}
& when (@rtl) {
.rtl-only {
margin: 3px;
}
}
}
& when (@dir = ltr) {
.ltr-only {
padding: 4px;
}
}

0 comments on commit 9b62113

Please sign in to comment.