|
| 1 | +"""Test display isolation. |
| 2 | +
|
| 3 | +An object whose metadata contains an "isolated" tag must be isolated |
| 4 | +from the rest of the document. |
| 5 | +""" |
| 6 | + |
| 7 | + |
| 8 | +def test_display_isolation(notebook): |
| 9 | + import_ln = "from IPython.core.display import HTML, SVG, display, display_svg" |
| 10 | + notebook.edit_cell(index=0, content=import_ln) |
| 11 | + notebook.execute_cell(notebook.current_cell) |
| 12 | + try: |
| 13 | + isolated_html(notebook) |
| 14 | + isolated_svg(notebook) |
| 15 | + finally: |
| 16 | + # Ensure we switch from iframe back to default content even if test fails |
| 17 | + notebook.browser.switch_to.default_content() |
| 18 | + |
| 19 | + |
| 20 | +def isolated_html(notebook): |
| 21 | + """Test HTML display isolation. |
| 22 | +
|
| 23 | + HTML styling rendered without isolation will affect the whole |
| 24 | + document, whereas styling applied with isolation will affect only |
| 25 | + the local display object. |
| 26 | + """ |
| 27 | + red = 'rgb(255, 0, 0)' |
| 28 | + blue = 'rgb(0, 0, 255)' |
| 29 | + test_str = "<div id='test'>Should be red from non-isolation</div>" |
| 30 | + notebook.add_and_execute_cell(content="display(HTML(%r))" % test_str) |
| 31 | + non_isolated = ( |
| 32 | + "<style>div{color:%s;}</style>" % red + |
| 33 | + "<div id='non-isolated'>Should be red</div>") |
| 34 | + display_ni = "display(HTML(%r), metadata={'isolated':False})" % ( |
| 35 | + non_isolated) |
| 36 | + notebook.add_and_execute_cell(content=display_ni) |
| 37 | + isolated = ( |
| 38 | + "<style>div{color:%s;}</style>" % blue + |
| 39 | + "<div id='isolated'>Should be blue</div>") |
| 40 | + display_i = "display(HTML(%r), metadata={'isolated':True})" % ( |
| 41 | + isolated) |
| 42 | + notebook.add_and_execute_cell(content=display_i) |
| 43 | + |
| 44 | + # The non-isolated div will be in the body |
| 45 | + non_isolated_div = notebook.body.find_element_by_id("non-isolated") |
| 46 | + assert non_isolated_div.value_of_css_property("color") == red |
| 47 | + |
| 48 | + # The non-isolated styling will have affected the output of other cells |
| 49 | + test_div = notebook.body.find_element_by_id("test") |
| 50 | + assert test_div.value_of_css_property("color") == red |
| 51 | + |
| 52 | + # The isolated div will be in an iframe, only that element will be blue |
| 53 | + iframe = notebook.body.find_element_by_tag_name("iframe") |
| 54 | + notebook.browser.switch_to.frame(iframe) |
| 55 | + isolated_div = notebook.browser.find_element_by_id("isolated") |
| 56 | + assert isolated_div.value_of_css_property("color") == blue |
| 57 | + notebook.browser.switch_to.default_content() |
| 58 | + |
| 59 | + |
| 60 | +def isolated_svg(notebook): |
| 61 | + """Test that multiple isolated SVGs have different scopes. |
| 62 | +
|
| 63 | + Asserts that there no CSS leaks between two isolated SVGs. |
| 64 | + """ |
| 65 | + yellow = "rgb(255, 255, 0)" |
| 66 | + black = "rgb(0, 0, 0)" |
| 67 | + svg_1_str = """s1 = '''<svg width="1cm" height="1cm" viewBox="0 0 1000 500"><defs><style>rect {fill:%s;}; </style></defs><rect id="r1" x="200" y="100" width="600" height="300" /></svg>'''""" % yellow |
| 68 | + svg_2_str = """s2 = '''<svg width="1cm" height="1cm" viewBox="0 0 1000 500"><rect id="r2" x="200" y="100" width="600" height="300" /></svg>'''""" |
| 69 | + |
| 70 | + notebook.add_and_execute_cell(content=svg_1_str) |
| 71 | + notebook.add_and_execute_cell(content=svg_2_str) |
| 72 | + notebook.add_and_execute_cell( |
| 73 | + content="display_svg(SVG(s1), metadata=dict(isolated=True))") |
| 74 | + notebook.add_and_execute_cell( |
| 75 | + content="display_svg(SVG(s2), metadata=dict(isolated=True))") |
| 76 | + iframes = notebook.body.find_elements_by_tag_name("iframe") |
| 77 | + |
| 78 | + # The first rectangle will be red |
| 79 | + notebook.browser.switch_to.frame(iframes[1]) |
| 80 | + isolated_svg_1 = notebook.browser.find_element_by_id('r1') |
| 81 | + assert isolated_svg_1.value_of_css_property("fill") == yellow |
| 82 | + notebook.browser.switch_to.default_content() |
| 83 | + |
| 84 | + # The second rectangle will be black |
| 85 | + notebook.browser.switch_to.frame(iframes[2]) |
| 86 | + isolated_svg_2 = notebook.browser.find_element_by_id('r2') |
| 87 | + assert isolated_svg_2.value_of_css_property("fill") == black |
| 88 | + |
0 commit comments