From 6fa8ee1284d5bd3809b8b002cfa45a4afd848e37 Mon Sep 17 00:00:00 2001 From: Arkadiusz Filipczak Date: Wed, 5 Jan 2022 16:57:26 +0100 Subject: [PATCH 1/3] Add support for ` ); + + expect( getModelData( model, { withoutSelection: true } ) ).to.equal( + `Foo` + ); + + expect( editor.getData() ).to.equal( `

Foo

` ); + } ); + + it( 'should allow attributes', () => { + dataFilter.allowAttributes( { name: 'script', attributes: [ 'type', 'nonce' ] } ); + + editor.setData( `

Foo

` ); + + expect( getModelDataWithAttributes( model, { withoutSelection: true } ) ).to.deep.equal( { + data: `Foo`, + attributes: { + 1: { + attributes: { + nonce: 'qwerty', + type: 'c++' + } + }, + 2: CODE_CPP + } + } ); + + expect( editor.getData() ).to.equal( `

Foo

` ); + } ); + + it( 'should disallow attributes', () => { + dataFilter.allowAttributes( { name: 'script', attributes: true } ); + dataFilter.disallowAttributes( { name: 'script', attributes: 'nonce' } ); + + editor.setData( `

Foo

` ); + + expect( getModelDataWithAttributes( model, { withoutSelection: true } ) ).to.deep.equal( { + data: `Foo`, + attributes: { + 1: { + attributes: { + type: 'c++' + } + }, + 2: CODE_CPP + } + } ); + + expect( editor.getData() ).to.equal( `

Foo

` ); + } ); + + describe( 'element position', () => { + const testCases = [ { + name: 'paragraph', + data: `

FooBar

`, + model: + '' + + `FooBar` + + '' + }, { + name: 'section', + data: `

Foo

`, + model: + '' + + `Foo` + + '' + }, { + name: 'article', + data: `

Foo

`, + model: + '' + + `Foo` + + '' + }, { + name: 'root', + data: `

Foo

`, + model: + 'Foo' + + `` + } ]; + + for ( const { name, data, model: modelData } of testCases ) { + it( `should allow element inside ${ name }`, () => { + dataFilter.allowElement( 'article' ); + dataFilter.allowElement( 'section' ); + + editor.setData( data ); + + expect( getModelData( model, { withoutSelection: true } ) ).to.equal( modelData ); + + expect( editor.getData() ).to.equal( data ); + } ); + } + } ); + + it( 'should not consume attributes already consumed (downcast)', () => { + dataFilter.allowAttributes( { name: 'script', attributes: true } ); + + editor.conversion.for( 'downcast' ).add( dispatcher => { + dispatcher.on( 'attribute:htmlAttributes:htmlScript', ( evt, data, conversionApi ) => { + conversionApi.consumable.consume( data.item, evt.name ); + }, { priority: 'high' } ); + } ); + + editor.setData( `

Foo

` ); + + expect( getModelDataWithAttributes( model, { withoutSelection: true } ) ).to.deep.equal( { + data: `Foo`, + attributes: { + 1: { attributes: { nonce: 'qwerty' } }, + 2: CODE + } + } ); + + expect( editor.getData() ).to.equal( `

Foo

` ); + } ); + + it( 'should not consume attributes already consumed (upcast)', () => { + dataFilter.allowAttributes( { name: 'script', attributes: true } ); + + editor.conversion.for( 'upcast' ).add( dispatcher => { + dispatcher.on( 'element:script', ( evt, data, conversionApi ) => { + conversionApi.consumable.consume( data.viewItem, { attributes: 'nonce' } ); + }, { priority: 'high' } ); + } ); + + editor.setData( `

Foo

