git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 08/13] gitweb.js: Provide getElementsByClassName method (if it not exists)

From
Jakub Narebski <jnareb@gmail.com>
Date
Apr 28, 2011, 19:04 UTC
Message-ID
<1304017451-12283-9-git-send-email-jnareb@gmail.com>
In-Reply-To
<1304017451-12283-1-git-send-email-jnareb@gmail.com>

The code is simplified and does not support full specification of native getElementsByClassName method, but implements just subset that would be enough for gitweb, supporting only single class name.

Signed-off-by: John 'Warthog9' Hawley <warthog9@eaglescrag.net>
Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
This patch is unchanged from first version of this series.

Instead of providing findElementsByClassName function like in J.H. patch, which could use getElementsByClassName if present (it didn't cache the check for document.getElementsByClassName, but I guess any performance gain is negligible), this version adds getElementsByClassName method to 'document' if it doesn't exist.

Note that in this implementation we avoid 'push' method of Array instances, as supposedly not all web browsers implement it, and it is easy to work around.

In this implementation of document.getElementsByClassName we use 'document.all' if it exists instead of 'document.getElementsByTagName("*")'; this avoids issue with getElementsByTagName("*") that supposedly some versions of IE have. This workaround is not present in original patch by J.H.

 gitweb/static/js/lib/common-lib.js |   51 ++++++++++++++++++++++++++++++++++++
 1 files changed, 51 insertions(+), 0 deletions(-)
diff --git a/gitweb/static/js/lib/common-lib.js b/gitweb/static/js/lib/common-lib.js
index d6b0c0d..b371391 100644
--- a/gitweb/static/js/lib/common-lib.js
+++ b/gitweb/static/js/lib/common-lib.js
@@ -89,6 +89,57 @@ function createRequestObject() {
 
 
 /* ............................................................ */
+/* Support for legacy browsers */
+
+/**
+ * Provides getElementsByClassName method, if there is no native
+ * implementation of this method.
+ *
+ * NOTE that there are limits and differences compared to native
+ * getElementsByClassName as defined by e.g.:
+ *   https://developer.mozilla.org/en/DOM/document.getElementsByClassName
+ *   http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#dom-getelementsbyclassname
+ *   http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#dom-document-getelementsbyclassname
+ *
+ * Namely, this implementation supports only single class name as
+ * argument and not set of space-separated tokens representing classes,
+ * it returns Array of nodes rather than live NodeList, and has
+ * additional optional argument where you can limit search to given tags
+ * (via getElementsByTagName).
+ *
+ * Based on
+ *   http://code.google.com/p/getelementsbyclassname/
+ *   http://www.dustindiaz.com/getelementsbyclass/
+ *   http://stackoverflow.com/questions/1818865/do-we-have-getelementsbyclassname-in-javascript
+ *
+ * See also http://ejohn.org/blog/getelementsbyclassname-speed-comparison/
+ *
+ * @param {String} class: name of _single_ class to find
+ * @param {String} [taghint] limit search to given tags
+ * @returns {Node[]} array of matching elements
+ */
+if (!('getElementsByClassName' in document)) {
+	document.getElementsByClassName = function (classname, taghint) {
+		taghint = taghint || "*";
+		var elements = (taghint === "*" && document.all) ?
+		               document.all :
+		               document.getElementsByTagName(taghint);
+		var pattern = new RegExp("(^|\\s)" + classname + "(\\s|$)");
+		var matches= [];
+		for (var i = 0, j = 0, n = elements.length; i < n; i++) {
+			var el= elements[i];
+			if (el.className && pattern.test(el.className)) {
+				// matches.push(el);
+				matches[j] = el;
+				j++;
+			}
+		}
+		return matches;
+	};
+} // end if
+
+
+/* ............................................................ */
 /* unquoting/unescaping filenames */
 
 /**#@+
-- 
1.7.3
Previous: Jakub NarebskiNext: Jakub Narebski
Message 9 of 14 in “[PATCHv3 00/13] gitweb: Change timezone in dates using JavaScript”
  1. Jakub NarebskiApr 28, 2011
  2. 01/13 git-instaweb: Simplify build dependency on gitwebJakub Narebski, Apr 28, 2011
  3. 02/13 Remove gitweb/gitweb.cgi and other legacy targets from main MakefileJakub Narebski, Apr 28, 2011
  4. 03/13 gitweb: Split JavaScript for maintability, combining on buildJakub Narebski, Apr 28, 2011
  5. 04/13 gitweb.js: Update and improve comments in JavaScript filesJakub Narebski, Apr 28, 2011
  6. 05/13 gitweb.js: Provide default values for padding in padLeftStr and padLeftJakub Narebski, Apr 28, 2011
  7. 06/13 gitweb.js: Extract and improve datetime handlingJakub Narebski, Apr 28, 2011
  8. 07/13 gitweb.js: Introduce code to handle cookies from JavaScriptJakub Narebski, Apr 28, 2011
  9. 08/13 gitweb.js: Provide getElementsByClassName method (if it not exists)Jakub Narebski, Apr 28, 2011
  10. 09/13 gitweb: Refactor generating of long dates into format_timestamp_htmlJakub Narebski, Apr 28, 2011
  11. 10/13 gitweb: Unify the way long timestamp is displayedJakub Narebski, Apr 28, 2011
  12. 11/13 gitweb: JavaScript ability to adjust time based on timezoneJakub Narebski, Apr 28, 2011
  13. 12/13 gitweb.js: Add UI for selecting common timezone to display datesJakub Narebski, Apr 28, 2011
  14. 13/13 gitweb: Make JavaScript ability to adjust timezones configurableJakub Narebski, Apr 28, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.