Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style attribute conversion should work with attributeToAttribute() #15688

Merged
merged 5 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions packages/ckeditor5-engine/src/conversion/downcasthelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import type ViewElement from '../view/element.js';
import type ViewNode from '../view/node.js';
import type ViewPosition from '../view/position.js';
import type ViewRange from '../view/range.js';
import StylesMap from '../view/stylesmap.js';
import type {
default as Mapper,
MapperModelToViewPositionEvent
Expand Down Expand Up @@ -1664,16 +1665,26 @@ function changeAttribute( attributeCreator: AttributeCreatorFunction ) {
// First remove the old attribute if there was one.
if ( data.attributeOldValue !== null && oldAttribute ) {
if ( oldAttribute.key == 'class' ) {
const classes = toArray( oldAttribute.value );
const classes = typeof oldAttribute.value == 'string' ? oldAttribute.value.split( /\s+/ ) : oldAttribute.value;

for ( const className of classes ) {
viewWriter.removeClass( className, viewElement );
}
} else if ( oldAttribute.key == 'style' ) {
const keys = Object.keys( oldAttribute.value );
if ( typeof oldAttribute.value == 'string' ) {
const styles = new StylesMap( viewWriter.document.stylesProcessor );

for ( const key of keys ) {
viewWriter.removeStyle( key, viewElement );
styles.setTo( oldAttribute.value );

for ( const [ key ] of styles.getStylesEntries() ) {
viewWriter.removeStyle( key, viewElement );
}
} else {
const keys = Object.keys( oldAttribute.value );

for ( const key of keys ) {
viewWriter.removeStyle( key, viewElement );
}
}
} else {
viewWriter.removeAttribute( oldAttribute.key, viewElement );
Expand All @@ -1683,16 +1694,26 @@ function changeAttribute( attributeCreator: AttributeCreatorFunction ) {
// Then set the new attribute.
if ( data.attributeNewValue !== null && newAttribute ) {
if ( newAttribute.key == 'class' ) {
const classes = toArray( newAttribute.value );
const classes = typeof newAttribute.value == 'string' ? newAttribute.value.split( /\s+/ ) : newAttribute.value;

for ( const className of classes ) {
viewWriter.addClass( className, viewElement );
}
} else if ( newAttribute.key == 'style' ) {
const keys = Object.keys( newAttribute.value );
if ( typeof newAttribute.value == 'string' ) {
const styles = new StylesMap( viewWriter.document.stylesProcessor );

for ( const key of keys ) {
viewWriter.setStyle( key, ( newAttribute.value as Record<string, string> )[ key ], viewElement );
styles.setTo( newAttribute.value );

for ( const [ key, value ] of styles.getStylesEntries() ) {
viewWriter.setStyle( key, value, viewElement );
}
} else {
const keys = Object.keys( newAttribute.value );

for ( const key of keys ) {
viewWriter.setStyle( key, ( newAttribute.value as Record<string, string> )[ key ], viewElement );
}
}
} else {
viewWriter.setAttribute( newAttribute.key, newAttribute.value as string, viewElement );
Expand Down
5 changes: 2 additions & 3 deletions packages/ckeditor5-engine/src/conversion/upcasthelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -977,17 +977,16 @@ function normalizeViewAttributeKeyValueConfig( config: any ) {
}

const key: string = config.view.key;
const value = typeof config.view.value == 'undefined' ? /[\s\S]*/ : config.view.value;
let normalized: MatcherPattern;

if ( key == 'class' || key == 'style' ) {
const keyName = key == 'class' ? 'classes' : 'styles';

normalized = {
[ keyName ]: config.view.value
[ keyName ]: value
};
} else {
const value = typeof config.view.value == 'undefined' ? /[\s\S]*/ : config.view.value;

normalized = {
attributes: {
[ key ]: value
Expand Down
6 changes: 3 additions & 3 deletions packages/ckeditor5-engine/src/view/stylesmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export default class StylesMap {
return '';
}

return this._getStylesEntries()
return this.getStylesEntries()
.map( arr => arr.join( ':' ) )
.sort()
.join( ';' ) + ';';
Expand Down Expand Up @@ -411,7 +411,7 @@ export default class StylesMap {
return this._styleProcessor.getStyleNames( this._styles );
}

const entries = this._getStylesEntries();
const entries = this.getStylesEntries();

return entries.map( ( [ key ] ) => key );
}
Expand All @@ -426,7 +426,7 @@ export default class StylesMap {
/**
* Returns normalized styles entries for further processing.
*/
private _getStylesEntries(): Array<PropertyDescriptor> {
public getStylesEntries(): Array<PropertyDescriptor> {
const parsed: Array<PropertyDescriptor> = [];

const keys = Object.keys( this._styles );
Expand Down
56 changes: 56 additions & 0 deletions packages/ckeditor5-engine/tests/conversion/downcasthelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3298,6 +3298,62 @@ describe( 'DowncastHelpers', () => {
expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
} );

it( 'config.view and config.model as strings (class attribute)', () => {
const consoleWarnStub = testUtils.sinon.stub( console, 'warn' );

downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
downcastHelpers.attributeToAttribute( { model: 'test', view: 'class' } );

model.change( writer => {
writer.insertElement( 'paragraph', { test: 'foo bar' }, modelRoot, 0 );
writer.insertElement( 'paragraph', { test: 'abc def' }, modelRoot, 1 );
} );

expectResult(
'<p class="bar foo"></p>' +
'<p class="abc def"></p>'
);
expect( consoleWarnStub.callCount ).to.equal( 0 );

model.change( writer => {
writer.setAttribute( 'test', 'bar', modelRoot.getChild( 0 ) );
writer.removeAttribute( 'test', modelRoot.getChild( 1 ) );
} );

expectResult(
'<p class="bar"></p>' +
'<p></p>'
);
} );

it( 'config.view and config.model as strings (style attribute)', () => {
const consoleWarnStub = testUtils.sinon.stub( console, 'warn' );

downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
downcastHelpers.attributeToAttribute( { model: 'test', view: 'style' } );

model.change( writer => {
writer.insertElement( 'paragraph', { test: 'background: red; padding: 4px 10px;' }, modelRoot, 0 );
writer.insertElement( 'paragraph', { test: 'color: blue; background: yellow;' }, modelRoot, 1 );
} );

expectResult(
'<p style="background:red;padding:4px 10px"></p>' +
'<p style="background:yellow;color:blue"></p>'
);
expect( consoleWarnStub.callCount ).to.equal( 0 );

model.change( writer => {
writer.setAttribute( 'test', 'background: pink;', modelRoot.getChild( 0 ) );
writer.removeAttribute( 'test', modelRoot.getChild( 1 ) );
} );

expectResult(
'<p style="background:pink"></p>' +
'<p></p>'
);
} );

it( 'should be possible to override setAttribute', () => {
downcastHelpers.attributeToAttribute( {
model: 'class',
Expand Down
58 changes: 58 additions & 0 deletions packages/ckeditor5-engine/tests/conversion/upcasthelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,64 @@ describe( 'UpcastHelpers', () => {
);
} );

it( 'config.view does not have value set for style key', () => {
schema.extend( 'imageBlock', {
allowAttributes: [ 'styled' ]
} );

upcastHelpers.attributeToAttribute( {
view: 'style',
model: 'styled'
} );

// Ensure that proper consumables are consumed.
upcastDispatcher.on( 'element', ( evt, data, { consumable } ) => {
expect( consumable.test( data.viewItem, { styles: [ 'border', 'padding' ] } ) ).to.be.true;
expect( consumable.test( data.viewItem, { styles: [ 'border' ] } ) ).to.be.true;
expect( consumable.test( data.viewItem, { styles: [ 'padding' ] } ) ).to.be.true;
}, { priority: 'highest' } );

upcastDispatcher.on( 'element', ( evt, data, { consumable } ) => {
expect( consumable.test( data.viewItem, { styles: [ 'border', 'padding' ] } ) ).to.be.false;
expect( consumable.test( data.viewItem, { styles: [ 'border' ] } ) ).to.be.false;
expect( consumable.test( data.viewItem, { styles: [ 'padding' ] } ) ).to.be.false;
}, { priority: 'lowest' } );

expectResult(
new ViewAttributeElement( viewDocument, 'img', { 'style': 'border: 2px solid red; padding: 6px 3px;' } ),
'<imageBlock styled="border:2px solid red;padding:6px 3px;"></imageBlock>'
);
} );

it( 'config.view does not have value set for class key', () => {
schema.extend( 'imageBlock', {
allowAttributes: [ 'classNames' ]
} );

upcastHelpers.attributeToAttribute( {
view: 'class',
model: 'classNames'
} );

// Ensure that proper consumables are consumed.
upcastDispatcher.on( 'element', ( evt, data, { consumable } ) => {
expect( consumable.test( data.viewItem, { classes: [ 'foo', 'bar' ] } ) ).to.be.true;
expect( consumable.test( data.viewItem, { classes: [ 'foo' ] } ) ).to.be.true;
expect( consumable.test( data.viewItem, { classes: [ 'bar' ] } ) ).to.be.true;
}, { priority: 'highest' } );

upcastDispatcher.on( 'element', ( evt, data, { consumable } ) => {
expect( consumable.test( data.viewItem, { classes: [ 'foo', 'bar' ] } ) ).to.be.false;
expect( consumable.test( data.viewItem, { classes: [ 'foo' ] } ) ).to.be.false;
expect( consumable.test( data.viewItem, { classes: [ 'bar' ] } ) ).to.be.false;
}, { priority: 'lowest' } );

expectResult(
new ViewAttributeElement( viewDocument, 'img', { 'class': 'foo bar' } ),
'<imageBlock classNames="foo bar"></imageBlock>'
);
} );

it( 'model attribute value is a string', () => {
schema.extend( 'imageBlock', {
allowAttributes: [ 'styled' ]
Expand Down