` ); + + expect( getModelDataWithAttributes( model, { withoutSelection: true } ) ).to.deep.equal( { + data: `Foo`, + attributes: { + 1: { attributes: { type: 'c++' } }, + 2: CODE_CPP + } + } ); + } ); +} ); diff --git a/packages/ckeditor5-html-support/tests/manual/generalhtmlsupport.js b/packages/ckeditor5-html-support/tests/manual/generalhtmlsupport.js index cd3103cc1c8..d085bd41a83 100644 --- a/packages/ckeditor5-html-support/tests/manual/generalhtmlsupport.js +++ b/packages/ckeditor5-html-support/tests/manual/generalhtmlsupport.js @@ -110,6 +110,10 @@ ClassicEditor } ], styles: { 'background-color': true } + }, + { + name: 'script', + attributes: true } ], disallow: [ From c7188439edb53a9c8f76b88ff67612041e47e2b3 Mon Sep 17 00:00:00 2001 From: Arkadiusz Filipczak Date: Fri, 7 Jan 2022 14:23:05 +0100 Subject: [PATCH 2/3] Suppress console.warn() in tests --- .../ckeditor5-html-support/tests/integrations/script.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/ckeditor5-html-support/tests/integrations/script.js b/packages/ckeditor5-html-support/tests/integrations/script.js index 2eca7458a3f..3c9ae74d0ba 100644 --- a/packages/ckeditor5-html-support/tests/integrations/script.js +++ b/packages/ckeditor5-html-support/tests/integrations/script.js @@ -9,13 +9,13 @@ import GeneralHtmlSupport from '../../src/generalhtmlsupport'; import { getModelDataWithAttributes } from '../_utils/utils'; import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; -/* global document */ +/* global console, document */ describe( 'ScriptElementSupport', () => { const CODE = 'console.log( "Hello World" )'; const CODE_CPP = 'cout << "Hello World" << endl;'; - let editor, model, editorElement, dataFilter; + let editor, model, editorElement, dataFilter, warnStub; beforeEach( async () => { editorElement = document.createElement( 'div' ); @@ -29,9 +29,12 @@ describe( 'ScriptElementSupport', () => { dataFilter = editor.plugins.get( 'DataFilter' ); dataFilter.allowElement( 'script' ); + + warnStub = sinon.stub( console, 'warn' ); } ); afterEach( () => { + warnStub.restore(); editorElement.remove(); return editor.destroy(); From ed38d6ee3f9597da029a44584eeb40582d8b01b6 Mon Sep 17 00:00:00 2001 From: Kuba Niegowski Date: Tue, 11 Jan 2022 18:34:44 +0100 Subject: [PATCH 3/3] Code review fixes. --- packages/ckeditor5-html-support/src/integrations/script.js | 4 ++-- packages/ckeditor5-html-support/src/schemadefinitions.js | 1 - packages/ckeditor5-html-support/tests/integrations/script.js | 3 +-- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/ckeditor5-html-support/src/integrations/script.js b/packages/ckeditor5-html-support/src/integrations/script.js index 994d4dd3010..dc4579e36cb 100644 --- a/packages/ckeditor5-html-support/src/integrations/script.js +++ b/packages/ckeditor5-html-support/src/integrations/script.js @@ -4,7 +4,7 @@ */ /** - * @module html-support/integrations/codeblock + * @module html-support/integrations/script */ import { Plugin } from 'ckeditor5/src/core'; @@ -44,7 +44,7 @@ export default class ScriptElementSupport extends Plugin { schema.register( 'htmlScript', definition.modelSchema ); schema.extend( 'htmlScript', { - allowAttributes: [ 'htmlAttributes' ] + allowAttributes: [ 'htmlAttributes', 'htmlContent' ] } ); editor.data.registerRawContentMatcher( { diff --git a/packages/ckeditor5-html-support/src/schemadefinitions.js b/packages/ckeditor5-html-support/src/schemadefinitions.js index c5d1c209664..dc7fe108d1f 100644 --- a/packages/ckeditor5-html-support/src/schemadefinitions.js +++ b/packages/ckeditor5-html-support/src/schemadefinitions.js @@ -46,7 +46,6 @@ // // Skipped hidden elements: // noscript -// script export default { block: [ diff --git a/packages/ckeditor5-html-support/tests/integrations/script.js b/packages/ckeditor5-html-support/tests/integrations/script.js index 3c9ae74d0ba..e5d03373110 100644 --- a/packages/ckeditor5-html-support/tests/integrations/script.js +++ b/packages/ckeditor5-html-support/tests/integrations/script.js @@ -21,10 +21,9 @@ describe( 'ScriptElementSupport', () => { editorElement = document.createElement( 'div' ); document.body.appendChild( editorElement ); - const newEditor = await ClassicTestEditor.create( editorElement, { + editor = await ClassicTestEditor.create( editorElement, { plugins: [ Paragraph, GeneralHtmlSupport ] } ); - editor = newEditor; model = editor.model; dataFilter = editor.plugins.get( 'DataFilter' );