Skip to content

Commit e0c9f2f

Browse files
committed
MBS-10672: Format agent formelements with Markdown and add unit tests
- Add format_text() for label and explanation fields in format_output() - Preserve newValue unchanged for form field injection - Refactor test structure with setUp() method - Add comprehensive unit tests with data providers: - Label/explanation Markdown formatting - newValue preservation (HTML, scripts, code) - chatoutput intro/outro formatting - Invalid input handling - Missing fields handling - Security: script tag sanitization in chatoutput
1 parent b006a9e commit e0c9f2f

File tree

2 files changed

+822
-21
lines changed

2 files changed

+822
-21
lines changed

purposes/agent/classes/purpose.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,26 @@ public function format_output(string $output): string {
217217
return $erroroutput;
218218
}
219219

220+
// Format formelements text fields to prevent HTML/Markdown from breaking the layout.
221+
foreach ($outputrecord['formelements'] as $key => $formelement) {
222+
if (isset($formelement['label'])) {
223+
$outputrecord['formelements'][$key]['label'] = format_text(
224+
$formelement['label'],
225+
FORMAT_MARKDOWN,
226+
['filter' => false]
227+
);
228+
}
229+
if (isset($formelement['explanation'])) {
230+
$outputrecord['formelements'][$key]['explanation'] = format_text(
231+
$formelement['explanation'],
232+
FORMAT_MARKDOWN,
233+
['filter' => false]
234+
);
235+
}
236+
// Note: newValue is intentionally NOT formatted as it needs to be injected into form fields as-is.
237+
// The display value (suggestiondisplayvalue) is handled separately in the frontend template with proper escaping.
238+
}
239+
220240
// Checking the correct structure of chat output.
221241
$outputrecord['chatoutput'] = $this->validate_chatoutput($outputrecord['chatoutput']);
222242
foreach ($outputrecord['chatoutput'] as $key => $outputobject) {

0 commit comments

Comments
 (0)