Bug 1251498: Implement IDBKeyRange.includes(). r=baku

This commit is contained in:
Kyle Huey 2016-02-26 13:58:47 -08:00
parent a0271adb8d
commit ef22731cc4
7 changed files with 105 additions and 2 deletions

View File

@ -334,6 +334,55 @@ IDBKeyRange::GetUpper(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
aResult.set(mCachedUpperVal);
}
bool
IDBKeyRange::Includes(JSContext* aCx,
JS::Handle<JS::Value> aValue,
ErrorResult& aRv) const
{
Key key;
aRv = GetKeyFromJSVal(aCx, aValue, key);
if (aRv.Failed()) {
return false;
}
switch (Key::CompareKeys(Lower(), key)) {
case 1:
return false;
case 0:
// Identical keys.
if (LowerOpen()) {
return false;
}
break;
case -1:
if (IsOnly()) {
return false;
}
break;
default:
MOZ_CRASH();
}
if (!IsOnly()) {
switch (Key::CompareKeys(key, Upper())) {
case 1:
return false;
case 0:
// Identical keys.
if (UpperOpen()) {
return false;
}
break;
case -1:
break;
}
} else {
MOZ_ASSERT(key == Lower());
}
return true;
}
// static
already_AddRefed<IDBKeyRange>
IDBKeyRange::Only(const GlobalObject& aGlobal,

View File

@ -125,6 +125,11 @@ public:
return mIsOnly ? mLower : mUpper;
}
bool
Includes(JSContext* aCx,
JS::Handle<JS::Value> aKey,
ErrorResult& aRv) const;
bool
IsOnly() const
{

View File

@ -235,7 +235,7 @@ public:
SetFromValueArray(mozIStorageValueArray* aValues, uint32_t aIndex);
static int16_t
CompareKeys(Key& aFirst, Key& aSecond)
CompareKeys(const Key& aFirst, const Key& aSecond)
{
int32_t result = Compare(aFirst.mBuffer, aSecond.mBuffer);

View File

@ -19,6 +19,10 @@ interface IDBKeyRange {
readonly attribute boolean lowerOpen;
[Constant]
readonly attribute boolean upperOpen;
[Throws]
boolean includes(any key);
[NewObject, Throws]
static IDBKeyRange only (any value);
[NewObject, Throws]

View File

@ -33768,7 +33768,13 @@
{
"path": "html/semantics/embedded-content/the-img-element/update-media.html",
"url": "/html/semantics/embedded-content/the-img-element/update-media.html"
}
}
],
"IndexedDB/idbkeyrange-includes.htm": [
{
"path": "IndexedDB/idbkeyrange-includes.htm",
"url": "/IndexedDB/idbkeyrange-includes.htm"
}
]
}
},

View File

@ -218,6 +218,12 @@
"url": "/_mozilla/service-workers/service-worker/fetch-request-css-base-url.https.html"
}
],
"service-workers/service-worker/fetch-request-css-images.https.html": [
{
"path": "service-workers/service-worker/fetch-request-css-images.https.html",
"url": "/_mozilla/service-workers/service-worker/fetch-request-css-images.https.html"
}
],
"service-workers/service-worker/fetch-request-fallback.https.html": [
{
"path": "service-workers/service-worker/fetch-request-fallback.https.html",

View File

@ -0,0 +1,33 @@
<!doctype html>
<meta charset=utf-8>
<title></title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script>
test( function() {
var closedRange = IDBKeyRange.bound(5, 20);
assert_true(!!closedRange.includes, "IDBKeyRange has a .includes");
assert_true(closedRange.includes(7), "in range");
assert_false(closedRange.includes(1), "below range");
assert_false(closedRange.includes(42), "above range");
assert_true(closedRange.includes(5) && closedRange.includes(20),
"boundary points");
assert_throws("DataError", function() { closedRange.includes({}) },
"invalid key");
}, "IDBKeyRange.includes() with a closed range");
test( function() {
var openRange = IDBKeyRange.bound(5, 20, true, true);
assert_false(openRange.includes(5) || openRange.includes(20),
"boundary points");
}, "IDBKeyRange.includes() with an open range");
test( function() {
var range = IDBKeyRange.only(42);
assert_true(range.includes(42), "in range");
assert_false(range.includes(1), "below range");
assert_false(range.includes(9000), "above range");
}, "IDBKeyRange.includes() with an only range");
</script>