From 525be9afeac29d69c61a260ca0687ab0c0492b16 Mon Sep 17 00:00:00 2001
From: Jonas Jenwald <jonas.jenwald@gmail.com>
Date: Fri, 27 Oct 2023 17:38:01 +0200
Subject: [PATCH] Use CSS nesting in the textLayer

---
 test/text_layer_test.css   |  27 +++----
 web/text_layer_builder.css | 148 ++++++++++++++++++-------------------
 2 files changed, 86 insertions(+), 89 deletions(-)

diff --git a/test/text_layer_test.css b/test/text_layer_test.css
index 9f7b0ca57..df0d5859b 100644
--- a/test/text_layer_test.css
+++ b/test/text_layer_test.css
@@ -20,18 +20,19 @@
   inset: 0;
   line-height: 1;
   opacity: 1;
-}
-.textLayer :is(span, br) {
-  color: black;
-  position: absolute;
-  white-space: pre;
-  transform-origin: 0% 0%;
-  border: solid 1px rgb(255 0 0 / 0.5);
-  background-color: rgb(255 255 32 / 0.1);
-  box-sizing: border-box;
-}
 
-.textLayer .markedContent {
-  border: none;
-  background-color: transparent;
+  :is(span, br) {
+    color: black;
+    position: absolute;
+    white-space: pre;
+    transform-origin: 0% 0%;
+    border: solid 1px rgb(255 0 0 / 0.5);
+    background-color: rgb(255 255 32 / 0.1);
+    box-sizing: border-box;
+  }
+
+  .markedContent {
+    border: none;
+    background-color: transparent;
+  }
 }
diff --git a/web/text_layer_builder.css b/web/text_layer_builder.css
index 992c888b1..fca3ac8d7 100644
--- a/web/text_layer_builder.css
+++ b/web/text_layer_builder.css
@@ -13,18 +13,6 @@
  * limitations under the License.
  */
 
-:root {
-  --highlight-bg-color: rgb(180 0 170);
-  --highlight-selected-bg-color: rgb(0 100 0);
-}
-
-@media screen and (forced-colors: active) {
-  :root {
-    --highlight-bg-color: Highlight;
-    --highlight-selected-bg-color: ButtonText;
-  }
-}
-
 .textLayer {
   position: absolute;
   text-align: initial;
@@ -36,75 +24,83 @@
   forced-color-adjust: none;
   transform-origin: 0 0;
   z-index: 2;
-}
 
-.textLayer :is(span, br) {
-  color: transparent;
-  position: absolute;
-  white-space: pre;
-  cursor: text;
-  transform-origin: 0% 0%;
-}
+  :is(span, br) {
+    color: transparent;
+    position: absolute;
+    white-space: pre;
+    cursor: text;
+    transform-origin: 0% 0%;
+  }
 
-/* Only necessary in Google Chrome, see issue 14205, and most unfortunately
- * the problem doesn't show up in "text" reference tests. */
-/*#if !MOZCENTRAL*/
-.textLayer span.markedContent {
-  top: 0;
-  height: 0;
-}
-/*#endif*/
-
-.textLayer .highlight {
-  margin: -1px;
-  padding: 1px;
-  background-color: var(--highlight-bg-color);
-  border-radius: 4px;
-}
-
-.textLayer .highlight.appended {
-  position: initial;
-}
-
-.textLayer .highlight.begin {
-  border-radius: 4px 0 0 4px;
-}
-
-.textLayer .highlight.end {
-  border-radius: 0 4px 4px 0;
-}
-
-.textLayer .highlight.middle {
-  border-radius: 0;
-}
-
-.textLayer .highlight.selected {
-  background-color: var(--highlight-selected-bg-color);
-}
-
-.textLayer ::selection {
+  /* Only necessary in Google Chrome, see issue 14205, and most unfortunately
+   * the problem doesn't show up in "text" reference tests. */
   /*#if !MOZCENTRAL*/
-  background: blue;
+  span.markedContent {
+    top: 0;
+    height: 0;
+  }
   /*#endif*/
-  background: AccentColor; /* stylelint-disable-line declaration-block-no-duplicate-properties */
-}
 
-/* Avoids https://github.com/mozilla/pdf.js/issues/13840 in Chrome */
-/*#if !MOZCENTRAL*/
-.textLayer br::selection {
-  background: transparent;
-}
-/*#endif*/
+  .highlight {
+    --highlight-bg-color: rgb(180 0 170);
+    --highlight-selected-bg-color: rgb(0 100 0);
 
-.textLayer .endOfContent {
-  display: block;
-  position: absolute;
-  inset: 100% 0 0;
-  z-index: -1;
-  cursor: default;
-  user-select: none;
-}
+    @media screen and (forced-colors: active) {
+      --highlight-bg-color: Highlight;
+      --highlight-selected-bg-color: ButtonText;
+    }
 
-.textLayer .endOfContent.active {
-  top: 0;
+    margin: -1px;
+    padding: 1px;
+    background-color: var(--highlight-bg-color);
+    border-radius: 4px;
+
+    &.appended {
+      position: initial;
+    }
+
+    &.begin {
+      border-radius: 4px 0 0 4px;
+    }
+
+    &.end {
+      border-radius: 0 4px 4px 0;
+    }
+
+    &.middle {
+      border-radius: 0;
+    }
+
+    &.selected {
+      background-color: var(--highlight-selected-bg-color);
+    }
+  }
+
+  ::selection {
+    /*#if !MOZCENTRAL*/
+    background: blue;
+    /*#endif*/
+    background: AccentColor; /* stylelint-disable-line declaration-block-no-duplicate-properties */
+  }
+
+  /* Avoids https://github.com/mozilla/pdf.js/issues/13840 in Chrome */
+  /*#if !MOZCENTRAL*/
+  br::selection {
+    background: transparent;
+  }
+  /*#endif*/
+
+  .endOfContent {
+    display: block;
+    position: absolute;
+    inset: 100% 0 0;
+    z-index: -1;
+    cursor: default;
+    user-select: none;
+
+    &.active {
+      top: 0;
+    }
+  }
 }