Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/compiler-vapor/__tests__/transforms/vBind.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1428,4 +1428,42 @@ describe('compiler v-bind', () => {
`_setDynamicProps(n5, [_ctx.obj, { onClick: _ctx.fn4 }], k0)`,
)
})

test('svg namespace elements that share a tag name with html', () => {
let { code } = compileWithVBind(`<svg><a :href="url" :class="cls"/></svg>`)
expect(code).contains('_setAttr(n0, "href", _ctx.url, true)')
expect(code).contains('_setClass(n0, _ctx.cls, true)')

code = compileWithVBind(`<svg><a v-bind="obj"/></svg>`).code
expect(code).contains('_setDynamicProps(n0, [_ctx.obj], null, true)')

// back to html inside <foreignObject>
code = compileWithVBind(
`<svg><foreignObject><a :href="url" :class="cls"/></foreignObject></svg>`,
).code
expect(code).contains('_setProp(n0, "href", _ctx.url)')
expect(code).contains('_setClass(n0, _ctx.cls)')
})

test('uses the svg class helper for object class bindings on svg anchors', () => {
const { code } = compileWithVBind(
`<svg><a :class="{ active: flag }"/></svg>`,
)

expect(code).toContain('_setClass(n0, { active: _ctx.flag }, true)')
expect(code).not.toContain('_setClassName')
})

