fix mithril-node-render

This commit is contained in:
daniel-j 2016-08-10 16:49:56 +02:00
parent b977066ce8
commit 543f9d907f
3 changed files with 50 additions and 27 deletions

View file

@ -1,8 +1,10 @@
import m from 'mithril/render/hyperscript' import hyperscript from 'mithril/render/hyperscript'
import render from './lib/mithril-node-render' import render from './lib/mithril-node-render'
import isNode from 'detect-node' import isNode from 'detect-node'
const m = hyperscript
import fetch from './fetch' import fetch from './fetch'
import { tidyOptions, youtubeKey } from './constants' import { tidyOptions, youtubeKey } from './constants'
@ -71,7 +73,7 @@ export function cleanMarkup (html, callback) {
m('img', {src: thumbnail, alt: title}) m('img', {src: thumbnail, alt: title})
), ),
m('figcaption', m('a', {href: youtubeUrl}, caption)) m('figcaption', m('a', {href: youtubeUrl}, caption))
])) ]), {strict: true})
}) })
html = html.replace('<blockquote style="margin: 10px 0px; box-sizing:border-box; -moz-box-sizing:border-box;margin-right:25px; padding: 15px;background-color: #F7F7F7;border: 1px solid #AAA;width: 50%;float:left;box-shadow: 5px 5px 0px #EEE;">', '<blockquote class="left_insert">') html = html.replace('<blockquote style="margin: 10px 0px; box-sizing:border-box; -moz-box-sizing:border-box;margin-right:25px; padding: 15px;background-color: #F7F7F7;border: 1px solid #AAA;width: 50%;float:left;box-shadow: 5px 5px 0px #EEE;">', '<blockquote class="left_insert">')

View file

