-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
[GHS] custom elements #11744
Merged
Merged
[GHS] custom elements #11744
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d10933b
PoC of handling custom elements by GHS.
niegowski 41d207d
Fix comment.
niegowski cc5c341
Checking alternative approach.
niegowski 76d4fce
Revert "Checking alternative approach."
niegowski 43463c7
Fixed a case with spaces replaced with nbsp entity inside a custom el…
niegowski 50f70f2
Merge branch 'master' into ck/11432-ghs-custom-elements
niegowski af95af9
Adding tests.
niegowski 7758d4d
Adding tests.
niegowski 1c888d4
Added tests.
niegowski 1dccf1c
Added integration with HTML comments.
niegowski 5dfa93e
Merge branch 'master' into ck/11432-ghs-custom-elements
oleq e1e409a
Docs.
oleq 26e1859
Docs.
oleq 39932cc
Gotta name 'em all.
oleq fa2aa81
Applied review suggestions.
oleq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
packages/ckeditor5-html-support/src/integrations/customelement.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/** | ||
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
/** | ||
* @module html-support/integrations/customelement | ||
*/ | ||
|
||
import { Plugin } from 'ckeditor5/src/core'; | ||
import { UpcastWriter } from 'ckeditor5/src/engine'; | ||
|
||
import DataSchema from '../dataschema'; | ||
import DataFilter from '../datafilter'; | ||
import { setViewAttributes } from '../conversionutils'; | ||
|
||
/** | ||
* Provides the General HTML Support for custom elements (not registered in the DataSchema). | ||
oleq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @extends module:core/plugin~Plugin | ||
*/ | ||
export default class CustomElementSupport extends Plugin { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
static get requires() { | ||
return [ DataFilter, DataSchema ]; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
init() { | ||
const dataFilter = this.editor.plugins.get( DataFilter ); | ||
const dataSchema = this.editor.plugins.get( DataSchema ); | ||
|
||
dataFilter.on( 'register:$customElement', ( evt, definition ) => { | ||
evt.stop(); | ||
|
||
const editor = this.editor; | ||
const schema = editor.model.schema; | ||
const conversion = editor.conversion; | ||
const unsafeElements = editor.editing.view.domConverter.unsafeElements; | ||
const preLikeElements = editor.data.htmlProcessor.domConverter.preElements; | ||
|
||
schema.register( definition.model, definition.modelSchema ); | ||
schema.extend( definition.model, { | ||
allowAttributes: [ 'htmlElementName', 'htmlAttributes', 'htmlContent' ], | ||
isContent: true | ||
} ); | ||
|
||
conversion.for( 'upcast' ).elementToElement( { | ||
view: /.*/, | ||
model: ( viewElement, conversionApi ) => { | ||
// Do not try to convert $comment fake element. | ||
if ( viewElement.name == '$comment' ) { | ||
return; | ||
} | ||
|
||
// Allow for fallback only if this element is not defined in data schema to make sure | ||
// that this will handle only custom elements not registered in the data schema. | ||
if ( dataSchema.getDefinitionsForView( viewElement.name ).size ) { | ||
return; | ||
} | ||
|
||
// Make sure that this element will not render in the editing view. | ||
if ( !unsafeElements.includes( viewElement.name ) ) { | ||
unsafeElements.push( viewElement.name ); | ||
} | ||
|
||
// Make sure that whitespaces will not be trimmed or replaced by nbsps while stringify content. | ||
if ( !preLikeElements.includes( viewElement.name ) ) { | ||
preLikeElements.push( viewElement.name ); | ||
} | ||
|
||
const modelElement = conversionApi.writer.createElement( definition.model, { | ||
htmlElementName: viewElement.name | ||
} ); | ||
|
||
const htmlAttributes = dataFilter.processViewAttributes( viewElement, conversionApi ); | ||
|
||
if ( htmlAttributes ) { | ||
conversionApi.writer.setAttribute( 'htmlAttributes', htmlAttributes, modelElement ); | ||
} | ||
|
||
// Store the whole element in the attribute so that DomConverter will be able to use the pre like element context. | ||
const viewWriter = new UpcastWriter( viewElement.document ); | ||
const documentFragment = viewWriter.createDocumentFragment( viewElement ); | ||
const htmlContent = editor.data.processor.toData( documentFragment ); | ||
|
||
conversionApi.writer.setAttribute( 'htmlContent', htmlContent, modelElement ); | ||
|
||
// Consume the content of the element. | ||
for ( const { item } of editor.editing.view.createRangeIn( viewElement ) ) { | ||
conversionApi.consumable.consume( item, { name: true } ); | ||
} | ||
|
||
return modelElement; | ||
}, | ||
converterPriority: 'low' | ||
} ); | ||
|
||
conversion.for( 'editingDowncast' ).elementToElement( { | ||
model: { | ||
name: definition.model, | ||
attributes: [ 'htmlElementName', 'htmlAttributes', 'htmlContent' ] | ||
}, | ||
view: ( modelElement, { writer } ) => { | ||
const viewName = modelElement.getAttribute( 'htmlElementName' ); | ||
const viewElement = writer.createRawElement( viewName ); | ||
|
||
if ( modelElement.hasAttribute( 'htmlAttributes' ) ) { | ||
setViewAttributes( writer, modelElement.getAttribute( 'htmlAttributes' ), viewElement ); | ||
} | ||
|
||
return viewElement; | ||
} | ||
} ); | ||
|
||
conversion.for( 'dataDowncast' ).elementToElement( { | ||
model: { | ||
name: definition.model, | ||
attributes: [ 'htmlElementName', 'htmlAttributes', 'htmlContent' ] | ||
}, | ||
view: ( modelElement, { writer } ) => { | ||
const viewName = modelElement.getAttribute( 'htmlElementName' ); | ||
const htmlContent = modelElement.getAttribute( 'htmlContent' ); | ||
|
||
const viewElement = writer.createRawElement( viewName, null, ( domElement, domConverter ) => { | ||
domConverter.setContentOf( domElement, htmlContent ); | ||
|
||
// Unwrap the custom element content (it was stored in the attribute as the whole custom element). | ||
const customElement = domElement.firstChild; | ||
|
||
customElement.remove(); | ||
|
||
while ( customElement.firstChild ) { | ||
domElement.appendChild( customElement.firstChild ); | ||
} | ||
} ); | ||
|
||
if ( modelElement.hasAttribute( 'htmlAttributes' ) ) { | ||
setViewAttributes( writer, modelElement.getAttribute( 'htmlAttributes' ), viewElement ); | ||
} | ||
|
||
return viewElement; | ||
} | ||
} ); | ||
} ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This breaks the following test:
ckeditor5/packages/ckeditor5-engine/tests/view/domconverter/whitespace-handling-integration.js
Line 110 in 30286f7
We need to choose to either trim that nbsp in that case or correctly handle scenarios like:
where the space (normal space) should be trimmed.
Unfortunately, since these are custom elements we can't easily make the decision on DOM->view conversion whether these are spaces between block or inline elements.
Additionally, unfortunately we lose the information whether this was an nbsp or normal space upon DOM->view conversion. Currently, all are normalized to normal spaces at this stage. if they weren't, the below code would actually correctly differentiate these situation.
Hence, we need to: