2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
26 function createDefinitionList(items) {
27 var list = document.createElement('dl');
28 items.forEach(function(pair) {
29 var dt = document.createElement('dt');
30 dt.appendChild(pair[0]);
31 var dd = document.createElement('dd');
32 dd.appendChild(pair[1]);
39 function fetchResource(url, method, queryParameters, callback, errorCallback) {
40 var xhr = new XMLHttpRequest();
41 xhr.onreadystatechange = function() {
42 if (this.readyState !== 4)
44 // Allow a status of 0 for easier testing with local files.
45 if (!this.status || this.status === 200)
47 else if (errorCallback)
51 if (method === 'GET' && queryParameters)
52 url = addQueryParametersToURL(url, queryParameters);
54 xhr.open(method, url);
56 if (method === 'GET') {
61 var data = urlEncodeQueryParameters(queryParameters);
62 xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
66 function getResource(url, callback, errorCallback) {
67 fetchResource(url, 'GET', null, callback, errorCallback);
70 function urlEncodeQueryParameters(queryParameters) {
71 var encodedParameters = Object.keys(queryParameters).map(function(key) {
72 return key + '=' + encodeURIComponent(queryParameters[key]);
74 return encodedParameters.join('&');
77 function addQueryParametersToURL(url, queryParameters) {
78 if (url.indexOf('?') < 0)
83 return url + urlEncodeQueryParameters(queryParameters);
86 function longestCommonPathPrefix(paths) {
87 const separator = '/';
89 var splitPaths = paths.map(function(path) { return path.split(separator) });
90 var firstSplitPath = splitPaths.shift();
93 for (var i = 0; i < firstSplitPath.length; ++i) {
94 if (!splitPaths.every(function(splitPath) { return splitPath[i] === firstSplitPath[i] }))
96 result.push(firstSplitPath[i]);
101 return result.join(separator);
104 function removePathExtension(string) {
105 var dotIndex = string.lastIndexOf('.');
108 return string.substring(0, dotIndex);
111 function sorted(array, sortFunction) {
112 var newArray = array.slice();
113 newArray.sort(sortFunction);
117 Array.prototype.contains = function(value) {
118 return this.indexOf(value) >= 0;
121 Array.prototype.findFirst = function(predicate) {
122 for (var i = 0; i < this.length; ++i) {
123 if (predicate(this[i]))
129 Array.prototype.findLast = function(predicate) {
130 for (var i = this.length - 1; i >= 0; --i) {
131 if (predicate(this[i]))
137 Array.prototype.last = function() {
140 return this[this.length - 1];
143 Element.prototype.hasStyleClass = function(klass) {
144 var regex = new RegExp('\\b' + klass + '\\b');
145 return regex.test(this.className);
148 Element.prototype.addStyleClass = function(klass) {
149 if (this.hasStyleClass(klass))
151 this.className += ' ' + klass;
154 Element.prototype.removeStyleClass = function(klass) {
155 var regex = new RegExp('\\b' + klass + '\\b', 'g');
156 this.className = this.className.replace(regex, '');
159 Element.prototype.toggleStyleClass = function(klass) {
160 if (this.hasStyleClass(klass))
161 this.removeStyleClass(klass);
163 this.addStyleClass(klass);
166 Node.prototype.appendChildren = function(children) {
167 for (var i = 0; i < children.length; ++i)
168 this.appendChild(children[i]);
171 Node.prototype.removeAllChildren = function() {
172 while (this.firstChild)
173 this.removeChild(this.firstChild);
176 String.prototype.contains = function(substring) {
177 return this.indexOf(substring) >= 0;