Skip to content

Commit

Permalink
fix(runtime-dom): ensure only symbols are explicitly stringified duri…
Browse files Browse the repository at this point in the history
…ng attribute patching (#11182)

close #11177
  • Loading branch information
LinusBorg committed Jun 22, 2024
1 parent 7936dae commit a2e35d6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
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

0 comments on commit a2e35d6

Please sign in to comment.