From 3798cdffe75d789c21c0c2b2e752f08f23003de8 Mon Sep 17 00:00:00 2001 From: Tal Weinfeld Date: Sun, 11 Apr 2021 19:18:05 +0300 Subject: [PATCH 1/2] "setAttributeByValue" will now check its affected node for existing attribute values before altering them (resolved viv-481) --- components/textfield/src/vwc-textfield.ts | 37 +++++++++++++++-------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/components/textfield/src/vwc-textfield.ts b/components/textfield/src/vwc-textfield.ts index 848060216..846ece8a7 100644 --- a/components/textfield/src/vwc-textfield.ts +++ b/components/textfield/src/vwc-textfield.ts @@ -227,15 +227,28 @@ export class VWCTextField extends MWCTextField { } } -function setAttributeByValue( - attributeName: string, - value: unknown, - target: HTMLInputElement, - asEmpty = false -): void { - if (value) { - target.setAttribute(attributeName, asEmpty ? '' : String(value)); - } else { - target.removeAttribute(attributeName); - } -} +const setAttributeByValue = (function () { + const NOT_ASSIGNED = Symbol('NotAssigned'); + return function ( + attributeName: string, + value: unknown, + target: HTMLInputElement, + asEmpty = false + ): void { + const newValue:unknown = value + ? (() => { return asEmpty ? '' : String(value); })() + : NOT_ASSIGNED; + + const currentValue:unknown = target.hasAttribute(attributeName) + ? target.getAttribute(attributeName) + : NOT_ASSIGNED; + + if (newValue !== currentValue) { + if (newValue === NOT_ASSIGNED) { + target.removeAttribute(attributeName); + } else { + target.setAttribute(attributeName, newValue as string); + } + } + }; +}()); From 0ffd09e49d47f0fc69c2748a2fc06ca3b0064e45 Mon Sep 17 00:00:00 2001 From: Tal Weinfeld Date: Mon, 12 Apr 2021 10:49:49 +0300 Subject: [PATCH 2/2] Unmarked Yuri's validity tests --- components/textfield/test/textfield.autovalidate.test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/components/textfield/test/textfield.autovalidate.test.js b/components/textfield/test/textfield.autovalidate.test.js index a4200b72f..6b3388d00 100644 --- a/components/textfield/test/textfield.autovalidate.test.js +++ b/components/textfield/test/textfield.autovalidate.test.js @@ -87,10 +87,10 @@ describe('textfield validation', () => { function assertValidityState(texfield, valid, assertHelper = false) { // TODO: unmark the lines below when the invlalid class issue settled - // const labelHint = texfield.shadowRoot.querySelector('.mdc-text-field--invalid'); - // if ((valid && labelHint) || (!valid && !labelHint)) { - // throw new Error(`expected textfield validity to be '${valid}', but the invalid label state ${labelHint ? '' : 'NOT'} found`); - // } + const labelHint = texfield.shadowRoot.querySelector('.mdc-text-field--invalid'); + if ((valid && labelHint) || (!valid && !labelHint)) { + throw new Error(`expected textfield validity to be '${valid}', but the invalid label state ${labelHint ? '' : 'NOT'} found`); + } if (assertHelper) { const helperHint = texfield.shadowRoot.querySelector('vwc-helper-message[is-error]'); if ((valid && helperHint) || (!valid && !helperHint)) {