1818import static com .google .common .base .Preconditions .checkArgument ;
1919import static java .util .Objects .requireNonNull ;
2020
21- import java .net .URI ;
22- import java .net .URISyntaxException ;
2321import java .util .regex .Pattern ;
2422
25- import com .google .common .collect .ImmutableSet ;
26-
2723/**
28- * Util class for DocServiceBuilder#injectedScripts method .
24+ * Provides utilities for {@link DocServiceBuilder#injectedScripts(String...)} .
2925 */
30- public final class DocServiceInjectedScriptsUtil {
26+ public final class DocServiceInjectableScripts {
3127
32- private static final String HEX_COLOR_PATTERN = "^#([0-9a-fA-F]{6 }|[0-9a-fA-F]{3 })$" ;
28+ private static final String HEX_COLOR_PATTERN = "^#? ([0-9a-fA-F]{3 }|[0-9a-fA-F]{6 })$" ;
3329 private static final int MAX_COLOR_LENGTH = 7 ;
3430 private static final String SAFE_DOM_HOOK = "data-js-target" ;
35- private static final ImmutableSet < String > ALLOWED_FAVICON_EXTENSIONS =
36- ImmutableSet . of ( ".ico" , ".png" , ".svg" ) ;
37- private static final ImmutableSet < String > ALLOWED_SCHEMES = ImmutableSet . of ( "http" , "https" ) ;
31+ private static final String TITLE_BACKGROUND_KEY = "titleBackground" ;
32+ private static final String GOTO_BACKGROUND_KEY = "gotoBackground" ;
33+ private static final String FAVICON_KEY = "favicon" ;
3834
3935 /**
40- * Returns a js script to change the title background color.
36+ * Returns a js script to change the title background to the specified color in hex code format .
4137 *
4238 * @param color the color string to set
4339 * @return the js script
4440 */
45- public static String withTitleBackground (String color ) {
46- final String titleBackgroundKey = "titleBackground" ;
41+ public static String titleBackground (String color ) {
4742 final String targetAttr = "main-app-bar" ;
48- validateHexColor (color , titleBackgroundKey );
43+ validateHexColor (color , TITLE_BACKGROUND_KEY );
4944
50- return buildStyleScript (color , targetAttr );
45+ return buildStyleScript (checkHashtagInHexColorCode ( color ) , targetAttr );
5146 }
5247
5348 /**
54- * Returns a js script to change the goto component background color.
49+ * Returns a js script to change the background of the goto component to the specified color in hex code
50+ * format.
5551 *
5652 * @param color the color string to set
5753 * @return the js script
5854 */
59- public static String withGotoBackground (String color ) {
60- final String gotoBackgroundKey = "gotoBackground" ;
55+ public static String gotoBackground (String color ) {
6156 final String targetAttr = "goto-app-bar" ;
62- validateHexColor (color , gotoBackgroundKey );
57+ validateHexColor (color , GOTO_BACKGROUND_KEY );
6358
64- return buildStyleScript (color , targetAttr );
59+ return buildStyleScript (checkHashtagInHexColorCode ( color ) , targetAttr );
6560 }
6661
6762 /**
@@ -70,14 +65,13 @@ public static String withGotoBackground(String color) {
7065 * @param uri the uri string to set
7166 * @return the js script
7267 */
73- public static String withFavicon (String uri ) {
74- final String faviconKey = "favicon" ;
75- validateFaviconUri (uri , faviconKey );
68+ public static String favicon (String uri ) {
69+ validateFaviconUri (uri , FAVICON_KEY );
7670
7771 return buildFaviconScript (escapeJavaScriptUri (uri ));
7872 }
7973
80- private DocServiceInjectedScriptsUtil () {}
74+ private DocServiceInjectableScripts () {}
8175
8276 /**
8377 * Validates that the given color is a non-null, non-empty, character hex color string.
@@ -94,6 +88,20 @@ private static void validateHexColor(String color, String key) {
9488 "%s not in hex format: %s." , key , color );
9589 }
9690
91+ /**
92+ * Check if the given color starts with a hashtag char.
93+ *
94+ * @param color the color string to validate
95+ * @return hex color string with hashtag included
96+ */
97+ private static String checkHashtagInHexColorCode (String color ) {
98+
99+ if (color .startsWith ("#" )) {
100+ return color ;
101+ }
102+ return '#' + color ;
103+ }
104+
97105 /**
98106 * Builds a JavaScript snippet that sets the background color of a DOM element.
99107 *
@@ -118,38 +126,6 @@ private static String buildStyleScript(String color, String targetAttr) {
118126 private static void validateFaviconUri (String uri , String key ) {
119127 requireNonNull (uri , key );
120128 checkArgument (!uri .trim ().isEmpty (), "%s is empty." , key );
121- checkArgument (isValidUri (uri ), "%s uri invalid." , key );
122- checkArgument (hasValidFaviconExtension (uri ), "%s extension not allowed." ,key );
123- }
124-
125- /**
126- * Check if the input is a valid URI.
127- * @param input the uri string to validate
128- * @return true if is valid
129- */
130- public static boolean isValidUri (String input ) {
131- try {
132- final URI uri = new URI (input );
133- final String scheme = uri .getScheme ();
134- if (scheme == null ) {
135- return true ;
136- }
137- return ALLOWED_SCHEMES .contains (scheme .toLowerCase ());
138- } catch (URISyntaxException e ) {
139- return false ;
140- }
141- }
142-
143- /**
144- * Validates the favicon extension.
145- *
146- * @param uri the uri string
147- * @return the result of validation
148- */
149- private static boolean hasValidFaviconExtension (String uri ) {
150- final String lowerUrl = uri .toLowerCase ();
151- return ALLOWED_FAVICON_EXTENSIONS .stream ()
152- .anyMatch (lowerUrl ::endsWith );
153129 }
154130
155131 /**
0 commit comments