javascriptgoogle-chrome-extensionknockout.jscontent-security-policyknockout-3.0

Why is this syntax error encountered in knockout-secure-binding


I am using Knockout Secure Binding (KSB) to make Knockout compatible with the content security policy in a Chrome browser extension.

The following works with Knockout's default binder, where plaintext is an observable value that returns a string:

 <section data-bind="foreach: plaintext().match(/.{1,17}/g)">

But when attempting to apply the binding with KSB this error is logged to the console:

{
   name: "SyntaxError",
   message: "Expected ')' but got '/'",
   at: 28,
   text: "foreach: plaintext().match(/.{1,17}/g)"
}

Since the / seemed to be the problem I tried creating the RegExp using this syntax instead:

 <section data-bind="foreach: plaintext().match(new RegExp('.{1,17}', 'g'))">

But KSB similarly complained, this time:

{
   name:"SyntaxError",
   message:"Expected ')' but got 'n'",
   at:28,
   text:"foreach: plaintext().match(new RegExp('.{1,17}', 'g'))"
}

This suggests it's not going to accept anything at all within the parenthesis for match(). In reading the docs and repo issues I haven't discovered anything about not using match or regex with KSB.

What am I missing here?


Solution

  • The default Knockout binding provider allows for any valid JS expression. The purpose of KSB is to change that behavior, so it kind of makes sense that stuff like this wouldn't work anymore.

    You should probably just move your regex to a computed observable.