Skip to content

Commit 33978ae

Browse files
committed
Properly render the news preview in both dark and lightmode.
This requires us to render the email template in an iframe to prevent it's styles leaking out into the main page.
1 parent 8babc8e commit 33978ae

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

media/css/main.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,14 @@ details.release-notes-list {
18511851
max-width: 650px;
18521852
}
18531853

1854+
.moderation-preview-iframe {
1855+
width: 100%;
1856+
min-height: 400px;
1857+
border: 1px solid var(--button-input-bdr-color);
1858+
border-radius: 5px;
1859+
background-color: #fff;
1860+
}
1861+
18541862
/* Buttons that are images */
18551863
button.imagebutton {
18561864
border: 0;

media/js/moderation_preview.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Moderation preview handler
3+
* Renders HTML content in iframes to isolate styles from the main page
4+
*/
5+
document.addEventListener('DOMContentLoaded', function() {
6+
/* Find preview data textareas and render them in iframes */
7+
var dataAreas = document.getElementsByClassName('moderation-preview-data');
8+
for (var i = 0; i < dataAreas.length; i++) {
9+
var dataArea = dataAreas[i];
10+
var container = dataArea.parentElement;
11+
var content = dataArea.value;
12+
13+
/* Create an iframe to isolate the HTML content styles */
14+
var iframe = document.createElement('iframe');
15+
iframe.className = 'moderation-preview-iframe';
16+
iframe.sandbox = 'allow-same-origin';
17+
iframe.srcdoc = content;
18+
19+
/* Resize iframe to fit content after it loads */
20+
iframe.onload = function() {
21+
try {
22+
var contentHeight = this.contentDocument.body.scrollHeight;
23+
if (contentHeight > 0) {
24+
this.style.height = (contentHeight + 20) + 'px';
25+
}
26+
} catch (e) {
27+
/* Only ignore SecurityError/cross-origin errors, rethrow others */
28+
if (e.name !== 'SecurityError') {
29+
throw e;
30+
}
31+
}
32+
};
33+
34+
container.appendChild(iframe);
35+
}
36+
});

templates/account/submit_preview.html

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,24 @@ <h2>Your {{objtype}}</h2>
1717
{%for fld, title, contents, mdcontents, note in preview %}
1818
<div class="row">
1919
<div class="col-sm-2 col-form-label"><strong>{{title}}</strong></div>
20-
<div class="col-sm-10">{%if mdcontents%}<div class="moderation-mdpreview-wrap">{{mdcontents|safe}}</div>{%else%}{{contents}}{%endif%}</div>
20+
<div class="col-sm-10">
21+
{%if mdcontents%}
22+
<div class="moderation-mdpreview-wrap moderation-preview-container">
23+
{# Use hidden textarea to store raw HTML without browser parsing #}
24+
<textarea class="moderation-preview-data" style="display:none;">{{mdcontents}}</textarea>
25+
</div>
26+
{%else%}
27+
{{contents}}
28+
{%endif%}
29+
</div>
2130
</div>
2231
{%endfor%}
2332

2433
<h2>Confirm</h2>
2534
{%include "base/form_contents.html" with savebutton="Submit for moderation"%}
2635

2736
{%endblock%}
37+
38+
{%block extrascript%}
39+
<script src="/media/js/moderation_preview.js?{{gitrev}}"></script>
40+
{%endblock%}

0 commit comments

Comments
 (0)