feat: Prevent empty wrapper divs for about sections with no content (#37551)

This commit is contained in:
Peter Kulko
2025-10-29 16:52:21 +02:00
committed by GitHub
parent 18d5abb2f6
commit 2a473cffd3
2 changed files with 19 additions and 1 deletions

View File

@@ -53,6 +53,7 @@ from lms.djangoapps.courseware.exceptions import CourseAccessRedirect, CourseRun
from lms.djangoapps.courseware.masquerade import check_content_start_date_for_masquerade_user
from lms.djangoapps.courseware.model_data import FieldDataCache
from lms.djangoapps.courseware.block_render import get_block
from lms.djangoapps.courseware.utils import is_empty_html
from lms.djangoapps.grades.api import CourseGradeFactory
from lms.djangoapps.survey.utils import SurveyRequiredAccessError, check_survey_required_and_unanswered
from openedx.core.djangoapps.content.block_structure.api import get_block_structure_manager
@@ -418,7 +419,10 @@ def get_course_about_section(request, course, section_key):
if about_block is not None:
try:
html = about_block.render(STUDENT_VIEW).content
# Only render XBlock if content exists to avoid generating empty wrapper divs
content = about_block.data
if not is_empty_html(content):
html = about_block.render(STUDENT_VIEW).content
except Exception: # pylint: disable=broad-except
html = render_to_string('courseware/error-message.html', None)
log.exception(

View File

@@ -4,6 +4,7 @@
import datetime
import hashlib
import logging
from bs4 import BeautifulSoup
from django.conf import settings
from django.http import HttpResponse, HttpResponseBadRequest
@@ -229,3 +230,16 @@ def _use_new_financial_assistance_flow(course_id):
):
return True
return False
def is_empty_html(html_content):
"""
Check if HTML content is effectively empty.
"""
if not html_content:
return True
soup = BeautifulSoup(html_content, 'html.parser')
text = soup.get_text(strip=True)
return not text