ImportMap.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright (c) 2022, networkException <networkexception@serenityos.org>
  3. * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/HashMap.h>
  9. #include <LibURL/URL.h>
  10. #include <LibWeb/WebIDL/ExceptionOr.h>
  11. namespace Web::HTML {
  12. using ModuleSpecifierMap = HashMap<ByteString, Optional<URL::URL>>;
  13. using ModuleIntegrityMap = HashMap<URL::URL, ByteString>;
  14. // https://html.spec.whatwg.org/multipage/webappapis.html#import-map
  15. class ImportMap {
  16. public:
  17. ImportMap() = default;
  18. ModuleSpecifierMap const& imports() const { return m_imports; }
  19. ModuleSpecifierMap& imports() { return m_imports; }
  20. void set_imports(ModuleSpecifierMap const& imports) { m_imports = imports; }
  21. HashMap<URL::URL, ModuleSpecifierMap> const& scopes() const { return m_scopes; }
  22. HashMap<URL::URL, ModuleSpecifierMap>& scopes() { return m_scopes; }
  23. void set_scopes(HashMap<URL::URL, ModuleSpecifierMap> const& scopes) { m_scopes = scopes; }
  24. ModuleIntegrityMap const& integrity() const { return m_integrity; }
  25. ModuleIntegrityMap integrity() { return m_integrity; }
  26. void set_integrity(ModuleIntegrityMap const& integrity) { m_integrity = integrity; }
  27. private:
  28. ModuleSpecifierMap m_imports;
  29. HashMap<URL::URL, ModuleSpecifierMap> m_scopes;
  30. ModuleIntegrityMap m_integrity;
  31. };
  32. WebIDL::ExceptionOr<ImportMap> parse_import_map_string(JS::Realm& realm, ByteString const& input, URL::URL base_url);
  33. WebIDL::ExceptionOr<Optional<DeprecatedFlyString>> normalise_specifier_key(JS::Realm& realm, DeprecatedFlyString specifier_key, URL::URL base_url);
  34. WebIDL::ExceptionOr<ModuleSpecifierMap> sort_and_normalise_module_specifier_map(JS::Realm& realm, JS::Object& original_map, URL::URL base_url);
  35. WebIDL::ExceptionOr<HashMap<URL::URL, ModuleSpecifierMap>> sort_and_normalise_scopes(JS::Realm& realm, JS::Object& original_map, URL::URL base_url);
  36. WebIDL::ExceptionOr<ModuleIntegrityMap> normalize_module_integrity_map(JS::Realm& realm, JS::Object& original_map, URL::URL base_url);
  37. }