1 // Copyright (C) 2007, Fredrik Kuivinen <frekui@gmail.com>
2 // 2007, Petr Baudis <pasky@suse.cz>
3 // 2008-2011, Jakub Narebski <jnareb@gmail.com>
6 * @fileOverview Generic JavaScript code (helper functions)
7 * @license GPLv2 or later
11 /* ============================================================ */
12 /* ............................................................ */
16 * pad INPUT on the left with STR that is assumed to have visible
17 * width of single character (for example nonbreakable spaces),
20 * example: padLeftStr(12, 3, '\u00A0') == '\u00A012'
21 * ('\u00A0' is nonbreakable space)
23 * @param {Number|String} input: number to pad
24 * @param {Number} width: visible width of output
25 * @param {String} str: string to prefix to string, defaults to '\u00A0'
26 * @returns {String} INPUT prefixed with STR x (WIDTH - INPUT.length)
28 function padLeftStr(input, width, str) {
30 if (typeof str === 'undefined') {
31 ch = '\u00A0'; // using ' ' doesn't work in all browsers
34 width -= input.toString().length;
39 return prefix + input;
43 * Pad INPUT on the left to WIDTH, using given padding character CH,
44 * for example padLeft('a', 3, '_') is '__a'
45 * padLeft(4, 2) is '04' (same as padLeft(4, 2, '0'))
47 * @param {String} input: input value converted to string.
48 * @param {Number} width: desired length of output.
49 * @param {String} ch: single character to prefix to string, defaults to '0'.
51 * @returns {String} Modified string, at least SIZE length.
53 function padLeft(input, width, ch) {
55 if (typeof ch === 'undefined') {
59 while (s.length < width) {
66 /* ............................................................ */
67 /* Handling browser incompatibilities */
70 * Create XMLHttpRequest object in cross-browser way
71 * @returns XMLHttpRequest object, or null
73 function createRequestObject() {
75 return new XMLHttpRequest();
78 return window.createRequest();
81 return new ActiveXObject("Msxml2.XMLHTTP");
84 return new ActiveXObject("Microsoft.XMLHTTP");
92 * Insert rule giving specified STYLE to given SELECTOR at the end of
93 * first CSS stylesheet.
95 * @param {String} selector: CSS selector, e.g. '.class'
96 * @param {String} style: rule contents, e.g. 'background-color: red;'
98 function addCssRule(selector, style) {
99 var stylesheet = document.styleSheets[0];
102 if (stylesheet.cssRules) { // W3C way
103 theRules = stylesheet.cssRules;
104 } else if (stylesheet.rules) { // IE way
105 theRules = stylesheet.rules;
108 if (stylesheet.insertRule) { // W3C way
109 stylesheet.insertRule(selector + ' { ' + style + ' }', theRules.length);
110 } else if (stylesheet.addRule) { // IE way
111 stylesheet.addRule(selector, style);
116 /* ............................................................ */
117 /* Support for legacy browsers */
120 * Provides getElementsByClassName method, if there is no native
121 * implementation of this method.
123 * NOTE that there are limits and differences compared to native
124 * getElementsByClassName as defined by e.g.:
125 * https://developer.mozilla.org/en/DOM/document.getElementsByClassName
126 * http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#dom-getelementsbyclassname
127 * http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#dom-document-getelementsbyclassname
129 * Namely, this implementation supports only single class name as
130 * argument and not set of space-separated tokens representing classes,
131 * it returns Array of nodes rather than live NodeList, and has
132 * additional optional argument where you can limit search to given tags
133 * (via getElementsByTagName).
136 * http://code.google.com/p/getelementsbyclassname/
137 * http://www.dustindiaz.com/getelementsbyclass/
138 * http://stackoverflow.com/questions/1818865/do-we-have-getelementsbyclassname-in-javascript
140 * See also http://ejohn.org/blog/getelementsbyclassname-speed-comparison/
142 * @param {String} class: name of _single_ class to find
143 * @param {String} [taghint] limit search to given tags
144 * @returns {Node[]} array of matching elements
146 if (!('getElementsByClassName' in document)) {
147 document.getElementsByClassName = function (classname, taghint) {
148 taghint = taghint || "*";
149 var elements = (taghint === "*" && document.all) ?
151 document.getElementsByTagName(taghint);
152 var pattern = new RegExp("(^|\\s)" + classname + "(\\s|$)");
154 for (var i = 0, j = 0, n = elements.length; i < n; i++) {
156 if (el.className && pattern.test(el.className)) {
167 /* ............................................................ */
168 /* unquoting/unescaping filenames */
173 var escCodeRe = /\\([^0-7]|[0-7]{1,3})/g;
174 var octEscRe = /^[0-7]{1,3}$/;
175 var maybeQuotedRe = /^\"(.*)\"$/;
179 * unquote maybe C-quoted filename (as used by git, i.e. it is
180 * in double quotes '"' if there is any escape character used)
181 * e.g. 'aa' -> 'aa', '"a\ta"' -> 'a a'
183 * @param {String} str: git-quoted string
184 * @returns {String} Unquoted and unescaped string
186 * @globals escCodeRe, octEscRe, maybeQuotedRe
188 function unquote(str) {
191 // character escape codes, aka escape sequences (from C)
192 // replacements are to some extent JavaScript specific
193 t: "\t", // tab (HT, TAB)
194 n: "\n", // newline (NL)
195 r: "\r", // return (CR)
196 f: "\f", // form feed (FF)
197 b: "\b", // backspace (BS)
198 a: "\x07", // alarm (bell) (BEL)
199 e: "\x1B", // escape (ESC)
200 v: "\v" // vertical tab (VT)
203 if (seq.search(octEscRe) !== -1) {
204 // octal char sequence
205 return String.fromCharCode(parseInt(seq, 8));
206 } else if (seq in es) {
207 // C escape sequence, aka character escape code
210 // quoted ordinary character
214 var match = str.match(maybeQuotedRe);
217 // perhaps str = eval('"'+str+'"'); would be enough?
218 str = str.replace(escCodeRe,
219 function (substr, p1, offset, s) { return unq(p1); });
224 /* end of common-lib.js */