5

I was looking at a Function that Django uses to get the CSRF Token, when you need to validate a user session, using Jquery and JavaScript. Here is the code:

function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie != '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) == (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}
var csrftoken = getCookie('csrftoken');

You can also see the code online here on the djangoproject website, here.

Okay, now when I paste this code, I get a light warning from my IDE saying this:

Binary operation argument type String is not assignable to type HTMLCollection

What does it mean by this? The error is on this line:

document.cookie && document.cookie != ''
Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
  • 8
    On which line is the error thrown? This is like calling 911 and then not giving your address. Line number, please! – Šime Vidas Jun 29 '13 at 19:33
  • Funny man... @ŠimeVidas – Jayram Jun 29 '13 at 19:43
  • 1
    What IDE are you talking about? The error message doesn't make sense for JavaScript code. – Pointy Jun 29 '13 at 19:50
  • If I do some research on the web, I think your IDE might be figuring out the type of an variable via the documentation, and notifies you that you are trying to compare or alter a HTMLCollection with a string, and that the string cannot be converted to a HTMLCollection. (Therefore, the comparisation would make no sense or both variables could not be combined with that operator. Depending on what you have in `name` (or what the IDE thinks is in it), that might be causing it.) – Sumurai8 Jun 29 '13 at 20:02
  • name is the string 'csrftoken' – Robert Gannon Jun 29 '13 at 20:03
  • @Pointy the IDE is [Jet Brain's Pycharm](http://www.jetbrains.com/pycharm/) IDE. – Robert Gannon Jun 29 '13 at 22:34
  • I think @Ariel answer is to the point and acceptable. – dashesy Jan 20 '15 at 22:44

3 Answers3

4

The problem is that Jetbrains PyCharm/Webstorm has "document.cookie" defined as @type {HTMLCollection}, as you can see when Ctrl-clicking on the 'cookie' variable (that will open the file "DHTML.js" containing the definition). That definition is wrong, all technical docs for 'document.cookie' describe it as a string, see for instance http://www.w3schools.com/jsref/prop_doc_cookie.asp Actually it was also reported as bug WEB-11410 in Jetbrains tools, and seems to be fixed now in the newer builds.

If you want to remove the warning, either add the inline comment as suggested by gztomas, or press Alt-Enter on the warning and go to "inspection 'type mismatch' options" and then "suppress for statement", that will disable the warning for the current statement only.

Changing the comparison to !== as suggested by Gannon does not help. Actually the comparison works correctly with !== also, most probably because this is just an issue with the IDE, 'document.cookie' is a string in all browsers.

(I don't have enough reputation for commenting on previous answers, so adding this as extra answer...)

Ariel
  • 202
  • 1
  • 8
  • this looks like a pretty good answer, referencing the related bug and more to actual documentation of what it should be. – dashesy Jan 20 '15 at 22:43
0

Line 3:

if (document.cookie && document.cookie != '') {

The IDE is warning that the empty string '' is a string type, and there is an implicit cast resulting from != of the empty string to the type of document.cookie, which is an HTMLCollection type.

Robert Gannon
  • 253
  • 4
  • 13
  • That did not change it I'm afraid. – Games Brainiac Jun 30 '13 at 07:40
  • I would not expect the warning to go away; if you use !== there won't be an implicit type conversion and your IDE error would go away, however your test would never return true becuas !== would check the types and return false. There is a [post](http://stackoverflow.com/questions/11125825/converting-a-object-htmlcollection-into-string-in-javascript) about converting HTMLCollection type objects into strings. – Robert Gannon Jun 30 '13 at 14:19
  • If Jetbrains Pycharm works like Jetbrains IDEa, you could probably change the inspection profile setting? – Robert Gannon Jul 02 '13 at 21:43
0

Try modifying line 3 to:

if (document.cookie && document.cookie != /**@type HTMLCollection*/'') {

Your IDE probably understands document.cookie as being of type HTMLCollection. The inline comment works as a cast for the interpreter.

gztomas
  • 3,030
  • 3
  • 27
  • 38