Evan You
9 years ago
6 changed files with 80 additions and 32 deletions
@ -1,33 +1,44 @@ |
|||||
import { renderStartingTag } from './render-starting-tag' |
import { renderStartingTag } from './render-starting-tag' |
||||
|
|
||||
export function createSyncRenderer (modules, directives, isUnaryTag) { |
export function createSyncRenderer (modules, directives, isUnaryTag) { |
||||
function renderComponent (component) { |
function renderComponent (component, isRoot) { |
||||
component.$mount() |
component.$mount() |
||||
return renderNode(component._vnode) |
return renderNode(component._vnode, isRoot) |
||||
} |
} |
||||
|
|
||||
function renderNode (node) { |
function renderNode (node, isRoot) { |
||||
if (node.componentOptions) { |
if (node.componentOptions) { |
||||
node.data.hook.init(node) |
node.data.hook.init(node) |
||||
return renderComponent(node.child) |
return renderComponent(node.child, isRoot) |
||||
} else { |
} else { |
||||
return node.tag |
return node.tag |
||||
? renderElement(node) |
? renderElement(node, isRoot) |
||||
: node.text |
: node.text |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
function renderElement (el) { |
function renderElement (el, isRoot) { |
||||
|
if (isRoot) { |
||||
|
if (!el.data) el.data = {} |
||||
|
if (!el.data.attrs) el.data.attrs = {} |
||||
|
el.data.attrs['server-rendered'] = true |
||||
|
} |
||||
const startTag = renderStartingTag(el, modules, directives) |
const startTag = renderStartingTag(el, modules, directives) |
||||
|
const endTag = `</${el.tag}>` |
||||
if (isUnaryTag(el.tag)) { |
if (isUnaryTag(el.tag)) { |
||||
return startTag |
return startTag |
||||
|
} else if (!el.children || !el.children.length) { |
||||
|
return startTag + endTag |
||||
} else { |
} else { |
||||
const children = el.children |
let children = '' |
||||
? el.children.map(renderNode).join('') |
for (let i = 0; i < el.children.length; i++) { |
||||
: '' |
children += renderNode(el.children[i]) |
||||
return startTag + children + `</${el.tag}>` |
} |
||||
|
return startTag + children + endTag |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
return renderComponent |
return function renderToString (component) { |
||||
|
return renderComponent(component, true) |
||||
|
} |
||||
} |
} |
||||
|
Loading…
Reference in new issue