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

Allow storing comment's content as the $root attribute #10286

Merged
merged 1 commit into from
Aug 2, 2021
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
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