Opera 12.15 Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OpDocumentEditSelection.cpp 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /* -*- Mode: c++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
  2. *
  3. * Copyright (C) 1995-2009 Opera Software ASA. All rights reserved.
  4. *
  5. * This file is part of the Opera web browser.
  6. * It may not be distributed under any circumstances.
  7. */
  8. #include "core/pch.h"
  9. #include "modules/documentedit/OpDocumentEdit.h"
  10. #include "modules/documentedit/OpDocumentEditUtils.h"
  11. #include "modules/doc/html_doc.h"
  12. #include "modules/layout/box/box.h"
  13. #include "modules/layout/traverse/traverse.h"
  14. #include "modules/logdoc/selection.h"
  15. #ifdef DOCUMENT_EDIT_SUPPORT
  16. HTML_Element* GetPrevSiblingActual(HTML_Element *elm)
  17. {
  18. for (const HTML_Element *leaf = elm; leaf; leaf = leaf->ParentActual() )
  19. {
  20. HTML_Element *candidate = leaf->PredActual();
  21. if (candidate)
  22. return candidate;
  23. }
  24. return NULL;
  25. }
  26. OpDocumentEditSelection::OpDocumentEditSelection(OpDocumentEdit *edit)
  27. : m_edit(edit)
  28. {
  29. DEBUG_CHECKER_CONSTRUCTOR();
  30. }
  31. void OpDocumentEditSelection::Select(OldStyleTextSelectionPoint *startpoint, OldStyleTextSelectionPoint *endpoint, BOOL place_caret_at_endpoint)
  32. {
  33. DEBUG_CHECKER(TRUE);
  34. OP_ASSERT(startpoint && startpoint->GetElement());
  35. OP_ASSERT(endpoint && endpoint->GetElement());
  36. HTML_Document *hdoc = m_edit->GetDoc()->GetHtmlDocument();
  37. OldStyleTextSelectionPoint *start, *end;
  38. if (startpoint->Precedes(*endpoint))
  39. {
  40. start = startpoint;
  41. end = endpoint;
  42. }
  43. else
  44. {
  45. start = endpoint;
  46. end = startpoint;
  47. place_caret_at_endpoint = !place_caret_at_endpoint;
  48. }
  49. if(hdoc)
  50. {
  51. SelectionBoundaryPoint fixed_start = OldStyleTextSelectionPoint::ConvertFromOldStyleSelectionPoint(*start);
  52. SelectionBoundaryPoint fixed_end = OldStyleTextSelectionPoint::ConvertFromOldStyleSelectionPoint(*end);
  53. hdoc->SetSelection(&fixed_start, &fixed_end, place_caret_at_endpoint);
  54. }
  55. }
  56. void OpDocumentEditSelection::SelectToCaret(HTML_Element* from_helm, INT32 from_ofs)
  57. {
  58. DEBUG_CHECKER(TRUE);
  59. OP_ASSERT(from_helm);
  60. OldStyleTextSelectionPoint sel_point1, sel_point2;
  61. if(!m_edit->m_doc->GetCaret()->GetElement())
  62. {
  63. OP_ASSERT(FALSE);
  64. return;
  65. }
  66. sel_point1 = m_edit->GetTextSelectionPoint(from_helm, from_ofs);
  67. sel_point2 = m_edit->GetTextSelectionPoint(m_edit->m_doc->GetCaret()->GetElement(), m_edit->m_doc->GetCaret()->GetOffset());
  68. Select(&sel_point1, &sel_point2, TRUE);
  69. }
  70. void OpDocumentEditSelection::SelectAll()
  71. {
  72. DEBUG_CHECKER(TRUE);
  73. // Select all either in the document or in the focused fragment
  74. m_edit->ReflowAndUpdate();
  75. HTML_Document *hdoc = m_edit->GetDoc()->GetHtmlDocument();
  76. if(hdoc)
  77. {
  78. HTML_Element* ec = m_edit->GetEditableContainer(m_edit->m_caret.GetElement());
  79. BOOL is_focused = m_edit->IsFocused(FALSE) || m_edit->IsFocused(TRUE);
  80. hdoc->SelectAll(TRUE, is_focused ? ec : NULL);
  81. }
  82. }
  83. void OpDocumentEditSelection::SelectNothing()
  84. {
  85. DEBUG_CHECKER(TRUE);
  86. // Collapse the selection around the caret.
  87. if (TextSelection* sel = m_edit->GetDoc()->GetTextSelection())
  88. {
  89. if (!sel->IsEmpty())
  90. {
  91. SelectionBoundaryPoint focus_point = *sel->GetFocusPoint();
  92. m_edit->GetDoc()->SetSelection(&focus_point, &focus_point);
  93. }
  94. }
  95. }
  96. BOOL OpDocumentEditSelection::HasContent()
  97. {
  98. DEBUG_CHECKER(TRUE);
  99. return m_edit->GetDoc()->HasSelectedText();
  100. }
  101. void OpDocumentEditSelection::RemoveContent(BOOL aggressive)
  102. {
  103. DEBUG_CHECKER(TRUE);
  104. SelectionState sel_state = m_edit->GetSelectionState(TRUE,FALSE,FALSE);
  105. if(!sel_state.HasContent() || !m_edit->ActualizeSelectionState(sel_state))
  106. return;
  107. RemoveContent(aggressive, sel_state.start_elm, sel_state.stop_elm, sel_state.start_ofs, sel_state.stop_ofs);
  108. //if (!HasContent())
  109. // return;
  110. //HTML_Element* start_elm = GetStartElement();
  111. //HTML_Element* stop_elm = GetStopElement();
  112. //INT32 start_ofs = GetStartOfs();
  113. //INT32 stop_ofs = GetStopOfs();
  114. //m_edit->GetDoc()->GetHtmlDocument()->ClearSelection(FALSE); // FIX: Unneccesary selection update is done in here.
  115. //RemoveContent(aggressive, start_elm, stop_elm, start_ofs, stop_ofs);
  116. }
  117. BOOL OpDocumentEditSelection::GetRemoveStopElm(BOOL aggressive, HTML_Element *start_elm, int start_ofs, HTML_Element *stop_elm, int stop_ofs)
  118. {
  119. DEBUG_CHECKER(TRUE);
  120. if(!start_elm || !stop_elm || start_elm == stop_elm)
  121. {
  122. OP_ASSERT(FALSE);
  123. return FALSE;
  124. }
  125. // remove empty text-element
  126. if(stop_elm->Type() == HE_TEXT)
  127. return stop_elm->GetTextContentLength() == 0;
  128. // remove elements if stop_ofs > 0
  129. if(stop_elm->Type() != HE_BR)
  130. return !!stop_ofs;
  131. // if !aggressive we want to remove the same content that GetTextHtml returns, which includes this BR
  132. if(!aggressive)
  133. return TRUE;
  134. if(start_elm->Type() == HE_BR)
  135. return FALSE;
  136. return !m_edit->IsEndingBr(stop_elm);
  137. }
  138. void OpDocumentEditSelection::RemoveContent(BOOL aggressive, HTML_Element* start_elm, HTML_Element* stop_elm, INT32 start_ofs, INT32 stop_ofs, BOOL change_caret_pos)
  139. {
  140. DEBUG_CHECKER(TRUE);
  141. int nearest_ofs = 0;
  142. HTML_Element* nearest_helm = NULL;
  143. HTML_Element* body = m_edit->GetBody();
  144. FramesDocument* fdoc = m_edit->GetDoc();
  145. // FIX: will crash when start or stop returns something bad. (When selection goes first or last and there is no word is one example) OP_ASSERT(start_elm == stop_elm || start_elm->Precedes(stop_elm));
  146. if(start_elm != stop_elm && (start_elm->IsAncestorOf(stop_elm) || stop_elm->IsAncestorOf(start_elm)) || !body)
  147. {
  148. OP_ASSERT(start_elm->IsAncestorOf(stop_elm));
  149. OP_ASSERT(stop_elm->IsAncestorOf(start_elm));
  150. OP_ASSERT(body);
  151. return;
  152. }
  153. HTML_Element* shared_containing_elm = m_edit->GetSharedContainingElement(start_elm, stop_elm);
  154. OP_ASSERT(body->IsAncestorOf(shared_containing_elm));
  155. // Make sure that shared_containing_elm doesn't contain ALL content that should be removed,
  156. // beacuse shared_containing_elm might then itself be a candidate for removal by Tidy()
  157. // if aggressive==TRUE, and this would otherwise be prevented.
  158. if(aggressive && !start_ofs && stop_ofs == stop_elm->GetTextContentLength())
  159. {
  160. DocTree *prev = start_elm->PrevSibling();
  161. DocTree *next = stop_elm->NextSibling();
  162. while(shared_containing_elm != body &&
  163. (!prev || !shared_containing_elm->IsAncestorOf(prev)) &&
  164. (!next || !shared_containing_elm->IsAncestorOf(next)))
  165. {
  166. shared_containing_elm = m_edit->GetDoc()->GetCaret()->GetContainingElementActual(shared_containing_elm);
  167. }
  168. }
  169. m_edit->BeginChange(shared_containing_elm, CHANGE_FLAGS_ALLOW_APPEND);
  170. if (change_caret_pos)
  171. m_edit->m_caret.Set(NULL,0);
  172. OpDocumentEditWsPreserver preserver(start_elm, stop_elm, start_ofs, stop_ofs, m_edit);
  173. OpDocumentEditWsPreserverContainer preserver_container(&preserver, m_edit);
  174. OpDocumentEditRangeKeeper range_keeper(shared_containing_elm, start_elm, stop_elm, m_edit);
  175. if (start_elm == stop_elm)
  176. {
  177. m_edit->DeleteTextInElement(start_elm, start_ofs, stop_ofs - start_ofs);
  178. }
  179. else
  180. {
  181. HTML_Element* start_container = m_edit->GetDoc()->GetCaret()->GetContainingElementActual(start_elm);
  182. HTML_Element* stop_container = m_edit->GetDoc()->GetCaret()->GetContainingElementActual(stop_elm);
  183. OP_ASSERT(shared_containing_elm->IsAncestorOf(start_container));
  184. OP_ASSERT(shared_containing_elm->IsAncestorOf(stop_container));
  185. // Crop first element
  186. m_edit->DeleteTextInElement(start_elm, start_ofs, start_elm->GetTextContentLength() - start_ofs);
  187. // Remove elements between
  188. HTML_Element* tmp = (HTML_Element*)(start_elm->NextSiblingActual());
  189. BOOL moved_stop_elm = FALSE;
  190. while(tmp != stop_elm)
  191. {
  192. HTML_Element* next_elm = (HTML_Element *) tmp->NextActual();
  193. if (m_edit->IsStandaloneElement(tmp) && !tmp->FirstChild())
  194. {
  195. // Remove all standalone elements. Eventual empty parents will be removed in Tidy if they should.
  196. m_edit->DeleteElement(tmp);
  197. }
  198. else if (!m_edit->KeepWhenTidy(tmp) && m_edit->IsEnclosedBy(tmp, start_elm, stop_elm))
  199. {
  200. // If a non-standalone element is completly enclosed by selection, we can remove it.
  201. next_elm = (HTML_Element*) tmp->LastLeaf()->NextActual();
  202. m_edit->DeleteElement(tmp);
  203. }
  204. else if (!m_edit->KeepWhenTidy(tmp) && !m_edit->IsFriendlyElement(tmp) && tmp->Type() != HE_TABLE)
  205. {
  206. // User tries to delete at the end of a container, reaching out of that container and into next or parent.
  207. // We will move move up its content and delete the container.
  208. // Remove elements under tmp that is previous stop_elm
  209. HTML_Element *tmp_remove = stop_elm->Prev();
  210. while(tmp_remove && tmp->IsAncestorOf(tmp_remove) && tmp_remove != tmp)
  211. {
  212. HTML_Element *tmp_prev = tmp_remove->PrevActual();
  213. if(!tmp_remove->IsAncestorOf(stop_elm) && !m_edit->KeepWhenTidy(tmp_remove))
  214. m_edit->DeleteElement(tmp_remove);
  215. tmp_remove = tmp_prev;
  216. }
  217. if(!stop_container->IsAncestorOf(start_container))
  218. {
  219. HTML_Element *follow_me = start_elm;
  220. while(follow_me->Parent() != start_container)
  221. follow_me = follow_me->Parent();
  222. while(HTML_Element* move = stop_container->LastChild())
  223. {
  224. move->OutSafe(fdoc, FALSE);
  225. move->FollowSafe(fdoc, follow_me);
  226. move->MarkExtraDirty(fdoc);
  227. }
  228. m_edit->GetBody()->MarkDirty(fdoc, FALSE, TRUE);
  229. m_edit->DeleteElement(stop_container);
  230. moved_stop_elm = TRUE;
  231. }
  232. next_elm = stop_elm;
  233. }
  234. tmp = next_elm;
  235. }
  236. // Crop last element
  237. m_edit->DeleteTextInElement(stop_elm, 0, stop_ofs);
  238. // Possibly move stop_elm with "friends" down in the tree to after start_elm
  239. if(!moved_stop_elm && stop_container != start_container &&
  240. stop_container->IsAncestorOf(start_container) && m_edit->IsFriendlyElement(stop_elm,TRUE))
  241. {
  242. m_edit->ExtractElementsTo(stop_elm,NULL,stop_container,start_container,start_container->LastChild());
  243. }
  244. // Tidy up all garbage between start and stop
  245. m_edit->Tidy(start_elm, stop_elm, FALSE);
  246. if (GetRemoveStopElm(aggressive,start_elm,start_ofs,stop_elm,stop_ofs))
  247. {
  248. m_edit->DeleteElement(stop_elm);
  249. stop_elm = NULL;
  250. }
  251. }
  252. // Deal with deletion of the start_elm
  253. BOOL must_keep_start = FALSE;
  254. if(start_elm->Type() != HE_TEXT && start_elm->Type() != HE_BR && !start_ofs)
  255. {
  256. if(!m_edit->GetBestCaretPosFrom(start_elm,nearest_helm,nearest_ofs,MAYBE,TRUE))
  257. {
  258. nearest_helm = m_edit->NewTextElement(UNI_L(""),0);
  259. nearest_ofs = 0;
  260. if(!nearest_helm)
  261. m_edit->ReportOOM();
  262. else
  263. nearest_helm->PrecedeSafe(fdoc,start_elm);
  264. }
  265. if(nearest_helm)
  266. {
  267. m_edit->DeleteElement(start_elm);
  268. if (start_elm == stop_elm)
  269. {
  270. stop_elm = nearest_helm;
  271. stop_ofs = nearest_ofs;
  272. }
  273. start_elm = nearest_helm;
  274. start_ofs = nearest_ofs;
  275. must_keep_start = m_edit->IsReplacedElement(start_elm);
  276. }
  277. }
  278. // Make sure start_elm is not deleted if its containing element doesn't contain any editable element
  279. // and if it wasn't empty from the beginning.
  280. if (start_elm->Type() == HE_TEXT && start_elm->GetTextContentLength() == 0)
  281. {
  282. HTML_Element *start_containing_elm = m_edit->GetDoc()->GetCaret()->GetContainingElementActual(start_elm);
  283. if (m_edit->NeedAutoInsertedBR(start_containing_elm))
  284. {
  285. // We need to add a BR to not collapse the layout.
  286. HTML_Element *newbr = m_edit->NewElement(HE_BR, TRUE);
  287. if (newbr)
  288. {
  289. newbr->FollowSafe(fdoc, start_elm);
  290. nearest_ofs = 0;
  291. nearest_helm = newbr;
  292. }
  293. }
  294. else if(!m_edit->GetBestCaretPosFrom(start_elm,nearest_helm,nearest_ofs,MAYBE,TRUE))
  295. m_edit->SetElementText(start_elm, document_edit_dummy_str);
  296. }
  297. if (start_elm->Type() == HE_TEXT && start_elm->GetTextContentLength() == 0 && start_elm->Parent()->FirstChild() == start_elm->Parent()->LastChild())
  298. {
  299. start_ofs = 0;
  300. }
  301. else if (!must_keep_start && start_elm->GetTextContentLength() == 0 && aggressive && !(start_elm->Type() != HE_TEXT && start_ofs))
  302. {
  303. if (!nearest_helm && stop_elm && stop_elm->Type() == HE_BR && stop_ofs == 0)
  304. {
  305. // If we have a linebreak as stop element, we can place the caret there when we are done
  306. // instead of creating empty text node.
  307. nearest_ofs = 0;
  308. nearest_helm = stop_elm;
  309. }
  310. else if (!nearest_helm)
  311. {
  312. nearest_ofs = 0;
  313. nearest_helm = m_edit->NewTextElement(UNI_L(""),0);
  314. if(!nearest_helm)
  315. m_edit->ReportOOM();
  316. else
  317. nearest_helm->PrecedeSafe(fdoc,start_elm);
  318. }
  319. if (nearest_helm)
  320. {
  321. HTML_Element* tmp = start_elm;
  322. start_elm = nearest_helm;
  323. start_ofs = nearest_ofs;
  324. // Delete the element and it's parents if they become empty.
  325. HTML_Element* parent = tmp->Parent();
  326. m_edit->DeleteElement(tmp);
  327. if(tmp == stop_elm)
  328. stop_elm = NULL;
  329. while(parent && !parent->FirstChild() && parent != shared_containing_elm && !m_edit->KeepWhenTidy(parent))
  330. {
  331. tmp = parent;
  332. parent = parent->Parent();
  333. m_edit->DeleteElement(tmp);
  334. if(tmp == stop_elm)
  335. stop_elm = NULL;
  336. }
  337. }
  338. }
  339. if (start_elm->Type() == HE_TEXT && start_elm->GetTextContentLength() == 0 && start_elm->ParentActual()->IsContentEditable())
  340. m_edit->SetElementText(start_elm, document_edit_dummy_str);
  341. // Update caret, just use Set so we won't remove collapsed "garbage" that might be valid after preserver.WsPreserve()
  342. if (change_caret_pos)
  343. m_edit->m_caret.Set(start_elm, start_ofs);
  344. if(aggressive)
  345. {
  346. preserver.WsPreserve();
  347. m_edit->RemoveNBSPIfPossible(start_elm);
  348. m_edit->RemoveNBSPIfPossible(stop_elm);
  349. }
  350. if(!range_keeper.IsValid() || range_keeper.GetContainer() != shared_containing_elm)
  351. {
  352. OP_ASSERT(FALSE); // shared_containing_elm has probably been deleted!
  353. m_edit->AbortChange();
  354. }
  355. else
  356. {
  357. TIDY_LEVEL tidy_level = aggressive ? TIDY_LEVEL_AGGRESSIVE : TIDY_LEVEL_MINIMAL;
  358. HTML_Element *t_start = range_keeper.GetStartElement();
  359. HTML_Element *t_stop = range_keeper.GetStopElement();
  360. m_edit->EndChange(shared_containing_elm, t_start, t_stop, TRUE, tidy_level);
  361. }
  362. // Now it's ok to finally use Place, see above.
  363. if (change_caret_pos)
  364. m_edit->m_caret.Place(m_edit->m_caret.GetElement(), m_edit->m_caret.GetOffset(), FALSE, aggressive);
  365. }
  366. OP_STATUS OpDocumentEditSelection::GetText(OpString& text)
  367. {
  368. DEBUG_CHECKER(TRUE);
  369. int len = m_edit->GetDoc()->GetTextSelection()->GetSelectionAsText(m_edit->GetDoc(), NULL, 0, FALSE);
  370. if (text.Reserve(len + 1) == NULL)
  371. return OpStatus::ERR_NO_MEMORY;
  372. m_edit->GetDoc()->GetTextSelection()->GetSelectionAsText(m_edit->GetDoc(), text.CStr(), len + 1, FALSE);
  373. return OpStatus::OK;
  374. }
  375. OP_STATUS OpDocumentEditSelection::GetTextHTML(OpString& text, BOOL include_style_containers)
  376. {
  377. DEBUG_CHECKER(TRUE);
  378. SelectionState state = m_edit->GetSelectionState(FALSE);
  379. if(!state.IsValid())
  380. {
  381. return text.Set(UNI_L(""));
  382. }
  383. return GetTextHTML(text, state.editable_start_elm, state.editable_stop_elm, state.editable_start_ofs, state.editable_stop_ofs,include_style_containers);
  384. }
  385. OP_STATUS OpDocumentEditSelection::GetTextHTML(OpString& text, HTML_Element* start_elm, HTML_Element* stop_elm, INT32 start_ofs, INT32 stop_ofs, BOOL include_style_containers)
  386. {
  387. // Create a temporary elementtree with the selected text and call HTML5Parser::SerializeTreeL() to
  388. // convert it into htmltext. This should maby be done on a better way leter (without creating the extra elementtree).
  389. DEBUG_CHECKER(TRUE);
  390. FramesDocument *fdoc = m_edit->GetDoc();
  391. HLDocProfile *hld_profile = fdoc->GetHLDocProfile();
  392. HTML_Element* sel_start_elm = start_elm;
  393. HTML_Element* sel_stop_elm = stop_elm;
  394. if (start_elm == stop_elm && start_ofs == stop_ofs)
  395. return text.Set(UNI_L(""));
  396. HTML_Element* rootdiv_helm = m_edit->GetSharedContainingElement(start_elm, stop_elm);
  397. if(include_style_containers)
  398. {
  399. // Include UR, OL and PRE tags in the copied text to.
  400. // Note: Removed PRE for now since we doesn't handle copy/paste of style elements (an other) properly. Elements are duplicated when that happens.
  401. while(rootdiv_helm->Type() == HE_UL || rootdiv_helm->Type() == HE_OL /*|| rootdiv_helm->Type() == HE_PRE*/)
  402. rootdiv_helm = m_edit->GetDoc()->GetCaret()->GetContainingElementActual(rootdiv_helm);
  403. }
  404. // Get the toplevel of start and stop elements
  405. while(start_elm->ParentActual() != rootdiv_helm)
  406. start_elm = start_elm->ParentActual();
  407. while(stop_elm->ParentActual() != rootdiv_helm)
  408. stop_elm = stop_elm->ParentActual();
  409. // Create a temporary root and DeepClone everything inside start_elm and stop_elm.
  410. // FIX: use HTML_Element::DOMCloneElement ?
  411. HTML_Element* tmproot = m_edit->NewCopyOfElement(rootdiv_helm);
  412. if (!tmproot)
  413. return OpStatus::ERR_NO_MEMORY;
  414. HTML_Element* tmp = start_elm;
  415. while(tmp != stop_elm->Suc())
  416. {
  417. HTML_Element* new_elm = m_edit->NewCopyOfElement(tmp);
  418. if (!new_elm || OpStatus::IsError(new_elm->DeepClone(hld_profile, tmp)))
  419. {
  420. m_edit->DeleteElement(new_elm);
  421. m_edit->DeleteElement(tmproot);
  422. return OpStatus::ERR_NO_MEMORY;
  423. }
  424. new_elm->UnderSafe(fdoc, tmproot);
  425. tmp = tmp->Suc();
  426. }
  427. // Find the elements in the temporary tree that is the copies of selection start and stop in the original tree.
  428. HTML_Element* original_tree_elm = start_elm;
  429. // If tree was empty, we have already returned, so this cannot be null:
  430. // coverity[returned_null: FALSE]
  431. HTML_Element* temp_tree_elm = tmproot->FirstChildActual();
  432. while(original_tree_elm != sel_start_elm)
  433. {
  434. original_tree_elm = (HTML_Element*) original_tree_elm->NextActual();
  435. temp_tree_elm = (HTML_Element*) temp_tree_elm->NextActual();
  436. }
  437. HTML_Element* temp_start = temp_tree_elm;
  438. original_tree_elm = stop_elm;
  439. temp_tree_elm = tmproot->LastChild();
  440. while(original_tree_elm != sel_stop_elm)
  441. {
  442. original_tree_elm = (HTML_Element*) original_tree_elm->NextActual();
  443. temp_tree_elm = (HTML_Element*) temp_tree_elm->NextActual();
  444. }
  445. HTML_Element* temp_stop = temp_tree_elm;
  446. OP_ASSERT(temp_start->Type() == sel_start_elm->Type());
  447. OP_ASSERT(temp_stop->Type() == sel_stop_elm->Type());
  448. OP_ASSERT(temp_start->Type() != HE_TEXT || uni_strcmp(temp_start->TextContent(), sel_start_elm->TextContent()) == 0);
  449. OP_ASSERT(temp_stop->Type() != HE_TEXT || uni_strcmp(temp_stop->TextContent(), sel_stop_elm->TextContent()) == 0);
  450. // Delete the unselected textparts in the temporary tree
  451. tmp = tmproot->FirstChild();
  452. while(tmp != temp_start)
  453. {
  454. HTML_Element* next_elm = (HTML_Element*) tmp->Next();
  455. if (!tmp->IsAncestorOf(temp_start))
  456. {
  457. next_elm = (HTML_Element*)tmp->NextSibling();
  458. m_edit->DeleteElement(tmp);
  459. }
  460. tmp = next_elm;
  461. }
  462. tmp = (HTML_Element*) temp_stop->Next();
  463. while(tmp)
  464. {
  465. HTML_Element* next_elm = (HTML_Element*) tmp->NextSibling();
  466. m_edit->DeleteElement(tmp);
  467. tmp = next_elm;
  468. }
  469. if (temp_stop->Type() == HE_BR && stop_ofs == 0 && temp_start != temp_stop)
  470. {
  471. m_edit->DeleteElement(temp_stop);
  472. temp_stop = NULL;
  473. }
  474. else
  475. m_edit->DeleteTextInElement(temp_stop, stop_ofs, temp_stop->GetTextContentLength() - stop_ofs);
  476. m_edit->DeleteTextInElement(temp_start, 0, start_ofs);
  477. // Remove all nontexttags that are now empty (B, FONT, I etc..)
  478. m_edit->Tidy(tmproot, tmproot, FALSE, TIDY_LEVEL_AGGRESSIVE);
  479. // Convert the tmproot into a HTML string
  480. OP_STATUS oom_stat = m_edit->GetTextHTMLFromElement(text, tmproot, FALSE);
  481. m_edit->DeleteElement(tmproot);
  482. return oom_stat;
  483. }
  484. HTML_Element* OpDocumentEditSelection::GetStartElement(BOOL only_actual_element)
  485. {
  486. DEBUG_CHECKER(TRUE);
  487. OP_ASSERT(HasContent());
  488. HTML_Element* start_elm;
  489. int start_offset;
  490. HTML_Element* end_elm;
  491. int end_offset;
  492. TextSelection::ConvertPointToOldStyle(m_edit->GetDoc()->GetTextSelection()->GetStartSelectionPoint(), start_elm, start_offset);
  493. TextSelection::ConvertPointToOldStyle(m_edit->GetDoc()->GetTextSelection()->GetEndSelectionPoint(), end_elm, end_offset);
  494. OldStyleTextSelectionPoint start_point(start_elm, start_offset);
  495. OldStyleTextSelectionPoint end_point(end_elm, end_offset);
  496. if (end_point.Precedes(start_point))
  497. {
  498. start_elm = end_elm;
  499. start_offset = end_offset;
  500. }
  501. if (start_elm->GetIsPseudoElement() || (only_actual_element && !start_elm->IsIncludedActual()))
  502. start_elm = start_elm->ParentActual();
  503. else if (start_elm->Parent()->GetIsPseudoElement())
  504. {
  505. HTML_Element *candidate = start_elm->ParentActual()->FirstChildActual();
  506. if (!candidate)
  507. {
  508. start_elm = GetPrevSiblingActual(start_elm->ParentActual());
  509. while (!candidate && start_elm)
  510. {
  511. candidate = start_elm->LastChildActual();
  512. if (!candidate)
  513. start_elm = GetPrevSiblingActual(start_elm);
  514. }
  515. }
  516. return candidate;
  517. }
  518. return start_elm;
  519. }
  520. HTML_Element* OpDocumentEditSelection::GetStopElement(BOOL only_actual_element)
  521. {
  522. DEBUG_CHECKER(TRUE);
  523. OP_ASSERT(HasContent());
  524. HTML_Element* start_elm;
  525. int start_offset;
  526. HTML_Element* end_elm;
  527. int end_offset;
  528. TextSelection::ConvertPointToOldStyle(m_edit->GetDoc()->GetTextSelection()->GetStartSelectionPoint(), start_elm, start_offset);
  529. TextSelection::ConvertPointToOldStyle(m_edit->GetDoc()->GetTextSelection()->GetEndSelectionPoint(), end_elm, end_offset);
  530. OldStyleTextSelectionPoint start_point(start_elm, start_offset);
  531. OldStyleTextSelectionPoint end_point(end_elm, end_offset);
  532. if (end_point.Precedes(start_point))
  533. {
  534. end_elm = start_elm;
  535. end_offset = start_offset;
  536. }
  537. if (end_elm->GetIsPseudoElement() || (only_actual_element && !end_elm->IsIncludedActual()))
  538. end_elm = end_elm->ParentActual();
  539. else if (end_elm->Parent()->GetIsPseudoElement())
  540. {
  541. HTML_Element *candidate = end_elm->ParentActual()->LastChildActual();
  542. if (!candidate)
  543. {
  544. end_elm = end_elm->ParentActual()->NextSiblingActual();
  545. while (!candidate && end_elm)
  546. {
  547. candidate = end_elm->FirstChildActual();
  548. if (!candidate)
  549. end_elm = end_elm->NextSiblingActual();
  550. }
  551. }
  552. return candidate;
  553. }
  554. return end_elm;
  555. }
  556. INT32 OpDocumentEditSelection::GetStartOfs()
  557. {
  558. DEBUG_CHECKER(TRUE);
  559. OP_ASSERT(HasContent());
  560. TextSelection* ts = m_edit->GetDoc()->GetTextSelection();
  561. HTML_Element *helm;
  562. int offset;
  563. TextSelection::ConvertPointToOldStyle(ts->GetStartSelectionPoint(), helm, offset);
  564. if (GetStartElement() != helm)
  565. return 0;
  566. return offset;
  567. }
  568. INT32 OpDocumentEditSelection::GetStopOfs()
  569. {
  570. DEBUG_CHECKER(TRUE);
  571. OP_ASSERT(HasContent());
  572. TextSelection* ts = m_edit->GetDoc()->GetTextSelection();
  573. HTML_Element *helm;
  574. int offset;
  575. TextSelection::ConvertPointToOldStyle(ts->GetEndSelectionPoint(), helm, offset);
  576. HTML_Element *stop_elm = GetStopElement();
  577. if (stop_elm && stop_elm != helm)
  578. return stop_elm->GetTextContentLength();
  579. return offset;
  580. }
  581. BOOL OpDocumentEditSelection::IsEndPointFocusPoint()
  582. {
  583. DEBUG_CHECKER(TRUE);
  584. TextSelection* ts = m_edit->GetDoc()->GetTextSelection();
  585. return ts && ts->GetFocusPoint() == &ts->GetEndSelectionPoint();
  586. }
  587. #endif // DOCUMENT_EDIT_SUPPORT