gecko-dev/dom/html/HTMLTextAreaElement.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

1161 строка
36 KiB
C++
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
2012-05-21 15:12:37 +04:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/HTMLTextAreaElement.h"
#include "mozAutoDocUpdate.h"
#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/Attributes.h"
#include "mozilla/dom/HTMLFormSubmission.h"
#include "mozilla/dom/HTMLTextAreaElementBinding.h"
#include "mozilla/EventDispatcher.h"
#include "mozilla/EventStates.h"
#include "mozilla/MappedDeclarations.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/PresState.h"
#include "mozilla/TextControlState.h"
#include "nsAttrValueInlines.h"
#include "nsBaseCommandController.h"
#include "nsContentCID.h"
#include "nsContentCreatorFunctions.h"
#include "nsError.h"
#include "nsFocusManager.h"
#include "nsIConstraintValidation.h"
#include "nsIControllers.h"
#include "mozilla/dom/Document.h"
#include "nsIFormControlFrame.h"
#include "nsIFormControl.h"
#include "nsIForm.h"
#include "nsIFrame.h"
#include "nsITextControlFrame.h"
#include "nsLayoutUtils.h"
#include "nsLinebreakConverter.h"
#include "nsMappedAttributes.h"
#include "nsPIDOMWindow.h"
#include "nsPresContext.h"
#include "nsReadableUtils.h"
#include "nsStyleConsts.h"
#include "nsBaseCommandController.h"
#include "nsXULControllers.h"
1998-09-03 05:03:33 +04:00
NS_IMPL_NS_NEW_HTML_ELEMENT_CHECK_PARSER(TextArea)
1998-09-03 05:03:33 +04:00
namespace mozilla {
namespace dom {
HTMLTextAreaElement::HTMLTextAreaElement(
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
FromParser aFromParser)
: TextControlElement(std::move(aNodeInfo), aFromParser, NS_FORM_TEXTAREA),
mValueChanged(false),
mLastValueChangeWasInteractive(false),
mHandlingSelect(false),
mDoneAddingChildren(!aFromParser),
mInhibitStateRestoration(!!(aFromParser & FROM_PARSER_FRAGMENT)),
mDisabledChanged(false),
mCanShowInvalidUI(true),
mCanShowValidUI(true),
mIsPreviewEnabled(false),
mAutocompleteAttrState(nsContentUtils::eAutocompleteAttrState_Unknown),
mState(TextControlState::Construct(this)) {
2007-01-30 08:48:22 +03:00
AddMutationObserver(this);
// Set up our default state. By default we're enabled (since we're
// a control type that can be disabled but not actually disabled
// right now), optional, and valid. We are NOT readwrite by default
// until someone calls UpdateEditableState on us, apparently! Also
// by default we don't have to show validity UI and so forth.
AddStatesSilently(NS_EVENT_STATE_ENABLED | NS_EVENT_STATE_OPTIONAL |
NS_EVENT_STATE_VALID);
1998-09-03 05:03:33 +04:00
}
HTMLTextAreaElement::~HTMLTextAreaElement() {
mState->Destroy();
mState = nullptr;
}
NS_IMPL_CYCLE_COLLECTION_CLASS(HTMLTextAreaElement)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(HTMLTextAreaElement,
TextControlElement)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mValidity)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mControllers)
if (tmp->mState) {
tmp->mState->Traverse(cb);
}
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(HTMLTextAreaElement,
TextControlElement)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mValidity)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mControllers)
if (tmp->mState) {
tmp->mState->Unlink();
}
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED(HTMLTextAreaElement,
TextControlElement,
nsIMutationObserver,
nsIConstraintValidation)
1998-09-03 05:03:33 +04:00
// nsIDOMHTMLTextAreaElement
nsresult HTMLTextAreaElement::Clone(dom::NodeInfo* aNodeInfo,
nsINode** aResult) const {
*aResult = nullptr;
RefPtr<HTMLTextAreaElement> it =
new HTMLTextAreaElement(do_AddRef(aNodeInfo));
nsresult rv = const_cast<HTMLTextAreaElement*>(this)->CopyInnerTo(it);
NS_ENSURE_SUCCESS(rv, rv);
if (mValueChanged) {
// Set our value on the clone.
nsAutoString value;
GetValueInternal(value, true);
// SetValueInternal handles setting mValueChanged for us
rv = it->SetValueInternal(value, TextControlState::eSetValue_Notify);
NS_ENSURE_SUCCESS(rv, rv);
}
it->mLastValueChangeWasInteractive = mLastValueChangeWasInteractive;
it.forget(aResult);
return NS_OK;
}
// nsIContent
void HTMLTextAreaElement::Select() {
// XXX Bug? We have to give the input focus before contents can be
// selected
FocusTristate state = FocusState();
if (state == eUnfocusable) {
return;
}
nsFocusManager* fm = nsFocusManager::GetFocusManager();
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<nsPresContext> presContext = GetPresContext(eForComposedDoc);
if (state == eInactiveWindow) {
if (fm) fm->SetFocus(this, nsIFocusManager::FLAG_NOSCROLL);
SelectAll(presContext);
return;
}
nsEventStatus status = nsEventStatus_eIgnore;
WidgetGUIEvent event(true, eFormSelect, nullptr);
// XXXbz HTMLInputElement guards against this reentering; shouldn't we?
EventDispatcher::Dispatch(static_cast<nsIContent*>(this), presContext, &event,
nullptr, &status);
// If the DOM event was not canceled (e.g. by a JS event handler
// returning false)
if (status == nsEventStatus_eIgnore) {
if (fm) {
fm->SetFocus(this, nsIFocusManager::FLAG_NOSCROLL);
// ensure that the element is actually focused
if (this == fm->GetFocusedElement()) {
// Now Select all the text!
SelectAll(presContext);
}
}
}
}
NS_IMETHODIMP
HTMLTextAreaElement::SelectAll(nsPresContext* aPresContext) {
nsIFormControlFrame* formControlFrame = GetFormControlFrame(true);
if (formControlFrame) {
formControlFrame->SetFormProperty(nsGkAtoms::select, EmptyString());
}
return NS_OK;
1998-09-03 05:03:33 +04:00
}
bool HTMLTextAreaElement::IsHTMLFocusable(bool aWithMouse, bool* aIsFocusable,
int32_t* aTabIndex) {
if (nsGenericHTMLFormElementWithState::IsHTMLFocusable(
aWithMouse, aIsFocusable, aTabIndex)) {
return true;
}
// disabled textareas are not focusable
*aIsFocusable = !IsDisabled();
return false;
}
int32_t HTMLTextAreaElement::TabIndexDefault() { return 0; }
void HTMLTextAreaElement::GetType(nsAString& aType) {
aType.AssignLiteral("textarea");
}
void HTMLTextAreaElement::GetValue(nsAString& aValue) {
GetValueInternal(aValue, true);
MOZ_ASSERT(aValue.FindChar(static_cast<char16_t>('\r')) == -1);
}
void HTMLTextAreaElement::GetValueInternal(nsAString& aValue,
bool aIgnoreWrap) const {
MOZ_ASSERT(mState);
mState->GetValue(aValue, aIgnoreWrap);
}
bool HTMLTextAreaElement::ValueEquals(const nsAString& aValue) const {
MOZ_ASSERT(mState);
return mState->ValueEquals(aValue);
}
TextEditor* HTMLTextAreaElement::GetTextEditor() {
MOZ_ASSERT(mState);
return mState->GetTextEditor();
}
TextEditor* HTMLTextAreaElement::GetTextEditorWithoutCreation() {
MOZ_ASSERT(mState);
return mState->GetTextEditorWithoutCreation();
}
nsISelectionController* HTMLTextAreaElement::GetSelectionController() {
MOZ_ASSERT(mState);
return mState->GetSelectionController();
}
nsFrameSelection* HTMLTextAreaElement::GetConstFrameSelection() {
MOZ_ASSERT(mState);
return mState->GetConstFrameSelection();
}
nsresult HTMLTextAreaElement::BindToFrame(nsTextControlFrame* aFrame) {
MOZ_ASSERT(!nsContentUtils::IsSafeToRunScript());
MOZ_ASSERT(mState);
return mState->BindToFrame(aFrame);
}
void HTMLTextAreaElement::UnbindFromFrame(nsTextControlFrame* aFrame) {
MOZ_ASSERT(mState);
if (aFrame) {
mState->UnbindFromFrame(aFrame);
}
}
nsresult HTMLTextAreaElement::CreateEditor() {
MOZ_ASSERT(mState);
return mState->PrepareEditor();
}
void HTMLTextAreaElement::UpdateOverlayTextVisibility(bool aNotify) {
MOZ_ASSERT(mState);
mState->UpdateOverlayTextVisibility(aNotify);
}
bool HTMLTextAreaElement::GetPlaceholderVisibility() {
MOZ_ASSERT(mState);
return mState->GetPlaceholderVisibility();
}
void HTMLTextAreaElement::SetPreviewValue(const nsAString& aValue) {
MOZ_ASSERT(mState);
mState->SetPreviewText(aValue, true);
}
void HTMLTextAreaElement::GetPreviewValue(nsAString& aValue) {
MOZ_ASSERT(mState);
mState->GetPreviewText(aValue);
}
void HTMLTextAreaElement::EnablePreview() {
if (mIsPreviewEnabled) {
return;
}
mIsPreviewEnabled = true;
// Reconstruct the frame to append an anonymous preview node
nsLayoutUtils::PostRestyleEvent(this, RestyleHint{0},
nsChangeHint_ReconstructFrame);
}
bool HTMLTextAreaElement::IsPreviewEnabled() { return mIsPreviewEnabled; }
bool HTMLTextAreaElement::GetPreviewVisibility() {
MOZ_ASSERT(mState);
return mState->GetPreviewVisibility();
}
nsresult HTMLTextAreaElement::SetValueInternal(const nsAString& aValue,
uint32_t aFlags) {
MOZ_ASSERT(mState);
// Need to set the value changed flag here if our value has in fact changed
// (i.e. if eSetValue_Notify is in aFlags), so that
// nsTextControlFrame::UpdateValueDisplay retrieves the correct value if
// needed.
if (aFlags & TextControlState::eSetValue_Notify) {
SetValueChanged(true);
}
if (!mState->SetValue(aValue, aFlags)) {
return NS_ERROR_OUT_OF_MEMORY;
}
return NS_OK;
}
1998-09-03 05:03:33 +04:00
void HTMLTextAreaElement::SetValue(const nsAString& aValue,
ErrorResult& aError) {
// If the value has been set by a script, we basically want to keep the
// current change event state. If the element is ready to fire a change
// event, we should keep it that way. Otherwise, we should make sure the
// element will not fire any event because of the script interaction.
//
// NOTE: this is currently quite expensive work (too much string
// manipulation). We should probably optimize that.
nsAutoString currentValue;
GetValueInternal(currentValue, true);
nsresult rv = SetValueInternal(
aValue, TextControlState::eSetValue_ByContent |
TextControlState::eSetValue_Notify |
TextControlState::eSetValue_MoveCursorToEndIfValueChanged);
if (NS_WARN_IF(NS_FAILED(rv))) {
aError.Throw(rv);
return;
}
if (mFocusedValue.Equals(currentValue)) {
GetValueInternal(mFocusedValue, true);
}
}
void HTMLTextAreaElement::SetUserInput(const nsAString& aValue,
nsIPrincipal& aSubjectPrincipal) {
SetValueInternal(
aValue, TextControlState::eSetValue_BySetUserInput |
TextControlState::eSetValue_Notify |
TextControlState::eSetValue_MoveCursorToEndIfValueChanged);
}
nsresult HTMLTextAreaElement::SetValueChanged(bool aValueChanged) {
MOZ_ASSERT(mState);
bool previousValue = mValueChanged;
mValueChanged = aValueChanged;
if (!aValueChanged && !mState->IsEmpty()) {
mState->EmptyValue();
2013-03-25 23:35:55 +04:00
}
if (mValueChanged != previousValue) {
UpdateState(true);
}
return NS_OK;
}
void HTMLTextAreaElement::GetDefaultValue(nsAString& aDefaultValue,
ErrorResult& aError) {
if (!nsContentUtils::GetNodeTextContent(this, false, aDefaultValue,
fallible)) {
aError.Throw(NS_ERROR_OUT_OF_MEMORY);
}
}
void HTMLTextAreaElement::SetDefaultValue(const nsAString& aDefaultValue,
ErrorResult& aError) {
nsresult rv = nsContentUtils::SetNodeTextContent(this, aDefaultValue, true);
if (NS_SUCCEEDED(rv) && !mValueChanged) {
Reset();
}
if (NS_FAILED(rv)) {
aError.Throw(rv);
}
}
bool HTMLTextAreaElement::ParseAttribute(int32_t aNamespaceID,
nsAtom* aAttribute,
const nsAString& aValue,
nsIPrincipal* aMaybeScriptedPrincipal,
nsAttrValue& aResult) {
if (aNamespaceID == kNameSpaceID_None) {
if (aAttribute == nsGkAtoms::maxlength ||
aAttribute == nsGkAtoms::minlength) {
return aResult.ParseNonNegativeIntValue(aValue);
} else if (aAttribute == nsGkAtoms::cols) {
aResult.ParseIntWithFallback(aValue, DEFAULT_COLS);
return true;
} else if (aAttribute == nsGkAtoms::rows) {
aResult.ParseIntWithFallback(aValue, DEFAULT_ROWS_TEXTAREA);
return true;
} else if (aAttribute == nsGkAtoms::autocomplete) {
aResult.ParseAtomArray(aValue);
return true;
}
}
return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
aMaybeScriptedPrincipal, aResult);
1998-09-03 05:03:33 +04:00
}
void HTMLTextAreaElement::MapAttributesIntoRule(
const nsMappedAttributes* aAttributes, MappedDeclarations& aDecls) {
// wrap=off
if (!aDecls.PropertyIsSet(eCSSProperty_white_space)) {
const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::wrap);
if (value && value->Type() == nsAttrValue::eString &&
value->Equals(nsGkAtoms::OFF, eIgnoreCase)) {
aDecls.SetKeywordValue(eCSSProperty_white_space, StyleWhiteSpace::Pre);
}
}
nsGenericHTMLFormElementWithState::MapDivAlignAttributeInto(aAttributes,
aDecls);
nsGenericHTMLFormElementWithState::MapCommonAttributesInto(aAttributes,
aDecls);
1998-09-03 05:03:33 +04:00
}
nsChangeHint HTMLTextAreaElement::GetAttributeChangeHint(
const nsAtom* aAttribute, int32_t aModType) const {
nsChangeHint retval =
nsGenericHTMLFormElementWithState::GetAttributeChangeHint(aAttribute,
aModType);
if (aAttribute == nsGkAtoms::rows || aAttribute == nsGkAtoms::cols) {
retval |= NS_STYLE_HINT_REFLOW;
} else if (aAttribute == nsGkAtoms::wrap) {
retval |= nsChangeHint_ReconstructFrame;
} else if (aAttribute == nsGkAtoms::placeholder) {
retval |= nsChangeHint_ReconstructFrame;
}
return retval;
}
NS_IMETHODIMP_(bool)
HTMLTextAreaElement::IsAttributeMapped(const nsAtom* aAttribute) const {
static const MappedAttributeEntry attributes[] = {{nsGkAtoms::wrap},
{nullptr}};
static const MappedAttributeEntry* const map[] = {
attributes,
sDivAlignAttributeMap,
sCommonAttributeMap,
};
return FindAttributeDependence(aAttribute, map);
}
nsMapRuleToAttributesFunc HTMLTextAreaElement::GetAttributeMappingFunction()
const {
return &MapAttributesIntoRule;
}
bool HTMLTextAreaElement::IsDisabledForEvents(WidgetEvent* aEvent) {
nsIFormControlFrame* formControlFrame = GetFormControlFrame(false);
nsIFrame* formFrame = do_QueryFrame(formControlFrame);
return IsElementDisabledForEvents(aEvent, formFrame);
}
void HTMLTextAreaElement::GetEventTargetParent(EventChainPreVisitor& aVisitor) {
aVisitor.mCanHandle = false;
if (IsDisabledForEvents(aVisitor.mEvent)) {
return;
}
// Don't dispatch a second select event if we are already handling
// one.
if (aVisitor.mEvent->mMessage == eFormSelect) {
if (mHandlingSelect) {
return;
}
mHandlingSelect = true;
}
if (aVisitor.mEvent->mMessage == eBlur) {
// Set mWantsPreHandleEvent and fire change event in PreHandleEvent to
// prevent it breaks event target chain creation.
aVisitor.mWantsPreHandleEvent = true;
}
nsGenericHTMLFormElementWithState::GetEventTargetParent(aVisitor);
}
nsresult HTMLTextAreaElement::PreHandleEvent(EventChainVisitor& aVisitor) {
if (aVisitor.mEvent->mMessage == eBlur) {
// Fire onchange (if necessary), before we do the blur, bug 370521.
FireChangeEventIfNeeded();
}
return nsGenericHTMLFormElementWithState::PreHandleEvent(aVisitor);
}
void HTMLTextAreaElement::FireChangeEventIfNeeded() {
nsString value;
GetValueInternal(value, true);
if (mFocusedValue.Equals(value)) {
return;
}
// Dispatch the change event.
mFocusedValue = value;
nsContentUtils::DispatchTrustedEvent(
OwnerDoc(), static_cast<nsIContent*>(this), NS_LITERAL_STRING("change"),
CanBubble::eYes, Cancelable::eNo);
}
nsresult HTMLTextAreaElement::PostHandleEvent(EventChainPostVisitor& aVisitor) {
if (aVisitor.mEvent->mMessage == eFormSelect) {
mHandlingSelect = false;
}
if (aVisitor.mEvent->mMessage == eFocus ||
aVisitor.mEvent->mMessage == eBlur) {
if (aVisitor.mEvent->mMessage == eFocus) {
// If the invalid UI is shown, we should show it while focusing (and
// update). Otherwise, we should not.
GetValueInternal(mFocusedValue, true);
mCanShowInvalidUI = !IsValid() && ShouldShowValidityUI();
// If neither invalid UI nor valid UI is shown, we shouldn't show the
// valid UI while typing.
mCanShowValidUI = ShouldShowValidityUI();
} else { // eBlur
mCanShowInvalidUI = true;
mCanShowValidUI = true;
}
UpdateState(true);
}
return NS_OK;
1998-09-03 05:03:33 +04:00
}
void HTMLTextAreaElement::DoneAddingChildren(bool aHaveNotified) {
if (!mValueChanged) {
if (!mDoneAddingChildren) {
// Reset now that we're done adding children if the content sink tried to
// sneak some text in without calling AppendChildTo.
Reset();
}
if (!mInhibitStateRestoration) {
GenerateStateKey();
RestoreFormControlState();
}
}
mDoneAddingChildren = true;
}
bool HTMLTextAreaElement::IsDoneAddingChildren() { return mDoneAddingChildren; }
// Controllers Methods
nsIControllers* HTMLTextAreaElement::GetControllers(ErrorResult& aError) {
if (!mControllers) {
mControllers = new nsXULControllers();
if (!mControllers) {
aError.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<nsBaseCommandController> commandController =
nsBaseCommandController::CreateEditorController();
if (!commandController) {
aError.Throw(NS_ERROR_FAILURE);
return nullptr;
}
mControllers->AppendController(commandController);
commandController = nsBaseCommandController::CreateEditingController();
if (!commandController) {
aError.Throw(NS_ERROR_FAILURE);
return nullptr;
}
mControllers->AppendController(commandController);
}
return mControllers;
}
nsresult HTMLTextAreaElement::GetControllers(nsIControllers** aResult) {
NS_ENSURE_ARG_POINTER(aResult);
ErrorResult error;
*aResult = GetControllers(error);
NS_IF_ADDREF(*aResult);
return error.StealNSResult();
}
uint32_t HTMLTextAreaElement::GetTextLength() {
nsAutoString val;
GetValue(val);
return val.Length();
}
Nullable<uint32_t> HTMLTextAreaElement::GetSelectionStart(ErrorResult& aError) {
uint32_t selStart, selEnd;
GetSelectionRange(&selStart, &selEnd, aError);
return Nullable<uint32_t>(selStart);
}
void HTMLTextAreaElement::SetSelectionStart(
const Nullable<uint32_t>& aSelectionStart, ErrorResult& aError) {
MOZ_ASSERT(mState);
mState->SetSelectionStart(aSelectionStart, aError);
}
Nullable<uint32_t> HTMLTextAreaElement::GetSelectionEnd(ErrorResult& aError) {
uint32_t selStart, selEnd;
GetSelectionRange(&selStart, &selEnd, aError);
return Nullable<uint32_t>(selEnd);
}
void HTMLTextAreaElement::SetSelectionEnd(
const Nullable<uint32_t>& aSelectionEnd, ErrorResult& aError) {
MOZ_ASSERT(mState);
mState->SetSelectionEnd(aSelectionEnd, aError);
}
void HTMLTextAreaElement::GetSelectionRange(uint32_t* aSelectionStart,
uint32_t* aSelectionEnd,
ErrorResult& aRv) {
MOZ_ASSERT(mState);
return mState->GetSelectionRange(aSelectionStart, aSelectionEnd, aRv);
}
void HTMLTextAreaElement::GetSelectionDirection(nsAString& aDirection,
ErrorResult& aError) {
MOZ_ASSERT(mState);
mState->GetSelectionDirectionString(aDirection, aError);
}
void HTMLTextAreaElement::SetSelectionDirection(const nsAString& aDirection,
ErrorResult& aError) {
MOZ_ASSERT(mState);
mState->SetSelectionDirection(aDirection, aError);
}
void HTMLTextAreaElement::SetSelectionRange(
uint32_t aSelectionStart, uint32_t aSelectionEnd,
const Optional<nsAString>& aDirection, ErrorResult& aError) {
MOZ_ASSERT(mState);
mState->SetSelectionRange(aSelectionStart, aSelectionEnd, aDirection, aError);
}
void HTMLTextAreaElement::SetRangeText(const nsAString& aReplacement,
ErrorResult& aRv) {
MOZ_ASSERT(mState);
mState->SetRangeText(aReplacement, aRv);
}
void HTMLTextAreaElement::SetRangeText(const nsAString& aReplacement,
uint32_t aStart, uint32_t aEnd,
SelectionMode aSelectMode,
ErrorResult& aRv) {
MOZ_ASSERT(mState);
mState->SetRangeText(aReplacement, aStart, aEnd, aSelectMode, aRv);
}
void HTMLTextAreaElement::GetValueFromSetRangeText(nsAString& aValue) {
GetValueInternal(aValue, false);
}
nsresult HTMLTextAreaElement::SetValueFromSetRangeText(
const nsAString& aValue) {
return SetValueInternal(aValue, TextControlState::eSetValue_ByContent |
TextControlState::eSetValue_Notify);
}
nsresult HTMLTextAreaElement::Reset() {
nsAutoString resetVal;
GetDefaultValue(resetVal, IgnoreErrors());
SetValueChanged(false);
nsresult rv =
SetValueInternal(resetVal, TextControlState::eSetValue_Internal);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
NS_IMETHODIMP
HTMLTextAreaElement::SubmitNamesValues(HTMLFormSubmission* aFormSubmission) {
// Disabled elements don't submit
if (IsDisabled()) {
return NS_OK;
}
//
// Get the name (if no name, no submit)
//
nsAutoString name;
GetAttr(kNameSpaceID_None, nsGkAtoms::name, name);
if (name.IsEmpty()) {
return NS_OK;
}
//
// Get the value
//
nsAutoString value;
GetValueInternal(value, false);
//
// Submit
//
return aFormSubmission->AddNameValuePair(name, value);
}
NS_IMETHODIMP
HTMLTextAreaElement::SaveState() {
nsresult rv = NS_OK;
// Only save if value != defaultValue (bug 62713)
PresState* state = nullptr;
if (mValueChanged) {
state = GetPrimaryPresState();
if (state) {
nsAutoString value;
GetValueInternal(value, true);
rv = nsLinebreakConverter::ConvertStringLineBreaks(
value, nsLinebreakConverter::eLinebreakPlatform,
nsLinebreakConverter::eLinebreakContent);
if (NS_FAILED(rv)) {
NS_ERROR("Converting linebreaks failed!");
return rv;
}
state->contentData() = std::move(value);
}
}
if (mDisabledChanged) {
if (!state) {
state = GetPrimaryPresState();
rv = NS_OK;
}
if (state) {
// We do not want to save the real disabled state but the disabled
// attribute.
state->disabled() = HasAttr(kNameSpaceID_None, nsGkAtoms::disabled);
state->disabledSet() = true;
}
}
return rv;
}
bool HTMLTextAreaElement::RestoreState(PresState* aState) {
const PresContentData& state = aState->contentData();
if (state.type() == PresContentData::TnsString) {
ErrorResult rv;
SetValue(state.get_nsString(), rv);
ENSURE_SUCCESS(rv, false);
}
if (aState->disabledSet() && !aState->disabled()) {
SetDisabled(false, IgnoreErrors());
}
return false;
}
EventStates HTMLTextAreaElement::IntrinsicState() const {
EventStates state = nsGenericHTMLFormElementWithState::IntrinsicState();
if (IsCandidateForConstraintValidation()) {
if (IsValid()) {
state |= NS_EVENT_STATE_VALID;
} else {
state |= NS_EVENT_STATE_INVALID;
// :-moz-ui-invalid always apply if the element suffers from a custom
// error and never applies if novalidate is set on the form owner.
if ((!mForm ||
!mForm->HasAttr(kNameSpaceID_None, nsGkAtoms::novalidate)) &&
(GetValidityState(VALIDITY_STATE_CUSTOM_ERROR) ||
(mCanShowInvalidUI && ShouldShowValidityUI()))) {
state |= NS_EVENT_STATE_MOZ_UI_INVALID;
}
}
// :-moz-ui-valid applies if all the following are true:
// 1. The element is not focused, or had either :-moz-ui-valid or
// :-moz-ui-invalid applying before it was focused ;
// 2. The element is either valid or isn't allowed to have
// :-moz-ui-invalid applying ;
// 3. The element has no form owner or its form owner doesn't have the
// novalidate attribute set ;
// 4. The element has already been modified or the user tried to submit the
// form owner while invalid.
if ((!mForm || !mForm->HasAttr(kNameSpaceID_None, nsGkAtoms::novalidate)) &&
(mCanShowValidUI && ShouldShowValidityUI() &&
(IsValid() || (state.HasState(NS_EVENT_STATE_MOZ_UI_INVALID) &&
!mCanShowInvalidUI)))) {
state |= NS_EVENT_STATE_MOZ_UI_VALID;
}
}
if (HasAttr(kNameSpaceID_None, nsGkAtoms::placeholder) && IsValueEmpty()) {
state |= NS_EVENT_STATE_PLACEHOLDERSHOWN;
}
return state;
}
Bug 1555216 - Change the signature of BindToTree to be (BindContext&, nsINode& aParentNode). r=bzbarsky BindContext was going to have way more information at first, but then I realized that most of the things I wanted to know were basically a flag away using the parent node. Still I think it's worth it, now experimenting with BindToTree will only mean adding a field to a struct that's included from a couple cpp files, instead of a massive pain. I also think this is clearer, and doing this highlights quite a few inconsistencies in our code which I've left untouched, but commented with FIXMEs. Steps are: $ for file in $(rg 'nsresult BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#nsresult BindToTree(Document\* aDocument, nsIContent\* aParent,#nsresult BindToTree(BindContext\&, nsINode\& aParent)#g' $file; done $ for file in $(rg 'nsresult BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's# nsIContent\* aBindingParent) override#override#g' $file; done $ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#::BindToTree(Document\* aDocument, nsIContent\* aParent,#::BindToTree(BindContext\& aContext, nsINode\& aParent)#g' $file; done $ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#nsIContent\* aBindingParent)##g' $file; done $ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#::BindToTree(aDocument, aParent, aBindingParent)#::BindToTree(aContext, aParent)#g' $file; done $ ./mach clang-format Then manual fixups. Depends on D32948 Differential Revision: https://phabricator.services.mozilla.com/D32949
2019-05-29 07:27:04 +03:00
nsresult HTMLTextAreaElement::BindToTree(BindContext& aContext,
nsINode& aParent) {
nsresult rv =
nsGenericHTMLFormElementWithState::BindToTree(aContext, aParent);
NS_ENSURE_SUCCESS(rv, rv);
// If there is a disabled fieldset in the parent chain, the element is now
// barred from constraint validation and can't suffer from value missing.
UpdateValueMissingValidityState();
UpdateBarredFromConstraintValidation();
// And now make sure our state is up to date
UpdateState(false);
return rv;
}
void HTMLTextAreaElement::UnbindFromTree(bool aNullParent) {
nsGenericHTMLFormElementWithState::UnbindFromTree(aNullParent);
// We might be no longer disabled because of parent chain changed.
UpdateValueMissingValidityState();
UpdateBarredFromConstraintValidation();
// And now make sure our state is up to date
UpdateState(false);
}
nsresult HTMLTextAreaElement::BeforeSetAttr(int32_t aNameSpaceID, nsAtom* aName,
const nsAttrValueOrString* aValue,
bool aNotify) {
if (aNotify && aName == nsGkAtoms::disabled &&
aNameSpaceID == kNameSpaceID_None) {
mDisabledChanged = true;
}
return nsGenericHTMLFormElementWithState::BeforeSetAttr(aNameSpaceID, aName,
aValue, aNotify);
}
2007-01-30 08:48:22 +03:00
void HTMLTextAreaElement::CharacterDataChanged(nsIContent* aContent,
const CharacterDataChangeInfo&) {
2007-01-30 08:48:22 +03:00
ContentChanged(aContent);
}
void HTMLTextAreaElement::ContentAppended(nsIContent* aFirstNewContent) {
ContentChanged(aFirstNewContent);
2007-01-30 08:48:22 +03:00
}
void HTMLTextAreaElement::ContentInserted(nsIContent* aChild) {
2007-01-30 08:48:22 +03:00
ContentChanged(aChild);
}
void HTMLTextAreaElement::ContentRemoved(nsIContent* aChild,
nsIContent* aPreviousSibling) {
2007-01-30 08:48:22 +03:00
ContentChanged(aChild);
}
void HTMLTextAreaElement::ContentChanged(nsIContent* aContent) {
2007-01-30 08:48:22 +03:00
if (!mValueChanged && mDoneAddingChildren &&
nsContentUtils::IsInSameAnonymousTree(this, aContent)) {
// Hard to say what the reset can trigger, so be safe pending
// further auditing.
nsCOMPtr<nsIMutationObserver> kungFuDeathGrip(this);
2007-01-30 08:48:22 +03:00
Reset();
}
}
nsresult HTMLTextAreaElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
const nsAttrValue* aValue,
const nsAttrValue* aOldValue,
nsIPrincipal* aSubjectPrincipal,
bool aNotify) {
if (aNameSpaceID == kNameSpaceID_None) {
if (aName == nsGkAtoms::required || aName == nsGkAtoms::disabled ||
aName == nsGkAtoms::readonly) {
if (aName == nsGkAtoms::disabled) {
// This *has* to be called *before* validity state check because
// UpdateBarredFromConstraintValidation and
// UpdateValueMissingValidityState depend on our disabled state.
UpdateDisabledState(aNotify);
}
if (aName == nsGkAtoms::required) {
// This *has* to be called *before* UpdateValueMissingValidityState
// because UpdateValueMissingValidityState depends on our required
// state.
UpdateRequiredState(!!aValue, aNotify);
}
UpdateValueMissingValidityState();
// This *has* to be called *after* validity has changed.
if (aName == nsGkAtoms::readonly || aName == nsGkAtoms::disabled) {
UpdateBarredFromConstraintValidation();
}
} else if (aName == nsGkAtoms::autocomplete) {
// Clear the cached @autocomplete attribute state.
mAutocompleteAttrState = nsContentUtils::eAutocompleteAttrState_Unknown;
} else if (aName == nsGkAtoms::maxlength) {
UpdateTooLongValidityState();
} else if (aName == nsGkAtoms::minlength) {
UpdateTooShortValidityState();
}
}
return nsGenericHTMLFormElementWithState::AfterSetAttr(
aNameSpaceID, aName, aValue, aOldValue, aSubjectPrincipal, aNotify);
}
nsresult HTMLTextAreaElement::CopyInnerTo(Element* aDest) {
nsresult rv = nsGenericHTMLFormElementWithState::CopyInnerTo(aDest);
NS_ENSURE_SUCCESS(rv, rv);
if (aDest->OwnerDoc()->IsStaticDocument()) {
nsAutoString value;
GetValueInternal(value, true);
ErrorResult ret;
static_cast<HTMLTextAreaElement*>(aDest)->SetValue(value, ret);
return ret.StealNSResult();
}
return NS_OK;
}
bool HTMLTextAreaElement::IsMutable() const {
return (!HasAttr(kNameSpaceID_None, nsGkAtoms::readonly) && !IsDisabled());
}
bool HTMLTextAreaElement::IsValueEmpty() const {
nsAutoString value;
GetValueInternal(value, true);
return value.IsEmpty();
}
void HTMLTextAreaElement::SetCustomValidity(const nsAString& aError) {
nsIConstraintValidation::SetCustomValidity(aError);
UpdateState(true);
}
bool HTMLTextAreaElement::IsTooLong() {
if (!mValueChanged || !mLastValueChangeWasInteractive ||
!HasAttr(kNameSpaceID_None, nsGkAtoms::maxlength)) {
return false;
}
int32_t maxLength = MaxLength();
// Maxlength of -1 means parsing error.
if (maxLength == -1) {
return false;
}
int32_t textLength = GetTextLength();
return textLength > maxLength;
}
bool HTMLTextAreaElement::IsTooShort() {
if (!mValueChanged || !mLastValueChangeWasInteractive ||
!HasAttr(kNameSpaceID_None, nsGkAtoms::minlength)) {
return false;
}
int32_t minLength = MinLength();
// Minlength of -1 means parsing error.
if (minLength == -1) {
return false;
}
int32_t textLength = GetTextLength();
return textLength && textLength < minLength;
}
bool HTMLTextAreaElement::IsValueMissing() const {
if (!Required() || !IsMutable()) {
return false;
}
return IsValueEmpty();
}
void HTMLTextAreaElement::UpdateTooLongValidityState() {
SetValidityState(VALIDITY_STATE_TOO_LONG, IsTooLong());
}
void HTMLTextAreaElement::UpdateTooShortValidityState() {
SetValidityState(VALIDITY_STATE_TOO_SHORT, IsTooShort());
}
void HTMLTextAreaElement::UpdateValueMissingValidityState() {
SetValidityState(VALIDITY_STATE_VALUE_MISSING, IsValueMissing());
}
void HTMLTextAreaElement::UpdateBarredFromConstraintValidation() {
SetBarredFromConstraintValidation(
HasAttr(kNameSpaceID_None, nsGkAtoms::readonly) || IsDisabled());
}
nsresult HTMLTextAreaElement::GetValidationMessage(
nsAString& aValidationMessage, ValidityStateType aType) {
nsresult rv = NS_OK;
switch (aType) {
case VALIDITY_STATE_TOO_LONG: {
nsAutoString message;
int32_t maxLength = MaxLength();
int32_t textLength = GetTextLength();
nsAutoString strMaxLength;
nsAutoString strTextLength;
strMaxLength.AppendInt(maxLength);
strTextLength.AppendInt(textLength);
rv = nsContentUtils::FormatMaybeLocalizedString(
message, nsContentUtils::eDOM_PROPERTIES, "FormValidationTextTooLong",
OwnerDoc(), strMaxLength, strTextLength);
aValidationMessage = message;
} break;
case VALIDITY_STATE_TOO_SHORT: {
nsAutoString message;
int32_t minLength = MinLength();
int32_t textLength = GetTextLength();
nsAutoString strMinLength;
nsAutoString strTextLength;
strMinLength.AppendInt(minLength);
strTextLength.AppendInt(textLength);
rv = nsContentUtils::FormatMaybeLocalizedString(
message, nsContentUtils::eDOM_PROPERTIES,
"FormValidationTextTooShort", OwnerDoc(), strMinLength,
strTextLength);
aValidationMessage = message;
} break;
case VALIDITY_STATE_VALUE_MISSING: {
nsAutoString message;
rv = nsContentUtils::GetMaybeLocalizedString(
nsContentUtils::eDOM_PROPERTIES, "FormValidationValueMissing",
OwnerDoc(), message);
aValidationMessage = message;
} break;
default:
rv = nsIConstraintValidation::GetValidationMessage(aValidationMessage,
aType);
}
return rv;
}
bool HTMLTextAreaElement::IsSingleLineTextControl() const { return false; }
bool HTMLTextAreaElement::IsTextArea() const { return true; }
bool HTMLTextAreaElement::IsPasswordTextControl() const { return false; }
int32_t HTMLTextAreaElement::GetCols() { return Cols(); }
int32_t HTMLTextAreaElement::GetWrapCols() {
nsHTMLTextWrap wrapProp;
TextControlElement::GetWrapPropertyEnum(this, wrapProp);
if (wrapProp == TextControlElement::eHTMLTextWrap_Off) {
// do not wrap when wrap=off
return 0;
}
// Otherwise we just wrap at the given number of columns
return GetCols();
}
int32_t HTMLTextAreaElement::GetRows() {
const nsAttrValue* attr = GetParsedAttr(nsGkAtoms::rows);
if (attr && attr->Type() == nsAttrValue::eInteger) {
int32_t rows = attr->GetIntegerValue();
return (rows <= 0) ? DEFAULT_ROWS_TEXTAREA : rows;
}
return DEFAULT_ROWS_TEXTAREA;
}
void HTMLTextAreaElement::GetDefaultValueFromContent(nsAString& aValue) {
GetDefaultValue(aValue, IgnoreErrors());
2013-03-25 23:35:55 +04:00
}
bool HTMLTextAreaElement::ValueChanged() const { return mValueChanged; }
void HTMLTextAreaElement::GetTextEditorValue(nsAString& aValue,
bool aIgnoreWrap) const {
MOZ_ASSERT(mState);
mState->GetValue(aValue, aIgnoreWrap);
}
void HTMLTextAreaElement::InitializeKeyboardEventListeners() {
MOZ_ASSERT(mState);
mState->InitializeKeyboardEventListeners();
}
void HTMLTextAreaElement::OnValueChanged(ValueChangeKind aKind) {
if (aKind != ValueChangeKind::Internal) {
mLastValueChangeWasInteractive = aKind == ValueChangeKind::UserInteraction;
}
// Update the validity state
bool validBefore = IsValid();
UpdateTooLongValidityState();
UpdateTooShortValidityState();
UpdateValueMissingValidityState();
if (validBefore != IsValid() || HasAttr(nsGkAtoms::placeholder)) {
UpdateState(true);
}
}
bool HTMLTextAreaElement::HasCachedSelection() {
MOZ_ASSERT(mState);
return mState->IsSelectionCached();
}
void HTMLTextAreaElement::FieldSetDisabledChanged(bool aNotify) {
// This *has* to be called before UpdateBarredFromConstraintValidation and
// UpdateValueMissingValidityState because these two functions depend on our
// disabled state.
nsGenericHTMLFormElementWithState::FieldSetDisabledChanged(aNotify);
UpdateValueMissingValidityState();
UpdateBarredFromConstraintValidation();
UpdateState(aNotify);
}
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv The only manual changes here are to BindingUtils.h, BindingUtils.cpp, Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp, dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp, Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp, Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The rest of this diff was generated by running the following commands: find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
JSObject* HTMLTextAreaElement::WrapNode(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return HTMLTextAreaElement_Binding::Wrap(aCx, this, aGivenProto);
}
void HTMLTextAreaElement::GetAutocomplete(DOMString& aValue) {
const nsAttrValue* attributeVal = GetParsedAttr(nsGkAtoms::autocomplete);
mAutocompleteAttrState = nsContentUtils::SerializeAutocompleteAttribute(
attributeVal, aValue, mAutocompleteAttrState);
}
} // namespace dom
} // namespace mozilla