Skip to content

Commit

Permalink
Allow storing comment's content as the $root attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
psmyrek committed Aug 2, 2021
1 parent 9790e3d commit 5f4a190
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 5f4a190

Please sign in to comment.