js menu and string replacement

Charla's Avatar

Charla

29 Mar, 2012 04:06 PM

I have a js menu for navigation using velocity format for output. The menu items are pulled from the title field of a data definition. I need to replace the apostrophe in the tile. I've tried this:

#set ($itemTitle = $itemTitle.replaceAll("'","\'"))

It doesn't seem to work. The still breaks if there is an apostrophe in the page title.

Showing the most recent page of comments. View the first page

  1. Support Staff 32 Posted by Ryan Griffith on 06 Jul, 2012 12:48 PM

    Ryan Griffith's Avatar

    Charla,

    I came across the url() method for the EscapeTool, perhaps this may help. Let's try the following.

    Replace:

    #set ($itemPath = $itemPath.replaceAll("&", "\&"))
    

    With:

    #set ( $itemTitle = $_EscapeTool.url($itemPath) )
    
  2. 33 Posted by Charla on 10 Jul, 2012 06:23 PM

    Charla's Avatar

    This is the error it came back with:

    Jul 10, 2012 1:13 PMAn error occurred: Error on line 75: The entity name
    must immediately follow the '&' in the entity reference.
    You may choose to retry the operation <>. If the problem persists, please
    contact a system administrator.
    The error has been logged to the system console.
    Home https://cmsprod.lamar.edu/home.act

    With the macro looking like:

    
    #macro (outputItemPath $node)#set ($itemPath =
    $node.getChild("link").value)
    #if ($node.getName() == "first-level-link")
    #set ($itemPath = $node.getChild("content").value)
    #elseif ($node.getName() == "second-level-link")
    #set ($itemPath = $node.getChild("content").value)
    #elseif ($node.getName() == "third-level-link")
    #set ($itemPath = $node.getChild("content").value)
    #end#set ($itemTitle = $node.getChild("title").value)
    #set ($itemTitle = $_EscapeTool.javascript($node.getChild("title").value))
    #if ($node.getName() == "system-folder")
    #set ($itemPath = $itemPath + "/index")
    #elseif ($node.getName() == "system-symlink")
    #set($itemTitle = $_EscapeTool.url($itemPath))
    #end<a href="${itemPath}">${itemTitle}</a>
    #end
    

    On 7/6/12 7:48 AM, "Ryan Griffith"
    [email blocked] wrote:

    set ( $itemTitle = $_EscapeTool.url($itemPath) )

    CONFIDENTIALITY: Any information contained in this e-mail
    (including attachments) is the property of The State of Texas and unauthorized disclosure or use is prohibited. Sending, receiving or
    forwarding of confidential, proprietary and privileged information is
    prohibited under Lamar Policy. If you received this e-mail in error,
    please notify the sender and delete this e-mail from your system.

  3. Support Staff 34 Posted by Ryan Griffith on 10 Jul, 2012 07:11 PM

    Ryan Griffith's Avatar

    Hi Charla, would you be able to provide me with a copy of the XML, or index block, that this Format is applied to? I'll see if I can get something set up in my local instance to I can test some more.

    Thanks!

  4. 35 Posted by Charla on 10 Jul, 2012 07:29 PM

    Charla's Avatar

    It doesn't apply to an index block, it applies to a data def. I've
    attached the xml for the data def and the nav format in txt format.

  5. Support Staff 36 Posted by Ryan Griffith on 11 Jul, 2012 03:07 PM

    Ryan Griffith's Avatar

    Charla,

    Perhaps you can try the following:

    Change:

    #set ( $itemTitle = $_EscapeTool.javascript($node.getChild("title").value) )
    

    To:

    #set ( $itemTitle = $_SerializerTool.serialize($node.getChild("title"),true) )
    
  6. 37 Posted by Charla on 11 Jul, 2012 03:36 PM

    Charla's Avatar

    Identical error:

    An error occurred: Error on line 75: The entity name must immediately
    follow the '&' in the entity reference.
    You may choose to retry the operation <>. If the problem persists, please
    contact a system administrator.
    The error has been logged to the system console.
    Home <https://cmsprod.lamar.edu/home.act&gt;

    Macro:

    @@@

    #macro (outputItemPath $node)
    #set ($itemPath = $node.getChild("link").value)
    #if ($node.getName() == "first-level-link")
    #set ($itemPath = $node.getChild("content").value)
    #elseif ($node.getName() == "second-level-link")
    #set ($itemPath = $node.getChild("content").value)
    #elseif ($node.getName() == "third-level-link")
    #set ($itemPath = $node.getChild("content").value)
    #end#set ($itemTitle = $node.getChild("title").value)
    #set ($itemTitle = $_EscapeTool.javascript($node.getChild("title").value))
    #if ($node.getName() == "system-folder")
    #set ($itemPath = $itemPath + "/index")
    #elseif ($node.getName() == "system-symlink")
    #set($itemTitle = $_SerializerTool.serialize($node.getChild("title"),true))
    #end
    <a href="${itemPath}">${itemTitle}</a>
    #end

    @@@

    On 7/11/12 10:07 AM, "Ryan Griffith"
    <[email blocked]> wrote:

    >#set ( $itemTitle =
    >$_SerializerTool.serialize($node.getChild("title"),true) )

    CONFIDENTIALITY: Any information contained in this e-mail
    (including attachments) is the property of The State of Texas and
    unauthorized disclosure or use is prohibited. Sending, receiving or
    forwarding of confidential, proprietary and privileged information is
    prohibited under Lamar Policy. If you received this e-mail in error,
    please notify the sender and delete this e-mail from your system.

  7. Support Staff 38 Posted by Ryan Griffith on 11 Jul, 2012 03:39 PM

    Ryan Griffith's Avatar

    It looks like there are similar lines, you may have changed the wrong one. Try the following:

    #macro (outputItemPath $node)
        #set ($itemPath = $node.getChild("link").value)
        
        #if ($node.getName() == "first-level-link")
            #set ($itemPath = $node.getChild("content").value)
        #elseif ($node.getName() == "second-level-link")
            #set ($itemPath = $node.getChild("content").value)
        #elseif ($node.getName() == "third-level-link")
            #set ($itemPath = $node.getChild("content").value)
        #end
    
        #set ( $itemTitle = $_SerializerTool.serialize($node.getChild("title"),true) ) 
        ## #set ($itemTitle = $_EscapeTool.javascript($node.getChild("title").value))
       
        #if ($node.getName() == "system-folder")
            #set ($itemPath = $itemPath + "/index")
        #elseif ($node.getName() == "system-symlink")
            #set ( $itemTitle = $_SerializerTool.serialize($node.getChild("link"),true) ) 
            ## #set($itemTitle = $_EscapeTool.javascript($itemPath))
        #end
        <a href="${itemPath}">${itemTitle}</a>
    #end
    
  8. 39 Posted by Charla on 11 Jul, 2012 03:47 PM

    Charla's Avatar

    Sorry, this macro in my format has no spaces and it's difficult to see
    where to put text.

    That fixed the & issue but broke my javascript escape. When someone type
    a apostrophe in a title, it breaks my js nav.

    On 7/11/12 10:39 AM, "Ryan Griffith"
    <[email blocked]> wrote:

    >## #set($itemTitle = $_EscapeTool.javascript($itemPath))

    CONFIDENTIALITY: Any information contained in this e-mail
    (including attachments) is the property of The State of Texas and
    unauthorized disclosure or use is prohibited. Sending, receiving or
    forwarding of confidential, proprietary and privileged information is
    prohibited under Lamar Policy. If you received this e-mail in error,
    please notify the sender and delete this e-mail from your system.

  9. Support Staff 40 Posted by Ryan Griffith on 11 Jul, 2012 06:33 PM

    Ryan Griffith's Avatar

    This might be a long shot, but you could try the following:

    #macro (outputItemPath $node)
        ## Create a double-quote variable for simpler HTML quote escaping.
        #set( $Q = '"' )
        
        ## Get the title of the asset,
        ## if a symlink, we just want the Link.
        #set ($itemTitle = $node.getChild("title").value)
        #if ($node.getChild("content").getChild("system-symlink"))
            #set($itemTitle = $itemPath)
        #end
        
        ## Get the path of the asset, or the symlink's Link.
        #set ($itemPath = $_XPathTool.selectSingleNode($node, "content/system-symlink|link").value)
    
        ## Build the resulting HTML and output it with the EscapeTool
        #set ( $output = "<a href=${Q}${itemPath}${Q}>${itemTitle}</a>" )    
        $_EscapeTool.xml($output)
    #end
    

    I updated the following:

    • Removed the top if statements that checked the level because it didn't look like that was being used.
    • Updated the way the $itemPath and $itemTitle are set and the additional check for the Symlink because it looked like you were using getName which would return something like first-level-nav and not system-symlink or system-folder
    • Removed the check for system-folder, because I do not believe this would ever be present given the Data Definition
    • Ran the entire resulting HTML through the $_EscapeTool to make things a bit more efficient and easier to read
  10. Ryan Griffith closed this discussion on 18 Oct, 2012 08:19 PM.

Comments are currently closed for this discussion. You can start a new one.