Patch #44621 ยป 0001-Hide-Mermaid-code-blocks-until-they-are-rendered-to-.patch
| app/assets/stylesheets/application.css | ||
|---|---|---|
|
position: relative;
|
||
|
}
|
||
|
/* Hide Mermaid code blocks until they are rendered so that their source does not flash.
|
||
|
visibility keeps their space so that the content below does not jump twice.
|
||
|
The mermaid Stimulus controller adds mermaid-failed to show the source again on failure. */
|
||
|
html.mermaid-available pre:has(> code[data-controller="mermaid"]):not(.mermaid-failed) {
|
||
|
visibility: hidden;
|
||
|
}
|
||
|
div.wiki pre {
|
||
|
margin-block: 1em;
|
||
|
margin-inline: 1.6em 1em;
|
||
| app/helpers/application_helper.rb | ||
|---|---|---|
|
# several MB, so it is not part of every page load. When it is available,
|
||
|
# its URL is exposed here and the mermaid Stimulus controller loads it
|
||
|
# only when a Mermaid code block is actually present on the page.
|
||
|
# The mermaid-available class hides Mermaid code blocks from the first
|
||
|
# paint so that their source does not flash before the diagram replaces
|
||
|
# it. It is set by JavaScript so that the blocks stay visible without it.
|
||
|
if Redmine::Mermaid.available?
|
||
|
tags <<
|
||
|
javascript_tag(
|
||
|
"window.MermaidAssetUrl = #{asset_path(Redmine::Mermaid::FILENAME).to_json};"
|
||
|
"window.MermaidAssetUrl = #{asset_path(Redmine::Mermaid::FILENAME).to_json};" \
|
||
|
"document.documentElement.classList.add('mermaid-available');"
|
||
|
)
|
||
|
end
|
||
|
unless User.current.pref.warn_on_leaving_unsaved == '0'
|
||
| app/javascript/controllers/mermaid_controller.js | ||
|---|---|---|
|
if (this.element.isConnected) this.render(mermaid);
|
||
|
}).catch((error) => {
|
||
|
console.error('Failed to load Mermaid.js:', error);
|
||
|
// Show the source that the mermaid-available CSS rule hides.
|
||
|
this.element.closest('pre')?.classList.add('mermaid-failed');
|
||
|
});
|
||
|
}
|
||
| ... | ... | |
|
this.errorElement?.remove();
|
||
|
this.container = this.errorElement = null;
|
||
|
const pre = this.element.closest('pre');
|
||
|
if (pre) pre.style.display = '';
|
||
|
if (pre) {
|
||
|
pre.style.display = '';
|
||
|
pre.classList.remove('mermaid-failed');
|
||
|
}
|
||
|
}
|
||
|
render(mermaid) {
|
||
| ... | ... | |
|
const container = document.createElement('div');
|
||
|
container.className = 'mermaid';
|
||
|
container.textContent = this.element.textContent.trim();
|
||
|
// Keep the source text in the container from flashing until Mermaid.js
|
||
|
// replaces it with the diagram. Mermaid.js measures the diagram inside
|
||
|
// the container, so it is made invisible rather than display: none, and
|
||
|
// taken out of the flow so that only the hidden <pre> reserves space.
|
||
|
container.style.visibility = 'hidden';
|
||
|
container.style.position = 'absolute';
|
||
|
container.style.insetInline = '0';
|
||
|
pre.insertAdjacentElement('afterend', container);
|
||
|
this.container = container;
|
||
| ... | ... | |
|
// visible, so that the invalid diagram can be inspected and copied.
|
||
|
mermaid.run({ nodes: [container], suppressErrors: false }).then(() => {
|
||
|
pre.style.display = 'none';
|
||
|
container.style.visibility = container.style.position = container.style.insetInline = '';
|
||
|
}).catch((error) => {
|
||
|
container.remove();
|
||
|
if (this.container === container) this.container = null;
|
||
|
pre.classList.add('mermaid-failed');
|
||
|
const errorElement = document.createElement('div');
|
||
|
errorElement.className = 'flash error';
|
||
| test/helpers/application_helper_test.rb | ||
|---|---|---|
|
assert_not_include 'MermaidAssetUrl', javascript_heads
|
||
|
end
|
||
|
def test_javascript_heads_should_add_mermaid_available_class_when_mermaid_is_available
|
||
|
Redmine::Mermaid.stubs(:available?).returns(true)
|
||
|
assert_include "document.documentElement.classList.add('mermaid-available');", javascript_heads
|
||
|
end
|
||
|
def test_favicon_path
|
||
|
assert_match %r{^/assets/favicon-\w+\.ico}, favicon_path
|
||
|
end
|
||
| test/system/mermaid_rendering_test.rb | ||
|---|---|---|
|
end
|
||
|
end
|
||
|
def test_should_show_mermaid_source_when_mermaid_js_fails_to_load
|
||
|
skip unless mermaid_installed?
|
||
|
log_user('jsmith', 'jsmith')
|
||
|
visit '/projects/ecookbook/issues/new'
|
||
|
# The source of Mermaid code blocks is hidden until rendered; it must be
|
||
|
# shown again if Mermaid.js cannot be loaded.
|
||
|
page.execute_script("window.MermaidAssetUrl = '/nonexistent-mermaid.js';")
|
||
|
within('form#issue-form') do
|
||
|
fill_in 'Subject', :with => 'mermaid load failure test'
|
||
|
fill_in 'Description', :with => "```mermaid\ngraph TD;\nA-->B;\n```"
|
||
|
click_link 'Preview'
|
||
|
within('div.wiki-preview') do
|
||
|
assert_selector 'pre.mermaid-failed', text: 'graph TD;'
|
||
|
assert_no_selector 'div.mermaid'
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
def test_should_not_execute_script_from_malicious_mermaid_syntax
|
||
|
skip unless mermaid_installed?
|
||