0
votes

I'm generating a JCR path in Javascript; I would need to escape each path item for illegal JCR character (e.g., / : etc...). Does anybody know a Javascript implementation of the Java Text.escapeIllegalJcrChars() ?

1

1 Answers

1
votes

Based on this code: (https://github.com/apache/jackrabbit/blob/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Text.java)

and this unit test (https://github.com/apache/jackrabbit/blob/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/TextTest.java)

this should do the same:

function escapeIllegalChars(name) {
  var illegalChars = "%/:[]*|\t\r\n";
     var buffer = "";
        for (var i = 0; i < name.length; i++) {
            var ch = name.charAt(i);
            if (illegalChars.indexOf(ch) != -1 || 
                (ch == '.' && name.length < 3)|| 
                (ch == ' ' && (i === 0 || i == name.length - 1))) {
                buffer += escape(ch);                
            } else {
                buffer += ch;
            }
        }
        return buffer;
}

You can see the code and associated test passing on JSBin at: (http://jsbin.com/eciYodo/2/edit?js,output)