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): these attrs should be set in chreateElement #6012

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 26 additions & 0 deletions packages/runtime-dom/__tests__/nodeOps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@ describe('runtime-dom: node-ops', () => {
expect(option2.selected).toBe(true)
})

test("the <input>'s value, checked attrs should be set in createElement", () => {
const value = 'foo'
const checked = true

const el = nodeOps.createElement('input', false, undefined, {
value,
checked
}) as HTMLInputElement

el.value = value + value
el.checked = !checked

expect(el.getAttribute('value')).toBe(value)
expect(el.getAttribute('checked')).toBe('')
})

test("the <option>'s selected attr should be set in createElement", () => {
const el = nodeOps.createElement('option', false, undefined, {
selected: true
}) as HTMLOptionElement

el.selected = false

expect(el.getAttribute('selected')).toBe('')
})

describe('insertStaticContent', () => {
test('fresh insertion', () => {
const content = `<div>one</div><div>two</div>three`
Expand Down
13 changes: 13 additions & 0 deletions packages/runtime-dom/src/nodeOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ export const nodeOps: Omit<RendererOptions<Node, Element>, 'patchProp'> = {
;(el as HTMLSelectElement).setAttribute('multiple', props.multiple)
}

if (tag === 'option' && props && props.selected) {
el.setAttribute('selected', '')
}

if (tag === 'input' && props) {
if (props.value) {
zhmushan marked this conversation as resolved.
Show resolved Hide resolved
el.setAttribute('value', props.value)
}
if (props.checked) {
el.setAttribute('checked', '')
}
}

return el
},

Expand Down