Search code examples
xpages

why does xPage process my Comments as though they weren't


I got a strange behavior in XPage, with commented Code.

I had a textfield with a lot of code in it which outputs html where I hit this issue. While developing I had a lot of trouble with some fields other fields so I decided -to get a better idea what the field is doing- to copy the code from the troublefields to a comment inside my textfield to have the other code at my sight. But then the xPage started to behave strange until I found the issue.

The Code below is a example what caused my issue, it has two text fields in it one which sets a scope var and also has a comment which sets the same var but commented, ant another one which shows the ScopeVar. I thought this would output 'where I am' in the second text box but instead I got the 'Huhu I am here'.

        <xp:text escape="true" id="computedField7">
            <xp:this.value><![CDATA[#{javascript://
                sessionScope.put("findme","where i am");
                /* #{javascript:sessionScope.put("findme","HuHu I am here!");} */
                return sessionScope.findme;}]]></xp:this.value>
        </xp:text>
        <xp:br></xp:br>
        <xp:text escape="true" id="computedField6"
            value="#{javascript:return sessionScope.findme;}">
        </xp:text>

In my original Code where I hit this issue I wanted to comment the old #{} el out to use a JavaScript instead but keep the el in comment in the middle of the code.. same result. It seams that if you use #{ or ${ in a comment it will always get computed!

Got this fixed in notes 9. I am currently using 8.5.3.

Update: As a small note: Be careful when using the dojoAttribute queryExpr because the query Looks like SSJS "${0}" and also gets interpreted as SSJS. I now use this:

<xe:this.queryExpr><![CDATA[${javascript:"*$\{0}*";}]]></xe:this.queryExpr>

to make it work. Thanks to Paul Stephen Withers for the tip with the \{.


Solution

  • This is a funny bug.

    It is caused by pre-processor functionality of JavaScript interpreter. Normally, you can write #{javascript:...} in CSJS code to replace parts of code before it gets put to rendered page.

    In your case it is SSJS. Again, the interpreter replaces the #{javascript:...} inside your SSJS code and thinks, work is done. This way you see the code on rendered page instead the result of executed code.

    rendered page

    As a workaround, just delete # from /* #{javascript... and it will work like expected.