Skip to content

body.class breaks media query selector due to missing .rcmBody container #9911

Description

@hiegova

Prerequisites

  • I have searched for duplicate or closed issues
  • I can recreate the issue with all plugins disabled

Describe the issue

Roundcube's HTML email sanitization process shows inconsistent behavior when rewriting CSS selectors, specifically when the <body> element has a class attribute. This results in broken styles and malformed HTML structure.

Expected behavior

CSS selectors should be consistently rewritten and the HTML structure should remain valid regardless of whether classes are applied to <p> elements or <body> elements.

Actual behavior

When a <body> element has a class attribute, Roundcube:

  1. Fails to include the rcmBody wrapper div that should be present according to the CSS selector rewriting pattern
  2. Creates duplicate IDs (id="message-htmlpart1" appears twice in the DOM)
  3. Generates CSS selectors that don't match the actual DOM structure

Steps to reproduce

Test Case 1 (Working - class on <p> element):

Input HTML:

<!DOCTYPE html>
<html lang="en">
  <head>
    <style>
      @media (min-width: 600px) {
        .p_class_name {
          color: red;
        }
      }
    </style>
  </head>
  <body>
    <p class="p_class_name">Working CSS selector</p>
  </body>
</html>

Output (Working correctly):

<div id="messagebody">
  <div class="message-htmlpart" id="message-htmlpart1">
    <!-- html ignored --><!-- head ignored --><!-- meta ignored -->
    <style type="text/css">
      @media (min-width: 600px) {
        #message-htmlpart1 div.rcmBody .v1p_class_name {
          color: red;
        }
      }
    </style>

    <div class="rcmBody" id="message-htmlpart1">
      <p class="v1p_class_name">Working CSS selector</p>
    </div>
  </div>
</div>

Test Case 2 (Broken - class on <body> element):

Input HTML:

<!DOCTYPE html>
<html lang="en">
  <head>
    <style>
      @media (min-width: 600px) {
        .body_class_name {
          color: red;
        }
      }
    </style>
  </head>
  <body class="body_class_name">
    <p>Broken CSS selector</p>
  </body>
</html>

Output (Broken):

<div id="messagebody">
  <div class="message-htmlpart" id="message-htmlpart1">
    <!-- html ignored --><!-- head ignored --><!-- meta ignored -->
    <style type="text/css">
      @media (min-width: 600px) {
        #message-htmlpart1 div.rcmBody .v1body_class_name {
          color: red;
        }
      }
    </style>

    <div class="v1body_class_name" id="message-htmlpart1">
      <p>Broken CSS selector</p>
    </div>
  </div>
</div>

Issues identified

  1. Missing rcmBody wrapper: In the broken case, the <div class="rcmBody"> wrapper is missing, but the CSS selector still references div.rcmBody
  2. Duplicate IDs: Both the outer container and inner div have id="message-htmlpart1", which violates HTML standards
  3. CSS selector mismatch: The generated CSS selector #message-htmlpart1 div.rcmBody .v1body_class_name cannot match the actual DOM structure <div id="message-htmlpart1"><div class="v1body_class_name"> because there's no element with class rcmBody

Impact

This bug breaks email styling for HTML emails that apply CSS classes to the <body> element, which is a common pattern in responsive email design and email templates.

What browser(s) are you seeing the problem on?

Chrome

What version of PHP are you using?

v8.4.8

What version of Roundcube are you using?

v1.6.11

JavaScript errors

No response

PHP errors

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions