Auto merge of #23669 - georgeroman:more_array_like_types, r=ferjm

Add support for more array-like types in is_array_like

<!-- Please describe your changes on the following line: -->

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/23669)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2019-07-04 08:18:52 -04:00 committed by GitHub
commit 29097d15d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -40,6 +40,11 @@ use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::{ByteString, DOMString, USVString};
use crate::dom::bindings::trace::{JSTraceable, RootedTraceableBox};
use crate::dom::bindings::utils::DOMClass;
use crate::dom::filelist::FileList;
use crate::dom::htmlcollection::HTMLCollection;
use crate::dom::htmlformcontrolscollection::HTMLFormControlsCollection;
use crate::dom::htmloptionscollection::HTMLOptionsCollection;
use crate::dom::nodelist::NodeList;
use js::conversions::latin1_to_string;
pub use js::conversions::ConversionBehavior;
pub use js::conversions::{ConversionResult, FromJSValConvertible, ToJSValConvertible};
@ -545,13 +550,38 @@ impl<T: DomObject> ToJSValConvertible for DomRoot<T> {
}
}
/// Returns whether `value` is an array-like object.
/// Note: Currently only Arrays are supported.
/// TODO: Expand this to support sequences and other array-like objects
/// Returns whether `value` is an array-like object (Array, FileList,
/// HTMLCollection, HTMLFormControlsCollection, HTMLOptionsCollection,
/// NodeList).
pub unsafe fn is_array_like(cx: *mut JSContext, value: HandleValue) -> bool {
let mut result = false;
assert!(JS_IsArrayObject(cx, value, &mut result));
result
let mut is_array = false;
assert!(JS_IsArrayObject(cx, value, &mut is_array));
if is_array {
return true;
}
let object: *mut JSObject = match FromJSValConvertible::from_jsval(cx, value, ()).unwrap() {
ConversionResult::Success(object) => object,
_ => return false,
};
if root_from_object::<FileList>(object, cx).is_ok() {
return true;
}
if root_from_object::<HTMLCollection>(object, cx).is_ok() {
return true;
}
if root_from_object::<HTMLFormControlsCollection>(object, cx).is_ok() {
return true;
}
if root_from_object::<HTMLOptionsCollection>(object, cx).is_ok() {
return true;
}
if root_from_object::<NodeList>(object, cx).is_ok() {
return true;
}
false
}
/// Get a property from a JS object.