inDOMView.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. /* -*- Mode: C++; tab-width: 2; 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 "inDOMView.h"
  6. #include "inIDOMUtils.h"
  7. #include "inLayoutUtils.h"
  8. #include "nsString.h"
  9. #include "nsReadableUtils.h"
  10. #include "nsIDOMNode.h"
  11. #include "nsIDOMNodeFilter.h"
  12. #include "nsIDOMNodeList.h"
  13. #include "nsIDOMCharacterData.h"
  14. #include "nsIDOMAttr.h"
  15. #include "nsIDOMMozNamedAttrMap.h"
  16. #include "nsIDOMMutationEvent.h"
  17. #include "nsBindingManager.h"
  18. #include "nsNameSpaceManager.h"
  19. #include "nsIDocument.h"
  20. #include "nsIServiceManager.h"
  21. #include "nsITreeColumns.h"
  22. #include "nsITreeBoxObject.h"
  23. #include "mozilla/dom/Element.h"
  24. #include "mozilla/Services.h"
  25. #ifdef ACCESSIBILITY
  26. #include "nsAccessibilityService.h"
  27. #endif
  28. using namespace mozilla;
  29. ////////////////////////////////////////////////////////////////////////
  30. // inDOMViewNode
  31. class inDOMViewNode
  32. {
  33. public:
  34. inDOMViewNode() {}
  35. explicit inDOMViewNode(nsIDOMNode* aNode);
  36. ~inDOMViewNode();
  37. nsCOMPtr<nsIDOMNode> node;
  38. inDOMViewNode* parent;
  39. inDOMViewNode* next;
  40. inDOMViewNode* previous;
  41. int32_t level;
  42. bool isOpen;
  43. bool isContainer;
  44. bool hasAnonymous;
  45. bool hasSubDocument;
  46. };
  47. inDOMViewNode::inDOMViewNode(nsIDOMNode* aNode) :
  48. node(aNode),
  49. parent(nullptr),
  50. next(nullptr),
  51. previous(nullptr),
  52. level(0),
  53. isOpen(false),
  54. isContainer(false),
  55. hasAnonymous(false),
  56. hasSubDocument(false)
  57. {
  58. }
  59. inDOMViewNode::~inDOMViewNode()
  60. {
  61. }
  62. ////////////////////////////////////////////////////////////////////////
  63. inDOMView::inDOMView() :
  64. mShowAnonymous(false),
  65. mShowSubDocuments(false),
  66. mShowWhitespaceNodes(true),
  67. mShowAccessibleNodes(false),
  68. mWhatToShow(nsIDOMNodeFilter::SHOW_ALL)
  69. {
  70. }
  71. inDOMView::~inDOMView()
  72. {
  73. SetRootNode(nullptr);
  74. }
  75. ////////////////////////////////////////////////////////////////////////
  76. // nsISupports
  77. NS_IMPL_ISUPPORTS(inDOMView,
  78. inIDOMView,
  79. nsITreeView,
  80. nsIMutationObserver)
  81. ////////////////////////////////////////////////////////////////////////
  82. // inIDOMView
  83. NS_IMETHODIMP
  84. inDOMView::GetRootNode(nsIDOMNode** aNode)
  85. {
  86. *aNode = mRootNode;
  87. NS_IF_ADDREF(*aNode);
  88. return NS_OK;
  89. }
  90. NS_IMETHODIMP
  91. inDOMView::SetRootNode(nsIDOMNode* aNode)
  92. {
  93. if (mTree)
  94. mTree->BeginUpdateBatch();
  95. if (mRootDocument) {
  96. // remove previous document observer
  97. nsCOMPtr<nsINode> doc(do_QueryInterface(mRootDocument));
  98. if (doc)
  99. doc->RemoveMutationObserver(this);
  100. }
  101. RemoveAllNodes();
  102. mRootNode = aNode;
  103. if (aNode) {
  104. // If we are able to show element nodes, then start with the root node
  105. // as the first node in the buffer
  106. if (mWhatToShow & nsIDOMNodeFilter::SHOW_ELEMENT) {
  107. // allocate new node array
  108. AppendNode(CreateNode(aNode, nullptr));
  109. } else {
  110. // place only the children of the root node in the buffer
  111. ExpandNode(-1);
  112. }
  113. // store an owning reference to document so that it isn't
  114. // destroyed before we are
  115. mRootDocument = do_QueryInterface(aNode);
  116. if (!mRootDocument) {
  117. aNode->GetOwnerDocument(getter_AddRefs(mRootDocument));
  118. }
  119. // add document observer
  120. nsCOMPtr<nsINode> doc(do_QueryInterface(mRootDocument));
  121. if (doc)
  122. doc->AddMutationObserver(this);
  123. } else {
  124. mRootDocument = nullptr;
  125. }
  126. if (mTree)
  127. mTree->EndUpdateBatch();
  128. return NS_OK;
  129. }
  130. NS_IMETHODIMP
  131. inDOMView::GetNodeFromRowIndex(int32_t rowIndex, nsIDOMNode **_retval)
  132. {
  133. inDOMViewNode* viewNode = nullptr;
  134. RowToNode(rowIndex, &viewNode);
  135. if (!viewNode) return NS_ERROR_FAILURE;
  136. *_retval = viewNode->node;
  137. NS_IF_ADDREF(*_retval);
  138. return NS_OK;
  139. }
  140. NS_IMETHODIMP
  141. inDOMView::GetRowIndexFromNode(nsIDOMNode *node, int32_t *_retval)
  142. {
  143. NodeToRow(node, _retval);
  144. return NS_OK;
  145. }
  146. NS_IMETHODIMP
  147. inDOMView::GetShowAnonymousContent(bool *aShowAnonymousContent)
  148. {
  149. *aShowAnonymousContent = mShowAnonymous;
  150. return NS_OK;
  151. }
  152. NS_IMETHODIMP
  153. inDOMView::SetShowAnonymousContent(bool aShowAnonymousContent)
  154. {
  155. mShowAnonymous = aShowAnonymousContent;
  156. return NS_OK;
  157. }
  158. NS_IMETHODIMP
  159. inDOMView::GetShowSubDocuments(bool *aShowSubDocuments)
  160. {
  161. *aShowSubDocuments = mShowSubDocuments;
  162. return NS_OK;
  163. }
  164. NS_IMETHODIMP
  165. inDOMView::SetShowSubDocuments(bool aShowSubDocuments)
  166. {
  167. mShowSubDocuments = aShowSubDocuments;
  168. return NS_OK;
  169. }
  170. NS_IMETHODIMP
  171. inDOMView::GetShowWhitespaceNodes(bool *aShowWhitespaceNodes)
  172. {
  173. *aShowWhitespaceNodes = mShowWhitespaceNodes;
  174. return NS_OK;
  175. }
  176. NS_IMETHODIMP
  177. inDOMView::SetShowWhitespaceNodes(bool aShowWhitespaceNodes)
  178. {
  179. mShowWhitespaceNodes = aShowWhitespaceNodes;
  180. return NS_OK;
  181. }
  182. NS_IMETHODIMP
  183. inDOMView::GetShowAccessibleNodes(bool *aShowAccessibleNodes)
  184. {
  185. *aShowAccessibleNodes = mShowAccessibleNodes;
  186. return NS_OK;
  187. }
  188. NS_IMETHODIMP
  189. inDOMView::SetShowAccessibleNodes(bool aShowAccessibleNodes)
  190. {
  191. mShowAccessibleNodes = aShowAccessibleNodes;
  192. return NS_OK;
  193. }
  194. NS_IMETHODIMP
  195. inDOMView::GetWhatToShow(uint32_t *aWhatToShow)
  196. {
  197. *aWhatToShow = mWhatToShow;
  198. return NS_OK;
  199. }
  200. NS_IMETHODIMP
  201. inDOMView::SetWhatToShow(uint32_t aWhatToShow)
  202. {
  203. mWhatToShow = aWhatToShow;
  204. return NS_OK;
  205. }
  206. NS_IMETHODIMP
  207. inDOMView::Rebuild()
  208. {
  209. nsCOMPtr<nsIDOMNode> root;
  210. GetRootNode(getter_AddRefs(root));
  211. SetRootNode(root);
  212. return NS_OK;
  213. }
  214. ////////////////////////////////////////////////////////////////////////
  215. // nsITreeView
  216. NS_IMETHODIMP
  217. inDOMView::GetRowCount(int32_t *aRowCount)
  218. {
  219. *aRowCount = GetRowCount();
  220. return NS_OK;
  221. }
  222. NS_IMETHODIMP
  223. inDOMView::GetRowProperties(int32_t index, nsAString& aProps)
  224. {
  225. return NS_OK;
  226. }
  227. NS_IMETHODIMP
  228. inDOMView::GetCellProperties(int32_t row, nsITreeColumn* col,
  229. nsAString& aProps)
  230. {
  231. inDOMViewNode* node = nullptr;
  232. RowToNode(row, &node);
  233. if (!node) return NS_ERROR_FAILURE;
  234. nsCOMPtr<nsIContent> content = do_QueryInterface(node->node);
  235. if (content && content->IsInAnonymousSubtree()) {
  236. aProps.AppendLiteral("anonymous ");
  237. }
  238. uint16_t nodeType;
  239. node->node->GetNodeType(&nodeType);
  240. switch (nodeType) {
  241. case nsIDOMNode::ELEMENT_NODE:
  242. aProps.AppendLiteral("ELEMENT_NODE");
  243. break;
  244. case nsIDOMNode::ATTRIBUTE_NODE:
  245. aProps.AppendLiteral("ATTRIBUTE_NODE");
  246. break;
  247. case nsIDOMNode::TEXT_NODE:
  248. aProps.AppendLiteral("TEXT_NODE");
  249. break;
  250. case nsIDOMNode::CDATA_SECTION_NODE:
  251. aProps.AppendLiteral("CDATA_SECTION_NODE");
  252. break;
  253. case nsIDOMNode::ENTITY_REFERENCE_NODE:
  254. aProps.AppendLiteral("ENTITY_REFERENCE_NODE");
  255. break;
  256. case nsIDOMNode::ENTITY_NODE:
  257. aProps.AppendLiteral("ENTITY_NODE");
  258. break;
  259. case nsIDOMNode::PROCESSING_INSTRUCTION_NODE:
  260. aProps.AppendLiteral("PROCESSING_INSTRUCTION_NODE");
  261. break;
  262. case nsIDOMNode::COMMENT_NODE:
  263. aProps.AppendLiteral("COMMENT_NODE");
  264. break;
  265. case nsIDOMNode::DOCUMENT_NODE:
  266. aProps.AppendLiteral("DOCUMENT_NODE");
  267. break;
  268. case nsIDOMNode::DOCUMENT_TYPE_NODE:
  269. aProps.AppendLiteral("DOCUMENT_TYPE_NODE");
  270. break;
  271. case nsIDOMNode::DOCUMENT_FRAGMENT_NODE:
  272. aProps.AppendLiteral("DOCUMENT_FRAGMENT_NODE");
  273. break;
  274. case nsIDOMNode::NOTATION_NODE:
  275. aProps.AppendLiteral("NOTATION_NODE");
  276. break;
  277. }
  278. #ifdef ACCESSIBILITY
  279. if (mShowAccessibleNodes) {
  280. nsAccessibilityService* accService = GetOrCreateAccService();
  281. NS_ENSURE_TRUE(accService, NS_ERROR_FAILURE);
  282. if (accService->HasAccessible(node->node))
  283. aProps.AppendLiteral(" ACCESSIBLE_NODE");
  284. }
  285. #endif
  286. return NS_OK;
  287. }
  288. NS_IMETHODIMP
  289. inDOMView::GetColumnProperties(nsITreeColumn* col, nsAString& aProps)
  290. {
  291. return NS_OK;
  292. }
  293. NS_IMETHODIMP
  294. inDOMView::GetImageSrc(int32_t row, nsITreeColumn* col, nsAString& _retval)
  295. {
  296. return NS_OK;
  297. }
  298. NS_IMETHODIMP
  299. inDOMView::GetProgressMode(int32_t row, nsITreeColumn* col, int32_t* _retval)
  300. {
  301. return NS_OK;
  302. }
  303. NS_IMETHODIMP
  304. inDOMView::GetCellValue(int32_t row, nsITreeColumn* col, nsAString& _retval)
  305. {
  306. return NS_OK;
  307. }
  308. NS_IMETHODIMP
  309. inDOMView::GetCellText(int32_t row, nsITreeColumn* col, nsAString& _retval)
  310. {
  311. inDOMViewNode* node = nullptr;
  312. RowToNode(row, &node);
  313. if (!node) return NS_ERROR_FAILURE;
  314. nsIDOMNode* domNode = node->node;
  315. nsAutoString colID;
  316. col->GetId(colID);
  317. if (colID.EqualsLiteral("colNodeName"))
  318. domNode->GetNodeName(_retval);
  319. else if (colID.EqualsLiteral("colLocalName"))
  320. domNode->GetLocalName(_retval);
  321. else if (colID.EqualsLiteral("colPrefix"))
  322. domNode->GetPrefix(_retval);
  323. else if (colID.EqualsLiteral("colNamespaceURI"))
  324. domNode->GetNamespaceURI(_retval);
  325. else if (colID.EqualsLiteral("colNodeType")) {
  326. uint16_t nodeType;
  327. domNode->GetNodeType(&nodeType);
  328. nsAutoString temp;
  329. temp.AppendInt(int32_t(nodeType));
  330. _retval = temp;
  331. } else if (colID.EqualsLiteral("colNodeValue"))
  332. domNode->GetNodeValue(_retval);
  333. else {
  334. if (StringBeginsWith(colID, NS_LITERAL_STRING("col@"))) {
  335. nsCOMPtr<nsIDOMElement> el = do_QueryInterface(node->node);
  336. if (el) {
  337. nsAutoString attr;
  338. colID.Right(attr, colID.Length()-4); // have to use this because Substring is crashing on me!
  339. el->GetAttribute(attr, _retval);
  340. }
  341. }
  342. }
  343. return NS_OK;
  344. }
  345. NS_IMETHODIMP
  346. inDOMView::IsContainer(int32_t index, bool *_retval)
  347. {
  348. inDOMViewNode* node = nullptr;
  349. RowToNode(index, &node);
  350. if (!node) return NS_ERROR_FAILURE;
  351. *_retval = node->isContainer;
  352. return NS_OK;
  353. }
  354. NS_IMETHODIMP
  355. inDOMView::IsContainerOpen(int32_t index, bool *_retval)
  356. {
  357. inDOMViewNode* node = nullptr;
  358. RowToNode(index, &node);
  359. if (!node) return NS_ERROR_FAILURE;
  360. *_retval = node->isOpen;
  361. return NS_OK;
  362. }
  363. NS_IMETHODIMP
  364. inDOMView::IsContainerEmpty(int32_t index, bool *_retval)
  365. {
  366. inDOMViewNode* node = nullptr;
  367. RowToNode(index, &node);
  368. if (!node) return NS_ERROR_FAILURE;
  369. *_retval = node->isContainer ? false : true;
  370. return NS_OK;
  371. }
  372. NS_IMETHODIMP
  373. inDOMView::GetLevel(int32_t index, int32_t *_retval)
  374. {
  375. inDOMViewNode* node = nullptr;
  376. RowToNode(index, &node);
  377. if (!node) return NS_ERROR_FAILURE;
  378. *_retval = node->level;
  379. return NS_OK;
  380. }
  381. NS_IMETHODIMP
  382. inDOMView::GetParentIndex(int32_t rowIndex, int32_t *_retval)
  383. {
  384. inDOMViewNode* node = nullptr;
  385. RowToNode(rowIndex, &node);
  386. if (!node) return NS_ERROR_FAILURE;
  387. // GetParentIndex returns -1 if there is no parent
  388. *_retval = -1;
  389. inDOMViewNode* checkNode = nullptr;
  390. int32_t i = rowIndex - 1;
  391. do {
  392. nsresult rv = RowToNode(i, &checkNode);
  393. if (NS_FAILED(rv)) {
  394. // No parent. Just break out.
  395. break;
  396. }
  397. if (checkNode == node->parent) {
  398. *_retval = i;
  399. return NS_OK;
  400. }
  401. --i;
  402. } while (checkNode);
  403. return NS_OK;
  404. }
  405. NS_IMETHODIMP
  406. inDOMView::HasNextSibling(int32_t rowIndex, int32_t afterIndex, bool *_retval)
  407. {
  408. inDOMViewNode* node = nullptr;
  409. RowToNode(rowIndex, &node);
  410. if (!node) return NS_ERROR_FAILURE;
  411. *_retval = node->next != nullptr;
  412. return NS_OK;
  413. }
  414. NS_IMETHODIMP
  415. inDOMView::ToggleOpenState(int32_t index)
  416. {
  417. inDOMViewNode* node = nullptr;
  418. RowToNode(index, &node);
  419. if (!node) return NS_ERROR_FAILURE;
  420. int32_t oldCount = GetRowCount();
  421. if (node->isOpen)
  422. CollapseNode(index);
  423. else
  424. ExpandNode(index);
  425. // Update the twisty.
  426. mTree->InvalidateRow(index);
  427. mTree->RowCountChanged(index+1, GetRowCount() - oldCount);
  428. return NS_OK;
  429. }
  430. NS_IMETHODIMP
  431. inDOMView::SetTree(nsITreeBoxObject *tree)
  432. {
  433. mTree = tree;
  434. return NS_OK;
  435. }
  436. NS_IMETHODIMP
  437. inDOMView::GetSelection(nsITreeSelection * *aSelection)
  438. {
  439. *aSelection = mSelection;
  440. NS_IF_ADDREF(*aSelection);
  441. return NS_OK;
  442. }
  443. NS_IMETHODIMP inDOMView::SetSelection(nsITreeSelection * aSelection)
  444. {
  445. mSelection = aSelection;
  446. return NS_OK;
  447. }
  448. NS_IMETHODIMP
  449. inDOMView::SelectionChanged()
  450. {
  451. return NS_OK;
  452. }
  453. NS_IMETHODIMP
  454. inDOMView::SetCellValue(int32_t row, nsITreeColumn* col, const nsAString& value)
  455. {
  456. return NS_OK;
  457. }
  458. NS_IMETHODIMP
  459. inDOMView::SetCellText(int32_t row, nsITreeColumn* col, const nsAString& value)
  460. {
  461. return NS_OK;
  462. }
  463. NS_IMETHODIMP
  464. inDOMView::CycleHeader(nsITreeColumn* col)
  465. {
  466. return NS_OK;
  467. }
  468. NS_IMETHODIMP
  469. inDOMView::CycleCell(int32_t row, nsITreeColumn* col)
  470. {
  471. return NS_OK;
  472. }
  473. NS_IMETHODIMP
  474. inDOMView::IsEditable(int32_t row, nsITreeColumn* col, bool *_retval)
  475. {
  476. return NS_OK;
  477. }
  478. NS_IMETHODIMP
  479. inDOMView::IsSelectable(int32_t row, nsITreeColumn* col, bool *_retval)
  480. {
  481. return NS_OK;
  482. }
  483. NS_IMETHODIMP
  484. inDOMView::IsSeparator(int32_t index, bool *_retval)
  485. {
  486. return NS_OK;
  487. }
  488. NS_IMETHODIMP
  489. inDOMView::IsSorted(bool *_retval)
  490. {
  491. return NS_OK;
  492. }
  493. NS_IMETHODIMP
  494. inDOMView::CanDrop(int32_t index, int32_t orientation,
  495. nsIDOMDataTransfer* aDataTransfer, bool *_retval)
  496. {
  497. *_retval = false;
  498. return NS_OK;
  499. }
  500. NS_IMETHODIMP
  501. inDOMView::Drop(int32_t row, int32_t orientation, nsIDOMDataTransfer* aDataTransfer)
  502. {
  503. return NS_OK;
  504. }
  505. NS_IMETHODIMP
  506. inDOMView::PerformAction(const char16_t *action)
  507. {
  508. return NS_OK;
  509. }
  510. NS_IMETHODIMP
  511. inDOMView::PerformActionOnRow(const char16_t *action, int32_t row)
  512. {
  513. return NS_OK;
  514. }
  515. NS_IMETHODIMP
  516. inDOMView::PerformActionOnCell(const char16_t* action, int32_t row, nsITreeColumn* col)
  517. {
  518. return NS_OK;
  519. }
  520. ///////////////////////////////////////////////////////////////////////
  521. // nsIMutationObserver
  522. void
  523. inDOMView::NodeWillBeDestroyed(const nsINode* aNode)
  524. {
  525. NS_NOTREACHED("Document destroyed while we're holding a strong ref to it");
  526. }
  527. void
  528. inDOMView::AttributeChanged(nsIDocument* aDocument, dom::Element* aElement,
  529. int32_t aNameSpaceID, nsIAtom* aAttribute,
  530. int32_t aModType,
  531. const nsAttrValue* aOldValue)
  532. {
  533. if (!mTree) {
  534. return;
  535. }
  536. if (!(mWhatToShow & nsIDOMNodeFilter::SHOW_ATTRIBUTE)) {
  537. return;
  538. }
  539. nsCOMPtr<nsIMutationObserver> kungFuDeathGrip(this);
  540. // get the dom attribute node, if there is any
  541. nsCOMPtr<nsIDOMElement> el(do_QueryInterface(aElement));
  542. nsCOMPtr<nsIDOMAttr> domAttr;
  543. nsDependentAtomString attrStr(aAttribute);
  544. if (aNameSpaceID) {
  545. nsNameSpaceManager* nsm = nsNameSpaceManager::GetInstance();
  546. if (!nsm) {
  547. // we can't find out which attribute we want :(
  548. return;
  549. }
  550. nsString attrNS;
  551. nsresult rv = nsm->GetNameSpaceURI(aNameSpaceID, attrNS);
  552. if (NS_FAILED(rv)) {
  553. return;
  554. }
  555. (void)el->GetAttributeNodeNS(attrNS, attrStr, getter_AddRefs(domAttr));
  556. } else {
  557. (void)el->GetAttributeNode(attrStr, getter_AddRefs(domAttr));
  558. }
  559. if (aModType == nsIDOMMutationEvent::MODIFICATION) {
  560. // No fancy stuff here, just invalidate the changed row
  561. if (!domAttr) {
  562. return;
  563. }
  564. int32_t row = 0;
  565. NodeToRow(domAttr, &row);
  566. mTree->InvalidateRange(row, row);
  567. } else if (aModType == nsIDOMMutationEvent::ADDITION) {
  568. if (!domAttr) {
  569. return;
  570. }
  571. // get the number of attributes on this content node
  572. nsCOMPtr<nsIDOMMozNamedAttrMap> attrs;
  573. el->GetAttributes(getter_AddRefs(attrs));
  574. uint32_t attrCount;
  575. attrs->GetLength(&attrCount);
  576. inDOMViewNode* contentNode = nullptr;
  577. int32_t contentRow;
  578. int32_t attrRow;
  579. if (mRootNode == el &&
  580. !(mWhatToShow & nsIDOMNodeFilter::SHOW_ELEMENT)) {
  581. // if this view has a root node but is not displaying it,
  582. // it is ok to act as if the changed attribute is on the root.
  583. attrRow = attrCount - 1;
  584. } else {
  585. if (NS_FAILED(NodeToRow(el, &contentRow))) {
  586. return;
  587. }
  588. RowToNode(contentRow, &contentNode);
  589. if (!contentNode->isOpen) {
  590. return;
  591. }
  592. attrRow = contentRow + attrCount;
  593. }
  594. inDOMViewNode* newNode = CreateNode(domAttr, contentNode);
  595. inDOMViewNode* insertNode = nullptr;
  596. RowToNode(attrRow, &insertNode);
  597. if (insertNode) {
  598. if (contentNode &&
  599. insertNode->level <= contentNode->level) {
  600. RowToNode(attrRow-1, &insertNode);
  601. InsertLinkAfter(newNode, insertNode);
  602. } else
  603. InsertLinkBefore(newNode, insertNode);
  604. }
  605. InsertNode(newNode, attrRow);
  606. mTree->RowCountChanged(attrRow, 1);
  607. } else if (aModType == nsIDOMMutationEvent::REMOVAL) {
  608. // At this point, the attribute is already gone from the DOM, but is still represented
  609. // in our mRows array. Search through the content node's children for the corresponding
  610. // node and remove it.
  611. // get the row of the content node
  612. inDOMViewNode* contentNode = nullptr;
  613. int32_t contentRow;
  614. int32_t baseLevel;
  615. if (NS_SUCCEEDED(NodeToRow(el, &contentRow))) {
  616. RowToNode(contentRow, &contentNode);
  617. baseLevel = contentNode->level;
  618. } else {
  619. if (mRootNode == el) {
  620. contentRow = -1;
  621. baseLevel = -1;
  622. } else
  623. return;
  624. }
  625. // search for the attribute node that was removed
  626. inDOMViewNode* checkNode = nullptr;
  627. int32_t row = 0;
  628. for (row = contentRow+1; row < GetRowCount(); ++row) {
  629. checkNode = GetNodeAt(row);
  630. if (checkNode->level == baseLevel+1) {
  631. domAttr = do_QueryInterface(checkNode->node);
  632. if (domAttr) {
  633. nsAutoString attrName;
  634. domAttr->GetNodeName(attrName);
  635. if (attrName.Equals(attrStr)) {
  636. // we have found the row for the attribute that was removed
  637. RemoveLink(checkNode);
  638. RemoveNode(row);
  639. mTree->RowCountChanged(row, -1);
  640. break;
  641. }
  642. }
  643. }
  644. if (checkNode->level <= baseLevel)
  645. break;
  646. }
  647. }
  648. }
  649. void
  650. inDOMView::ContentAppended(nsIDocument *aDocument,
  651. nsIContent* aContainer,
  652. nsIContent* aFirstNewContent,
  653. int32_t /* unused */)
  654. {
  655. if (!mTree) {
  656. return;
  657. }
  658. for (nsIContent* cur = aFirstNewContent; cur; cur = cur->GetNextSibling()) {
  659. // Our ContentInserted impl doesn't use the index
  660. ContentInserted(aDocument, aContainer, cur, 0);
  661. }
  662. }
  663. void
  664. inDOMView::ContentInserted(nsIDocument *aDocument, nsIContent* aContainer,
  665. nsIContent* aChild, int32_t /* unused */)
  666. {
  667. if (!mTree)
  668. return;
  669. nsresult rv;
  670. nsCOMPtr<nsIDOMNode> childDOMNode(do_QueryInterface(aChild));
  671. nsCOMPtr<nsIDOMNode> parent;
  672. if (!mDOMUtils) {
  673. mDOMUtils = services::GetInDOMUtils();
  674. if (!mDOMUtils) {
  675. return;
  676. }
  677. }
  678. mDOMUtils->GetParentForNode(childDOMNode, mShowAnonymous,
  679. getter_AddRefs(parent));
  680. // find the inDOMViewNode for the parent of the inserted content
  681. int32_t parentRow = 0;
  682. if (NS_FAILED(rv = NodeToRow(parent, &parentRow)))
  683. return;
  684. inDOMViewNode* parentNode = nullptr;
  685. if (NS_FAILED(rv = RowToNode(parentRow, &parentNode)))
  686. return;
  687. nsCOMPtr<nsIMutationObserver> kungFuDeathGrip(this);
  688. if (!parentNode->isOpen) {
  689. // Parent is not open, so don't bother creating tree rows for the
  690. // kids. But do indicate that it's now a container, if needed.
  691. if (!parentNode->isContainer) {
  692. parentNode->isContainer = true;
  693. mTree->InvalidateRow(parentRow);
  694. }
  695. return;
  696. }
  697. // get the previous sibling of the inserted content
  698. nsCOMPtr<nsIDOMNode> previous;
  699. GetRealPreviousSibling(childDOMNode, parent, getter_AddRefs(previous));
  700. inDOMViewNode* previousNode = nullptr;
  701. int32_t row = 0;
  702. if (previous) {
  703. // find the inDOMViewNode for the previous sibling of the inserted content
  704. int32_t previousRow = 0;
  705. if (NS_FAILED(rv = NodeToRow(previous, &previousRow)))
  706. return;
  707. if (NS_FAILED(rv = RowToNode(previousRow, &previousNode)))
  708. return;
  709. // get the last descendant of the previous row, which is the row
  710. // after which to insert this new row
  711. GetLastDescendantOf(previousNode, previousRow, &row);
  712. ++row;
  713. } else {
  714. // there is no previous sibling, so the new row will be inserted after the parent
  715. row = parentRow+1;
  716. }
  717. inDOMViewNode* newNode = CreateNode(childDOMNode, parentNode);
  718. if (previous) {
  719. InsertLinkAfter(newNode, previousNode);
  720. } else {
  721. int32_t firstChildRow;
  722. if (NS_SUCCEEDED(GetFirstDescendantOf(parentNode, parentRow, &firstChildRow))) {
  723. inDOMViewNode* firstChild;
  724. RowToNode(firstChildRow, &firstChild);
  725. InsertLinkBefore(newNode, firstChild);
  726. }
  727. }
  728. // insert new node
  729. InsertNode(newNode, row);
  730. mTree->RowCountChanged(row, 1);
  731. }
  732. void
  733. inDOMView::ContentRemoved(nsIDocument *aDocument, nsIContent* aContainer,
  734. nsIContent* aChild, int32_t aIndexInContainer,
  735. nsIContent* aPreviousSibling)
  736. {
  737. if (!mTree)
  738. return;
  739. nsresult rv;
  740. // find the inDOMViewNode for the old child
  741. nsCOMPtr<nsIDOMNode> oldDOMNode(do_QueryInterface(aChild));
  742. int32_t row = 0;
  743. if (NS_FAILED(rv = NodeToRow(oldDOMNode, &row)))
  744. return;
  745. inDOMViewNode* oldNode;
  746. if (NS_FAILED(rv = RowToNode(row, &oldNode)))
  747. return;
  748. nsCOMPtr<nsIMutationObserver> kungFuDeathGrip(this);
  749. // The parent may no longer be a container. Note that we don't want
  750. // to access oldNode after calling RemoveNode, so do this now.
  751. inDOMViewNode* parentNode = oldNode->parent;
  752. bool isOnlyChild = oldNode->previous == nullptr && oldNode->next == nullptr;
  753. // Keep track of how many rows we are removing. It's at least one,
  754. // but if we're open it's more.
  755. int32_t oldCount = GetRowCount();
  756. if (oldNode->isOpen)
  757. CollapseNode(row);
  758. RemoveLink(oldNode);
  759. RemoveNode(row);
  760. if (isOnlyChild) {
  761. // Fix up the parent
  762. parentNode->isContainer = false;
  763. parentNode->isOpen = false;
  764. mTree->InvalidateRow(NodeToRow(parentNode));
  765. }
  766. mTree->RowCountChanged(row, GetRowCount() - oldCount);
  767. }
  768. ///////////////////////////////////////////////////////////////////////
  769. // inDOMView
  770. //////// NODE MANAGEMENT
  771. inDOMViewNode*
  772. inDOMView::GetNodeAt(int32_t aRow)
  773. {
  774. return mNodes.ElementAt(aRow);
  775. }
  776. int32_t
  777. inDOMView::GetRowCount()
  778. {
  779. return mNodes.Length();
  780. }
  781. int32_t
  782. inDOMView::NodeToRow(inDOMViewNode* aNode)
  783. {
  784. return mNodes.IndexOf(aNode);
  785. }
  786. inDOMViewNode*
  787. inDOMView::CreateNode(nsIDOMNode* aNode, inDOMViewNode* aParent)
  788. {
  789. inDOMViewNode* viewNode = new inDOMViewNode(aNode);
  790. viewNode->level = aParent ? aParent->level+1 : 0;
  791. viewNode->parent = aParent;
  792. nsCOMArray<nsIDOMNode> grandKids;
  793. GetChildNodesFor(aNode, grandKids);
  794. viewNode->isContainer = (grandKids.Count() > 0);
  795. return viewNode;
  796. }
  797. bool
  798. inDOMView::RowOutOfBounds(int32_t aRow, int32_t aCount)
  799. {
  800. return aRow < 0 || aRow >= GetRowCount() || aCount+aRow > GetRowCount();
  801. }
  802. void
  803. inDOMView::AppendNode(inDOMViewNode* aNode)
  804. {
  805. mNodes.AppendElement(aNode);
  806. }
  807. void
  808. inDOMView::InsertNode(inDOMViewNode* aNode, int32_t aRow)
  809. {
  810. if (RowOutOfBounds(aRow, 1))
  811. AppendNode(aNode);
  812. else
  813. mNodes.InsertElementAt(aRow, aNode);
  814. }
  815. void
  816. inDOMView::RemoveNode(int32_t aRow)
  817. {
  818. if (RowOutOfBounds(aRow, 1))
  819. return;
  820. delete GetNodeAt(aRow);
  821. mNodes.RemoveElementAt(aRow);
  822. }
  823. void
  824. inDOMView::ReplaceNode(inDOMViewNode* aNode, int32_t aRow)
  825. {
  826. if (RowOutOfBounds(aRow, 1))
  827. return;
  828. delete GetNodeAt(aRow);
  829. mNodes.ElementAt(aRow) = aNode;
  830. }
  831. void
  832. inDOMView::InsertNodes(nsTArray<inDOMViewNode*>& aNodes, int32_t aRow)
  833. {
  834. if (aRow < 0 || aRow > GetRowCount())
  835. return;
  836. mNodes.InsertElementsAt(aRow, aNodes);
  837. }
  838. void
  839. inDOMView::RemoveNodes(int32_t aRow, int32_t aCount)
  840. {
  841. if (aRow < 0)
  842. return;
  843. int32_t rowCount = GetRowCount();
  844. for (int32_t i = aRow; i < aRow+aCount && i < rowCount; ++i) {
  845. delete GetNodeAt(i);
  846. }
  847. mNodes.RemoveElementsAt(aRow, aCount);
  848. }
  849. void
  850. inDOMView::RemoveAllNodes()
  851. {
  852. int32_t rowCount = GetRowCount();
  853. for (int32_t i = 0; i < rowCount; ++i) {
  854. delete GetNodeAt(i);
  855. }
  856. mNodes.Clear();
  857. }
  858. void
  859. inDOMView::ExpandNode(int32_t aRow)
  860. {
  861. inDOMViewNode* node = nullptr;
  862. RowToNode(aRow, &node);
  863. nsCOMArray<nsIDOMNode> kids;
  864. GetChildNodesFor(node ? node->node : mRootNode,
  865. kids);
  866. int32_t kidCount = kids.Count();
  867. nsTArray<inDOMViewNode*> list(kidCount);
  868. inDOMViewNode* newNode = nullptr;
  869. inDOMViewNode* prevNode = nullptr;
  870. for (int32_t i = 0; i < kidCount; ++i) {
  871. newNode = CreateNode(kids[i], node);
  872. list.AppendElement(newNode);
  873. if (prevNode)
  874. prevNode->next = newNode;
  875. newNode->previous = prevNode;
  876. prevNode = newNode;
  877. }
  878. InsertNodes(list, aRow+1);
  879. if (node)
  880. node->isOpen = true;
  881. }
  882. void
  883. inDOMView::CollapseNode(int32_t aRow)
  884. {
  885. inDOMViewNode* node = nullptr;
  886. nsresult rv = RowToNode(aRow, &node);
  887. if (NS_FAILED(rv)) {
  888. return;
  889. }
  890. int32_t row = 0;
  891. GetLastDescendantOf(node, aRow, &row);
  892. RemoveNodes(aRow+1, row-aRow);
  893. node->isOpen = false;
  894. }
  895. //////// NODE AND ROW CONVERSION
  896. nsresult
  897. inDOMView::RowToNode(int32_t aRow, inDOMViewNode** aNode)
  898. {
  899. if (aRow < 0 || aRow >= GetRowCount())
  900. return NS_ERROR_FAILURE;
  901. *aNode = GetNodeAt(aRow);
  902. return NS_OK;
  903. }
  904. nsresult
  905. inDOMView::NodeToRow(nsIDOMNode* aNode, int32_t* aRow)
  906. {
  907. int32_t rowCount = GetRowCount();
  908. for (int32_t i = 0; i < rowCount; ++i) {
  909. if (GetNodeAt(i)->node == aNode) {
  910. *aRow = i;
  911. return NS_OK;
  912. }
  913. }
  914. *aRow = -1;
  915. return NS_ERROR_FAILURE;
  916. }
  917. //////// NODE HIERARCHY MUTATION
  918. void
  919. inDOMView::InsertLinkAfter(inDOMViewNode* aNode, inDOMViewNode* aInsertAfter)
  920. {
  921. if (aInsertAfter->next)
  922. aInsertAfter->next->previous = aNode;
  923. aNode->next = aInsertAfter->next;
  924. aInsertAfter->next = aNode;
  925. aNode->previous = aInsertAfter;
  926. }
  927. void
  928. inDOMView::InsertLinkBefore(inDOMViewNode* aNode, inDOMViewNode* aInsertBefore)
  929. {
  930. if (aInsertBefore->previous)
  931. aInsertBefore->previous->next = aNode;
  932. aNode->previous = aInsertBefore->previous;
  933. aInsertBefore->previous = aNode;
  934. aNode->next = aInsertBefore;
  935. }
  936. void
  937. inDOMView::RemoveLink(inDOMViewNode* aNode)
  938. {
  939. if (aNode->previous)
  940. aNode->previous->next = aNode->next;
  941. if (aNode->next)
  942. aNode->next->previous = aNode->previous;
  943. }
  944. void
  945. inDOMView::ReplaceLink(inDOMViewNode* aNewNode, inDOMViewNode* aOldNode)
  946. {
  947. if (aOldNode->previous)
  948. aOldNode->previous->next = aNewNode;
  949. if (aOldNode->next)
  950. aOldNode->next->previous = aNewNode;
  951. aNewNode->next = aOldNode->next;
  952. aNewNode->previous = aOldNode->previous;
  953. }
  954. //////// NODE HIERARCHY UTILITIES
  955. nsresult
  956. inDOMView::GetFirstDescendantOf(inDOMViewNode* aNode, int32_t aRow, int32_t* aResult)
  957. {
  958. // get the first node that is a descendant of the previous sibling
  959. int32_t row = 0;
  960. inDOMViewNode* node;
  961. for (row = aRow+1; row < GetRowCount(); ++row) {
  962. node = GetNodeAt(row);
  963. if (node->parent == aNode) {
  964. *aResult = row;
  965. return NS_OK;
  966. }
  967. if (node->level <= aNode->level)
  968. break;
  969. }
  970. return NS_ERROR_FAILURE;
  971. }
  972. nsresult
  973. inDOMView::GetLastDescendantOf(inDOMViewNode* aNode, int32_t aRow, int32_t* aResult)
  974. {
  975. // get the last node that is a descendant of the previous sibling
  976. int32_t row = 0;
  977. for (row = aRow+1; row < GetRowCount(); ++row) {
  978. if (GetNodeAt(row)->level <= aNode->level)
  979. break;
  980. }
  981. *aResult = row-1;
  982. return NS_OK;
  983. }
  984. //////// DOM UTILITIES
  985. nsresult
  986. inDOMView::GetChildNodesFor(nsIDOMNode* aNode, nsCOMArray<nsIDOMNode>& aResult)
  987. {
  988. NS_ENSURE_ARG(aNode);
  989. // attribute nodes
  990. if (mWhatToShow & nsIDOMNodeFilter::SHOW_ATTRIBUTE) {
  991. nsCOMPtr<nsIDOMElement> element = do_QueryInterface(aNode);
  992. if (element) {
  993. nsCOMPtr<nsIDOMMozNamedAttrMap> attrs;
  994. element->GetAttributes(getter_AddRefs(attrs));
  995. if (attrs) {
  996. AppendAttrsToArray(attrs, aResult);
  997. }
  998. }
  999. }
  1000. if (mWhatToShow & nsIDOMNodeFilter::SHOW_ELEMENT) {
  1001. nsCOMPtr<nsIDOMNodeList> kids;
  1002. if (!mDOMUtils) {
  1003. mDOMUtils = services::GetInDOMUtils();
  1004. if (!mDOMUtils) {
  1005. return NS_ERROR_FAILURE;
  1006. }
  1007. }
  1008. mDOMUtils->GetChildrenForNode(aNode, mShowAnonymous,
  1009. getter_AddRefs(kids));
  1010. if (kids) {
  1011. AppendKidsToArray(kids, aResult);
  1012. }
  1013. }
  1014. if (mShowSubDocuments) {
  1015. nsCOMPtr<nsIDOMNode> domdoc =
  1016. do_QueryInterface(inLayoutUtils::GetSubDocumentFor(aNode));
  1017. if (domdoc) {
  1018. aResult.AppendObject(domdoc);
  1019. }
  1020. }
  1021. return NS_OK;
  1022. }
  1023. nsresult
  1024. inDOMView::GetRealPreviousSibling(nsIDOMNode* aNode, nsIDOMNode* aRealParent, nsIDOMNode** aSibling)
  1025. {
  1026. // XXXjrh: This won't work for some cases during some situations where XBL insertion points
  1027. // are involved. Fix me!
  1028. aNode->GetPreviousSibling(aSibling);
  1029. return NS_OK;
  1030. }
  1031. nsresult
  1032. inDOMView::AppendKidsToArray(nsIDOMNodeList* aKids,
  1033. nsCOMArray<nsIDOMNode>& aArray)
  1034. {
  1035. uint32_t l = 0;
  1036. aKids->GetLength(&l);
  1037. nsCOMPtr<nsIDOMNode> kid;
  1038. uint16_t nodeType = 0;
  1039. // Try and get DOM Utils in case we don't have one yet.
  1040. if (!mShowWhitespaceNodes && !mDOMUtils) {
  1041. mDOMUtils = services::GetInDOMUtils();
  1042. }
  1043. for (uint32_t i = 0; i < l; ++i) {
  1044. aKids->Item(i, getter_AddRefs(kid));
  1045. kid->GetNodeType(&nodeType);
  1046. NS_ASSERTION(nodeType && nodeType <= nsIDOMNode::NOTATION_NODE,
  1047. "Unknown node type. "
  1048. "Were new types added to the spec?");
  1049. // As of DOM Level 2 Core and Traversal, each NodeFilter constant
  1050. // is defined as the lower nth bit in the NodeFilter bitmask,
  1051. // where n is the numeric constant of the nodeType it represents.
  1052. // If this invariant ever changes, we will need to update the
  1053. // following line.
  1054. uint32_t filterForNodeType = 1 << (nodeType - 1);
  1055. if (mWhatToShow & filterForNodeType) {
  1056. if ((nodeType == nsIDOMNode::TEXT_NODE ||
  1057. nodeType == nsIDOMNode::COMMENT_NODE) &&
  1058. !mShowWhitespaceNodes && mDOMUtils) {
  1059. nsCOMPtr<nsIDOMCharacterData> data = do_QueryInterface(kid);
  1060. NS_ASSERTION(data, "Does not implement nsIDOMCharacterData!");
  1061. bool ignore;
  1062. mDOMUtils->IsIgnorableWhitespace(data, &ignore);
  1063. if (ignore) {
  1064. continue;
  1065. }
  1066. }
  1067. aArray.AppendElement(kid.forget());
  1068. }
  1069. }
  1070. return NS_OK;
  1071. }
  1072. nsresult
  1073. inDOMView::AppendAttrsToArray(nsIDOMMozNamedAttrMap* aAttributes,
  1074. nsCOMArray<nsIDOMNode>& aArray)
  1075. {
  1076. uint32_t l = 0;
  1077. aAttributes->GetLength(&l);
  1078. nsCOMPtr<nsIDOMAttr> attribute;
  1079. for (uint32_t i = 0; i < l; ++i) {
  1080. aAttributes->Item(i, getter_AddRefs(attribute));
  1081. aArray.AppendElement(attribute.forget());
  1082. }
  1083. return NS_OK;
  1084. }