Skip to content

Commit 636af72

Browse files
authored
Merge pull request #4 from purescript-web/XMLSerializer
implemented XMLSerializer FFI; fixes #3
2 parents 9210a2a + a9d48e7 commit 636af72

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

src/Web/DOM/DOMParser.purs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,40 @@ import Web.DOM.Node (textContent)
2121

2222
foreign import data DOMParserType
2323

24-
--| Create a new `DOMParser`
24+
-- | Create a new `DOMParser`
2525
foreign import makeDOMParser Effect DOMParser
2626

27-
--| Parse a string with the first argumet being a string for a doctype.
28-
--| Does not capture errors; consider using other wrapper functions,
29-
--| e.g. `parseXMLFromString`.
27+
-- | Parse a string with the first argumet being a string for a doctype.
28+
-- | Does not capture errors; consider using other wrapper functions,
29+
-- | e.g. `parseXMLFromString`.
3030
foreign import parseFromString String -> String -> DOMParser -> Effect Document
3131

32-
--| Convience function to parse HTML from a string, partially applying
33-
--| `parseFromString` with "text/html"
32+
-- | Convience function to parse HTML from a string, partially applying
33+
-- | `parseFromString` with "text/html"
3434
parseHTMLFromString String -> DOMParser -> Effect (Either String Document)
3535
parseHTMLFromString s d = do
3636
doc <- parseFromString "text/html" s d
3737
errMay <- _getParserError doc
3838
pure $ returnIfNothing errMay doc
3939

40-
--| Convience function to parse SVG from a string, partially applying
41-
--| `parseFromString` with "image/svg+xml"
40+
-- | Convience function to parse SVG from a string, partially applying
41+
-- | `parseFromString` with "image/svg+xml"
4242
parseSVGFromString String -> DOMParser -> Effect (Either String Document)
4343
parseSVGFromString s d = do
4444
doc <- parseFromString "image/svg+xml" s d
4545
errMay <- _getParserError doc
4646
pure $ returnIfNothing errMay doc
4747

48-
--| Convience function to parse XML from a string, partially applying
49-
--| `parseFromString` with "application/xml"
48+
-- | Convience function to parse XML from a string, partially applying
49+
-- | `parseFromString` with "application/xml"
5050
parseXMLFromString String -> DOMParser -> Effect (Either String Document)
5151
parseXMLFromString s d = do
5252
doc <- parseFromString "application/xml" s d
5353
errMay <- _getParserError doc
5454
pure $ returnIfNothing errMay doc
5555

56-
--| Utility method for extracting Dom Parser errors from document;
57-
--| should only need to be used if calling `parseFromString` directly.
56+
-- | Utility method for extracting Dom Parser errors from document;
57+
-- | should only need to be used if calling `parseFromString` directly.
5858
_getParserError :: Document -> Effect (Maybe String)
5959
_getParserError doc = do
6060
peElems :: Array Element <- join $ map toArray $ getElementsByTagName "parsererror" doc
@@ -66,9 +66,9 @@ _getParserError doc = do
6666
Nothing -> pure $ Nothing
6767
Just nd -> map Just $ textContent nd
6868

69-
--| Like [Data.Either.note](https://pursuit.purescript.org/packages/purescript-either/docs/Data.Either#v:note),
70-
--| but with the logic reversed. Used internally for converting the
71-
--| result of `_getParserError` to an `Either`.
69+
-- | Like [Data.Either.note](https://pursuit.purescript.org/packages/purescript-either/docs/Data.Either#v:note),
70+
-- | but with the logic reversed. Used internally for converting the
71+
-- | result of `_getParserError` to an `Either`.
7272
returnIfNothing :: forall a b. Maybe a -> b -> Either a b
7373
returnIfNothing errMay val = case errMay of
7474
Nothing -> Right val

src/Web/DOM/XMLSerializer.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* Web.DOM.XMLSerializer */
2+
"use strict";
3+
4+
exports.makeXMLSerializer = function () {
5+
return new XMLSerializer();
6+
};
7+
8+
exports.serializeToString = function (doc) {
9+
return function (xmlSerializer) {
10+
return function () { // Effect thunk
11+
return xmlSerializer.serializeToString(doc);
12+
};
13+
};
14+
};

src/Web/DOM/XMLSerializer.purs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Web.DOM.XMLSerializer
2+
( XMLSerializer
3+
, makeXMLSerializer
4+
, serializeToString
5+
) where
6+
7+
import Effect (Effect)
8+
import Web.DOM.Document (Document)
9+
10+
foreign import data XMLSerializerType
11+
12+
-- | Create a new `XMLSerializer`
13+
foreign import makeXMLSerializer Effect XMLSerializer
14+
15+
-- | The `serializeToString(root)` method must [produce an XML
16+
-- | serialization](https://www.w3.org/TR/DOM-Parsing/#dfn-concept-serialize-xml) of
17+
-- | `root` passing a value of false for the [require well-formed
18+
-- | parameter](https://www.w3.org/TR/DOM-Parsing/#dfn-concept-well-formed), and
19+
-- | return the result.
20+
foreign import serializeToString Document -> XMLSerializer -> Effect String

test/Main.purs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ module Test.Main where
22

33
import Prelude
44

5-
import Data.Either (Either, isLeft, isRight)
5+
import Data.Either (Either, fromRight, isLeft, isRight)
66
import Data.Maybe (Maybe(..))
77
import Effect (Effect)
88
import Effect.Console (log)
9+
import Partial.Unsafe (unsafePartial)
910
import Test.Data as TD
1011

1112
import Web.DOM.Document (Document)
12-
import Web.DOM.DOMParser (DOMParser, makeDOMParser, parseFromString,
13-
parseXMLFromString, _getParserError)
13+
import Web.DOM.DOMParser (DOMParser, makeDOMParser, parseFromString
14+
, parseXMLFromString, _getParserError)
15+
import Web.DOM.XMLSerializer (XMLSerializer, makeXMLSerializer
16+
, serializeToString)
1417

1518
parseNoteDocRaw :: DOMParser -> Effect Document
1619
parseNoteDocRaw = parseFromString "application/xml" TD.noteXml
@@ -43,5 +46,8 @@ main = do
4346
log $ "is Right? " <> show (isRight shouldBeRight)
4447
shouldBeLeft <- parseGarbage domParser
4548
log $ "is Left? " <> show (isLeft shouldBeLeft)
49+
xmlSrlzr <- makeXMLSerializer
50+
strFromNote <- unsafePartial $ serializeToString (fromRight shouldBeRight) xmlSrlzr
51+
log $ "serialization of note is:\n" <> strFromNote
4652

4753
log "TODO: You should add some tests."

0 commit comments

Comments
 (0)