-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
88 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,23 @@ | ||
module Halogen.VDom.Finders where | ||
|
||
import Prelude | ||
|
||
import Data.Either (Either(..), note) | ||
import Effect (Effect) | ||
import Web.DOM.Element (Element) | ||
import Web.DOM.ParentNode (ParentNode) | ||
import Web.DOM.ParentNode (firstElementChild, childElementCount) as DOM.ParentNode | ||
import Web.DOM.ParentNode (querySelector, QuerySelector(..)) as DOM | ||
import Data.Maybe (maybe) | ||
import Effect.Exception (error, throwException) | ||
import Web.DOM (Element) | ||
import Web.DOM.Element (toParentNode) as DOM.Element | ||
|
||
findRequiredElement ∷ String → ParentNode → Effect Element | ||
findRequiredElement selector parentNode = | ||
DOM.querySelector (DOM.QuerySelector selector) parentNode | ||
>>= maybe (throwException $ error $ selector <> " not found") pure | ||
import Web.DOM.ParentNode (firstElementChild, childElementCount) as DOM.ParentNode | ||
|
||
-- | Used for hydration | ||
findRootElementInsideOfRootContainer :: Element -> Effect Element | ||
findRootElementInsideOfRootContainer container = do | ||
findElementFirstChild :: Element -> Effect (Either String Element) | ||
findElementFirstChild container = do | ||
let | ||
container' = DOM.Element.toParentNode container | ||
|
||
childrenCount <- DOM.ParentNode.childElementCount container' | ||
unless (childrenCount == 1) (throwException $ error $ "Root container should have 1 child element (aka root element; it can be Element, Keyed, Text, etc.), but actual children count is " <> show childrenCount) | ||
rootElement ← DOM.ParentNode.firstElementChild container' | ||
maybe (throwException $ error $ "Root element not found") pure rootElement | ||
where | ||
container' = DOM.Element.toParentNode container | ||
|
||
if childrenCount /= 1 | ||
then pure $ Left $ "Root container should have only 1 child element (aka root element; it can be Element, Keyed, Text, etc.), but actual children count is " <> show childrenCount | ||
else do | ||
maybeRootElement <- DOM.ParentNode.firstElementChild container' | ||
pure $ note "Root element not found" $ maybeRootElement |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters