Skip to content

Commit

Permalink
replace jQuery.val( with local implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ro0gr committed Jan 13, 2024
1 parent 35ab147 commit e8db4f1
Show file tree
Hide file tree
Showing 3 changed files with 453 additions and 4 deletions.
31 changes: 28 additions & 3 deletions addon/src/properties/value.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { $ } from '../-private/jquery';
import { findOne } from '../-private/finders';
import { getter } from '../macros/index';

Expand Down Expand Up @@ -83,7 +82,33 @@ export function value(selector, userOptions = {}) {
const element = findOne(this, selector, options);

return element.hasAttribute('contenteditable')
? $(element).html()
: $(element).val();
? element.innerHTML
: getValue(element);
});
}

function getValue(element) {
const { value } = element;
if (value !== undefined && element.tagName.toLowerCase() === 'select') {
return selectValue(element);
}

return element.value;
}

function selectValue(element) {
const selectedOptions = Array.from(element.selectedOptions).filter(
(option) =>
!option.disabled &&
(option.parentNode.tagName.toLowerCase() !== 'optgroup' ||
!option.parentNode.disabled)
);

if (element.multiple) {
return selectedOptions.map((option) => option.value);
} else if (selectedOptions.length === 0) {
return null;
}

return element.value;
}
2 changes: 1 addition & 1 deletion addon/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ declare module 'ember-cli-page-object' {
export function isHidden(scope?: string, options?: FindOptions): GetterDescriptor<boolean>;
export function isPresent(scope?: string, options?: FindOptions): GetterDescriptor<boolean>;
export function text(scope?: string, options?: FindOptions & { normalize?: boolean }): GetterDescriptor<string>;
export function value(scope?: string, options?: FindOptions): GetterDescriptor<string>;
export function value<T = string>(scope?: string, options?: FindOptions): GetterDescriptor<T>;
export function property<T = unknown>(name: string, scope?: string, options?: FindOptions): GetterDescriptor<T>;
export function hasClass(className: string, scope?: string, options?: FindOptions): GetterDescriptor<boolean>;
export function notHasClass(className: string, scope?: string, options?: FindOptions): GetterDescriptor<boolean>;
Expand Down
Loading

0 comments on commit e8db4f1

Please # to comment.