Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8269030

Support loading stylesheets from data-URIs

    XMLWordPrintable

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P4
    • Resolution: Approved
    • Fix Version/s: openjfx17
    • Component/s: javafx
    • Labels:
      None
    • Subcomponent:
    • Compatibility Kind:
      source, behavioral
    • Compatibility Risk:
      minimal
    • Interface Kind:
      Java API, File or wire format
    • Scope:
      JDK

      Description

      Summary

      Add support for the data-URI scheme to load stylesheets embedded into code or FXML files.

      Problem

      Currently, JavaFX can only load stylesheets from external CSS files. Embedding CSS files into data URIs can be useful for small applications that would otherwise need to deploy their stylesheets alongside the application. It can also be useful to dynamically generate and add stylesheets to applications without deploying temporary files on disk.

      Solution

      Stylesheet.loadStylesheetUnPrivileged will be extended to detect data URIs and load the stylesheet from the embedded data.

      Specification

      The documentation of Scene.getStylesheets will be changed as follows:

        /**
         * Gets an observable list of string URLs linking to the stylesheets to use
         * with this scene's contents.
         * <p>
         * The URL is a hierarchical URI of the form [scheme:]//authority[path]. If the URL
         * does not have a [scheme:] component, the URL is considered to be the [path] component only.
         * Any leading '/' character of the [path] is ignored and the [path] is treated as a path relative to
         * the root of the application's classpath.
      +  * <p>
      +  * The RFC 2397 "data" scheme for URLs is supported in addition to the protocol handlers that
      +  * are registered for the application.
      +  * If a URL uses the "data" scheme and the MIME type is either empty, "text/plain", or "text/css",
      +  * the payload will be interpreted as a CSS file.
      +  * If the MIME type is "application/octet-stream", the payload will be interpreted as a binary
      +  * CSS file (see {@link Stylesheet#convertToBinary(File, File)}).
         * <pre><code>
         *
         * package com.example.javafx.app;
         *
         * import javafx.application.Application;
         * import javafx.scene.Group;
         * import javafx.scene.Scene;
         * import javafx.stage.Stage;
         *
         * public class MyApp extends Application {
         *
         *     {@literal @}Override public void start(Stage stage) {
         *         Scene scene = new Scene(new Group());
         *         scene.getStylesheets().add("/com/example/javafx/app/mystyles.css");
         *         stage.setScene(scene);
         *         stage.show();
         *     }
         *
         *     public static void main(String[] args) {
         *         launch(args);
         *     }
         * }
         * </code></pre>
         * For additional information about using CSS with the scene graph,
         * see the <a href="doc-files/cssref.html">CSS Reference Guide</a>.
         *
         * @return the list of stylesheets to use with this scene
         */
        public final ObservableList<String> getStylesheets() { return stylesheets; }

      The documentation of Scene.setUserAgentStylesheet will be changed as follows:

        /**
         * Set the URL of the user-agent stylesheet that will be used by this Scene in place of the
         * the platform-default user-agent stylesheet. If the URL does not resolve to a valid location,
         * the platform-default user-agent stylesheet will be used.
         * <p>
      -  * For additional information about using CSS with the scene graph,
      -  * see the <a href="doc-files/cssref.html">CSS Reference Guide</a>.
      -  * </p>
      -  * @param url The URL is a hierarchical URI of the form [scheme:]//authority[path]. If the URL
      +  * The URL is a hierarchical URI of the form [scheme:]//authority[path]. If the URL
         * does not have a [scheme:] component, the URL is considered to be the [path] component only.
         * Any leading '/' character of the [path] is ignored and the [path] is treated as a path relative to
         * the root of the application's classpath.
      +  * <p>
      +  * The RFC 2397 "data" scheme for URLs is supported in addition to the protocol handlers that
      +  * are registered for the application.
      +  * If a URL uses the "data" scheme and the MIME type is either empty, "text/plain", or "text/css",
      +  * the payload will be interpreted as a CSS file.
      +  * If the MIME type is "application/octet-stream", the payload will be interpreted as a binary
      +  * CSS file (see {@link Stylesheet#convertToBinary(File, File)}).
      +  * <p>
      +  * For additional information about using CSS with the scene graph,
      +  * see the <a href="doc-files/cssref.html">CSS Reference Guide</a>.
      +  *
      +  * @param url the URL of the user-agent stylesheet
         * @since  JavaFX 8u20
         */
        public final void setUserAgentStylesheet(String url) {

      The documentation of SubScene.setUserAgentStylesheet will be changed as follows:

        /**
         * Set the URL of the user-agent stylesheet that will be used by this SubScene in place of the
         * the platform-default user-agent stylesheet. If the URL does not resolve to a valid location,
         * the platform-default user-agent stylesheet will be used.
         * <p>
      -  * For additional information about using CSS with the scene graph,
      -  * see the <a href="doc-files/cssref.html">CSS Reference Guide</a>.
      -  * </p>
      -  * @param url The URL is a hierarchical URI of the form [scheme:]//authority[path]. If the URL
      +  * The URL is a hierarchical URI of the form [scheme:]//authority[path]. If the URL
         * does not have a [scheme:] component, the URL is considered to be the [path] component only.
         * Any leading '/' character of the [path] is ignored and the [path] is treated as a path relative to
         * the root of the application's classpath.
      +  * <p>
      +  * The RFC 2397 "data" scheme for URLs is supported in addition to the protocol handlers that
      +  * are registered for the application.
      +  * If a URL uses the "data" scheme and the MIME type is either empty, "text/plain", or "text/css",
      +  * the payload will be interpreted as a CSS file.
      +  * If the MIME type is "application/octet-stream", the payload will be interpreted as a binary
      +  * CSS file (see {@link Stylesheet#convertToBinary(File, File)}).
      +  * <p>
      +  * For additional information about using CSS with the scene graph,
      +  * see the <a href="doc-files/cssref.html">CSS Reference Guide</a>.
      +  *
      +  * @param url the URL of the user-agent stylesheet
         * @since  JavaFX 8u20
         */
        public final void setUserAgentStylesheet(String url) {

      The documentation of Application.setUserAgentStylesheet will be changed as follows:

        /**
         * Set the user agent stylesheet used by the whole application. This is used
         * to provide default styling for all ui controls and other nodes. Each
         * release of JavaFX may have a new default value for this so if you need
         * to guarantee consistency you will need to call this method and choose
         * what default you would like for your application. A value of null will
         * restore the platform default stylesheet. This property can also be set
         * on the command line with {@code -Djavafx.userAgentStylesheetUrl=[URL]}
         * Setting it on the command line overrides anything set using this method
         * in code.
         * <p>
      +  * The URL is a hierarchical URI of the form [scheme:]//authority[path]. If the URL
      +  * does not have a [scheme:] component, the URL is considered to be the [path] component only.
      +  * Any leading '/' character of the [path] is ignored and the [path] is treated as a path relative to
      +  * the root of the application's classpath.
      +  * <p>
      +  * The RFC 2397 "data" scheme for URLs is supported in addition to the protocol handlers that
      +  * are registered for the application.
      +  * If a URL uses the "data" scheme and the MIME type is either empty, "text/plain", or "text/css",
      +  * the payload will be interpreted as a CSS file.
      +  * If the MIME type is "application/octet-stream", the payload will be interpreted as a binary
      +  * CSS file (see {@link Stylesheet#convertToBinary(File, File)}).
      +  * <p>
         * NOTE: This method must be called on the JavaFX Application Thread.
      -  * </p>
      -  *
         *
         * @param url The URL to the stylesheet as a String.
         * @since JavaFX 8.0
         */
        public static void setUserAgentStylesheet(String url) {

      A new method Stylesheet.loadBinary(InputStream) will be added (this is an overloaded version of the existing Stylesheet.loadBinary(URL)):

      /**
       * Loads a binary stylesheet from a stream.
       *
       * @param stream the input stream
       * @return the loaded {@code Stylesheet}
       * @throws IOException if the binary stream corresponds to a more recent binary
       * css version or if an I/O error occurs while reading from the stream
       *
       * @since 17
       */
      public static Stylesheet loadBinary(InputStream stream) throws IOException

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              mstrauss Michael Strauß
              Reporter:
              mstrauss Michael Strauß
              Reviewed By:
              Kevin Rushforth
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: