forked from capricorn86/happy-dom
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
capricorn86#428@minor: Fixes select element undefined options error.
- Loading branch information
1 parent
b8f80e9
commit ae1ad43
Showing
23 changed files
with
16,288 additions
and
313 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/happy-dom/src/nodes/html-input-element/HTMLInputElement.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/happy-dom/src/nodes/html-opt-group-element/HTMLOptGroupElement.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import HTMLElement from '../html-element/HTMLElement'; | ||
import IHTMLOptGroupElement from './IHTMLOptGroupElement'; | ||
|
||
/** | ||
* HTML Opt Group Element. | ||
* | ||
* Reference: | ||
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptGroupElement. | ||
*/ | ||
export default class HTMLOptGroupElement extends HTMLElement implements IHTMLOptGroupElement { | ||
/** | ||
* Returns label. | ||
* | ||
* @returns Label. | ||
*/ | ||
public get label(): string { | ||
return this.getAttributeNS(null, 'label') || ''; | ||
} | ||
|
||
/** | ||
* Sets label. | ||
* | ||
* @param label Label. | ||
*/ | ||
public set label(label: string) { | ||
if (!label) { | ||
this.removeAttributeNS(null, 'label'); | ||
} else { | ||
this.setAttributeNS(null, 'label', label); | ||
} | ||
} | ||
|
||
/** | ||
* Returns disabled. | ||
* | ||
* @returns Disabled. | ||
*/ | ||
public get disabled(): boolean { | ||
return this.getAttributeNS(null, 'disabled') !== null; | ||
} | ||
|
||
/** | ||
* Sets disabled. | ||
* | ||
* @param disabled Disabled. | ||
*/ | ||
public set disabled(disabled: boolean) { | ||
if (!disabled) { | ||
this.removeAttributeNS(null, 'disabled'); | ||
} else { | ||
this.setAttributeNS(null, 'disabled', ''); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/happy-dom/src/nodes/html-opt-group-element/IHTMLOptGroupElement.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import IHTMLElement from '../html-element/IHTMLElement'; | ||
|
||
/** | ||
* HTML Opt Group Element. | ||
* | ||
* Reference: | ||
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptGroupElement. | ||
*/ | ||
export default interface IHTMLOptGroupElement extends IHTMLElement { | ||
disabled: boolean; | ||
label: string; | ||
} |
117 changes: 117 additions & 0 deletions
117
packages/happy-dom/src/nodes/html-option-element/HTMLOptionElement.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import HTMLElement from '../html-element/HTMLElement'; | ||
import IHTMLElement from '../html-element/IHTMLElement'; | ||
import IHTMLFormElement from '../html-form-element/IHTMLFormElement'; | ||
import HTMLOptionElementValueSanitizer from './HTMLOptionElementValueSanitizer'; | ||
import IHTMLOptionElement from './IHTMLOptionElement'; | ||
|
||
/** | ||
* HTML Option Element. | ||
* | ||
* Reference: | ||
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement. | ||
*/ | ||
export default class HTMLOptionElement extends HTMLElement implements IHTMLOptionElement { | ||
public _index: number; | ||
|
||
/** | ||
* Returns inner text, which is the rendered appearance of text. | ||
* | ||
* @returns Inner text. | ||
*/ | ||
public get text(): string { | ||
return this.innerText; | ||
} | ||
|
||
/** | ||
* Sets the inner text, which is the rendered appearance of text. | ||
* | ||
* @param innerText Inner text. | ||
*/ | ||
public set text(text: string) { | ||
this.innerText = text; | ||
} | ||
|
||
/** | ||
* Returns index. | ||
* | ||
* @returns Index. | ||
*/ | ||
public get index(): number { | ||
return this._index; | ||
} | ||
|
||
/** | ||
* Returns the parent form element. | ||
* | ||
* @returns Form. | ||
*/ | ||
public get form(): IHTMLFormElement { | ||
let parent = <IHTMLElement>this.parentNode; | ||
while (parent && parent.tagName !== 'FORM') { | ||
parent = <IHTMLElement>parent.parentNode; | ||
} | ||
return <IHTMLFormElement>parent; | ||
} | ||
|
||
/** | ||
* Returns selected. | ||
* | ||
* @returns Selected. | ||
*/ | ||
public get selected(): boolean { | ||
return this.getAttributeNS(null, 'selected') !== null; | ||
} | ||
|
||
/** | ||
* Sets selected. | ||
* | ||
* @param selected Selected. | ||
*/ | ||
public set selected(selected: boolean) { | ||
if (!selected) { | ||
this.removeAttributeNS(null, 'selected'); | ||
} else { | ||
this.setAttributeNS(null, 'selected', ''); | ||
} | ||
} | ||
|
||
/** | ||
* Returns disabled. | ||
* | ||
* @returns Disabled. | ||
*/ | ||
public get disabled(): boolean { | ||
return this.getAttributeNS(null, 'disabled') !== null; | ||
} | ||
|
||
/** | ||
* Sets disabled. | ||
* | ||
* @param disabled Disabled. | ||
*/ | ||
public set disabled(disabled: boolean) { | ||
if (!disabled) { | ||
this.removeAttributeNS(null, 'disabled'); | ||
} else { | ||
this.setAttributeNS(null, 'disabled', ''); | ||
} | ||
} | ||
|
||
/** | ||
* Returns value. | ||
* | ||
* @returns Value. | ||
*/ | ||
public get value(): string { | ||
return this.getAttributeNS(null, 'value') || ''; | ||
} | ||
|
||
/** | ||
* Sets value. | ||
* | ||
* @param value Value. | ||
*/ | ||
public set value(value: string) { | ||
this.setAttributeNS(null, 'value', HTMLOptionElementValueSanitizer.sanitize(value)); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/happy-dom/src/nodes/html-option-element/HTMLOptionElementValueSanitizer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const NEW_LINES_REGEXP = /[\n\r]/gm; | ||
|
||
/** | ||
* HTML select element value sanitizer. | ||
*/ | ||
export default class HTMLOptionElementValueSanitizer { | ||
/** | ||
* Sanitizes a value. | ||
* | ||
* @param value Value. | ||
*/ | ||
public static sanitize(value: string): string { | ||
return value.trim().replace(NEW_LINES_REGEXP, ''); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
packages/happy-dom/src/nodes/html-option-element/HTMLOptionsCollection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import DOMException from '../../exception/DOMException'; | ||
import HTMLCollection from '../element/HTMLCollection'; | ||
import HTMLOptGroupElement from '../html-opt-group-element/HTMLOptGroupElement'; | ||
import HTMLOptionElement from './HTMLOptionElement'; | ||
import IHTMLOptionsCollection from './IHTMLOptionsCollection'; | ||
|
||
/** | ||
* HTML Options Collection. | ||
* | ||
* Reference: | ||
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptionsCollection. | ||
*/ | ||
export default class HTMLOptionsCollection | ||
extends HTMLCollection | ||
implements IHTMLOptionsCollection | ||
{ | ||
public _selectedIndex: number; | ||
|
||
/** | ||
* Returns selectedIndex. | ||
* | ||
* @returns SelectedIndex. | ||
*/ | ||
public get selectedIndex(): number { | ||
return this._selectedIndex; | ||
} | ||
|
||
/** | ||
* Sets selectedIndex. | ||
* | ||
* @param selectedIndex SelectedIndex. | ||
*/ | ||
public set selectedIndex(selectedIndex: number) { | ||
this._selectedIndex = selectedIndex; | ||
} | ||
|
||
/** | ||
* Returns item by index. | ||
* | ||
* @param index Index. | ||
*/ | ||
public item(index: number): HTMLOptionElement | HTMLOptGroupElement { | ||
return this[index]; | ||
} | ||
|
||
/** | ||
* | ||
* @param element | ||
* @param before | ||
*/ | ||
public add( | ||
element: HTMLOptionElement | HTMLOptGroupElement, | ||
before?: number | HTMLOptionElement | HTMLOptGroupElement | ||
): void { | ||
if (!before && before !== 0) { | ||
this.push(element); | ||
return; | ||
} | ||
|
||
if (!Number.isNaN(Number(before))) { | ||
if (before < 0) { | ||
return; | ||
} | ||
|
||
this.splice(<number>before, 0, element); | ||
return; | ||
} | ||
|
||
const idx = this.findIndex((element) => element === before); | ||
if (idx === -1) { | ||
throw new DOMException( | ||
"Failed to execute 'add' on 'DOMException': The node before which the new node is to be inserted is not a child of this node." | ||
); | ||
} | ||
|
||
this.splice(idx, 0, element); | ||
} | ||
|
||
/** | ||
* Removes indexed element from collection. | ||
* | ||
* @param index Index. | ||
*/ | ||
public remove(index: number): void { | ||
this.splice(index, 1); | ||
if (index === this.selectedIndex) { | ||
if (this.length) { | ||
this.selectedIndex = 0; | ||
} else { | ||
this.selectedIndex = -1; | ||
} | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/happy-dom/src/nodes/html-option-element/IHTMLOptionElement.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import IHTMLElement from '../html-element/IHTMLElement'; | ||
import IHTMLFormElement from '../html-form-element/IHTMLFormElement'; | ||
|
||
/** | ||
* HTML Option Element. | ||
* | ||
* Reference: | ||
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement. | ||
*/ | ||
export default interface IHTMLOptionElement extends IHTMLElement { | ||
readonly form: IHTMLFormElement; | ||
readonly index: number; | ||
selected: boolean; | ||
value: string; | ||
text: string; | ||
disabled: boolean; | ||
} |
Oops, something went wrong.