Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Introduced standalone view-to-model converter for img element. #97

Merged
merged 5 commits into from
Apr 24, 2017
Merged

Conversation

scofalik
Copy link
Contributor

@scofalik scofalik commented Apr 12, 2017

Suggested merge commit message (convention)

Feature: Introduced view-to-model conversion for bare <img> elements. Closes ckeditor/ckeditor5#5032.

…t. View-to-model figure element converter do not create model image element on it's own.
@scofalik
Copy link
Contributor Author

Will need a PR in engine...

@scofalik
Copy link
Contributor Author

@@ -52,8 +54,18 @@ export default class ImageEngine extends Plugin {
createImageAttributeConverter( [ editing.modelToView, data.modelToView ], 'src' );
createImageAttributeConverter( [ editing.modelToView, data.modelToView ], 'alt' );

// Build converter for view img element to model image element.
buildViewConverter().for( data.viewToModel )
.from( { name: 'img', attribute: { src: /.+/ } } )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the src check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src attribute is required to correctly convert view <img> to model <image> (which requires src attribute). If I understand the code correctly :), this was also implemented in "old" converter (before changes introduced in this PR): https://github.com/ckeditor/ckeditor5-image/pull/97/files#diff-ac5209f1bc4dd7be6f3619d8632013c9L43 here and below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, being picky, I should just mention that . doesn't match new lines.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/[\s\S]+/ is what we usually use. On the other hand, you didn't add ^ and $ so, in fact, if that regexp is later used with match(), then . alone would be enough :D

Copy link
Contributor Author

@scofalik scofalik Apr 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

being picky

Being PK-y?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😆

Do you want to change this regexp to /./)? Or is that too PK-y?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/./ is fine, but let's please close this already.

@scofalik
Copy link
Contributor Author

Testes passes after related PR in engine has been merged.

@Reinmar
Copy link
Member

Reinmar commented Apr 20, 2017

Two small things:

  • let's have the alt converter targeting images only,
  • and let's have a test what happens when there are two images in a figure (just out of curiosity :D).

Tests: added test checking converters behaviour for <figure> with multiple <img> elements.
@scofalik
Copy link
Contributor Author

Fixed

@@ -57,12 +57,13 @@ export default class ImageEngine extends Plugin {
// Build converter for view img element to model image element.
buildViewConverter().for( data.viewToModel )
.from( { name: 'img', attribute: { src: /./ } } )
.toElement( viewImage => new ModelElement( 'image', { src: viewImage.getAttribute( 'src' ) } ) );
.toElement( ( viewImage ) => new ModelElement( 'image', { src: viewImage.getAttribute( 'src' ) } ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know I was removing these parenthesis in the previous commit? :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current standard is having parenthesis around arguments of every arrow function.

@@ -147,6 +147,28 @@ describe( 'ImageEngine', () => {
expect( getModelData( document, { withoutSelection: true } ) )
.to.equal( '<image alt="alt text" src="foo.png"></image>' );
} );

it( 'should not convert alt attribute on non-img element', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test doesn't check anything. Check out src to the previous commit and it will also pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, two things:

  1. Allowing alt on div is required, that's true. Without this the test doesn't make much sense.
  2. It will pass on previous version, because that was a feature of previous version too.

const data = editor.data;
const editing = editor.editing;

document.schema.registerItem( 'div', '$block' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's missing alt allowed here?

@Reinmar Reinmar merged commit fb6ab1a into master Apr 24, 2017
@Reinmar Reinmar deleted the t/8 branch April 24, 2017 11:25
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow bare <img> to be converted to model
2 participants