Skip to content

Commit c2afb90

Browse files
committed
Merge pull request mathjax#154 from mathjax/css-patch
Add more patches for padding and margin
2 parents a62eab9 + ff06060 commit c2afb90

File tree

1 file changed

+141
-46
lines changed

1 file changed

+141
-46
lines changed

lib/patch/jsdom.js

Lines changed: 141 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,38 @@ try {fs.accessSync('node_modules/'+PARSERS, fs.F_OK)} catch (e) {
1010
PARSERS = 'cssstyle/lib/parsers.js'; // node 5 heirarchy
1111
}
1212

13+
//
14+
// Companion to implicitSetter, but for the individual parts.
15+
// This sets the individual value, and checks to see if all four
16+
// sub-parts are set. If so, it sets the shorthand version and removes
17+
// the individual parts from the cssText.
18+
//
19+
var subImplicitSetter = function (prefix, part, isValid, parser) {
20+
var property = prefix + '-' + part;
21+
var subparts = [prefix+"-top", prefix+"-right", prefix+"-bottom", prefix+"-left"];
22+
23+
return function (v) {
24+
if (typeof v === 'number') v = v.toString();
25+
if (typeof v !== 'string') return undefined;
26+
if (!isValid(v)) return undefined;
27+
v = parser(v);
28+
this._setProperty(property,v);
29+
var parts = [];
30+
for (var i = 0; i < 4; i++) {
31+
if (this._values[subparts[i]] == null || this._values[subparts[i]] === '') break;
32+
parts.push(this._values[subparts[i]]);
33+
}
34+
if (parts.length === 4) {
35+
for (i = 0; i < 4; i++) {
36+
this.removeProperty(subparts[i]);
37+
this._values[subparts[i]] = parts[i];
38+
}
39+
this._setProperty(prefix,parts.join(" "));
40+
}
41+
return v;
42+
};
43+
};
44+
1345
//
1446
// Patch for CSSStyleDeclaration padding property so that it sets/clears
1547
// the Top, Right, Bottom, and Left properties (and also validates the
@@ -26,34 +58,38 @@ var PADDING = (function () {
2658

2759
var parser = function (v) {
2860
return parsers.parseMeasurement(v);
29-
}
61+
};
3062

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

3466
return {
35-
set: function (v) {
36-
if (typeof v === "number") v = String(v);
37-
if (typeof v !== "string") return;
38-
var V = v.toLowerCase();
39-
switch (V) {
40-
case 'inherit':
41-
case 'initial':
42-
case 'unset':
43-
case '':
44-
myGlobal.call(this, V);
45-
break;
67+
definition: {
68+
set: function (v) {
69+
if (typeof v === "number") v = String(v);
70+
if (typeof v !== "string") return;
71+
var V = v.toLowerCase();
72+
switch (V) {
73+
case 'inherit':
74+
case 'initial':
75+
case 'unset':
76+
case '':
77+
myGlobal.call(this, V);
78+
break;
4679

47-
default:
48-
mySetter.call(this, v);
49-
break;
50-
}
51-
},
52-
get: function () {
53-
return this.getPropertyValue('padding');
80+
default:
81+
mySetter.call(this, v);
82+
break;
83+
}
84+
},
85+
get: function () {
86+
return this.getPropertyValue('padding');
87+
},
88+
enumerable: true,
89+
configurable: true
5490
},
55-
enumerable: true,
56-
configurable: true
91+
isValid: isValid,
92+
parser: parser
5793
};
5894
})();
5995

@@ -76,34 +112,38 @@ var MARGIN = (function () {
76112
var V = v.toLowerCase();
77113
if (V === "auto") return V;
78114
return parsers.parseMeasurement(v);
79-
}
115+
};
80116

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

84120
return {
85-
set: function (v) {
86-
if (typeof v === "number") v = String(v);
87-
if (typeof v !== "string") return;
88-
var V = v.toLowerCase();
89-
switch (V) {
90-
case 'inherit':
91-
case 'initial':
92-
case 'unset':
93-
case '':
94-
myGlobal.call(this, V);
95-
break;
96-
97-
default:
98-
mySetter.call(this, v);
99-
break;
100-
}
101-
},
102-
get: function () {
103-
return this.getPropertyValue('margin');
121+
definition: {
122+
set: function (v) {
123+
if (typeof v === "number") v = String(v);
124+
if (typeof v !== "string") return;
125+
var V = v.toLowerCase();
126+
switch (V) {
127+
case 'inherit':
128+
case 'initial':
129+
case 'unset':
130+
case '':
131+
myGlobal.call(this, V);
132+
break;
133+
134+
default:
135+
mySetter.call(this, v);
136+
break;
137+
}
138+
},
139+
get: function () {
140+
return this.getPropertyValue('margin');
141+
},
142+
enumerable: true,
143+
configurable: true
104144
},
105-
enumerable: true,
106-
configurable: true
145+
isValid: isValid,
146+
parser: parser
107147
};
108148
})();
109149

@@ -150,8 +190,63 @@ exports.patch = function (jsdom) {
150190
if (div.style.paddingTop !== "1px") {
151191
var core = require("jsdom/lib/jsdom/level1/core");
152192
Object.defineProperties(core.CSSStyleDeclaration.prototype,{
153-
padding: PADDING,
154-
margin: MARGIN
193+
padding: PADDING.definition,
194+
margin: MARGIN.definition
155195
});
156196
}
157-
}
197+
div.style.padding = "1px 2px 3px 4px";
198+
div.style.paddingTop = "10px";
199+
if (div.style.padding !== "10px 2px 3px 4px") {
200+
var core = require("jsdom/lib/jsdom/level1/core");
201+
Object.defineProperties(core.CSSStyleDeclaration.prototype,{
202+
marginTop: {
203+
set: subImplicitSetter('margin', 'top', MARGIN.isValid, MARGIN.parser),
204+
get: function () {
205+
return this.getPropertyValue('margin-top');
206+
}
207+
},
208+
marginRight: {
209+
set: subImplicitSetter('margin', 'right', MARGIN.isValid, MARGIN.parser),
210+
get: function () {
211+
return this.getPropertyValue('margin-right');
212+
}
213+
},
214+
marginBottom: {
215+
set: subImplicitSetter('margin', 'bottom', MARGIN.isValid, MARGIN.parser),
216+
get: function () {
217+
return this.getPropertyValue('margin-bottom');
218+
}
219+
},
220+
marginLeft: {
221+
set: subImplicitSetter('margin', 'left', MARGIN.isValid, MARGIN.parser),
222+
get: function () {
223+
return this.getPropertyValue('margin-left');
224+
}
225+
},
226+
paddingTop: {
227+
set: subImplicitSetter('padding', 'top', PADDING.isValid, PADDING.parser),
228+
get: function () {
229+
return this.getPropertyValue('padding-top');
230+
}
231+
},
232+
paddingRight: {
233+
set: subImplicitSetter('padding', 'right', PADDING.isValid, PADDING.parser),
234+
get: function () {
235+
return this.getPropertyValue('padding-right');
236+
}
237+
},
238+
paddingBottom: {
239+
set: subImplicitSetter('padding', 'bottom', PADDING.isValid, PADDING.parser),
240+
get: function () {
241+
return this.getPropertyValue('padding-bottom');
242+
}
243+
},
244+
paddingLeft: {
245+
set: subImplicitSetter('padding', 'left', PADDING.isValid, PADDING.parser),
246+
get: function () {
247+
return this.getPropertyValue('padding-left');
248+
}
249+
}
250+
});
251+
}
252+
}

0 commit comments

Comments
 (0)