Annotation Interface JsStringFallback


@Documented @Retention(RUNTIME) @Target(TYPE) public @interface JsStringFallback
Marks a JsDefinition interface whose JavaScript stands in for JavaScript that is sent as a string, typically by an add-on that the application can not change.

When the Content Security Policy mode reports string JavaScript, a string that matches what a marked interface declares exactly, including whitespace, runs JavaScript from the bundle instead of being refused in the strict mode or reported in the warn mode. A string that does not match is refused or reported as before. In the off mode strings are sent as they are.

What a string is matched against depends on the API that sends it:

  • Element.executeJs(String, Object...) and Page.executeJs(String, Object...): a method whose JsExpression value is the string, with as many parameters as the call passes.
  • JsFunction.of(String, Object...), and so Element.addJsInitializer(String, Object...), and Page.addDynamicImport(String): a method that returns a JsFunction, whose JsExpression value is the function body, with as many parameters as there are captures. When the code then calls withArguments on the function, the method declares the same names with JsFunctionParameterNames; other names send the string again. These methods go into an interface that extends JsSnippets, and the methods for executeJs into one that extends JsInvokers, since one interface can not do both.
  • DomListenerRegistration.setFilter(String) and DomListenerRegistration.addEventData(String): the strings listed in filters() and eventData().
For example:
 @JsDefinition
 @JsStringFallback(filters = "event.key === 'Escape'")
 public interface AddonFallbacks extends JsInvokers {
     @JsExpression("return this.scrollTop")
     PendingJavaScriptResult scrollTop();
 }

 @JsDefinition
 @JsStringFallback
 public interface AddonFunctionFallbacks extends JsSnippets {
     @JsExpression("this.title = $0")
     JsFunction setTitle(Object title);
 }
 

Strings that are built from values that change, for example by joining a color into the JavaScript text, are not supported: a declaration matches one exact string, and the bundle can only hold strings that are known when the application is built. Such a string has to be fixed in the code that sends it, by passing the values as parameters ($0, $1, ...) or by using a JsDefinition. The report for a call site that sends many different strings says so.

JavaScript that only the browser refuses is not supported either, since Flow does not run it: an inline event handler attribute, such as onclick, or a call to eval or new Function in an add-on's own frontend files. The add-on has to change.

The JavaScript in the bundle runs in JavaScript strict mode, since the bundle is made of modules, while a string that the browser compiles does not. A string that only works outside strict mode, for example one that assigns to a variable it never declares, fails the same way when it runs from the bundle. Since the declared text has to be the same string, such a string can not be fixed with a fallback: the code that sends it has to change.

The build finds the marked interfaces together with the other JsDefinition interfaces, and fails if one of them can not stand in for a string: a variadic method, a snippet method that does not return a JsFunction, two methods that declare the same JavaScript with the same parameter count, two methods of one interface with the same name and parameter count, or a filter or event data value that is a property path, which the browser reads without compiling it.

Since:
25.4
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    The event data values, as passed to DomListenerRegistration.addEventData, that the build puts into the bundle as functions, so that adding one of them runs the function instead of compiling the value.
    The filters, as passed to DomListenerRegistration.setFilter, that the build puts into the bundle as functions, so that setting one of them runs the function instead of compiling the filter.
  • Element Details

    • filters

      String[] filters
      The filters, as passed to DomListenerRegistration.setFilter, that the build puts into the bundle as functions, so that setting one of them runs the function instead of compiling the filter.
      Returns:
      the filters, empty by default
      Default:
      {}
    • eventData

      String[] eventData
      The event data values, as passed to DomListenerRegistration.addEventData, that the build puts into the bundle as functions, so that adding one of them runs the function instead of compiling the value. The value is sent under the same key, so reading the event data does not change.
      Returns:
      the event data values, empty by default
      Default:
      {}