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

feat: add new css parser (postcss) #193

Draft
wants to merge 17 commits into
base: develop
Choose a base branch
from
Next Next commit
feat: add new css parser
  • Loading branch information
daibhin committed Apr 24, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit a04450ab19a135f97040dade50cd4421932912a0
8 changes: 6 additions & 2 deletions packages/rrweb-snapshot/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rrweb-snapshot",
"version": "2.0.0-alpha.13",
"version": "2.0.0-alpha.12",
"description": "rrweb's component to take a snapshot of DOM, aka DOM serializer",
"scripts": {
"prepare": "npm run prepack",
@@ -58,6 +58,10 @@
"rollup-plugin-typescript2": "^0.31.2",
"ts-jest": "^27.0.5",
"ts-node": "^7.0.1",
"tslib": "^1.9.3"
"tslib": "^1.9.3",
"@types/css-tree": "^2.3.7"
},
"dependencies": {
"css-tree": "^2.3.1"
}
}
8 changes: 6 additions & 2 deletions packages/rrweb-snapshot/src/rebuild.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Rule, Media, NodeWithRules, parse } from './css';
import { Rule, Media, NodeWithRules, parse as parseOld } from './css';
import {
serializedNodeWithId,
NodeType,
@@ -8,6 +8,7 @@ import {
legacyAttributes,
} from './types';
import { isElement, Mirror, isNodeMetaEqual } from './utils';
import { parse, findAll } from 'css-tree';

const tagMap: tagMap = {
script: 'noscript',
@@ -70,14 +71,17 @@ export function adaptCssForReplay(cssText: string, cache: BuildCache): string {
const cachedStyle = cache?.stylesWithHoverClass.get(cssText);
if (cachedStyle) return cachedStyle;

const ast = parse(cssText, {
const ast = parseOld(cssText, {
silent: true,
});

if (!ast.stylesheet) {
return cssText;
}

const newAst = parse(cssText);
const newSelectors: string[] = findAll(newAst, (node, _, _) => node.type === 'Selector');

const selectors: string[] = [];
const medias: string[] = [];
function getSelectors(rule: Rule | Media | NodeWithRules) {
Original file line number Diff line number Diff line change
@@ -364,6 +364,12 @@ exports[`integration tests [html file]: picture-in-frame.html 1`] = `
</body></html>"
`;

exports[`integration tests [html file]: picture-with-inline-onload.html 1`] = `
"<html xmlns=\\"http://www.w3.org/1999/xhtml\\"><head></head><body>
<img src=\\"http://localhost:3030/images/robot.png\\" alt=\\"This is a robot\\" style=\\"opacity: 1;\\" _onload=\\"this.style.opacity=1\\" />
</body></html>"
`;

exports[`integration tests [html file]: preload.html 1`] = `
"<!DOCTYPE html><html lang=\\"en\\"><head>
<meta charset=\\"UTF-8\\" />
35 changes: 34 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
@@ -3277,6 +3277,11 @@
resolved "https://registry.npmjs.org/@types/css-font-loading-module/-/css-font-loading-module-0.0.7.tgz"
integrity sha512-nl09VhutdjINdWyXxHWN/w9zlNCfr60JUqJbd24YXUuCwgeL0TpFSdElCwb6cxfB6ybE19Gjj4g0jsgkXxKv1Q==

"@types/css-tree@^2.3.7":
version "2.3.7"
resolved "https://registry.yarnpkg.com/@types/css-tree/-/css-tree-2.3.7.tgz#02818035f0d9cf88c2e6983c838526e5624bf189"
integrity sha512-LUlutQBpR2TgqZJdvXCPOx9EME7a4PHSEo2Y2c8POFpj1E9a6V94PUZNwjVdfHWyb8RQZoNHTYOKs980+sOi+g==

"@types/cssom@^0.4.1":
version "0.4.1"
resolved "https://registry.npmjs.org/@types/cssom/-/cssom-0.4.1.tgz#fb64e145b425bd6c1b0ed78ebd66ba43b6e088ab"
@@ -5474,6 +5479,14 @@ css-tree@^1.0.0-alpha.39, css-tree@^1.1.2:
mdn-data "2.0.14"
source-map "^0.6.1"

css-tree@^2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/css-tree/-/css-tree-2.3.1.tgz#10264ce1e5442e8572fc82fbe490644ff54b5c20"
integrity sha512-6Fv1DV/TYw//QF5IzQdqsNDjx/wc8TrMBZsqjL9eW01tWb7R7k/mq+/VXfJCl7SoD5emsJop9cOByJZfs8hYIw==
dependencies:
mdn-data "2.0.30"
source-map-js "^1.0.1"

css-what@^3.2.1:
version "3.4.2"
resolved "https://registry.npmjs.org/css-what/-/css-what-3.4.2.tgz"
@@ -5564,10 +5577,15 @@ csso@^4.0.2:
dependencies:
css-tree "^1.1.2"

cssom@^0.4.4, cssom@^0.5.0, "cssom@https://registry.npmjs.org/rrweb-cssom/-/rrweb-cssom-0.6.0.tgz":
cssom@^0.4.4, "cssom@https://registry.npmjs.org/rrweb-cssom/-/rrweb-cssom-0.6.0.tgz":
version "0.6.0"
resolved "https://registry.npmjs.org/rrweb-cssom/-/rrweb-cssom-0.6.0.tgz#ed298055b97cbddcdeb278f904857629dec5e0e1"

cssom@^0.5.0:
version "0.5.0"
resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.5.0.tgz#d254fa92cd8b6fbd83811b9fbaed34663cc17c36"
integrity sha512-iKuQcq+NdHqlAcwUY0o/HL69XQrUaQdMjmStJ8JFmUaiiQErlhrmuigkg/CU4E2J0IyUKUrMAgl36TvN67MqTw==

cssom@~0.3.6:
version "0.3.8"
resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.3.8.tgz#9f1276f5b2b463f2114d3f2c75250af8c1a36f4a"
@@ -10802,6 +10820,11 @@ mdn-data@2.0.14:
resolved "https://registry.npmjs.org/mdn-data/-/mdn-data-2.0.14.tgz"
integrity sha512-dn6wd0uw5GsdswPFfsgMp5NSB0/aDe6fK94YJV/AJDYXL6HVLWBsxeq7js7Ad+mU2K9LAlwpk6kN2D5mwCPVow==

mdn-data@2.0.30:
version "2.0.30"
resolved "https://registry.yarnpkg.com/mdn-data/-/mdn-data-2.0.30.tgz#ce4df6f80af6cfbe218ecd5c552ba13c4dfa08cc"
integrity sha512-GaqWWShW4kv/G9IEucWScBx9G1/vsFZZJUO+tD26M8J8z3Kw5RDQjaoZe03YAClgeS/SWPOcb4nkFBTEi5DUEA==

mdn-data@2.0.4:
version "2.0.4"
resolved "https://registry.npmjs.org/mdn-data/-/mdn-data-2.0.4.tgz"
@@ -13160,6 +13183,11 @@ rollup@~2.78.0:
optionalDependencies:
fsevents "~2.3.2"

rrweb-snapshot@^2.0.0-alpha.13:
version "2.0.0-alpha.13"
resolved "https://registry.yarnpkg.com/rrweb-snapshot/-/rrweb-snapshot-2.0.0-alpha.13.tgz#25097ae71ca4ac6c66edf3cae754607886f689e2"
integrity sha512-slbhNBCYjxLGCeH95a67ECCy5a22nloXp1F5wF7DCzUNw80FN7tF9Lef1sRGLNo32g3mNqTc2sWLATlKejMxYw==

run-async@^2.4.0:
version "2.4.1"
resolved "https://registry.npmjs.org/run-async/-/run-async-2.4.1.tgz"
@@ -13562,6 +13590,11 @@ sorcery@^0.11.0:
minimist "^1.2.0"
sander "^0.5.0"

source-map-js@^1.0.1:
version "1.2.0"
resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.2.0.tgz#16b809c162517b5b8c3e7dcd315a2a5c2612b2af"
integrity sha512-itJW8lvSA0TXEphiRoawsCksnlf8SyvmFzIhltqAHluXd88pkCd+cXJVHTDwdCr0IzwptSm035IHQktUu1QUMg==

source-map-js@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.0.2.tgz#adbc361d9c62df380125e7f161f71c826f1e490c"