diff --git a/classes/helpers/FrmStylesHelper.php b/classes/helpers/FrmStylesHelper.php index 6e6ecab830..08cf843654 100644 --- a/classes/helpers/FrmStylesHelper.php +++ b/classes/helpers/FrmStylesHelper.php @@ -852,10 +852,12 @@ public static function get_base_font_size_settings() { /** * Get the style settings that the styler preview can update with JavaScript, without requesting new CSS. * A setting only belongs here when its value is output as a CSS variable and nothing else. - * Settings used directly in a CSS template (in a condition, a calculation, or a raw value) need the server. - * This includes the templates from Pro versions that are still supported, so those settings are left out until Pro outputs them only as CSS variables. + * Settings used directly in a CSS template (in a calculation or a raw value) need the server. + * A template may still check if a setting is empty, since the preview requests new CSS when a value is or was empty. + * This includes the templates from Pro versions that are still supported, so the settings Pro uses directly are only added when Pro can remove them again. * * @since 7.1 + * @since x.x Added the settings the Lite templates now output only as CSS variables. * * @return array */ @@ -921,8 +923,39 @@ public static function get_live_preview_settings() { 'success_border_color', 'success_font_size', 'progress_border_size', + 'font', + 'width', + 'description_font_size', + 'description_color', + 'description_weight', + 'description_style', + 'description_align', + 'description_margin', + 'field_width', + 'field_margin', + 'border_color', + 'border_radius', + 'border_width_error', + 'check_label_color', + 'check_weight', + 'check_font_size', + 'submit_width', + 'submit_height', + 'submit_border_width', + 'submit_shadow_color', ); + $pro_outputs_style = has_action( 'frm_output_single_style', 'FrmProStylesController::output_single_style' ); + + if ( ! $pro_outputs_style ) { + $settings = array_merge( $settings, self::get_live_preview_settings_without_pro() ); + } + + // Pro 7.0.1 and later remove the settings their templates use directly with the filter below. + if ( ! $pro_outputs_style || has_filter( 'frm_style_live_preview_settings', 'FrmProStylesController::live_preview_settings' ) ) { + $settings = array_merge( $settings, self::get_live_preview_settings_filtered_by_pro() ); + } + /** * Filter the style settings that the styler preview can update with JavaScript. * Remove any setting that a frm_output_single_style callback uses directly, so it still gets new CSS from the server. @@ -938,6 +971,41 @@ public static function get_live_preview_settings() { return is_array( $settings ) ? array_values( array_unique( array_filter( $settings, 'is_string' ) ) ) : array(); } + /** + * Get the style settings that every Pro version uses directly in its style template. + * Lite only uses these in CSS variables, so the preview updates them with JavaScript when Pro is not active. + * + * @since x.x + * + * @return array + */ + private static function get_live_preview_settings_without_pro() { + return array( + 'field_height', + 'field_border_width', + ); + } + + /** + * Get the style settings that Pro versions before 7.0.1 use directly in their style template. + * Pro 7.0.1 and later remove the ones they still use directly with the frm_style_live_preview_settings filter. + * + * @since x.x + * + * @return array + */ + private static function get_live_preview_settings_filtered_by_pro() { + return array( + 'font_size', + 'label_color', + 'text_color', + 'border_color_active', + 'field_pad', + 'error_text', + 'success_text_color', + ); + } + /** * Get style font size scale value. * diff --git a/css/_single_theme.css.php b/css/_single_theme.css.php index c1a624a6c5..f0da54e2c6 100644 --- a/css/_single_theme.css.php +++ b/css/_single_theme.css.php @@ -55,28 +55,28 @@ . .frm_error, . .frm_pro_max_limit_desc{ - margin:; + margin:var(--description-margin); margin-top: 6px; padding:0; - font-family:; + font-family:var(--font); - font-size:; + font-size:var(--description-font-size); - color:; + color:var(--description-color); - font-weight:; + font-weight:var(--description-weight); - text-align:; + text-align:var(--description-align); - font-style:; + font-style:var(--description-style); max-width:100%; } @@ -103,11 +103,11 @@ . .frm_form_field.frm_left_container{ - grid-template-columns: auto; + grid-template-columns: var(--width) auto; } . .frm_form_field.frm_right_container{ - grid-template-columns: auto ; + grid-template-columns: auto var(--width); } . .frm_inline_container.frm_dynamic_select_container .frm_data_container, @@ -128,16 +128,16 @@ . .frm_scale label{ - font-weight:; + font-weight:var(--check-weight); - font-family:; + font-family:var(--font); - font-size:; + font-size:var(--check-font-size); - color:; + color:var(--check-label-color); } @@ -183,6 +183,10 @@ outline: none; box-shadow:0px 0px 5px 0px rgba(, 0.6); + + box-shadow:0px 0px 5px 0px color-mix(in srgb, var(--border-color-active) 60%, transparent); } @@ -191,6 +195,7 @@ -webkit-box-shadow: none; -webkit-box-shadow: 0px 0px 5px 0px rgba(, 0.6); + -webkit-box-shadow: 0px 0px 5px 0px color-mix(in srgb, var(--border-color-active) 60%, transparent); } @@ -203,16 +208,16 @@ . .frm_submit input[type=button], . .frm_submit button, .frm_form_submit_style { - width:; + width:var(--submit-width,); - font-family:; + font-family:var(--font,); font-size:var(--submit-font-size,); - height:; + height:var(--submit-height,); line-height:normal; text-align:center; background: @@ -226,7 +231,7 @@ ?> ; - border-width:; + border-width:var(--submit-border-width,); border-color:var(--submit-border-color,); border-style:solid; @@ -238,7 +243,7 @@ padding:var(--submit-padding,); box-sizing:border-box; - box-shadow:0 1px 1px ; + box-shadow:0 1px 1px var(--submit-shadow-color,); margin:; .frm_submit.frm_inline_submit::before { content:"before"; - font-family:; + font-family:var(--font); font-size:var(--font-size); color:var(--label-color); @@ -356,7 +361,7 @@ ?> . #frm_field_cptch_number_container{ - font-family:; + font-family:var(--font); font-size:var(--font-size); color:var(--label-color); @@ -481,7 +486,7 @@ . #frm_loading .progress-striped .progress-bar{ - background-image:linear-gradient(45deg, 25%, rgba(0, 0, 0, 0) 25%, rgba(0, 0, 0, 0) 50%, 50%, 75%, rgba(0, 0, 0, 0) 75%, rgba(0, 0, 0, 0)); + background-image:linear-gradient(45deg, var(--border-color) 25%, rgba(0, 0, 0, 0) 25%, rgba(0, 0, 0, 0) 50%, var(--border-color) 50%, var(--border-color) 75%, rgba(0, 0, 0, 0) 75%, rgba(0, 0, 0, 0)); } . #frm_loading .progress-bar{ @@ -493,7 +498,7 @@ . .frm_form_field.frm_total_big textarea, . .frm_form_field.frm_total input, . .frm_form_field.frm_total textarea{ - color: ; + color: var(--text-color); background-color:transparent; border:none; display:inline; diff --git a/js/admin/style.js b/js/admin/style.js index 30b26aff44..918cd88e13 100644 --- a/js/admin/style.js +++ b/js/admin/style.js @@ -1528,9 +1528,10 @@ return; } + const previousValues = livePreview.values; livePreview.values = values; - const liveKeys = changedKeys.filter( key => canUpdateLive( key, values ) ); + const liveKeys = changedKeys.filter( key => canUpdateLive( key, values, previousValues ) ); if ( liveKeys.length !== changedKeys.length ) { debouncedChangeStyling(); } @@ -1541,9 +1542,12 @@ livePreview.updateCount++; liveKeys.forEach( key => { - livePreview.vars.set( getCssVarName( key ), { - value: prepareLiveValue( key, values[ key ] ), - updateCount: livePreview.updateCount + const value = prepareLiveValue( key, values[ key ] ); + getCssVarNames( key, values ).forEach( name => { + livePreview.vars.set( name, { + value, + updateCount: livePreview.updateCount + } ); } ); } ); renderLiveVars(); @@ -1601,13 +1605,28 @@ * Check if a setting can be updated without new CSS from the server. * * @since 7.1 + * @since x.x Added the previousValues parameter. * - * @param {string} key The setting key. - * @param {Object} values All current setting values. + * @param {string} key The setting key. + * @param {Object} values All current setting values. + * @param {Object} previousValues The setting values the preview reflected before this change. * @return {boolean} True if the CSS variable can be set with JavaScript. */ - function canUpdateLive( key, values ) { - if ( ! livePreview.settings.has( key ) || ! isSafeLiveValue( values[ key ] ) ) { + function canUpdateLive( key, values, previousValues ) { + if ( ! livePreview.settings.has( key ) ) { + return false; + } + + // The templates leave out some rules when a value is empty, so a change to or from an empty value needs the server. + if ( ! isSafeLiveValue( values[ key ] ) || ! isSafeLiveValue( previousValues[ key ] ) ) { + return false; + } + + if ( switchesTemplateBranch( key, values[ key ] ) || switchesTemplateBranch( key, previousValues[ key ] ) ) { + return false; + } + + if ( usesFieldShapeValue( key, values ) ) { return false; } @@ -1615,6 +1634,40 @@ return ! livePreview.baseFontSizeSettings.has( key ) || ! usesBaseFontSize( values ); } + /** + * The CSS templates add or skip rules when these settings are set to auto, so those values need the server. + * This matches the checks for field_height, submit_width and submit_height in css/_single_theme.css.php. + * + * @since x.x + * + * @param {string} key The setting key. + * @param {string} value The setting value. + * @return {boolean} True if the value changes which rules the template outputs. + */ + function switchesTemplateBranch( key, value ) { + const autoSettings = [ 'field_height', 'submit_width', 'submit_height' ]; + return autoSettings.includes( key ) && 'auto' === value.trim(); + } + + /** + * This matches the field shape overrides in FrmStylesHelper::css_var_prepare_value. + * + * @since x.x + * + * @param {string} key The setting key. + * @param {Object} values All current setting values. + * @return {boolean} True if the server replaces the setting value because of the field shape. + */ + function usesFieldShapeValue( key, values ) { + const shape = values.field_shape_type; + + if ( 'border_radius' === key ) { + return [ 'underline', 'regular', 'circle' ].includes( shape ); + } + + return [ 'field_border_width', 'border_width_error' ].includes( key ) && 'underline' === shape; + } + /** * This matches the check in FrmStylesHelper::update_base_font_size. * @@ -1674,6 +1727,24 @@ return `--${ key.replace( /_/g, '-' ).replace( /[^a-zA-Z0-9_-]/g, '' ) }`; } + /** + * Get every CSS variable that a setting sets. + * The auto width variable uses the field width unless automatic width is on, as in FrmStylesHelper::get_settings_for_output. + * + * @since x.x + * + * @param {string} key The setting key. + * @param {Object} values All current setting values. + * @return {string[]} The CSS variable names. + */ + function getCssVarNames( key, values ) { + const names = [ getCssVarName( key ) ]; + if ( 'field_width' === key && ( ! values.auto_width || '0' === values.auto_width ) ) { + names.push( getCssVarName( 'auto_width' ) ); + } + return names; + } + /** * Write the CSS variables set with JavaScript to a style tag after #this_css, so they take priority over the last server response. * diff --git a/tests/js/style-live-preview.test.mjs b/tests/js/style-live-preview.test.mjs index 68492414c8..914aeed29d 100644 --- a/tests/js/style-live-preview.test.mjs +++ b/tests/js/style-live-preview.test.mjs @@ -9,7 +9,10 @@ const source = readFileSync( new URL( '../../js/admin/style.js', import.meta.url const ast = parseSync( source, { configFile: false, babelrc: false } ); const styler = ast.program.body[ 0 ].expression.callee.body.body; const editor = styler.find( node => node.type === 'FunctionDeclaration' && node.id.name === 'initEditPage' ); -const names = [ 'canUpdateLive', 'usesBaseFontSize', 'isSafeLiveValue', 'isIncompleteHexColor', 'updatePreview', 'changeStyling' ]; +const names = [ + 'canUpdateLive', 'usesBaseFontSize', 'isSafeLiveValue', 'isIncompleteHexColor', 'updatePreview', 'changeStyling', + 'switchesTemplateBranch', 'usesFieldShapeValue', 'getCssVarNames' +]; const functions = names.map( name => { const declaration = editor.body.body.find( node => node.type === 'FunctionDeclaration' && node.id.name === name @@ -26,11 +29,15 @@ const functions = names.map( name => { * @return {Object} The production decision functions. */ function loadDecisions() { - return runInNewContext( `${ functions }\n({ canUpdateLive, isSafeLiveValue })`, { + return runInNewContext( `${ functions }\n({ canUpdateLive, isSafeLiveValue, getCssVarNames })`, { livePreview: { - settings: new Set( [ 'title_color', 'field_font_size', 'title_size' ] ), + settings: new Set( [ + 'title_color', 'field_font_size', 'title_size', 'field_height', 'submit_width', 'submit_height', + 'border_radius', 'field_border_width', 'border_width_error' + ] ), baseFontSizeSettings: new Set( [ 'field_font_size', 'title_size' ] ) - } + }, + getCssVarName: key => `--${ key.replace( /_/g, '-' ) }` } ); } @@ -69,6 +76,7 @@ function loadPreview() { setSelectPlaceholderColor: () => {}, getStyleSettingValues: () => preview.values, getCssVarName: key => key, + getCssVarNames: key => [ key ], prepareLiveValue: ( key, value ) => value, renderLiveVars: () => preview.renders++, debouncedChangeStyling: () => preview.requests++, @@ -142,12 +150,58 @@ test( 'values changed by server sanitization require server CSS', () => { test( 'only allowlisted settings with safe submitted values update live', () => { const { canUpdateLive } = loadDecisions(); - assert.equal( canUpdateLive( 'title_color', { title_color: '#abc' } ), true ); - assert.equal( canUpdateLive( 'style_name', { style_name: 'Example' } ), false ); - assert.equal( canUpdateLive( 'custom_setting', { custom_setting: '12px' } ), false ); - assert.equal( canUpdateLive( 'title_color', {} ), false ); - assert.equal( canUpdateLive( 'title_color', { title_color: '' } ), false ); - assert.equal( canUpdateLive( 'title_color', { title_color: 'red; color: blue' } ), false ); + assert.equal( canUpdateLive( 'title_color', { title_color: '#abc' }, { title_color: '#000' } ), true ); + assert.equal( canUpdateLive( 'style_name', { style_name: 'Example' }, { style_name: 'Old' } ), false ); + assert.equal( canUpdateLive( 'custom_setting', { custom_setting: '12px' }, { custom_setting: '10px' } ), false ); + assert.equal( canUpdateLive( 'title_color', {}, { title_color: '#000' } ), false ); + assert.equal( canUpdateLive( 'title_color', { title_color: '' }, { title_color: '#000' } ), false ); + assert.equal( canUpdateLive( 'title_color', { title_color: 'red; color: blue' }, { title_color: '#000' } ), false ); +} ); + +test( 'a change to or from an empty value requires server CSS', () => { + const { canUpdateLive } = loadDecisions(); + assert.equal( canUpdateLive( 'title_color', { title_color: '#abc' }, { title_color: '' } ), false ); + assert.equal( canUpdateLive( 'title_color', { title_color: '#abc' }, {} ), false ); + assert.equal( canUpdateLive( 'title_color', { title_color: '' }, { title_color: '#abc' } ), false ); + assert.equal( canUpdateLive( 'title_color', { title_color: '#abc' }, { title_color: '#def' } ), true ); +} ); + +test( 'auto heights and widths require server CSS because the template switches branches', () => { + const { canUpdateLive } = loadDecisions(); + for ( const key of [ 'field_height', 'submit_width', 'submit_height' ] ) { + assert.equal( canUpdateLive( key, { [ key ]: 'auto' }, { [ key ]: '32px' } ), false, `${ key } to auto` ); + assert.equal( canUpdateLive( key, { [ key ]: ' auto ' }, { [ key ]: '32px' } ), false, `${ key } to padded auto` ); + assert.equal( canUpdateLive( key, { [ key ]: '40px' }, { [ key ]: 'auto' } ), false, `${ key } from auto` ); + assert.equal( canUpdateLive( key, { [ key ]: '40px' }, { [ key ]: '32px' } ), true, `${ key } between sizes` ); + } + assert.equal( canUpdateLive( 'title_color', { title_color: 'auto' }, { title_color: '#abc' } ), true ); +} ); + +test( 'settings the field shape overrides require server CSS', () => { + const { canUpdateLive } = loadDecisions(); + const check = ( key, shape ) => canUpdateLive( + key, + { [ key ]: '5px', field_shape_type: shape }, + { [ key ]: '4px', field_shape_type: shape } + ); + for ( const shape of [ 'circle', 'regular', 'underline' ] ) { + assert.equal( check( 'border_radius', shape ), false, `border_radius with ${ shape }` ); + } + assert.equal( check( 'border_radius', 'rounded_corner' ), true ); + assert.equal( check( 'border_radius', '' ), true ); + for ( const key of [ 'field_border_width', 'border_width_error' ] ) { + assert.equal( check( key, 'underline' ), false, `${ key } with underline` ); + assert.equal( check( key, 'circle' ), true, `${ key } with circle` ); + assert.equal( check( key, '' ), true, `${ key } without a shape` ); + } +} ); + +test( 'field width also sets the auto width variable unless automatic width is on', () => { + const { getCssVarNames } = loadDecisions(); + assert.deepEqual( [ ...getCssVarNames( 'field_width', { auto_width: '0' } ) ], [ '--field-width', '--auto-width' ] ); + assert.deepEqual( [ ...getCssVarNames( 'field_width', {} ) ], [ '--field-width', '--auto-width' ] ); + assert.deepEqual( [ ...getCssVarNames( 'field_width', { auto_width: '1' } ) ], [ '--field-width' ] ); + assert.deepEqual( [ ...getCssVarNames( 'title_color', { auto_width: '0' } ) ], [ '--title-color' ] ); } ); test( 'scaled font sizes require server CSS when base font size is enabled', () => { @@ -160,23 +214,21 @@ test( 'scaled font sizes require server CSS when base font size is enabled', () title_size: '24px', title_color: '#abc' }; - assert.equal( canUpdateLive( 'field_font_size', values ), false ); - assert.equal( canUpdateLive( 'title_size', values ), false ); - assert.equal( canUpdateLive( 'title_color', values ), true ); + assert.equal( canUpdateLive( 'field_font_size', values, values ), false ); + assert.equal( canUpdateLive( 'title_size', values, values ), false ); + assert.equal( canUpdateLive( 'title_color', values, values ), true ); } } ); test( 'font sizes update live when base font size is absent or disabled', () => { const { canUpdateLive } = loadDecisions(); for ( const baseFontSize of [ undefined, '', '0' ] ) { - assert.equal( canUpdateLive( 'field_font_size', { - field_font_size: '20px', base_font_size: baseFontSize, use_base_font_size: '1' - } ), true ); + const values = { field_font_size: '20px', base_font_size: baseFontSize, use_base_font_size: '1' }; + assert.equal( canUpdateLive( 'field_font_size', values, values ), true ); } for ( const useBaseFontSize of [ undefined, '', '0', 'false' ] ) { - assert.equal( canUpdateLive( 'field_font_size', { - field_font_size: '20px', base_font_size: '16px', use_base_font_size: useBaseFontSize - } ), true ); + const values = { field_font_size: '20px', base_font_size: '16px', use_base_font_size: useBaseFontSize }; + assert.equal( canUpdateLive( 'field_font_size', values, values ), true ); } } ); @@ -204,9 +256,16 @@ test( 'clearing a color or leaving a partial hex color still updates the preview preview.update( false, true ); assert.equal( preview.requests, 1 ); + // A color set after an empty value needs the server, since the template skips some rules for empty values. + preview.values = { title_color: '#34' }; + preview.update(); + assert.equal( preview.requests, 2 ); + assert.equal( preview.vars.has( 'title_color' ), false ); + preview.values = { title_color: '#12' }; preview.update(); assert.equal( preview.vars.get( 'title_color' ).value, '#12' ); + assert.equal( preview.requests, 2 ); } ); test( 'a failed styling request makes the next change send every setting again', () => { @@ -221,7 +280,13 @@ test( 'a failed styling request makes the next change send every setting again', preview.values = { title_color: '#abc', custom_setting: '70px' }; preview.update(); assert.equal( preview.requests, 2 ); - assert.equal( preview.vars.get( 'title_color' ).value, '#abc' ); + // The previous values are unknown after a failure, so the server CSS shows this change. + assert.equal( preview.vars.has( 'title_color' ), false ); + + preview.values = { title_color: '#def', custom_setting: '70px' }; + preview.update(); + assert.equal( preview.requests, 2 ); + assert.equal( preview.vars.get( 'title_color' ).value, '#def' ); } ); test( 'outdated styling responses cannot overwrite a newer preview', () => { diff --git a/tests/phpunit/styles/test_FrmStylesHelper.php b/tests/phpunit/styles/test_FrmStylesHelper.php index f8f7868ffc..bb464ae577 100644 --- a/tests/phpunit/styles/test_FrmStylesHelper.php +++ b/tests/phpunit/styles/test_FrmStylesHelper.php @@ -384,23 +384,52 @@ public function test_get_live_preview_settings() { $this->assertContains( 'submit_text_color', $settings ); $this->assertSame( array_values( array_unique( $settings ) ), $settings ); - // Pro versions that are still supported use these directly in their CSS templates. + // The Lite templates output these only as CSS variables, or skip them when they are empty. + $lite_settings = array( + 'description_color', + 'description_margin', + 'check_label_color', + 'width', + 'font', + 'border_color', + 'submit_width', + ); + + foreach ( $lite_settings as $key ) { + $this->assertContains( $key, $settings ); + } + + // Without Pro, the settings only the Pro templates use directly can update with JavaScript too. $used_directly_by_pro = array( 'font_size', 'label_color', + 'text_color', 'field_pad', - 'collapse_pos', + 'field_height', + 'field_border_width', 'error_text', - 'success_text_color', - 'section_border_color', - 'progress_border_color', - 'progress_size', ); foreach ( $used_directly_by_pro as $key ) { + $this->assertContains( $key, $settings ); + } + + // Lite still uses these directly, or only in a calculation. + $used_directly_by_lite = array( + 'direction', + 'submit_margin', + 'submit_bg_img', + 'collapse_pos', + 'progress_border_color', + ); + + foreach ( $used_directly_by_lite as $key ) { $this->assertNotContains( $key, $settings ); } + // Pro versions before 7.0.1 output style CSS without filtering this list, so the settings they use directly need the server. + $this->assert_live_preview_settings_without_pro_filter( array_merge( $used_directly_by_pro, array( 'border_color_active' ) ), $lite_settings ); + $filter = function ( $settings ) { $settings = array_diff( $settings, array( 'title_color' ) ); $settings[] = 'toggle_on_color'; @@ -427,6 +456,35 @@ public function test_get_live_preview_settings() { remove_filter( 'frm_style_live_preview_settings', '__return_null' ); } + /** + * Check the live preview settings when an older Pro version outputs style CSS without the frm_style_live_preview_settings filter. + * + * @param array $excluded Settings that need new CSS from the server. + * @param array $included Settings that still update with JavaScript. + * + * @return void + */ + private function assert_live_preview_settings_without_pro_filter( $excluded, $included ) { + $callback = 'FrmProStylesController::output_single_style'; + + if ( has_action( 'frm_output_single_style', $callback ) ) { + // Pro is active in this test run, and it adds the filter. + return; + } + + add_action( 'frm_output_single_style', $callback ); + $settings = FrmStylesHelper::get_live_preview_settings(); + remove_action( 'frm_output_single_style', $callback ); + + foreach ( $excluded as $key ) { + $this->assertNotContains( $key, $settings ); + } + + foreach ( $included as $key ) { + $this->assertContains( $key, $settings ); + } + } + /** * The styler preview updates the live submit button settings with CSS variables only, * so the submit button rule must read them from CSS variables.