-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjsxgraph.js
More file actions
280 lines (246 loc) · 8.46 KB
/
jsxgraph.js
File metadata and controls
280 lines (246 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
var H5P = H5P || {};
H5P.JSXGraph = (function ($) {
// var $ = H5P.jQuery;
// this.$ = $(this);
var useMathJax = true;
var depurify = function (str) {
return str.replace(/'/g, "'").
replace(/"/g, '"').
replace(/</g, '<').
replace(/>/g, '>').
replace(/&/g, '&');
};
/**
* Remove unwanted code
*
* @param {String} str
* @returns {String} purified string
*/
var sanitizeJS = function(str) {
return str.
replace(/<script.*<\/script>/ig, '').
replace(/<embed.*>/ig, '').
replace(/<iframe.*>/ig, '').
replace(/<link.*>/ig, '').
replace(/<object.*>/ig, '').
replace(/onclick/ig, '').
replace(/javascript:/ig, '').
replace(/document\.cookie/g, '').
replace(/document\.write/g, '').
replace(/charAt\(/g, '').
replace(/fromCharCode\(/g, '').
replace(/new Function\(/g, '').
replace(/\s+eval\(/g, '');
};
// var jsEscape = function (str) {
// return String(str).replace(/[^\w. ]/gi, function(c){
// return '\\u'+('0000'+c.charCodeAt(0).toString(16)).slice(-4);
// });
// };
/**
* CSS properties for the style attribute of the JSXGraph div.
* @param {Object} options
* @returns {String} String of CSS properties.
*/
var getCSSForJXG = function (options) {
var txt = '',
res, w, h;
if (false /*options.advanced.showFixedSize*/) {
txt += 'width:' + options.advanced.width + '; ';
txt += 'height:' + options.advanced.height + '; ';
}
else {
// Use the new CSS property aspect-ratio.
//txt += 'max-width:' + options.advanced.maxWidth + '; ';
// txt += 'aspect-ratio:' + options.advanced.aspectRatio + '; ';
// txt += 'margin: 0 auto;'
// Use the old aspect-ratio hack.
res = options.advanced.aspectRatio.match(/\w*(\d+)\w*\/\w*(\d+)\w*/);
w = parseFloat(res[1]);
h = parseFloat(res[2]);
txt += 'height:0; padding-bottom:' + (100 * h / w) + '%;'
}
return txt;
};
/**
* Compose the HTML head of the iframe
* @param {String} libPath Path for JSXGraph js and CSS files
* @param {String} csp String containing the content security policy
* @param {String} secureJS Additional security
* @returns {String} HTML text of the head part of the iframe
*/
var getHead = function(libPath, csp, secureJS) {
var headTxt = '<head><meta http-equiv="Content-Security-Policy" content="' + csp + '">';
/**
* Add JavaScript libraries and CSS files to the iFrame
*/
if (useMathJax) {
headTxt +=
'<script src="' + libPath + '3rdparty/mathjax/es5/tex-chtml.js" type="text/javascript"></script>';
}
headTxt +=
'<script src="' + libPath + 'jsxgraphcore.js" type="text/javascript"></script>' +
'<link rel="stylesheet" href="' + libPath + 'h5p-jsxgraph.css" type="text/css">' +
'<link rel="stylesheet" href="' + libPath + 'jsxgraph.css" type="text/css">';
headTxt += secureJS;
headTxt += '</head>';
return headTxt;
};
/**
* Build string consisting of the body of the iframe.
* @param {String} divId
* @param {Object} options
* @returns
*/
var getBody = function(divId, options) {
var bodyTxt, outerPre, outerPost;
// Set class on container to identify it as a JSXGraph construction.
if (true /*!options.advanced.showFixedSize*/) {
// Use the old aspect-ratio hack
outerPre = '<div style="max-width:' + options.advanced.maxWidth + ';';
outerPre += 'margin: 0 auto;';
outerPre += '">'
outerPost = '</div>';
}
else {
outerPre = '';
outerPost = '';
}
bodyTxt = '<body>' +
// First text field
'<div id="pre" class="h5p-jsxgraph-comment-post">' +
options.commentPre +
'</div>' +
// JSXGraph div
outerPre +
'<div id="' + divId + '" class="jxgbox" ' +
'style="' + getCSSForJXG(options) + '"' +
'></div>' +
outerPost +
'' +
// JavaScript code
'<script type="text/javascript">' +
sanitizeJS(depurify(options.code).replace(/BOARDID/g, "'" + divId + "'")) +
'</script>' +
// Second text field
'<div id="post" class="h5p-jsxgraph-comment-post">'
+ options.commentPost +
'</div>';
bodyTxt += '</body>';
return bodyTxt;
};
/**
* Constructor function.
*/
function C(options, id) {
// Extend defaults with provided options
this.options = $.extend(true, {}, {
resizeSupported: true, // Unused?
commentPre: 'BEFORE',
commentPost: 'AFTER',
advanced: {
divId: '',
// showFixedSize: true,
width: '500px',
height: '900px',
maxWidth: '100%',
aspectRatio: '1/1'
},
code: ''
}, options);
// Keep provided id.
this.id = id;
};
/**
* Attach function called by H5P framework to insert H5P content into
* page
*
* @param {jQuery} $container
*/
C.prototype.attach = function ($container) {
var divId, csp, libPath, secureJS,
doc, sandbox,
options = this.options;
/**
* divId is the Id of the JSXGraph div
*/
if (options.divId !== undefined && options.divId !== '') {
// Take the supplied Id for the JSXGraph div.
divId = options.divId;
}
else {
// Choose random Id for the JSXGraph div.
divId = 'jxg' + Math.random();
}
// libPath = "/sites/default/files/h5p/development/H5P.JSXGraph/";
libPath = H5P.getLibraryPath('H5P.JSXGraph');
// libPath = libPath.replace('libraries', 'development');
libPath += '/';
/**
* Security: Add Content-Security-Policy to the head of the iframe
*
* Iframe can only connect to the hosting server.
*
*/
csp = 'navigate-to \'none\'; ' +
'connect-src \'none\'; ' +
'prefetch-src \'none\'; ' +
'worker-src \'none\'; ' +
'font-src \'self\'; ' +
'img-src \'self\'; ' +
'default-src \'self\'; ' +
'style-src \'unsafe-inline\' \'self\' ;' +
'object-src \'none\'; ' +
'form-action \'none\'; ' +
'frame-src \'self\'; ' +
'script-src \'unsafe-inline\' \'self\';';
/**
* Additional security measure:
* Block XHR and Websockets by disabling the objects
* This should be redundant because of Content-Security-Policy.
*/
secureJS = '<script type="text/javascript">' +
'XMLHttpRequest = {};' +
'WebSocket = {};' +
'document.write = function() {};' + // It seems better to disable it instead of removing it from the code as above
'</script>';
/**
* Attribute sandbox for the iframe.
* See https://cloud.google.com/blog/products/data-analytics/iframe-sandbox-tutorial,
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe
*
* Allowed are:
* allow-same-origin Allows the iframe content to be treated as being from the same origin
* allow-scripts Allows to run scripts
* Blocked are:
* allow-forms: Allows form submission
* allow-modals: Allows to open modal windows
* allow-orientation-lock: Allows to lock the screen orientation
* allow-pointer-lock: Allows to use the Pointer Lock API
* allow-popups: Allows popups
* allow-popups-to-escape-sandbox: Allows popups to open new windows without inheriting the sandboxing
* allow-presentation: Allows to start a presentation session
* allow-top-navigation: Allows the iframe content to navigate its top-level browsing context
* allow-top-navigation-by-user-: Allows the iframeactivation content to navigate its top-level browsing context, but only if initiated by user
*/
sandbox = ' sandbox="allow-same-origin allow-scripts" ';
if (false) {
// Use H5P iframe
$container.append(getHead(libPath, csp, secureJS) + getBody(divId, options));
}
else {
// Create iframe from scratch
$iframe = $('<iframe src="about:blank" scrolling="auto" frameborder="0"' + sandbox + 'class="h5p-iframe-content h5p-iframe-wrapper" />');
$container.html('');
$container.append($iframe);
doc = $iframe.contents()[0];
doc.open('text/html', 'replace');
doc.writeln(getHead(libPath, csp, secureJS));
doc.writeln(getBody(divId, options));
doc.close();
$iframe.css({ width: '100%', height: this.options.advanced.height});
}
$container.addClass("h5p-jsxgraph");
};
return C;
})(H5P.jQuery);