Bug 1275473 - Implement CompositionEvent.ranges. r=masayuki, r=smaug

This commit is contained in:
isangelawu 2016-07-26 12:09:07 +02:00
Родитель 8f7ffc84b7
Коммит 5362a0f92e
9 изменённых файлов: 166 добавлений и 0 удалений

Просмотреть файл

@ -69,6 +69,27 @@ CompositionEvent::InitCompositionEvent(const nsAString& aType,
mLocale = aLocale;
}
void
CompositionEvent::GetRanges(TextClauseArray& aRanges)
{
// If the mRanges is not empty, we return the cached value.
if (!mRanges.IsEmpty()) {
aRanges = mRanges;
return;
}
RefPtr<TextRangeArray> textRangeArray = mEvent->AsCompositionEvent()->mRanges;
if (!textRangeArray) {
return;
}
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(mOwner);
const TextRange* targetRange = textRangeArray->GetTargetClause();
for (size_t i = 0; i < textRangeArray->Length(); i++) {
const TextRange& range = textRangeArray->ElementAt(i);
mRanges.AppendElement(new TextClause(window, range, targetRange));
}
aRanges = mRanges;
}
} // namespace dom
} // namespace mozilla

Просмотреть файл

@ -8,12 +8,16 @@
#define mozilla_dom_CompositionEvent_h_
#include "mozilla/dom/CompositionEventBinding.h"
#include "mozilla/dom/TextClause.h"
#include "mozilla/dom/TypedArray.h"
#include "mozilla/dom/UIEvent.h"
#include "mozilla/EventForwards.h"
namespace mozilla {
namespace dom {
typedef nsTArray<RefPtr<TextClause>> TextClauseArray;
class CompositionEvent : public UIEvent
{
public:
@ -37,12 +41,14 @@ public:
const nsAString& aLocale);
void GetData(nsAString&) const;
void GetLocale(nsAString&) const;
void GetRanges(TextClauseArray& aRanges);
protected:
~CompositionEvent() {}
nsString mData;
nsString mLocale;
TextClauseArray mRanges;
};
} // namespace dom

50
dom/events/TextClause.cpp Normal file
Просмотреть файл

@ -0,0 +1,50 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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/TextClause.h"
#include "mozilla/dom/TextClauseBinding.h"
#include "mozilla/TextEvents.h"
namespace mozilla {
namespace dom {
// Only needed for refcounted objects.
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(TextClause)
NS_IMPL_CYCLE_COLLECTING_ADDREF(TextClause)
NS_IMPL_CYCLE_COLLECTING_RELEASE(TextClause)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TextClause)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
TextClause::TextClause(nsPIDOMWindowInner* aOwner, const TextRange& aRange,
const TextRange* aTargetRange)
: mOwner(aOwner)
, mIsTargetClause(false)
{
MOZ_ASSERT(aOwner);
mStartOffset = aRange.mStartOffset;
mEndOffset = aRange.mEndOffset;
if (aRange.IsClause()) {
mIsCaret = false;
if (aTargetRange && aTargetRange->mStartOffset == mStartOffset) {
mIsTargetClause = true;
}
} else {
mIsCaret = true;
}
}
JSObject*
TextClause::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return TextClauseBinding::Wrap(aCx, this, aGivenProto);
}
TextClause::~TextClause() {}
} // namespace dom
} // namespace mozilla

57
dom/events/TextClause.h Normal file
Просмотреть файл

@ -0,0 +1,57 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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/. */
#ifndef mozilla_dom_TextClause_h
#define mozilla_dom_TextClause_h
#include "js/TypeDecls.h"
#include "mozilla/Attributes.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "nsCycleCollectionParticipant.h"
#include "nsWrapperCache.h"
namespace mozilla {
namespace dom {
class TextClause final : public nsISupports
, public nsWrapperCache
{
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(TextClause)
nsPIDOMWindowInner* GetParentObject() const { return mOwner; }
TextClause(nsPIDOMWindowInner* aWindow, const TextRange& aRange,
const TextRange* targetRange);
virtual JSObject* WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) override;
inline uint32_t StartOffset() const { return mStartOffset; }
inline uint32_t EndOffset() const { return mEndOffset; }
inline bool IsCaret() const { return mIsCaret; }
inline bool IsTargetClause() const { return mIsTargetClause; }
private:
~TextClause();
nsCOMPtr<nsPIDOMWindowInner> mOwner;
// Following members, please take look at widget/TextRange.h.
uint32_t mStartOffset;
uint32_t mEndOffset;
bool mIsCaret;
bool mIsTargetClause;
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_TextClause_h

Просмотреть файл

@ -64,6 +64,7 @@ EXPORTS.mozilla.dom += [
'ScrollAreaEvent.h',
'SimpleGestureEvent.h',
'StorageEvent.h',
'TextClause.h',
'Touch.h',
'TouchEvent.h',
'TransitionEvent.h',
@ -112,6 +113,7 @@ UNIFIED_SOURCES += [
'ScrollAreaEvent.cpp',
'SimpleGestureEvent.cpp',
'StorageEvent.cpp',
'TextClause.cpp',
'TextComposition.cpp',
'Touch.cpp',
'TouchEvent.cpp',

Просмотреть файл

@ -13,6 +13,13 @@ interface CompositionEvent : UIEvent
{
readonly attribute DOMString? data;
readonly attribute DOMString locale;
/**
* ranges is trying to expose TextRangeArray in Gecko so a
* js-plugin couble be able to know the clauses information
*/
[ChromeOnly,Cached,Pure]
readonly attribute sequence<TextClause> ranges;
};
partial interface CompositionEvent

Просмотреть файл

@ -0,0 +1,21 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/.
*/
[ChromeOnly]
interface TextClause
{
// The start offset of TextClause
readonly attribute long startOffset;
// The end offset of TextClause
readonly attribute long endOffset;
// If the TextClause is Caret or not
readonly attribute boolean isCaret;
// If the TextClause is TargetClause or not
readonly attribute boolean isTargetClause;
};

Просмотреть файл

@ -545,6 +545,7 @@ WEBIDL_FILES = [
'TelephonyCallGroup.webidl',
'TelephonyCallId.webidl',
'Text.webidl',
'TextClause.webidl',
'TextDecoder.webidl',
'TextEncoder.webidl',
'TextTrack.webidl',

Просмотреть файл

@ -548,6 +548,7 @@ public:
mData = aEvent.mData;
mOriginalMessage = aEvent.mOriginalMessage;
mRanges = aEvent.mRanges;
// Currently, we don't need to copy the other members because they are
// for internal use only (not available from JS).