Skip to content

Commit 27d251e

Browse files
committed
Update jsdom to 7.x and add patches for several jsdom or cssstyle library problems. This provides a nice means of updating mathjax-node without needing to wait for jsdom or cssstyle to merge pull requests.
1 parent c41a269 commit 27d251e

File tree

4 files changed

+151
-2
lines changed

4 files changed

+151
-2
lines changed

lib/mj-page.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ var fs = require('fs');
3030
var path = require('path');
3131
var url = require('url');
3232
var fmt = require('util').format;
33-
var jsdom = require("jsdom").jsdom;
33+
var jsdom = require('jsdom').jsdom;
3434
var execFile = require('child_process').execFile;
3535
var speech = require('speech-rule-engine');
3636
var os = require('os');
3737

38+
require('./patch/jsdom.js').patch(jsdom); // Fix some bugs in jsdom
39+
3840
var displayMessages = false; // don't log Message.Set() calls
3941
var displayErrors = true; // show error messages on the console
4042
var undefinedChar = false; // unknown characters are not saved in the error array

lib/mj-single.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ var execFile = require('child_process').execFile;
3636
var speech = require('speech-rule-engine');
3737
var os = require('os');
3838

39+
require('./patch/jsdom.js').patch(jsdom); // Fix some bugs in jsdom
40+
3941
var displayMessages = false; // don't log Message.Set() calls
4042
var displayErrors = true; // show error messages on the console
4143
var undefinedChar = false; // unknown characters are not saved in the error array

lib/patch/jsdom.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
//
2+
// Patch for CSSStyleDeclaration padding property so that it sets/clears
3+
// the Top, Right, Bottom, and Left properties (and also validates the
4+
// padding value)
5+
//
6+
var PADDING = (function () {
7+
var parsers = require('jsdom/node_modules/cssstyle/lib/parsers.js');
8+
var TYPES = parsers.TYPES;
9+
10+
var isValid = function (v) {
11+
var type = parsers.valueType(v);
12+
return type === TYPES.LENGTH || type === TYPES.PERCENT;
13+
};
14+
15+
var parser = function (v) {
16+
return parsers.parseMeasurement(v);
17+
}
18+
19+
var mySetter = parsers.implicitSetter('padding', '', isValid, parser);
20+
var myGlobal = parsers.implicitSetter('padding', '', function () {return true}, function (v) {return v});
21+
22+
return {
23+
set: function (v) {
24+
if (typeof v === "number") v = String(v);
25+
if (typeof v !== "string") return;
26+
var V = v.toLowerCase();
27+
switch (V) {
28+
case 'inherit':
29+
case 'initial':
30+
case 'unset':
31+
case '':
32+
myGlobal.call(this, V);
33+
break;
34+
35+
default:
36+
mySetter.call(this, v);
37+
break;
38+
}
39+
},
40+
get: function () {
41+
return this.getPropertyValue('padding');
42+
},
43+
enumerable: true,
44+
configurable: true
45+
};
46+
})();
47+
48+
//
49+
// Patch for CSSStyleDeclaration margin property so that it sets/clears
50+
// the Top, Right, Bottom, and Left properties (and also validates the
51+
// margin value)
52+
//
53+
var MARGIN = (function () {
54+
var parsers = require('jsdom/node_modules/cssstyle/lib/parsers.js');
55+
var TYPES = parsers.TYPES;
56+
57+
var isValid = function (v) {
58+
if (v.toLowerCase() === "auto") return true;
59+
var type = parsers.valueType(v);
60+
return type === TYPES.LENGTH || type === TYPES.PERCENT;
61+
};
62+
63+
var parser = function (v) {
64+
var V = v.toLowerCase();
65+
if (V === "auto") return V;
66+
return parsers.parseMeasurement(v);
67+
}
68+
69+
var mySetter = parsers.implicitSetter('margin', '', isValid, parser);
70+
var myGlobal = parsers.implicitSetter('margin', '', function () {return true}, function (v) {return v});
71+
72+
return {
73+
set: function (v) {
74+
if (typeof v === "number") v = String(v);
75+
if (typeof v !== "string") return;
76+
var V = v.toLowerCase();
77+
switch (V) {
78+
case 'inherit':
79+
case 'initial':
80+
case 'unset':
81+
case '':
82+
myGlobal.call(this, V);
83+
break;
84+
85+
default:
86+
mySetter.call(this, v);
87+
break;
88+
}
89+
},
90+
get: function () {
91+
return this.getPropertyValue('margin');
92+
},
93+
enumerable: true,
94+
configurable: true
95+
};
96+
})();
97+
98+
99+
//
100+
// Patch jsdom functions
101+
//
102+
exports.patch = function (jsdom) {
103+
var document = jsdom('');
104+
var window = document.defaultView;
105+
//
106+
// Fix setting of style attributes so shorthands work properly.
107+
//
108+
var div = document.createElement("div");
109+
div.style.border = "1px solid black";
110+
if (div.style.border !== "1px solid black") {
111+
var INIT = window.HTMLElement._init;
112+
window.HTMLElement._init = function () {
113+
INIT.apply(this,arguments);
114+
var that = this;
115+
this.style._onChange = function (csstext) {
116+
if (!that._settingCssText) {
117+
that._settingCssText = true;
118+
that.setAttribute('style', csstext);
119+
that._settingCssText = false;
120+
}
121+
};
122+
}
123+
}
124+
//
125+
// Add missing nodeName to Attr (after jsdom 7.1.0, it is no longer defined)
126+
// since this is used in mml2jax.
127+
//
128+
if (!("nodeName" in window.Attr.prototype)) {
129+
Object.defineProperties(window.Attr.prototype,{
130+
nodeName: {get: function() {return this.name}}
131+
});
132+
}
133+
//
134+
// Fix CSSStyleDeclaration properties that are broken (padding, margin)
135+
//
136+
div.style.paddingTop = "10px";
137+
div.style.padding = "1px";
138+
if (div.style.paddingTop !== "1px") {
139+
var core = require("jsdom/lib/jsdom/level1/core");
140+
Object.defineProperties(core.CSSStyleDeclaration.prototype,{
141+
padding: PADDING,
142+
margin: MARGIN
143+
});
144+
}
145+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"url": "git://github.com/mathjax/MathJax-node.git"
2323
},
2424
"dependencies": {
25-
"jsdom": "^6.0.0",
25+
"jsdom": "^7.1.0",
2626
"speech-rule-engine": "*",
2727
"yargs": "^3.0.0",
2828
"MathJax": "https://github.com/mathjax/MathJax/tarball/mathjax-node-2.5.1"

0 commit comments

Comments
 (0)