txNameTest.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "txExpr.h"
  6. #include "nsIAtom.h"
  7. #include "nsGkAtoms.h"
  8. #include "txXPathTreeWalker.h"
  9. #include "txIXPathContext.h"
  10. txNameTest::txNameTest(nsIAtom* aPrefix, nsIAtom* aLocalName, int32_t aNSID,
  11. uint16_t aNodeType)
  12. :mPrefix(aPrefix), mLocalName(aLocalName), mNamespace(aNSID),
  13. mNodeType(aNodeType)
  14. {
  15. if (aPrefix == nsGkAtoms::_empty)
  16. mPrefix = nullptr;
  17. NS_ASSERTION(aLocalName, "txNameTest without a local name?");
  18. NS_ASSERTION(aNodeType == txXPathNodeType::DOCUMENT_NODE ||
  19. aNodeType == txXPathNodeType::ELEMENT_NODE ||
  20. aNodeType == txXPathNodeType::ATTRIBUTE_NODE,
  21. "Go fix txNameTest::matches");
  22. }
  23. bool txNameTest::matches(const txXPathNode& aNode, txIMatchContext* aContext)
  24. {
  25. if ((mNodeType == txXPathNodeType::ELEMENT_NODE &&
  26. !txXPathNodeUtils::isElement(aNode)) ||
  27. (mNodeType == txXPathNodeType::ATTRIBUTE_NODE &&
  28. !txXPathNodeUtils::isAttribute(aNode)) ||
  29. (mNodeType == txXPathNodeType::DOCUMENT_NODE &&
  30. !txXPathNodeUtils::isRoot(aNode))) {
  31. return false;
  32. }
  33. // Totally wild?
  34. if (mLocalName == nsGkAtoms::_asterisk && !mPrefix)
  35. return true;
  36. // Compare namespaces
  37. if (mNamespace != txXPathNodeUtils::getNamespaceID(aNode)
  38. && !(mNamespace == kNameSpaceID_None &&
  39. txXPathNodeUtils::isHTMLElementInHTMLDocument(aNode))
  40. )
  41. return false;
  42. // Name wild?
  43. if (mLocalName == nsGkAtoms::_asterisk)
  44. return true;
  45. // Compare local-names
  46. return txXPathNodeUtils::localNameEquals(aNode, mLocalName);
  47. }
  48. /*
  49. * Returns the default priority of this txNodeTest
  50. */
  51. double txNameTest::getDefaultPriority()
  52. {
  53. if (mLocalName == nsGkAtoms::_asterisk) {
  54. if (!mPrefix)
  55. return -0.5;
  56. return -0.25;
  57. }
  58. return 0;
  59. }
  60. txNodeTest::NodeTestType
  61. txNameTest::getType()
  62. {
  63. return NAME_TEST;
  64. }
  65. bool
  66. txNameTest::isSensitiveTo(Expr::ContextSensitivity aContext)
  67. {
  68. return !!(aContext & Expr::NODE_CONTEXT);
  69. }
  70. #ifdef TX_TO_STRING
  71. void
  72. txNameTest::toString(nsAString& aDest)
  73. {
  74. if (mPrefix) {
  75. nsAutoString prefix;
  76. mPrefix->ToString(prefix);
  77. aDest.Append(prefix);
  78. aDest.Append(char16_t(':'));
  79. }
  80. nsAutoString localName;
  81. mLocalName->ToString(localName);
  82. aDest.Append(localName);
  83. }
  84. #endif