Skip to content

Commit

Permalink
capricorn86#652@patch: Remove try/catch in Dataset.deleteProperty
Browse files Browse the repository at this point in the history
Add a new method, `_removeNamedItem`, which removes the item without throwing if it does not exist, and override that in subclasses instead of the primary `removeNamedItem` method.
  • Loading branch information
RussianCow committed Sep 22, 2023
1 parent 124546a commit 7f701d3
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 26 deletions.
12 changes: 11 additions & 1 deletion packages/happy-dom/src/named-node-map/NamedNodeMap.ts
Expand Up @@ -104,7 +104,7 @@ export default class NamedNodeMap implements INamedNodeMap {
* @returns Removed item.
*/
public removeNamedItem(name: string): IAttr {
const item = this._removeNamedItemWithoutConsequences(name);
const item = this._removeNamedItem(name);
if (!item) {
throw new DOMException(
`Failed to execute 'removeNamedItem' on 'NamedNodeMap': No item with name '${name}' was found.`,
Expand Down Expand Up @@ -157,6 +157,16 @@ export default class NamedNodeMap implements INamedNodeMap {
return null;
}

/**
* Removes an item without throwing if it doesn't exist.
*
* @param name Name of item.
* @returns Removed item, or null if it didn't exist.
*/
public _removeNamedItem(name: string): IAttr | null {
return this._removeNamedItemWithoutConsequences(name);
}

/**
* Removes an item without calling listeners for certain attributes.
*
Expand Down
9 changes: 4 additions & 5 deletions packages/happy-dom/src/nodes/element/Dataset.ts
@@ -1,4 +1,5 @@
import Element from '../element/Element.js';
import HTMLElementNamedNodeMap from '../html-element/HTMLElementNamedNodeMap.js';

/**
* Storage type for a dataset proxy.
Expand Down Expand Up @@ -46,11 +47,9 @@ export default class Dataset {
return true;
},
deleteProperty(dataset: DatasetRecord, key: string): boolean {
try {
element.attributes.removeNamedItem('data-' + Dataset.camelCaseToKebab(key));
} catch (error) {
// Ignore DOMException when the attribute does not exist.
}
(<HTMLElementNamedNodeMap>element.attributes)._removeNamedItem(
'data-' + Dataset.camelCaseToKebab(key)
);
return delete dataset[key];
},
ownKeys(dataset: DatasetRecord): string[] {
Expand Down
4 changes: 2 additions & 2 deletions packages/happy-dom/src/nodes/element/ElementNamedNodeMap.ts
Expand Up @@ -114,8 +114,8 @@ export default class ElementNamedNodeMap extends NamedNodeMap {
/**
* @override
*/
public override removeNamedItem(name: string): IAttr | null {
const removedItem = super.removeNamedItem(this._getAttributeName(name));
public override _removeNamedItem(name: string): IAttr | null {
const removedItem = super._removeNamedItem(this._getAttributeName(name));

if (!removedItem) {
return null;
Expand Down
Expand Up @@ -32,8 +32,8 @@ export default class HTMLAnchorElementNamedNodeMap extends HTMLElementNamedNodeM
/**
* @override
*/
public override removeNamedItem(name: string): IAttr | null {
const removedItem = super.removeNamedItem(name);
public override _removeNamedItem(name: string): IAttr | null {
const removedItem = super._removeNamedItem(name);

if (removedItem) {
if (removedItem.name === 'rel' && this._ownerElement._relList) {
Expand Down
Expand Up @@ -38,8 +38,8 @@ export default class HTMLButtonElementNamedNodeMap extends HTMLElementNamedNodeM
/**
* @override
*/
public override removeNamedItem(name: string): IAttr | null {
const removedItem = super.removeNamedItem(name);
public override _removeNamedItem(name: string): IAttr | null {
const removedItem = super._removeNamedItem(name);

if (
removedItem &&
Expand Down
Expand Up @@ -26,8 +26,8 @@ export default class HTMLElementNamedNodeMap extends ElementNamedNodeMap {
/**
* @override
*/
public override removeNamedItem(name: string): IAttr | null {
const removedItem = super.removeNamedItem(name);
public override _removeNamedItem(name: string): IAttr | null {
const removedItem = super._removeNamedItem(name);

if (removedItem && removedItem.name === 'style' && this._ownerElement._style) {
this._ownerElement._style.cssText = '';
Expand Down
Expand Up @@ -38,8 +38,8 @@ export default class HTMLInputElementNamedNodeMap extends HTMLElementNamedNodeMa
/**
* @override
*/
public override removeNamedItem(name: string): IAttr | null {
const removedItem = super.removeNamedItem(name);
public override _removeNamedItem(name: string): IAttr | null {
const removedItem = super._removeNamedItem(name);

if (
removedItem &&
Expand Down
Expand Up @@ -31,8 +31,8 @@ export default class HTMLLinkElementNamedNodeMap extends HTMLElementNamedNodeMap
/**
* @override
*/
public override removeNamedItem(name: string): IAttr | null {
const removedItem = super.removeNamedItem(name);
public override _removeNamedItem(name: string): IAttr | null {
const removedItem = super._removeNamedItem(name);

if (removedItem && removedItem.name === 'rel' && this._ownerElement._relList) {
this._ownerElement._relList._updateIndices();
Expand Down
Expand Up @@ -37,8 +37,8 @@ export default class HTMLOptionElementNamedNodeMap extends HTMLElementNamedNodeM
/**
* @override
*/
public override removeNamedItem(name: string): IAttr | null {
const removedItem = super.removeNamedItem(name);
public override _removeNamedItem(name: string): IAttr | null {
const removedItem = super._removeNamedItem(name);

if (removedItem && !this._ownerElement._dirtyness && removedItem.name === 'selected') {
const selectNode = <HTMLSelectElement>this._ownerElement._selectNode;
Expand Down
Expand Up @@ -38,8 +38,8 @@ export default class HTMLSelectElementNamedNodeMap extends HTMLElementNamedNodeM
/**
* @override
*/
public override removeNamedItem(name: string): IAttr | null {
const removedItem = super.removeNamedItem(name);
public override _removeNamedItem(name: string): IAttr | null {
const removedItem = super._removeNamedItem(name);

if (
removedItem &&
Expand Down
Expand Up @@ -38,8 +38,8 @@ export default class HTMLTextAreaElementNamedNodeMap extends HTMLElementNamedNod
/**
* @override
*/
public override removeNamedItem(name: string): IAttr | null {
const removedItem = super.removeNamedItem(name);
public override _removeNamedItem(name: string): IAttr | null {
const removedItem = super._removeNamedItem(name);

if (
removedItem &&
Expand Down
Expand Up @@ -26,8 +26,8 @@ export default class SVGElementNamedNodeMap extends ElementNamedNodeMap {
/**
* @override
*/
public override removeNamedItem(name: string): IAttr | null {
const removedItem = super.removeNamedItem(name);
public override _removeNamedItem(name: string): IAttr | null {
const removedItem = super._removeNamedItem(name);

if (removedItem && removedItem.name === 'style' && this._ownerElement._style) {
this._ownerElement._style.cssText = '';
Expand Down

0 comments on commit 7f701d3

Please sign in to comment.