Properly escape HTML in every Scribe actions
In recent commits I separated how we could add text in the editor. So we need to escape HTML in both method that add text: insertTextInEditor and setTextInEditor. I had to extract the HTML escape part of convertTextContentToHtmlContent because when using setTextInEditor we do not need to convert \n to <br>.
This commit is contained in:
@@ -251,16 +251,14 @@ class StringConvert {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static String convertTextContentToHtmlContent(String textContent) {
|
static String escapeTextContent(String textContent) {
|
||||||
// Escape HTML entities first to prevent interpretation as HTML
|
const HtmlEscape htmlEscape = HtmlEscape();
|
||||||
final escapedContent = textContent
|
|
||||||
.replaceAll('&', '&')
|
|
||||||
.replaceAll('<', '<')
|
|
||||||
.replaceAll('>', '>')
|
|
||||||
.replaceAll('"', '"')
|
|
||||||
.replaceAll("'", ''');
|
|
||||||
|
|
||||||
final htmlContent = escapedContent.replaceAll('\n', '<br>');
|
return htmlEscape.convert(textContent);
|
||||||
|
}
|
||||||
|
|
||||||
|
static String convertTextContentToHtmlContent(String textContent) {
|
||||||
|
final htmlContent = textContent.replaceAll('\n', '<br>');
|
||||||
|
|
||||||
return '<div>$htmlContent</div>';
|
return '<div>$htmlContent</div>';
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-4
@@ -36,7 +36,8 @@ extension HandleAiScribeInComposerExtension on ComposerController {
|
|||||||
|
|
||||||
Future<void> insertTextInEditor(String text) async {
|
Future<void> insertTextInEditor(String text) async {
|
||||||
try {
|
try {
|
||||||
final htmlContent = StringConvert.convertTextContentToHtmlContent(text);
|
final escapedText = StringConvert.escapeTextContent(text);
|
||||||
|
final htmlContent = StringConvert.convertTextContentToHtmlContent(escapedText);
|
||||||
|
|
||||||
if (PlatformInfo.isWeb) {
|
if (PlatformInfo.isWeb) {
|
||||||
await richTextWebController?.editorController.evaluateJavascriptWeb(
|
await richTextWebController?.editorController.evaluateJavascriptWeb(
|
||||||
@@ -55,13 +56,15 @@ extension HandleAiScribeInComposerExtension on ComposerController {
|
|||||||
|
|
||||||
Future<void> setTextInEditor(String text) async {
|
Future<void> setTextInEditor(String text) async {
|
||||||
try {
|
try {
|
||||||
|
final escapedText = StringConvert.escapeTextContent(text);
|
||||||
|
|
||||||
if (PlatformInfo.isWeb) {
|
if (PlatformInfo.isWeb) {
|
||||||
richTextWebController?.editorController.setText(text);
|
richTextWebController?.editorController.setText(escapedText);
|
||||||
} else {
|
} else {
|
||||||
await richTextMobileTabletController?.htmlEditorApi?.setText(text);
|
await richTextMobileTabletController?.htmlEditorApi?.setText(escapedText);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logWarning('$runtimeType::insertTextInEditor:Exception = $e');
|
logWarning('$runtimeType::setTextInEditor:Exception = $e');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user