55// TODO: Make these tests more comprehensive. They should cover a wider range of sanitization cases.
66
77import { test , expect , type Page } from '@playwright/test' ;
8- import { cssToHtml } from '../src/index ' ;
8+ import { evaluate , innerHTML } from './utilities ' ;
99
1010const css = `
1111@import url('http://localhost:5173/import4.css');
@@ -19,7 +19,7 @@ test('Sanitization Off', async ({ page }) => {
1919 page . on ( 'console' , message => consoleMessages . push ( message . text ( ) ) ) ;
2020
2121 const conditions = async ( ) => {
22- const body = await page . evaluate ( async css => { document . body = await cssToHtml ( css , { imports : 'include' , sanitize : 'off' } ) ; return document . body . outerHTML ; } , css ) ;
22+ await evaluate ( page , css , { imports : 'include' , sanitize : 'off' } ) ;
2323
2424 // The body should have exactly two direct children.
2525 const bodyDirectChildren = page . locator ( 'body > *' ) ;
@@ -37,14 +37,11 @@ test('Sanitization Off', async ({ page }) => {
3737 await expect ( img ) . toHaveAttribute ( 'onerror' , / .{ 10 } / ) ;
3838 await expect ( img ) . toHaveClass ( 'xss' ) ;
3939
40- // There should be exactly one div element.
40+ // There should be exactly one div element,
41+ // and it should have specific text content.
4142 const div = page . locator ( 'div' ) ;
4243 await expect ( div ) . toHaveCount ( 1 ) ;
43-
44- // The div should have specific text content.
45- const divElement = await div . elementHandle ( ) ;
46- const divContent = await divElement ?. innerHTML ( ) ;
47- expect ( divContent ) . toBe ( 'A' ) ;
44+ await expect ( innerHTML ( div ) ) . resolves . toBe ( 'A' ) ;
4845
4946 // The div should have an `onclick` attribute.
5047 await expect ( div ) . toHaveAttribute ( 'onclick' , 'console.log(\'foo\')' ) ;
@@ -67,7 +64,7 @@ test('Sanitize Imports Only', async ({ page }) => {
6764 page . on ( 'console' , message => consoleMessages . push ( message . text ( ) ) ) ;
6865
6966 const conditions = async ( ) => {
70- const body = await page . evaluate ( async css => { document . body = await cssToHtml ( css , { imports : 'include' , sanitize : 'imports' } ) ; return document . body . outerHTML ; } , css ) ;
67+ await evaluate ( page , css , { imports : 'include' , sanitize : 'imports' } ) ;
7168
7269 // The body should have exactly two direct children.
7370 const bodyDirectChildren = page . locator ( 'body > *' ) ;
@@ -85,14 +82,11 @@ test('Sanitize Imports Only', async ({ page }) => {
8582 await expect ( img ) . not . toHaveAttribute ( 'onerror' ) ;
8683 await expect ( img ) . not . toHaveClass ( 'xss' ) ;
8784
88- // There should be exactly one div element.
85+ // There should be exactly one div element,
86+ // and it should have specific text content.
8987 const div = page . locator ( 'div' ) ;
9088 await expect ( div ) . toHaveCount ( 1 ) ;
91-
92- // The div should have specific text content.
93- const divElement = await div . elementHandle ( ) ;
94- const divContent = await divElement ?. innerHTML ( ) ;
95- expect ( divContent ) . toBe ( 'A' ) ;
89+ await expect ( innerHTML ( div ) ) . resolves . toBe ( 'A' ) ;
9690
9791 // The div should have an `onclick` attribute.
9892 await expect ( div ) . toHaveAttribute ( 'onclick' , 'console.log(\'foo\')' ) ;
@@ -127,14 +121,11 @@ async function expectEverythingToBeSanitized (page: Page): Promise<void> {
127121 await expect ( img ) . not . toHaveAttribute ( 'onerror' ) ;
128122 await expect ( img ) . not . toHaveClass ( 'xss' ) ;
129123
130- // There should be exactly one div element.
124+ // There should be exactly one div element,
125+ // and it should have specific text content.
131126 const div = page . locator ( 'div' ) ;
132127 await expect ( div ) . toHaveCount ( 1 ) ;
133-
134- // The div should have specific text content.
135- const divElement = await div . elementHandle ( ) ;
136- const divContent = await divElement ?. innerHTML ( ) ;
137- expect ( divContent ) . toBe ( 'A' ) ;
128+ await expect ( innerHTML ( div ) ) . resolves . toBe ( 'A' ) ;
138129
139130 // The div should not have an `onclick` attribute.
140131 await expect ( div ) . not . toHaveAttribute ( 'onclick' ) ;
@@ -145,7 +136,7 @@ test('Sanitize Everything', async ({ page }) => {
145136 page . on ( 'console' , message => consoleMessages . push ( message . text ( ) ) ) ;
146137
147138 const conditions = async ( ) => {
148- const body = await page . evaluate ( async css => { document . body = await cssToHtml ( css , { imports : 'include' , sanitize : 'all' } ) ; return document . body . outerHTML ; } , css ) ;
139+ await evaluate ( page , css , { imports : 'include' , sanitize : 'all' } ) ;
149140
150141 await expectEverythingToBeSanitized ( page ) ;
151142
@@ -167,7 +158,7 @@ test('Sanitize Everything By Default', async ({ page }) => {
167158 page . on ( 'console' , message => consoleMessages . push ( message . text ( ) ) ) ;
168159
169160 const conditions = async ( ) => {
170- const body = await page . evaluate ( async css => { document . body = await cssToHtml ( css , { imports : 'include' } ) ; return document . body . outerHTML ; } , css ) ;
161+ await evaluate ( page , css , { imports : 'include' } ) ;
171162
172163 await expectEverythingToBeSanitized ( page ) ;
173164
0 commit comments