Skip to content

Commit

Permalink
Simplify the hover replacement function
Browse files Browse the repository at this point in the history
  • Loading branch information
eoghanmurray committed Jul 16, 2024
1 parent 40bbc25 commit 6e33e1f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 52 deletions.
6 changes: 6 additions & 0 deletions .changeset/simplifify-hover-replacement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"rrweb-snapshot": patch
"rrweb": patch
---

Slight simplification to how we replace :hover after #1458
55 changes: 3 additions & 52 deletions packages/rrweb-snapshot/src/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const mediaSelectorPlugin: AcceptedPlugin = {
},
};

// Adapted from https://github.com/giuseppeg/postcss-pseudo-classes/blob/master/index.js
// Simplified from https://github.com/giuseppeg/postcss-pseudo-classes/blob/master/index.js
const pseudoClassPlugin: AcceptedPlugin = {
postcssPlugin: 'postcss-hover-classes',
prepare: function () {
Expand All @@ -28,58 +28,9 @@ const pseudoClassPlugin: AcceptedPlugin = {
return;
}
fixed.push(rule);

rule.selectors.forEach(function (selector) {
if (!selector.includes(':')) {
return;
}

const selectorParts = selector.replace(/\n/g, ' ').split(' ');
const pseudoedSelectorParts: string[] = [];

selectorParts.forEach(function (selectorPart) {
const pseudos = selectorPart.match(/::?([^:]+)/g);

if (!pseudos) {
pseudoedSelectorParts.push(selectorPart);
return;
}

const baseSelector = selectorPart.substr(
0,
selectorPart.length - pseudos.join('').length,
);

const classPseudos = pseudos.map(function (pseudo) {
const pseudoToCheck = pseudo.replace(/\(.*/g, '');
if (pseudoToCheck !== ':hover') {
return pseudo;
}

// Ignore pseudo-elements!
if (pseudo.match(/^::/)) {
return pseudo;
}

// Kill the colon
pseudo = pseudo.substr(1);

// Replace left and right parens
pseudo = pseudo.replace(/\(/g, '\\(');
pseudo = pseudo.replace(/\)/g, '\\)');

return '.' + '\\:' + pseudo;
});

pseudoedSelectorParts.push(baseSelector + classPseudos.join(''));
});

addSelector(pseudoedSelectorParts.join(' '));

function addSelector(newSelector: string) {
if (newSelector && newSelector !== selector) {
rule.selector += ',\n' + newSelector;
}
if (selector.includes(':hover')) {
rule.selector += ',\n' + selector.replace(/:hover/g, '.\\:hover');
}
});
},
Expand Down

0 comments on commit 6e33e1f

Please sign in to comment.