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

Commit

Permalink
Merge branch 'master' into popstate-event
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Szamotulski committed Nov 7, 2017
2 parents d75aad0 + 9174791 commit 975a2f9
Show file tree
Hide file tree
Showing 17 changed files with 284 additions and 193 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"scripts": {
"clean": "rimraf output && rimraf .pulp-cache",
"build": "eslint src && pulp build -- --censor-lib --strict",
"test": "PHANTOM_TEST_PATH=$(pwd) pulp test --runtime phantomjs"
"test": "pulp test --runtime phantomjs"
},
"devDependencies": {
"eslint": "^3.19.0",
Expand Down
2 changes: 1 addition & 1 deletion src/DOM/Event/EventTarget.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import DOM (DOM)
import DOM.Event.Types (EventTarget, Event, EventType)

-- | A boxed function that can be used as an event listener. This is necessary
-- | due to the underling implementation of Eff functions.
-- | due to the underlying implementation of Eff functions.
foreign import data EventListener :: # Effect -> Type

-- | Creates an EventListener from a normal PureScript Eff function.
Expand Down
10 changes: 10 additions & 0 deletions src/DOM/File/Blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@
exports.typeImpl = function (blob) { return blob.type; };

exports.size = function (blob) { return blob.size; };

exports.slice = function (contentType) {
return function (start) {
return function (end) {
return function (blob) {
return blob.slice(start, end, contentType);
};
};
};
};
47 changes: 45 additions & 2 deletions src/DOM/File/Blob.purs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
module DOM.File.Blob
( type_
, size
, StartByte(..)
, EndByte(..)
, idxFromInt
, idxFromNumber
, ByteIdx
, slice
, slice'
) where

import Prelude ((==))
import DOM.File.Types (Blob)
import Data.Int (toNumber)
import Data.Maybe (Maybe(..))
import Data.MediaType (MediaType(..))
import DOM.File.Types (Blob)
import Math (round)
import Prelude ((==), (>>>))
import Unsafe.Coerce (unsafeCoerce)

foreign import typeImpl :: Blob -> String

Expand All @@ -23,3 +33,36 @@ type_ blob =

-- | The size (in bytes) of the data contained in the `Blob`.
foreign import size :: Blob -> Number

-- | An index into the Blob indicating the first byte to include in the new Blob.
-- | If you specify a negative value, it's treated as an offset from the end of the
-- | string toward the beginning. For example, -10 would be the 10th from last byte
-- | in the Blob. If you specify a value for start that is larger than the size
-- | of the source Blob, the returned Blob has size 0 and contains no data.
newtype StartByte = StartByte ByteIdx

-- | An index into the Blob indicating the first byte that will *not* be included
-- | in the new Blob (i.e. the byte exactly at this index is not included).
-- | If you specify a negative value, it's treated as an offset from the end of
-- | the string toward the beginning. For example, -10 would be the 10th from
-- | last byte in the Blob. The default value is size.
newtype EndByte = EndByte ByteIdx

foreign import data ByteIdx :: Type

-- | Creates `ByteIdx` from `Int` value
idxFromInt :: Int -> ByteIdx
idxFromInt = toNumber >>> unsafeCoerce

-- | Creates `ByteIdx` from `Number` value using `Math.round`.
idxFromNumber :: Number -> ByteIdx
idxFromNumber = round >>> unsafeCoerce

-- | Creates a new `Blob` object (with specified `MediaType`), containing the
-- | data in the specified range of bytes of the source Blob, by setting .
foreign import slice MediaType -> StartByte -> EndByte -> Blob -> Blob

-- | Creates a new `Blob` object containing the data in the specified range
-- | of bytes of the source Blob.
slice' StartByte -> EndByte -> Blob -> Blob
slice' = slice (MediaType "")
6 changes: 6 additions & 0 deletions src/DOM/HTML/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ exports._body = function (doc) {
return doc.body;
};
};

