nsContentSupportMap.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #ifndef nsContentSupportMap_h__
  6. #define nsContentSupportMap_h__
  7. #include "PLDHashTable.h"
  8. #include "nsTemplateMatch.h"
  9. /**
  10. * The nsContentSupportMap maintains a mapping from a "resource element"
  11. * in the content tree to the nsTemplateMatch that was used to instantiate it. This
  12. * is necessary to allow the XUL content to be built lazily. Specifically,
  13. * when building "resumes" on a partially-built content element, the builder
  14. * will walk upwards in the content tree to find the first element with an
  15. * 'id' attribute. This element is assumed to be the "resource element",
  16. * and allows the content builder to access the nsTemplateMatch (variable assignments
  17. * and rule information).
  18. */
  19. class nsContentSupportMap {
  20. public:
  21. nsContentSupportMap() : mMap(PLDHashTable::StubOps(), sizeof(Entry)) { }
  22. ~nsContentSupportMap() { }
  23. nsresult Put(nsIContent* aElement, nsTemplateMatch* aMatch) {
  24. PLDHashEntryHdr* hdr = mMap.Add(aElement, mozilla::fallible);
  25. if (!hdr)
  26. return NS_ERROR_OUT_OF_MEMORY;
  27. Entry* entry = static_cast<Entry*>(hdr);
  28. NS_ASSERTION(entry->mMatch == nullptr, "over-writing entry");
  29. entry->mContent = aElement;
  30. entry->mMatch = aMatch;
  31. return NS_OK;
  32. }
  33. bool Get(nsIContent* aElement, nsTemplateMatch** aMatch) {
  34. PLDHashEntryHdr* hdr = mMap.Search(aElement);
  35. if (!hdr)
  36. return false;
  37. Entry* entry = static_cast<Entry*>(hdr);
  38. *aMatch = entry->mMatch;
  39. return true;
  40. }
  41. void Remove(nsIContent* aElement);
  42. void Clear() { mMap.Clear(); }
  43. protected:
  44. PLDHashTable mMap;
  45. struct Entry : public PLDHashEntryHdr {
  46. nsIContent* mContent;
  47. nsTemplateMatch* mMatch;
  48. };
  49. };
  50. #endif