Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

clipper: minor refactors and cleanup

Abdullah Attaad91da9f2d50
diff --git a/packages/clipper/package-lock.json b/packages/clipper/package-lock.json
index 893ca9634..76201607c 100644
--- a/packages/clipper/package-lock.json
+++ b/packages/clipper/package-lock.json
@@ -10,19 +10,10 @@
"license": "GPL-3.0-or-later",
"dependencies": {
"@mozilla/readability": "^0.4.2",
- "boreas": "^1.0.1",
- "css": "^3.0.0",
- "css-expression": "^0.0.8",
- "css-tree": "^2.2.1",
"css-what": "^6.1.0",
- "dompurify": "^2.4.0",
"hyperapp": "^2.0.22",
"jest-environment-jsdom": "^29.0.3",
- "postcss-value-parser": "^4.2.0",
- "shady-css-parser": "^0.1.0",
- "slugify": "^1.6.5",
- "specificity": "^0.4.1",
- "stylis": "^4.1.2"
+ "specificity": "^0.4.1"
},
"devDependencies": {
"@playwright/test": "^1.27.1",
@@ -31,6 +22,7 @@
"@types/dompurify": "^2.3.4",
"@types/stylis": "^4.0.2",
"esbuild": "^0.15.9",
+ "slugify": "^1.6.5",
"ts-node": "^10.9.1",
"typescript": "^4.8.2"
}
@@ -506,22 +498,6 @@
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
},
- "node_modules/atob": {
- "version": "2.1.2",
- "resolved": "https://registry.npmjs.org/atob/-/atob-2.1.2.tgz",
- "integrity": "sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg==",
- "bin": {
- "atob": "bin/atob.js"
- },
- "engines": {
- "node": ">= 4.5.0"
- }
- },
- "node_modules/boreas": {
- "version": "1.0.1",
- "resolved": "https://registry.npmjs.org/boreas/-/boreas-1.0.1.tgz",
- "integrity": "sha512-4gwEdtD4BGZKdmXNVlW18ej/618YilIVQSCvwTngMhVoa/WsVmMXbIWB3UKuwmvUhd/AgL89OG6e/GnteZETAA=="
- },
"node_modules/braces": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz",
@@ -591,37 +567,6 @@
"integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==",
"dev": true
},
- "node_modules/css": {
- "version": "3.0.0",
- "resolved": "https://registry.npmjs.org/css/-/css-3.0.0.tgz",
- "integrity": "sha512-DG9pFfwOrzc+hawpmqX/dHYHJG+Bsdb0klhyi1sDneOgGOXy9wQIC8hzyVp1e4NRYDBdxcylvywPkkXCHAzTyQ==",
- "dependencies": {
- "inherits": "^2.0.4",
- "source-map": "^0.6.1",
- "source-map-resolve": "^0.6.0"
- }
- },
- "node_modules/css-expression": {
- "version": "0.0.8",
- "resolved": "https://registry.npmjs.org/css-expression/-/css-expression-0.0.8.tgz",
- "integrity": "sha512-LxpsxtSsnFzf0vcbQZEc1otQnDIOQowQUilIczPVjVF/D73fRswlb6NCS/AX2Jp0QVDa0Dp+EGJzvj+GA92Klg==",
- "engines": {
- "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
- }
- },
- "node_modules/css-tree": {
- "version": "2.2.1",
- "resolved": "https://registry.npmjs.org/css-tree/-/css-tree-2.2.1.tgz",
- "integrity": "sha512-OA0mILzGc1kCOCSJerOeqDxDQ4HOh+G8NbOJFOTgOCzpw7fCBubk0fEyxp8AgOL/jvLgYA/uV0cMbe43ElF1JA==",
- "dependencies": {
- "mdn-data": "2.0.28",
- "source-map-js": "^1.0.1"
- },
- "engines": {
- "node": "^10 || ^12.20.0 || ^14.13.0 || >=15.0.0",
- "npm": ">=7.0.0"
- }
- },
"node_modules/css-what": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/css-what/-/css-what-6.1.0.tgz",
@@ -711,14 +656,6 @@
"resolved": "https://registry.npmjs.org/decimal.js/-/decimal.js-10.4.1.tgz",
"integrity": "sha512-F29o+vci4DodHYT9UrR5IEbfBw9pE5eSapIJdTqXK5+6hq+t8VRxwQyKlW2i+KDKFkkJQRvFyI/QXD83h8LyQw=="
},
- "node_modules/decode-uri-component": {
- "version": "0.2.0",
- "resolved": "https://registry.npmjs.org/decode-uri-component/-/decode-uri-component-0.2.0.tgz",
- "integrity": "sha512-hjf+xovcEn31w/EUYdTXQh/8smFL/dzYjohQGEIgjyNavaJfBY2p5F527Bo1VPATxv0VYTUC2bOcXvqFwk78Og==",
- "engines": {
- "node": ">=0.10"
- }
- },
"node_modules/deep-is": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.4.tgz",
@@ -752,11 +689,6 @@
"node": ">=12"
}
},
- "node_modules/dompurify": {
- "version": "2.4.0",
- "resolved": "https://registry.npmjs.org/dompurify/-/dompurify-2.4.0.tgz",
- "integrity": "sha512-Be9tbQMZds4a3C6xTmz68NlMfeONA//4dOavl/1rNw50E+/QO0KVpbcU0PcaW0nsQxurXls9ZocqFxk8R2mWEA=="
- },
"node_modules/entities": {
"version": "4.4.0",
"resolved": "https://registry.npmjs.org/entities/-/entities-4.4.0.tgz",
@@ -1263,11 +1195,6 @@
"node": ">=0.10.0"
}
},
- "node_modules/inherits": {
- "version": "2.0.4",
- "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
- "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
- },
"node_modules/is-number": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz",
@@ -1450,11 +1377,6 @@
"integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==",
"dev": true
},
- "node_modules/mdn-data": {
- "version": "2.0.28",
- "resolved": "https://registry.npmjs.org/mdn-data/-/mdn-data-2.0.28.tgz",
- "integrity": "sha512-aylIc7Z9y4yzHYAJNuESG3hfhC+0Ibp/MAMiaOZgNv4pmEdFyfZhhhny4MNiAfWdBQ1RQ2mfDWmM1x8SvGyp8g=="
- },
"node_modules/micromatch": {
"version": "4.0.5",
"resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz",
@@ -1546,11 +1468,6 @@
"node": ">=14"
}
},
- "node_modules/postcss-value-parser": {
- "version": "4.2.0",
- "resolved": "https://registry.npmjs.org/postcss-value-parser/-/postcss-value-parser-4.2.0.tgz",
- "integrity": "sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ=="
- },
"node_modules/prelude-ls": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.1.2.tgz",
@@ -1627,11 +1544,6 @@
"node": ">=v12.22.7"
}
},
- "node_modules/shady-css-parser": {
- "version": "0.1.0",
- "resolved": "https://registry.npmjs.org/shady-css-parser/-/shady-css-parser-0.1.0.tgz",
- "integrity": "sha512-irfJUUkEuDlNHKZNAp2r7zOyMlmbfVJ+kWSfjlCYYUx/7dJnANLCyTzQZsuxy5NJkvtNwSxY5Gj8MOlqXUQPyA=="
- },
"node_modules/slash": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/slash/-/slash-3.0.0.tgz",
@@ -1644,6 +1556,7 @@
"version": "1.6.5",
"resolved": "https://registry.npmjs.org/slugify/-/slugify-1.6.5.tgz",
"integrity": "sha512-8mo9bslnBO3tr5PEVFzMPIWwWnipGS0xVbYf65zxDqfNwmzYn1LpiKNrR6DlClusuvo+hDHd1zKpmfAe83NQSQ==",
+ "dev": true,
"engines": {
"node": ">=8.0.0"
}
@@ -1652,28 +1565,11 @@
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
"integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
+ "optional": true,
"engines": {
"node": ">=0.10.0"
}
},
- "node_modules/source-map-js": {
- "version": "1.0.2",
- "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.0.2.tgz",
- "integrity": "sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==",
- "engines": {
- "node": ">=0.10.0"
- }
- },
- "node_modules/source-map-resolve": {
- "version": "0.6.0",
- "resolved": "https://registry.npmjs.org/source-map-resolve/-/source-map-resolve-0.6.0.tgz",
- "integrity": "sha512-KXBr9d/fO/bWo97NXsPIAW1bFSBOuCnjbNTBMO7N59hsv5i9yzRDfcYwwt0l04+VqnKC+EwzvJZIP/qkuMgR/w==",
- "deprecated": "See https://github.com/lydell/source-map-resolve#deprecated",
- "dependencies": {
- "atob": "^2.1.2",
- "decode-uri-component": "^0.2.0"
- }
- },
"node_modules/specificity": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/specificity/-/specificity-0.4.1.tgz",
@@ -1693,11 +1589,6 @@
"node": ">=10"
}
},
- "node_modules/stylis": {
- "version": "4.1.2",
- "resolved": "https://registry.npmjs.org/stylis/-/stylis-4.1.2.tgz",
- "integrity": "sha512-Nn2CCrG2ZaFziDxaZPN43CXqn+j7tcdjPFCkRBkFue8QYXC2HdEwnw5TCBo4yQZ2WxKYeSi0fdoOrtEqgDrXbA=="
- },
"node_modules/supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
@@ -2323,16 +2214,6 @@
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
},
- "atob": {
- "version": "2.1.2",
- "resolved": "https://registry.npmjs.org/atob/-/atob-2.1.2.tgz",
- "integrity": "sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg=="
- },
- "boreas": {
- "version": "1.0.1",
- "resolved": "https://registry.npmjs.org/boreas/-/boreas-1.0.1.tgz",
- "integrity": "sha512-4gwEdtD4BGZKdmXNVlW18ej/618YilIVQSCvwTngMhVoa/WsVmMXbIWB3UKuwmvUhd/AgL89OG6e/GnteZETAA=="
- },
"braces": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz",
@@ -2387,30 +2268,6 @@
"integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==",
"dev": true
},
- "css": {
- "version": "3.0.0",
- "resolved": "https://registry.npmjs.org/css/-/css-3.0.0.tgz",
- "integrity": "sha512-DG9pFfwOrzc+hawpmqX/dHYHJG+Bsdb0klhyi1sDneOgGOXy9wQIC8hzyVp1e4NRYDBdxcylvywPkkXCHAzTyQ==",
- "requires": {
- "inherits": "^2.0.4",
- "source-map": "^0.6.1",
- "source-map-resolve": "^0.6.0"
- }
- },
- "css-expression": {
- "version": "0.0.8",
- "resolved": "https://registry.npmjs.org/css-expression/-/css-expression-0.0.8.tgz",
- "integrity": "sha512-LxpsxtSsnFzf0vcbQZEc1otQnDIOQowQUilIczPVjVF/D73fRswlb6NCS/AX2Jp0QVDa0Dp+EGJzvj+GA92Klg=="
- },
- "css-tree": {
- "version": "2.2.1",
- "resolved": "https://registry.npmjs.org/css-tree/-/css-tree-2.2.1.tgz",
- "integrity": "sha512-OA0mILzGc1kCOCSJerOeqDxDQ4HOh+G8NbOJFOTgOCzpw7fCBubk0fEyxp8AgOL/jvLgYA/uV0cMbe43ElF1JA==",
- "requires": {
- "mdn-data": "2.0.28",
- "source-map-js": "^1.0.1"
- }
- },
"css-what": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/css-what/-/css-what-6.1.0.tgz",
@@ -2478,11 +2335,6 @@
"resolved": "https://registry.npmjs.org/decimal.js/-/decimal.js-10.4.1.tgz",
"integrity": "sha512-F29o+vci4DodHYT9UrR5IEbfBw9pE5eSapIJdTqXK5+6hq+t8VRxwQyKlW2i+KDKFkkJQRvFyI/QXD83h8LyQw=="
},
- "decode-uri-component": {
- "version": "0.2.0",
- "resolved": "https://registry.npmjs.org/decode-uri-component/-/decode-uri-component-0.2.0.tgz",
- "integrity": "sha512-hjf+xovcEn31w/EUYdTXQh/8smFL/dzYjohQGEIgjyNavaJfBY2p5F527Bo1VPATxv0VYTUC2bOcXvqFwk78Og=="
- },
"deep-is": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.4.tgz",
@@ -2507,11 +2359,6 @@
"webidl-conversions": "^7.0.0"
}
},
- "dompurify": {
- "version": "2.4.0",
- "resolved": "https://registry.npmjs.org/dompurify/-/dompurify-2.4.0.tgz",
- "integrity": "sha512-Be9tbQMZds4a3C6xTmz68NlMfeONA//4dOavl/1rNw50E+/QO0KVpbcU0PcaW0nsQxurXls9ZocqFxk8R2mWEA=="
- },
"entities": {
"version": "4.4.0",
"resolved": "https://registry.npmjs.org/entities/-/entities-4.4.0.tgz",
@@ -2782,11 +2629,6 @@
"safer-buffer": ">= 2.1.2 < 3.0.0"
}
},
- "inherits": {
- "version": "2.0.4",
- "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
- "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
- },
"is-number": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz",
@@ -2933,11 +2775,6 @@
"integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==",
"dev": true
},
- "mdn-data": {
- "version": "2.0.28",
- "resolved": "https://registry.npmjs.org/mdn-data/-/mdn-data-2.0.28.tgz",
- "integrity": "sha512-aylIc7Z9y4yzHYAJNuESG3hfhC+0Ibp/MAMiaOZgNv4pmEdFyfZhhhny4MNiAfWdBQ1RQ2mfDWmM1x8SvGyp8g=="
- },
"micromatch": {
"version": "4.0.5",
"resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz",
@@ -3002,11 +2839,6 @@
"integrity": "sha512-9EmeXDncC2Pmp/z+teoVYlvmPWUC6ejSSYZUln7YaP89Z6lpAaiaAnqroUt/BoLo8tn7WYShcfaCh+xofZa44Q==",
"dev": true
},
- "postcss-value-parser": {
- "version": "4.2.0",
- "resolved": "https://registry.npmjs.org/postcss-value-parser/-/postcss-value-parser-4.2.0.tgz",
- "integrity": "sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ=="
- },
"prelude-ls": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.1.2.tgz",
@@ -3067,11 +2899,6 @@
"xmlchars": "^2.2.0"
}
},
- "shady-css-parser": {
- "version": "0.1.0",
- "resolved": "https://registry.npmjs.org/shady-css-parser/-/shady-css-parser-0.1.0.tgz",
- "integrity": "sha512-irfJUUkEuDlNHKZNAp2r7zOyMlmbfVJ+kWSfjlCYYUx/7dJnANLCyTzQZsuxy5NJkvtNwSxY5Gj8MOlqXUQPyA=="
- },
"slash": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/slash/-/slash-3.0.0.tgz",
@@ -3080,26 +2907,14 @@
"slugify": {
"version": "1.6.5",
"resolved": "https://registry.npmjs.org/slugify/-/slugify-1.6.5.tgz",
- "integrity": "sha512-8mo9bslnBO3tr5PEVFzMPIWwWnipGS0xVbYf65zxDqfNwmzYn1LpiKNrR6DlClusuvo+hDHd1zKpmfAe83NQSQ=="
+ "integrity": "sha512-8mo9bslnBO3tr5PEVFzMPIWwWnipGS0xVbYf65zxDqfNwmzYn1LpiKNrR6DlClusuvo+hDHd1zKpmfAe83NQSQ==",
+ "dev": true
},
"source-map": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
- "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g=="
- },
- "source-map-js": {
- "version": "1.0.2",
- "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.0.2.tgz",
- "integrity": "sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw=="
- },
- "source-map-resolve": {
- "version": "0.6.0",
- "resolved": "https://registry.npmjs.org/source-map-resolve/-/source-map-resolve-0.6.0.tgz",
- "integrity": "sha512-KXBr9d/fO/bWo97NXsPIAW1bFSBOuCnjbNTBMO7N59hsv5i9yzRDfcYwwt0l04+VqnKC+EwzvJZIP/qkuMgR/w==",
- "requires": {
- "atob": "^2.1.2",
- "decode-uri-component": "^0.2.0"
- }
+ "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
+ "optional": true
},
"specificity": {
"version": "0.4.1",
@@ -3114,11 +2929,6 @@
"escape-string-regexp": "^2.0.0"
}
},
- "stylis": {
- "version": "4.1.2",
- "resolved": "https://registry.npmjs.org/stylis/-/stylis-4.1.2.tgz",
- "integrity": "sha512-Nn2CCrG2ZaFziDxaZPN43CXqn+j7tcdjPFCkRBkFue8QYXC2HdEwnw5TCBo4yQZ2WxKYeSi0fdoOrtEqgDrXbA=="
- },
"supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
@@ -3261,8 +3071,7 @@
"ws": {
"version": "8.9.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.9.0.tgz",
- "integrity": "sha512-Ja7nszREasGaYUYCI2k4lCKIRTt+y7XuqVoHR44YpI49TtryyqbqvDMn5eqfW7e6HzTukDRIsXqzVHScqRcafg==",
- "requires": {}
+ "integrity": "sha512-Ja7nszREasGaYUYCI2k4lCKIRTt+y7XuqVoHR44YpI49TtryyqbqvDMn5eqfW7e6HzTukDRIsXqzVHScqRcafg=="
},
"xml-name-validator": {
"version": "4.0.0",
diff --git a/packages/clipper/package.json b/packages/clipper/package.json
index 878b43a5d..7c5a2e9d6 100644
--- a/packages/clipper/package.json
+++ b/packages/clipper/package.json
@@ -37,9 +37,7 @@
"dependencies": {
"@mozilla/readability": "^0.4.2",
"css-what": "^6.1.0",
- "dompurify": "^2.4.0",
"hyperapp": "^2.0.22",
- "jest-environment-jsdom": "^29.0.3",
"specificity": "^0.4.1"
}
}
diff --git a/packages/clipper/src/clone.ts b/packages/clipper/src/clone.ts
index c6aed838e..a62510cf8 100644
--- a/packages/clipper/src/clone.ts
+++ b/packages/clipper/src/clone.ts
@@ -105,6 +105,7 @@ type CloneProps = {
filter?: Filter;
root: boolean;
vector: boolean;
+ styles?: boolean;
getElementStyles?: (element: HTMLElement) => CSSStyleDeclaration | undefined;
getPseudoElementStyles?: (
element: HTMLElement,
@@ -210,8 +211,10 @@ function processClone(
// if (clone instanceof HTMLElement) removeAttributes(clone);
- copyStyle(original, clone, options);
- clonePseudoElements(original, clone, options);
+ if (options.styles) {
+ copyStyle(original, clone, options);
+ clonePseudoElements(original, clone, options);
+ }
copyUserInput(original, clone);
fixSvg(clone);
diff --git a/packages/clipper/src/domtoimage.ts b/packages/clipper/src/domtoimage.ts
index 7a34be72d..43d271d80 100644
--- a/packages/clipper/src/domtoimage.ts
+++ b/packages/clipper/src/domtoimage.ts
@@ -23,7 +23,6 @@ import { inlineAllImages } from "./images";
import { Options } from "./types";
import { canvasToBlob, delay, escapeXhtml, height, width } from "./utils";
import { cacheStylesheets, inlineStylesheets } from "./styles";
-import purify from "dompurify";
// Default impl options
const defaultOptions: Options = {
@@ -34,36 +33,27 @@ const defaultOptions: Options = {
async function getInlinedNode(node: HTMLElement, options: Options) {
const { fonts, images, stylesheets } = options.inlineOptions || {};
- console.time("inline styles");
if (stylesheets) await inlineStylesheets(options.fetchOptions || {});
- console.timeEnd("inline styles");
const documentStyles = getComputedStyle(document.documentElement);
- console.time("cache styles");
- const styleCache = cacheStylesheets(documentStyles);
- console.timeEnd("cache styles");
+ const styleCache = stylesheets ? cacheStylesheets(documentStyles) : undefined;
- console.time("clone");
let clone = await cloneNode(node, {
+ styles: options.styles,
filter: options.filter,
root: true,
vector: !options.raster,
fetchOptions: options.fetchOptions,
- getElementStyles: styleCache.get,
- getPseudoElementStyles: styleCache.getPseudo
+ getElementStyles: styleCache?.get,
+ getPseudoElementStyles: styleCache?.getPseudo
});
- console.timeEnd("clone");
if (!clone || clone instanceof Text) return;
- console.time("embed fonts");
if (fonts) clone = await embedFonts(clone, options.fetchOptions || {});
- console.timeEnd("embed fonts");
- console.time("inline images");
if (images) await inlineAllImages(clone, options.fetchOptions || {});
- console.timeEnd("inline images");
finalize(clone);
return clone;
@@ -82,12 +72,6 @@ async function toSvg(node: HTMLElement, options: Options) {
clone = applyOptions(clone, options);
- clone = purify.sanitize(clone, {
- RETURN_DOM: true,
- KEEP_CONTENT: false,
- ADD_ATTR: ["style"]
- });
-
return makeSvgDataUri(
clone,
options.width || width(node),
@@ -101,15 +85,6 @@ function applyOptions(clone: HTMLElement, options: Options) {
if (options.width) clone.style.width = options.width + "px";
if (options.height) clone.style.height = options.height + "px";
- if (options.style) {
- const style = options.style;
- Object.keys(style).forEach(function (property) {
- clone.style.setProperty(property, style.getPropertyValue(property));
- // clone.style[property] = style[property];
- });
- }
-
- options.onCloned?.(clone);
return clone;
}
diff --git a/packages/clipper/src/images.ts b/packages/clipper/src/images.ts
index 49365773c..90121bd0d 100644
--- a/packages/clipper/src/images.ts
+++ b/packages/clipper/src/images.ts
@@ -22,8 +22,6 @@ import { isDataUrl } from "./utils";
async function inlineAllImages(root: HTMLElement, options: FetchOptions) {
const imageNodes = root.querySelectorAll("img");
- console.log("total images", imageNodes.length);
-
const promises: Promise<any>[] = [];
for (let i = 0; i < imageNodes.length; ++i) {
const image = imageNodes[i];
diff --git a/packages/clipper/src/index.ts b/packages/clipper/src/index.ts
index cea2f38c7..721bb7b0a 100644
--- a/packages/clipper/src/index.ts
+++ b/packages/clipper/src/index.ts
@@ -17,17 +17,10 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import purify from "dompurify";
import { Readability } from "@mozilla/readability";
import { injectCss } from "./utils";
import { app, h, text } from "hyperapp";
-import {
- getInlinedNode,
- toBlob,
- toJpeg,
- toPixelData,
- toPng
-} from "./domtoimage";
+import { getInlinedNode, toBlob, toJpeg, toPng } from "./domtoimage";
import { InlineOptions } from "./types";
import { FetchOptions } from "./fetch";
@@ -56,42 +49,6 @@ const inlineOptions: InlineOptions = {
stylesheets: true
};
-async function getPage(
- document: Document,
- styles: boolean,
- onlyVisible = false
-) {
- const body = await getInlinedNode(document.body, {
- raster: true,
- fetchOptions,
- inlineOptions,
- filter: (node) => {
- return !onlyVisible || isElementInViewport(node);
- }
- });
-
- if (!body) return {};
-
- const head = document.createElement("head");
- head.title = document.title;
-
- return {
- body,
- head
- };
-}
-
-function toDocument(head: HTMLElement, body: HTMLElement) {
- const newHTMLDocument = document.implementation.createHTMLDocument();
- newHTMLDocument.open();
- newHTMLDocument.write(head.outerHTML, body.outerHTML);
- newHTMLDocument.close();
-
- // newHTMLDocument.insertBefore(documentType, newHTMLDocument.childNodes[0]);
-
- return newHTMLDocument;
-}
-
async function clipPage(
document: Document,
withStyles: boolean,
@@ -103,31 +60,6 @@ async function clipPage(
return `<!doctype html>\n${result}`;
}
-const cleanup = () => {
- setTimeout(() => {
- document.querySelectorAll(`.${CLASSES.nodeSelected}`).forEach((node) => {
- if (node instanceof HTMLElement) {
- node.classList.remove(CLASSES.nodeSelected);
- }
- });
-
- document
- .querySelectorAll(`.${CLASSES.nodeSelectionContainer}`)
- .forEach((node) => node.remove());
-
- removeHoverListeners(document);
- removeClickHandlers(document);
- }, 0);
-};
-
-function clipNode(
- element: HTMLElement | null | undefined,
- keepStyles = true
-): string | null {
- if (!element) return null;
- return purifyBody(element.outerHTML, keepStyles).outerHTML;
-}
-
async function clipArticle(
doc: Document,
withStyles: boolean
@@ -150,34 +82,14 @@ async function clipArticle(
"valign",
"vspace"
];
+
const result = readability.parse();
- return `<html>${head?.outerHTML || ""}<body>${
+ return `<!DOCTYPE html><html>${head?.outerHTML || ""}<body>${
result?.content || ""
}</body></html>`;
}
-// async function clipSimplifiedArticle(doc: Document): Promise<string | null> {
-// const article = await clipArticle(doc);
-// if (!article) return null;
-// return purifyBody(article, false).outerHTML;
-// }
-
-function purifyBody(htmlString: string, keepStyles = true) {
- return purify.sanitize(htmlString, {
- RETURN_DOM: true,
- KEEP_CONTENT: false,
- ADD_TAGS: keepStyles ? ["use"] : [],
- ADD_ATTR: keepStyles ? ["style", "class", "id"] : [],
- FORBID_ATTR: !keepStyles ? ["style", "class", "id"] : [],
- FORBID_TAGS: !keepStyles ? ["style"] : [],
- ADD_DATA_URI_TAGS: ["style"],
- CUSTOM_ELEMENT_HANDLING: {
- attributeNameCheck: /notesnook/ // allow all attributes containing "baz"
- }
- }) as HTMLElement;
-}
-
async function clipScreenshot<
TOutputFormat extends "jpeg" | "png" | "raw",
TOutput extends TOutputFormat extends "jpeg"
@@ -202,7 +114,8 @@ async function clipScreenshot<
fonts: true,
images: true,
stylesheets: true
- }
+ },
+ styles: true
});
if (output === "jpeg" || output === "png")
@@ -279,6 +192,8 @@ function enterNodeSelectionMode(doc: Document) {
return new Promise((resolve, reject) => {
injectNodeSelectionControls(
async () => {
+ cleanup();
+
const selectedNodes = document.querySelectorAll(
`.${CLASSES.nodeSelected}`
);
@@ -294,7 +209,6 @@ function enterNodeSelectionMode(doc: Document) {
if (!inlined) continue;
div.appendChild(inlined);
}
- cleanup();
resolve(div?.outerHTML);
},
() => reject("Cancelled.")
@@ -306,7 +220,6 @@ export {
clipPage,
clipArticle,
cleanup,
- clipNode,
clipScreenshot,
enterNodeSelectionMode
};
@@ -315,7 +228,6 @@ const mod = {
clipPage,
clipArticle,
cleanup,
- clipNode,
clipScreenshot,
enterNodeSelectionMode
};
@@ -332,7 +244,7 @@ function injectStyles() {
border: 2px solid green;
cursor: pointer;
}
-
+
.${CLASSES.nodeSelectionContainer} {
position: fixed;
bottom: 0px;
@@ -352,9 +264,11 @@ function injectNodeSelectionControls(
document.body.appendChild(controlContainer);
}, 0);
- app({
- init: {},
- view: () =>
+ app<{ isClipping: boolean }>({
+ init: {
+ isClipping: false
+ },
+ view: ({ isClipping }) =>
h(
"div",
{
@@ -378,7 +292,11 @@ function injectNodeSelectionControls(
fontStyle: "italic"
}
},
- [text("Click on any element to select it.")]
+ [
+ isClipping
+ ? text("Clipping selected elements. Please wait...")
+ : text("Click on any element to select it.")
+ ]
),
h(
"div",
@@ -392,18 +310,29 @@ function injectNodeSelectionControls(
h(
"button",
{
- onclick: (_state) => onDone?.(),
- style: { marginRight: "5px" }
+ onclick: (state) => {
+ return [
+ { ...state, isClipping: true },
+ (dispatch) => {
+ onDone?.();
+ dispatch({ isClipping: false });
+ }
+ ];
+ },
+ style: { marginRight: "5px" },
+ disabled: isClipping
},
- [text("Done")]
+ [isClipping ? text("Clipping...") : text("Clip")]
),
h(
"button",
{
- onclick: (_state) => {
+ onclick: (state) => {
cleanup();
onCancel?.();
- }
+ return state;
+ },
+ disabled: isClipping
},
[text("Cancel")]
)
@@ -498,3 +427,59 @@ function getElementViewportInfo(el: HTMLElement) {
return result;
}
+
+function toDocument(head: HTMLElement, body: HTMLElement) {
+ const newHTMLDocument = document.implementation.createHTMLDocument();
+ newHTMLDocument.head.outerHTML = head.outerHTML;
+ newHTMLDocument.body.outerHTML = body.outerHTML;
+ return newHTMLDocument;
+}
+
+function cleanup() {
+ setTimeout(() => {
+ document.querySelectorAll(`.${CLASSES.nodeSelected}`).forEach((node) => {
+ if (node instanceof HTMLElement) {
+ node.classList.remove(CLASSES.nodeSelected);
+ }
+ });
+
+ document
+ .querySelectorAll(`.${CLASSES.nodeSelectionContainer}`)
+ .forEach((node) => node.remove());
+
+ removeHoverListeners(document);
+ removeClickHandlers(document);
+ }, 0);
+}
+
+async function getPage(
+ document: Document,
+ styles: boolean,
+ onlyVisible = false
+) {
+ const body = await getInlinedNode(document.body, {
+ raster: true,
+ fetchOptions,
+ inlineOptions: {
+ fonts: false,
+ images: true,
+ stylesheets: styles
+ },
+ styles,
+ filter: (node) => {
+ return !onlyVisible || isElementInViewport(node);
+ }
+ });
+
+ if (!body) return {};
+
+ const head = document.createElement("head");
+ const title = document.createElement("title");
+ title.innerHTML = document.title;
+ head.appendChild(title);
+
+ return {
+ body,
+ head
+ };
+}
diff --git a/packages/clipper/src/styles.ts b/packages/clipper/src/styles.ts
index d49d766af..9c8c5571e 100644
--- a/packages/clipper/src/styles.ts
+++ b/packages/clipper/src/styles.ts
@@ -114,7 +114,6 @@ async function downloadStylesheet(href: string, options: FetchOptions) {
if (!response.ok) return false;
style.innerText = await response.text();
style.setAttribute("href", href);
- console.log(href);
return style;
} catch (e) {
console.error("Failed to inline stylesheet", href);
diff --git a/packages/clipper/src/types.ts b/packages/clipper/src/types.ts
index 63bcb533c..3bf59128f 100644
--- a/packages/clipper/src/types.ts
+++ b/packages/clipper/src/types.ts
@@ -39,14 +39,13 @@ export type InlineOptions = {
export type Options = {
filter?: Filter;
- onCloned?: (document: HTMLElement) => void;
backgroundColor?: CSSStyleDeclaration["backgroundColor"];
width?: number;
height?: number;
- style?: CSSStyleDeclaration;
quality?: number;
raster?: boolean;
scale?: number;
fetchOptions?: FetchOptions;
inlineOptions?: InlineOptions;
+ styles?: boolean;
};