Skip to content

Commit

Permalink
Merge pull request #154 from mathjax/css-patch
Browse files Browse the repository at this point in the history
Add more patches for padding and margin
  • Loading branch information
dpvc committed Feb 5, 2016
2 parents a62eab9 + ff06060 commit c2afb90
Showing 1 changed file with 141 additions and 46 deletions.
187 changes: 141 additions & 46 deletions lib/patch/jsdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,38 @@ try {fs.accessSync('node_modules/'+PARSERS, fs.F_OK)} catch (e) {
PARSERS = 'cssstyle/lib/parsers.js'; // node 5 heirarchy
}

//
// Companion to implicitSetter, but for the individual parts.
// This sets the individual value, and checks to see if all four
// sub-parts are set. If so, it sets the shorthand version and removes
// the individual parts from the cssText.
//
var subImplicitSetter = function (prefix, part, isValid, parser) {
var property = prefix + '-' + part;
var subparts = [prefix+"-top", prefix+"-right", prefix+"-bottom", prefix+"-left"];

return function (v) {
if (typeof v === 'number') v = v.toString();
if (typeof v !== 'string') return undefined;
if (!isValid(v)) return undefined;
v = parser(v);
this._setProperty(property,v);
var parts = [];
for (var i = 0; i < 4; i++) {
if (this._values[subparts[i]] == null || this._values[subparts[i]] === '') break;
parts.push(this._values[subparts[i]]);
}
if (parts.length === 4) {
for (i = 0; i < 4; i++) {
this.removeProperty(subparts[i]);
this._values[subparts[i]] = parts[i];
}
this._setProperty(prefix,parts.join(" "));
}
return v;
};
};

//
// Patch for CSSStyleDeclaration padding property so that it sets/clears
// the Top, Right, Bottom, and Left properties (and also validates the
Expand All @@ -26,34 +58,38 @@ var PADDING = (function () {

var parser = function (v) {
return parsers.parseMeasurement(v);
}
};

var mySetter = parsers.implicitSetter('padding', '', isValid, parser);
var myGlobal = parsers.implicitSetter('padding', '', function () {return true}, function (v) {return v});

return {
set: function (v) {
if (typeof v === "number") v = String(v);
if (typeof v !== "string") return;
var V = v.toLowerCase();
switch (V) {
case 'inherit':
case 'initial':
case 'unset':
case '':
myGlobal.call(this, V);
break;
definition: {
set: function (v) {
if (typeof v === "number") v = String(v);
if (typeof v !== "string") return;
var V = v.toLowerCase();
switch (V) {
case 'inherit':
case 'initial':
case 'unset':
case '':
myGlobal.call(this, V);
break;

default:
mySetter.call(this, v);
break;
}
},
get: function () {
return this.getPropertyValue('padding');
default:
mySetter.call(this, v);
break;
}
},
get: function () {
return this.getPropertyValue('padding');
},
enumerable: true,
configurable: true
},
enumerable: true,
configurable: true
isValid: isValid,
parser: parser
};
})();

Expand All @@ -76,34 +112,38 @@ var MARGIN = (function () {
var V = v.toLowerCase();
if (V === "auto") return V;
return parsers.parseMeasurement(v);
}
};

var mySetter = parsers.implicitSetter('margin', '', isValid, parser);
var myGlobal = parsers.implicitSetter('margin', '', function () {return true}, function (v) {return v});

return {
set: function (v) {
if (typeof v === "number") v = String(v);
if (typeof v !== "string") return;
var V = v.toLowerCase();
switch (V) {
case 'inherit':
case 'initial':
case 'unset':
case '':
myGlobal.call(this, V);
break;

default:
mySetter.call(this, v);
break;
}
},
get: function () {
return this.getPropertyValue('margin');
definition: {
set: function (v) {
if (typeof v === "number") v = String(v);
if (typeof v !== "string") return;
var V = v.toLowerCase();
switch (V) {
case 'inherit':
case 'initial':
case 'unset':
case '':
myGlobal.call(this, V);
break;

default:
mySetter.call(this, v);
break;
}
},
get: function () {
return this.getPropertyValue('margin');
},
enumerable: true,
configurable: true
},
enumerable: true,
configurable: true
isValid: isValid,
parser: parser
};
})();

Expand Down Expand Up @@ -150,8 +190,63 @@ exports.patch = function (jsdom) {
if (div.style.paddingTop !== "1px") {
var core = require("jsdom/lib/jsdom/level1/core");
Object.defineProperties(core.CSSStyleDeclaration.prototype,{
padding: PADDING,
margin: MARGIN
padding: PADDING.definition,
margin: MARGIN.definition
});
}
}
div.style.padding = "1px 2px 3px 4px";
div.style.paddingTop = "10px";
if (div.style.padding !== "10px 2px 3px 4px") {
var core = require("jsdom/lib/jsdom/level1/core");
Object.defineProperties(core.CSSStyleDeclaration.prototype,{
marginTop: {
set: subImplicitSetter('margin', 'top', MARGIN.isValid, MARGIN.parser),
get: function () {
return this.getPropertyValue('margin-top');
}
},
marginRight: {
set: subImplicitSetter('margin', 'right', MARGIN.isValid, MARGIN.parser),
get: function () {
return this.getPropertyValue('margin-right');
}
},
marginBottom: {
set: subImplicitSetter('margin', 'bottom', MARGIN.isValid, MARGIN.parser),
get: function () {
return this.getPropertyValue('margin-bottom');
}
},
marginLeft: {
set: subImplicitSetter('margin', 'left', MARGIN.isValid, MARGIN.parser),
get: function () {
return this.getPropertyValue('margin-left');
}
},
paddingTop: {
set: subImplicitSetter('padding', 'top', PADDING.isValid, PADDING.parser),
get: function () {
return this.getPropertyValue('padding-top');
}
},
paddingRight: {
set: subImplicitSetter('padding', 'right', PADDING.isValid, PADDING.parser),
get: function () {
return this.getPropertyValue('padding-right');
}
},
paddingBottom: {
set: subImplicitSetter('padding', 'bottom', PADDING.isValid, PADDING.parser),
get: function () {
return this.getPropertyValue('padding-bottom');
}
},
paddingLeft: {
set: subImplicitSetter('padding', 'left', PADDING.isValid, PADDING.parser),
get: function () {
return this.getPropertyValue('padding-left');
}
}
});
}
}

0 comments on commit c2afb90

Please sign in to comment.