exports._readyState = function (doc) {
return function () {
return doc.readyState;
};
};
15 changes: 13 additions & 2 deletions src/DOM/HTML/Document.purs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
module DOM.HTML.Document
( body
, readyState
, module Exports
) where

import Prelude

import Control.Monad.Eff (Eff)
import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toMaybe)
import DOM (DOM)
import DOM.HTML.Document.ReadyState (ReadyState(..)) as Exports
import DOM.HTML.Document.ReadyState (ReadyState, parseReadyState)
import DOM.HTML.Types (HTMLElement, HTMLDocument)
import Data.Maybe (Maybe, fromJust)
import Data.Nullable (Nullable, toMaybe)
import Partial.Unsafe (unsafePartial)

foreign import _body :: forall eff. HTMLDocument -> Eff (dom :: DOM | eff) (Nullable HTMLElement)

body :: forall eff. HTMLDocument -> Eff (dom :: DOM | eff) (Maybe HTMLElement)
body = map toMaybe <<< _body

foreign import _readyState :: forall eff. HTMLDocument -> Eff (dom :: DOM | eff) String

readyState :: forall eff. HTMLDocument -> Eff (dom :: DOM | eff) ReadyState
readyState = map (unsafePartial fromJust <<< parseReadyState) <<< _readyState
60 changes: 60 additions & 0 deletions src/DOM/HTML/Document/ReadyState.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module DOM.HTML.Document.ReadyState where

import Prelude
import Data.Enum (class Enum, class BoundedEnum, Cardinality(..), defaultSucc, defaultPred)
import Data.Maybe (Maybe(..))

data ReadyState
= Loading
| Interactive
| Complete

derive instance eqReadyState :: Eq ReadyState
derive instance ordReadyState :: Ord ReadyState

instance showReadyState :: Show ReadyState where
show = case _ of
Loading -> "Loading"
Interactive -> "Interactive"
Complete -> "Complete"

printReadyState :: ReadyState -> String
printReadyState = case _ of
Loading -> "loading"
Interactive -> "interactive"
Complete -> "complete"

parseReadyState :: String -> Maybe ReadyState
parseReadyState = case _ of
"loading" -> Just Loading
"interactive" -> Just Interactive
"complete" -> Just Complete
_ -> Nothing

instance boundedReadyState :: Bounded ReadyState where
bottom = Loading
top = Complete

instance enumReadyState :: Enum ReadyState where
succ = defaultSucc toEnumReadyState fromEnumReadyState
pred = defaultPred toEnumReadyState fromEnumReadyState

instance boundedEnumReadyState :: BoundedEnum ReadyState where
cardinality = Cardinality 3
toEnum = toEnumReadyState
fromEnum = fromEnumReadyState

toEnumReadyState :: Int -> Maybe ReadyState
toEnumReadyState =
case _ of
0 -> Just Loading
1 -> Just Interactive
2 -> Just Complete
_ -> Nothing

