Skip to content
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

Fix the restore boilerplate button #4528

Merged
merged 1 commit into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/assets/javascripts/exercise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,12 @@ function initExerciseShow(exerciseId: number, programmingLanguage: string, logge

const resetButton = restoreWarning.querySelector("a");
resetButton.addEventListener("click", () => {
editor.setValue(boilerplate);
// the boilerplate has been escaped, so we need to unescape it
const wrapper = document.createElement("div");
wrapper.innerHTML = boilerplate;
const rawBoilerplate = wrapper.textContent || wrapper.innerText || "";

editor.setValue(rawBoilerplate);
editor.focus();
restoreWarning.hidden = true;
});
Expand Down
2 changes: 1 addition & 1 deletion app/views/activities/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ end %>
<%= @course&.id || "null" %>,
<%= raw "\"#{@series&.deadline&.httpdate}\"" || "null" %>,
"<%= submissions_url %>",
`<%= (@activity.exercise? && raw(@activity.boilerplate&.gsub!('`', '\\\\`')))|| "" %>`,
`<%= (@activity.exercise? && @activity.boilerplate&.gsub('`', '\\\\`'))|| "" %>`,
);
});
</script>
2 changes: 1 addition & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = function (api) {
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-transform-for-of",
isTestEnv && "babel-plugin-dynamic-import-node",
isDevelopmentEnv && ["istanbul", { include: ["app/assets/javascripts/**/*.{js,ts}"] }],
isDevelopmentEnv && ["istanbul", { include: ["app/assets/javascripts/**/*.{js,ts}"], coverageGlobalScopeFunc: false, coverageGlobalScope: "window" }],
"@babel/plugin-transform-destructuring",
["@babel/plugin-proposal-class-properties", { loose: true }],
["@babel/plugin-proposal-decorators", { decoratorsBeforeExport: true }],
Expand Down
28 changes: 15 additions & 13 deletions test/system/activities_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,24 @@ class ActivitiesTest < ApplicationSystemTestCase
end

test 'should show message to restore boilerplate if latest submission is shown' do
@instance.stubs(:boilerplate).returns('boilerplate') do
create(:submission, exercise: @instance, user: @user, status: :correct, code: 'print("hello")')
visit exercise_path(id: @instance.id)
assert_text 'Restore boilerplate'
end
Exercise.any_instance.stubs(:boilerplate).returns('boilerplate')
create(:submission, exercise: @instance, user: @user, status: :correct, code: 'print("hello")')
visit exercise_path(id: @instance.id)
assert_text 'Restore the boilerplate code.'
find('a', text: 'Restore the boilerplate code.').click
assert_text 'boilerplate'
end

test 'should not break on complex unicode characters' do
@instance.stubs(:boilerplate).returns('`<script>alert("😀")</script>`') do
visit exercise_path(id: @instance.id)
assert_text '`<script>alert("😀")</script>`'
Exercise.any_instance.stubs(:boilerplate).returns('`<script>alert("😀")</script>`')
visit exercise_path(id: @instance.id)
assert_text '`<script>alert("😀")</script>`'

create(:submission, exercise: @instance, user: @user, status: :correct, code: 'print("😀")')
visit exercise_path(id: @instance.id)
assert_text 'print("😀")'
assert_text 'Restore boilerplate'
end
create(:submission, exercise: @instance, user: @user, status: :correct, code: 'print("😀")')
visit exercise_path(id: @instance.id)
assert_text 'print("😀")'
assert_text 'Restore the boilerplate code.'
find('a', text: 'Restore the boilerplate code.').click
assert_text '`<script>alert("😀")</script>`'
end
end