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

Do not set invalid style #12214

Merged
merged 1 commit into from Apr 16, 2021
Merged

Conversation

ahocevar
Copy link
Member

We need to use block display for measuring text heights.

Fixes #12213.

Copy link
Contributor

@MoonE MoonE left a comment

Choose a reason for hiding this comment

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

The current version did not werk because !important is not allowed for styles set directly on an element. Which means all the styles set here did not have any effect.
The patch below should work, margin seems to have no effect on offsetHeight, though border does. left is not needed if the div is removed.

diff --git a/src/ol/render/canvas.js b/src/ol/render/canvas.js
index 61780050a..e5f793324 100644
--- a/src/ol/render/canvas.js
+++ b/src/ol/render/canvas.js
@@ -306,10 +306,10 @@ export const measureTextHeight = (function () {
         if (!div) {
           div = document.createElement('div');
           div.innerHTML = 'M';
-          div.style.margin = '0 !important';
-          div.style.padding = '0 !important';
-          div.style.position = 'absolute !important';
-          div.style.left = '-99999px !important';
+          div.style.padding = '0';
+          div.style.border = 'none';
+          div.style.position = 'absolute';
         }
         div.style.font = fontSpec;
         document.body.appendChild(div);

@ahocevar ahocevar changed the title Set block layout for text measurement Do not set invalid style Apr 16, 2021
@ahocevar
Copy link
Member Author

Wow, good find, @MoonE. You're absolutely right, I just force-pushed a commit with your suggestion, and it works indeed.

@MoonE
Copy link
Contributor

MoonE commented Apr 16, 2021

The pushed commit isn't quite my suggestion.
Margin can be removed and left can be removed, but border could be added in case it was set by a user style sheet.
See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetWidth

Anyway, to be extra sure, it might as well be

diff --git a/src/ol/render/canvas.js b/src/ol/render/canvas.js
index 61780050a..3d5ea7a0e 100644
--- a/src/ol/render/canvas.js
+++ b/src/ol/render/canvas.js
@@ -306,10 +306,13 @@ export const measureTextHeight = (function () {
         if (!div) {
           div = document.createElement('div');
           div.innerHTML = 'M';
-          div.style.margin = '0 !important';
-          div.style.padding = '0 !important';
-          div.style.position = 'absolute !important';
-          div.style.left = '-99999px !important';
+          div.style.minHeight = '0';
+          div.style.maxHeight = 'none';
+          div.style.height = 'auto';
+          div.style.padding = '0';
+          div.style.border = 'none';
+          div.style.position = 'absolute';
+          div.style.display = 'block';
         }
         div.style.font = fontSpec;
         document.body.appendChild(div);

Might be hyper correct now.
Styles only need to be reset in case the user overides them in a style sheet, normally you wouldn't set these with a super general rule, but who knows...

@ahocevar
Copy link
Member Author

left was added intentionally to render the element outside the visible viewport. Feel free to modify my branch directly. Thanks!

@ahocevar
Copy link
Member Author

Thanks, looks good to me now. Do you agree, @MoonE ?

@ahocevar ahocevar merged commit 72f5b34 into openlayers:main Apr 16, 2021
@ahocevar ahocevar deleted the avoid-flex-layout branch April 16, 2021 22:08
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.

backgroundFill is bugged when body is flex
2 participants