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

Commit

Permalink
Merge pull request #96 from ckeditor/t/94
Browse files Browse the repository at this point in the history
Feature: Provided support for numeric values for the `font-weight` attribute. Closes #94. Closes ckeditor/ckeditor5-paste-from-office#74.
  • Loading branch information
jodator authored Aug 29, 2019
2 parents 0b9c20d + b1bbdba commit 25a0d7c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/features/basic-styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ By default, each feature can upcast more than one type of the content. Here's th

| Style feature | Supported input elements |
|-----|---|
| {@link module:basic-styles/bold~Bold} | `<strong>`, `<b>`, `<* style="font-weight: bold">` |
| {@link module:basic-styles/bold~Bold} | `<strong>`, `<b>`, `<* style="font-weight: bold">` (or numeric values that are greater or equal 600) |
| {@link module:basic-styles/italic~Italic} | `<i>`, `<em>`, `<* style="font-style: italic">` |
| {@link module:basic-styles/underline~Underline} | `<u>`, `<* style="text-decoration: underline">` |
| {@link module:basic-styles/strikethrough~Strikethrough} | `<s>`, `<del>`, `<strike>`, `<* style="text-decoration: line-through">` |
Expand Down
17 changes: 13 additions & 4 deletions src/bold/boldediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,24 @@ export default class BoldEditing extends Plugin {
} );

// Build converter from model to view for data and editing pipelines.

editor.conversion.attributeToElement( {
model: BOLD,
view: 'strong',
upcastAlso: [
'b',
{
styles: {
'font-weight': 'bold'
viewElement => {
const fontWeight = viewElement.getStyle( 'font-weight' );

if ( !fontWeight ) {
return null;
}

// Value of the `font-weight` attribute can be defined as a string or a number.
if ( fontWeight == 'bold' || Number( fontWeight ) >= 600 ) {
return {
name: true,
styles: [ 'font-weight' ]
};
}
}
]
Expand Down
27 changes: 27 additions & 0 deletions tests/bold/boldediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,33 @@ describe( 'BoldEditing', () => {
expect( editor.getData() ).to.equal( '<p><strong>foo</strong>bar</p>' );
} );

it( 'should convert font-weight defined as number to bold attribute (if the value is higher or equal to 600)', () => {
editor.setData( '<p><span style="font-weight: 600;">foo</span>bar</p>' );

expect( getModelData( model, { withoutSelection: true } ) )
.to.equal( '<paragraph><$text bold="true">foo</$text>bar</paragraph>' );

expect( editor.getData() ).to.equal( '<p><strong>foo</strong>bar</p>' );
} );

it( 'should not convert font-weight defined as number to bold attribute (if the value is lower than 600)', () => {
editor.setData( '<p><span style="font-weight: 500;">foo</span>bar</p>' );

expect( getModelData( model, { withoutSelection: true } ) )
.to.equal( '<paragraph>foobar</paragraph>' );

expect( editor.getData() ).to.equal( '<p>foobar</p>' );
} );

it( 'should not convert font-weight if the value is invalid', () => {
editor.setData( '<p><span style="font-weight: foo;">foo</span>bar</p>' );

expect( getModelData( model, { withoutSelection: true } ) )
.to.equal( '<paragraph>foobar</paragraph>' );

expect( editor.getData() ).to.equal( '<p>foobar</p>' );
} );

it( 'should be integrated with autoparagraphing', () => {
editor.setData( '<strong>foo</strong>bar' );

Expand Down
10 changes: 10 additions & 0 deletions tests/manual/basic-styles.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<div id="editor">
<p><i>This</i> <s>is</s> <code>an</code> <strong>editor</strong> <u>instance</u>, X<sub>1</sub>, X<sup>2</sup>.</p>
<p>
The <code>font-weight</code> attribute check:
<span style="font-weight:bold">bold</span>,
<span style="font-weight:400">400</span>,
<span style="font-weight:500">500</span>,
<span style="font-weight:600">600</span>,
<span style="font-weight:700">700</span>,
<span style="font-weight:800">800</span>,
<span style="font-weight:900">900</span>.
</p>
</div>
3 changes: 2 additions & 1 deletion tests/manual/basic-styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
* code `"an"`,
* subscript X<sub>1</sub>,
* superscript X<sup>2</sup>.
2. Test the bold, italic, strikethrough, underline, code, subscript and superscript features live.
2. The second sentence should bold the following words: `bold`, `600`, `700`, `800`, `900`.
3. Test the bold, italic, strikethrough, underline, code, subscript and superscript features live.

0 comments on commit 25a0d7c

Please sign in to comment.