txMozillaXPathTreeWalker.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  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 "txXPathTreeWalker.h"
  6. #include "nsIAtom.h"
  7. #include "nsIAttribute.h"
  8. #include "nsIDOMAttr.h"
  9. #include "nsIDOMDocument.h"
  10. #include "nsIDOMNode.h"
  11. #include "nsIDOMElement.h"
  12. #include "nsIDOMProcessingInstruction.h"
  13. #include "nsPrintfCString.h"
  14. #include "nsReadableUtils.h"
  15. #include "nsString.h"
  16. #include "nsTextFragment.h"
  17. #include "txXMLUtils.h"
  18. #include "txLog.h"
  19. #include "nsUnicharUtils.h"
  20. #include "nsAttrName.h"
  21. #include "nsTArray.h"
  22. #include "mozilla/dom/Attr.h"
  23. #include "mozilla/dom/Element.h"
  24. #include <stdint.h>
  25. #include <algorithm>
  26. using namespace mozilla::dom;
  27. const uint32_t kUnknownIndex = uint32_t(-1);
  28. txXPathTreeWalker::txXPathTreeWalker(const txXPathTreeWalker& aOther)
  29. : mPosition(aOther.mPosition),
  30. mCurrentIndex(aOther.mCurrentIndex)
  31. {
  32. }
  33. txXPathTreeWalker::txXPathTreeWalker(const txXPathNode& aNode)
  34. : mPosition(aNode),
  35. mCurrentIndex(kUnknownIndex)
  36. {
  37. }
  38. void
  39. txXPathTreeWalker::moveToRoot()
  40. {
  41. if (mPosition.isDocument()) {
  42. return;
  43. }
  44. nsIDocument* root = mPosition.mNode->GetUncomposedDoc();
  45. if (root) {
  46. mPosition.mIndex = txXPathNode::eDocument;
  47. mPosition.mNode = root;
  48. }
  49. else {
  50. nsINode *rootNode = mPosition.Root();
  51. NS_ASSERTION(rootNode->IsNodeOfType(nsINode::eCONTENT),
  52. "root of subtree wasn't an nsIContent");
  53. mPosition.mIndex = txXPathNode::eContent;
  54. mPosition.mNode = rootNode;
  55. }
  56. mCurrentIndex = kUnknownIndex;
  57. mDescendants.Clear();
  58. }
  59. bool
  60. txXPathTreeWalker::moveToElementById(const nsAString& aID)
  61. {
  62. if (aID.IsEmpty()) {
  63. return false;
  64. }
  65. nsIDocument* doc = mPosition.mNode->GetUncomposedDoc();
  66. nsCOMPtr<nsIContent> content;
  67. if (doc) {
  68. content = doc->GetElementById(aID);
  69. }
  70. else {
  71. // We're in a disconnected subtree, search only that subtree.
  72. nsINode *rootNode = mPosition.Root();
  73. NS_ASSERTION(rootNode->IsNodeOfType(nsINode::eCONTENT),
  74. "root of subtree wasn't an nsIContent");
  75. content = nsContentUtils::MatchElementId(
  76. static_cast<nsIContent*>(rootNode), aID);
  77. }
  78. if (!content) {
  79. return false;
  80. }
  81. mPosition.mIndex = txXPathNode::eContent;
  82. mPosition.mNode = content;
  83. mCurrentIndex = kUnknownIndex;
  84. mDescendants.Clear();
  85. return true;
  86. }
  87. bool
  88. txXPathTreeWalker::moveToFirstAttribute()
  89. {
  90. if (!mPosition.isContent()) {
  91. return false;
  92. }
  93. return moveToValidAttribute(0);
  94. }
  95. bool
  96. txXPathTreeWalker::moveToNextAttribute()
  97. {
  98. // XXX an assertion should be enough here with the current code
  99. if (!mPosition.isAttribute()) {
  100. return false;
  101. }
  102. return moveToValidAttribute(mPosition.mIndex + 1);
  103. }
  104. bool
  105. txXPathTreeWalker::moveToValidAttribute(uint32_t aStartIndex)
  106. {
  107. NS_ASSERTION(!mPosition.isDocument(), "documents doesn't have attrs");
  108. uint32_t total = mPosition.Content()->GetAttrCount();
  109. if (aStartIndex >= total) {
  110. return false;
  111. }
  112. uint32_t index;
  113. for (index = aStartIndex; index < total; ++index) {
  114. const nsAttrName* name = mPosition.Content()->GetAttrNameAt(index);
  115. // We need to ignore XMLNS attributes.
  116. if (name->NamespaceID() != kNameSpaceID_XMLNS) {
  117. mPosition.mIndex = index;
  118. return true;
  119. }
  120. }
  121. return false;
  122. }
  123. bool
  124. txXPathTreeWalker::moveToNamedAttribute(nsIAtom* aLocalName, int32_t aNSID)
  125. {
  126. if (!mPosition.isContent()) {
  127. return false;
  128. }
  129. const nsAttrName* name;
  130. uint32_t i;
  131. for (i = 0; (name = mPosition.Content()->GetAttrNameAt(i)); ++i) {
  132. if (name->Equals(aLocalName, aNSID)) {
  133. mPosition.mIndex = i;
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. bool
  140. txXPathTreeWalker::moveToFirstChild()
  141. {
  142. if (mPosition.isAttribute()) {
  143. return false;
  144. }
  145. NS_ASSERTION(!mPosition.isDocument() ||
  146. (mCurrentIndex == kUnknownIndex && mDescendants.IsEmpty()),
  147. "we shouldn't have any position info at the document");
  148. NS_ASSERTION(mCurrentIndex != kUnknownIndex || mDescendants.IsEmpty(),
  149. "Index should be known if parents index are");
  150. nsIContent* child = mPosition.mNode->GetFirstChild();
  151. if (!child) {
  152. return false;
  153. }
  154. mPosition.mIndex = txXPathNode::eContent;
  155. mPosition.mNode = child;
  156. if (mCurrentIndex != kUnknownIndex &&
  157. !mDescendants.AppendValue(mCurrentIndex)) {
  158. mDescendants.Clear();
  159. }
  160. mCurrentIndex = 0;
  161. return true;
  162. }
  163. bool
  164. txXPathTreeWalker::moveToLastChild()
  165. {
  166. if (mPosition.isAttribute()) {
  167. return false;
  168. }
  169. NS_ASSERTION(!mPosition.isDocument() ||
  170. (mCurrentIndex == kUnknownIndex && mDescendants.IsEmpty()),
  171. "we shouldn't have any position info at the document");
  172. NS_ASSERTION(mCurrentIndex != kUnknownIndex || mDescendants.IsEmpty(),
  173. "Index should be known if parents index are");
  174. uint32_t total = mPosition.mNode->GetChildCount();
  175. if (!total) {
  176. return false;
  177. }
  178. mPosition.mNode = mPosition.mNode->GetLastChild();
  179. if (mCurrentIndex != kUnknownIndex &&
  180. !mDescendants.AppendValue(mCurrentIndex)) {
  181. mDescendants.Clear();
  182. }
  183. mCurrentIndex = total - 1;
  184. return true;
  185. }
  186. bool
  187. txXPathTreeWalker::moveToNextSibling()
  188. {
  189. if (!mPosition.isContent()) {
  190. return false;
  191. }
  192. return moveToSibling(1);
  193. }
  194. bool
  195. txXPathTreeWalker::moveToPreviousSibling()
  196. {
  197. if (!mPosition.isContent()) {
  198. return false;
  199. }
  200. return moveToSibling(-1);
  201. }
  202. bool
  203. txXPathTreeWalker::moveToParent()
  204. {
  205. if (mPosition.isDocument()) {
  206. return false;
  207. }
  208. if (mPosition.isAttribute()) {
  209. mPosition.mIndex = txXPathNode::eContent;
  210. return true;
  211. }
  212. nsINode* parent = mPosition.mNode->GetParentNode();
  213. if (!parent) {
  214. return false;
  215. }
  216. uint32_t count = mDescendants.Length();
  217. if (count) {
  218. mCurrentIndex = mDescendants.ValueAt(--count);
  219. mDescendants.RemoveValueAt(count);
  220. }
  221. else {
  222. mCurrentIndex = kUnknownIndex;
  223. }
  224. mPosition.mIndex = mPosition.mNode->GetParent() ?
  225. txXPathNode::eContent : txXPathNode::eDocument;
  226. mPosition.mNode = parent;
  227. return true;
  228. }
  229. bool
  230. txXPathTreeWalker::moveToSibling(int32_t aDir)
  231. {
  232. NS_ASSERTION(mPosition.isContent(),
  233. "moveToSibling should only be called for content");
  234. nsINode* parent = mPosition.mNode->GetParentNode();
  235. if (!parent) {
  236. return false;
  237. }
  238. if (mCurrentIndex == kUnknownIndex) {
  239. mCurrentIndex = parent->IndexOf(mPosition.mNode);
  240. }
  241. // if mCurrentIndex is 0 we rely on GetChildAt returning null for an
  242. // index of uint32_t(-1).
  243. uint32_t newIndex = mCurrentIndex + aDir;
  244. nsIContent* newChild = parent->GetChildAt(newIndex);
  245. if (!newChild) {
  246. return false;
  247. }
  248. mPosition.mNode = newChild;
  249. mCurrentIndex = newIndex;
  250. return true;
  251. }
  252. txXPathNode::txXPathNode(const txXPathNode& aNode)
  253. : mNode(aNode.mNode),
  254. mRefCountRoot(aNode.mRefCountRoot),
  255. mIndex(aNode.mIndex)
  256. {
  257. MOZ_COUNT_CTOR(txXPathNode);
  258. if (mRefCountRoot) {
  259. NS_ADDREF(Root());
  260. }
  261. }
  262. txXPathNode::~txXPathNode()
  263. {
  264. MOZ_COUNT_DTOR(txXPathNode);
  265. if (mRefCountRoot) {
  266. nsINode *root = Root();
  267. NS_RELEASE(root);
  268. }
  269. }
  270. /* static */
  271. bool
  272. txXPathNodeUtils::getAttr(const txXPathNode& aNode, nsIAtom* aLocalName,
  273. int32_t aNSID, nsAString& aValue)
  274. {
  275. if (aNode.isDocument() || aNode.isAttribute()) {
  276. return false;
  277. }
  278. return aNode.Content()->GetAttr(aNSID, aLocalName, aValue);
  279. }
  280. /* static */
  281. already_AddRefed<nsIAtom>
  282. txXPathNodeUtils::getLocalName(const txXPathNode& aNode)
  283. {
  284. if (aNode.isDocument()) {
  285. return nullptr;
  286. }
  287. if (aNode.isContent()) {
  288. if (aNode.mNode->IsElement()) {
  289. nsCOMPtr<nsIAtom> localName =
  290. aNode.Content()->NodeInfo()->NameAtom();
  291. return localName.forget();
  292. }
  293. if (aNode.mNode->IsNodeOfType(nsINode::ePROCESSING_INSTRUCTION)) {
  294. nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aNode.mNode);
  295. nsAutoString target;
  296. node->GetNodeName(target);
  297. return NS_Atomize(target);
  298. }
  299. return nullptr;
  300. }
  301. nsCOMPtr<nsIAtom> localName = aNode.Content()->
  302. GetAttrNameAt(aNode.mIndex)->LocalName();
  303. return localName.forget();
  304. }
  305. nsIAtom*
  306. txXPathNodeUtils::getPrefix(const txXPathNode& aNode)
  307. {
  308. if (aNode.isDocument()) {
  309. return nullptr;
  310. }
  311. if (aNode.isContent()) {
  312. // All other nsIContent node types but elements have a null prefix
  313. // which is what we want here.
  314. return aNode.Content()->NodeInfo()->GetPrefixAtom();
  315. }
  316. return aNode.Content()->GetAttrNameAt(aNode.mIndex)->GetPrefix();
  317. }
  318. /* static */
  319. void
  320. txXPathNodeUtils::getLocalName(const txXPathNode& aNode, nsAString& aLocalName)
  321. {
  322. if (aNode.isDocument()) {
  323. aLocalName.Truncate();
  324. return;
  325. }
  326. if (aNode.isContent()) {
  327. if (aNode.mNode->IsElement()) {
  328. mozilla::dom::NodeInfo* nodeInfo = aNode.Content()->NodeInfo();
  329. nodeInfo->GetName(aLocalName);
  330. return;
  331. }
  332. if (aNode.mNode->IsNodeOfType(nsINode::ePROCESSING_INSTRUCTION)) {
  333. // PIs don't have a nodeinfo but do have a name
  334. nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aNode.mNode);
  335. node->GetNodeName(aLocalName);
  336. return;
  337. }
  338. aLocalName.Truncate();
  339. return;
  340. }
  341. aNode.Content()->GetAttrNameAt(aNode.mIndex)->LocalName()->
  342. ToString(aLocalName);
  343. // Check for html
  344. if (aNode.Content()->NodeInfo()->NamespaceEquals(kNameSpaceID_None) &&
  345. aNode.Content()->IsHTMLElement()) {
  346. nsContentUtils::ASCIIToUpper(aLocalName);
  347. }
  348. }
  349. /* static */
  350. void
  351. txXPathNodeUtils::getNodeName(const txXPathNode& aNode, nsAString& aName)
  352. {
  353. if (aNode.isDocument()) {
  354. aName.Truncate();
  355. return;
  356. }
  357. if (aNode.isContent()) {
  358. // Elements and PIs have a name
  359. if (aNode.mNode->IsElement() ||
  360. aNode.mNode->NodeType() ==
  361. nsIDOMNode::PROCESSING_INSTRUCTION_NODE) {
  362. aName = aNode.Content()->NodeName();
  363. return;
  364. }
  365. aName.Truncate();
  366. return;
  367. }
  368. aNode.Content()->GetAttrNameAt(aNode.mIndex)->GetQualifiedName(aName);
  369. }
  370. /* static */
  371. int32_t
  372. txXPathNodeUtils::getNamespaceID(const txXPathNode& aNode)
  373. {
  374. if (aNode.isDocument()) {
  375. return kNameSpaceID_None;
  376. }
  377. if (aNode.isContent()) {
  378. return aNode.Content()->GetNameSpaceID();
  379. }
  380. return aNode.Content()->GetAttrNameAt(aNode.mIndex)->NamespaceID();
  381. }
  382. /* static */
  383. void
  384. txXPathNodeUtils::getNamespaceURI(const txXPathNode& aNode, nsAString& aURI)
  385. {
  386. nsContentUtils::NameSpaceManager()->GetNameSpaceURI(getNamespaceID(aNode), aURI);
  387. }
  388. /* static */
  389. uint16_t
  390. txXPathNodeUtils::getNodeType(const txXPathNode& aNode)
  391. {
  392. if (aNode.isDocument()) {
  393. return txXPathNodeType::DOCUMENT_NODE;
  394. }
  395. if (aNode.isContent()) {
  396. return aNode.mNode->NodeType();
  397. }
  398. return txXPathNodeType::ATTRIBUTE_NODE;
  399. }
  400. /* static */
  401. void
  402. txXPathNodeUtils::appendNodeValue(const txXPathNode& aNode, nsAString& aResult)
  403. {
  404. if (aNode.isAttribute()) {
  405. const nsAttrName* name = aNode.Content()->GetAttrNameAt(aNode.mIndex);
  406. if (aResult.IsEmpty()) {
  407. aNode.Content()->GetAttr(name->NamespaceID(), name->LocalName(),
  408. aResult);
  409. }
  410. else {
  411. nsAutoString result;
  412. aNode.Content()->GetAttr(name->NamespaceID(), name->LocalName(),
  413. result);
  414. aResult.Append(result);
  415. }
  416. return;
  417. }
  418. if (aNode.isDocument() ||
  419. aNode.mNode->IsElement() ||
  420. aNode.mNode->IsNodeOfType(nsINode::eDOCUMENT_FRAGMENT)) {
  421. nsContentUtils::AppendNodeTextContent(aNode.mNode, true, aResult,
  422. mozilla::fallible);
  423. return;
  424. }
  425. aNode.Content()->AppendTextTo(aResult);
  426. }
  427. /* static */
  428. bool
  429. txXPathNodeUtils::isWhitespace(const txXPathNode& aNode)
  430. {
  431. NS_ASSERTION(aNode.isContent() && isText(aNode), "Wrong type!");
  432. return aNode.Content()->TextIsOnlyWhitespace();
  433. }
  434. /* static */
  435. txXPathNode*
  436. txXPathNodeUtils::getOwnerDocument(const txXPathNode& aNode)
  437. {
  438. return new txXPathNode(aNode.mNode->OwnerDoc());
  439. }
  440. const char gPrintfFmt[] = "id0x%p";
  441. const char gPrintfFmtAttr[] = "id0x%p-%010i";
  442. /* static */
  443. nsresult
  444. txXPathNodeUtils::getXSLTId(const txXPathNode& aNode,
  445. const txXPathNode& aBase,
  446. nsAString& aResult)
  447. {
  448. uintptr_t nodeid = ((uintptr_t)aNode.mNode) - ((uintptr_t)aBase.mNode);
  449. if (!aNode.isAttribute()) {
  450. CopyASCIItoUTF16(nsPrintfCString(gPrintfFmt, nodeid),
  451. aResult);
  452. }
  453. else {
  454. CopyASCIItoUTF16(nsPrintfCString(gPrintfFmtAttr,
  455. nodeid, aNode.mIndex), aResult);
  456. }
  457. return NS_OK;
  458. }
  459. /* static */
  460. nsresult
  461. txXPathNodeUtils::getBaseURI(const txXPathNode& aNode, nsAString& aURI)
  462. {
  463. return aNode.mNode->GetBaseURI(aURI);
  464. }
  465. /* static */
  466. int
  467. txXPathNodeUtils::comparePosition(const txXPathNode& aNode,
  468. const txXPathNode& aOtherNode)
  469. {
  470. // First check for equal nodes or attribute-nodes on the same element.
  471. if (aNode.mNode == aOtherNode.mNode) {
  472. if (aNode.mIndex == aOtherNode.mIndex) {
  473. return 0;
  474. }
  475. NS_ASSERTION(!aNode.isDocument() && !aOtherNode.isDocument(),
  476. "documents should always have a set index");
  477. if (aNode.isContent() || (!aOtherNode.isContent() &&
  478. aNode.mIndex < aOtherNode.mIndex)) {
  479. return -1;
  480. }
  481. return 1;
  482. }
  483. // Get document for both nodes.
  484. nsIDocument* document = aNode.mNode->GetUncomposedDoc();
  485. nsIDocument* otherDocument = aOtherNode.mNode->GetUncomposedDoc();
  486. // If the nodes have different current documents, compare the document
  487. // pointers.
  488. if (document != otherDocument) {
  489. return document < otherDocument ? -1 : 1;
  490. }
  491. // Now either both nodes are in orphan trees, or they are both in the
  492. // same tree.
  493. // Get parents up the tree.
  494. AutoTArray<nsINode*, 8> parents, otherParents;
  495. nsINode* node = aNode.mNode;
  496. nsINode* otherNode = aOtherNode.mNode;
  497. nsINode* parent;
  498. nsINode* otherParent;
  499. while (node && otherNode) {
  500. parent = node->GetParentNode();
  501. otherParent = otherNode->GetParentNode();
  502. // Hopefully this is a common case.
  503. if (parent == otherParent) {
  504. if (!parent) {
  505. // Both node and otherNode are root nodes in respective orphan
  506. // tree.
  507. return node < otherNode ? -1 : 1;
  508. }
  509. return parent->IndexOf(node) < parent->IndexOf(otherNode) ?
  510. -1 : 1;
  511. }
  512. parents.AppendElement(node);
  513. otherParents.AppendElement(otherNode);
  514. node = parent;
  515. otherNode = otherParent;
  516. }
  517. while (node) {
  518. parents.AppendElement(node);
  519. node = node->GetParentNode();
  520. }
  521. while (otherNode) {
  522. otherParents.AppendElement(otherNode);
  523. otherNode = otherNode->GetParentNode();
  524. }
  525. // Walk back down along the parent-chains until we find where they split.
  526. int32_t total = parents.Length() - 1;
  527. int32_t otherTotal = otherParents.Length() - 1;
  528. NS_ASSERTION(total != otherTotal, "Can't have same number of parents");
  529. int32_t lastIndex = std::min(total, otherTotal);
  530. int32_t i;
  531. parent = nullptr;
  532. for (i = 0; i <= lastIndex; ++i) {
  533. node = parents.ElementAt(total - i);
  534. otherNode = otherParents.ElementAt(otherTotal - i);
  535. if (node != otherNode) {
  536. if (!parent) {
  537. // The two nodes are in different orphan subtrees.
  538. NS_ASSERTION(i == 0, "this shouldn't happen");
  539. return node < otherNode ? -1 : 1;
  540. }
  541. int32_t index = parent->IndexOf(node);
  542. int32_t otherIndex = parent->IndexOf(otherNode);
  543. NS_ASSERTION(index != otherIndex && index >= 0 && otherIndex >= 0,
  544. "invalid index in compareTreePosition");
  545. return index < otherIndex ? -1 : 1;
  546. }
  547. parent = node;
  548. }
  549. // One node is a descendant of the other. The one with the shortest
  550. // parent-chain is first in the document.
  551. return total < otherTotal ? -1 : 1;
  552. }
  553. /* static */
  554. txXPathNode*
  555. txXPathNativeNode::createXPathNode(nsIContent* aContent, bool aKeepRootAlive)
  556. {
  557. nsINode* root = aKeepRootAlive ? txXPathNode::RootOf(aContent) : nullptr;
  558. return new txXPathNode(aContent, txXPathNode::eContent, root);
  559. }
  560. /* static */
  561. txXPathNode*
  562. txXPathNativeNode::createXPathNode(nsINode* aNode, bool aKeepRootAlive)
  563. {
  564. uint16_t nodeType = aNode->NodeType();
  565. if (nodeType == nsIDOMNode::ATTRIBUTE_NODE) {
  566. nsCOMPtr<nsIAttribute> attr = do_QueryInterface(aNode);
  567. NS_ASSERTION(attr, "doesn't implement nsIAttribute");
  568. mozilla::dom::NodeInfo *nodeInfo = attr->NodeInfo();
  569. mozilla::dom::Element* parent =
  570. static_cast<Attr*>(attr.get())->GetElement();
  571. if (!parent) {
  572. return nullptr;
  573. }
  574. nsINode* root = aKeepRootAlive ? txXPathNode::RootOf(parent) : nullptr;
  575. uint32_t i, total = parent->GetAttrCount();
  576. for (i = 0; i < total; ++i) {
  577. const nsAttrName* name = parent->GetAttrNameAt(i);
  578. if (nodeInfo->Equals(name->LocalName(), name->NamespaceID())) {
  579. return new txXPathNode(parent, i, root);
  580. }
  581. }
  582. NS_ERROR("Couldn't find the attribute in its parent!");
  583. return nullptr;
  584. }
  585. uint32_t index;
  586. nsINode* root = aKeepRootAlive ? aNode : nullptr;
  587. if (nodeType == nsIDOMNode::DOCUMENT_NODE) {
  588. index = txXPathNode::eDocument;
  589. }
  590. else {
  591. index = txXPathNode::eContent;
  592. if (root) {
  593. root = txXPathNode::RootOf(root);
  594. }
  595. }
  596. return new txXPathNode(aNode, index, root);
  597. }
  598. /* static */
  599. txXPathNode*
  600. txXPathNativeNode::createXPathNode(nsIDOMDocument* aDocument)
  601. {
  602. nsCOMPtr<nsIDocument> document = do_QueryInterface(aDocument);
  603. return new txXPathNode(document);
  604. }
  605. /* static */
  606. nsINode*
  607. txXPathNativeNode::getNode(const txXPathNode& aNode)
  608. {
  609. if (!aNode.isAttribute()) {
  610. return aNode.mNode;
  611. }
  612. const nsAttrName* name = aNode.Content()->GetAttrNameAt(aNode.mIndex);
  613. nsAutoString namespaceURI;
  614. nsContentUtils::NameSpaceManager()->GetNameSpaceURI(name->NamespaceID(), namespaceURI);
  615. nsCOMPtr<Element> element = do_QueryInterface(aNode.mNode);
  616. nsDOMAttributeMap* map = element->Attributes();
  617. return map->GetNamedItemNS(namespaceURI,
  618. nsDependentAtomString(name->LocalName()));
  619. }
  620. /* static */
  621. nsIContent*
  622. txXPathNativeNode::getContent(const txXPathNode& aNode)
  623. {
  624. NS_ASSERTION(aNode.isContent(),
  625. "Only call getContent on nsIContent wrappers!");
  626. return aNode.Content();
  627. }
  628. /* static */
  629. nsIDocument*
  630. txXPathNativeNode::getDocument(const txXPathNode& aNode)
  631. {
  632. NS_ASSERTION(aNode.isDocument(),
  633. "Only call getDocument on nsIDocument wrappers!");
  634. return aNode.Document();
  635. }