From 6f87bdf3825e727c227131c22b386e846f1c67c8 Mon Sep 17 00:00:00 2001 From: Kamil Piechaczek Date: Tue, 27 Aug 2019 15:24:53 +0200 Subject: [PATCH 1/3] Provided support for values defined as numbers in font-weight style. --- src/bold/boldediting.js | 17 +++++++++++++---- tests/bold/boldediting.js | 27 +++++++++++++++++++++++++++ tests/manual/basic-styles.html | 10 ++++++++++ tests/manual/basic-styles.md | 3 ++- 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/bold/boldediting.js b/src/bold/boldediting.js index 40602c4..f2e2b1f 100644 --- a/src/bold/boldediting.js +++ b/src/bold/boldediting.js @@ -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' || /\d+/.test( fontWeight ) && Number( fontWeight ) >= 600 ) { + return { + name: true, + styles: [ 'font-weight' ] + }; } } ] diff --git a/tests/bold/boldediting.js b/tests/bold/boldediting.js index 91d3f5c..e1377df 100644 --- a/tests/bold/boldediting.js +++ b/tests/bold/boldediting.js @@ -105,6 +105,33 @@ describe( 'BoldEditing', () => { expect( editor.getData() ).to.equal( '

foobar

' ); } ); + it( 'should convert font-weight defined as number to bold attribute (if the value is higher or equal to 600)', () => { + editor.setData( '

foobar

' ); + + expect( getModelData( model, { withoutSelection: true } ) ) + .to.equal( '<$text bold="true">foobar' ); + + expect( editor.getData() ).to.equal( '

foobar

' ); + } ); + + it( 'should not convert font-weight defined as number to bold attribute (if the value is lower than 600)', () => { + editor.setData( '

foobar

' ); + + expect( getModelData( model, { withoutSelection: true } ) ) + .to.equal( 'foobar' ); + + expect( editor.getData() ).to.equal( '

foobar

' ); + } ); + + it( 'should not convert font-weight if the value is invalid', () => { + editor.setData( '

foobar

' ); + + expect( getModelData( model, { withoutSelection: true } ) ) + .to.equal( 'foobar' ); + + expect( editor.getData() ).to.equal( '

foobar

' ); + } ); + it( 'should be integrated with autoparagraphing', () => { editor.setData( 'foobar' ); diff --git a/tests/manual/basic-styles.html b/tests/manual/basic-styles.html index f9f1c0e..a696834 100644 --- a/tests/manual/basic-styles.html +++ b/tests/manual/basic-styles.html @@ -1,3 +1,13 @@

This is an editor instance, X1, X2.

+

+ The font-weight attribute check: + bold, + 400, + 500, + 600, + 700, + 800, + 900. +

diff --git a/tests/manual/basic-styles.md b/tests/manual/basic-styles.md index 48a9130..11cc0cd 100644 --- a/tests/manual/basic-styles.md +++ b/tests/manual/basic-styles.md @@ -8,4 +8,5 @@ * code `"an"`, * subscript X1, * superscript X2. -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. From 2dc61f2f6c5a6ce6cff1dc2e8dd2301b32ccfcb7 Mon Sep 17 00:00:00 2001 From: Kamil Piechaczek Date: Tue, 27 Aug 2019 15:42:08 +0200 Subject: [PATCH 2/3] Simplified the if. --- src/bold/boldediting.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bold/boldediting.js b/src/bold/boldediting.js index f2e2b1f..0ce19a8 100644 --- a/src/bold/boldediting.js +++ b/src/bold/boldediting.js @@ -47,7 +47,7 @@ export default class BoldEditing extends Plugin { } // Value of the `font-weight` attribute can be defined as a string or a number. - if ( fontWeight == 'bold' || /\d+/.test( fontWeight ) && Number( fontWeight ) >= 600 ) { + if ( fontWeight == 'bold' || Number( fontWeight ) >= 600 ) { return { name: true, styles: [ 'font-weight' ] From b1bbdba838c4e434418a11d7ae6330c6f0a9d001 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Go=C5=82aszewski?= Date: Thu, 29 Aug 2019 08:19:07 +0200 Subject: [PATCH 3/3] Add numeric font-weight to bold's supported elements in feature guide. --- docs/features/basic-styles.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/features/basic-styles.md b/docs/features/basic-styles.md index 7efac8b..57da9cf 100644 --- a/docs/features/basic-styles.md +++ b/docs/features/basic-styles.md @@ -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} | ``, ``, `<* style="font-weight: bold">` | +| {@link module:basic-styles/bold~Bold} | ``, ``, `<* style="font-weight: bold">` (or numeric values that are greater or equal 600) | | {@link module:basic-styles/italic~Italic} | ``, ``, `<* style="font-style: italic">` | | {@link module:basic-styles/underline~Underline} | ``, `<* style="text-decoration: underline">` | | {@link module:basic-styles/strikethrough~Strikethrough} | ``, ``, ``, `<* style="text-decoration: line-through">` |