##// END OF EJS Templates
Merge pull request #4250 from pablooliveira/wrap-svg-in-iframes...
Paul Ivanov -
r13556:d0cdde9a merge
parent child Browse files
Show More
@@ -0,0 +1,42 b''
1 //
2 // Test svg isolation
3 // An object whose metadata contains an "isolated" tag must be isolated
4 // from the rest of the document. In the case of inline SVGs, this means
5 // that multiple SVGs have different scopes. This test checks that there
6 // are no CSS leaks between two isolated SVGs.
7 //
8
9 casper.notebook_test(function () {
10 this.evaluate(function () {
11 var cell = IPython.notebook.get_cell(0);
12 cell.set_text( "from IPython.core.display import SVG, display_svg\n"
13 + "s1 = '''<svg width='1cm' height='1cm' viewBox='0 0 1000 500'>"
14 + "<defs><style>rect {fill:red;}; </style></defs>"
15 + "<rect id='r1' x='200' y='100' width='600' height='300' /></svg>"
16 + "'''\n"
17 + "s2 = '''<svg width='1cm' height='1cm' viewBox='0 0 1000 500'>"
18 + "<rect id='r2' x='200' y='100' width='600' height='300' /></svg>"
19 + "'''\n"
20 + "display_svg(SVG(s1), metadata=dict(isolated=True))\n"
21 + "display_svg(SVG(s2), metadata=dict(isolated=True))\n"
22 );
23 cell.execute();
24 });
25
26 this.wait_for_output(0);
27
28 this.then(function () {
29 var colors = this.evaluate(function () {
30 var colors = [];
31 var ifr = __utils__.findAll("iframe");
32 var svg1 = ifr[0].contentWindow.document.getElementById('r1');
33 colors[0] = window.getComputedStyle(svg1)["fill"];
34 var svg2 = ifr[1].contentWindow.document.getElementById('r2');
35 colors[1] = window.getComputedStyle(svg2)["fill"];
36 return colors;
37 });
38
39 this.test.assertEquals(colors[0], '#ff0000', 'First svg should be red');
40 this.test.assertEquals(colors[1], '#000000', 'Second svg should be black');
41 });
42 });
@@ -326,7 +326,51 b' var IPython = (function (IPython) {'
326 326 }
327 327 return oa;
328 328 };
329
329
330
331 OutputArea.prototype.create_output_subarea = function(md, classes) {
332 var subarea = $('<div/>').addClass('output_subarea').addClass(classes);
333 if (md['isolated']) {
334 // Create an iframe to isolate the subarea from the rest of the
335 // document
336 var iframe = $('<iframe/>').addClass('box-flex1');
337 iframe.css({'height':1, 'width':'100%', 'display':'block'});
338 iframe.attr('frameborder', 0);
339 iframe.attr('scrolling', 'auto');
340
341 // Once the iframe is loaded, the subarea is dynamically inserted
342 iframe.on('load', function() {
343 // Workaround needed by Firefox, to properly render svg inside
344 // iframes, see http://stackoverflow.com/questions/10177190/
345 // svg-dynamically-added-to-iframe-does-not-render-correctly
346 this.contentDocument.open();
347
348 // Insert the subarea into the iframe
349 // We must directly write the html. When using Jquery's append
350 // method, javascript is evaluated in the parent document and
351 // not in the iframe document.
352 this.contentDocument.write(subarea.html());
353
354 this.contentDocument.close();
355
356 var body = this.contentDocument.body;
357 // Adjust the iframe height automatically
358 iframe.height(body.scrollHeight + 'px');
359 });
360
361 // Elements should be appended to the inner subarea and not to the
362 // iframe
363 iframe.append = function(that) {
364 subarea.append(that);
365 };
366
367 return iframe;
368 } else {
369 return subarea;
370 }
371 }
372
373
330 374 OutputArea.prototype._append_javascript_error = function (err, container) {
331 375 // display a message when a javascript error occurs in display output
332 376 var msg = "Javascript error adding output!"
@@ -460,7 +504,7 b' var IPython = (function (IPython) {'
460 504
461 505
462 506 OutputArea.prototype.append_html = function (html, md, element) {
463 var toinsert = $("<div/>").addClass("output_subarea output_html rendered_html");
507 var toinsert = this.create_output_subarea(md, "output_html rendered_html");
464 508 toinsert.append(html);
465 509 element.append(toinsert);
466 510 };
@@ -468,7 +512,7 b' var IPython = (function (IPython) {'
468 512
469 513 OutputArea.prototype.append_javascript = function (js, md, container) {
470 514 // We just eval the JS code, element appears in the local scope.
471 var element = $("<div/>").addClass("output_subarea");
515 var element = this.create_output_subarea(md, "");
472 516 container.append(element);
473 517 // Div for js shouldn't be drawn, as it will add empty height to the area.
474 518 container.hide();
@@ -483,7 +527,7 b' var IPython = (function (IPython) {'
483 527
484 528
485 529 OutputArea.prototype.append_text = function (data, md, element, extra_class) {
486 var toinsert = $("<div/>").addClass("output_subarea output_text");
530 var toinsert = this.create_output_subarea(md, "output_text");
487 531 // escape ANSI & HTML specials in plaintext:
488 532 data = utils.fixConsole(data);
489 533 data = utils.fixCarriageReturn(data);
@@ -497,7 +541,7 b' var IPython = (function (IPython) {'
497 541
498 542
499 543 OutputArea.prototype.append_svg = function (svg, md, element) {
500 var toinsert = $("<div/>").addClass("output_subarea output_svg");
544 var toinsert = this.create_output_subarea(md, "output_svg");
501 545 toinsert.append(svg);
502 546 element.append(toinsert);
503 547 };
@@ -531,7 +575,7 b' var IPython = (function (IPython) {'
531 575
532 576
533 577 OutputArea.prototype.append_png = function (png, md, element) {
534 var toinsert = $("<div/>").addClass("output_subarea output_png");
578 var toinsert = this.create_output_subarea(md, "output_png");
535 579 var img = $("<img/>").attr('src','data:image/png;base64,'+png);
536 580 if (md['height']) {
537 581 img.attr('height', md['height']);
@@ -546,7 +590,7 b' var IPython = (function (IPython) {'
546 590
547 591
548 592 OutputArea.prototype.append_jpeg = function (jpeg, md, element) {
549 var toinsert = $("<div/>").addClass("output_subarea output_jpeg");
593 var toinsert = this.create_output_subarea(md, "output_jpeg");
550 594 var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg);
551 595 if (md['height']) {
552 596 img.attr('height', md['height']);
@@ -563,7 +607,7 b' var IPython = (function (IPython) {'
563 607 OutputArea.prototype.append_latex = function (latex, md, element) {
564 608 // This method cannot do the typesetting because the latex first has to
565 609 // be on the page.
566 var toinsert = $("<div/>").addClass("output_subarea output_latex");
610 var toinsert = this.create_output_subarea(md, "output_latex");
567 611 toinsert.append(latex);
568 612 element.append(toinsert);
569 613 };
General Comments 0
You need to be logged in to leave comments. Login now