Skip to content

Make the Visual Styler preview match the front end - #3701

Open
Crabcyborg wants to merge 4 commits into
masterfrom
fix/styler-preview-accuracy
Open

Crabcyborg wants to merge 4 commits into
masterfrom
fix/styler-preview-accuracy

Conversation

@Crabcyborg

Copy link
Copy Markdown
Contributor

Related: https://github.com/Strategy11/formidable-views/pull/745 (same theme CSS approach, for the View editor)

The Visual Styler preview sits inside wp-admin, so admin CSS reached into it and the theme CSS never did. A field could look noticeably different in the styler than on the live form. Comparing computed styles between the styler preview and the in-theme preview for the same form and style, this takes the number of differing properties from 397 to 26. Most of what remains depends on front end JavaScript that the preview does not load (intl-tel phone input, SlimSelect, range fill).

What changed

  • Formidable admin CSS no longer applies inside the preview. About 120 selectors in resources/scss/admin (fonts, paragraphs, headings, links, labels, descriptions, radios and checkboxes, field widths and margins) get :where(:not(#frm_style_preview *)) through a new $outside-styler-preview SCSS variable. :where() keeps their specificity unchanged everywhere else, so the builder and settings pages are unaffected.
  • This fixes the label padding setting in the preview. The admin .frm_form_field > label:first-child { padding: 0 } rule overrode --label-padding, so that setting never showed in the styler.
  • New css/admin/styler-preview.css undoes the wp-admin element styles from common.css and forms.css inside the preview. Each rule mirrors the wp-admin selector inside @scope and sets revert, so it beats the wp-admin rule but not Formidable style rules or the theme.
  • The active theme's front end CSS is loaded into the preview, scoped with @scope to #frm_active_style_form, #frm_sample_form. New FrmStylesThemeCssHelper collects the theme's enqueued stylesheets plus the global stylesheet, rewrites html/body/:root selectors and rem lengths, and hoists @font-face and @keyframes. It is ported from the Views PR above, with the scope passed in so Views can reuse it. It is served from a new frm_styler_theme_css AJAX action, cached in a transient, and can be turned off with the new frm_styler_load_theme_css filter. Browsers without @scope support ignore the block, so the preview looks the way it does today.
  • Removed css/admin/style.css overrides that made the preview differ on purpose: the dashicon select arrow, appearance: none on selects, and the label and checkbox overrides that only existed to fight admin CSS.
  • Faster styler loads. The styler's copy of the saved stylesheet (frmpro_css, over 1 MB on sites with many styles) and the theme CSS are now browser-cacheable. Their URL versions change on any style save, global custom CSS edit, or theme or Global Styles change. Both end their response with die() rather than wp_die(), because the AJAX wp_die() handler re-sends no-cache headers.
  • js/admin/style.js cancels an outdated preview request when a newer one starts, so dragging a slider no longer queues up requests.

Notes

  • A theme rule that is less specific than a wp-admin selector, like * { box-sizing: border-box; }, loses to the reset. Settings where the wp-admin value matches what nearly every theme does are therefore not reset. Small known leftovers: radio line-height, a 3px radio margin, and the 10px form padding the styler adds on purpose.
  • Tested with a classic theme (hello-elementor). Block themes go through the same global stylesheet path but were not tested.
  • css/frm_testing_mode.css changes only because it shares the button SCSS. The added selector never matches outside the styler.

Testing

  1. Activate a theme with a distinctive body font and form element styles.
  2. Open Formidable > Styles > edit a style, with a form that has text, select, radio, checkbox, HTML and description content.
  3. Compare the preview with the same form on a front end page. Fonts, base text size and color, headings, paragraphs, label spacing, select arrows and radio and checkbox styling should now match.
  4. Change the style's label padding. The preview label should move. Before this change it did not.
  5. Check that the styler sidebar, the form builder and the global settings pages look unchanged.
  6. Reload the styler. In the network panel, the frmpro_css and frm_styler_theme_css requests should come from the browser cache. Save the style, reload, and both should download again.
  7. Add add_filter( 'frm_styler_load_theme_css', '__return_false' ); and confirm the preview loads without the theme CSS.

🤖 Generated with Claude Code

Keep admin CSS out of the Visual Styler preview so a form looks the
same there as on the live site. Formidable admin rules that reached
into the preview now skip it, and a scoped reset undoes the wp-admin
element styles. This also lets the label padding setting show in the
preview, which an admin rule used to zero.

Load the active theme's front end CSS into the preview, scoped with
@scope so it cannot leak into the rest of wp-admin. It can be turned
off with the frm_styler_load_theme_css filter.

Let the browser cache the styler's versioned stylesheets, and cancel
an outdated preview request when a newer one starts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 18 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between ab4ee48 and b02ea6c.

Files selected for processing: 26

  • classes/controllers/FrmHooksController.php
  • classes/controllers/FrmStylesController.php
  • classes/helpers/FrmStylesPreviewHelper.php
  • classes/helpers/FrmStylesThemeCssHelper.php
  • css/admin/style.css
  • css/admin/styler-preview.css
  • css/frm_admin.css
  • css/frm_testing_mode.css
  • js/admin/style.js
  • resources/scss/admin/base/_variables.scss
  • resources/scss/admin/base/typography/_font.scss
  • resources/scss/admin/base/typography/_headings.scss
  • resources/scss/admin/base/typography/_text.scss
  • resources/scss/admin/components/builder/_insert-fields.scss
  • resources/scss/admin/components/button/_button.scss
  • resources/scss/admin/components/button/_frm-button.scss
  • resources/scss/admin/components/form/_fields-part2.scss
  • resources/scss/admin/components/form/_fields-part3.scss
  • resources/scss/admin/components/form/_fields.scss
  • resources/scss/admin/components/form/_form-error.scss
  • resources/scss/admin/components/form/_form-field.scss
  • resources/scss/admin/components/form/_form-inputs.scss
  • resources/scss/admin/components/form/_mce.scss
  • resources/scss/admin/components/links/_links.scss
  • resources/scss/admin/media-queries/_screen-tablet.scss
  • tests/phpunit/test_FrmStylesPreviewAlignment.php
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@deepsource-io

deepsource-io Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

DeepSource Code Review

We reviewed changes in ab4ee48...b02ea6c on this pull request. Below is the summary for the review, and you can see the individual issues we found as inline review comments.

See full review on DeepSource ↗

PR Report Card

Overall Grade   Security  

Reliability  

Complexity  

Hygiene  

Code Review Summary

Analyzer Status Updated (UTC) Details
PHP Nov 18, 2026 7:54p.m. Review ↗
JavaScript Nov 18, 2026 7:54p.m. Review ↗

Important

AI Review is run only on demand for your team. We're only showing results of static analysis review right now. To trigger AI Review, comment @deepsourcebot review on this thread.

$last_update = get_option( 'frm_last_style_update' );
$custom_css = self::get_custom_css();

return FrmAppHelper::plugin_version() . '-' . ( $last_update ? $last_update : '0' ) . '-' . substr( md5( is_string( $custom_css ) ? $custom_css : '' ), 0, 8 );

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Use of insecure md5() function found


Using md5(), sha1() function is not recommended to generate secure passwords. Due to its fast nature to compute passwords too quickly, these functions can become really easy to crack a password using brute force attack.

It is recommended to use PHP's password hashing function password_hash() to create a secure password hash.

}

$version = FrmStylesThemeCssHelper::get_version();
$cache_key = 'frm_styler_theme_css_' . md5( $version );

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Use of insecure md5() function found


Using md5(), sha1() function is not recommended to generate secure passwords. Due to its fast nature to compute passwords too quickly, these functions can become really easy to crack a password using brute force attack.

It is recommended to use PHP's password hashing function password_hash() to create a secure password hash.

$theme = wp_get_theme();
$global_styles = self::get_global_stylesheet();

return FrmAppHelper::plugin_version() . '-' . $theme->get_stylesheet() . '-' . $theme->get( 'Version' ) . '-' . substr( md5( $global_styles ), 0, 8 );

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Use of insecure md5() function found


Using md5(), sha1() function is not recommended to generate secure passwords. Due to its fast nature to compute passwords too quickly, these functions can become really easy to crack a password using brute force attack.

It is recommended to use PHP's password hashing function password_hash() to create a secure password hash.

*
* @return array List of chunks with 'type', 'name', 'prelude' and 'block' keys.
*/
private static function split_rules( $css ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

`split_rules` has a cyclomatic complexity of 21 with "High" risk


A function with high cyclomatic complexity can be hard to understand and
maintain. Cyclomatic complexity is a software metric that measures the number of
independent paths through a function. A higher cyclomatic complexity indicates
that the function has more decision points and is more complex.

Require the frm_ajax nonce, as the other styler AJAX actions do,
before serving the theme CSS for the preview.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@franky-the-going-merry franky-the-going-merry Bot added franky-working Franky is actively reviewing this and removed franky-working Franky is actively reviewing this labels Nov 12, 2026

@franky-the-going-merry franky-the-going-merry Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Request changes. I ran this on the PR head (68e86cc) in the Formidable preview env with the block theme Twenty Twenty-Five.

What works (live): I compared computed styles of the same form (text, textarea, label, description, submit) on a front-end page and in the Visual Styler. On master the preview differed from the front end on 12 properties (font family on every control, label line-height / letter-spacing / bottom margin vs padding, textarea line-height, description line-height, submit font). On this head there were 0 differences. The label-padding claim holds: the master preview shows label padding-bottom: 0 and margin-bottom: 6px where the front end has 5px padding. The new frm_styler_theme_css endpoint returns the scoped CSS (200, 27 KB), is capability- and nonce-gated, and both stylesheets go out with the private, max-age=31536000, immutable header. Builder and styler sidebar still render normally (screenshots checked). Security pass by hand: the endpoint reads theme files only and echoes no request input; nothing exploitable found.

Blocking

  1. FrmStylesThemeCssHelper.php:774: conditional html.x / body.x rules become unconditional :scope rules, so a dark-mode or page-specific theme rule leaks into every preview (reproduced, see inline).
  2. FrmStylesThemeCssHelper.php:388: the url() rewrite breaks on parentheses in data URIs and can corrupt the rules after it (reproduced, see inline).
  3. No tests. This adds a 934-line CSS rewriter with no PHPUnit coverage; tests/phpunit/styles/ has the neighbouring tests. The two bugs above are what a table-driven test over rewrite_selector_list(), absolutize_urls() and rewrite_rules() would have caught.

Non-blocking

  • Cache/version issues, see the inline note at FrmStylesController.php:224.
  • Theme-root matching at FrmStylesThemeCssHelper.php:228 uses str_starts_with() on a root URL with no trailing slash, so .../themes/twentytwenty also matches .../themes/twentytwentyone/.... Compare against $root['url'] . '/'.
  • do_action( 'wp_enqueue_scripts' ) runs every plugin's front-end enqueue callbacks inside an admin-ajax request. The try/catch covers exceptions and errors, not exit. Fine to keep, but worth a note in the docblock.

Tests run: php -l on both PHP files and node --check js/admin/style.js are clean. The helper reproductions above were run directly against the PR's code. PHPUnit, PHPCS, PHPStan and Stylelint were not run by me, and CI shows most of those jobs as skipping on this head (only Psalm, Typos, DeepScan and Scrutinizer ran and passed; DeepSource PHP failed), so the PR currently has no lint or unit run at all. Not tested: block-theme theme.json edge cases beyond TT25, classic theme (PR says hello-elementor was tested), and the "from browser cache" network-panel check (verified through the response headers instead).

return $selector;
}

return ':scope' . ( '' === $stripped || str_starts_with( $stripped, '::' ) ? '' : ' ' ) . $stripped;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Blocking: html/body selectors lose their class, attribute and :not() conditions, so conditional theme rules apply unconditionally in the preview.

rewrite_selector() strips everything after html / body / :root that is a class, attribute or pseudo (the second and third preg_replace). A rule that only applies on the front end under a condition becomes an always-on rule. Reproduced on this head with the helper's own methods:

html.dark-mode .btn        => :scope .btn
body.home .x               => :scope .x
html:not(.no-js) p         => :scope p
body.custom-bg             => :scope
body.dark{background:#000;color:#fff}  =>  :scope{color:#fff}

So a theme with a body.dark-mode { color:#fff } (or body.home, body.logged-in, html.no-js ...) block makes the styler preview render white text, while the real form (no such class on the page) does not. That is the opposite of what this PR is for.

Fix: only rewrite a bare html / body / :root (optionally followed by a combinator). If the compound has a class, attribute, id or :not()/:is()/:has() condition, drop the whole selector from the list (the preview container never carries those classes), not just the condition. Add a test per case above.

return 'url(' . FrmStylesThemeCssHelper::absolutize_url( $matches[1], $base_url ) . ')';
};

return preg_replace_callback( '/url\(\s*([^)]+?)\s*\)/i', $callback, $css );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Blocking: the url() regex stops at the first ), which corrupts any URL that contains a parenthesis (inline SVG data URIs are the usual one).

return preg_replace_callback( '/url\(\s*([^)]+?)\s*\)/i', $callback, $css );

Reproduced on this head:

in : .b{background:url("data:image/svg+xml,%3Csvg(1)")}
out: .b{background:url("data:image/svg+xml,%3Csvg(1")")}

The stray ") leaves an unterminated string, and a browser's error recovery can then drop the following declarations or rules, so the damage is not limited to that one property. Themes use url("data:image/svg+xml,...rgb(...)...") for select arrows and checkmarks all the time, which is exactly what the styler preview shows.

Fix: match quoted values first, e.g. /url\(\s*(?:"([^"]*)"|'([^']*)'|([^)\s]+))\s*\)/i, and leave data:, #fragment and already-absolute values untouched (check that absolutize_url() does). Add a test with a quoted data URI containing parentheses.


if ( ! is_string( $css ) ) {
$css = FrmStylesThemeCssHelper::get_scoped_theme_css( '#frm_active_style_form, #frm_sample_form' );
set_transient( $cache_key, $css, DAY_IN_SECONDS );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Non-blocking: an empty result is cached for a day, and the version key misses real inputs.

  • get_scoped_theme_css() returns '' when the theme exposes no readable CSS or the sandboxed enqueue throws. is_string( '' ) is true, so set_transient() stores it and every request for the next 24h gets an empty sheet. Consider not caching an empty string.
  • FrmStylesThemeCssHelper::get_version() is built from the theme slug, the theme's Version header and a hash of wp_get_global_stylesheet(). Editing a classic theme's style.css without a version bump, or anything added through frm_preview_theme_stylesheets or a plugin's wp_enqueue_scripts, leaves the key unchanged. The transient then serves the old CSS for a day, and the immutable header keeps the browser copy too. The docblock also says a Customizer edit changes the version; only the Global Styles part does. Consider adding the stylesheet files' filemtime() to the version.

@franky-the-going-merry franky-the-going-merry Bot removed franky-review franky-working Franky is actively reviewing this labels Nov 12, 2026
The preview alignment rules now target only fields that inherit the
style layout, plus the sample form. A field with its own layout no
longer gets overridden when an unrelated style setting changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
$this->assertStringNotContainsString( '#frm_style_preview .with_frm_style .frm_radio {', $css, 'An unrelated setting must not override a radio layout exception.' );
$this->assertStringNotContainsString( '#frm_style_preview .with_frm_style .frm_checkbox {', $css, 'An unrelated setting must not override a checkbox layout exception.' );
$this->assertStringContainsString( '.frm-default-option-align .frm_radio', $css, 'Inherited radio layouts should still follow the style.' );
$this->assertStringContainsString( '.frm-default-option-align .frm_checkbox', $css, 'Inherited checkbox layouts should still follow the style.' );

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Call to an undefined method test_FrmStylesPreviewAlignment::assertStringContainsString()


The method you are trying to call is not defined, which can result in a fatal error.

$this->assertStringNotContainsString( '#frm_style_preview .with_frm_style .frm_checkbox {', $css, 'An unrelated setting must not override a checkbox layout exception.' );
$this->assertStringContainsString( '.frm-default-option-align .frm_radio', $css, 'Inherited radio layouts should still follow the style.' );
$this->assertStringContainsString( '.frm-default-option-align .frm_checkbox', $css, 'Inherited checkbox layouts should still follow the style.' );
$this->assertStringContainsString( '#frm_sample_form .with_frm_style .frm_radio', $css, 'The sample form should still preview layout changes.' );

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Call to an undefined method test_FrmStylesPreviewAlignment::assertStringContainsString()


The method you are trying to call is not defined, which can result in a fatal error.

);
$css = ob_get_clean();
$this->assertStringNotContainsString( '#frm_style_preview .with_frm_style .frm_radio {', $css, 'An unrelated setting must not override a radio layout exception.' );
$this->assertStringNotContainsString( '#frm_style_preview .with_frm_style .frm_checkbox {', $css, 'An unrelated setting must not override a checkbox layout exception.' );

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Call to an undefined method test_FrmStylesPreviewAlignment::assertStringNotContainsString()


The method you are trying to call is not defined, which can result in a fatal error.

$css = ob_get_clean();
$this->assertStringNotContainsString( '#frm_style_preview .with_frm_style .frm_radio {', $css, 'An unrelated setting must not override a radio layout exception.' );
$this->assertStringNotContainsString( '#frm_style_preview .with_frm_style .frm_checkbox {', $css, 'An unrelated setting must not override a checkbox layout exception.' );
$this->assertStringContainsString( '.frm-default-option-align .frm_radio', $css, 'Inherited radio layouts should still follow the style.' );

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Call to an undefined method test_FrmStylesPreviewAlignment::assertStringContainsString()


The method you are trying to call is not defined, which can result in a fatal error.

true
);
$css = ob_get_clean();
$this->assertStringNotContainsString( '#frm_style_preview .with_frm_style .frm_radio {', $css, 'An unrelated setting must not override a radio layout exception.' );

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Call to an undefined method test_FrmStylesPreviewAlignment::assertStringNotContainsString()


The method you are trying to call is not defined, which can result in a fatal error.

@Crabcyborg Crabcyborg modified the milestones: 7.1, 7.2 Nov 16, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant