Skip to content

Commit 34f3cc8

Browse files
committed
Merge branch 'patch-jsdom' into develop
2 parents b354f9c + 5dc941c commit 34f3cc8

File tree

7 files changed

+160
-35
lines changed

7 files changed

+160
-35
lines changed

bin/page2mml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ function getXMLNS(document) {
116116
var html = document.head.parentNode;
117117
for (var i = 0, m = html.attributes.length; i < m; i++) {
118118
var attr = html.attributes[i];
119-
if (attr.nodeName.substr(0,6) === "xmlns:" &&
120-
attr.nodeValue === "http://www.w3.org/1998/Math/MathML")
121-
{return attr.nodeName.substr(6)}
119+
if (attr.name.substr(0,6) === "xmlns:" &&
120+
attr.value === "http://www.w3.org/1998/Math/MathML")
121+
{return attr.name.substr(6)}
122122
}
123123
return "mml";
124124
}

bin/page2png

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ function getXMLNS(document) {
142142
var html = document.head.parentNode;
143143
for (var i = 0, m = html.attributes.length; i < m; i++) {
144144
var attr = html.attributes[i];
145-
if (attr.nodeName.substr(0,6) === "xmlns:" &&
146-
attr.nodeValue === "http://www.w3.org/1998/Math/MathML")
147-
{return attr.nodeName.substr(6)}
145+
if (attr.name.substr(0,6) === "xmlns:" &&
146+
attr.value === "http://www.w3.org/1998/Math/MathML")
147+
{return attr.name.substr(6)}
148148
}
149149
return "mml";
150150
}

bin/page2svg

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ function getXMLNS(document) {
150150
var html = document.head.parentNode;
151151
for (var i = 0, m = html.attributes.length; i < m; i++) {
152152
var attr = html.attributes[i];
153-
if (attr.nodeName.substr(0,6) === "xmlns:" &&
154-
attr.nodeValue === "http://www.w3.org/1998/Math/MathML")
155-
{return attr.nodeName.substr(6)}
153+
if (attr.name.substr(0,6) === "xmlns:" &&
154+
attr.value === "http://www.w3.org/1998/Math/MathML")
155+
{return attr.name.substr(6)}
156156
}
157157
return "mml";
158158
}

lib/mj-page.js

Lines changed: 3 additions & 25 deletions
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
@@ -442,22 +444,6 @@ function AddError(prefix,message,nopush) {
442444
}
443445

444446

445-
/********************************************************************/
446-
447-
//
448-
// A bug in jsdom prevents explicit width and height from being
449-
// kept in the style information, so work around that.
450-
// WARNING: this depends on the internals of jsdom, and may need
451-
// to be adjusted in the future.
452-
//
453-
function SetWH(node,w,h) {
454-
if (!node.style.width) {
455-
var style = node.style;
456-
style[style._length] = "width"; style._length++; style._values.width = w;
457-
style[style._length] = "height"; style._length++; style._values.height = h;
458-
}
459-
}
460-
461447
/********************************************************************/
462448

463449
//
@@ -596,14 +582,6 @@ function AdjustSVG() {
596582
nodes[i].removeAttribute("href");
597583
}
598584
//
599-
// Fix a problem with jsdom not setting width and height CSS.
600-
//
601-
nodes = document.querySelectorAll(".MathJax_SVG > span > span");
602-
for (i = nodes.length-1; i >= 0; i--) {
603-
var child = nodes[i].firstChild;
604-
SetWH(nodes[i],child.getAttribute("width"),child.getAttribute("height"));
605-
}
606-
//
607585
// Add speech text, if needed
608586
//
609587
if (data.speakText) {callback = GetSpeech()}

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)