diff options
author | arv@chromium.org <arv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-30 00:41:37 +0000 |
---|---|---|
committer | arv@chromium.org <arv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-30 00:41:37 +0000 |
commit | a02afd36072896ba584f32664cc8e4bc1889bae0 (patch) | |
tree | d202bf35af23f91f629df2680d3e2af3218f9b8d /chrome/browser/resources/shared | |
parent | e030e767b4f0fef8ef1de21ebe977c2a4dec840b (diff) | |
download | chromium_src-a02afd36072896ba584f32664cc8e4bc1889bae0.zip chromium_src-a02afd36072896ba584f32664cc8e4bc1889bae0.tar.gz chromium_src-a02afd36072896ba584f32664cc8e4bc1889bae0.tar.bz2 |
NTP - Refactor the most visited code to uncouple it from the rest of the NTP.
The goal of this refactoring is to allow splitting the different parts of the NTP into different reusable components.
BUG=None
TEST=Manually
Review URL: http://codereview.chromium.org/1695022
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@46021 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/resources/shared')
-rw-r--r-- | chrome/browser/resources/shared/js/class_list.js | 94 | ||||
-rw-r--r-- | chrome/browser/resources/shared/js/class_list_test.html | 91 |
2 files changed, 185 insertions, 0 deletions
diff --git a/chrome/browser/resources/shared/js/class_list.js b/chrome/browser/resources/shared/js/class_list.js new file mode 100644 index 0000000..edfd98f --- /dev/null +++ b/chrome/browser/resources/shared/js/class_list.js @@ -0,0 +1,94 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/** + * @fileoverview This implements the HTML5 HTMLElement classList property. + */ + +// TODO(arv): Remove this when classList is available in WebKit. +// https://bugs.webkit.org/show_bug.cgi?id=20709 + +if (typeof document.createElement('div').classList == 'undefined') { + +function DOMTokenList(el) { + this.el_ = el; +} + +(function() { + + var re = /\s+/; + + function split(el) { + var cn = el.className.replace(/^\s+|\s$/g, ''); + if (cn == '') + return []; + return cn.split(re); + } + + function DOMException_(code) { + this.code = code; + } + DOMException_.prototype = DOMException.prototype; + + function assertValidToken(s) { + if (s == '') + throw new DOMException_(DOMException.SYNTAX_ERR); + if (re.test(s)) + throw new DOMException_(DOMException.INVALID_CHARACTER_ERR); + } + + DOMTokenList.prototype = { + contains: function(token) { + assertValidToken(token); + return split(this.el_).indexOf(token) >= 0; + }, + add: function(token) { + assertValidToken(token); + if (this.contains(token)) + return; + var cn = this.el_.className; + this.el_.className += (cn == '' || re.test(cn.slice(-1)) ? '' : ' ') + + token; + }, + remove: function(token) { + assertValidToken(token); + var names = split(this.el_); + var s = []; + for (var i = 0; i < names.length; i++) { + if (names[i] != token) + s.push(names[i]) + } + this.el_.className = s.join(' '); + }, + toggle: function(token) { + assertValidToken(token); + if (this.contains(token)) { + this.remove(token); + return false; + } else { + this.add(token); + return true; + } + }, + get length() { + return split(this.el_).length; + }, + item: function(index) { + return split(this.el_)[index]; + } + }; + +})(); + +HTMLElement.prototype.__defineGetter__('classList', function() { + var tl = new DOMTokenList(this); + // Override so that we reuse the same DOMTokenList and so that + // el.classList == el.classList + this.__defineGetter__('classList', function() { + return tl; + }); + return tl; +}); + +} // if diff --git a/chrome/browser/resources/shared/js/class_list_test.html b/chrome/browser/resources/shared/js/class_list_test.html new file mode 100644 index 0000000..c84b8ce --- /dev/null +++ b/chrome/browser/resources/shared/js/class_list_test.html @@ -0,0 +1,91 @@ +<!DOCTYPE html> +<html> +<head> +<title></title> +<script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.js"></script> +<script src="class_list.js"></script> +<script> + +goog.require('goog.testing.jsunit'); + +</script> +</head> +<body> + +<script> + +var el = document.body; + +function testLength() { + el.className = 'a b'; + assertEquals(2, el.classList.length); + el.setAttribute('class', 'a b c'); + assertEquals(3, el.classList.length); +} + +function testItem() { + el.className = 'a b'; + assertEquals('a', el.classList.item(0)); + assertEquals('b', el.classList.item(1)); + el.setAttribute('class', 'a b c'); + assertEquals('a', el.classList.item(0)); + assertEquals('b', el.classList.item(1)); + assertEquals('c', el.classList.item(2)); +} + +function testContains() { + el.className = 'a b'; + assertTrue(el.classList.contains('a')); + assertTrue(el.classList.contains('b')); + assertFalse(el.classList.contains('c')); + + assertEquals('b', el.classList.item(1)); + el.setAttribute('class', 'a b c'); + assertTrue(el.classList.contains('a')); + assertTrue(el.classList.contains('b')); + assertTrue(el.classList.contains('c')); +} + +function testToken() { + assertThrows(function() { + el.classList.add(''); + }); + assertThrows(function() { + el.classList.add(' '); + }); + assertThrows(function() { + el.classList.add('\t'); + }); + assertThrows(function() { + el.classList.add('\n'); + }); +} + +function testAdd() { + el.className = 'a b'; + el.classList.add('a'); + assertEquals('a b', el.className); + el.classList.add('c'); + assertEquals('a b c', el.className); +} + +function testRemove() { + el.className = 'a b'; + el.classList.remove('b'); + assertEquals('a', el.className); + el.classList.remove('a'); + assertEquals('', el.className); +} + +function testToggle() { + el.className = 'a b'; + assertFalse(el.classList.toggle('a')); + assertEquals('b', el.className); + assertTrue(el.classList.toggle('a')); + assertEquals('b a', el.className); +} + +</script> + +</body> +</html> |