Skip to content

Commit 6ef279c

Browse files
authored
Fix upcasting of background-color in tables. (#18808)
* Restore `shouldUpcast` method due to incorrect upcast of background styling in tables. * Invert condition * Add tests. * Add symmetry test.
1 parent 1aadc5d commit 6ef279c

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

packages/ckeditor5-table/src/converters/tableproperties.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function upcastStyleToAttribute(
3232
viewElement: string | RegExp;
3333
defaultValue: string;
3434
reduceBoxSides?: boolean;
35+
shouldUpcast?: ( viewElement: ViewElement ) => boolean;
3536
}
3637
): void {
3738
const {
@@ -41,6 +42,7 @@ export function upcastStyleToAttribute(
4142
attributeType,
4243
viewElement,
4344
defaultValue,
45+
shouldUpcast = () => true,
4446
reduceBoxSides = false
4547
} = options;
4648

@@ -55,10 +57,7 @@ export function upcastStyleToAttribute(
5557
key: modelAttribute,
5658
value: ( viewElement: ViewElement, conversionApi: UpcastConversionApi, data: UpcastConversionData<ViewElement> ) => {
5759
// Ignore table elements inside figures and figures without the table class.
58-
if (
59-
viewElement.name == 'table' && viewElement.parent!.name == 'figure' ||
60-
viewElement.name == 'figure' && !viewElement.hasClass( 'table' )
61-
) {
60+
if ( !shouldUpcast( viewElement ) ) {
6261
return;
6362
}
6463

packages/ckeditor5-table/src/tableproperties/tablepropertiesediting.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,14 @@ function enableTableToFigureProperty(
363363

364364
schema.setAttributeProperties( modelAttribute, { isFormatting: true } );
365365

366-
upcastStyleToAttribute( conversion, { viewElement: /^(table|figure)$/, ...options } );
366+
upcastStyleToAttribute( conversion, {
367+
viewElement: /^(table|figure)$/,
368+
shouldUpcast: ( viewElement: ViewElement ) => !(
369+
viewElement.name == 'table' && viewElement.parent!.name == 'figure' ||
370+
viewElement.name == 'figure' && !viewElement.hasClass( 'table' )
371+
),
372+
...options
373+
} );
374+
367375
downcastAttributeToStyle( conversion, { modelElement: 'table', ...options } );
368376
}

packages/ckeditor5-table/tests/tableproperties/tablepropertiesediting.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,24 @@ describe( 'table properties', () => {
899899
expect( table.getAttribute( 'tableBackgroundColor' ) ).to.equal( '#f00' );
900900
} );
901901

902+
it( 'should upcast background-color from table within <figure> with `.table` class', () => {
903+
editor.setData(
904+
'<figure class="table"><table style="background-color:#f00"><tr><td>foo</td></tr></table></figure>'
905+
);
906+
const table = model.document.getRoot().getNodeByPath( [ 0 ] );
907+
908+
expect( table.getAttribute( 'tableBackgroundColor' ) ).to.equal( '#f00' );
909+
} );
910+
911+
it( 'should upcast background-color from table without <figure> without `.table` class', () => {
912+
editor.setData(
913+
'<figure><table style="background-color:#f00"><tr><td>foo</td></tr></table></figure>'
914+
);
915+
const table = model.document.getRoot().getNodeByPath( [ 0 ] );
916+
917+
expect( table.getAttribute( 'tableBackgroundColor' ) ).to.equal( '#f00' );
918+
} );
919+
902920
it( 'should upcast from background shorthand', () => {
903921
editor.setData( '<table style="background:#f00 center center"><tr><td>foo</td></tr></table>' );
904922
const table = model.document.getRoot().getNodeByPath( [ 0 ] );
@@ -1024,6 +1042,27 @@ describe( 'table properties', () => {
10241042
assertTableStyle( editor, 'background-color:#ba7;' );
10251043
} );
10261044
} );
1045+
1046+
it( 'data symmetry - should upcast downcasted tableBackgroundColor', () => {
1047+
editor.setData( '<table style="background-color:#f00"><tr><td>foo</td></tr></table>' );
1048+
const table = model.document.getRoot().getNodeByPath( [ 0 ] );
1049+
1050+
expect( table.getAttribute( 'tableBackgroundColor' ) ).to.equal( '#f00' );
1051+
1052+
model.change( writer => writer.setAttribute( 'tableBackgroundColor', '#ba7', table ) );
1053+
1054+
expect( editor.getData() ).to.equalMarkup(
1055+
'<figure class="table">' +
1056+
'<table style="background-color:#ba7;">' +
1057+
'<tbody>' +
1058+
'<tr>' +
1059+
'<td>foo</td>' +
1060+
'</tr>' +
1061+
'</tbody>' +
1062+
'</table>' +
1063+
'</figure>'
1064+
);
1065+
} );
10271066
} );
10281067

10291068
describe( 'tableWidth', () => {

0 commit comments

Comments
 (0)