ContentEventHandler.cpp 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "ContentEventHandler.h"
  6. #include "mozilla/IMEStateManager.h"
  7. #include "mozilla/TextComposition.h"
  8. #include "mozilla/TextEvents.h"
  9. #include "mozilla/dom/Element.h"
  10. #include "mozilla/dom/HTMLUnknownElement.h"
  11. #include "mozilla/dom/Selection.h"
  12. #include "nsCaret.h"
  13. #include "nsCOMPtr.h"
  14. #include "nsContentUtils.h"
  15. #include "nsCopySupport.h"
  16. #include "nsFocusManager.h"
  17. #include "nsFontMetrics.h"
  18. #include "nsFrameSelection.h"
  19. #include "nsIContentIterator.h"
  20. #include "nsIPresShell.h"
  21. #include "nsISelection.h"
  22. #include "nsIFrame.h"
  23. #include "nsIObjectFrame.h"
  24. #include "nsLayoutUtils.h"
  25. #include "nsPresContext.h"
  26. #include "nsQueryObject.h"
  27. #include "nsRange.h"
  28. #include "nsTextFragment.h"
  29. #include "nsTextFrame.h"
  30. #include "nsView.h"
  31. #include <algorithm>
  32. namespace mozilla {
  33. using namespace dom;
  34. using namespace widget;
  35. /******************************************************************/
  36. /* ContentEventHandler */
  37. /******************************************************************/
  38. // NOTE
  39. //
  40. // ContentEventHandler *creates* ranges as following rules:
  41. // 1. Start of range:
  42. // 1.1. Cases: [textNode or text[Node or textNode[
  43. // When text node is start of a range, start node is the text node and
  44. // start offset is any number between 0 and the length of the text.
  45. // 1.2. Case: [<element>:
  46. // When start of an element node is start of a range, start node is
  47. // parent of the element and start offset is the element's index in the
  48. // parent.
  49. // 1.3. Case: <element/>[
  50. // When after an empty element node is start of a range, start node is
  51. // parent of the element and start offset is the element's index in the
  52. // parent + 1.
  53. // 1.4. Case: <element>[
  54. // When start of a non-empty element is start of a range, start node is
  55. // the element and start offset is 0.
  56. // 1.5. Case: <root>[
  57. // When start of a range is 0 and there are no nodes causing text,
  58. // start node is the root node and start offset is 0.
  59. // 1.6. Case: [</root>
  60. // When start of a range is out of bounds, start node is the root node
  61. // and start offset is number of the children.
  62. // 2. End of range:
  63. // 2.1. Cases: ]textNode or text]Node or textNode]
  64. // When a text node is end of a range, end node is the text node and
  65. // end offset is any number between 0 and the length of the text.
  66. // 2.2. Case: ]<element>
  67. // When before an element node (meaning before the open tag of the
  68. // element) is end of a range, end node is previous node causing text.
  69. // Note that this case shouldn't be handled directly. If rule 2.1 and
  70. // 2.3 are handled correctly, the loop with nsContentIterator shouldn't
  71. // reach the element node since the loop should've finished already at
  72. // handling the last node which caused some text.
  73. // 2.3. Case: <element>]
  74. // When a line break is caused before a non-empty element node and it's
  75. // end of a range, end node is the element and end offset is 0.
  76. // (i.e., including open tag of the element)
  77. // 2.4. Cases: <element/>]
  78. // When after an empty element node is end of a range, end node is
  79. // parent of the element node and end offset is the element's index in
  80. // the parent + 1. (i.e., including close tag of the element or empty
  81. // element)
  82. // 2.5. Case: ]</root>
  83. // When end of a range is out of bounds, end node is the root node and
  84. // end offset is number of the children.
  85. //
  86. // ContentEventHandler *treats* ranges as following additional rules:
  87. // 1. When the start node is an element node which doesn't have children,
  88. // it includes a line break caused before itself (i.e., includes its open
  89. // tag). For example, if start position is { <br>, 0 }, the line break
  90. // caused by <br> should be included into the flatten text.
  91. // 2. When the end node is an element node which doesn't have children,
  92. // it includes the end (i.e., includes its close tag except empty element).
  93. // Although, currently, any close tags don't cause line break, this also
  94. // includes its open tag. For example, if end position is { <br>, 0 }, the
  95. // line break caused by the <br> should be included into the flatten text.
  96. ContentEventHandler::ContentEventHandler(nsPresContext* aPresContext)
  97. : mPresContext(aPresContext)
  98. , mPresShell(aPresContext->GetPresShell())
  99. , mSelection(nullptr)
  100. , mFirstSelectedRange(nullptr)
  101. , mRootContent(nullptr)
  102. {
  103. }
  104. nsresult
  105. ContentEventHandler::InitBasic()
  106. {
  107. NS_ENSURE_TRUE(mPresShell, NS_ERROR_NOT_AVAILABLE);
  108. // If text frame which has overflowing selection underline is dirty,
  109. // we need to flush the pending reflow here.
  110. mPresShell->FlushPendingNotifications(Flush_Layout);
  111. // Flushing notifications can cause mPresShell to be destroyed (bug 577963).
  112. NS_ENSURE_TRUE(!mPresShell->IsDestroying(), NS_ERROR_FAILURE);
  113. return NS_OK;
  114. }
  115. nsresult
  116. ContentEventHandler::InitRootContent(Selection* aNormalSelection)
  117. {
  118. MOZ_ASSERT(aNormalSelection);
  119. // Root content should be computed with normal selection because normal
  120. // selection is typically has at least one range but the other selections
  121. // not so. If there is a range, computing its root is easy, but if
  122. // there are no ranges, we need to use ancestor limit instead.
  123. MOZ_ASSERT(aNormalSelection->Type() == SelectionType::eNormal);
  124. if (!aNormalSelection->RangeCount()) {
  125. // If there is no selection range, we should compute the selection root
  126. // from ancestor limiter or root content of the document.
  127. nsresult rv =
  128. aNormalSelection->GetAncestorLimiter(getter_AddRefs(mRootContent));
  129. if (NS_WARN_IF(NS_FAILED(rv))) {
  130. return NS_ERROR_FAILURE;
  131. }
  132. if (!mRootContent) {
  133. mRootContent = mPresShell->GetDocument()->GetRootElement();
  134. if (NS_WARN_IF(!mRootContent)) {
  135. return NS_ERROR_NOT_AVAILABLE;
  136. }
  137. }
  138. return NS_OK;
  139. }
  140. RefPtr<nsRange> range(aNormalSelection->GetRangeAt(0));
  141. if (NS_WARN_IF(!range)) {
  142. return NS_ERROR_UNEXPECTED;
  143. }
  144. // If there is a selection, we should retrieve the selection root from
  145. // the range since when the window is inactivated, the ancestor limiter
  146. // of selection was cleared by blur event handler of EditorBase but the
  147. // selection range still keeps storing the nodes. If the active element of
  148. // the deactive window is <input> or <textarea>, we can compute the
  149. // selection root from them.
  150. nsINode* startNode = range->GetStartParent();
  151. nsINode* endNode = range->GetEndParent();
  152. if (NS_WARN_IF(!startNode) || NS_WARN_IF(!endNode)) {
  153. return NS_ERROR_FAILURE;
  154. }
  155. // See bug 537041 comment 5, the range could have removed node.
  156. if (NS_WARN_IF(startNode->GetUncomposedDoc() != mPresShell->GetDocument())) {
  157. return NS_ERROR_FAILURE;
  158. }
  159. NS_ASSERTION(startNode->GetUncomposedDoc() == endNode->GetUncomposedDoc(),
  160. "firstNormalSelectionRange crosses the document boundary");
  161. mRootContent = startNode->GetSelectionRootContent(mPresShell);
  162. if (NS_WARN_IF(!mRootContent)) {
  163. return NS_ERROR_FAILURE;
  164. }
  165. return NS_OK;
  166. }
  167. nsresult
  168. ContentEventHandler::InitCommon(SelectionType aSelectionType)
  169. {
  170. if (mSelection && mSelection->Type() == aSelectionType) {
  171. return NS_OK;
  172. }
  173. mSelection = nullptr;
  174. mFirstSelectedRange = nullptr;
  175. mRootContent = nullptr;
  176. nsresult rv = InitBasic();
  177. NS_ENSURE_SUCCESS(rv, rv);
  178. nsCOMPtr<nsISelectionController> selectionController =
  179. mPresShell->GetSelectionControllerForFocusedContent();
  180. if (NS_WARN_IF(!selectionController)) {
  181. return NS_ERROR_NOT_AVAILABLE;
  182. }
  183. nsCOMPtr<nsISelection> selection;
  184. rv = selectionController->GetSelection(ToRawSelectionType(aSelectionType),
  185. getter_AddRefs(selection));
  186. if (NS_WARN_IF(NS_FAILED(rv))) {
  187. return NS_ERROR_UNEXPECTED;
  188. }
  189. mSelection = static_cast<Selection*>(selection.get());
  190. if (NS_WARN_IF(!mSelection)) {
  191. return NS_ERROR_NOT_AVAILABLE;
  192. }
  193. RefPtr<Selection> normalSelection;
  194. if (mSelection->Type() == SelectionType::eNormal) {
  195. normalSelection = mSelection;
  196. } else {
  197. nsCOMPtr<nsISelection> domSelection;
  198. nsresult rv =
  199. selectionController->GetSelection(
  200. nsISelectionController::SELECTION_NORMAL,
  201. getter_AddRefs(domSelection));
  202. if (NS_WARN_IF(NS_FAILED(rv))) {
  203. return NS_ERROR_UNEXPECTED;
  204. }
  205. if (NS_WARN_IF(!domSelection)) {
  206. return NS_ERROR_NOT_AVAILABLE;
  207. }
  208. normalSelection = domSelection->AsSelection();
  209. if (NS_WARN_IF(!normalSelection)) {
  210. return NS_ERROR_NOT_AVAILABLE;
  211. }
  212. }
  213. rv = InitRootContent(normalSelection);
  214. if (NS_WARN_IF(NS_FAILED(rv))) {
  215. return rv;
  216. }
  217. if (mSelection->RangeCount()) {
  218. mFirstSelectedRange = mSelection->GetRangeAt(0);
  219. if (NS_WARN_IF(!mFirstSelectedRange)) {
  220. return NS_ERROR_UNEXPECTED;
  221. }
  222. return NS_OK;
  223. }
  224. // Even if there are no selection ranges, it's usual case if aSelectionType
  225. // is a special selection.
  226. if (aSelectionType != SelectionType::eNormal) {
  227. MOZ_ASSERT(!mFirstSelectedRange);
  228. return NS_OK;
  229. }
  230. // But otherwise, we need to assume that there is a selection range at the
  231. // beginning of the root content if aSelectionType is eNormal.
  232. rv = nsRange::CreateRange(mRootContent, 0, mRootContent, 0,
  233. getter_AddRefs(mFirstSelectedRange));
  234. if (NS_WARN_IF(NS_FAILED(rv)) || NS_WARN_IF(!mFirstSelectedRange)) {
  235. return NS_ERROR_UNEXPECTED;
  236. }
  237. return NS_OK;
  238. }
  239. nsresult
  240. ContentEventHandler::Init(WidgetQueryContentEvent* aEvent)
  241. {
  242. NS_ASSERTION(aEvent, "aEvent must not be null");
  243. MOZ_ASSERT(aEvent->mMessage == eQuerySelectedText ||
  244. aEvent->mInput.mSelectionType == SelectionType::eNormal);
  245. if (NS_WARN_IF(!aEvent->mInput.IsValidOffset()) ||
  246. NS_WARN_IF(!aEvent->mInput.IsValidEventMessage(aEvent->mMessage))) {
  247. return NS_ERROR_FAILURE;
  248. }
  249. // Note that we should ignore WidgetQueryContentEvent::Input::mSelectionType
  250. // if the event isn't eQuerySelectedText.
  251. SelectionType selectionType =
  252. aEvent->mMessage == eQuerySelectedText ? aEvent->mInput.mSelectionType :
  253. SelectionType::eNormal;
  254. if (NS_WARN_IF(selectionType == SelectionType::eNone)) {
  255. return NS_ERROR_FAILURE;
  256. }
  257. nsresult rv = InitCommon(selectionType);
  258. NS_ENSURE_SUCCESS(rv, rv);
  259. // Be aware, WidgetQueryContentEvent::mInput::mOffset should be made absolute
  260. // offset before sending it to ContentEventHandler because querying selection
  261. // every time may be expensive. So, if the caller caches selection, it
  262. // should initialize the event with the cached value.
  263. if (aEvent->mInput.mRelativeToInsertionPoint) {
  264. MOZ_ASSERT(selectionType == SelectionType::eNormal);
  265. RefPtr<TextComposition> composition =
  266. IMEStateManager::GetTextCompositionFor(aEvent->mWidget);
  267. if (composition) {
  268. uint32_t compositionStart = composition->NativeOffsetOfStartComposition();
  269. if (NS_WARN_IF(!aEvent->mInput.MakeOffsetAbsolute(compositionStart))) {
  270. return NS_ERROR_FAILURE;
  271. }
  272. } else {
  273. LineBreakType lineBreakType = GetLineBreakType(aEvent);
  274. uint32_t selectionStart = 0;
  275. rv = GetStartOffset(mFirstSelectedRange, &selectionStart, lineBreakType);
  276. if (NS_WARN_IF(NS_FAILED(rv))) {
  277. return NS_ERROR_FAILURE;
  278. }
  279. if (NS_WARN_IF(!aEvent->mInput.MakeOffsetAbsolute(selectionStart))) {
  280. return NS_ERROR_FAILURE;
  281. }
  282. }
  283. }
  284. aEvent->mSucceeded = false;
  285. aEvent->mReply.mContentsRoot = mRootContent.get();
  286. aEvent->mReply.mHasSelection = !mSelection->IsCollapsed();
  287. nsRect r;
  288. nsIFrame* frame = nsCaret::GetGeometry(mSelection, &r);
  289. if (!frame) {
  290. frame = mRootContent->GetPrimaryFrame();
  291. if (NS_WARN_IF(!frame)) {
  292. return NS_ERROR_FAILURE;
  293. }
  294. }
  295. aEvent->mReply.mFocusedWidget = frame->GetNearestWidget();
  296. return NS_OK;
  297. }
  298. nsresult
  299. ContentEventHandler::Init(WidgetSelectionEvent* aEvent)
  300. {
  301. NS_ASSERTION(aEvent, "aEvent must not be null");
  302. nsresult rv = InitCommon();
  303. NS_ENSURE_SUCCESS(rv, rv);
  304. aEvent->mSucceeded = false;
  305. return NS_OK;
  306. }
  307. nsIContent*
  308. ContentEventHandler::GetFocusedContent()
  309. {
  310. nsIDocument* doc = mPresShell->GetDocument();
  311. if (!doc) {
  312. return nullptr;
  313. }
  314. nsCOMPtr<nsPIDOMWindowOuter> window = doc->GetWindow();
  315. nsCOMPtr<nsPIDOMWindowOuter> focusedWindow;
  316. return nsFocusManager::GetFocusedDescendant(window, true,
  317. getter_AddRefs(focusedWindow));
  318. }
  319. bool
  320. ContentEventHandler::IsPlugin(nsIContent* aContent)
  321. {
  322. return aContent &&
  323. aContent->GetDesiredIMEState().mEnabled == IMEState::PLUGIN;
  324. }
  325. nsresult
  326. ContentEventHandler::QueryContentRect(nsIContent* aContent,
  327. WidgetQueryContentEvent* aEvent)
  328. {
  329. NS_PRECONDITION(aContent, "aContent must not be null");
  330. nsIFrame* frame = aContent->GetPrimaryFrame();
  331. NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);
  332. // get rect for first frame
  333. nsRect resultRect(nsPoint(0, 0), frame->GetRect().Size());
  334. nsresult rv = ConvertToRootRelativeOffset(frame, resultRect);
  335. NS_ENSURE_SUCCESS(rv, rv);
  336. // account for any additional frames
  337. while ((frame = frame->GetNextContinuation()) != nullptr) {
  338. nsRect frameRect(nsPoint(0, 0), frame->GetRect().Size());
  339. rv = ConvertToRootRelativeOffset(frame, frameRect);
  340. NS_ENSURE_SUCCESS(rv, rv);
  341. resultRect.UnionRect(resultRect, frameRect);
  342. }
  343. aEvent->mReply.mRect = LayoutDeviceIntRect::FromUnknownRect(
  344. resultRect.ToOutsidePixels(mPresContext->AppUnitsPerDevPixel()));
  345. // Returning empty rect may cause native IME confused, let's make sure to
  346. // return non-empty rect.
  347. EnsureNonEmptyRect(aEvent->mReply.mRect);
  348. aEvent->mSucceeded = true;
  349. return NS_OK;
  350. }
  351. // Editor places a bogus BR node under its root content if the editor doesn't
  352. // have any text. This happens even for single line editors.
  353. // When we get text content and when we change the selection,
  354. // we don't want to include the bogus BRs at the end.
  355. static bool IsContentBR(nsIContent* aContent)
  356. {
  357. return aContent->IsHTMLElement(nsGkAtoms::br) &&
  358. !aContent->AttrValueIs(kNameSpaceID_None,
  359. nsGkAtoms::type,
  360. nsGkAtoms::moz,
  361. eIgnoreCase) &&
  362. !aContent->AttrValueIs(kNameSpaceID_None,
  363. nsGkAtoms::mozeditorbogusnode,
  364. nsGkAtoms::_true,
  365. eIgnoreCase);
  366. }
  367. static bool IsMozBR(nsIContent* aContent)
  368. {
  369. return aContent->IsHTMLElement(nsGkAtoms::br) && !IsContentBR(aContent);
  370. }
  371. static void ConvertToNativeNewlines(nsAFlatString& aString)
  372. {
  373. #if defined(XP_WIN)
  374. aString.ReplaceSubstring(NS_LITERAL_STRING("\n"), NS_LITERAL_STRING("\r\n"));
  375. #endif
  376. }
  377. static void AppendString(nsAString& aString, nsIContent* aContent)
  378. {
  379. NS_ASSERTION(aContent->IsNodeOfType(nsINode::eTEXT),
  380. "aContent is not a text node!");
  381. const nsTextFragment* text = aContent->GetText();
  382. if (!text) {
  383. return;
  384. }
  385. text->AppendTo(aString);
  386. }
  387. static void AppendSubString(nsAString& aString, nsIContent* aContent,
  388. uint32_t aXPOffset, uint32_t aXPLength)
  389. {
  390. NS_ASSERTION(aContent->IsNodeOfType(nsINode::eTEXT),
  391. "aContent is not a text node!");
  392. const nsTextFragment* text = aContent->GetText();
  393. if (!text) {
  394. return;
  395. }
  396. text->AppendTo(aString, int32_t(aXPOffset), int32_t(aXPLength));
  397. }
  398. #if defined(XP_WIN)
  399. static uint32_t CountNewlinesInXPLength(nsIContent* aContent,
  400. uint32_t aXPLength)
  401. {
  402. NS_ASSERTION(aContent->IsNodeOfType(nsINode::eTEXT),
  403. "aContent is not a text node!");
  404. const nsTextFragment* text = aContent->GetText();
  405. if (!text) {
  406. return 0;
  407. }
  408. // For automated tests, we should abort on debug build.
  409. MOZ_ASSERT(aXPLength == UINT32_MAX || aXPLength <= text->GetLength(),
  410. "aXPLength is out-of-bounds");
  411. const uint32_t length = std::min(aXPLength, text->GetLength());
  412. uint32_t newlines = 0;
  413. for (uint32_t i = 0; i < length; ++i) {
  414. if (text->CharAt(i) == '\n') {
  415. ++newlines;
  416. }
  417. }
  418. return newlines;
  419. }
  420. static uint32_t CountNewlinesInNativeLength(nsIContent* aContent,
  421. uint32_t aNativeLength)
  422. {
  423. NS_ASSERTION(aContent->IsNodeOfType(nsINode::eTEXT),
  424. "aContent is not a text node!");
  425. const nsTextFragment* text = aContent->GetText();
  426. if (!text) {
  427. return 0;
  428. }
  429. // For automated tests, we should abort on debug build.
  430. MOZ_ASSERT(
  431. (aNativeLength == UINT32_MAX || aNativeLength <= text->GetLength() * 2),
  432. "aNativeLength is unexpected value");
  433. const uint32_t xpLength = text->GetLength();
  434. uint32_t newlines = 0;
  435. for (uint32_t i = 0, nativeOffset = 0;
  436. i < xpLength && nativeOffset < aNativeLength;
  437. ++i, ++nativeOffset) {
  438. // For automated tests, we should abort on debug build.
  439. MOZ_ASSERT(i < text->GetLength(), "i is out-of-bounds");
  440. if (text->CharAt(i) == '\n') {
  441. ++newlines;
  442. ++nativeOffset;
  443. }
  444. }
  445. return newlines;
  446. }
  447. #endif
  448. /* static */ uint32_t
  449. ContentEventHandler::GetNativeTextLength(nsIContent* aContent,
  450. uint32_t aStartOffset,
  451. uint32_t aEndOffset)
  452. {
  453. MOZ_ASSERT(aEndOffset >= aStartOffset,
  454. "aEndOffset must be equals or larger than aStartOffset");
  455. if (NS_WARN_IF(!aContent->IsNodeOfType(nsINode::eTEXT))) {
  456. return 0;
  457. }
  458. if (aStartOffset == aEndOffset) {
  459. return 0;
  460. }
  461. return GetTextLength(aContent, LINE_BREAK_TYPE_NATIVE, aEndOffset) -
  462. GetTextLength(aContent, LINE_BREAK_TYPE_NATIVE, aStartOffset);
  463. }
  464. /* static */ uint32_t
  465. ContentEventHandler::GetNativeTextLength(nsIContent* aContent,
  466. uint32_t aMaxLength)
  467. {
  468. if (NS_WARN_IF(!aContent->IsNodeOfType(nsINode::eTEXT))) {
  469. return 0;
  470. }
  471. return GetTextLength(aContent, LINE_BREAK_TYPE_NATIVE, aMaxLength);
  472. }
  473. /* static */ uint32_t
  474. ContentEventHandler::GetNativeTextLengthBefore(nsIContent* aContent,
  475. nsINode* aRootNode)
  476. {
  477. if (NS_WARN_IF(aContent->IsNodeOfType(nsINode::eTEXT))) {
  478. return 0;
  479. }
  480. return ShouldBreakLineBefore(aContent, aRootNode) ?
  481. GetBRLength(LINE_BREAK_TYPE_NATIVE) : 0;
  482. }
  483. /* static inline */ uint32_t
  484. ContentEventHandler::GetBRLength(LineBreakType aLineBreakType)
  485. {
  486. #if defined(XP_WIN)
  487. // Length of \r\n
  488. return (aLineBreakType == LINE_BREAK_TYPE_NATIVE) ? 2 : 1;
  489. #else
  490. return 1;
  491. #endif
  492. }
  493. /* static */ uint32_t
  494. ContentEventHandler::GetTextLength(nsIContent* aContent,
  495. LineBreakType aLineBreakType,
  496. uint32_t aMaxLength)
  497. {
  498. MOZ_ASSERT(aContent->IsNodeOfType(nsINode::eTEXT));
  499. uint32_t textLengthDifference =
  500. #if defined(XP_WIN)
  501. // On Windows, the length of a native newline ("\r\n") is twice the length
  502. // of the XP newline ("\n"), so XP length is equal to the length of the
  503. // native offset plus the number of newlines encountered in the string.
  504. (aLineBreakType == LINE_BREAK_TYPE_NATIVE) ?
  505. CountNewlinesInXPLength(aContent, aMaxLength) : 0;
  506. #else
  507. // On other platforms, the native and XP newlines are the same.
  508. 0;
  509. #endif
  510. const nsTextFragment* text = aContent->GetText();
  511. if (!text) {
  512. return 0;
  513. }
  514. uint32_t length = std::min(text->GetLength(), aMaxLength);
  515. return length + textLengthDifference;
  516. }
  517. static uint32_t ConvertToXPOffset(nsIContent* aContent, uint32_t aNativeOffset)
  518. {
  519. #if defined(XP_WIN)
  520. // On Windows, the length of a native newline ("\r\n") is twice the length of
  521. // the XP newline ("\n"), so XP offset is equal to the length of the native
  522. // offset minus the number of newlines encountered in the string.
  523. return aNativeOffset - CountNewlinesInNativeLength(aContent, aNativeOffset);
  524. #else
  525. // On other platforms, the native and XP newlines are the same.
  526. return aNativeOffset;
  527. #endif
  528. }
  529. /* static */ bool
  530. ContentEventHandler::ShouldBreakLineBefore(nsIContent* aContent,
  531. nsINode* aRootNode)
  532. {
  533. // We don't need to append linebreak at the start of the root element.
  534. if (aContent == aRootNode) {
  535. return false;
  536. }
  537. // If it's not an HTML element (including other markup language's elements),
  538. // we shouldn't insert like break before that for now. Becoming this is a
  539. // problem must be edge case. E.g., when ContentEventHandler is used with
  540. // MathML or SVG elements.
  541. if (!aContent->IsHTMLElement()) {
  542. return false;
  543. }
  544. // If the element is <br>, we need to check if the <br> is caused by web
  545. // content. Otherwise, i.e., it's caused by internal reason of Gecko,
  546. // it shouldn't be exposed as a line break to flatten text.
  547. if (aContent->IsHTMLElement(nsGkAtoms::br)) {
  548. return IsContentBR(aContent);
  549. }
  550. // Note that ideally, we should refer the style of the primary frame of
  551. // aContent for deciding if it's an inline. However, it's difficult
  552. // IMEContentObserver to notify IME of text change caused by style change.
  553. // Therefore, currently, we should check only from the tag for now.
  554. if (aContent->IsAnyOfHTMLElements(nsGkAtoms::a,
  555. nsGkAtoms::abbr,
  556. nsGkAtoms::acronym,
  557. nsGkAtoms::b,
  558. nsGkAtoms::bdi,
  559. nsGkAtoms::bdo,
  560. nsGkAtoms::big,
  561. nsGkAtoms::cite,
  562. nsGkAtoms::code,
  563. nsGkAtoms::data,
  564. nsGkAtoms::del,
  565. nsGkAtoms::dfn,
  566. nsGkAtoms::em,
  567. nsGkAtoms::font,
  568. nsGkAtoms::i,
  569. nsGkAtoms::ins,
  570. nsGkAtoms::kbd,
  571. nsGkAtoms::mark,
  572. nsGkAtoms::s,
  573. nsGkAtoms::samp,
  574. nsGkAtoms::small,
  575. nsGkAtoms::span,
  576. nsGkAtoms::strike,
  577. nsGkAtoms::strong,
  578. nsGkAtoms::sub,
  579. nsGkAtoms::sup,
  580. nsGkAtoms::time,
  581. nsGkAtoms::tt,
  582. nsGkAtoms::u,
  583. nsGkAtoms::var)) {
  584. return false;
  585. }
  586. // If the element is unknown element, we shouldn't insert line breaks before
  587. // it since unknown elements should be ignored.
  588. RefPtr<HTMLUnknownElement> unknownHTMLElement = do_QueryObject(aContent);
  589. return !unknownHTMLElement;
  590. }
  591. nsresult
  592. ContentEventHandler::GenerateFlatTextContent(nsIContent* aContent,
  593. nsAFlatString& aString,
  594. LineBreakType aLineBreakType)
  595. {
  596. MOZ_ASSERT(aString.IsEmpty());
  597. RefPtr<nsRange> range = new nsRange(mRootContent);
  598. ErrorResult rv;
  599. range->SelectNodeContents(*aContent, rv);
  600. if (NS_WARN_IF(rv.Failed())) {
  601. return rv.StealNSResult();
  602. }
  603. return GenerateFlatTextContent(range, aString, aLineBreakType);
  604. }
  605. nsresult
  606. ContentEventHandler::GenerateFlatTextContent(nsRange* aRange,
  607. nsAFlatString& aString,
  608. LineBreakType aLineBreakType)
  609. {
  610. MOZ_ASSERT(aString.IsEmpty());
  611. if (aRange->Collapsed()) {
  612. return NS_OK;
  613. }
  614. nsINode* startNode = aRange->GetStartParent();
  615. nsINode* endNode = aRange->GetEndParent();
  616. if (NS_WARN_IF(!startNode) || NS_WARN_IF(!endNode)) {
  617. return NS_ERROR_FAILURE;
  618. }
  619. if (startNode == endNode && startNode->IsNodeOfType(nsINode::eTEXT)) {
  620. nsIContent* content = startNode->AsContent();
  621. AppendSubString(aString, content, aRange->StartOffset(),
  622. aRange->EndOffset() - aRange->StartOffset());
  623. ConvertToNativeNewlines(aString);
  624. return NS_OK;
  625. }
  626. nsCOMPtr<nsIContentIterator> iter = NS_NewPreContentIterator();
  627. nsresult rv = iter->Init(aRange);
  628. if (NS_WARN_IF(NS_FAILED(rv))) {
  629. return rv;
  630. }
  631. for (; !iter->IsDone(); iter->Next()) {
  632. nsINode* node = iter->GetCurrentNode();
  633. if (NS_WARN_IF(!node)) {
  634. break;
  635. }
  636. if (!node->IsContent()) {
  637. continue;
  638. }
  639. nsIContent* content = node->AsContent();
  640. if (content->IsNodeOfType(nsINode::eTEXT)) {
  641. if (content == startNode) {
  642. AppendSubString(aString, content, aRange->StartOffset(),
  643. content->TextLength() - aRange->StartOffset());
  644. } else if (content == endNode) {
  645. AppendSubString(aString, content, 0, aRange->EndOffset());
  646. } else {
  647. AppendString(aString, content);
  648. }
  649. } else if (ShouldBreakLineBefore(content, mRootContent)) {
  650. aString.Append(char16_t('\n'));
  651. }
  652. }
  653. if (aLineBreakType == LINE_BREAK_TYPE_NATIVE) {
  654. ConvertToNativeNewlines(aString);
  655. }
  656. return NS_OK;
  657. }
  658. static FontRange*
  659. AppendFontRange(nsTArray<FontRange>& aFontRanges, uint32_t aBaseOffset)
  660. {
  661. FontRange* fontRange = aFontRanges.AppendElement();
  662. fontRange->mStartOffset = aBaseOffset;
  663. return fontRange;
  664. }
  665. /* static */ uint32_t
  666. ContentEventHandler::GetTextLengthInRange(nsIContent* aContent,
  667. uint32_t aXPStartOffset,
  668. uint32_t aXPEndOffset,
  669. LineBreakType aLineBreakType)
  670. {
  671. MOZ_ASSERT(aContent->IsNodeOfType(nsINode::eTEXT));
  672. return aLineBreakType == LINE_BREAK_TYPE_NATIVE ?
  673. GetNativeTextLength(aContent, aXPStartOffset, aXPEndOffset) :
  674. aXPEndOffset - aXPStartOffset;
  675. }
  676. /* static */ void
  677. ContentEventHandler::AppendFontRanges(FontRangeArray& aFontRanges,
  678. nsIContent* aContent,
  679. int32_t aBaseOffset,
  680. int32_t aXPStartOffset,
  681. int32_t aXPEndOffset,
  682. LineBreakType aLineBreakType)
  683. {
  684. MOZ_ASSERT(aContent->IsNodeOfType(nsINode::eTEXT));
  685. nsIFrame* frame = aContent->GetPrimaryFrame();
  686. if (!frame) {
  687. // It is a non-rendered content, create an empty range for it.
  688. AppendFontRange(aFontRanges, aBaseOffset);
  689. return;
  690. }
  691. int32_t baseOffset = aBaseOffset;
  692. nsTextFrame* curr = do_QueryFrame(frame);
  693. MOZ_ASSERT(curr, "Not a text frame");
  694. while (curr) {
  695. int32_t frameXPStart = std::max(curr->GetContentOffset(), aXPStartOffset);
  696. int32_t frameXPEnd = std::min(curr->GetContentEnd(), aXPEndOffset);
  697. if (frameXPStart >= frameXPEnd) {
  698. curr = static_cast<nsTextFrame*>(curr->GetNextContinuation());
  699. continue;
  700. }
  701. gfxSkipCharsIterator iter = curr->EnsureTextRun(nsTextFrame::eInflated);
  702. gfxTextRun* textRun = curr->GetTextRun(nsTextFrame::eInflated);
  703. nsTextFrame* next = nullptr;
  704. if (frameXPEnd < aXPEndOffset) {
  705. next = static_cast<nsTextFrame*>(curr->GetNextContinuation());
  706. while (next && next->GetTextRun(nsTextFrame::eInflated) == textRun) {
  707. frameXPEnd = std::min(next->GetContentEnd(), aXPEndOffset);
  708. next = frameXPEnd < aXPEndOffset ?
  709. static_cast<nsTextFrame*>(next->GetNextContinuation()) : nullptr;
  710. }
  711. }
  712. gfxTextRun::Range skipRange(iter.ConvertOriginalToSkipped(frameXPStart),
  713. iter.ConvertOriginalToSkipped(frameXPEnd));
  714. gfxTextRun::GlyphRunIterator runIter(textRun, skipRange);
  715. int32_t lastXPEndOffset = frameXPStart;
  716. while (runIter.NextRun()) {
  717. gfxFont* font = runIter.GetGlyphRun()->mFont.get();
  718. int32_t startXPOffset =
  719. iter.ConvertSkippedToOriginal(runIter.GetStringStart());
  720. // It is possible that the first glyph run has exceeded the frame,
  721. // because the whole frame is filled by skipped chars.
  722. if (startXPOffset >= frameXPEnd) {
  723. break;
  724. }
  725. if (startXPOffset > lastXPEndOffset) {
  726. // Create range for skipped leading chars.
  727. AppendFontRange(aFontRanges, baseOffset);
  728. baseOffset += GetTextLengthInRange(
  729. aContent, lastXPEndOffset, startXPOffset, aLineBreakType);
  730. lastXPEndOffset = startXPOffset;
  731. }
  732. FontRange* fontRange = AppendFontRange(aFontRanges, baseOffset);
  733. fontRange->mFontName = font->GetName();
  734. fontRange->mFontSize = font->GetAdjustedSize();
  735. // The converted original offset may exceed the range,
  736. // hence we need to clamp it.
  737. int32_t endXPOffset =
  738. iter.ConvertSkippedToOriginal(runIter.GetStringEnd());
  739. endXPOffset = std::min(frameXPEnd, endXPOffset);
  740. baseOffset += GetTextLengthInRange(aContent, startXPOffset, endXPOffset,
  741. aLineBreakType);
  742. lastXPEndOffset = endXPOffset;
  743. }
  744. if (lastXPEndOffset < frameXPEnd) {
  745. // Create range for skipped trailing chars. It also handles case
  746. // that the whole frame contains only skipped chars.
  747. AppendFontRange(aFontRanges, baseOffset);
  748. baseOffset += GetTextLengthInRange(
  749. aContent, lastXPEndOffset, frameXPEnd, aLineBreakType);
  750. }
  751. curr = next;
  752. }
  753. }
  754. nsresult
  755. ContentEventHandler::GenerateFlatFontRanges(nsRange* aRange,
  756. FontRangeArray& aFontRanges,
  757. uint32_t& aLength,
  758. LineBreakType aLineBreakType)
  759. {
  760. MOZ_ASSERT(aFontRanges.IsEmpty(), "aRanges must be empty array");
  761. if (aRange->Collapsed()) {
  762. return NS_OK;
  763. }
  764. nsINode* startNode = aRange->GetStartParent();
  765. nsINode* endNode = aRange->GetEndParent();
  766. if (NS_WARN_IF(!startNode) || NS_WARN_IF(!endNode)) {
  767. return NS_ERROR_FAILURE;
  768. }
  769. // baseOffset is the flattened offset of each content node.
  770. int32_t baseOffset = 0;
  771. nsCOMPtr<nsIContentIterator> iter = NS_NewPreContentIterator();
  772. nsresult rv = iter->Init(aRange);
  773. if (NS_WARN_IF(NS_FAILED(rv))) {
  774. return rv;
  775. }
  776. for (; !iter->IsDone(); iter->Next()) {
  777. nsINode* node = iter->GetCurrentNode();
  778. if (NS_WARN_IF(!node)) {
  779. break;
  780. }
  781. if (!node->IsContent()) {
  782. continue;
  783. }
  784. nsIContent* content = node->AsContent();
  785. if (content->IsNodeOfType(nsINode::eTEXT)) {
  786. int32_t startOffset = content != startNode ? 0 : aRange->StartOffset();
  787. int32_t endOffset = content != endNode ?
  788. content->TextLength() : aRange->EndOffset();
  789. AppendFontRanges(aFontRanges, content, baseOffset,
  790. startOffset, endOffset, aLineBreakType);
  791. baseOffset += GetTextLengthInRange(content, startOffset, endOffset,
  792. aLineBreakType);
  793. } else if (ShouldBreakLineBefore(content, mRootContent)) {
  794. if (aFontRanges.IsEmpty()) {
  795. MOZ_ASSERT(baseOffset == 0);
  796. FontRange* fontRange = AppendFontRange(aFontRanges, baseOffset);
  797. nsIFrame* frame = content->GetPrimaryFrame();
  798. if (frame) {
  799. const nsFont& font = frame->GetParent()->StyleFont()->mFont;
  800. const FontFamilyList& fontList = font.fontlist;
  801. const FontFamilyName& fontName = fontList.IsEmpty() ?
  802. FontFamilyName(fontList.GetDefaultFontType()) :
  803. fontList.GetFontlist()[0];
  804. fontName.AppendToString(fontRange->mFontName, false);
  805. fontRange->mFontSize =
  806. frame->PresContext()->AppUnitsToDevPixels(font.size);
  807. }
  808. }
  809. baseOffset += GetBRLength(aLineBreakType);
  810. }
  811. }
  812. aLength = baseOffset;
  813. return NS_OK;
  814. }
  815. nsresult
  816. ContentEventHandler::ExpandToClusterBoundary(nsIContent* aContent,
  817. bool aForward,
  818. uint32_t* aXPOffset)
  819. {
  820. // XXX This method assumes that the frame boundaries must be cluster
  821. // boundaries. It's false, but no problem now, maybe.
  822. if (!aContent->IsNodeOfType(nsINode::eTEXT) ||
  823. *aXPOffset == 0 || *aXPOffset == aContent->TextLength()) {
  824. return NS_OK;
  825. }
  826. NS_ASSERTION(*aXPOffset <= aContent->TextLength(),
  827. "offset is out of range.");
  828. RefPtr<nsFrameSelection> fs = mPresShell->FrameSelection();
  829. int32_t offsetInFrame;
  830. CaretAssociationHint hint =
  831. aForward ? CARET_ASSOCIATE_BEFORE : CARET_ASSOCIATE_AFTER;
  832. nsIFrame* frame = fs->GetFrameForNodeOffset(aContent, int32_t(*aXPOffset),
  833. hint, &offsetInFrame);
  834. if (!frame) {
  835. // This content doesn't have any frames, we only can check surrogate pair...
  836. const nsTextFragment* text = aContent->GetText();
  837. NS_ENSURE_TRUE(text, NS_ERROR_FAILURE);
  838. if (NS_IS_LOW_SURROGATE(text->CharAt(*aXPOffset)) &&
  839. NS_IS_HIGH_SURROGATE(text->CharAt(*aXPOffset - 1))) {
  840. *aXPOffset += aForward ? 1 : -1;
  841. }
  842. return NS_OK;
  843. }
  844. int32_t startOffset, endOffset;
  845. nsresult rv = frame->GetOffsets(startOffset, endOffset);
  846. NS_ENSURE_SUCCESS(rv, rv);
  847. if (*aXPOffset == static_cast<uint32_t>(startOffset) ||
  848. *aXPOffset == static_cast<uint32_t>(endOffset)) {
  849. return NS_OK;
  850. }
  851. if (frame->GetType() != nsGkAtoms::textFrame) {
  852. return NS_ERROR_FAILURE;
  853. }
  854. nsTextFrame* textFrame = static_cast<nsTextFrame*>(frame);
  855. int32_t newOffsetInFrame = *aXPOffset - startOffset;
  856. newOffsetInFrame += aForward ? -1 : 1;
  857. textFrame->PeekOffsetCharacter(aForward, &newOffsetInFrame);
  858. *aXPOffset = startOffset + newOffsetInFrame;
  859. return NS_OK;
  860. }
  861. nsresult
  862. ContentEventHandler::SetRangeFromFlatTextOffset(nsRange* aRange,
  863. uint32_t aOffset,
  864. uint32_t aLength,
  865. LineBreakType aLineBreakType,
  866. bool aExpandToClusterBoundaries,
  867. uint32_t* aNewOffset,
  868. nsIContent** aLastTextNode)
  869. {
  870. if (aNewOffset) {
  871. *aNewOffset = aOffset;
  872. }
  873. if (aLastTextNode) {
  874. *aLastTextNode = nullptr;
  875. }
  876. // Special case like <br contenteditable>
  877. if (!mRootContent->HasChildren()) {
  878. nsresult rv = aRange->CollapseTo(mRootContent, 0);
  879. if (NS_WARN_IF(NS_FAILED(rv))) {
  880. return rv;
  881. }
  882. }
  883. nsCOMPtr<nsIContentIterator> iter = NS_NewPreContentIterator();
  884. nsresult rv = iter->Init(mRootContent);
  885. if (NS_WARN_IF(NS_FAILED(rv))) {
  886. return rv;
  887. }
  888. uint32_t offset = 0;
  889. uint32_t endOffset = aOffset + aLength;
  890. bool startSet = false;
  891. for (; !iter->IsDone(); iter->Next()) {
  892. nsINode* node = iter->GetCurrentNode();
  893. if (NS_WARN_IF(!node)) {
  894. break;
  895. }
  896. // FYI: mRootContent shouldn't cause any text. So, we can skip it simply.
  897. if (node == mRootContent || !node->IsContent()) {
  898. continue;
  899. }
  900. nsIContent* content = node->AsContent();
  901. if (aLastTextNode && content->IsNodeOfType(nsINode::eTEXT)) {
  902. NS_IF_RELEASE(*aLastTextNode);
  903. NS_ADDREF(*aLastTextNode = content);
  904. }
  905. uint32_t textLength =
  906. content->IsNodeOfType(nsINode::eTEXT) ?
  907. GetTextLength(content, aLineBreakType) :
  908. (ShouldBreakLineBefore(content, mRootContent) ?
  909. GetBRLength(aLineBreakType) : 0);
  910. if (!textLength) {
  911. continue;
  912. }
  913. // When the start offset is in between accumulated offset and the last
  914. // offset of the node, the node is the start node of the range.
  915. if (!startSet && aOffset <= offset + textLength) {
  916. nsINode* startNode = nullptr;
  917. int32_t startNodeOffset = -1;
  918. if (content->IsNodeOfType(nsINode::eTEXT)) {
  919. // Rule #1.1: [textNode or text[Node or textNode[
  920. uint32_t xpOffset = aOffset - offset;
  921. if (aLineBreakType == LINE_BREAK_TYPE_NATIVE) {
  922. xpOffset = ConvertToXPOffset(content, xpOffset);
  923. }
  924. if (aExpandToClusterBoundaries) {
  925. uint32_t oldXPOffset = xpOffset;
  926. rv = ExpandToClusterBoundary(content, false, &xpOffset);
  927. if (NS_WARN_IF(NS_FAILED(rv))) {
  928. return rv;
  929. }
  930. if (aNewOffset) {
  931. // This is correct since a cluster shouldn't include line break.
  932. *aNewOffset -= (oldXPOffset - xpOffset);
  933. }
  934. }
  935. startNode = content;
  936. startNodeOffset = static_cast<int32_t>(xpOffset);
  937. } else if (aOffset < offset + textLength) {
  938. // Rule #1.2 [<element>
  939. startNode = content->GetParent();
  940. if (NS_WARN_IF(!startNode)) {
  941. return NS_ERROR_FAILURE;
  942. }
  943. startNodeOffset = startNode->IndexOf(content);
  944. if (NS_WARN_IF(startNodeOffset == -1)) {
  945. // The content is being removed from the parent!
  946. return NS_ERROR_FAILURE;
  947. }
  948. } else if (!content->HasChildren()) {
  949. // Rule #1.3: <element/>[
  950. startNode = content->GetParent();
  951. if (NS_WARN_IF(!startNode)) {
  952. return NS_ERROR_FAILURE;
  953. }
  954. startNodeOffset = startNode->IndexOf(content) + 1;
  955. if (NS_WARN_IF(startNodeOffset == 0)) {
  956. // The content is being removed from the parent!
  957. return NS_ERROR_FAILURE;
  958. }
  959. } else {
  960. // Rule #1.4: <element>[
  961. startNode = content;
  962. startNodeOffset = 0;
  963. }
  964. NS_ASSERTION(startNode, "startNode must not be nullptr");
  965. NS_ASSERTION(startNodeOffset >= 0,
  966. "startNodeOffset must not be negative");
  967. rv = aRange->SetStart(startNode, startNodeOffset);
  968. if (NS_WARN_IF(NS_FAILED(rv))) {
  969. return rv;
  970. }
  971. startSet = true;
  972. if (!aLength) {
  973. rv = aRange->SetEnd(startNode, startNodeOffset);
  974. if (NS_WARN_IF(NS_FAILED(rv))) {
  975. return rv;
  976. }
  977. return NS_OK;
  978. }
  979. }
  980. // When the end offset is in the content, the node is the end node of the
  981. // range.
  982. if (endOffset <= offset + textLength) {
  983. MOZ_ASSERT(startSet,
  984. "The start of the range should've been set already");
  985. if (content->IsNodeOfType(nsINode::eTEXT)) {
  986. // Rule #2.1: ]textNode or text]Node or textNode]
  987. uint32_t xpOffset = endOffset - offset;
  988. if (aLineBreakType == LINE_BREAK_TYPE_NATIVE) {
  989. uint32_t xpOffsetCurrent = ConvertToXPOffset(content, xpOffset);
  990. if (xpOffset && GetBRLength(aLineBreakType) > 1) {
  991. MOZ_ASSERT(GetBRLength(aLineBreakType) == 2);
  992. uint32_t xpOffsetPre = ConvertToXPOffset(content, xpOffset - 1);
  993. // If previous character's XP offset is same as current character's,
  994. // it means that the end offset is between \r and \n. So, the
  995. // range end should be after the \n.
  996. if (xpOffsetPre == xpOffsetCurrent) {
  997. xpOffset = xpOffsetCurrent + 1;
  998. } else {
  999. xpOffset = xpOffsetCurrent;
  1000. }
  1001. }
  1002. }
  1003. if (aExpandToClusterBoundaries) {
  1004. rv = ExpandToClusterBoundary(content, true, &xpOffset);
  1005. if (NS_WARN_IF(NS_FAILED(rv))) {
  1006. return rv;
  1007. }
  1008. }
  1009. NS_ASSERTION(xpOffset <= INT32_MAX,
  1010. "The end node offset is too large");
  1011. rv = aRange->SetEnd(content, static_cast<int32_t>(xpOffset));
  1012. if (NS_WARN_IF(NS_FAILED(rv))) {
  1013. return rv;
  1014. }
  1015. return NS_OK;
  1016. }
  1017. if (endOffset == offset) {
  1018. // Rule #2.2: ]<element>
  1019. // NOTE: Please don't crash on release builds because it must be
  1020. // overreaction but we shouldn't allow this bug when some
  1021. // automated tests find this.
  1022. MOZ_ASSERT(false, "This case should've already been handled at "
  1023. "the last node which caused some text");
  1024. return NS_ERROR_FAILURE;
  1025. }
  1026. if (content->HasChildren() &&
  1027. ShouldBreakLineBefore(content, mRootContent)) {
  1028. // Rule #2.3: </element>]
  1029. rv = aRange->SetEnd(content, 0);
  1030. if (NS_WARN_IF(NS_FAILED(rv))) {
  1031. return rv;
  1032. }
  1033. return NS_OK;
  1034. }
  1035. // Rule #2.4: <element/>]
  1036. nsINode* endNode = content->GetParent();
  1037. if (NS_WARN_IF(!endNode)) {
  1038. return NS_ERROR_FAILURE;
  1039. }
  1040. int32_t indexInParent = endNode->IndexOf(content);
  1041. if (NS_WARN_IF(indexInParent == -1)) {
  1042. // The content is being removed from the parent!
  1043. return NS_ERROR_FAILURE;
  1044. }
  1045. rv = aRange->SetEnd(endNode, indexInParent + 1);
  1046. if (NS_WARN_IF(NS_FAILED(rv))) {
  1047. return rv;
  1048. }
  1049. return NS_OK;
  1050. }
  1051. offset += textLength;
  1052. }
  1053. if (!startSet) {
  1054. MOZ_ASSERT(!mRootContent->IsNodeOfType(nsINode::eTEXT));
  1055. if (!offset) {
  1056. // Rule #1.5: <root>[</root>
  1057. // When there are no nodes causing text, the start of the DOM range
  1058. // should be start of the root node since clicking on such editor (e.g.,
  1059. // <div contenteditable><span></span></div>) sets caret to the start of
  1060. // the editor (i.e., before <span> in the example).
  1061. rv = aRange->SetStart(mRootContent, 0);
  1062. if (NS_WARN_IF(NS_FAILED(rv))) {
  1063. return rv;
  1064. }
  1065. if (!aLength) {
  1066. rv = aRange->SetEnd(mRootContent, 0);
  1067. if (NS_WARN_IF(NS_FAILED(rv))) {
  1068. return rv;
  1069. }
  1070. return NS_OK;
  1071. }
  1072. } else {
  1073. // Rule #1.5: [</root>
  1074. rv = aRange->SetStart(mRootContent,
  1075. static_cast<int32_t>(mRootContent->GetChildCount()));
  1076. if (NS_WARN_IF(NS_FAILED(rv))) {
  1077. return rv;
  1078. }
  1079. }
  1080. if (aNewOffset) {
  1081. *aNewOffset = offset;
  1082. }
  1083. }
  1084. // Rule #2.5: ]</root>
  1085. rv = aRange->SetEnd(mRootContent,
  1086. static_cast<int32_t>(mRootContent->GetChildCount()));
  1087. if (NS_WARN_IF(NS_FAILED(rv))) {
  1088. return rv;
  1089. }
  1090. return NS_OK;
  1091. }
  1092. /* static */ LineBreakType
  1093. ContentEventHandler::GetLineBreakType(WidgetQueryContentEvent* aEvent)
  1094. {
  1095. return GetLineBreakType(aEvent->mUseNativeLineBreak);
  1096. }
  1097. /* static */ LineBreakType
  1098. ContentEventHandler::GetLineBreakType(WidgetSelectionEvent* aEvent)
  1099. {
  1100. return GetLineBreakType(aEvent->mUseNativeLineBreak);
  1101. }
  1102. /* static */ LineBreakType
  1103. ContentEventHandler::GetLineBreakType(bool aUseNativeLineBreak)
  1104. {
  1105. return aUseNativeLineBreak ?
  1106. LINE_BREAK_TYPE_NATIVE : LINE_BREAK_TYPE_XP;
  1107. }
  1108. nsresult
  1109. ContentEventHandler::HandleQueryContentEvent(WidgetQueryContentEvent* aEvent)
  1110. {
  1111. switch (aEvent->mMessage) {
  1112. case eQuerySelectedText:
  1113. return OnQuerySelectedText(aEvent);
  1114. case eQueryTextContent:
  1115. return OnQueryTextContent(aEvent);
  1116. case eQueryCaretRect:
  1117. return OnQueryCaretRect(aEvent);
  1118. case eQueryTextRect:
  1119. return OnQueryTextRect(aEvent);
  1120. case eQueryTextRectArray:
  1121. return OnQueryTextRectArray(aEvent);
  1122. case eQueryEditorRect:
  1123. return OnQueryEditorRect(aEvent);
  1124. case eQueryContentState:
  1125. return OnQueryContentState(aEvent);
  1126. case eQuerySelectionAsTransferable:
  1127. return OnQuerySelectionAsTransferable(aEvent);
  1128. case eQueryCharacterAtPoint:
  1129. return OnQueryCharacterAtPoint(aEvent);
  1130. case eQueryDOMWidgetHittest:
  1131. return OnQueryDOMWidgetHittest(aEvent);
  1132. default:
  1133. return NS_ERROR_NOT_IMPLEMENTED;
  1134. }
  1135. return NS_OK;
  1136. }
  1137. // Similar to nsFrameSelection::GetFrameForNodeOffset,
  1138. // but this is more flexible for OnQueryTextRect to use
  1139. static nsresult GetFrameForTextRect(nsINode* aNode,
  1140. int32_t aNodeOffset,
  1141. bool aHint,
  1142. nsIFrame** aReturnFrame)
  1143. {
  1144. NS_ENSURE_TRUE(aNode && aNode->IsNodeOfType(nsINode::eCONTENT),
  1145. NS_ERROR_UNEXPECTED);
  1146. nsIContent* content = static_cast<nsIContent*>(aNode);
  1147. nsIFrame* frame = content->GetPrimaryFrame();
  1148. NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);
  1149. int32_t childNodeOffset = 0;
  1150. return frame->GetChildFrameContainingOffset(aNodeOffset, aHint,
  1151. &childNodeOffset, aReturnFrame);
  1152. }
  1153. nsresult
  1154. ContentEventHandler::OnQuerySelectedText(WidgetQueryContentEvent* aEvent)
  1155. {
  1156. nsresult rv = Init(aEvent);
  1157. if (NS_FAILED(rv)) {
  1158. return rv;
  1159. }
  1160. if (!mFirstSelectedRange) {
  1161. MOZ_ASSERT(aEvent->mInput.mSelectionType != SelectionType::eNormal);
  1162. MOZ_ASSERT(aEvent->mReply.mOffset == WidgetQueryContentEvent::NOT_FOUND);
  1163. MOZ_ASSERT(aEvent->mReply.mString.IsEmpty());
  1164. MOZ_ASSERT(!aEvent->mReply.mHasSelection);
  1165. aEvent->mSucceeded = true;
  1166. return NS_OK;
  1167. }
  1168. nsINode* const startNode = mFirstSelectedRange->GetStartParent();
  1169. nsINode* const endNode = mFirstSelectedRange->GetEndParent();
  1170. // Make sure the selection is within the root content range.
  1171. if (!nsContentUtils::ContentIsDescendantOf(startNode, mRootContent) ||
  1172. !nsContentUtils::ContentIsDescendantOf(endNode, mRootContent)) {
  1173. return NS_ERROR_NOT_AVAILABLE;
  1174. }
  1175. NS_ASSERTION(aEvent->mReply.mString.IsEmpty(),
  1176. "The reply string must be empty");
  1177. LineBreakType lineBreakType = GetLineBreakType(aEvent);
  1178. rv = GetStartOffset(mFirstSelectedRange,
  1179. &aEvent->mReply.mOffset, lineBreakType);
  1180. NS_ENSURE_SUCCESS(rv, rv);
  1181. nsCOMPtr<nsINode> anchorNode, focusNode;
  1182. int32_t anchorOffset = 0, focusOffset = 0;
  1183. if (mSelection->RangeCount()) {
  1184. // If there is only one selection range, the anchor/focus node and offset
  1185. // are the information of the range. Therefore, we have the direction
  1186. // information.
  1187. if (mSelection->RangeCount() == 1) {
  1188. anchorNode = mSelection->GetAnchorNode();
  1189. focusNode = mSelection->GetFocusNode();
  1190. if (NS_WARN_IF(!anchorNode) || NS_WARN_IF(!focusNode)) {
  1191. return NS_ERROR_FAILURE;
  1192. }
  1193. anchorOffset = static_cast<int32_t>(mSelection->AnchorOffset());
  1194. focusOffset = static_cast<int32_t>(mSelection->FocusOffset());
  1195. if (NS_WARN_IF(anchorOffset < 0) || NS_WARN_IF(focusOffset < 0)) {
  1196. return NS_ERROR_FAILURE;
  1197. }
  1198. int16_t compare = nsContentUtils::ComparePoints(anchorNode, anchorOffset,
  1199. focusNode, focusOffset);
  1200. aEvent->mReply.mReversed = compare > 0;
  1201. }
  1202. // However, if there are 2 or more selection ranges, we have no information
  1203. // of that.
  1204. else {
  1205. aEvent->mReply.mReversed = false;
  1206. }
  1207. if (!mFirstSelectedRange->Collapsed()) {
  1208. rv = GenerateFlatTextContent(mFirstSelectedRange, aEvent->mReply.mString,
  1209. lineBreakType);
  1210. if (NS_WARN_IF(NS_FAILED(rv))) {
  1211. return rv;
  1212. }
  1213. } else {
  1214. aEvent->mReply.mString.Truncate();
  1215. }
  1216. } else {
  1217. NS_ASSERTION(mFirstSelectedRange->Collapsed(),
  1218. "When mSelection doesn't have selection, mFirstSelectedRange must be "
  1219. "collapsed");
  1220. anchorNode = focusNode = mFirstSelectedRange->GetStartParent();
  1221. if (NS_WARN_IF(!anchorNode)) {
  1222. return NS_ERROR_FAILURE;
  1223. }
  1224. anchorOffset = focusOffset =
  1225. static_cast<int32_t>(mFirstSelectedRange->StartOffset());
  1226. if (NS_WARN_IF(anchorOffset < 0)) {
  1227. return NS_ERROR_FAILURE;
  1228. }
  1229. aEvent->mReply.mReversed = false;
  1230. aEvent->mReply.mString.Truncate();
  1231. }
  1232. nsIFrame* frame = nullptr;
  1233. rv = GetFrameForTextRect(focusNode, focusOffset, true, &frame);
  1234. if (NS_SUCCEEDED(rv) && frame) {
  1235. aEvent->mReply.mWritingMode = frame->GetWritingMode();
  1236. } else {
  1237. aEvent->mReply.mWritingMode = WritingMode();
  1238. }
  1239. aEvent->mSucceeded = true;
  1240. return NS_OK;
  1241. }
  1242. nsresult
  1243. ContentEventHandler::OnQueryTextContent(WidgetQueryContentEvent* aEvent)
  1244. {
  1245. nsresult rv = Init(aEvent);
  1246. if (NS_FAILED(rv)) {
  1247. return rv;
  1248. }
  1249. NS_ASSERTION(aEvent->mReply.mString.IsEmpty(),
  1250. "The reply string must be empty");
  1251. LineBreakType lineBreakType = GetLineBreakType(aEvent);
  1252. RefPtr<nsRange> range = new nsRange(mRootContent);
  1253. rv = SetRangeFromFlatTextOffset(range, aEvent->mInput.mOffset,
  1254. aEvent->mInput.mLength, lineBreakType, false,
  1255. &aEvent->mReply.mOffset);
  1256. NS_ENSURE_SUCCESS(rv, rv);
  1257. rv = GenerateFlatTextContent(range, aEvent->mReply.mString, lineBreakType);
  1258. NS_ENSURE_SUCCESS(rv, rv);
  1259. if (aEvent->mWithFontRanges) {
  1260. uint32_t fontRangeLength;
  1261. rv = GenerateFlatFontRanges(range, aEvent->mReply.mFontRanges,
  1262. fontRangeLength, lineBreakType);
  1263. if (NS_WARN_IF(NS_FAILED(rv))) {
  1264. return rv;
  1265. }
  1266. MOZ_ASSERT(fontRangeLength == aEvent->mReply.mString.Length(),
  1267. "Font ranges doesn't match the string");
  1268. }
  1269. aEvent->mSucceeded = true;
  1270. return NS_OK;
  1271. }
  1272. void
  1273. ContentEventHandler::EnsureNonEmptyRect(nsRect& aRect) const
  1274. {
  1275. // See the comment in ContentEventHandler.h why this doesn't set them to
  1276. // one device pixel.
  1277. aRect.height = std::max(1, aRect.height);
  1278. aRect.width = std::max(1, aRect.width);
  1279. }
  1280. void
  1281. ContentEventHandler::EnsureNonEmptyRect(LayoutDeviceIntRect& aRect) const
  1282. {
  1283. aRect.height = std::max(1, aRect.height);
  1284. aRect.width = std::max(1, aRect.width);
  1285. }
  1286. ContentEventHandler::NodePosition
  1287. ContentEventHandler::GetNodePositionHavingFlatText(
  1288. const NodePosition& aNodePosition)
  1289. {
  1290. return GetNodePositionHavingFlatText(aNodePosition.mNode,
  1291. aNodePosition.mOffset);
  1292. }
  1293. ContentEventHandler::NodePosition
  1294. ContentEventHandler::GetNodePositionHavingFlatText(nsINode* aNode,
  1295. int32_t aNodeOffset)
  1296. {
  1297. if (aNode->IsNodeOfType(nsINode::eTEXT)) {
  1298. return NodePosition(aNode, aNodeOffset);
  1299. }
  1300. int32_t childCount = static_cast<int32_t>(aNode->GetChildCount());
  1301. // If it's a empty element node, returns itself.
  1302. if (!childCount) {
  1303. MOZ_ASSERT(!aNodeOffset || aNodeOffset == 1);
  1304. return NodePosition(aNode, aNodeOffset);
  1305. }
  1306. // If there is a node at given position, return the start of it.
  1307. if (aNodeOffset < childCount) {
  1308. return NodePosition(aNode->GetChildAt(aNodeOffset), 0);
  1309. }
  1310. // If the offset represents "after" the node, we need to return the last
  1311. // child of it. For example, if a range is |<p>[<br>]</p>|, then, the
  1312. // end point is {<p>, 1}. In such case, callers need the <br> node.
  1313. if (aNodeOffset == childCount) {
  1314. NodePosition result;
  1315. result.mNode = aNode->GetChildAt(childCount - 1);
  1316. result.mOffset = result.mNode->IsNodeOfType(nsINode::eTEXT) ?
  1317. static_cast<int32_t>(result.mNode->AsContent()->TextLength()) : 1;
  1318. }
  1319. NS_WARNING("aNodeOffset is invalid value");
  1320. return NodePosition();
  1321. }
  1322. ContentEventHandler::FrameAndNodeOffset
  1323. ContentEventHandler::GetFirstFrameInRangeForTextRect(nsRange* aRange)
  1324. {
  1325. NodePosition nodePosition;
  1326. nsCOMPtr<nsIContentIterator> iter = NS_NewPreContentIterator();
  1327. for (iter->Init(aRange); !iter->IsDone(); iter->Next()) {
  1328. nsINode* node = iter->GetCurrentNode();
  1329. if (NS_WARN_IF(!node)) {
  1330. break;
  1331. }
  1332. if (!node->IsContent()) {
  1333. continue;
  1334. }
  1335. if (node->IsNodeOfType(nsINode::eTEXT)) {
  1336. // If the range starts at the end of a text node, we need to find
  1337. // next node which causes text.
  1338. int32_t offsetInNode =
  1339. node == aRange->GetStartParent() ? aRange->StartOffset() : 0;
  1340. if (static_cast<uint32_t>(offsetInNode) < node->Length()) {
  1341. nodePosition.mNode = node;
  1342. nodePosition.mOffset = offsetInNode;
  1343. break;
  1344. }
  1345. continue;
  1346. }
  1347. // If the element node causes a line break before it, it's the first
  1348. // node causing text.
  1349. if (ShouldBreakLineBefore(node->AsContent(), mRootContent) ||
  1350. IsMozBR(node->AsContent())) {
  1351. nodePosition.mNode = node;
  1352. nodePosition.mOffset = 0;
  1353. }
  1354. }
  1355. if (!nodePosition.IsValid()) {
  1356. return FrameAndNodeOffset();
  1357. }
  1358. nsIFrame* firstFrame = nullptr;
  1359. GetFrameForTextRect(nodePosition.mNode, nodePosition.mOffset,
  1360. true, &firstFrame);
  1361. return FrameAndNodeOffset(firstFrame, nodePosition.mOffset);
  1362. }
  1363. ContentEventHandler::FrameAndNodeOffset
  1364. ContentEventHandler::GetLastFrameInRangeForTextRect(nsRange* aRange)
  1365. {
  1366. NodePosition nodePosition;
  1367. nsCOMPtr<nsIContentIterator> iter = NS_NewPreContentIterator();
  1368. iter->Init(aRange);
  1369. nsINode* endNode = aRange->GetEndParent();
  1370. uint32_t endOffset = static_cast<uint32_t>(aRange->EndOffset());
  1371. // If the end point is start of a text node or specified by its parent and
  1372. // index, the node shouldn't be included into the range. For example,
  1373. // with this case, |<p>abc[<br>]def</p>|, the range ends at 3rd children of
  1374. // <p> (see the range creation rules, "2.4. Cases: <element/>]"). This causes
  1375. // following frames:
  1376. // +----+-----+
  1377. // | abc|[<br>|
  1378. // +----+-----+
  1379. // +----+
  1380. // |]def|
  1381. // +----+
  1382. // So, if this method includes the 2nd text frame's rect to its result, the
  1383. // caller will return too tall rect which includes 2 lines in this case isn't
  1384. // expected by native IME (e.g., popup of IME will be positioned at bottom
  1385. // of "d" instead of right-bottom of "c"). Therefore, this method shouldn't
  1386. // include the last frame when its content isn't really in aRange.
  1387. nsINode* nextNodeOfRangeEnd = nullptr;
  1388. if (endNode->IsNodeOfType(nsINode::eTEXT)) {
  1389. // Don't set nextNodeOfRangeEnd to the start node of aRange because if
  1390. // endNode is same as start node of the range, the text node shouldn't be
  1391. // next of range end even if the offset is 0. This could occur with empty
  1392. // text node.
  1393. if (!endOffset && aRange->GetStartParent() != endNode) {
  1394. nextNodeOfRangeEnd = endNode;
  1395. }
  1396. } else if (endOffset < endNode->GetChildCount()) {
  1397. nextNodeOfRangeEnd = endNode->GetChildAt(endOffset);
  1398. }
  1399. for (iter->Last(); !iter->IsDone(); iter->Prev()) {
  1400. nsINode* node = iter->GetCurrentNode();
  1401. if (NS_WARN_IF(!node)) {
  1402. break;
  1403. }
  1404. if (!node->IsContent() || node == nextNodeOfRangeEnd) {
  1405. continue;
  1406. }
  1407. if (node->IsNodeOfType(nsINode::eTEXT)) {
  1408. nodePosition.mNode = node;
  1409. if (node == aRange->GetEndParent()) {
  1410. nodePosition.mOffset = aRange->EndOffset();
  1411. } else {
  1412. nodePosition.mOffset = node->Length();
  1413. }
  1414. // If the text node is empty or the last node of the range but the index
  1415. // is 0, we should store current position but continue looking for
  1416. // previous node (If there are no nodes before it, we should use current
  1417. // node position for returning its frame).
  1418. if (!nodePosition.mOffset) {
  1419. continue;
  1420. }
  1421. break;
  1422. }
  1423. if (ShouldBreakLineBefore(node->AsContent(), mRootContent) ||
  1424. IsMozBR(node->AsContent())) {
  1425. nodePosition.mNode = node;
  1426. nodePosition.mOffset = 0;
  1427. break;
  1428. }
  1429. }
  1430. if (!nodePosition.IsValid()) {
  1431. return FrameAndNodeOffset();
  1432. }
  1433. nsIFrame* lastFrame = nullptr;
  1434. GetFrameForTextRect(nodePosition.mNode, nodePosition.mOffset,
  1435. true, &lastFrame);
  1436. if (!lastFrame) {
  1437. return FrameAndNodeOffset();
  1438. }
  1439. // If the last frame is a text frame, we need to check if the range actually
  1440. // includes at least one character in the range. Therefore, if it's not a
  1441. // text frame, we need to do nothing anymore.
  1442. if (lastFrame->GetType() != nsGkAtoms::textFrame) {
  1443. return FrameAndNodeOffset(lastFrame, nodePosition.mOffset);
  1444. }
  1445. int32_t start, end;
  1446. if (NS_WARN_IF(NS_FAILED(lastFrame->GetOffsets(start, end)))) {
  1447. return FrameAndNodeOffset();
  1448. }
  1449. // If the start offset in the node is same as the computed offset in the
  1450. // node and it's not 0, the frame shouldn't be added to the text rect. So,
  1451. // this should return previous text frame and its last offset if there is
  1452. // at least one text frame.
  1453. if (nodePosition.mOffset && nodePosition.mOffset == start) {
  1454. GetFrameForTextRect(nodePosition.mNode, --nodePosition.mOffset,
  1455. true, &lastFrame);
  1456. if (NS_WARN_IF(!lastFrame)) {
  1457. return FrameAndNodeOffset();
  1458. }
  1459. }
  1460. return FrameAndNodeOffset(lastFrame, nodePosition.mOffset);
  1461. }
  1462. ContentEventHandler::FrameRelativeRect
  1463. ContentEventHandler::GetLineBreakerRectBefore(nsIFrame* aFrame)
  1464. {
  1465. // Note that this method should be called only with an element's frame whose
  1466. // open tag causes a line break or moz-<br> for computing empty last line's
  1467. // rect.
  1468. MOZ_ASSERT(ShouldBreakLineBefore(aFrame->GetContent(), mRootContent) ||
  1469. IsMozBR(aFrame->GetContent()));
  1470. nsIFrame* frameForFontMetrics = aFrame;
  1471. // If it's not a <br> frame, this method computes the line breaker's rect
  1472. // outside the frame. Therefore, we need to compute with parent frame's
  1473. // font metrics in such case.
  1474. if (aFrame->GetType() != nsGkAtoms::brFrame && aFrame->GetParent()) {
  1475. frameForFontMetrics = aFrame->GetParent();
  1476. }
  1477. // Note that <br> element's rect is decided with line-height but we need
  1478. // a rect only with font height. Additionally, <br> frame's width and
  1479. // height are 0 in quirks mode if it's not an empty line. So, we cannot
  1480. // use frame rect information even if it's a <br> frame.
  1481. FrameRelativeRect result(aFrame);
  1482. RefPtr<nsFontMetrics> fontMetrics =
  1483. nsLayoutUtils::GetInflatedFontMetricsForFrame(frameForFontMetrics);
  1484. if (NS_WARN_IF(!fontMetrics)) {
  1485. return FrameRelativeRect();
  1486. }
  1487. const WritingMode kWritingMode = frameForFontMetrics->GetWritingMode();
  1488. nscoord baseline = aFrame->GetCaretBaseline();
  1489. if (kWritingMode.IsVertical()) {
  1490. if (kWritingMode.IsLineInverted()) {
  1491. result.mRect.x = baseline - fontMetrics->MaxDescent();
  1492. } else {
  1493. result.mRect.x = baseline - fontMetrics->MaxAscent();
  1494. }
  1495. result.mRect.width = fontMetrics->MaxHeight();
  1496. } else {
  1497. result.mRect.y = baseline - fontMetrics->MaxAscent();
  1498. result.mRect.height = fontMetrics->MaxHeight();
  1499. }
  1500. // If aFrame isn't a <br> frame, caret should be at outside of it because
  1501. // the line break is before its open tag. For example, case of
  1502. // |<div><p>some text</p></div>|, caret is before <p> element and in <div>
  1503. // element, the caret should be left of top-left corner of <p> element like:
  1504. //
  1505. // +-<div>------------------- <div>'s border box
  1506. // | I +-<p>----------------- <p>'s border box
  1507. // | I |
  1508. // | I |
  1509. // | |
  1510. // ^- caret
  1511. //
  1512. // However, this is a hack for unusual scenario. This hack shouldn't be
  1513. // used as far as possible.
  1514. if (aFrame->GetType() != nsGkAtoms::brFrame) {
  1515. if (kWritingMode.IsVertical()) {
  1516. if (kWritingMode.IsLineInverted()) {
  1517. // above of top-left corner of aFrame.
  1518. result.mRect.x = 0;
  1519. } else {
  1520. // above of top-right corner of aFrame.
  1521. result.mRect.x = aFrame->GetRect().XMost() - result.mRect.width;
  1522. }
  1523. result.mRect.y = -mPresContext->AppUnitsPerDevPixel();
  1524. } else {
  1525. // left of top-left corner of aFrame.
  1526. result.mRect.x = -mPresContext->AppUnitsPerDevPixel();
  1527. result.mRect.y = 0;
  1528. }
  1529. }
  1530. return result;
  1531. }
  1532. ContentEventHandler::FrameRelativeRect
  1533. ContentEventHandler::GuessLineBreakerRectAfter(nsIContent* aTextContent)
  1534. {
  1535. // aTextContent should be a text node.
  1536. MOZ_ASSERT(aTextContent->IsNodeOfType(nsINode::eTEXT));
  1537. FrameRelativeRect result;
  1538. int32_t length = static_cast<int32_t>(aTextContent->Length());
  1539. if (NS_WARN_IF(length < 0)) {
  1540. return result;
  1541. }
  1542. // Get the last nsTextFrame which is caused by aTextContent. Note that
  1543. // a text node can cause multiple text frames, e.g., the text is too long
  1544. // and wrapped by its parent block or the text has line breakers and its
  1545. // white-space property respects the line breakers (e.g., |pre|).
  1546. nsIFrame* lastTextFrame = nullptr;
  1547. nsresult rv = GetFrameForTextRect(aTextContent, length, true, &lastTextFrame);
  1548. if (NS_WARN_IF(NS_FAILED(rv)) || NS_WARN_IF(!lastTextFrame)) {
  1549. return result;
  1550. }
  1551. const nsRect kLastTextFrameRect = lastTextFrame->GetRect();
  1552. if (lastTextFrame->GetWritingMode().IsVertical()) {
  1553. // Below of the last text frame.
  1554. result.mRect.SetRect(0, kLastTextFrameRect.height,
  1555. kLastTextFrameRect.width, 0);
  1556. } else {
  1557. // Right of the last text frame (not bidi-aware).
  1558. result.mRect.SetRect(kLastTextFrameRect.width, 0,
  1559. 0, kLastTextFrameRect.height);
  1560. }
  1561. result.mBaseFrame = lastTextFrame;
  1562. return result;
  1563. }
  1564. ContentEventHandler::FrameRelativeRect
  1565. ContentEventHandler::GuessFirstCaretRectIn(nsIFrame* aFrame)
  1566. {
  1567. const WritingMode kWritingMode = aFrame->GetWritingMode();
  1568. // Computes the font height, but if it's not available, we should use
  1569. // default font size of Firefox. The default font size in default settings
  1570. // is 16px.
  1571. RefPtr<nsFontMetrics> fontMetrics =
  1572. nsLayoutUtils::GetInflatedFontMetricsForFrame(aFrame);
  1573. const nscoord kMaxHeight =
  1574. fontMetrics ? fontMetrics->MaxHeight() :
  1575. 16 * mPresContext->AppUnitsPerDevPixel();
  1576. nsRect caretRect;
  1577. const nsRect kContentRect = aFrame->GetContentRect() - aFrame->GetPosition();
  1578. caretRect.y = kContentRect.y;
  1579. if (!kWritingMode.IsVertical()) {
  1580. if (kWritingMode.IsBidiLTR()) {
  1581. caretRect.x = kContentRect.x;
  1582. } else {
  1583. // Move 1px left for the space of caret itself.
  1584. const nscoord kOnePixel = mPresContext->AppUnitsPerDevPixel();
  1585. caretRect.x = kContentRect.XMost() - kOnePixel;
  1586. }
  1587. caretRect.height = kMaxHeight;
  1588. // However, don't add kOnePixel here because it may cause 2px width at
  1589. // aligning the edge to device pixels.
  1590. caretRect.width = 1;
  1591. } else {
  1592. if (kWritingMode.IsVerticalLR()) {
  1593. caretRect.x = kContentRect.x;
  1594. } else {
  1595. caretRect.x = kContentRect.XMost() - kMaxHeight;
  1596. }
  1597. caretRect.width = kMaxHeight;
  1598. // Don't add app units for a device pixel because it may cause 2px height
  1599. // at aligning the edge to device pixels.
  1600. caretRect.height = 1;
  1601. }
  1602. return FrameRelativeRect(caretRect, aFrame);
  1603. }
  1604. nsresult
  1605. ContentEventHandler::OnQueryTextRectArray(WidgetQueryContentEvent* aEvent)
  1606. {
  1607. nsresult rv = Init(aEvent);
  1608. if (NS_WARN_IF(NS_FAILED(rv))) {
  1609. return rv;
  1610. }
  1611. LineBreakType lineBreakType = GetLineBreakType(aEvent);
  1612. const uint32_t kBRLength = GetBRLength(lineBreakType);
  1613. RefPtr<nsRange> range = new nsRange(mRootContent);
  1614. bool isVertical = false;
  1615. LayoutDeviceIntRect rect;
  1616. uint32_t offset = aEvent->mInput.mOffset;
  1617. const uint32_t kEndOffset = offset + aEvent->mInput.mLength;
  1618. bool wasLineBreaker = false;
  1619. // lastCharRect stores the last charRect value (see below for the detail of
  1620. // charRect).
  1621. nsRect lastCharRect;
  1622. // lastFrame is base frame of lastCharRect.
  1623. nsIFrame* lastFrame = nullptr;
  1624. while (offset < kEndOffset) {
  1625. nsCOMPtr<nsIContent> lastTextContent;
  1626. rv = SetRangeFromFlatTextOffset(range, offset, 1, lineBreakType, true,
  1627. nullptr, getter_AddRefs(lastTextContent));
  1628. if (NS_WARN_IF(NS_FAILED(rv))) {
  1629. return rv;
  1630. }
  1631. // If the range is collapsed, offset has already reached the end of the
  1632. // contents.
  1633. if (range->Collapsed()) {
  1634. break;
  1635. }
  1636. // Get the first frame which causes some text after the offset.
  1637. FrameAndNodeOffset firstFrame = GetFirstFrameInRangeForTextRect(range);
  1638. // If GetFirstFrameInRangeForTextRect() does not return valid frame, that
  1639. // means that there are no visible frames having text or the offset reached
  1640. // the end of contents.
  1641. if (!firstFrame.IsValid()) {
  1642. nsAutoString allText;
  1643. rv = GenerateFlatTextContent(mRootContent, allText, lineBreakType);
  1644. // If the offset doesn't reach the end of contents yet but there is no
  1645. // frames for the node, that means that current offset's node is hidden
  1646. // by CSS or something. Ideally, we should handle it with the last
  1647. // visible text node's last character's rect, but it's not usual cases
  1648. // in actual web services. Therefore, currently, we should make this
  1649. // case fail.
  1650. if (NS_WARN_IF(NS_FAILED(rv)) || offset < allText.Length()) {
  1651. return NS_ERROR_FAILURE;
  1652. }
  1653. // Otherwise, we should append caret rect at the end of the contents
  1654. // later.
  1655. break;
  1656. }
  1657. nsIContent* firstContent = firstFrame.mFrame->GetContent();
  1658. if (NS_WARN_IF(!firstContent)) {
  1659. return NS_ERROR_FAILURE;
  1660. }
  1661. bool startsBetweenLineBreaker = false;
  1662. nsAutoString chars;
  1663. // XXX not bidi-aware this class...
  1664. isVertical = firstFrame->GetWritingMode().IsVertical();
  1665. nsIFrame* baseFrame = firstFrame;
  1666. // charRect should have each character rect or line breaker rect relative
  1667. // to the base frame.
  1668. AutoTArray<nsRect, 16> charRects;
  1669. // If the first frame is a text frame, the result should be computed with
  1670. // the frame's API.
  1671. if (firstFrame->GetType() == nsGkAtoms::textFrame) {
  1672. rv = firstFrame->GetCharacterRectsInRange(firstFrame.mOffsetInNode,
  1673. kEndOffset - offset, charRects);
  1674. if (NS_WARN_IF(NS_FAILED(rv)) || NS_WARN_IF(charRects.IsEmpty())) {
  1675. return rv;
  1676. }
  1677. // Assign the characters whose rects are computed by the call of
  1678. // nsTextFrame::GetCharacterRectsInRange().
  1679. AppendSubString(chars, firstContent, firstFrame.mOffsetInNode,
  1680. charRects.Length());
  1681. if (NS_WARN_IF(chars.Length() != charRects.Length())) {
  1682. return NS_ERROR_UNEXPECTED;
  1683. }
  1684. if (kBRLength > 1 && chars[0] == '\n' &&
  1685. offset == aEvent->mInput.mOffset && offset) {
  1686. // If start of range starting from previous offset of query range is
  1687. // same as the start of query range, the query range starts from
  1688. // between a line breaker (i.e., the range starts between "\r" and
  1689. // "\n").
  1690. RefPtr<nsRange> rangeToPrevOffset = new nsRange(mRootContent);
  1691. rv = SetRangeFromFlatTextOffset(rangeToPrevOffset,
  1692. aEvent->mInput.mOffset - 1, 1,
  1693. lineBreakType, true, nullptr);
  1694. if (NS_WARN_IF(NS_FAILED(rv))) {
  1695. return rv;
  1696. }
  1697. startsBetweenLineBreaker =
  1698. range->GetStartParent() == rangeToPrevOffset->GetStartParent() &&
  1699. range->StartOffset() == rangeToPrevOffset->StartOffset();
  1700. }
  1701. }
  1702. // Other contents should cause a line breaker rect before it.
  1703. // Note that moz-<br> element does not cause any text, however,
  1704. // it represents empty line at the last of current block. Therefore,
  1705. // we need to compute its rect too.
  1706. else if (ShouldBreakLineBefore(firstContent, mRootContent) ||
  1707. IsMozBR(firstContent)) {
  1708. nsRect brRect;
  1709. // If the frame is not a <br> frame, we need to compute the caret rect
  1710. // with last character's rect before firstContent if there is.
  1711. // For example, if caret is after "c" of |<p>abc</p><p>def</p>|, IME may
  1712. // query a line breaker's rect after "c". Then, if we compute it only
  1713. // with the 2nd <p>'s block frame, the result will be:
  1714. // +-<p>--------------------------------+
  1715. // |abc |
  1716. // +------------------------------------+
  1717. //
  1718. // I+-<p>--------------------------------+
  1719. // |def |
  1720. // +------------------------------------+
  1721. // However, users expect popup windows of IME should be positioned at
  1722. // right-bottom of "c" like this:
  1723. // +-<p>--------------------------------+
  1724. // |abcI |
  1725. // +------------------------------------+
  1726. //
  1727. // +-<p>--------------------------------+
  1728. // |def |
  1729. // +------------------------------------+
  1730. // Therefore, if the first frame isn't a <br> frame and there is a text
  1731. // node before the first node in the queried range, we should compute the
  1732. // first rect with the previous character's rect.
  1733. // If we already compute a character's rect in the queried range, we can
  1734. // compute it with the cached last character's rect. (However, don't
  1735. // use this path if it's a <br> frame because trusting <br> frame's rect
  1736. // is better than guessing the rect from the previous character.)
  1737. if (firstFrame->GetType() != nsGkAtoms::brFrame &&
  1738. aEvent->mInput.mOffset != offset) {
  1739. baseFrame = lastFrame;
  1740. brRect = lastCharRect;
  1741. if (!wasLineBreaker) {
  1742. if (isVertical) {
  1743. // Right of the last character.
  1744. brRect.y = brRect.YMost() + 1;
  1745. brRect.height = 1;
  1746. } else {
  1747. // Under the last character.
  1748. brRect.x = brRect.XMost() + 1;
  1749. brRect.width = 1;
  1750. }
  1751. }
  1752. }
  1753. // If it's not a <br> frame and it's the first character rect at the
  1754. // queried range, we need to the previous character of the start of
  1755. // the queried range if there is a text node.
  1756. else if (firstFrame->GetType() != nsGkAtoms::brFrame && lastTextContent) {
  1757. FrameRelativeRect brRectRelativeToLastTextFrame =
  1758. GuessLineBreakerRectAfter(lastTextContent);
  1759. if (NS_WARN_IF(!brRectRelativeToLastTextFrame.IsValid())) {
  1760. return NS_ERROR_FAILURE;
  1761. }
  1762. // Look for the last text frame for lastTextContent.
  1763. nsIFrame* primaryFrame = lastTextContent->GetPrimaryFrame();
  1764. if (NS_WARN_IF(!primaryFrame)) {
  1765. return NS_ERROR_FAILURE;
  1766. }
  1767. baseFrame = primaryFrame->LastContinuation();
  1768. if (NS_WARN_IF(!baseFrame)) {
  1769. return NS_ERROR_FAILURE;
  1770. }
  1771. brRect = brRectRelativeToLastTextFrame.RectRelativeTo(baseFrame);
  1772. }
  1773. // Otherwise, we need to compute the line breaker's rect only with the
  1774. // first frame's rect. But this may be unexpected. For example,
  1775. // |<div contenteditable>[<p>]abc</p></div>|. In this case, caret is
  1776. // before "a", therefore, users expect the rect left of "a". However,
  1777. // we don't have enough information about the next character here and
  1778. // this isn't usual case (e.g., IME typically tries to query the rect
  1779. // of "a" or caret rect for computing its popup position). Therefore,
  1780. // we shouldn't do more complicated hack here unless we'll get some bug
  1781. // reports actually.
  1782. else {
  1783. FrameRelativeRect relativeBRRect = GetLineBreakerRectBefore(firstFrame);
  1784. brRect = relativeBRRect.RectRelativeTo(firstFrame);
  1785. }
  1786. charRects.AppendElement(brRect);
  1787. chars.AssignLiteral("\n");
  1788. if (kBRLength > 1 && offset == aEvent->mInput.mOffset && offset) {
  1789. // If the first frame for the previous offset of the query range and
  1790. // the first frame for the start of query range are same, that means
  1791. // the start offset is between the first line breaker (i.e., the range
  1792. // starts between "\r" and "\n").
  1793. rv = SetRangeFromFlatTextOffset(range, aEvent->mInput.mOffset - 1, 1,
  1794. lineBreakType, true, nullptr);
  1795. if (NS_WARN_IF(NS_FAILED(rv))) {
  1796. return NS_ERROR_UNEXPECTED;
  1797. }
  1798. FrameAndNodeOffset frameForPrevious =
  1799. GetFirstFrameInRangeForTextRect(range);
  1800. startsBetweenLineBreaker = frameForPrevious.mFrame == firstFrame.mFrame;
  1801. }
  1802. } else {
  1803. NS_WARNING("The frame is neither a text frame nor a frame whose content "
  1804. "causes a line break");
  1805. return NS_ERROR_FAILURE;
  1806. }
  1807. for (size_t i = 0; i < charRects.Length() && offset < kEndOffset; i++) {
  1808. nsRect charRect = charRects[i];
  1809. // Store lastCharRect before applying CSS transform because it may be
  1810. // used for computing a line breaker rect. Then, the computed line
  1811. // breaker rect will be applied CSS transform again. Therefore,
  1812. // the value of lastCharRect should be raw rect value relative to the
  1813. // base frame.
  1814. lastCharRect = charRect;
  1815. lastFrame = baseFrame;
  1816. rv = ConvertToRootRelativeOffset(baseFrame, charRect);
  1817. if (NS_WARN_IF(NS_FAILED(rv))) {
  1818. return rv;
  1819. }
  1820. rect = LayoutDeviceIntRect::FromUnknownRect(
  1821. charRect.ToOutsidePixels(mPresContext->AppUnitsPerDevPixel()));
  1822. // Returning empty rect may cause native IME confused, let's make sure to
  1823. // return non-empty rect.
  1824. EnsureNonEmptyRect(rect);
  1825. aEvent->mReply.mRectArray.AppendElement(rect);
  1826. offset++;
  1827. // If it's not a line breaker or the line breaker length is same as
  1828. // XP line breaker's, we need to do nothing for current character.
  1829. wasLineBreaker = chars[i] == '\n';
  1830. if (!wasLineBreaker || kBRLength == 1) {
  1831. continue;
  1832. }
  1833. MOZ_ASSERT(kBRLength == 2);
  1834. // If it's already reached the end of query range, we don't need to do
  1835. // anymore.
  1836. if (offset == kEndOffset) {
  1837. break;
  1838. }
  1839. // If the query range starts from between a line breaker, i.e., it starts
  1840. // between "\r" and "\n", the appended rect was for the "\n". Therefore,
  1841. // we don't need to append same rect anymore for current "\r\n".
  1842. if (startsBetweenLineBreaker) {
  1843. continue;
  1844. }
  1845. // The appended rect was for "\r" of "\r\n". Therefore, we need to
  1846. // append same rect for "\n" too because querying rect of "\r" and "\n"
  1847. // should return same rect. E.g., IME may query previous character's
  1848. // rect of first character of a line.
  1849. aEvent->mReply.mRectArray.AppendElement(rect);
  1850. offset++;
  1851. }
  1852. }
  1853. // If the query range is longer than actual content length, we should append
  1854. // caret rect at the end of the content as the last character rect because
  1855. // native IME may want to query character rect at the end of contents for
  1856. // deciding the position of a popup window (e.g., suggest window for next
  1857. // word). Note that when this method hasn't appended character rects, it
  1858. // means that the offset is too large or the query range is collapsed.
  1859. if (offset < kEndOffset || aEvent->mReply.mRectArray.IsEmpty()) {
  1860. // If we've already retrieved some character rects before current offset,
  1861. // we can guess the last rect from the last character's rect unless it's a
  1862. // line breaker. (If it's a line breaker, the caret rect is in next line.)
  1863. if (!aEvent->mReply.mRectArray.IsEmpty() && !wasLineBreaker) {
  1864. rect = aEvent->mReply.mRectArray.LastElement();
  1865. if (isVertical) {
  1866. rect.y = rect.YMost() + 1;
  1867. rect.height = 1;
  1868. MOZ_ASSERT(rect.width);
  1869. } else {
  1870. rect.x = rect.XMost() + 1;
  1871. rect.width = 1;
  1872. MOZ_ASSERT(rect.height);
  1873. }
  1874. aEvent->mReply.mRectArray.AppendElement(rect);
  1875. } else {
  1876. // Note that don't use eQueryCaretRect here because if caret is at the
  1877. // end of the content, it returns actual caret rect instead of computing
  1878. // the rect itself. It means that the result depends on caret position.
  1879. // So, we shouldn't use it for consistency result in automated tests.
  1880. WidgetQueryContentEvent queryTextRect(eQueryTextRect, *aEvent);
  1881. WidgetQueryContentEvent::Options options(*aEvent);
  1882. queryTextRect.InitForQueryTextRect(offset, 1, options);
  1883. rv = OnQueryTextRect(&queryTextRect);
  1884. if (NS_WARN_IF(NS_FAILED(rv))) {
  1885. return rv;
  1886. }
  1887. if (NS_WARN_IF(!queryTextRect.mSucceeded)) {
  1888. return NS_ERROR_FAILURE;
  1889. }
  1890. MOZ_ASSERT(!queryTextRect.mReply.mRect.IsEmpty());
  1891. if (queryTextRect.mReply.mWritingMode.IsVertical()) {
  1892. queryTextRect.mReply.mRect.height = 1;
  1893. } else {
  1894. queryTextRect.mReply.mRect.width = 1;
  1895. }
  1896. aEvent->mReply.mRectArray.AppendElement(queryTextRect.mReply.mRect);
  1897. }
  1898. }
  1899. aEvent->mSucceeded = true;
  1900. return NS_OK;
  1901. }
  1902. nsresult
  1903. ContentEventHandler::OnQueryTextRect(WidgetQueryContentEvent* aEvent)
  1904. {
  1905. nsresult rv = Init(aEvent);
  1906. if (NS_FAILED(rv)) {
  1907. return rv;
  1908. }
  1909. // If mLength is 0 (this may be caused by bug of native IME), we should
  1910. // redirect this event to OnQueryCaretRect().
  1911. if (!aEvent->mInput.mLength) {
  1912. return OnQueryCaretRect(aEvent);
  1913. }
  1914. LineBreakType lineBreakType = GetLineBreakType(aEvent);
  1915. RefPtr<nsRange> range = new nsRange(mRootContent);
  1916. nsCOMPtr<nsIContent> lastTextContent;
  1917. rv = SetRangeFromFlatTextOffset(range, aEvent->mInput.mOffset,
  1918. aEvent->mInput.mLength, lineBreakType, true,
  1919. &aEvent->mReply.mOffset,
  1920. getter_AddRefs(lastTextContent));
  1921. NS_ENSURE_SUCCESS(rv, rv);
  1922. rv = GenerateFlatTextContent(range, aEvent->mReply.mString, lineBreakType);
  1923. NS_ENSURE_SUCCESS(rv, rv);
  1924. // used to iterate over all contents and their frames
  1925. nsCOMPtr<nsIContentIterator> iter = NS_NewContentIterator();
  1926. iter->Init(range);
  1927. // Get the first frame which causes some text after the offset.
  1928. FrameAndNodeOffset firstFrame = GetFirstFrameInRangeForTextRect(range);
  1929. // If GetFirstFrameInRangeForTextRect() does not return valid frame, that
  1930. // means that there are no visible frames having text or the offset reached
  1931. // the end of contents.
  1932. if (!firstFrame.IsValid()) {
  1933. nsAutoString allText;
  1934. rv = GenerateFlatTextContent(mRootContent, allText, lineBreakType);
  1935. // If the offset doesn't reach the end of contents but there is no frames
  1936. // for the node, that means that current offset's node is hidden by CSS or
  1937. // something. Ideally, we should handle it with the last visible text
  1938. // node's last character's rect, but it's not usual cases in actual web
  1939. // services. Therefore, currently, we should make this case fail.
  1940. if (NS_WARN_IF(NS_FAILED(rv)) ||
  1941. static_cast<uint32_t>(aEvent->mInput.mOffset) < allText.Length()) {
  1942. return NS_ERROR_FAILURE;
  1943. }
  1944. // Look for the last frame which should be included text rects.
  1945. ErrorResult erv;
  1946. range->SelectNodeContents(*mRootContent, erv);
  1947. if (NS_WARN_IF(erv.Failed())) {
  1948. return NS_ERROR_UNEXPECTED;
  1949. }
  1950. nsRect rect;
  1951. FrameAndNodeOffset lastFrame = GetLastFrameInRangeForTextRect(range);
  1952. // If there is at least one frame which can be used for computing a rect
  1953. // for a character or a line breaker, we should use it for guessing the
  1954. // caret rect at the end of the contents.
  1955. if (lastFrame) {
  1956. if (NS_WARN_IF(!lastFrame->GetContent())) {
  1957. return NS_ERROR_FAILURE;
  1958. }
  1959. FrameRelativeRect relativeRect;
  1960. // If there is a <br> frame at the end, it represents an empty line at
  1961. // the end with moz-<br> or content <br> in a block level element.
  1962. if (lastFrame->GetType() == nsGkAtoms::brFrame) {
  1963. relativeRect = GetLineBreakerRectBefore(lastFrame);
  1964. }
  1965. // If there is a text frame at the end, use its information.
  1966. else if (lastFrame->GetType() == nsGkAtoms::textFrame) {
  1967. relativeRect = GuessLineBreakerRectAfter(lastFrame->GetContent());
  1968. }
  1969. // If there is an empty frame which is neither a text frame nor a <br>
  1970. // frame at the end, guess caret rect in it.
  1971. else {
  1972. relativeRect = GuessFirstCaretRectIn(lastFrame);
  1973. }
  1974. if (NS_WARN_IF(!relativeRect.IsValid())) {
  1975. return NS_ERROR_FAILURE;
  1976. }
  1977. rect = relativeRect.RectRelativeTo(lastFrame);
  1978. rv = ConvertToRootRelativeOffset(lastFrame, rect);
  1979. if (NS_WARN_IF(NS_FAILED(rv))) {
  1980. return rv;
  1981. }
  1982. aEvent->mReply.mWritingMode = lastFrame->GetWritingMode();
  1983. }
  1984. // Otherwise, if there are no contents in mRootContent, guess caret rect in
  1985. // its frame (with its font height and content box).
  1986. else {
  1987. nsIFrame* rootContentFrame = mRootContent->GetPrimaryFrame();
  1988. if (NS_WARN_IF(!rootContentFrame)) {
  1989. return NS_ERROR_FAILURE;
  1990. }
  1991. FrameRelativeRect relativeRect = GuessFirstCaretRectIn(rootContentFrame);
  1992. if (NS_WARN_IF(!relativeRect.IsValid())) {
  1993. return NS_ERROR_FAILURE;
  1994. }
  1995. rect = relativeRect.RectRelativeTo(rootContentFrame);
  1996. rv = ConvertToRootRelativeOffset(rootContentFrame, rect);
  1997. if (NS_WARN_IF(NS_FAILED(rv))) {
  1998. return rv;
  1999. }
  2000. aEvent->mReply.mWritingMode = rootContentFrame->GetWritingMode();
  2001. }
  2002. aEvent->mReply.mRect = LayoutDeviceIntRect::FromUnknownRect(
  2003. rect.ToOutsidePixels(mPresContext->AppUnitsPerDevPixel()));
  2004. EnsureNonEmptyRect(aEvent->mReply.mRect);
  2005. aEvent->mSucceeded = true;
  2006. return NS_OK;
  2007. }
  2008. nsRect rect, frameRect;
  2009. nsPoint ptOffset;
  2010. // If the first frame is a text frame, the result should be computed with
  2011. // the frame's rect but not including the rect before start point of the
  2012. // queried range.
  2013. if (firstFrame->GetType() == nsGkAtoms::textFrame) {
  2014. rect.SetRect(nsPoint(0, 0), firstFrame->GetRect().Size());
  2015. rv = ConvertToRootRelativeOffset(firstFrame, rect);
  2016. if (NS_WARN_IF(NS_FAILED(rv))) {
  2017. return rv;
  2018. }
  2019. frameRect = rect;
  2020. // Exclude the rect before start point of the queried range.
  2021. firstFrame->GetPointFromOffset(firstFrame.mOffsetInNode, &ptOffset);
  2022. if (firstFrame->GetWritingMode().IsVertical()) {
  2023. rect.y += ptOffset.y;
  2024. rect.height -= ptOffset.y;
  2025. } else {
  2026. rect.x += ptOffset.x;
  2027. rect.width -= ptOffset.x;
  2028. }
  2029. }
  2030. // If first frame causes a line breaker but it's not a <br> frame, we cannot
  2031. // compute proper rect only with the frame because typically caret is at
  2032. // right of the last character of it. For example, if caret is after "c" of
  2033. // |<p>abc</p><p>def</p>|, IME may query a line breaker's rect after "c".
  2034. // Then, if we compute it only with the 2nd <p>'s block frame, the result
  2035. // will be:
  2036. // +-<p>--------------------------------+
  2037. // |abc |
  2038. // +------------------------------------+
  2039. //
  2040. // I+-<p>--------------------------------+
  2041. // |def |
  2042. // +------------------------------------+
  2043. // However, users expect popup windows of IME should be positioned at
  2044. // right-bottom of "c" like this:
  2045. // +-<p>--------------------------------+
  2046. // |abcI |
  2047. // +------------------------------------+
  2048. //
  2049. // +-<p>--------------------------------+
  2050. // |def |
  2051. // +------------------------------------+
  2052. // Therefore, if the first frame isn't a <br> frame and there is a text
  2053. // node before the first node in the queried range, we should compute the
  2054. // first rect with the previous character's rect.
  2055. else if (firstFrame->GetType() != nsGkAtoms::brFrame && lastTextContent) {
  2056. FrameRelativeRect brRectAfterLastChar =
  2057. GuessLineBreakerRectAfter(lastTextContent);
  2058. if (NS_WARN_IF(!brRectAfterLastChar.IsValid())) {
  2059. return NS_ERROR_FAILURE;
  2060. }
  2061. rect = brRectAfterLastChar.mRect;
  2062. rv = ConvertToRootRelativeOffset(brRectAfterLastChar.mBaseFrame, rect);
  2063. if (NS_WARN_IF(NS_FAILED(rv))) {
  2064. return rv;
  2065. }
  2066. frameRect = rect;
  2067. }
  2068. // Otherwise, we need to compute the line breaker's rect only with the
  2069. // first frame's rect. But this may be unexpected. For example,
  2070. // |<div contenteditable>[<p>]abc</p></div>|. In this case, caret is before
  2071. // "a", therefore, users expect the rect left of "a". However, we don't
  2072. // have enough information about the next character here and this isn't
  2073. // usual case (e.g., IME typically tries to query the rect of "a" or caret
  2074. // rect for computing its popup position). Therefore, we shouldn't do
  2075. // more complicated hack here unless we'll get some bug reports actually.
  2076. else {
  2077. FrameRelativeRect relativeRect = GetLineBreakerRectBefore(firstFrame);
  2078. if (NS_WARN_IF(!relativeRect.IsValid())) {
  2079. return NS_ERROR_FAILURE;
  2080. }
  2081. rect = relativeRect.RectRelativeTo(firstFrame);
  2082. rv = ConvertToRootRelativeOffset(firstFrame, rect);
  2083. if (NS_WARN_IF(NS_FAILED(rv))) {
  2084. return rv;
  2085. }
  2086. frameRect = rect;
  2087. }
  2088. // UnionRect() requires non-empty rect. So, let's make sure to get non-emtpy
  2089. // rect from the first frame.
  2090. EnsureNonEmptyRect(rect);
  2091. // Get the last frame which causes some text in the range.
  2092. FrameAndNodeOffset lastFrame = GetLastFrameInRangeForTextRect(range);
  2093. if (NS_WARN_IF(!lastFrame.IsValid())) {
  2094. return NS_ERROR_FAILURE;
  2095. }
  2096. // iterate over all covered frames
  2097. for (nsIFrame* frame = firstFrame; frame != lastFrame;) {
  2098. frame = frame->GetNextContinuation();
  2099. if (!frame) {
  2100. do {
  2101. iter->Next();
  2102. nsINode* node = iter->GetCurrentNode();
  2103. if (!node) {
  2104. break;
  2105. }
  2106. if (!node->IsNodeOfType(nsINode::eCONTENT)) {
  2107. continue;
  2108. }
  2109. nsIFrame* primaryFrame = node->AsContent()->GetPrimaryFrame();
  2110. // The node may be hidden by CSS.
  2111. if (!primaryFrame) {
  2112. continue;
  2113. }
  2114. // We should take only text frame's rect and br frame's rect. We can
  2115. // always use frame rect of text frame and GetLineBreakerRectBefore()
  2116. // can return exactly correct rect only for <br> frame for now. On the
  2117. // other hand, GetLineBreakRectBefore() returns guessed caret rect for
  2118. // the other frames. We shouldn't include such odd rect to the result.
  2119. if (primaryFrame->GetType() == nsGkAtoms::textFrame ||
  2120. primaryFrame->GetType() == nsGkAtoms::brFrame) {
  2121. frame = primaryFrame;
  2122. }
  2123. } while (!frame && !iter->IsDone());
  2124. if (!frame) {
  2125. break;
  2126. }
  2127. }
  2128. if (frame->GetType() == nsGkAtoms::textFrame) {
  2129. frameRect.SetRect(nsPoint(0, 0), frame->GetRect().Size());
  2130. } else {
  2131. MOZ_ASSERT(frame->GetType() == nsGkAtoms::brFrame);
  2132. FrameRelativeRect relativeRect = GetLineBreakerRectBefore(frame);
  2133. if (NS_WARN_IF(!relativeRect.IsValid())) {
  2134. return NS_ERROR_FAILURE;
  2135. }
  2136. frameRect = relativeRect.RectRelativeTo(frame);
  2137. }
  2138. rv = ConvertToRootRelativeOffset(frame, frameRect);
  2139. if (NS_WARN_IF(NS_FAILED(rv))) {
  2140. return rv;
  2141. }
  2142. // UnionRect() requires non-empty rect. So, let's make sure to get
  2143. // non-emtpy rect from the frame.
  2144. EnsureNonEmptyRect(frameRect);
  2145. if (frame != lastFrame) {
  2146. // not last frame, so just add rect to previous result
  2147. rect.UnionRect(rect, frameRect);
  2148. }
  2149. }
  2150. // Get the ending frame rect.
  2151. // FYI: If first frame and last frame are same, frameRect is already set
  2152. // to the rect excluding the text before the query range.
  2153. if (firstFrame.mFrame != lastFrame.mFrame) {
  2154. frameRect.SetRect(nsPoint(0, 0), lastFrame->GetRect().Size());
  2155. rv = ConvertToRootRelativeOffset(lastFrame, frameRect);
  2156. if (NS_WARN_IF(NS_FAILED(rv))) {
  2157. return rv;
  2158. }
  2159. }
  2160. // Shrink the last frame for cutting off the text after the query range.
  2161. if (lastFrame->GetType() == nsGkAtoms::textFrame) {
  2162. lastFrame->GetPointFromOffset(lastFrame.mOffsetInNode, &ptOffset);
  2163. if (lastFrame->GetWritingMode().IsVertical()) {
  2164. frameRect.height -= lastFrame->GetRect().height - ptOffset.y;
  2165. } else {
  2166. frameRect.width -= lastFrame->GetRect().width - ptOffset.x;
  2167. }
  2168. // UnionRect() requires non-empty rect. So, let's make sure to get
  2169. // non-empty rect from the last frame.
  2170. EnsureNonEmptyRect(frameRect);
  2171. if (firstFrame.mFrame == lastFrame.mFrame) {
  2172. rect.IntersectRect(rect, frameRect);
  2173. } else {
  2174. rect.UnionRect(rect, frameRect);
  2175. }
  2176. }
  2177. aEvent->mReply.mRect = LayoutDeviceIntRect::FromUnknownRect(
  2178. rect.ToOutsidePixels(mPresContext->AppUnitsPerDevPixel()));
  2179. // Returning empty rect may cause native IME confused, let's make sure to
  2180. // return non-empty rect.
  2181. EnsureNonEmptyRect(aEvent->mReply.mRect);
  2182. aEvent->mReply.mWritingMode = lastFrame->GetWritingMode();
  2183. aEvent->mSucceeded = true;
  2184. return NS_OK;
  2185. }
  2186. nsresult
  2187. ContentEventHandler::OnQueryEditorRect(WidgetQueryContentEvent* aEvent)
  2188. {
  2189. nsresult rv = Init(aEvent);
  2190. if (NS_FAILED(rv)) {
  2191. return rv;
  2192. }
  2193. nsIContent* focusedContent = GetFocusedContent();
  2194. rv = QueryContentRect(IsPlugin(focusedContent) ?
  2195. focusedContent : mRootContent.get(), aEvent);
  2196. NS_ENSURE_SUCCESS(rv, rv);
  2197. return NS_OK;
  2198. }
  2199. nsresult
  2200. ContentEventHandler::OnQueryCaretRect(WidgetQueryContentEvent* aEvent)
  2201. {
  2202. nsresult rv = Init(aEvent);
  2203. if (NS_FAILED(rv)) {
  2204. return rv;
  2205. }
  2206. // When the selection is collapsed and the queried offset is current caret
  2207. // position, we should return the "real" caret rect.
  2208. if (mSelection->IsCollapsed()) {
  2209. nsRect caretRect;
  2210. nsIFrame* caretFrame = nsCaret::GetGeometry(mSelection, &caretRect);
  2211. if (caretFrame) {
  2212. uint32_t offset;
  2213. rv = GetStartOffset(mFirstSelectedRange,
  2214. &offset, GetLineBreakType(aEvent));
  2215. NS_ENSURE_SUCCESS(rv, rv);
  2216. if (offset == aEvent->mInput.mOffset) {
  2217. rv = ConvertToRootRelativeOffset(caretFrame, caretRect);
  2218. NS_ENSURE_SUCCESS(rv, rv);
  2219. nscoord appUnitsPerDevPixel =
  2220. caretFrame->PresContext()->AppUnitsPerDevPixel();
  2221. aEvent->mReply.mRect = LayoutDeviceIntRect::FromUnknownRect(
  2222. caretRect.ToOutsidePixels(appUnitsPerDevPixel));
  2223. // Returning empty rect may cause native IME confused, let's make sure
  2224. // to return non-empty rect.
  2225. EnsureNonEmptyRect(aEvent->mReply.mRect);
  2226. aEvent->mReply.mWritingMode = caretFrame->GetWritingMode();
  2227. aEvent->mReply.mOffset = aEvent->mInput.mOffset;
  2228. aEvent->mSucceeded = true;
  2229. return NS_OK;
  2230. }
  2231. }
  2232. }
  2233. // Otherwise, we should guess the caret rect from the character's rect.
  2234. WidgetQueryContentEvent queryTextRectEvent(eQueryTextRect, *aEvent);
  2235. WidgetQueryContentEvent::Options options(*aEvent);
  2236. queryTextRectEvent.InitForQueryTextRect(aEvent->mInput.mOffset, 1, options);
  2237. rv = OnQueryTextRect(&queryTextRectEvent);
  2238. if (NS_WARN_IF(NS_FAILED(rv)) || NS_WARN_IF(!queryTextRectEvent.mSucceeded)) {
  2239. return NS_ERROR_FAILURE;
  2240. }
  2241. queryTextRectEvent.mReply.mString.Truncate();
  2242. aEvent->mReply = queryTextRectEvent.mReply;
  2243. if (aEvent->GetWritingMode().IsVertical()) {
  2244. aEvent->mReply.mRect.height = 1;
  2245. } else {
  2246. aEvent->mReply.mRect.width = 1;
  2247. }
  2248. // Returning empty rect may cause native IME confused, let's make sure to
  2249. // return non-empty rect.
  2250. aEvent->mSucceeded = true;
  2251. return NS_OK;
  2252. }
  2253. nsresult
  2254. ContentEventHandler::OnQueryContentState(WidgetQueryContentEvent* aEvent)
  2255. {
  2256. nsresult rv = Init(aEvent);
  2257. if (NS_FAILED(rv)) {
  2258. return rv;
  2259. }
  2260. aEvent->mSucceeded = true;
  2261. return NS_OK;
  2262. }
  2263. nsresult
  2264. ContentEventHandler::OnQuerySelectionAsTransferable(
  2265. WidgetQueryContentEvent* aEvent)
  2266. {
  2267. nsresult rv = Init(aEvent);
  2268. if (NS_FAILED(rv)) {
  2269. return rv;
  2270. }
  2271. if (!aEvent->mReply.mHasSelection) {
  2272. aEvent->mSucceeded = true;
  2273. aEvent->mReply.mTransferable = nullptr;
  2274. return NS_OK;
  2275. }
  2276. nsCOMPtr<nsIDocument> doc = mPresShell->GetDocument();
  2277. NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
  2278. rv = nsCopySupport::GetTransferableForSelection(
  2279. mSelection, doc, getter_AddRefs(aEvent->mReply.mTransferable));
  2280. NS_ENSURE_SUCCESS(rv, rv);
  2281. aEvent->mSucceeded = true;
  2282. return NS_OK;
  2283. }
  2284. nsresult
  2285. ContentEventHandler::OnQueryCharacterAtPoint(WidgetQueryContentEvent* aEvent)
  2286. {
  2287. nsresult rv = Init(aEvent);
  2288. if (NS_FAILED(rv)) {
  2289. return rv;
  2290. }
  2291. aEvent->mReply.mOffset = aEvent->mReply.mTentativeCaretOffset =
  2292. WidgetQueryContentEvent::NOT_FOUND;
  2293. nsIFrame* rootFrame = mPresShell->GetRootFrame();
  2294. NS_ENSURE_TRUE(rootFrame, NS_ERROR_FAILURE);
  2295. nsIWidget* rootWidget = rootFrame->GetNearestWidget();
  2296. NS_ENSURE_TRUE(rootWidget, NS_ERROR_FAILURE);
  2297. // The root frame's widget might be different, e.g., the event was fired on
  2298. // a popup but the rootFrame is the document root.
  2299. if (rootWidget != aEvent->mWidget) {
  2300. NS_PRECONDITION(aEvent->mWidget, "The event must have the widget");
  2301. nsView* view = nsView::GetViewFor(aEvent->mWidget);
  2302. NS_ENSURE_TRUE(view, NS_ERROR_FAILURE);
  2303. rootFrame = view->GetFrame();
  2304. NS_ENSURE_TRUE(rootFrame, NS_ERROR_FAILURE);
  2305. rootWidget = rootFrame->GetNearestWidget();
  2306. NS_ENSURE_TRUE(rootWidget, NS_ERROR_FAILURE);
  2307. }
  2308. WidgetQueryContentEvent eventOnRoot(true, eQueryCharacterAtPoint,
  2309. rootWidget);
  2310. eventOnRoot.mUseNativeLineBreak = aEvent->mUseNativeLineBreak;
  2311. eventOnRoot.mRefPoint = aEvent->mRefPoint;
  2312. if (rootWidget != aEvent->mWidget) {
  2313. eventOnRoot.mRefPoint += aEvent->mWidget->WidgetToScreenOffset() -
  2314. rootWidget->WidgetToScreenOffset();
  2315. }
  2316. nsPoint ptInRoot =
  2317. nsLayoutUtils::GetEventCoordinatesRelativeTo(&eventOnRoot, rootFrame);
  2318. nsIFrame* targetFrame = nsLayoutUtils::GetFrameForPoint(rootFrame, ptInRoot);
  2319. if (!targetFrame || !targetFrame->GetContent() ||
  2320. !nsContentUtils::ContentIsDescendantOf(targetFrame->GetContent(),
  2321. mRootContent)) {
  2322. // There is no character at the point.
  2323. aEvent->mSucceeded = true;
  2324. return NS_OK;
  2325. }
  2326. nsPoint ptInTarget = ptInRoot + rootFrame->GetOffsetToCrossDoc(targetFrame);
  2327. int32_t rootAPD = rootFrame->PresContext()->AppUnitsPerDevPixel();
  2328. int32_t targetAPD = targetFrame->PresContext()->AppUnitsPerDevPixel();
  2329. ptInTarget = ptInTarget.ScaleToOtherAppUnits(rootAPD, targetAPD);
  2330. nsIFrame::ContentOffsets tentativeCaretOffsets =
  2331. targetFrame->GetContentOffsetsFromPoint(ptInTarget);
  2332. if (!tentativeCaretOffsets.content ||
  2333. !nsContentUtils::ContentIsDescendantOf(tentativeCaretOffsets.content,
  2334. mRootContent)) {
  2335. // There is no character nor tentative caret point at the point.
  2336. aEvent->mSucceeded = true;
  2337. return NS_OK;
  2338. }
  2339. rv = GetFlatTextLengthInRange(NodePosition(mRootContent, 0),
  2340. NodePosition(tentativeCaretOffsets),
  2341. mRootContent,
  2342. &aEvent->mReply.mTentativeCaretOffset,
  2343. GetLineBreakType(aEvent));
  2344. if (NS_WARN_IF(NS_FAILED(rv))) {
  2345. return rv;
  2346. }
  2347. if (targetFrame->GetType() != nsGkAtoms::textFrame) {
  2348. // There is no character at the point but there is tentative caret point.
  2349. aEvent->mSucceeded = true;
  2350. return NS_OK;
  2351. }
  2352. MOZ_ASSERT(
  2353. aEvent->mReply.mTentativeCaretOffset != WidgetQueryContentEvent::NOT_FOUND,
  2354. "The point is inside a character bounding box. Why tentative caret point "
  2355. "hasn't been found?");
  2356. nsTextFrame* textframe = static_cast<nsTextFrame*>(targetFrame);
  2357. nsIFrame::ContentOffsets contentOffsets =
  2358. textframe->GetCharacterOffsetAtFramePoint(ptInTarget);
  2359. NS_ENSURE_TRUE(contentOffsets.content, NS_ERROR_FAILURE);
  2360. uint32_t offset;
  2361. rv = GetFlatTextLengthInRange(NodePosition(mRootContent, 0),
  2362. NodePosition(contentOffsets),
  2363. mRootContent, &offset,
  2364. GetLineBreakType(aEvent));
  2365. if (NS_WARN_IF(NS_FAILED(rv))) {
  2366. return rv;
  2367. }
  2368. WidgetQueryContentEvent textRect(true, eQueryTextRect, aEvent->mWidget);
  2369. WidgetQueryContentEvent::Options options(*aEvent);
  2370. textRect.InitForQueryTextRect(offset, 1, options);
  2371. rv = OnQueryTextRect(&textRect);
  2372. NS_ENSURE_SUCCESS(rv, rv);
  2373. NS_ENSURE_TRUE(textRect.mSucceeded, NS_ERROR_FAILURE);
  2374. // currently, we don't need to get the actual text.
  2375. aEvent->mReply.mOffset = offset;
  2376. aEvent->mReply.mRect = textRect.mReply.mRect;
  2377. aEvent->mSucceeded = true;
  2378. return NS_OK;
  2379. }
  2380. nsresult
  2381. ContentEventHandler::OnQueryDOMWidgetHittest(WidgetQueryContentEvent* aEvent)
  2382. {
  2383. NS_ASSERTION(aEvent, "aEvent must not be null");
  2384. nsresult rv = InitBasic();
  2385. if (NS_FAILED(rv)) {
  2386. return rv;
  2387. }
  2388. aEvent->mSucceeded = false;
  2389. aEvent->mReply.mWidgetIsHit = false;
  2390. NS_ENSURE_TRUE(aEvent->mWidget, NS_ERROR_FAILURE);
  2391. nsIDocument* doc = mPresShell->GetDocument();
  2392. NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
  2393. nsIFrame* docFrame = mPresShell->GetRootFrame();
  2394. NS_ENSURE_TRUE(docFrame, NS_ERROR_FAILURE);
  2395. LayoutDeviceIntPoint eventLoc =
  2396. aEvent->mRefPoint + aEvent->mWidget->WidgetToScreenOffset();
  2397. nsIntRect docFrameRect = docFrame->GetScreenRect(); // Returns CSS pixels
  2398. CSSIntPoint eventLocCSS(
  2399. mPresContext->DevPixelsToIntCSSPixels(eventLoc.x) - docFrameRect.x,
  2400. mPresContext->DevPixelsToIntCSSPixels(eventLoc.y) - docFrameRect.y);
  2401. Element* contentUnderMouse =
  2402. doc->ElementFromPointHelper(eventLocCSS.x, eventLocCSS.y, false, false);
  2403. if (contentUnderMouse) {
  2404. nsIWidget* targetWidget = nullptr;
  2405. nsIFrame* targetFrame = contentUnderMouse->GetPrimaryFrame();
  2406. nsIObjectFrame* pluginFrame = do_QueryFrame(targetFrame);
  2407. if (pluginFrame) {
  2408. targetWidget = pluginFrame->GetWidget();
  2409. } else if (targetFrame) {
  2410. targetWidget = targetFrame->GetNearestWidget();
  2411. }
  2412. if (aEvent->mWidget == targetWidget) {
  2413. aEvent->mReply.mWidgetIsHit = true;
  2414. }
  2415. }
  2416. aEvent->mSucceeded = true;
  2417. return NS_OK;
  2418. }
  2419. /* static */ nsresult
  2420. ContentEventHandler::GetFlatTextLengthInRange(
  2421. const NodePosition& aStartPosition,
  2422. const NodePosition& aEndPosition,
  2423. nsIContent* aRootContent,
  2424. uint32_t* aLength,
  2425. LineBreakType aLineBreakType,
  2426. bool aIsRemovingNode /* = false */)
  2427. {
  2428. if (NS_WARN_IF(!aRootContent) || NS_WARN_IF(!aStartPosition.IsValid()) ||
  2429. NS_WARN_IF(!aEndPosition.IsValid()) || NS_WARN_IF(!aLength)) {
  2430. return NS_ERROR_INVALID_ARG;
  2431. }
  2432. if (aStartPosition == aEndPosition) {
  2433. *aLength = 0;
  2434. return NS_OK;
  2435. }
  2436. // Don't create nsContentIterator instance until it's really necessary since
  2437. // destroying without initializing causes unexpected NS_ASSERTION() call.
  2438. nsCOMPtr<nsIContentIterator> iter;
  2439. // Working with ContentIterator, we may need to adjust the end position for
  2440. // including it forcibly.
  2441. NodePosition endPosition(aEndPosition);
  2442. // This may be called for retrieving the text of removed nodes. Even in this
  2443. // case, the node thinks it's still in the tree because UnbindFromTree() will
  2444. // be called after here. However, the node was already removed from the
  2445. // array of children of its parent. So, be careful to handle this case.
  2446. if (aIsRemovingNode) {
  2447. DebugOnly<nsIContent*> parent = aStartPosition.mNode->GetParent();
  2448. MOZ_ASSERT(parent && parent->IndexOf(aStartPosition.mNode) == -1,
  2449. "At removing the node, the node shouldn't be in the array of children "
  2450. "of its parent");
  2451. MOZ_ASSERT(aStartPosition.mNode == endPosition.mNode,
  2452. "At removing the node, start and end node should be same");
  2453. MOZ_ASSERT(aStartPosition.mOffset == 0,
  2454. "When the node is being removed, the start offset should be 0");
  2455. MOZ_ASSERT(static_cast<uint32_t>(endPosition.mOffset) ==
  2456. endPosition.mNode->GetChildCount(),
  2457. "When the node is being removed, the end offset should be child count");
  2458. iter = NS_NewPreContentIterator();
  2459. nsresult rv = iter->Init(aStartPosition.mNode);
  2460. if (NS_WARN_IF(NS_FAILED(rv))) {
  2461. return rv;
  2462. }
  2463. } else {
  2464. RefPtr<nsRange> prev = new nsRange(aRootContent);
  2465. nsresult rv = aStartPosition.SetToRangeStart(prev);
  2466. if (NS_WARN_IF(NS_FAILED(rv))) {
  2467. return rv;
  2468. }
  2469. // When the end position is immediately after non-root element's open tag,
  2470. // we need to include a line break caused by the open tag.
  2471. if (endPosition.mNode != aRootContent &&
  2472. endPosition.IsImmediatelyAfterOpenTag()) {
  2473. if (endPosition.mNode->HasChildren()) {
  2474. // When the end node has some children, move the end position to before
  2475. // the open tag of its first child.
  2476. nsINode* firstChild = endPosition.mNode->GetFirstChild();
  2477. if (NS_WARN_IF(!firstChild)) {
  2478. return NS_ERROR_FAILURE;
  2479. }
  2480. endPosition = NodePositionBefore(firstChild, 0);
  2481. } else {
  2482. // When the end node is empty, move the end position after the node.
  2483. nsIContent* parentContent = endPosition.mNode->GetParent();
  2484. if (NS_WARN_IF(!parentContent)) {
  2485. return NS_ERROR_FAILURE;
  2486. }
  2487. int32_t indexInParent = parentContent->IndexOf(endPosition.mNode);
  2488. if (NS_WARN_IF(indexInParent < 0)) {
  2489. return NS_ERROR_FAILURE;
  2490. }
  2491. endPosition = NodePositionBefore(parentContent, indexInParent + 1);
  2492. }
  2493. }
  2494. if (endPosition.OffsetIsValid()) {
  2495. // Offset is within node's length; set end of range to that offset
  2496. rv = endPosition.SetToRangeEnd(prev);
  2497. if (NS_WARN_IF(NS_FAILED(rv))) {
  2498. return rv;
  2499. }
  2500. iter = NS_NewPreContentIterator();
  2501. rv = iter->Init(prev);
  2502. if (NS_WARN_IF(NS_FAILED(rv))) {
  2503. return rv;
  2504. }
  2505. } else if (endPosition.mNode != aRootContent) {
  2506. // Offset is past node's length; set end of range to end of node
  2507. rv = endPosition.SetToRangeEndAfter(prev);
  2508. if (NS_WARN_IF(NS_FAILED(rv))) {
  2509. return rv;
  2510. }
  2511. iter = NS_NewPreContentIterator();
  2512. rv = iter->Init(prev);
  2513. if (NS_WARN_IF(NS_FAILED(rv))) {
  2514. return rv;
  2515. }
  2516. } else {
  2517. // Offset is past the root node; set end of range to end of root node
  2518. iter = NS_NewPreContentIterator();
  2519. rv = iter->Init(aRootContent);
  2520. if (NS_WARN_IF(NS_FAILED(rv))) {
  2521. return rv;
  2522. }
  2523. }
  2524. }
  2525. *aLength = 0;
  2526. for (; !iter->IsDone(); iter->Next()) {
  2527. nsINode* node = iter->GetCurrentNode();
  2528. if (NS_WARN_IF(!node)) {
  2529. break;
  2530. }
  2531. if (!node->IsContent()) {
  2532. continue;
  2533. }
  2534. nsIContent* content = node->AsContent();
  2535. if (node->IsNodeOfType(nsINode::eTEXT)) {
  2536. // Note: our range always starts from offset 0
  2537. if (node == endPosition.mNode) {
  2538. *aLength += GetTextLength(content, aLineBreakType,
  2539. endPosition.mOffset);
  2540. } else {
  2541. *aLength += GetTextLength(content, aLineBreakType);
  2542. }
  2543. } else if (ShouldBreakLineBefore(content, aRootContent)) {
  2544. // If the start position is start of this node but doesn't include the
  2545. // open tag, don't append the line break length.
  2546. if (node == aStartPosition.mNode && !aStartPosition.IsBeforeOpenTag()) {
  2547. continue;
  2548. }
  2549. // If the end position is before the open tag, don't append the line
  2550. // break length.
  2551. if (node == endPosition.mNode && endPosition.IsBeforeOpenTag()) {
  2552. continue;
  2553. }
  2554. *aLength += GetBRLength(aLineBreakType);
  2555. }
  2556. }
  2557. return NS_OK;
  2558. }
  2559. nsresult
  2560. ContentEventHandler::GetStartOffset(nsRange* aRange,
  2561. uint32_t* aOffset,
  2562. LineBreakType aLineBreakType)
  2563. {
  2564. MOZ_ASSERT(aRange);
  2565. return GetFlatTextLengthInRange(
  2566. NodePosition(mRootContent, 0),
  2567. NodePosition(aRange->GetStartParent(), aRange->StartOffset()),
  2568. mRootContent, aOffset, aLineBreakType);
  2569. }
  2570. nsresult
  2571. ContentEventHandler::AdjustCollapsedRangeMaybeIntoTextNode(nsRange* aRange)
  2572. {
  2573. MOZ_ASSERT(aRange);
  2574. MOZ_ASSERT(aRange->Collapsed());
  2575. if (!aRange || !aRange->Collapsed()) {
  2576. return NS_ERROR_INVALID_ARG;
  2577. }
  2578. nsCOMPtr<nsINode> parentNode = aRange->GetStartParent();
  2579. int32_t offsetInParentNode = aRange->StartOffset();
  2580. if (NS_WARN_IF(!parentNode) || NS_WARN_IF(offsetInParentNode < 0)) {
  2581. return NS_ERROR_INVALID_ARG;
  2582. }
  2583. // If the node is text node, we don't need to modify aRange.
  2584. if (parentNode->IsNodeOfType(nsINode::eTEXT)) {
  2585. return NS_OK;
  2586. }
  2587. // If the parent is not a text node but it has a text node at the offset,
  2588. // we should adjust the range into the text node.
  2589. // NOTE: This is emulating similar situation of EditorBase.
  2590. nsINode* childNode = nullptr;
  2591. int32_t offsetInChildNode = -1;
  2592. if (!offsetInParentNode && parentNode->HasChildren()) {
  2593. // If the range is the start of the parent, adjusted the range to the
  2594. // start of the first child.
  2595. childNode = parentNode->GetFirstChild();
  2596. offsetInChildNode = 0;
  2597. } else if (static_cast<uint32_t>(offsetInParentNode) <
  2598. parentNode->GetChildCount()) {
  2599. // If the range is next to a child node, adjust the range to the end of
  2600. // the previous child.
  2601. childNode = parentNode->GetChildAt(offsetInParentNode - 1);
  2602. offsetInChildNode = childNode->Length();
  2603. }
  2604. // But if the found node isn't a text node, we cannot modify the range.
  2605. if (!childNode || !childNode->IsNodeOfType(nsINode::eTEXT) ||
  2606. NS_WARN_IF(offsetInChildNode < 0)) {
  2607. return NS_OK;
  2608. }
  2609. nsresult rv = aRange->CollapseTo(childNode, offsetInChildNode);
  2610. if (NS_WARN_IF(NS_FAILED(rv))) {
  2611. return rv;
  2612. }
  2613. return NS_OK;
  2614. }
  2615. nsresult
  2616. ContentEventHandler::GetStartFrameAndOffset(const nsRange* aRange,
  2617. nsIFrame*& aFrame,
  2618. int32_t& aOffsetInFrame)
  2619. {
  2620. MOZ_ASSERT(aRange);
  2621. aFrame = nullptr;
  2622. aOffsetInFrame = -1;
  2623. nsINode* node = aRange->GetStartParent();
  2624. if (NS_WARN_IF(!node) ||
  2625. NS_WARN_IF(!node->IsNodeOfType(nsINode::eCONTENT))) {
  2626. return NS_ERROR_FAILURE;
  2627. }
  2628. nsIContent* content = static_cast<nsIContent*>(node);
  2629. RefPtr<nsFrameSelection> fs = mPresShell->FrameSelection();
  2630. aFrame = fs->GetFrameForNodeOffset(content, aRange->StartOffset(),
  2631. fs->GetHint(), &aOffsetInFrame);
  2632. if (NS_WARN_IF(!aFrame)) {
  2633. return NS_ERROR_FAILURE;
  2634. }
  2635. return NS_OK;
  2636. }
  2637. nsresult
  2638. ContentEventHandler::ConvertToRootRelativeOffset(nsIFrame* aFrame,
  2639. nsRect& aRect)
  2640. {
  2641. NS_ASSERTION(aFrame, "aFrame must not be null");
  2642. nsPresContext* thisPC = aFrame->PresContext();
  2643. nsPresContext* rootPC = thisPC->GetRootPresContext();
  2644. if (NS_WARN_IF(!rootPC)) {
  2645. return NS_ERROR_FAILURE;
  2646. }
  2647. nsIFrame* rootFrame = rootPC->PresShell()->GetRootFrame();
  2648. if (NS_WARN_IF(!rootFrame)) {
  2649. return NS_ERROR_FAILURE;
  2650. }
  2651. aRect = nsLayoutUtils::TransformFrameRectToAncestor(aFrame, aRect, rootFrame);
  2652. // TransformFrameRectToAncestor returned the rect in the ancestor's appUnits,
  2653. // but we want it in aFrame's units (in case of different full-zoom factors),
  2654. // so convert back.
  2655. aRect = aRect.ScaleToOtherAppUnitsRoundOut(rootPC->AppUnitsPerDevPixel(),
  2656. thisPC->AppUnitsPerDevPixel());
  2657. return NS_OK;
  2658. }
  2659. static void AdjustRangeForSelection(nsIContent* aRoot,
  2660. nsINode** aNode,
  2661. int32_t* aNodeOffset)
  2662. {
  2663. nsINode* node = *aNode;
  2664. int32_t nodeOffset = *aNodeOffset;
  2665. if (aRoot == node || NS_WARN_IF(!node->GetParent()) ||
  2666. !node->IsNodeOfType(nsINode::eTEXT)) {
  2667. return;
  2668. }
  2669. // When the offset is at the end of the text node, set it to after the
  2670. // text node, to make sure the caret is drawn on a new line when the last
  2671. // character of the text node is '\n' in <textarea>.
  2672. int32_t textLength =
  2673. static_cast<int32_t>(static_cast<nsIContent*>(node)->TextLength());
  2674. MOZ_ASSERT(nodeOffset <= textLength, "Offset is past length of text node");
  2675. if (nodeOffset != textLength) {
  2676. return;
  2677. }
  2678. nsIContent* aRootParent = aRoot->GetParent();
  2679. if (NS_WARN_IF(!aRootParent)) {
  2680. return;
  2681. }
  2682. // If the root node is not an anonymous div of <textarea>, we don't need to
  2683. // do this hack. If you did this, ContentEventHandler couldn't distinguish
  2684. // if the range includes open tag of the next node in some cases, e.g.,
  2685. // textNode]<p></p> vs. textNode<p>]</p>
  2686. if (!aRootParent->IsHTMLElement(nsGkAtoms::textarea)) {
  2687. return;
  2688. }
  2689. *aNode = node->GetParent();
  2690. MOZ_ASSERT((*aNode)->IndexOf(node) != -1);
  2691. *aNodeOffset = (*aNode)->IndexOf(node) + 1;
  2692. }
  2693. nsresult
  2694. ContentEventHandler::OnSelectionEvent(WidgetSelectionEvent* aEvent)
  2695. {
  2696. aEvent->mSucceeded = false;
  2697. // Get selection to manipulate
  2698. // XXX why do we need to get them from ISM? This method should work fine
  2699. // without ISM.
  2700. nsCOMPtr<nsISelection> sel;
  2701. nsresult rv =
  2702. IMEStateManager::GetFocusSelectionAndRoot(getter_AddRefs(sel),
  2703. getter_AddRefs(mRootContent));
  2704. mSelection = sel ? sel->AsSelection() : nullptr;
  2705. if (rv != NS_ERROR_NOT_AVAILABLE) {
  2706. NS_ENSURE_SUCCESS(rv, rv);
  2707. } else {
  2708. rv = Init(aEvent);
  2709. NS_ENSURE_SUCCESS(rv, rv);
  2710. }
  2711. // Get range from offset and length
  2712. RefPtr<nsRange> range = new nsRange(mRootContent);
  2713. rv = SetRangeFromFlatTextOffset(range, aEvent->mOffset, aEvent->mLength,
  2714. GetLineBreakType(aEvent),
  2715. aEvent->mExpandToClusterBoundary);
  2716. NS_ENSURE_SUCCESS(rv, rv);
  2717. nsINode* startNode = range->GetStartParent();
  2718. nsINode* endNode = range->GetEndParent();
  2719. int32_t startNodeOffset = range->StartOffset();
  2720. int32_t endNodeOffset = range->EndOffset();
  2721. AdjustRangeForSelection(mRootContent, &startNode, &startNodeOffset);
  2722. AdjustRangeForSelection(mRootContent, &endNode, &endNodeOffset);
  2723. if (NS_WARN_IF(!startNode) || NS_WARN_IF(!endNode) ||
  2724. NS_WARN_IF(startNodeOffset < 0) || NS_WARN_IF(endNodeOffset < 0)) {
  2725. return NS_ERROR_UNEXPECTED;
  2726. }
  2727. mSelection->StartBatchChanges();
  2728. // Clear selection first before setting
  2729. rv = mSelection->RemoveAllRanges();
  2730. // Need to call EndBatchChanges at the end even if call failed
  2731. if (NS_SUCCEEDED(rv)) {
  2732. if (aEvent->mReversed) {
  2733. rv = mSelection->Collapse(endNode, endNodeOffset);
  2734. } else {
  2735. rv = mSelection->Collapse(startNode, startNodeOffset);
  2736. }
  2737. if (NS_SUCCEEDED(rv) &&
  2738. (startNode != endNode || startNodeOffset != endNodeOffset)) {
  2739. if (aEvent->mReversed) {
  2740. rv = mSelection->Extend(startNode, startNodeOffset);
  2741. } else {
  2742. rv = mSelection->Extend(endNode, endNodeOffset);
  2743. }
  2744. }
  2745. }
  2746. // Pass the eSetSelection events reason along with the BatchChange-end
  2747. // selection change notifications.
  2748. mSelection->EndBatchChangesInternal(aEvent->mReason);
  2749. NS_ENSURE_SUCCESS(rv, rv);
  2750. mSelection->ScrollIntoViewInternal(
  2751. nsISelectionController::SELECTION_FOCUS_REGION,
  2752. false, nsIPresShell::ScrollAxis(), nsIPresShell::ScrollAxis());
  2753. aEvent->mSucceeded = true;
  2754. return NS_OK;
  2755. }
  2756. nsRect
  2757. ContentEventHandler::FrameRelativeRect::RectRelativeTo(
  2758. nsIFrame* aDestFrame) const
  2759. {
  2760. if (!mBaseFrame || NS_WARN_IF(!aDestFrame)) {
  2761. return nsRect();
  2762. }
  2763. if (NS_WARN_IF(aDestFrame->PresContext() != mBaseFrame->PresContext())) {
  2764. return nsRect();
  2765. }
  2766. if (aDestFrame == mBaseFrame) {
  2767. return mRect;
  2768. }
  2769. nsIFrame* rootFrame = mBaseFrame->PresContext()->PresShell()->GetRootFrame();
  2770. nsRect baseFrameRectInRootFrame =
  2771. nsLayoutUtils::TransformFrameRectToAncestor(mBaseFrame, nsRect(),
  2772. rootFrame);
  2773. nsRect destFrameRectInRootFrame =
  2774. nsLayoutUtils::TransformFrameRectToAncestor(aDestFrame, nsRect(),
  2775. rootFrame);
  2776. nsPoint difference =
  2777. destFrameRectInRootFrame.TopLeft() - baseFrameRectInRootFrame.TopLeft();
  2778. return mRect - difference;
  2779. }
  2780. } // namespace mozilla