1/*
2 * RapidContext <https://www.rapidcontext.com/>
3 * Copyright (c) 2007-2025 Per Cederberg. All rights reserved.
4 *
5 * This program is free software: you can redistribute it and/or
6 * modify it under the terms of the BSD license.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * See the RapidContext LICENSE for more details.
13 */
14
15// Create default RapidContext object
16if (typeof(RapidContext) == "undefined") {
17 RapidContext = {};
18}
19
20/**
21 * Provides utility functions for basic objects, arrays, DOM nodes and CSS.
22 * These functions are complementary to what is available in MochiKit and/or
23 * jQuery.
24 * @namespace RapidContext.Util
25 */
26if (typeof(RapidContext.Util) == "undefined") {
27 RapidContext.Util = {};
28}
29
30
31// General utility functions
32
33/**
34 * Converts a string to a title-cased string. All word boundaries are replaced
35 * with a single space and the subsequent character is capitalized.
36 *
37 * All underscore ("_"), hyphen ("-") and lower-upper character pairs are
38 * recognized as word boundaries. Note that this function does not change the
39 * capitalization of other characters in the string.
40 *
41 * @param {string} str the string to convert
42 *
43 * @return {string} the converted string
44 *
45 * @example
46 * RapidContext.Util.toTitleCase("a short heading")
47 * ==> "A Short Heading"
48 *
49 * @example
50 * RapidContext.Util.toTitleCase("camelCase")
51 * ==> "Camel Case"
52 *
53 * @example
54 * RapidContext.Util.toTitleCase("bounding-box")
55 * ==> "Bounding Box"
56 *
57 * @example
58 * RapidContext.Util.toTitleCase("UPPER_CASE_VALUE")
59 * ==> "UPPER CASE VALUE"
60 */
61RapidContext.Util.toTitleCase = function (str) {
62 str = str.replace(/[._-]+/g, " ").trim();
63 str = str.replace(/[a-z][A-Z]/g, (s) => `${s.charAt(0)} ${s.charAt(1)}`);
64 str = str.replace(/(^|\s)[a-z]/g, (s) => s.toUpperCase());
65 return str;
66};
67
68
69// DOM utility functions
70
71/**
72 * Blurs (unfocuses) a specified DOM node and all relevant child nodes. This
73 * function will recursively blur all `<a>`, `<button>`, `<input>`,
74 * `<textarea>` and `<select>` child nodes found.
75 *
76 * @param {Object} node the HTML DOM node
77 */
78RapidContext.Util.blurAll = function (node) {
79 node.blur();
80 const tags = ["A", "BUTTON", "INPUT", "TEXTAREA", "SELECT"];
81 for (let i = 0; i < tags.length; i++) {
82 const nodes = node.getElementsByTagName(tags[i]);
83 for (let j = 0; j < nodes.length; j++) {
84 nodes[j].blur();
85 }
86 }
87};
88
RapidContext
Access · Discovery · Insight
www.rapidcontext.com