Evan You 10 лет назад
Родитель
Сommit
914da492bf

+ 36 - 0
src/compiler/codegen/helpers.js

@@ -50,3 +50,39 @@ export function parseText (text) {
   }
   return tokens.join('+')
 }
+
+// this map covers SVG elements that can appear as template root nodes
+const svgMap = {
+  g: 1,
+  defs: 1,
+  symbol: 1,
+  use: 1,
+  image: 1,
+  text: 1,
+  circle: 1,
+  ellipse: 1,
+  line: 1,
+  path: 1,
+  polygon: 1,
+  polyline: 1,
+  rect: 1
+}
+
+export function checkSVG (el) {
+  if (el.tag === 'svg') {
+    // recursively mark all children as svg
+    markSVG(el)
+  }
+  return el.svg || svgMap[el.tag]
+}
+
+function markSVG (el) {
+  el.svg = true
+  if (el.children) {
+    for (var i = 0; i < el.children.length; i++) {
+      if (el.children[i].tag) {
+        markSVG(el.children[i])
+      }
+    }
+  }
+}

+ 7 - 0
src/compiler/codegen/index.js

@@ -2,6 +2,7 @@ import { genHandlers, addHandler } from './on'
 import { genModel } from './model'
 import { genClass } from './class'
 import {
+  checkSVG,
   parseText,
   parseModifiers,
   removeModifiers,
@@ -69,6 +70,12 @@ function genData (el, key) {
     data += `key:${key},`
   }
 
+  // check SVG namespace.
+  // this has the side effect of marking all children if the tag itself is <svg>
+  if (checkSVG(el)) {
+    data += 'svg:true,'
+  }
+
   // class
   // do it before other attributes becaues it removes static class
   // and class bindings from the element

+ 2 - 2
src/runtime/instance/render.js

@@ -16,9 +16,9 @@ export function renderMixin (Vue) {
   Vue.prototype._update = function (vtree) {
     callHook(this, 'beforeUpdate')
     if (!this._tree) {
-      patch(this.$el, vtree)
+      this.$el = patch(this.$el, vtree)
     } else {
-      patch(this._tree, vtree)
+      this.$el = patch(this._tree, vtree)
     }
     this._tree = vtree
     callHook(this, 'updated')

+ 0 - 12
src/runtime/vdom/h.js

@@ -1,15 +1,6 @@
 import VNode from './vnode'
 import { isPrimitive, isArray } from '../util/index'
 
-function addNS (data, children) {
-  data.ns = 'http://www.w3.org/2000/svg'
-  if (children !== undefined) {
-    for (var i = 0; i < children.length; ++i) {
-      addNS(children[i].data, children[i].children)
-    }
-  }
-}
-
 export default function h (tag, data, children) {
   if (isArray(children)) {
     let _children = children
@@ -31,8 +22,5 @@ export default function h (tag, data, children) {
       }
     }
   }
-  if (tag === 'svg') {
-    addNS(data, children)
-  }
   return VNode(tag, data, children, undefined, undefined)
 }

+ 4 - 3
src/runtime/vdom/patch.js

@@ -4,6 +4,7 @@ import { isPrimitive } from '../util/index'
 
 const emptyNode = VNode('', {}, [], undefined, undefined)
 const hooks = ['create', 'update', 'remove', 'destroy', 'pre', 'post']
+const svgNS = 'http://www.w3.org/2000/svg'
 
 function isUndef (s) {
   return s === undefined
@@ -66,8 +67,8 @@ export default function createPatchFunction (modules, api) {
     const children = vnode.children
     const tag = vnode.sel
     if (isDef(tag)) {
-      elm = vnode.elm = isDef(data) && isDef(i = data.ns)
-        ? api.createElementNS(i, tag)
+      elm = vnode.elm = isDef(data) && data.svg
+        ? api.createElementNS(svgNS, tag)
         : api.createElement(tag)
       if (Array.isArray(children)) {
         for (i = 0; i < children.length; ++i) {
@@ -262,6 +263,6 @@ export default function createPatchFunction (modules, api) {
       insertedVnodeQueue[i].data.hook.insert(insertedVnodeQueue[i])
     }
     for (i = 0; i < cbs.post.length; ++i) cbs.post[i]()
-    return vnode
+    return vnode.elm
   }
 }