Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-dom): ensure only symbols are explicitly stringified during attribute patching #11182

Merged
merged 3 commits into from
Jun 22, 2024
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
19 changes: 19 additions & 0 deletions packages/runtime-dom/__tests__/patchAttrs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,23 @@ describe('runtime-dom: attrs patching', () => {
patchProp(el, 'value', null, symbol)
expect(el.value).toBe(symbol.toString())
})

// #11177
test('should allow setting value to object, leaving stringification to the element/browser', () => {
// normal behavior
const el = document.createElement('div')
const obj = { toString: () => 'foo' }
patchProp(el, 'data-test', null, obj)
expect(el.dataset.test).toBe('foo')

const el2 = document.createElement('div')
let testvalue: null | typeof obj = null
// simulating a web component that implements its own setAttribute handler
el2.setAttribute = (name, value) => {
testvalue = value
}
patchProp(el2, 'data-test', null, obj)
expect(el2.dataset.test).toBe(undefined)
expect(testvalue).toBe(obj)
})
})
6 changes: 5 additions & 1 deletion packages/runtime-dom/src/modules/attrs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
NOOP,
includeBooleanAttr,
isSpecialBooleanAttr,
isSymbol,
makeMap,
} from '@vue/shared'
import {
Expand Down Expand Up @@ -37,7 +38,10 @@ export function patchAttr(
el.removeAttribute(key)
} else {
// attribute value is a string https://html.spec.whatwg.org/multipage/dom.html#attributes
el.setAttribute(key, isBoolean ? '' : String(value))
el.setAttribute(
key,
isBoolean ? '' : isSymbol(value) ? String(value) : value,
)
}
}
}
Expand Down