fromEnumReadyState :: ReadyState -> Int
fromEnumReadyState =
case _ of
Loading -> 0
Interactive -> 1
Complete -> 2
61 changes: 20 additions & 41 deletions src/DOM/HTML/HTMLElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,6 @@ exports.setHidden = function (hidden) {

// ----------------------------------------------------------------------------

exports.click = function (elt) {
return function () {
return elt.click();
};
};

// ----------------------------------------------------------------------------

exports.tabIndex = function (elt) {
return function () {
return elt.tabIndex;
Expand All @@ -118,39 +110,6 @@ exports.setTabIndex = function (tabIndex) {

// ----------------------------------------------------------------------------

exports.hidden = function (elt) {
return function () {
return elt.hidden;
};
};

exports.setHidden = function (hidden) {
return function (elt) {
return function () {
elt.hidden = hidden;
return {};
};
};
};

// ----------------------------------------------------------------------------

exports.focus = function (elt) {
return function () {
return elt.focus();
};
};

// ----------------------------------------------------------------------------

exports.blur = function (elt) {
return function () {
return elt.blur();
};
};

// ----------------------------------------------------------------------------

exports.draggable = function (elt) {
return function () {
return elt.draggable;
Expand Down Expand Up @@ -206,6 +165,26 @@ exports.setSpellcheck = function (spellcheck) {
};
};

// ----------------------------------------------------------------------------

exports.click = function (elt) {
return function () {
return elt.click();
};
};

exports.focus = function (elt) {
return function () {
return elt.focus();
};
};

exports.blur = function (elt) {
return function () {
return elt.blur();
};
};

// - CSSOM ---------------------------------------------------------------------

exports.getBoundingClientRect = function (el) {
Expand Down
2 changes: 1 addition & 1 deletion src/DOM/HTML/Location.purs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ foreign import hostname :: forall eff. Location -> Eff (dom :: DOM | eff) String
foreign import setHostname :: forall eff. String -> Location -> Eff (dom :: DOM | eff) Unit

foreign import href :: forall eff. Location -> Eff (dom :: DOM | eff) String
foreign import setHref :: forall eff. String -> Location -> Eff (dom :: DOM | eff) String
foreign import setHref :: forall eff. String -> Location -> Eff (dom :: DOM | eff) Unit

foreign import origin :: forall eff. Location -> Eff (dom :: DOM | eff) String
foreign import setOrigin :: forall eff. String -> Location -> Eff (dom :: DOM | eff) Unit
Expand Down
2 changes: 1 addition & 1 deletion src/DOM/HTML/Window.purs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ foreign import moveBy :: forall eff. Int -> Int -> Window -> Eff (window :: WIND
foreign import moveTo :: forall eff. Int -> Int -> Window -> Eff (window :: WINDOW | eff) Unit

open :: forall eff. String -> String -> String -> Window -> Eff (window :: WINDOW | eff) (Maybe Window)
open window url' name features = toMaybe <$> _open window url' name features
open url' name features window = toMaybe <$> _open url' name features window

foreign import _open
:: forall eff
Expand Down
6 changes: 6 additions & 0 deletions src/DOM/Node/NodeList.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ exports.length = function (list) {
};
};

exports.toArray = function (list) {
return function () {
return [].slice.call(list);
};
};

exports._item = function (index) {
return function (list) {
return function () {
Expand Down
4 changes: 4 additions & 0 deletions src/DOM/Node/NodeList.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module DOM.Node.NodeList
( length
, item
, toArray
) where

import Prelude
Expand All @@ -13,6 +14,9 @@ import DOM.Node.Types (Node, NodeList)
-- | The number of items in a NodeList.
foreign import length :: forall eff. NodeList -> Eff (dom :: DOM | eff) Int

-- | The elements of a NodeList represented in an array.
foreign import toArray :: forall eff. NodeList -> Eff (dom :: DOM | eff) (Array Node)

-- | The item in a NodeList at the specified index, or Nothing if no such node
-- | exists.
item :: forall eff. Int -> NodeList -> Eff (dom :: DOM | eff) (Maybe Node)
Expand Down
2 changes: 1 addition & 1 deletion src/DOM/Websocket/WebSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exports.url = function (ws) {

exports.readyStateImpl = function (ws) {
return function () {
return ws.readyStateImpl;
return ws.readyState;
};
};

Expand Down
18 changes: 18 additions & 0 deletions test/DOM/HTML/Document.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Test.DOM.HTML.Document where

import Prelude

import Control.Monad.Eff.Class (liftEff)
import DOM (DOM)
import DOM.HTML (window)
import DOM.HTML.Document (ReadyState(..), readyState)
import DOM.HTML.Window (document)
import Test.Unit (TestSuite, describe, it)
import Test.Unit.Assert (shouldEqual)

domHtmlDocumentTests :: forall eff. TestSuite (dom :: DOM | eff)
domHtmlDocumentTests = do
describe "readyState" do
it "should return a sensible readyState" do
rs <- liftEff $ readyState =<< document =<< window
rs `shouldEqual` Complete
Loading

0 comments on commit 975a2f9

Please sign in to comment.