/** * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options */ import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import { ClassicTestEditor } from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor.js'; import { GeneralHtmlSupport } from '../src/index.js'; import { RemoveFormat } from '@ckeditor/ckeditor5-remove-format'; import { SelectAll } from '@ckeditor/ckeditor5-select-all'; import { Paragraph } from '@ckeditor/ckeditor5-paragraph'; import { Heading } from '@ckeditor/ckeditor5-heading'; import { Image, ImageCaption } from '@ckeditor/ckeditor5-image'; import { Table, TableCaption, TableCellProperties, TableColumnResize, TableProperties } from '@ckeditor/ckeditor5-table'; import { PageBreak } from '@ckeditor/ckeditor5-page-break'; import { CodeBlock } from '@ckeditor/ckeditor5-code-block'; import { List, ListProperties } from '@ckeditor/ckeditor5-list'; import { stubUid } from '@ckeditor/ckeditor5-list/tests/list/_utils/uid.js'; import { BlockQuote } from '@ckeditor/ckeditor5-block-quote'; import { _getModelData, _getViewData } from '@ckeditor/ckeditor5-engine'; describe( 'GeneralHtmlSupport', () => { let editor, element, dataSchema, generalHtmlSupport; beforeEach( async () => { element = document.createElement( 'div' ); document.body.appendChild( element ); editor = await ClassicTestEditor.create( element, { plugins: [ GeneralHtmlSupport ] } ); dataSchema = editor.plugins.get( 'DataSchema' ); generalHtmlSupport = editor.plugins.get( 'GeneralHtmlSupport' ); } ); afterEach( async () => { element.remove(); await editor.destroy(); } ); it( 'should have `isOfficialPlugin` static flag set to `true`', () => { expect( GeneralHtmlSupport.isOfficialPlugin ).toBe( true ); } ); it( 'should have `isPremiumPlugin` static flag set to `false`', () => { expect( GeneralHtmlSupport.isPremiumPlugin ).toBe( false ); } ); describe( 'getGhsAttributeNameForElement()', () => { beforeEach( () => { dataSchema.registerBlockElement( { model: 'def', view: 'def1' } ); dataSchema.registerBlockElement( { model: 'def', view: 'def2' } ); dataSchema.registerInlineElement( { model: 'htmlDef', view: 'def3' } ); dataSchema.registerInlineElement( { model: 'htmlDef', view: 'def4' } ); dataSchema.registerInlineElement( { model: 'htmlObj', view: 'def5', isObject: true } ); } ); it( 'should return "htmlXAttributes" for block elements', () => { expect( generalHtmlSupport.getGhsAttributeNameForElement( 'def1' ) ).toBe( 'htmlDef1Attributes' ); expect( generalHtmlSupport.getGhsAttributeNameForElement( 'def2' ) ).toBe( 'htmlDef2Attributes' ); } ); it( 'should return "htmlXAttributes" for inline object elements', () => { expect( generalHtmlSupport.getGhsAttributeNameForElement( 'def5' ) ).toBe( 'htmlDef5Attributes' ); } ); it( 'should return model attribute name for inline elements with multiple view representations', () => { expect( generalHtmlSupport.getGhsAttributeNameForElement( 'def3' ) ).toBe( 'htmlDef' ); expect( generalHtmlSupport.getGhsAttributeNameForElement( 'def4' ) ).toBe( 'htmlDef' ); } ); it( 'should return model attribute name for block elements with multiple view representations', () => { expect( generalHtmlSupport.getGhsAttributeNameForElement( 'td' ) ).toBe( 'htmlTdAttributes' ); expect( generalHtmlSupport.getGhsAttributeNameForElement( 'th' ) ).toBe( 'htmlThAttributes' ); } ); it( 'should return model attribute name for list elements with multiple view representations', () => { expect( generalHtmlSupport.getGhsAttributeNameForElement( 'ul' ) ).toBe( 'htmlUlAttributes' ); expect( generalHtmlSupport.getGhsAttributeNameForElement( 'ol' ) ).toBe( 'htmlOlAttributes' ); expect( generalHtmlSupport.getGhsAttributeNameForElement( 'li' ) ).toBe( 'htmlLiAttributes' ); } ); it( 'should return model attribute name for block elements', () => { expect( generalHtmlSupport.getGhsAttributeNameForElement( 'div' ) ).toBe( 'htmlDivAttributes' ); expect( generalHtmlSupport.getGhsAttributeNameForElement( 'p' ) ).toBe( 'htmlPAttributes' ); } ); it( 'should return model attribute name for inline elements', () => { expect( generalHtmlSupport.getGhsAttributeNameForElement( 'a' ) ).toBe( 'htmlA' ); expect( generalHtmlSupport.getGhsAttributeNameForElement( 'strong' ) ).toBe( 'htmlStrong' ); } ); } ); } ); describe( 'GeneralHtmlSupport - RemoveFormatCommand integration', () => { let editor, element, model, editingView; beforeEach( async () => { element = document.createElement( 'div' ); document.body.appendChild( element ); editor = await ClassicTestEditor.create( element, { plugins: [ GeneralHtmlSupport, RemoveFormat, SelectAll, Paragraph, Heading, Image, ImageCaption, Table, TableProperties, TableCellProperties, TableColumnResize, TableCaption, PageBreak, CodeBlock, BlockQuote, List, ListProperties ], htmlSupport: { allow: [ { name: /^.*$/, styles: true, attributes: true, classes: true } ] } } ); model = editor.model; editingView = editor.editing.view; } ); afterEach( async () => { element.remove(); await editor.destroy(); } ); it( 'should remove styles and classes from paragraph', () => { editor.setData( '
Test
' ); expect( editor.getData(), 'initial data' ).toBe( 'Test
' ); editor.execute( 'selectAll' ); editor.execute( 'removeFormat' ); expect( editor.getData(), 'data pipeline' ).toBe( 'Test
' ); expect( _getViewData( editingView, { withoutSelection: true } ), 'editing view' ).toBe( 'Test
' ); } ); it( 'should remove styles and classes from paragraph (no attributes are set)', () => { editor.setData( 'Test
' ); expect( editor.getData(), 'initial data' ).toBe( 'Test
' ); editor.execute( 'selectAll' ); editor.execute( 'removeFormat' ); expect( editor.getData(), 'data pipeline' ).toBe( 'Test
' ); expect( _getViewData( editingView, { withoutSelection: true } ), 'editing view' ).toBe( 'Test
' ); } ); it( 'should remove styles and classes from div', () => { editor.setData( '
' +
'
' +
'
' +
'
' +
'| foo | ' + 'bar | ' + '
| foo | ' + 'bar | ' + '
| foo | ' + 'bar | ' + '
foo'
);
expect( editor.getData(), 'initial data' ).toBe(
'foo'
);
editor.execute( 'selectAll' );
editor.execute( 'removeFormat' );
expect( editor.getData(), 'data pipeline' ).toBe(
'foo'
);
expect( _getViewData( editingView, { withoutSelection: true } ), 'editing view' ).toBe(
'' +
'foo' +
''
);
} );
it( 'should remove styles and classes from block-quote', () => {
editor.setData(
'a
' + '' + 'foo
bar
b
' ); expect( editor.getData(), 'initial data' ).toBe( 'a
' + '' + 'foo
bar
b
' ); editor.execute( 'selectAll' ); editor.execute( 'removeFormat' ); expect( editor.getData(), 'data pipeline' ).toBe( 'a
' + '' + 'foo
bar
b
' ); expect( _getViewData( editingView, { withoutSelection: true } ), 'editing view' ).toBe( 'a
' + '' + 'foo
bar
b
' ); } ); it( 'should remove styles and classes from list', () => { stubUid(); editor.setData( 'a
' + 'foo
' + 'b
' ); expect( editor.getData(), 'initial data' ).toBe( 'a
' + 'foo
' + 'b
' ); editor.execute( 'selectAll' ); editor.execute( 'removeFormat' ); expect( editor.getData(), 'data pipeline' ).toBe( 'a
' + 'foo
' + 'b
' ); expect( _getViewData( editingView, { withoutSelection: true } ), 'editing view' ).toBe( 'a
' + 'foo
' + 'b
' ); } ); } );