test('groups native svg event bindings without changing other prop helpers', () => {
const { code } = compileWithVBind(
`<svg><a :onclick="click" :onfocus="focus" :href="url" :class="cls"/></svg>`,
)

expect(code).toContain('const k0 = ["onclick","onfocus"]')
expect(code).toContain(
'_setDynamicProps(n0, [{ onclick: _ctx.click, onfocus: _ctx.focus }], k0, true)',
)
expect(code).toContain('_setAttr(n0, "href", _ctx.url, true)')
expect(code).toContain('_setClass(n0, _ctx.cls, true)')
})
})
9 changes: 4 additions & 5 deletions packages/compiler-vapor/src/generators/prop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
extend,
hyphenate,
isOn,
isSVGTag,
normalizeClass,
shouldSetAsAttr,
toHandlerKey,
Expand Down Expand Up @@ -74,11 +73,12 @@ export function genSetProp(
const {
prop: { key, values, modifier },
tag,
isSVG,
} = oper
if (!modifier && isOn(key.content)) {
return genSetListener(oper, context)
}
const resolvedHelper = getRuntimeHelper(tag, key.content, modifier)
const resolvedHelper = getRuntimeHelper(tag, isSVG, key.content, modifier)
if (
key.content === 'class' &&
!resolvedHelper.isSVG &&
Expand Down Expand Up @@ -393,7 +393,6 @@ export function genDynamicProps(
context: CodegenContext,
): CodeFragment[] {
const { helper } = context
const isSVG = isSVGTag(oper.tag)
const values = oper.props.map(props => {
if (Array.isArray(props)) {
return genLiteralObjectProps(props, context) // static and dynamic arg props
Expand All @@ -411,7 +410,7 @@ export function genDynamicProps(
`n${oper.element}`,
genMulti(DELIMITERS_ARRAY, ...values),
genDynamicPropNames(oper, context),
isSVG && 'true',
oper.isSVG && 'true',
),
]
}
Expand Down Expand Up @@ -628,11 +627,11 @@ export function genPropValue(

function getRuntimeHelper(
tag: string,
isSVG: boolean,
key: string,
modifier: '.' | '^' | undefined,
): HelperConfig {
const tagName = tag.toUpperCase()
const isSVG = isSVGTag(tag)

if (modifier) {
if (modifier === '.') {
Expand Down
3 changes: 2 additions & 1 deletion packages/compiler-vapor/src/ir/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export interface SetPropIRNode extends BaseIRNode {
element: number
prop: IRProp
tag: string
isSVG: boolean
/** Whether it's in effect; only a listener key (`onXxx`) needs to know */
effect?: boolean
}
Expand All @@ -156,7 +157,7 @@ export interface SetDynamicPropsIRNode extends BaseIRNode {
type: IRNodeTypes.SET_DYNAMIC_PROPS
element: number
props: IRProps[]
tag: string
isSVG: boolean
}

export interface SetDynamicEventsIRNode extends BaseIRNode {
Expand Down
49 changes: 41 additions & 8 deletions packages/compiler-vapor/src/transforms/transformElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
isFormattingTag,
isInlineTag,
isModelListener,
isNativeOn,
isOn,
isVoidTag,
makeMap,
Expand Down Expand Up @@ -524,6 +525,7 @@ function transformNativeElement(
) {
const { tag } = node
const { scopeId } = context.options
const isSVG = node.ns === Namespaces.SVG

let template = ''

Expand All @@ -538,7 +540,7 @@ function transformNativeElement(
type: IRNodeTypes.SET_DYNAMIC_PROPS,
element: context.reference(),
props: dynamicArgs,
tag,
isSVG,
},
getEffectIndex,
)
Expand All @@ -560,6 +562,7 @@ function transformNativeElement(
({ key, modifier }) =>
key.content === 'valueAsNumber' && modifier !== '^',
)
const nativeOnProps: IRProp[] = []
let hasEffect = false
for (const prop of propsResult[1]) {
const { key, values } = prop
Expand All @@ -575,6 +578,7 @@ function transformNativeElement(
element: context.reference(),
prop,
tag,
isSVG,
}
hasEffect = context.registerEffect(
values,
Expand Down Expand Up @@ -630,6 +634,9 @@ function transformNativeElement(
if (foldedValue) {
appendTemplateProp(key.content, foldedValue)
}
} else if (isSVG && !prop.modifier && isNativeOn(key.content)) {
// Native event bindings need the runtime value to choose prop vs attr.
nativeOnProps.push(prop)
} else {
// Constant setters can depend on preceding dynamic props, e.g.
// valueAsNumber needs type and max to be initialized first.
Expand All @@ -640,12 +647,26 @@ function transformNativeElement(
element: context.reference(),
prop,
tag,
isSVG,
},
getEffectIndex,
needsOrderedProps && hasEffect,
)
}
}
if (nativeOnProps.length) {
// One call per element keeps the dynamic prop cache shared by these keys.
context.registerEffect(
nativeOnProps.flatMap(({ values }) => values),
{
type: IRNodeTypes.SET_DYNAMIC_PROPS,
element: context.reference(),
props: [nativeOnProps],
isSVG,
},
getEffectIndex,
)
}
}

let children = context.childrenTemplate.join('')
Expand Down Expand Up @@ -986,6 +1007,7 @@ export function buildProps(
if (isComponent) {
pushStaticObjectLiteralProps(objectLiteralProps)
} else {
dynamicExpr.push(prop.exp)
results.push(...objectLiteralProps.map(toDirectiveResult))
}
} else {
Expand Down Expand Up @@ -1160,7 +1182,8 @@ export function mergesListeners(
// like vdom, an element merges its listeners at runtime in template order
// once their keys can collide: a v-bind spread that is not expanded into
// static props or carries a dynamic key may hold any `on*` key, and a v-on
// object may hold the key of a static listener (`@evt` or `:onXxx`)
// object may hold the key of a static listener (`@evt` or `:onXxx`).
// Native SVG on* bindings also share the dynamic prop cache with v-on objects.
function resolveListenerMerge(
node: ElementNode,
context: TransformContext<ElementNode>,
Expand All @@ -1173,16 +1196,26 @@ function resolveListenerMerge(
const arg = p.arg && resolveExpression(p.arg)
if (p.name === 'bind') {
if (!arg) {
if (
p.exp &&
!resolveNativeObjectLiteralBindProps(p.exp, context, props, p)
) {
return true
if (p.exp) {
const bindProps = resolveNativeObjectLiteralBindProps(
p.exp,
context,
props,
p,
)
if (!bindProps) return true
if (
node.ns === Namespaces.SVG &&
bindProps.some(({ key }) => isNativeOn(key.content))
) {
hasStaticListener = true
}
}
} else if (!arg.isStatic) {
return true
} else if (
isOn(arg.content) &&
(isOn(arg.content) ||
(node.ns === Namespaces.SVG && isNativeOn(arg.content))) &&
!isModelListener(arg.content) &&
!p.modifiers.some(m => m.content === 'prop' || m.content === 'attr')
) {
Expand Down
Loading
Loading