Skip to content

Commit 6e251e0

Browse files
Jinjiangyyx990803
authored andcommitted
Change svg parameter to namespace (#3)
* change svg parameter into namespace * fixed svg to namespace bug * change svg detecting into namespace detecting in parser * fixed eslint error
1 parent 065dc31 commit 6e251e0

File tree

8 files changed

+38
-30
lines changed

8 files changed

+38
-30
lines changed

Diff for: src/compiler/codegen/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function genElement (el) {
2929
// if the element is potentially a component,
3030
// wrap its children as a thunk.
3131
const children = genChildren(el, !isReservedTag(el.tag) /* asThunk */)
32-
const code = `__h__('${el.tag}', ${genData(el)}, ${children}, ${el.svg})`
32+
const code = `__h__('${el.tag}', ${genData(el)}, ${children}, '${el.namespace || ''}')`
3333
if (el.staticRoot) {
3434
// hoist static sub-trees out
3535
staticRenderFns.push(`with(this){return ${code}}`)

Diff for: src/compiler/parser/index.js

+19-15
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ const decodeHTMLCached = cached(decodeHTML)
2626
// attributes that should be using props for binding
2727
const mustUseProp = makeMap('value,selected,checked,muted')
2828

29-
// this map covers SVG elements that can appear as template root nodes
30-
const isSVG = makeMap('svg,g,defs,symbol,use,image,text,circle,ellipse,line,path,polygon,polyline,rect', true)
29+
// this map covers namespace elements that can appear as template root nodes
30+
const tagNamespace = makeMap('svg,g,defs,symbol,use,image,text,circle,ellipse,line,path,polygon,polyline,rect', true, 'svg')
3131

3232
// make warning customizable depending on environment.
3333
let warn
@@ -47,8 +47,9 @@ export function parse (template, options) {
4747
const stack = []
4848
let root
4949
let currentParent
50-
let inSvg = false
51-
let svgIndex = -1
50+
let inNamespace = false
51+
let namespaceIndex = -1
52+
let currentNamespace = ''
5253
let inPre = false
5354
let warned = false
5455
parseHTML(template, {
@@ -73,13 +74,15 @@ export function parse (template, options) {
7374
children: []
7475
}
7576

76-
// check svg
77-
if (inSvg) {
78-
element.svg = true
79-
} else if (isSVG(tag)) {
80-
element.svg = true
81-
inSvg = true
82-
svgIndex = stack.length
77+
// check namespace
78+
const namespace = tagNamespace(tag)
79+
if (inNamespace) {
80+
element.namespace = currentNamespace
81+
} else if (namespace) {
82+
element.namespace = namespace
83+
inNamespace = true
84+
currentNamespace = namespace
85+
namespaceIndex = stack.length
8386
}
8487

8588
if (!inPre) {
@@ -136,10 +139,11 @@ export function parse (template, options) {
136139
// pop stack
137140
stack.length -= 1
138141
currentParent = stack[stack.length - 1]
139-
// check svg state
140-
if (inSvg && stack.length <= svgIndex) {
141-
inSvg = false
142-
svgIndex = -1
142+
// check namespace state
143+
if (inNamespace && stack.length <= namespaceIndex) {
144+
inNamespace = false
145+
currentNamespace = ''
146+
namespaceIndex = -1
143147
}
144148
// check pre state
145149
if (element.pre) {

Diff for: src/runtime/dom/class-util.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { isIE9 } from '../util/index'
2-
import { svgNS } from './node-ops'
2+
import { namespaceMap } from './node-ops'
3+
4+
const svgNS = namespaceMap.svg
35

46
/**
57
* In IE9, setAttribute('class') will result in empty class

Diff for: src/runtime/dom/node-ops.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
export const svgNS = 'http://www.w3.org/2000/svg'
1+
export const namespaceMap = {
2+
svg: 'http://www.w3.org/2000/svg'
3+
}
24

35
export function createElement (tagName) {
46
return document.createElement(tagName)
57
}
68

7-
export function createSVGElement (tagName) {
8-
return document.createElementNS(svgNS, tagName)
9+
export function createElementNS (namespace, tagName) {
10+
return document.createElementNS(namespaceMap[namespace], tagName)
911
}
1012

1113
export function createTextNode (text) {

Diff for: src/runtime/vdom/create-element.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@ import { flatten } from './helpers'
44
import { renderState } from '../instance/render'
55
import { warn, isReservedTag, isUnknownElement, resolveAsset } from '../util/index'
66

7-
export default function createElement (tag, data, children, svg) {
7+
export default function createElement (tag, data, children, namespace) {
88
const context = this
99
const parent = renderState.activeInstance
1010
if (typeof tag === 'string') {
1111
let Ctor
1212
if (isReservedTag(tag)) {
13-
return VNode(tag, data, flatten(children), undefined, undefined, svg)
13+
return VNode(tag, data, flatten(children), undefined, undefined, namespace)
1414
} else if ((Ctor = resolveAsset(context.$options, 'components', tag))) {
1515
return createComponent(Ctor, data, parent, children)
1616
} else {
1717
if (process.env.NODE_ENV !== 'production') {
18-
if (!svg && isUnknownElement(tag)) {
18+
if (!namespace && isUnknownElement(tag)) {
1919
warn(
2020
'Unknown custom element: <' + tag + '> - did you ' +
2121
'register the component correctly? For recursive components, ' +
2222
'make sure to provide the "name" option.'
2323
)
2424
}
2525
}
26-
return VNode(tag, data, flatten(children && children()), undefined, undefined, svg)
26+
return VNode(tag, data, flatten(children && children()), undefined, undefined, namespace)
2727
}
2828
} else {
2929
return createComponent(tag, data, parent, children)

Diff for: src/runtime/vdom/patch.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ export default function createPatchFunction (backend) {
8181
const children = vnode.children
8282
const tag = vnode.tag
8383
if (isDef(tag)) {
84-
elm = vnode.elm = vnode.svg
85-
? nodeOps.createSVGElement(tag)
84+
elm = vnode.elm = vnode.namespace
85+
? nodeOps.createElementNS(vnode.namespace, tag)
8686
: nodeOps.createElement(tag)
8787
if (Array.isArray(children)) {
8888
for (i = 0; i < children.length; ++i) {

Diff for: src/runtime/vdom/vnode.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
export default function VNode (tag, data, children, text, elm, svg) {
1+
export default function VNode (tag, data, children, text, elm, namespace) {
22
return {
33
tag,
44
data,
55
children,
66
text,
77
elm,
8-
svg,
8+
namespace,
99
key: data && data.key
1010
}
1111
}

Diff for: src/shared/util.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
* @return {Function}
88
*/
99

10-
export function makeMap (str, expectsLowerCase) {
10+
export function makeMap (str, expectsLowerCase, defaultValue = true) {
1111
const map = Object.create(null)
1212
str.split(',').forEach(key => {
13-
map[key] = true
13+
map[key] = defaultValue
1414
})
1515
return expectsLowerCase
1616
? val => map[val.toLowerCase()]

0 commit comments

Comments
 (0)