From 507c358e91192e90b1ca47121a018aa8694ef4a8 Mon Sep 17 00:00:00 2001 From: Justin Le Date: Sat, 19 Mar 2016 20:21:26 -0700 Subject: [PATCH 1/3] FFI for DOMTokenList --- src/DOM/Node/DOMTokenList.js | 79 ++++++++++++++++++++++++++++++++++ src/DOM/Node/DOMTokenList.purs | 23 ++++++++++ src/DOM/Node/Element.js | 6 +++ src/DOM/Node/Element.purs | 3 ++ 4 files changed, 111 insertions(+) create mode 100644 src/DOM/Node/DOMTokenList.js create mode 100644 src/DOM/Node/DOMTokenList.purs diff --git a/src/DOM/Node/DOMTokenList.js b/src/DOM/Node/DOMTokenList.js new file mode 100644 index 0000000..aead198 --- /dev/null +++ b/src/DOM/Node/DOMTokenList.js @@ -0,0 +1,79 @@ +/* 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(tokens); + } + } +} + +exports.remove = function(tokens) { + return function (list) { + return function () { + return list.remove.apply(tokens); + } + } +} + +exports.toggle = function(token) { + return function (list) { + return function () { + return list.toggle(token); + } + } +} + +exports.toggleForce = function(token) { + return function (force) { + return function (list) { + return function () { + return list.toggle(token, force); + } + } + } +} + +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); + } + } +} + diff --git a/src/DOM/Node/DOMTokenList.purs b/src/DOM/Node/DOMTokenList.purs new file mode 100644 index 0000000..c5c8674 --- /dev/null +++ b/src/DOM/Node/DOMTokenList.purs @@ -0,0 +1,23 @@ +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 items in a DOMTokenList. +foreign import length :: forall eff. DOMTokenList -> Eff (dom :: DOM | eff) Int + +foreign import item :: forall eff. Int -> DOMTokenList -> Eff (dom :: DOM | eff) (Nullable String) +foreign import contains :: forall eff. String -> DOMTokenList -> Eff (dom :: DOM | eff) Int +foreign import add :: forall eff. Array String -> DOMTokenList -> Eff (dom :: DOM | eff) Unit +foreign import remove :: forall eff. Array String -> DOMTokenList -> Eff (dom :: DOM | eff) Unit +foreign import toggle :: forall eff. String -> DOMTokenList -> Eff (dom :: DOM | eff) Boolean +foreign import toggleForce :: forall eff. String -> Boolean -> DOMTokenList -> Eff (dom :: DOM | eff) Boolean +foreign import replace :: forall eff. String -> String -> DOMTokenList -> Eff (dom :: DOM | eff) Unit +foreign import supports :: forall eff. String -> DOMTokenList -> Eff (dom :: DOM | eff) Boolean diff --git a/src/DOM/Node/Element.js b/src/DOM/Node/Element.js index 9d4e3cf..7e55439 100644 --- a/src/DOM/Node/Element.js +++ b/src/DOM/Node/Element.js @@ -88,3 +88,9 @@ exports.getAttribute = function (name) { }; }; }; + +exports.classList = function (element) { + return function () { + return element.classList; + }; +}; diff --git a/src/DOM/Node/Element.purs b/src/DOM/Node/Element.purs index b4842e9..6d4c047 100644 --- a/src/DOM/Node/Element.purs +++ b/src/DOM/Node/Element.purs @@ -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 From 79a8e387a199a2935e8ce638c2f126f68fe2c462 Mon Sep 17 00:00:00 2001 From: Justin Le Date: Sat, 19 Mar 2016 21:27:16 -0700 Subject: [PATCH 2/3] improper invocation of 'apply' --- src/DOM/Node/DOMTokenList.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DOM/Node/DOMTokenList.js b/src/DOM/Node/DOMTokenList.js index aead198..e2d3d99 100644 --- a/src/DOM/Node/DOMTokenList.js +++ b/src/DOM/Node/DOMTokenList.js @@ -28,7 +28,7 @@ exports.contains = function(token) { exports.add = function(tokens) { return function (list) { return function () { - return list.add.apply(tokens); + return list.add.apply(list, tokens); } } } @@ -36,7 +36,7 @@ exports.add = function(tokens) { exports.remove = function(tokens) { return function (list) { return function () { - return list.remove.apply(tokens); + return list.remove.apply(list, tokens); } } } From 3e9af21a112d6627c983ef2ad83202fc672d814d Mon Sep 17 00:00:00 2001 From: Justin Le Date: Tue, 22 Mar 2016 01:02:39 -0700 Subject: [PATCH 3/3] added documentation, cleaned up API --- src/DOM/Node/DOMTokenList.js | 10 ---------- src/DOM/Node/DOMTokenList.purs | 25 ++++++++++++++++++++++--- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/DOM/Node/DOMTokenList.js b/src/DOM/Node/DOMTokenList.js index e2d3d99..8eabd90 100644 --- a/src/DOM/Node/DOMTokenList.js +++ b/src/DOM/Node/DOMTokenList.js @@ -49,16 +49,6 @@ exports.toggle = function(token) { } } -exports.toggleForce = function(token) { - return function (force) { - return function (list) { - return function () { - return list.toggle(token, force); - } - } - } -} - exports.replace = function(token) { return function (newToken) { return function (list) { diff --git a/src/DOM/Node/DOMTokenList.purs b/src/DOM/Node/DOMTokenList.purs index c5c8674..0f92fe6 100644 --- a/src/DOM/Node/DOMTokenList.purs +++ b/src/DOM/Node/DOMTokenList.purs @@ -10,14 +10,33 @@ import DOM import DOM.Node.Types --- | The number of items in a DOMTokenList. +-- | 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) -foreign import contains :: forall eff. String -> DOMTokenList -> Eff (dom :: DOM | eff) Int + +-- | 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 -foreign import toggleForce :: forall eff. String -> Boolean -> 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