/parser/htmlparser/src/nsParserService.cpp

http://github.com/zpao/v8monkey · C++ · 160 lines · 100 code · 22 blank · 38 comment · 10 complexity · 6e581850edd66aa7752246f6df5b51ef MD5 · raw file

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. *
  3. * ***** BEGIN LICENSE BLOCK *****
  4. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version
  7. * 1.1 (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is Mozilla Communicator client code.
  17. *
  18. * The Initial Developer of the Original Code is
  19. * Netscape Communications Corporation.
  20. * Portions created by the Initial Developer are Copyright (C) 1998
  21. * the Initial Developer. All Rights Reserved.
  22. *
  23. * Contributor(s):
  24. *
  25. * Alternatively, the contents of this file may be used under the terms of
  26. * either of the GNU General Public License Version 2 or later (the "GPL"),
  27. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. * in which case the provisions of the GPL or the LGPL are applicable instead
  29. * of those above. If you wish to allow use of your version of this file only
  30. * under the terms of either the GPL or the LGPL, and not to allow others to
  31. * use your version of this file under the terms of the MPL, indicate your
  32. * decision by deleting the provisions above and replace them with the notice
  33. * and other provisions required by the GPL or the LGPL. If you do not delete
  34. * the provisions above, a recipient may use your version of this file under
  35. * the terms of any one of the MPL, the GPL or the LGPL.
  36. *
  37. * ***** END LICENSE BLOCK ***** */
  38. #include "nsDOMError.h"
  39. #include "nsIAtom.h"
  40. #include "nsParserService.h"
  41. #include "nsHTMLEntities.h"
  42. #include "nsElementTable.h"
  43. #include "nsICategoryManager.h"
  44. #include "nsCategoryManagerUtils.h"
  45. extern "C" int MOZ_XMLCheckQName(const char* ptr, const char* end,
  46. int ns_aware, const char** colon);
  47. nsParserService::nsParserService()
  48. {
  49. }
  50. nsParserService::~nsParserService()
  51. {
  52. }
  53. NS_IMPL_ISUPPORTS1(nsParserService, nsIParserService)
  54. PRInt32
  55. nsParserService::HTMLAtomTagToId(nsIAtom* aAtom) const
  56. {
  57. return nsHTMLTags::LookupTag(nsDependentAtomString(aAtom));
  58. }
  59. PRInt32
  60. nsParserService::HTMLCaseSensitiveAtomTagToId(nsIAtom* aAtom) const
  61. {
  62. return nsHTMLTags::CaseSensitiveLookupTag(aAtom);
  63. }
  64. PRInt32
  65. nsParserService::HTMLStringTagToId(const nsAString& aTag) const
  66. {
  67. return nsHTMLTags::LookupTag(aTag);
  68. }
  69. const PRUnichar*
  70. nsParserService::HTMLIdToStringTag(PRInt32 aId) const
  71. {
  72. return nsHTMLTags::GetStringValue((nsHTMLTag)aId);
  73. }
  74. nsIAtom*
  75. nsParserService::HTMLIdToAtomTag(PRInt32 aId) const
  76. {
  77. return nsHTMLTags::GetAtom((nsHTMLTag)aId);
  78. }
  79. NS_IMETHODIMP
  80. nsParserService::HTMLConvertEntityToUnicode(const nsAString& aEntity,
  81. PRInt32* aUnicode) const
  82. {
  83. *aUnicode = nsHTMLEntities::EntityToUnicode(aEntity);
  84. return NS_OK;
  85. }
  86. NS_IMETHODIMP
  87. nsParserService::HTMLConvertUnicodeToEntity(PRInt32 aUnicode,
  88. nsCString& aEntity) const
  89. {
  90. const char* str = nsHTMLEntities::UnicodeToEntity(aUnicode);
  91. if (str) {
  92. aEntity.Assign(str);
  93. }
  94. return NS_OK;
  95. }
  96. NS_IMETHODIMP
  97. nsParserService::IsContainer(PRInt32 aId, bool& aIsContainer) const
  98. {
  99. aIsContainer = nsHTMLElement::IsContainer((eHTMLTags)aId);
  100. return NS_OK;
  101. }
  102. NS_IMETHODIMP
  103. nsParserService::IsBlock(PRInt32 aId, bool& aIsBlock) const
  104. {
  105. if((aId>eHTMLTag_unknown) && (aId<eHTMLTag_userdefined)) {
  106. aIsBlock=((gHTMLElements[aId].IsMemberOf(kBlock)) ||
  107. (gHTMLElements[aId].IsMemberOf(kBlockEntity)) ||
  108. (gHTMLElements[aId].IsMemberOf(kHeading)) ||
  109. (gHTMLElements[aId].IsMemberOf(kPreformatted))||
  110. (gHTMLElements[aId].IsMemberOf(kList)));
  111. }
  112. else {
  113. aIsBlock = false;
  114. }
  115. return NS_OK;
  116. }
  117. nsresult
  118. nsParserService::CheckQName(const nsAString& aQName,
  119. bool aNamespaceAware,
  120. const PRUnichar** aColon)
  121. {
  122. const char* colon;
  123. const PRUnichar *begin, *end;
  124. begin = aQName.BeginReading();
  125. end = aQName.EndReading();
  126. int result = MOZ_XMLCheckQName(reinterpret_cast<const char*>(begin),
  127. reinterpret_cast<const char*>(end),
  128. aNamespaceAware, &colon);
  129. *aColon = reinterpret_cast<const PRUnichar*>(colon);
  130. if (result == 0) {
  131. return NS_OK;
  132. }
  133. // MOZ_EXPAT_EMPTY_QNAME || MOZ_EXPAT_INVALID_CHARACTER
  134. if (result == (1 << 0) || result == (1 << 1)) {
  135. return NS_ERROR_DOM_INVALID_CHARACTER_ERR;
  136. }
  137. return NS_ERROR_DOM_NAMESPACE_ERR;
  138. }