|
| 1 | +/** |
| 2 | + * @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved. |
| 3 | + * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options |
| 4 | + */ |
| 5 | + |
| 6 | +/* global document, window, console, Response */ |
| 7 | + |
| 8 | +import collectStylesheets from '../src/collectstylesheets.js'; |
| 9 | + |
| 10 | +describe( 'collectStylesheets', () => { |
| 11 | + let styleSheetsMock; |
| 12 | + |
| 13 | + beforeEach( () => { |
| 14 | + styleSheetsMock = [ |
| 15 | + { |
| 16 | + ownerNode: { |
| 17 | + hasAttribute: name => name === 'data-cke' |
| 18 | + }, |
| 19 | + cssRules: [ |
| 20 | + { cssText: ':root { --variable1: white; }' }, |
| 21 | + { cssText: '.ck-content { color: black }' }, |
| 22 | + { cssText: '.some-styles { color: red }' } |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + ownerNode: { |
| 27 | + hasAttribute: name => name === 'data-cke' |
| 28 | + }, |
| 29 | + cssRules: [ |
| 30 | + { cssText: ':root { --variable2: blue; }' }, |
| 31 | + { cssText: '.ck-content { background: white }' } |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + ownerNode: { |
| 36 | + hasAttribute: () => false |
| 37 | + }, |
| 38 | + cssRules: [ |
| 39 | + { cssText: 'h2 { color: black }' } |
| 40 | + ] |
| 41 | + } |
| 42 | + ]; |
| 43 | + |
| 44 | + sinon.stub( document, 'styleSheets' ).get( () => styleSheetsMock ); |
| 45 | + } ); |
| 46 | + |
| 47 | + afterEach( () => { |
| 48 | + sinon.restore(); |
| 49 | + } ); |
| 50 | + |
| 51 | + it( 'should not return any styles if no paths to stylesheets provided', async () => { |
| 52 | + expect( await collectStylesheets( undefined ) ).to.equal( '' ); |
| 53 | + } ); |
| 54 | + |
| 55 | + it( 'should log into the console when ".ck-content" styles are missing', async () => { |
| 56 | + styleSheetsMock = [ { |
| 57 | + ownerNode: { |
| 58 | + hasAttribute: name => name === 'data-cke' |
| 59 | + }, |
| 60 | + cssRules: [ |
| 61 | + { cssText: ':root { --variable: white; }' } |
| 62 | + ] |
| 63 | + } ]; |
| 64 | + |
| 65 | + const consoleSpy = sinon.stub( console, 'warn' ); |
| 66 | + |
| 67 | + await collectStylesheets( [ 'EDITOR_STYLES' ] ); |
| 68 | + |
| 69 | + sinon.assert.calledOnce( consoleSpy ); |
| 70 | + } ); |
| 71 | + |
| 72 | + it( 'should get ".ck-content" styles when "EDITOR_STYLES" token is provided', async () => { |
| 73 | + const consoleSpy = sinon.stub( console, 'warn' ); |
| 74 | + |
| 75 | + const styles = await collectStylesheets( [ './foo.css', 'EDITOR_STYLES' ] ); |
| 76 | + |
| 77 | + sinon.assert.notCalled( consoleSpy ); |
| 78 | + |
| 79 | + expect( styles.length > 0 ).to.be.true; |
| 80 | + expect( styles.indexOf( '.ck-content' ) !== -1 ).to.be.true; |
| 81 | + } ); |
| 82 | + |
| 83 | + it( 'should get styles from multiple stylesheets with data-cke attribute', async () => { |
| 84 | + const styles = await collectStylesheets( [ 'EDITOR_STYLES' ] ); |
| 85 | + |
| 86 | + expect( styles ).to.include( ':root { --variable1: white; }' ); |
| 87 | + expect( styles ).to.include( ':root { --variable2: blue; }' ); |
| 88 | + expect( styles ).to.include( '.ck-content { color: black }' ); |
| 89 | + expect( styles ).to.include( '.ck-content { background: white }' ); |
| 90 | + } ); |
| 91 | + |
| 92 | + it( 'should collect all :root styles from stylesheets with data-cke attribute', async () => { |
| 93 | + const styles = await collectStylesheets( [ 'EDITOR_STYLES' ] ); |
| 94 | + |
| 95 | + expect( styles ).to.include( '--variable1: white' ); |
| 96 | + expect( styles ).to.include( '--variable2: blue' ); |
| 97 | + } ); |
| 98 | + |
| 99 | + it( 'should fetch stylesheets from the provided paths and return concat result', async () => { |
| 100 | + sinon |
| 101 | + .stub( window, 'fetch' ) |
| 102 | + .onFirstCall().resolves( new Response( '.foo { color: green; }' ) ) |
| 103 | + .onSecondCall().resolves( new Response( '.bar { color: red; }' ) ); |
| 104 | + |
| 105 | + const styles = await collectStylesheets( [ './foo.css', './bar.css' ] ); |
| 106 | + |
| 107 | + expect( styles ).to.equal( '.foo { color: green; } .bar { color: red; }' ); |
| 108 | + } ); |
| 109 | +} ); |
0 commit comments