From eab7a2bb05982252d76d1cb9a080a9666702ed65 Mon Sep 17 00:00:00 2001 From: stelar7 Date: Fri, 11 Apr 2025 10:47:08 +0200 Subject: [PATCH] LibWeb/IDB: Implement extract_a_key_from_a_value_using_a_key_path --- Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp | 14 ++++++++++++++ Libraries/LibWeb/IndexedDB/Internal/Algorithms.h | 1 + 2 files changed, 15 insertions(+) diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp index b96a5031872..0c93eb597e7 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp +++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp @@ -976,4 +976,18 @@ WebIDL::ExceptionOr> evaluate_key_path_on_a_value(JS::Realm& return value; } +// https://w3c.github.io/IndexedDB/#extract-a-key-from-a-value-using-a-key-path +WebIDL::ExceptionOr>> extract_a_key_from_a_value_using_a_key_path(JS::Realm& realm, JS::Value value, KeyPath const& key_path, bool multi_entry) +{ + // 1. Let r be the result of evaluating a key path on a value with value and keyPath. Rethrow any exceptions. + // 2. If r is failure, return failure. + auto r = TRY(TRY(evaluate_key_path_on_a_value(realm, value, key_path))); + + // 3. Let key be the result of converting a value to a key with r if the multiEntry flag is false, + // and the result of converting a value to a multiEntry key with r otherwise. Rethrow any exceptions. + // 4. If key is invalid, return invalid. + // 5. Return key. + return multi_entry ? TRY(convert_a_value_to_a_multi_entry_key(realm, r)) : TRY(convert_a_value_to_a_key(realm, r)); +} + } diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h index 2eed3c31a3a..89d9edd0827 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h +++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h @@ -31,5 +31,6 @@ void commit_a_transaction(JS::Realm&, GC::Ref); WebIDL::ExceptionOr clone_in_realm(JS::Realm&, JS::Value, GC::Ref); WebIDL::ExceptionOr>> convert_a_value_to_a_multi_entry_key(JS::Realm&, JS::Value); WebIDL::ExceptionOr> evaluate_key_path_on_a_value(JS::Realm&, JS::Value, KeyPath const&); +WebIDL::ExceptionOr>> extract_a_key_from_a_value_using_a_key_path(JS::Realm&, JS::Value, KeyPath const&, bool = false); }