Skip to content

Commit

Permalink
Merge pull request #966 from ckeditor/css-data-image-new-line-fix
Browse files Browse the repository at this point in the history
Fix: Should not corrupt the CSS code during splitting CSS into editor and content files. See ckeditor/ckeditor5#16670.
  • Loading branch information
pszczesniak authored Jul 4, 2024
2 parents e0cccdf + 130798c commit e196d78
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/ckeditor5-dev-build-tools/src/plugins/splitCss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { parse, type Rule, type Declaration, type Stylesheet } from 'css';
import type { Plugin, OutputBundle, NormalizedOutputOptions, EmittedAsset } from 'rollup';
import type { Processor } from 'postcss';
import cssnano from 'cssnano';
import { removeNewline } from '../utils';

export interface RollupSplitCssOptions {

Expand Down Expand Up @@ -269,5 +270,13 @@ async function unifyFileContentOutput( content: string = '', minimize: boolean )
* Wraps `declarations` list into passed `selector`;
*/
function wrapDefinitionsIntoSelector( selector: string, definitions: string ): string {
// When definition contains `data:image` it should be in one line following the specification.
// Currently used tool responsible for parsing definitions tries to split each definition into new line.
// When `data:image` contains `SVG` with style attribute which contains CSS definitions it splits it into new lines,
// which breaks the CSS.
if ( definitions.includes( 'data:image' ) ) {
definitions = removeNewline( definitions );
}

return `${ selector } {\n${ definitions }}\n`;
}
8 changes: 8 additions & 0 deletions packages/ckeditor5-dev-build-tools/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ export function removeWhitespace( text: string ): string {
return text.replaceAll( /\n\s+/gm, '\n' );
}

/**
* Returns string without newline.
*/

export function removeNewline( text: string ): string {
return text.replaceAll( /\r?\n|\r/g, '' );
}

/**
* Returns dependency resolved relative to the current working directory. This is needed to ensure
* that the dependency of this package itself (which may be in a different version) is not used.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md.
*/

import './body.css';

export const test = 123;
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,22 @@ test( 'should minify the content output', async () => {
verifyDividedStyleSheet( output, 'styles-editor.css', expectedResult );
verifyDividedStyleSheet( output, 'styles-content.css', '' );
} );

test( 'should correctly parse the `data:image` style definition (should do not add new lines)', async () => {
const output = await generateBundle(
'./fixtures/import-data-image/input.ts',
{ baseFileName: 'styles' }
);

const expectedResult = removeWhitespace(
'.ck {\n' +
'background-image: url("data:image/svg+xml;utf8,<svg width=\'120\' height=\'12\' xmlns=\'http://www.w3.org/2000/svg\' >' +
'<text style=\'paint-order:stroke fill; clip-path: inset(-3px);transform: translate(-2px, 0)\' stroke=\'%23EAEAEA\' ' +
'stroke-width=\'13\' dominant-baseline=\'middle\' fill=\'black\' x=\'100%\' text-anchor=\'end\' y=\'7\' font-size=\'9px\' ' +
'font-family=\'Consolas, %22Lucida Console%22, %22Lucida Sans Typewriter%22, %22DejaVu Sans Mono%22, ' +
'%22Bitstream Vera Sans Mono%22, %22Liberation Mono%22, Monaco, %22Courier New%22, Courier, monospace\'>' +
'FIGCAPTION</text></svg>");background-position: calc(100% - 1px) 1px;}\n' );

verifyDividedStyleSheet( output, 'styles-editor.css', expectedResult );
verifyDividedStyleSheet( output, 'styles-content.css', '' );
} );
8 changes: 7 additions & 1 deletion packages/ckeditor5-dev-build-tools/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { test, expect } from 'vitest';
import upath from 'upath';
import { getCwdPath, camelize, camelizeObjectKeys } from '../src/utils.js';
import { getCwdPath, camelize, camelizeObjectKeys, removeNewline } from '../src/utils.js';

test( 'getPath()', () => {
expect( getCwdPath( 'dist', 'index.js' ) ).toBe( upath.join( process.cwd(), '/dist/index.js' ) );
Expand All @@ -15,6 +15,12 @@ test( 'camelize()', () => {
expect( camelize( 'this-is-a-test' ) ).toBe( 'thisIsATest' );
} );

test( 'removeNewline()', () => {
const newLines = `line1;
line2;`;
expect( removeNewline( newLines ) ).toBe( 'line1;line2;' );
} );

test( 'camelizeObjectKeys()', () => {
expect( camelizeObjectKeys( {
'test-one': 1,
Expand Down

0 comments on commit e196d78

Please sign in to comment.