aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom
diff options
context:
space:
mode:
authorbors-servo <lbergstrom+bors@mozilla.com>2019-07-04 08:18:52 -0400
committerGitHub <noreply@github.com>2019-07-04 08:18:52 -0400
commit29097d15d031f9f0d18a284cf8db25c072b3b070 (patch)
tree7bfaf625154834ad4afad8959c46c62f42fb7c78 /components/script/dom
parentb32bff3b9737ac6b6905cb85685a88dc31dc6739 (diff)
parentb26aa3ec19b1270fa1b3583974320f415acb0bbf (diff)
downloadservo-29097d15d031f9f0d18a284cf8db25c072b3b070.tar.gz
servo-29097d15d031f9f0d18a284cf8db25c072b3b070.zip
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 -->
Diffstat (limited to 'components/script/dom')
-rw-r--r--components/script/dom/bindings/conversions.rs42
1 files changed, 36 insertions, 6 deletions
diff --git a/components/script/dom/bindings/conversions.rs b/components/script/dom/bindings/conversions.rs
index 442648fb9c8..17f72956ca1 100644
--- a/components/script/dom/bindings/conversions.rs
+++ b/components/script/dom/bindings/conversions.rs
@@ -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.