Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #9 from jonsterling/ready/8
Browse files Browse the repository at this point in the history
Bump deps; implement detectInputTypeSupport in terms of purescript-dom
  • Loading branch information
garyb committed Oct 1, 2015
2 parents 5eedadb + a609f6d commit 916f7ce
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
5 changes: 3 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"purescript-base": "^0.2.0",
"purescript-exceptions": "^0.3.0",
"purescript-prelude": "^0.1.2",
"purescript-dom": "^0.1.2",
"purescript-maps": "^0.4.0"
"purescript-dom": "^0.2.6",
"purescript-maps": "^0.5.0",
"purescript-refs": "^0.2.0"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"devDependencies": {
"gulp": "^3.9.0",
"gulp-purescript": "^0.5.0",
"purescript": "^0.7.1",
"purescript": "^0.7.4",
"webpack-stream": "^2.1.0"
},
"dependencies": {
Expand Down
23 changes: 3 additions & 20 deletions src/DOM/BrowserFeatures/Detectors.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
// module DOM.BrowserFeatures.Detectors

var _inputTypeSupportMemoTable = {};

exports._detectInputTypeSupport = function(type) {
return function() {
if (_inputTypeSupportMemoTable.hasOwnProperty(type)) {
return _inputTypeSupportMemoTable[type];
}

var el = document.createElement("input");

try {
el.setAttribute("type", type);
} catch (exn) {
return false;
}

var result = el.type === type;
_inputTypeSupportMemoTable[type] = result;

return result;
exports._getTypeProperty = function (el) {
return function () {
return el.type;
};
};
45 changes: 41 additions & 4 deletions src/DOM/BrowserFeatures/Detectors.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,63 @@ module DOM.BrowserFeatures.Detectors

import Prelude
import Control.Monad.Eff
import Control.Monad.Eff.Class (liftEff)
import qualified Control.Monad.Eff.Unsafe as Unsafe
import Control.Monad.Eff.Exception
import Control.Monad.Eff.Ref

import qualified Data.Array as Arr
import qualified Data.List as L
import qualified Data.Map as M
import Data.Maybe (fromMaybe)
import Data.Maybe (Maybe(..), fromMaybe)
import Data.Foldable (foldr)
import qualified Data.Nullable as Nullable
import Data.Traversable (traverse)
import Data.Tuple

import DOM
import qualified DOM.HTML as DOM
import qualified DOM.HTML.Types as DOM
import qualified DOM.Node.Types as DOM
import qualified DOM.HTML.Window as Win
import qualified DOM.Node.Document as Doc
import qualified DOM.Node.Element as Elem
import Data.BrowserFeatures
import qualified Data.BrowserFeatures.InputType as IT

foreign import _detectInputTypeSupport :: forall e. String -> Eff (dom :: DOM | e) Boolean
foreign import _getTypeProperty :: forall e. DOM.Element -> Eff (dom :: DOM | e) String

type InputTypeMap = M.Map IT.InputType Boolean

-- | This is safe, because memoization is a monotonic & universally benign
-- | effect.
memoizeEff :: forall i e o. (Ord i) => (i -> Eff e o) -> i -> Eff e o
memoizeEff f =
runPure <<< Unsafe.unsafeInterleaveEff $ do
cacheRef <- newRef M.empty
pure \i -> Unsafe.unsafeInterleaveEff $ do
cache <- readRef cacheRef
case M.lookup i cache of
Just o -> pure o
Nothing -> do
o <- Unsafe.unsafeInterleaveEff $ f i
modifyRef cacheRef (M.insert i o)
pure o

detectInputTypeSupport :: forall e. IT.InputType -> Eff (dom :: DOM | e) Boolean
detectInputTypeSupport = _detectInputTypeSupport <<< IT.renderInputType
detectInputTypeSupport =
memoizeEff \it -> do
window <- DOM.window
document <- DOM.htmlDocumentToDocument <$> Win.document window
element <- Doc.createElement "input" document

let ty = IT.renderInputType it
catchException (\_ -> pure false) $ do
Elem.setAttribute "type" ty element
ty' <- _getTypeProperty element
pure $ ty == ty'

detectInputTypeSupportMap :: forall e. Eff (dom :: DOM | e) (M.Map IT.InputType Boolean)
detectInputTypeSupportMap :: forall e. Eff (dom :: DOM | e) InputTypeMap
detectInputTypeSupportMap = M.fromList <$> traverse (\t -> Tuple t <$> detectInputTypeSupport t) inputTypes
where
inputTypes :: L.List IT.InputType
Expand Down

0 comments on commit 916f7ce

Please sign in to comment.