blob: 1319057a3bbeeb76c6dfd9b92aa471e6e8165f92 [file]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
function setValue(value) {
if (!this.isConnected || this.disabled || this.readOnly) throw Error('element is not editable');
const win = this.ownerDocument.defaultView;
const prototype =
this instanceof win.HTMLInputElement
? win.HTMLInputElement.prototype
: this instanceof win.HTMLTextAreaElement
? win.HTMLTextAreaElement.prototype
: this instanceof win.HTMLSelectElement
? win.HTMLSelectElement.prototype
: null;
if (!prototype) throw Error('setValue supports input, textarea and select elements');
if (
this instanceof win.HTMLSelectElement &&
!Array.from(this.options).some((option) => option.value === value && !option.disabled)
)
throw Error('select option value is unavailable');
const setter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
setter.call(this, value);
this.dispatchEvent(new win.Event('input', { bubbles: true }));
this.dispatchEvent(new win.Event('change', { bubbles: true }));
if (this.value !== value) throw Error('value read-back differs from requested value');
}