From bede72ab6378f6ba41d0c54615819363a44de394 Mon Sep 17 00:00:00 2001 From: Jonathan Sterling Date: Thu, 1 Oct 2015 14:10:52 -0600 Subject: [PATCH 1/2] Bump deps; implement detectInputTypeSupport in terms of purescript-dom Fix #8 --- bower.json | 5 +-- src/DOM/BrowserFeatures/Detectors.js | 23 ++----------- src/DOM/BrowserFeatures/Detectors.purs | 45 +++++++++++++++++++++++--- 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/bower.json b/bower.json index d3c605f..52f000b 100644 --- a/bower.json +++ b/bower.json @@ -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" } } diff --git a/src/DOM/BrowserFeatures/Detectors.js b/src/DOM/BrowserFeatures/Detectors.js index 649ad18..848f4d3 100644 --- a/src/DOM/BrowserFeatures/Detectors.js +++ b/src/DOM/BrowserFeatures/Detectors.js @@ -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; }; }; diff --git a/src/DOM/BrowserFeatures/Detectors.purs b/src/DOM/BrowserFeatures/Detectors.purs index 1a7c295..4ff923f 100644 --- a/src/DOM/BrowserFeatures/Detectors.purs +++ b/src/DOM/BrowserFeatures/Detectors.purs @@ -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 From a609f6dee844f62ca2815d0d71a04e6d22e10dbd Mon Sep 17 00:00:00 2001 From: Jonathan Sterling Date: Thu, 1 Oct 2015 15:25:46 -0600 Subject: [PATCH 2/2] [package.json] upgrade purescript compiler dep along the way --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 438e3df..33b40d2 100644 --- a/package.json +++ b/package.json @@ -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": {