Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find suitable z-index for the adder #2585

Merged
merged 1 commit into from
Oct 12, 2020
Merged

Conversation

esanzgar
Copy link
Contributor

@esanzgar esanzgar commented Sep 29, 2020

This PR incorporates a solution to programatically find a z-index for the adder. Before this, the z-index of the adder was a hard-coded value, which didn't work for some websites (see for example #2009).

We evaluated the option of finding the greatest z-index in the page. However, this takes too long for heavy pages.

This solution in this PR is not bulletproof, but probably works for the most part of the websites. It uses the greatest z-index of five nearby elements: left-top, left-bottom, middle-center, right-top, right-bottom elements around the adder.

Resolves #2009

@esanzgar esanzgar marked this pull request as draft September 29, 2020 10:32
@robertknight robertknight self-assigned this Sep 29, 2020
@robertknight
Copy link
Member

robertknight commented Sep 29, 2020

I have yet to figure out exactly why using attachTo: document.body failed, but using the following pattern which is quite common in our tests avoids the issue:

diff --git a/src/annotator/test/adder-test.js b/src/annotator/test/adder-test.js
index 5ee39cc98..7f0d54cd5 100644
--- a/src/annotator/test/adder-test.js
+++ b/src/annotator/test/adder-test.js
@@ -32,6 +32,17 @@ function revertOffsetElement(el) {
 }
 
 describe('findHighestZindex', () => {
+  let container;
+
+  beforeEach(() => {
+    container = document.createElement('div');
+    document.body.appendChild(container);
+  });
+
+  afterEach(() => {
+    container.remove();
+  });
+
   it('returns zero z-index (default value) if no element is passed)', () => {
     assert.strictEqual(findHighestZindex(undefined), 0);
     assert.strictEqual(findHighestZindex(null), 0);
@@ -44,7 +55,7 @@ describe('findHighestZindex', () => {
         <div className="child1" style={{ zIndex: 5 }} />
         <div className="child2" style={{ zIndex: 15 }} />
       </div>,
-      { attachTo: document.body }
+      { attachTo: container }
     );
 
     const parent = wrapper.find('.parent').getDOMNode();

The reason for the above pattern of putting the cleanup in an afterEach is that it ensures it happens even if the test fails, reducing the probability that changes made to the environment by the failed test cause failures elsewhere.

@esanzgar esanzgar force-pushed the z-index-cleaner-fix-2009 branch 3 times, most recently from fc885b1 to 60793af Compare September 30, 2020 16:12
@esanzgar esanzgar marked this pull request as ready for review September 30, 2020 16:17
@esanzgar esanzgar changed the title Find suitable zIndex for the adder Find suitable z-index for the adder Oct 1, 2020
@esanzgar esanzgar force-pushed the z-index-cleaner-fix-2009 branch 3 times, most recently from 6ddbe36 to 3305dc5 Compare October 2, 2020 13:23
@codecov
Copy link

codecov bot commented Oct 2, 2020

Codecov Report

Merging #2585 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2585   +/-   ##
=======================================
  Coverage   97.28%   97.29%           
=======================================
  Files         197      197           
  Lines        7300     7311   +11     
  Branches     1615     1616    +1     
=======================================
+ Hits         7102     7113   +11     
  Misses        198      198           
Impacted Files Coverage Δ
src/annotator/adder.js 96.70% <100.00%> (+0.45%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2414a08...d5f7443. Read the comment docs.

Copy link
Member

@robertknight robertknight left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Eduardo,

I had another look over this. I suggested some minor changes but I also noted a scenario which was highlighted by the test page that you added that might require re-thinking the approach a little.

@esanzgar esanzgar force-pushed the z-index-cleaner-fix-2009 branch 5 times, most recently from da0d3c9 to c46b549 Compare October 6, 2020 10:12
Copy link
Contributor Author

@esanzgar esanzgar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a more robust and simple implementation.

Copy link
Member

@robertknight robertknight left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Eduardo,

This looks nice and simple. We'll need a fallback for browsers that don't support document.elementsFromPoint since it requires, for example, quite recent versions of Firefox and Safari (>= 12). Returning a static value in that case would be fine.

Can you also add a few tests. These can mock document.elementsFromPoint to make things easier.

@esanzgar
Copy link
Contributor Author

esanzgar commented Oct 8, 2020

@robertknight, I fully agree about (1) returning a default static value (as we were doing previously) in case of document.elementsFromPoint is not available, and (2) adding more tests.

@esanzgar esanzgar force-pushed the z-index-cleaner-fix-2009 branch 3 times, most recently from ef8813a to 228134a Compare October 9, 2020 11:39
@esanzgar
Copy link
Contributor Author

esanzgar commented Oct 9, 2020

On the second commit I provided a fallback option in case document.elementsFromPoint is not defined.
IE and Edge supports a non-standard version of this method.

@esanzgar esanzgar requested a review from robertknight October 9, 2020 11:45
@esanzgar esanzgar force-pushed the z-index-cleaner-fix-2009 branch from 228134a to 6acf985 Compare October 9, 2020 12:25
Copy link
Member

@robertknight robertknight left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Eduardo,

This looks good. I think the comment for _findZindex could be improved though to explain why it is doing what it does. I believe the comments about Opera on MDN are out of date, although the value of 2^15 is a perfectly reasonable fallback value.

In tests we generally avoid testing private methods, although this may occasionally be OK if it is unlikely to get in the way of refactoring the code in future. I suggested a way that you could test the z-index without calling _findZindex directly.

@esanzgar esanzgar force-pushed the z-index-cleaner-fix-2009 branch from 6acf985 to e9e5ce8 Compare October 12, 2020 10:35
@esanzgar esanzgar requested a review from robertknight October 12, 2020 10:39
Copy link
Member

@robertknight robertknight left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Eduardo - This looks good. Can you update the description of the describe(... block in the test following the change to use only public API of the class. Also, I would suggest to remove the reference to MDN and Opera because that information is long out of date. 2^15 is effectively just "an arbitrary large value that will work almost all web pages". Other than these two small items, this is good to merge.

@esanzgar esanzgar force-pushed the z-index-cleaner-fix-2009 branch from e9e5ce8 to d5f7443 Compare October 12, 2020 16:10
@esanzgar esanzgar merged commit 701203c into master Oct 12, 2020
@esanzgar esanzgar deleted the z-index-cleaner-fix-2009 branch October 12, 2020 16:15
@esanzgar esanzgar linked an issue Oct 12, 2020 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Annotate/Highlight buttons not visible on nzz.ch Adder doesn't always appear above page content
2 participants