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

replace jQuery.val( with local implementation #633

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading