Skip to content

Commit 8394ba6

Browse files
committed
Add doctype to server side rendering
1 parent 980e07e commit 8394ba6

File tree

6 files changed

+16
-13
lines changed

6 files changed

+16
-13
lines changed

src/Flame/Html/Attribute/Event.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
let messageEventData = 5,
42
rawEventData = 6;
53

src/Flame/Html/Attribute/Internal.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
let styleData = 1,
42
classData = 2,
53
propertyData = 3,

src/Flame/Renderer/Internal/Dom.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
let namespace = 'http://www.w3.org/2000/svg',
42
eventPrefix = '__flame_',
53
eventPostfix = 'updater';

src/Flame/Renderer/String.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
'use strict';
2-
31
let textNode = 1,
2+
elementNode = 2,
43
svgNode = 3,
54
fragmentNode = 4,
65
lazyNode = 5,
@@ -110,7 +109,15 @@ let booleanAttributes = new Set([
110109
]);
111110

112111
/** String rendering adapted from https://github.com/snabbdom/snabbdom-to-html */
113-
export {stringify as render_};
112+
export function render_(html) {
113+
let docType = '<!DOCTYPE html>',
114+
rendered = stringify(html);
115+
116+
if (html.nodeType === elementNode && html.tag === 'html')
117+
rendered = docType + rendered;
118+
119+
return rendered;
120+
}
114121

115122
function stringify(html) {
116123
switch (html.nodeType) {

src/Flame/Renderer/String.purs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ import Flame.Types (Html)
88
foreign import render_ :: forall message. EffectFn1 (Html message) String
99

1010
-- | Render markup into a string, useful for server-side rendering
11+
-- |
12+
-- | If the root tag is `html`, doctype is automatically added
1113
render :: forall message. Html message -> Effect String
1214
render = EU.runEffectFn1 render_

test/Main.purs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ main = AF.launchAff_ $ TSR.runSpec [consoleReporter] do
213213
]
214214
]
215215
html' <- liftEffect $ FRS.render html
216-
TSA.shouldEqual """<html><head><title>title</title></head><body><main><button>-</button><br>Test<button>+</button><svg viewBox="0 0 23 0"><path d="234" /></svg><div><div><span><a>here</a></span></div></div></main></body></html>""" html'
216+
TSA.shouldEqual """<!DOCTYPE html><html><head><title>title</title></head><body><main><button>-</button><br>Test<button>+</button><svg viewBox="0 0 23 0"><path d="234" /></svg><div><div><span><a>here</a></span></div></div></main></body></html>""" html'
217217

218218
TS.it "nested nodes with attributes" do
219219
let html = HE.html [HA.lang "en"] [
@@ -232,7 +232,7 @@ main = AF.launchAff_ $ TSR.runSpec [consoleReporter] do
232232
]
233233
]
234234
html' <- liftEffect $ FRS.render html
235-
TSA.shouldEqual """<html lang="en"><head><title>title</title></head><body id="content"><main><button style="display: block; width: 20px">-</button><br>Test<button my-attribute="myValue">+</button><hr style="border: 200px solid blue"><div><div><span><a>here</a></span></div></div></main></body></html>""" html'
235+
TSA.shouldEqual """<!DOCTYPE html><html lang="en"><head><title>title</title></head><body id="content"><main><button style="display: block; width: 20px">-</button><br>Test<button my-attribute="myValue">+</button><hr style="border: 200px solid blue"><div><div><span><a>here</a></span></div></div></main></body></html>""" html'
236236

237237
TS.it "nested nodes with properties and attributes" do
238238
let html = HE.html [HA.lang "en"] [
@@ -252,13 +252,13 @@ main = AF.launchAff_ $ TSR.runSpec [consoleReporter] do
252252
]
253253
]
254254
html' <- liftEffect $ FRS.render html
255-
TSA.shouldEqual """<html lang="en"><head disabled="disabled"><title>title</title></head><body id="content"><main><button style="display: block; width: 20px">-</button><br>Test<button my-attribute="myValue">+</button><hr style="border: 200px solid blue" autocomplete="off"><div><div><span><a autofocus="autofocus">here</a></span></div></div></main></body></html>""" html'
255+
TSA.shouldEqual """<!DOCTYPE html><html lang="en"><head disabled="disabled"><title>title</title></head><body id="content"><main><button style="display: block; width: 20px">-</button><br>Test<button my-attribute="myValue">+</button><hr style="border: 200px solid blue" autocomplete="off"><div><div><span><a autofocus="autofocus">here</a></span></div></div></main></body></html>""" html'
256256

257257
TS.describe "root node" do
258258
TS.it "root node is unchanged" do
259259
liftEffect unsafeCreateEnviroment
260260
let html = HE.div "test-div" $ HE.input [HA.id "t", HA.value "a"]
261-
state <- mountHtml' html
261+
void $ mountHtml' html
262262
rootNode <- liftEffect $ FAD.querySelector "#mount-point"
263263
TSA.shouldSatisfy rootNode DM.isJust
264264

0 commit comments

Comments
 (0)