mirror of
https://github.com/less/less.js.git
synced 2026-01-24 06:38:05 -05:00
137 lines
3.8 KiB
JavaScript
137 lines
3.8 KiB
JavaScript
module.exports = function(tree, unitConversions) {
|
|
|
|
var Unit = function (numerator, denominator, backupUnit) {
|
|
this.numerator = numerator ? numerator.slice(0).sort() : [];
|
|
this.denominator = denominator ? denominator.slice(0).sort() : [];
|
|
this.backupUnit = backupUnit;
|
|
};
|
|
|
|
Unit.prototype = {
|
|
type: "Unit",
|
|
clone: function () {
|
|
return new Unit(this.numerator.slice(0), this.denominator.slice(0), this.backupUnit);
|
|
},
|
|
genCSS: function (env, output) {
|
|
if (this.numerator.length >= 1) {
|
|
output.add(this.numerator[0]);
|
|
} else
|
|
if (this.denominator.length >= 1) {
|
|
output.add(this.denominator[0]);
|
|
} else
|
|
if ((!env || !env.strictUnits) && this.backupUnit) {
|
|
output.add(this.backupUnit);
|
|
}
|
|
},
|
|
toCSS: tree.toCSS,
|
|
|
|
toString: function () {
|
|
var i, returnStr = this.numerator.join("*");
|
|
for (i = 0; i < this.denominator.length; i++) {
|
|
returnStr += "/" + this.denominator[i];
|
|
}
|
|
return returnStr;
|
|
},
|
|
|
|
compare: function (other) {
|
|
return this.is(other.toString()) ? 0 : -1;
|
|
},
|
|
|
|
is: function (unitString) {
|
|
return this.toString() === unitString;
|
|
},
|
|
|
|
isLength: function () {
|
|
return Boolean(this.toCSS().match(/px|em|%|in|cm|mm|pc|pt|ex/));
|
|
},
|
|
|
|
isEmpty: function () {
|
|
return this.numerator.length === 0 && this.denominator.length === 0;
|
|
},
|
|
|
|
isSingular: function() {
|
|
return this.numerator.length <= 1 && this.denominator.length === 0;
|
|
},
|
|
|
|
map: function(callback) {
|
|
var i;
|
|
|
|
for (i = 0; i < this.numerator.length; i++) {
|
|
this.numerator[i] = callback(this.numerator[i], false);
|
|
}
|
|
|
|
for (i = 0; i < this.denominator.length; i++) {
|
|
this.denominator[i] = callback(this.denominator[i], true);
|
|
}
|
|
},
|
|
|
|
usedUnits: function() {
|
|
var group, result = {}, mapUnit;
|
|
|
|
mapUnit = function (atomicUnit) {
|
|
/*jshint loopfunc:true */
|
|
if (group.hasOwnProperty(atomicUnit) && !result[groupName]) {
|
|
result[groupName] = atomicUnit;
|
|
}
|
|
|
|
return atomicUnit;
|
|
};
|
|
|
|
for (var groupName in unitConversions) {
|
|
if (unitConversions.hasOwnProperty(groupName)) {
|
|
group = unitConversions[groupName];
|
|
|
|
this.map(mapUnit);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
},
|
|
|
|
cancel: function () {
|
|
var counter = {}, atomicUnit, i, backup;
|
|
|
|
for (i = 0; i < this.numerator.length; i++) {
|
|
atomicUnit = this.numerator[i];
|
|
if (!backup) {
|
|
backup = atomicUnit;
|
|
}
|
|
counter[atomicUnit] = (counter[atomicUnit] || 0) + 1;
|
|
}
|
|
|
|
for (i = 0; i < this.denominator.length; i++) {
|
|
atomicUnit = this.denominator[i];
|
|
if (!backup) {
|
|
backup = atomicUnit;
|
|
}
|
|
counter[atomicUnit] = (counter[atomicUnit] || 0) - 1;
|
|
}
|
|
|
|
this.numerator = [];
|
|
this.denominator = [];
|
|
|
|
for (atomicUnit in counter) {
|
|
if (counter.hasOwnProperty(atomicUnit)) {
|
|
var count = counter[atomicUnit];
|
|
|
|
if (count > 0) {
|
|
for (i = 0; i < count; i++) {
|
|
this.numerator.push(atomicUnit);
|
|
}
|
|
} else if (count < 0) {
|
|
for (i = 0; i < -count; i++) {
|
|
this.denominator.push(atomicUnit);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (this.numerator.length === 0 && this.denominator.length === 0 && backup) {
|
|
this.backupUnit = backup;
|
|
}
|
|
|
|
this.numerator.sort();
|
|
this.denominator.sort();
|
|
}
|
|
};
|
|
return Unit;
|
|
}; |