Skip to content

fix: Support django CMS 5 data bridge for text-enabled plugins #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 15, 2025

Conversation

fsbraun
Copy link
Member

@fsbraun fsbraun commented May 13, 2025

This PR fixes #86 :

django CMS 5's data bridge (the data connection between JS frontend editor and the cms app) has a new format. Now, djangocms-text accepts django CMS 3.11, 4.1, and 5.0 data bridge formats for both inline editing and adding text-enabled plugins.

Summary by Sourcery

Add compatibility with django CMS 5 data bridge formats and refactor JS and Python code to streamline data bridge handling and HTML sanitization, including deprecation of the clean_html full flag and test adjustments.

New Features:

  • Support Django CMS 5 data bridge format for inline editing and plugin additions

Bug Fixes:

  • Correct template variable lookup in cms_plugins.get_plugins
  • Trigger full page reload when no data bridge is found

Enhancements:

  • Refactor CMS editor JS by extracting data bridge parsing into a new processDataBridge method
  • Make clean_html's full parameter optional and emit deprecation warning only when provided

Tests:

  • Remove deprecated full=False arguments in clean_html calls across tests
  • Clean up unused lines in widget test

Copy link
Contributor

sourcery-ai bot commented May 13, 2025

Reviewer's Guide

Centralize and standardize Django CMS data bridge parsing to support CMS 5, refactor the clean_html API by deprecating the full parameter and update all callers and tests accordingly, and fix a plugin template reference bug with minor test cleanup.

File-Level Changes

Change Details Files
Centralized CMS data bridge parsing into a reusable method
  • Extract inline data-bridge parsing logic into processDataBridge()
  • Replace ad-hoc parsing and reload logic with a single handler
  • Ensure missing bridges result in empty object and conditional reload
  • Apply unified parsing in both inline editing and addPluginForm
private/js/cms.editor.js
Refactored clean_html signature to deprecate the full flag
  • Changed full parameter default to None (Optional[bool])
  • Emit DeprecationWarning only when full is explicitly provided
djangocms_text/html.py
Updated all clean_html callers and tests to drop full=False
  • Removed full=False arguments in field and model clean paths
  • Eliminated full parameter from test_html calls
  • Adapted tests to pass without the deprecated flag
djangocms_text/fields.py
djangocms_text/models.py
tests/test_html.py
Fixed get_plugins template lookup
  • Replaced self.page.template with page.template
djangocms_text/cms_plugins.py
Minor test formatting cleanup
  • Removed an extraneous blank line in test_widget
tests/test_widget.py

Assessment against linked issues

Issue Objective Addressed Explanation
#86 Fix the error Uncaught TypeError: Cannot read properties of undefined (reading 'plugin_id') at CmsDialog.saveSuccess (plugin.js:320:1) at CmsDialog._dialogSaved (cms.dialog.js:161:1) at cms.dialog.js:121:1 at cms.editor.js:562:1 when adding an image through the CMS Plugins tab in the Text Editor.
#86 Ensure compatibility with django CMS 5's new data bridge format for inline text editing and plugin addition.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

codecov bot commented May 13, 2025

Codecov Report

Attention: Patch coverage is 60.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 81.61%. Comparing base (ffa847f) to head (3b197de).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
djangocms_text/html.py 33.33% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #87      +/-   ##
==========================================
- Coverage   81.80%   81.61%   -0.20%     
==========================================
  Files          14       14              
  Lines         940      941       +1     
  Branches      109      110       +1     
==========================================
- Hits          769      768       -1     
- Misses        129      130       +1     
- Partials       42       43       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@fsbraun fsbraun marked this pull request as ready for review May 14, 2025 22:19
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @fsbraun - I've reviewed your changes and found some issues that need to be addressed.

Blocking issues:

  • Undefined variable body in processDataBridge (link)

  • In processDataBridge you’re using an undefined body variable for the regex—make sure to pass in or derive the raw HTML string rather than referencing body.

  • Remove the console.log calls in processDataBridge once debugging is complete to avoid cluttering the console.

  • The clean_html docstring still refers to full: bool = False; please update it to reflect the new Optional[bool] signature and behavior.

Here's what I looked at during the review
  • 🔴 General issues: 1 blocking issue, 1 other issue
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

if (!this.CMS.API.Helpers.dataBridge) {
// No databridge found
this.CMS.API.Helpers.reloadBrowser('REFRESH_PAGE');
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): Undefined variable body in processDataBridge

Pass the HTML string (e.g., dom.innerHTML) into processDataBridge or rename the variable so that body is defined.

this.CMS.API.Helpers.reloadBrowser('REFRESH_PAGE');
return;
}
this.processDataBridge(dom);
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): dataBridge fallback sets empty object, preventing reload

Assigning {} makes dataBridge truthy, so the if (!dataBridge) check never fires. Return a success flag or set dataBridge to null/undefined on failure to allow the reload path to run.

@@ -497,6 +486,29 @@
}
}

processDataBridge(dom) {
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (complexity): Consider refactoring the dataBridge extraction logic into a single helper function that handles all cases, removes duplication, and restores immediate reload on failure.

Here’s one way to collapse the three “find‐then‐fallback” branches, kill the duplicated regex, remove the stray logs, and restore the original “reload on missing bridge” behavior—all while keeping every code‐path intact:

// call site
el.dataset.changed = 'false';
const dataBridge = this._fetchDataBridge(dom);
if (!dataBridge) {
  this.CMS.API.Helpers.reloadBrowser('REFRESH_PAGE');
  return;
}
delete dataBridge.structure?.content;
this.CMS.API.Helpers.dataBridge = dataBridge;
// new helper
_fetchDataBridge(dom, body = dom.innerHTML) {
  // 1) inline JSON <script id="data-bridge">…</script>
  const script = dom.querySelector('script#data-bridge');
  const txt    = script?.textContent?.trim();
  if (txt) {
    return JSON.parse(txt);
  }

  // 2) legacy JS assignments in page HTML
  const rx1 = /^\s*Window\.CMS\.API\.Helpers\.dataBridge\s*=\s*(.*?);/gm.exec(body);
  const rx2 = /^\s*Window\.CMS\.API\.Helpers\.dataBridge\.structure\s*=\s*(.*?);/gm.exec(body);
  if (rx1 && rx2) {
    const bridge = JSON.parse(rx1[1]);
    bridge.structure = JSON.parse(rx2[1]);
    return bridge;
  }

  // 3) nothing found
  return null;
}

—This

  • inlines your two regexes into one fall-through block
  • returns null on failure so the caller can reload immediately
  • removes all console.log noise
  • keeps the delete structure.content step right after you’ve parsed
  • never relies on the mysterious undefined body—defaults to dom.innerHTML but can be overridden (e.g. in the form callback)

@fsbraun fsbraun merged commit df1b635 into main May 15, 2025
41 of 52 checks passed
@fsbraun fsbraun deleted the fix/dcms-5 branch May 15, 2025 21:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't add image from inside CMS Plugins tabs on Text Editor
1 participant