Skip to content

Commit

Permalink
Add DOMTokenList
Browse files Browse the repository at this point in the history
Create the DOMToken list implementation (issue silexlabs#410)
  • Loading branch information
JbIPS committed Apr 4, 2014
1 parent 6a768a9 commit 61266cd
Show file tree
Hide file tree
Showing 6 changed files with 431 additions and 360 deletions.
5 changes: 3 additions & 2 deletions cocktail/core/css/CSSData.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package cocktail.core.css;

import cocktail.core.html.DOMTokenList;
import cocktail.core.geom.Matrix;
import cocktail.core.layout.LayoutData;
import cocktail.core.geom.GeomData;
Expand Down Expand Up @@ -78,12 +79,12 @@ class MatchedPseudoClassesVO {
public var hasId:Bool;
public var nodeId:String;
public var hasClasses:Bool;
public var nodeClassList:Array<String>;
public var nodeClassList:DOMTokenList;
public var nodeType:String;

public function new(hover:Bool, focus:Bool, active:Bool, link:Bool, enabled:Bool,
disabled:Bool, checked:Bool, fullscreen:Bool,
hasId:Bool, hasClasses:Bool, nodeId:String, nodeClassList:Array<String>, nodeType:String)
hasId:Bool, hasClasses:Bool, nodeId:String, nodeClassList:DOMTokenList, nodeType:String)
{
this.hover = hover;
this.focus = focus;
Expand Down
7 changes: 4 additions & 3 deletions cocktail/core/css/SelectorManager.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import cocktail.core.dom.DOMConstants;
import cocktail.core.dom.Node;
import cocktail.core.html.HTMLConstants;
import cocktail.core.html.HTMLElement;
import cocktail.core.html.DOMTokenList;
import cocktail.core.css.CSSData;

/**
Expand Down Expand Up @@ -271,8 +272,8 @@ class SelectorManager
{
//for this check the list of class of the node
case CSS_CLASS(value):
var classList:Array<String> = node.classList;
var classList:DOMTokenList = node.classList;

//here the node has no classes
if (classList == null)
{
Expand All @@ -282,7 +283,7 @@ class SelectorManager
var length:Int = classList.length;
for (i in 0...length)
{
if (value == classList[i])
if (value == classList.item(i))
{
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions cocktail/core/css/StyleManager.hx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class StyleManager
var classListLength:Int = matchedPseudoClasses.nodeClassList.length;
for (l in 0...classListLength)
{
if (matchedPseudoClasses.nodeClassList[l] == selector.firstClass)
if (matchedPseudoClasses.nodeClassList.item(l) == selector.firstClass)
{
//in this case, the selector only has a single
//class selector, so it is a match
Expand Down Expand Up @@ -506,4 +506,4 @@ class StyleManager

return _mostSpecificMatchingProperties;
}
}
}
5 changes: 3 additions & 2 deletions cocktail/core/dom/Element.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package cocktail.core.dom;

import cocktail.core.html.DOMTokenList;
import cocktail.core.html.HTMLConstants;
import cocktail.core.html.HTMLElement;

Expand Down Expand Up @@ -367,14 +368,14 @@ class Element extends Node
switch (childNode.nodeType)
{
case DOMConstants.ELEMENT_NODE:
var classList:Array<String> = childNode.classList;
var classList:DOMTokenList = childNode.classList;
if (classList != null)
{
var foundFlag:Bool = false;
var classListLength:Int = classList.length;
for (j in 0...classListLength)
{
if (classList[j] == className && foundFlag == false)
if (classList.item(j) == className && foundFlag == false)
{
elements.push(childNode);
foundFlag = true;
Expand Down
66 changes: 66 additions & 0 deletions cocktail/core/html/DOMTokenList.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cocktail.core.html;

using Lambda;

/**
* This type represents a set of space-separated tokens
**/
class DOMTokenList{

public function new(){
tokenArray = new Array<String>();
}

/**
* Length of the list. (Read-only)
**/
public var length (default, null):Int;

var tokenArray: Array<String>;

/**
* adds token to the underlying string
**/
public function add(tokens:String):Void {
tokenArray.push(tokens);
}

/**
* return true if the underlying string contains token, otherwise false
**/
public function contains(token:String):Bool {
return tokenArray.has(token);
}

/**
* returns an item in the list by its index
**/
public function item(index:Int):String {
return tokenArray[index];
}

/**
* remove token from the underlying string
**/
public function remove(tokens:String):Void {
tokenArray.remove(tokens);
}

public function toString():String {
return tokenArray.join(" ");
}

/**
* removes token from string and returns false. If token doesn't exist it's added and the function returns true
* // TODO I don't know what force param do
**/
public function toggle(token:String, ?force:Bool):Bool {
if(tokenArray.remove(token))
return false;
else{
add(token);
return true;
}

}
}
Loading

0 comments on commit 61266cd

Please sign in to comment.