Skip to content

Commit

Permalink
fix: fix input value when set to null.
Browse files Browse the repository at this point in the history
  • Loading branch information
andycall committed Aug 23, 2021
1 parent 60d4b35 commit fb0c0a1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
12 changes: 10 additions & 2 deletions bridge/bindings/jsc/DOM/elements/input_element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,16 @@ bool JSInputElement::InputElementInstance::setProperty(std::string &name, JSValu
if (prototypePropertyMap.count(name) > 0) return false;

if (propertyMap.count(name) > 0) {
JSStringRef stringRef = JSValueToStringCopy(_hostClass->ctx, value, exception);
std::string string = JSStringToStdString(stringRef);
JSStringRef valueString;
// https://github.com/WebKit/WebKit/blob/main/Source/WebCore/html/HTMLInputElement.cpp#L1060
// Webkit have specific logic when value is null.
if (name == "value" && JSValueIsNull(ctx, value)) {
valueString = JSStringCreateWithUTF8CString(""); // input's fallback value is null.
} else {
valueString = JSValueToStringCopy(_hostClass->ctx, value, exception);
}

std::string string = JSStringToStdString(valueString);
NativeString args_01{};
NativeString args_02{};
buildUICommandArgs(name, string, args_01, args_02);
Expand Down
28 changes: 19 additions & 9 deletions integration_tests/specs/dom/elements/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ describe('Tags input', () => {
div = createElement(
'input',
{
value: '1234567890',
value: '1234567890',
style: {
lineHeight: '50px',
fontSize: '30px',
fontSize: '30px',
},
}
);
Expand All @@ -69,7 +69,7 @@ describe('Tags input', () => {
input = createElement(
'input',
{
value: '1234567890',
value: '1234567890',
style: {
lineHeight: '10px',
fontSize: '30px'
Expand All @@ -86,7 +86,7 @@ describe('Tags input', () => {
input = createElement(
'input',
{
value: '1234567890',
value: '1234567890',
style: {
lineHeight: '100px',
fontSize: '30px'
Expand All @@ -97,15 +97,15 @@ describe('Tags input', () => {

await snapshot();
});

it('line-height changes when height is not set', async (done) => {
let input;
input = createElement(
'input',
{
value: '1234567890',
value: '1234567890',
style: {
lineHeight: '50px',
lineHeight: '50px',
},
}
);
Expand All @@ -125,7 +125,7 @@ describe('Tags input', () => {
input = createElement(
'input',
{
value: '1234567890',
value: '1234567890',
style: {
fontSize: '30px'
},
Expand All @@ -141,7 +141,7 @@ describe('Tags input', () => {
input = createElement(
'input',
{
value: '1234567890',
value: '1234567890',
style: {
},
}
Expand Down Expand Up @@ -375,4 +375,14 @@ describe('Tags input', () => {

simulateClick(10, 10);
});

it('should return empty string when set value to null', (done) => {
const input = document.createElement('input');
document.body.appendChild(input);
input.value = '1234';
expect(input.value).toBe('1234');
// @ts-ignore
input.value = null;
expect(input.value).toBe('');
});
});

0 comments on commit fb0c0a1

Please sign in to comment.