Skip to content

Commit

Permalink
Merge pull request #10286 from ckeditor/ck/10274
Browse files Browse the repository at this point in the history
Fix (html-support): Extended schema definition for $root to allow storing comment's content as the $root attribute. Closes #10274.
  • Loading branch information
maxbarnas authored Aug 2, 2021
2 parents 22ffc12 + 5f4a190 commit 35b08a9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/ckeditor5-html-support/src/htmlcomment.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ export default class HtmlComment extends Plugin {
init() {
const editor = this.editor;

// Allow storing comment's content as the $root attribute with the name `$comment:<unique id>`.
editor.model.schema.addAttributeCheck( ( context, attributeName ) => {
if ( context.endsWith( '$root' ) && attributeName.startsWith( '$comment' ) ) {
return true;
}
} );

// Convert the `$comment` view element to `$comment:<unique id>` marker and store its content (the comment itself) as a $root
// attribute. The comment content is needed in the `dataDowncast` pipeline to re-create the comment node.
editor.conversion.for( 'upcast' ).elementToMarker( {
Expand Down
12 changes: 12 additions & 0 deletions packages/ckeditor5-html-support/tests/htmlcomment.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ describe( 'HtmlComment', () => {
expect( editor.plugins.get( 'HtmlComment' ) ).to.be.instanceOf( HtmlComment );
} );

describe( 'schema', () => {
it( 'should allow root attributes containing comment\'s content in the schema', () => {
editor.setData( '<p><!-- comment 1 -->Foo<!-- comment 2 --></p>' );

model.change( writer => {
model.schema.removeDisallowedAttributes( [ root ], writer );

expect( editor.getData() ).to.equal( '<p><!-- comment 1 -->Foo<!-- comment 2 --></p>' );
} );
} );
} );

describe( 'upcast conversion', () => {
it( 'should convert each comment node to a collapsed marker', () => {
editor.setData( '<p><!-- comment 1 -->Foo<!-- comment 2 --></p>' );
Expand Down

0 comments on commit 35b08a9

Please sign in to comment.