Phantomjs: Array.from() with NodeList causes TypeError

Created on 20 Apr 2016  路  5Comments  路  Source: ariya/phantomjs

1. Which version of PhantomJS are you using?

2.1.5

2. What steps will reproduce the problem?

In a Babel generated code I have something like that:

Array.from(document.querySelectorAll("*"));

document.querySelectorAll("*") returns a NodeList. PhantomJS will fail with this code.

TypeError: undefined is not a constructor (evaluating 'Array.from(document.querySelectorAll("*"))')

3. Which operating system are you using?

Windows 7

4. Did you use binary PhantomJS or did you compile it from source?

I am using Karma, which acutally needs phantomjs-prebuilt. So I am not using phantomjs directly.

Most helpful comment

@Vitallium Thank you for your answer!

This is quite funny, because as I already said, this is a part of a _Babel generated code_.
When using

var arr = [...theNodeList];

to convert a NodeList to an array, the following (including Array.from()) will be generated, even when using the es2015 preset:

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

var arr = _toConsumableArray(theNodeList);

I have taken this way of conversion from the docs.

So, as you already mentioned that Array.from() is part of ES6, it is funny that Babel generates a ES6 code to workaround ES6. I will open an issue for that...

All 5 comments

@ariya

ES6 features are not supported in PhantomJS yet.

@Vitallium Thank you for your answer!

This is quite funny, because as I already said, this is a part of a _Babel generated code_.
When using

var arr = [...theNodeList];

to convert a NodeList to an array, the following (including Array.from()) will be generated, even when using the es2015 preset:

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

var arr = _toConsumableArray(theNodeList);

I have taken this way of conversion from the docs.

So, as you already mentioned that Array.from() is part of ES6, it is funny that Babel generates a ES6 code to workaround ES6. I will open an issue for that...

In the mean time, please track ES6 support in #14506. Thank you!

Was this page helpful?
0 / 5 - 0 ratings