commit b59df383b07ba23db8a6767af402b9e905f4e902
parent c8c81f28ea05d4c06c62730bb1ca15d1a9030bae
Author: Emilio Cobos Álvarez <emilio@crisal.io>
Date: Sat, 4 Oct 2025 19:37:22 +0000
Bug 1992551 - Simplify text editor value getter. r=smaug
Textarea no longer creates newlines in the middle (it only has a
trailing newline for caret positioning purposes), so this is equivalent
but avoids going through nsDocumentEncoder, nsPlainTextSerializer and so
on...
Differential Revision: https://phabricator.services.mozilla.com/D267509
Diffstat:
3 files changed, 21 insertions(+), 39 deletions(-)
diff --git a/dom/html/TextControlState.cpp b/dom/html/TextControlState.cpp
@@ -2468,26 +2468,8 @@ void TextControlState::GetValue(nsAString& aValue, bool aForDisplay) const {
}
aValue.Truncate(); // initialize out param
-
- uint32_t flags = nsIDocumentEncoder::OutputLFLineBreak |
- nsIDocumentEncoder::OutputPreformatted |
- nsIDocumentEncoder::OutputPersistNBSP |
- nsIDocumentEncoder::OutputBodyOnly;
- // What follows is a bit of a hack. The problem is that we could be in
- // this method because we're being destroyed for whatever reason while
- // script is executing. If that happens, editor will run with the
- // privileges of the executing script, which means it may not be able to
- // access its own DOM nodes! Let's try to deal with that by pushing a null
- // JSContext on the JSContext stack to make it clear that we're native
- // code. Note that any script that's directly trying to access our value
- // has to be going through some scriptable object to do that and that
- // already does the relevant security checks.
- // XXXbz if we could just get the textContent of our anonymous content (eg
- // if plaintext editor didn't create <br> nodes all over), we wouldn't need
- // this.
if (mEditorInitialized) {
- AutoNoJSAPI nojsapi;
- DebugOnly<nsresult> rv = mTextEditor->ComputeTextValue(flags, aValue);
+ DebugOnly<nsresult> rv = mTextEditor->ComputeTextValue(aValue);
MOZ_ASSERT(aValue.FindChar(u'\r') == -1);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to get value");
}
@@ -2829,7 +2811,7 @@ bool TextControlState::SetValueWithTextEditor(
IMEContentObserver* observer = GetIMEContentObserver();
if (observer && observer->WasInitializedWith(*textEditor)) {
nsAutoString currentValue;
- textEditor->ComputeTextValue(0, currentValue);
+ textEditor->ComputeTextValue(currentValue);
observer->OnTextControlValueChangedWhileNotObservable(currentValue);
}
}
diff --git a/editor/libeditor/TextEditor.cpp b/editor/libeditor/TextEditor.cpp
@@ -372,6 +372,22 @@ NS_IMETHODIMP TextEditor::InsertLineBreak() {
return EditorBase::ToGenericNSResult(rv);
}
+nsresult TextEditor::ComputeTextValue(nsAString& aString) const {
+ Element* anonymousDivElement = GetRoot();
+ if (NS_WARN_IF(!anonymousDivElement)) {
+ return NS_ERROR_NOT_INITIALIZED;
+ }
+
+ auto* text = Text::FromNodeOrNull(anonymousDivElement->GetFirstChild());
+ if (MOZ_UNLIKELY(!text)) {
+ MOZ_ASSERT_UNREACHABLE("how?");
+ return NS_ERROR_UNEXPECTED;
+ }
+
+ text->GetData(aString);
+ return NS_OK;
+}
+
nsresult TextEditor::InsertLineBreakAsAction(nsIPrincipal* aPrincipal) {
AutoEditActionDataSetter editActionData(*this, EditAction::eInsertLineBreak,
aPrincipal);
diff --git a/editor/libeditor/TextEditor.h b/editor/libeditor/TextEditor.h
@@ -183,25 +183,9 @@ class TextEditor final : public EditorBase,
InsertLineBreakAsAction(nsIPrincipal* aPrincipal = nullptr) final;
/**
- * ComputeTextValue() computes plaintext value of this editor. This may be
- * too expensive if it's in hot path.
- *
- * @param aDocumentEncoderFlags Flags of nsIDocumentEncoder.
- * @param aCharset Encoding of the document.
- */
- nsresult ComputeTextValue(uint32_t aDocumentEncoderFlags,
- nsAString& aOutputString) const {
- AutoEditActionDataSetter editActionData(*this, EditAction::eNotEditing);
- if (NS_WARN_IF(!editActionData.CanHandle())) {
- return NS_ERROR_NOT_INITIALIZED;
- }
- nsresult rv = ComputeValueInternal(u"text/plain"_ns, aDocumentEncoderFlags,
- aOutputString);
- if (NS_WARN_IF(NS_FAILED(rv))) {
- return EditorBase::ToGenericNSResult(rv);
- }
- return NS_OK;
- }
+ * ComputeTextValue() computes plaintext value of this editor.
+ */
+ nsresult ComputeTextValue(nsAString&) const;
/**
* The following methods are available only when the instance is a password