Skip to content
This repository has been archived by the owner on Oct 4, 2020. It is now read-only.

FFI bindings/API for DOMTokenList interface #44

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/DOM/Node/DOMTokenList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* global exports */
"use strict";

// module DOM.Node.DOMTokenList

exports.length = function (list) {
return function () {
return list.length;
};
};

exports.item = function (index) {
return function (list) {
return function () {
return list.item(index);
};
};
};

exports.contains = function(token) {
return function (list) {
return function () {
return list.contains(token);
}
}
}

exports.add = function(tokens) {
return function (list) {
return function () {
return list.add.apply(list, tokens);
}
}
}

exports.remove = function(tokens) {
return function (list) {
return function () {
return list.remove.apply(list, tokens);
}
}
}

exports.toggle = function(token) {
return function (list) {
return function () {
return list.toggle(token);
}
}
}

exports.replace = function(token) {
return function (newToken) {
return function (list) {
return function () {
return list.replace(token, newToken);
}
}
}
}

exports.supports = function(token) {
return function (list) {
return function () {
return list.supports(token);
}
}
}

42 changes: 42 additions & 0 deletions src/DOM/Node/DOMTokenList.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module DOM.Node.DOMTokenList where

import Prelude

import Control.Monad.Eff (Eff())

import Data.Nullable (Nullable())

import DOM
import DOM.Node.Types


-- | The number of tokens in a DOMTokenList.
foreign import length :: forall eff. DOMTokenList -> Eff (dom :: DOM | eff) Int

-- | The token in a DOMTokenList at the specified index, or null if no such
-- | token exists.
foreign import item :: forall eff. Int -> DOMTokenList -> Eff (dom :: DOM | eff) (Nullable String)

-- | Test whether or not the DOMTokenList contains the given token.
foreign import contains :: forall eff. String -> DOMTokenList -> Eff (dom :: DOM | eff) Boolean

-- | Add the given array tokens to the DOMTokenList; duplicate items are
-- | automatically pruned.
foreign import add :: forall eff. Array String -> DOMTokenList -> Eff (dom :: DOM | eff) Unit

-- | Remove the given tokens from the DOMTokenList if they exist in it.
foreign import remove :: forall eff. Array String -> DOMTokenList -> Eff (dom :: DOM | eff) Unit

-- | Toggle the presence of the given token in the DOMTokenList. Returns
-- | whether or not the token was originally in the DOMTokenList.
foreign import toggle :: forall eff. String -> DOMTokenList -> Eff (dom :: DOM | eff) Boolean

-- | Replaces an occurrence of the token string with the first token. If the
-- | first token is not in the DOMTokenList, nothing happens.
foreign import replace :: forall eff. String -> String -> DOMTokenList -> Eff (dom :: DOM | eff) Unit

-- | Returns whether or not the DOMTokenList supports containing the given token.
-- |
-- | TODO: This doesn't actually do this; it actually always returns false,
-- | unless the token is not valid. If it isn't, it throws an exception.
foreign import supports :: forall eff. String -> DOMTokenList -> Eff (dom :: DOM | eff) Boolean
6 changes: 6 additions & 0 deletions src/DOM/Node/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,9 @@ exports.getAttribute = function (name) {
};
};
};

exports.classList = function (element) {
return function () {
return element.classList;
};
};
3 changes: 3 additions & 0 deletions src/DOM/Node/Element.purs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ foreign import getElementsByClassName :: forall eff. String -> Element -> Eff (d

foreign import setAttribute :: forall eff. String -> String -> Element -> Eff (dom :: DOM | eff) Unit
foreign import getAttribute :: forall eff. String -> Element -> Eff (dom :: DOM | eff) (Nullable String)

-- | A (mutatable) DOMTokenList representing the classes of the element.
foreign import classList :: forall eff. Element -> Eff (dom :: DOM | eff) DOMTokenList