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

Commit 25a0d7c

Browse files
authored
Merge pull request #96 from ckeditor/t/94
Feature: Provided support for numeric values for the `font-weight` attribute. Closes #94. Closes ckeditor/ckeditor5-paste-from-office#74.
2 parents 0b9c20d + b1bbdba commit 25a0d7c

File tree

5 files changed

+53
-6
lines changed

5 files changed

+53
-6
lines changed

docs/features/basic-styles.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ By default, each feature can upcast more than one type of the content. Here's th
3333

3434
| Style feature | Supported input elements |
3535
|-----|---|
36-
| {@link module:basic-styles/bold~Bold} | `<strong>`, `<b>`, `<* style="font-weight: bold">` |
36+
| {@link module:basic-styles/bold~Bold} | `<strong>`, `<b>`, `<* style="font-weight: bold">` (or numeric values that are greater or equal 600) |
3737
| {@link module:basic-styles/italic~Italic} | `<i>`, `<em>`, `<* style="font-style: italic">` |
3838
| {@link module:basic-styles/underline~Underline} | `<u>`, `<* style="text-decoration: underline">` |
3939
| {@link module:basic-styles/strikethrough~Strikethrough} | `<s>`, `<del>`, `<strike>`, `<* style="text-decoration: line-through">` |

src/bold/boldediting.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,24 @@ export default class BoldEditing extends Plugin {
3434
} );
3535

3636
// Build converter from model to view for data and editing pipelines.
37-
3837
editor.conversion.attributeToElement( {
3938
model: BOLD,
4039
view: 'strong',
4140
upcastAlso: [
4241
'b',
43-
{
44-
styles: {
45-
'font-weight': 'bold'
42+
viewElement => {
43+
const fontWeight = viewElement.getStyle( 'font-weight' );
44+
45+
if ( !fontWeight ) {
46+
return null;
47+
}
48+
49+
// Value of the `font-weight` attribute can be defined as a string or a number.
50+
if ( fontWeight == 'bold' || Number( fontWeight ) >= 600 ) {
51+
return {
52+
name: true,
53+
styles: [ 'font-weight' ]
54+
};
4655
}
4756
}
4857
]

tests/bold/boldediting.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,33 @@ describe( 'BoldEditing', () => {
105105
expect( editor.getData() ).to.equal( '<p><strong>foo</strong>bar</p>' );
106106
} );
107107

108+
it( 'should convert font-weight defined as number to bold attribute (if the value is higher or equal to 600)', () => {
109+
editor.setData( '<p><span style="font-weight: 600;">foo</span>bar</p>' );
110+
111+
expect( getModelData( model, { withoutSelection: true } ) )
112+
.to.equal( '<paragraph><$text bold="true">foo</$text>bar</paragraph>' );
113+
114+
expect( editor.getData() ).to.equal( '<p><strong>foo</strong>bar</p>' );
115+
} );
116+
117+
it( 'should not convert font-weight defined as number to bold attribute (if the value is lower than 600)', () => {
118+
editor.setData( '<p><span style="font-weight: 500;">foo</span>bar</p>' );
119+
120+
expect( getModelData( model, { withoutSelection: true } ) )
121+
.to.equal( '<paragraph>foobar</paragraph>' );
122+
123+
expect( editor.getData() ).to.equal( '<p>foobar</p>' );
124+
} );
125+
126+
it( 'should not convert font-weight if the value is invalid', () => {
127+
editor.setData( '<p><span style="font-weight: foo;">foo</span>bar</p>' );
128+
129+
expect( getModelData( model, { withoutSelection: true } ) )
130+
.to.equal( '<paragraph>foobar</paragraph>' );
131+
132+
expect( editor.getData() ).to.equal( '<p>foobar</p>' );
133+
} );
134+
108135
it( 'should be integrated with autoparagraphing', () => {
109136
editor.setData( '<strong>foo</strong>bar' );
110137

tests/manual/basic-styles.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
<div id="editor">
22
<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>
3+
<p>
4+
The <code>font-weight</code> attribute check:
5+
<span style="font-weight:bold">bold</span>,
6+
<span style="font-weight:400">400</span>,
7+
<span style="font-weight:500">500</span>,
8+
<span style="font-weight:600">600</span>,
9+
<span style="font-weight:700">700</span>,
10+
<span style="font-weight:800">800</span>,
11+
<span style="font-weight:900">900</span>.
12+
</p>
313
</div>

tests/manual/basic-styles.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
* code `"an"`,
99
* subscript X<sub>1</sub>,
1010
* superscript X<sup>2</sup>.
11-
2. Test the bold, italic, strikethrough, underline, code, subscript and superscript features live.
11+
2. The second sentence should bold the following words: `bold`, `600`, `700`, `800`, `900`.
12+
3. Test the bold, italic, strikethrough, underline, code, subscript and superscript features live.

0 commit comments

Comments
 (0)