# Bug with assert\_select and javascript?

**URL:** <https://www.ruby-forum.com/t/bug-with-assert-select-and-javascript/155568>\
**Category:** Rails\
**Created:** [December 13, 2008, 6:04pm UTC](https://www.ruby-forum.com/t/bug-with-assert-select-and-javascript/155568 "2008-12-13T18:04:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [December 13, 2008, 6:04pm UTC](https://www.ruby-forum.com/t/bug-with-assert-select-and-javascript/155568/1 "2008-12-13T18:04:36Z")

</div>

Hi, I am using Rails 2.2.2 and have some javascript in my html.erb file  
in the body of the document, inside script tags. The script (which works  
as intended) contains the line  
for(var i=0; i\<boxes.length; i++)  
In my functional tests I use assert\_select and it is generating a  
warning “ignoring attempt to close boxes with script”. It appears to be  
interpreting the text “\<boxes” as the start of a tag. The only way I  
have found to get round it is to add a javascript comment "// "  
to the end of the line.  
Is this a bug in assert\_select or am I doing something foolish in my  
ignorance?

Colin

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [December 13, 2008, 6:30pm UTC](https://www.ruby-forum.com/t/bug-with-assert-select-and-javascript/155568/2 "2008-12-13T18:30:26Z")

</div>

On 13 Dec 2008, at 17:04, Colin L. wrote:

> Hi, I am using Rails 2.2.2 and have some javascript in my html.erb  
> file  
> in the body of the document, inside script tags. The script (which  
> works  
> as intended) contains the line  
> is the javascript inside a \<![CDATA[ section ?

fred

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [December 13, 2008, 6:43pm UTC](https://www.ruby-forum.com/t/bug-with-assert-select-and-javascript/155568/3 "2008-12-13T18:43:04Z")

</div>

Frederick C. wrote:

> On 13 Dec 2008, at 17:04, Colin L. wrote:
> 
> > Hi, I am using Rails 2.2.2 and have some javascript in my html.erb  
> > file  
> > in the body of the document, inside script tags. The script (which  
> > works  
> > as intended) contains the line  
> > is the javascript inside a \<![CDATA[ section ?
> 
> fred

It wasn’t but it is now and is working, many thanks.

Colin

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [December 14, 2008, 3:08am UTC](https://www.ruby-forum.com/t/bug-with-assert-select-and-javascript/155568/4 "2008-12-14T03:08:03Z")

</div>

Colin L. wrote:

> for(var i=0; i\<boxes.length; i++)

> Is this a bug in assert\_select or am I doing something foolish in my  
> ignorance?

Ultimately the fault lies in zillions of web pages written with Notepad.

The Law (actually the Recommendations) say thou shalt escape \< with  
\<. But  
not all web pages treat & correctly, so browsers in turn cannot enforce  
the  
exact rules. And our tools follow suit!

You might be able to get by with \< there.

Whatever the fix, you should move all but trivial JS up into  
application.js or  
some similar included file.

We dealt with && recently (in JS code we inexplicably had to add \<% %\>  
tags to)  
by writing function aaaand(q,k){ return q && k; }. We put aaaand up in  
application.js, but note this hack turns off boolean short-circuiting!

–  
Phlip