@ -1,5 +1,9 @@
'use strict' 'use strict'
var VOID_TAGS = ['area', 'base', 'br', 'col', 'command', 'embed', 'hr',
'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track',
'wbr', '!doctype']
function isArray (thing) { function isArray (thing) {
return Object.prototype.toString.call(thing) === '[object Array]' return Object.prototype.toString.call(thing) === '[object Array]'
} }
@ -21,9 +25,9 @@ function escapeHtml (s, replaceDoubleQuote) {
if (typeof (s) !== 'string') { if (typeof (s) !== 'string') {
s = s + '' s = s + ''
} }
s = s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;') s = s.replace(/\&/g, '&amp;').replace(/</g, '&lt;').replace(/\>/g, '&gt;')
if (replaceDoubleQuote) { if (replaceDoubleQuote) {
return s.replace(/"/g, '&quot;') return s.replace(/\"/g, '&quot;')
} }
return s return s
} }
@ -65,12 +69,15 @@ function createAttrString (view, escapeAttributeValue) {
}).join('') }).join('')
} }
function createChildrenContent (view) { function createChildrenContent (view, options) {
if (view.text != null) {
return options.escapeString(view.text)
}
if (isArray(view.children) && !view.children.length) { if (isArray(view.children) && !view.children.length) {
return '' return ''
} }
return render(view.children) return render(view.children, options)
} }
function render (view, options) { function render (view, options) {
@ -78,7 +85,8 @@ function render (view, options) {
var defaultOptions = { var defaultOptions = {
escapeAttributeValue: escapeHtml, escapeAttributeValue: escapeHtml,
escapeString: escapeHtml escapeString: escapeHtml,
strict: false
} }
Object.keys(defaultOptions).forEach(function (key) { Object.keys(defaultOptions).forEach(function (key) {
@ -104,21 +112,31 @@ function render (view, options) {
} }
// compontent // compontent
if (view.view) { if (typeof view.tag === 'object' && view.tag.view) {
var scope = view.controller ? new view.controller() : {} var compontent = view.tag
var result = render(view.view(scope), options) var node = view
if (scope.onunload) { if (compontent.oninit) {
scope.onunload() compontent.oninit(node)
}
var result = render(compontent.view(node), options)
if (compontent.onremove) {
compontent.onremove(node)
} }
return result return result
} }
if (view.$trusted) { if (view.tag === '<') {
return '' + view return '' + view.children
} }
var children = createChildrenContent(view) var children = createChildrenContent(view, options)
if (!children) { if (view.tag === '#') {
return '<' + view.tag + createAttrString(view, options.escapeAttributeValue) + '/>' return options.escapeString(children)
}
if (view.tag === '[') {
return '' + children
}
if (!children && (options.strict || VOID_TAGS.indexOf(view.tag.toLowerCase()) >= 0)) {
return '<' + view.tag + createAttrString(view, options.escapeAttributeValue) + (options.strict ? '/' : '') + '>'
} }
return [ return [
'<', view.tag, createAttrString(view, options.escapeAttributeValue), '>', '<', view.tag, createAttrString(view, options.escapeAttributeValue), '>',

View file

@ -1,5 +1,5 @@
import m from 'mithril/render/hyperscript' import hyperscript from 'mithril/render/hyperscript'
import trust from 'mithril/render/trust' import trust from 'mithril/render/trust'
import render from './lib/mithril-node-render' import render from './lib/mithril-node-render'
import { pd as pretty } from 'pretty-data' import { pd as pretty } from 'pretty-data'
@ -8,6 +8,9 @@ import zeroFill from 'zero-fill'
import { cleanMarkup } from './cleanMarkup' import { cleanMarkup } from './cleanMarkup'
import { NS } from './constants' import { NS } from './constants'
const m = hyperscript
m.trust = trust
function nth (d) { function nth (d) {
if (d > 3 && d < 21) return 'th' if (d > 3 && d < 21) return 'th'
switch (d % 10) { switch (d % 10) {
@ -42,8 +45,8 @@ export function createChapter (ch, html, callback) {
chapter = chapter.substring(0, pos) chapter = chapter.substring(0, pos)
let sections = [ let sections = [
m('div#chapter_container', trust(chapter)), m('div#chapter_container', m.trust(chapter)),
authorNotes ? m('div#author_notes', {className: authorNotesPos < chapterPos ? 'top' : 'bottom'}, trust(authorNotes)) : null authorNotes ? m('div#author_notes', {className: authorNotesPos < chapterPos ? 'top' : 'bottom'}, m.trust(authorNotes)) : null
] ]
if (authorNotes && authorNotesPos < chapterPos) { if (authorNotes && authorNotesPos < chapterPos) {
@ -59,7 +62,7 @@ export function createChapter (ch, html, callback) {
]), ]),
m('body', sections) m('body', sections)
]) ])
) , {strict: true})
cleanMarkup(chapterPage, (html) => { cleanMarkup(chapterPage, (html) => {
callback(html) callback(html)
@ -124,7 +127,7 @@ export function createOpf (ffc) {
m('reference', {type: 'toc', title: 'Contents', href: 'Text/nav.xhtml'}) m('reference', {type: 'toc', title: 'Contents', href: 'Text/nav.xhtml'})
]) ])
]) ])
)) , {strict: true}))
// console.log(contentOpf) // console.log(contentOpf)
return contentOpf return contentOpf
} }
@ -157,7 +160,7 @@ export function createNcx (ffc) {
[ch.title, 'Text/chapter_' + zeroFill(3, num + 1) + '.xhtml'] [ch.title, 'Text/chapter_' + zeroFill(3, num + 1) + '.xhtml']
)))) ))))
]) ])
)) , {strict: true}))
// console.log(tocNcx) // console.log(tocNcx)
return tocNcx return tocNcx
} }
@ -181,7 +184,7 @@ export function createNav (ffc) {
]) ])
]) ])
]) ])
)) , {strict: true}))
// console.log(navDocument) // console.log(navDocument)
return navDocument return navDocument
} }
@ -210,7 +213,7 @@ export function createCoverPage (coverFilename, w, h) {
]), ]),
m('body', {'epub:type': 'cover'}, body) m('body', {'epub:type': 'cover'}, body)
]) ])
)) , {strict: true}))
// console.log(coverPage) // console.log(coverPage)
return coverPage return coverPage
} }
@ -250,7 +253,7 @@ export function createTitlePage (ffc) {
'This story is a sequel to ', 'This story is a sequel to ',
m('a', {href: ffc.storyInfo.prequel.url}, ffc.storyInfo.prequel.title) m('a', {href: ffc.storyInfo.prequel.url}, ffc.storyInfo.prequel.title)
]), m('hr')] : null, ]), m('hr')] : null,
m('#description', trust(ffc.storyInfo.description)), m('#description', m.trust(ffc.storyInfo.description)),
m('hr'), m('hr'),
m('.extra_story_data', [ m('.extra_story_data', [
ffc.storyInfo.publishDate && dateBox('First Published', new Date(ffc.storyInfo.publishDate * 1000)), ffc.storyInfo.publishDate && dateBox('First Published', new Date(ffc.storyInfo.publishDate * 1000)),
@ -261,7 +264,7 @@ export function createTitlePage (ffc) {
]) ])
]) ])
]) ])
)) , {strict: true}))
// console.log(titlePage) // console.log(titlePage)
return titlePage return titlePage
} }