Skip to content

Commit 2f93f69

Browse files
committed
🎨: improve performance of native selection CSS management
Previous code would always loop for a very large array of CSS rules. The new approach just remembers where the rule is stored and directly deletes it, avoiding the search entirely.
1 parent 029665f commit 2f93f69

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

lively.morphic/text/morph.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -939,13 +939,11 @@ export class Text extends Morph {
939939
}
940940

941941
setupSelectionColorCSSRuleForColor (color) {
942-
const rules = document.styleSheets[0].cssRules;
943-
let idx = 0;
944-
for (const rule of rules) {
945-
if (rule.selectorText === `#${this.id} > div > .line > span::selection`) document.styleSheets[0].deleteRule(idx);
946-
idx++;
942+
if (obj.isNumber(this._ruleIndex)) {
943+
document.styleSheets[0].deleteRule(this._ruleIndex);
944+
delete this._ruleIndex;
947945
}
948-
if (this.selectionMode === 'native') document.styleSheets[0].insertRule(`#${this.id} > div > .line > span::selection { background: ${color.toCSSString()}; }`, 0);
946+
if (this.selectionMode === 'native') this._ruleIndex = document.styleSheets[0].insertRule(`#${this.id} > div > .line > span::selection { background: ${color.toCSSString()}; }`, 0);
949947
}
950948

951949
renderStyles (styleProps) {

0 commit comments

Comments
 (0)