@@ -282,26 +282,32 @@ def test_sticky_basic(styler, index, columns, index_name):
282282 if columns :
283283 styler .set_sticky (axis = 1 )
284284
285- res = styler .set_uuid ("" ).to_html ()
286-
287- css_for_index = (
288- "tr th:first-child {\n position: sticky;\n background-color: white;\n "
289- "left: 0px;\n z-index: 1;\n }"
285+ left_css = (
286+ "#T_ {0} {{\n position: sticky;\n background-color: white;\n "
287+ " left: 0px;\n z-index: {1};\n }}"
290288 )
291- assert (css_for_index in res ) is index
292-
293- css_for_cols_1 = (
294- "thead tr:first-child {\n position: sticky;\n background-color: white;\n "
295- "top: 0px;\n z-index: 2;\n "
289+ top_css = (
290+ "#T_ {0} {{\n position: sticky;\n background-color: white;\n "
291+ " top: {1}px;\n z-index: {2};\n {3}}}"
296292 )
297- css_for_cols_1 += " height: 25px;\n }" if index_name else "}"
298- assert (css_for_cols_1 in res ) is columns
299293
300- css_for_cols_2 = (
301- "thead tr:nth-child(2) {\n position: sticky;\n background-color: white;\n "
302- "top: 25px;\n z-index: 2;\n height: 25px;\n }"
294+ res = styler .set_uuid ("" ).to_html ()
295+
296+ # test index stickys over thead and tbody
297+ assert (left_css .format ("thead tr th:nth-child(1)" , "3 !important" ) in res ) is index
298+ assert (left_css .format ("tbody tr th:nth-child(1)" , "1" ) in res ) is index
299+
300+ # test column stickys including if name row
301+ assert (
302+ top_css .format ("thead tr:nth-child(1) th" , "0" , "2" , " height: 25px;\n " ) in res
303+ ) is (columns and index_name )
304+ assert (
305+ top_css .format ("thead tr:nth-child(2) th" , "25" , "2" , " height: 25px;\n " )
306+ in res
307+ ) is (columns and index_name )
308+ assert (top_css .format ("thead tr:nth-child(1) th" , "0" , "2" , "" ) in res ) is (
309+ columns and not index_name
303310 )
304- assert (css_for_cols_2 in res ) is (index_name and columns )
305311
306312
307313@pytest .mark .parametrize ("index" , [False , True ])
@@ -312,73 +318,30 @@ def test_sticky_mi(styler_mi, index, columns):
312318 if columns :
313319 styler_mi .set_sticky (axis = 1 )
314320
315- res = styler_mi .set_uuid ("" ).to_html ()
316- assert (
317- (
318- dedent (
319- """\
320- #T_ tbody th.level0 {
321- position: sticky;
322- left: 0px;
323- min-width: 75px;
324- max-width: 75px;
325- background-color: white;
326- }
327- """
328- )
329- in res
330- )
331- is index
321+ left_css = (
322+ "#T_ {0} {{\n position: sticky;\n background-color: white;\n "
323+ " left: {1}px;\n min-width: 75px;\n max-width: 75px;\n z-index: {2};\n }}"
332324 )
333- assert (
334- (
335- dedent (
336- """\
337- #T_ tbody th.level1 {
338- position: sticky;
339- left: 75px;
340- min-width: 75px;
341- max-width: 75px;
342- background-color: white;
343- }
344- """
345- )
346- in res
347- )
348- is index
325+ top_css = (
326+ "#T_ {0} {{\n position: sticky;\n background-color: white;\n "
327+ " top: {1}px;\n height: 25px;\n z-index: {2};\n }}"
349328 )
329+
330+ res = styler_mi .set_uuid ("" ).to_html ()
331+
332+ # test the index stickys for thead and tbody over both levels
350333 assert (
351- (
352- dedent (
353- """\
354- #T_ thead th.level0 {
355- position: sticky;
356- top: 0px;
357- height: 25px;
358- background-color: white;
359- }
360- """
361- )
362- in res
363- )
364- is columns
365- )
334+ left_css .format ("thead tr th:nth-child(1)" , "0" , "3 !important" ) in res
335+ ) is index
336+ assert (left_css .format ("tbody tr th.level0" , "0" , "1" ) in res ) is index
366337 assert (
367- (
368- dedent (
369- """\
370- #T_ thead th.level1 {
371- position: sticky;
372- top: 25px;
373- height: 25px;
374- background-color: white;
375- }
376- """
377- )
378- in res
379- )
380- is columns
381- )
338+ left_css .format ("thead tr th:nth-child(2)" , "75" , "3 !important" ) in res
339+ ) is index
340+ assert (left_css .format ("tbody tr th.level1" , "75" , "1" ) in res ) is index
341+
342+ # test the column stickys for each level row
343+ assert (top_css .format ("thead tr:nth-child(1) th" , "0" , "2" ) in res ) is columns
344+ assert (top_css .format ("thead tr:nth-child(2) th" , "25" , "2" ) in res ) is columns
382345
383346
384347@pytest .mark .parametrize ("index" , [False , True ])
@@ -389,43 +352,29 @@ def test_sticky_levels(styler_mi, index, columns):
389352 if columns :
390353 styler_mi .set_sticky (axis = 1 , levels = [1 ])
391354
392- res = styler_mi .set_uuid ("" ).to_html ()
393- assert "#T_ tbody th.level0 {" not in res
394- assert "#T_ thead th.level0 {" not in res
395- assert (
396- (
397- dedent (
398- """\
399- #T_ tbody th.level1 {
400- position: sticky;
401- left: 0px;
402- min-width: 75px;
403- max-width: 75px;
404- background-color: white;
405- }
406- """
407- )
408- in res
409- )
410- is index
355+ left_css = (
356+ "#T_ {0} {{\n position: sticky;\n background-color: white;\n "
357+ " left: {1}px;\n min-width: 75px;\n max-width: 75px;\n z-index: {2};\n }}"
411358 )
412- assert (
413- (
414- dedent (
415- """\
416- #T_ thead th.level1 {
417- position: sticky;
418- top: 0px;
419- height: 25px;
420- background-color: white;
421- }
422- """
423- )
424- in res
425- )
426- is columns
359+ top_css = (
360+ "#T_ {0} {{\n position: sticky;\n background-color: white;\n "
361+ " top: {1}px;\n height: 25px;\n z-index: {2};\n }}"
427362 )
428363
364+ res = styler_mi .set_uuid ("" ).to_html ()
365+
366+ # test no sticking of level0
367+ assert "#T_ thead tr th:nth-child(1)" not in res
368+ assert "#T_ tbody tr th.level0" not in res
369+ assert "#T_ thead tr:nth-child(1) th" not in res
370+
371+ # test sticking level1
372+ assert (
373+ left_css .format ("thead tr th:nth-child(2)" , "0" , "3 !important" ) in res
374+ ) is index
375+ assert (left_css .format ("tbody tr th.level1" , "0" , "1" ) in res ) is index
376+ assert (top_css .format ("thead tr:nth-child(2) th" , "0" , "2" ) in res ) is columns
377+
429378
430379def test_sticky_raises (styler ):
431380 with pytest .raises (ValueError , match = "`axis` must be" ):
0 commit comments