HEX
Server: Apache
System: Linux efa57bbe-abb1-400d-2985-3b056fbc2701.secureserver.net 6.1.147-1.el9.elrepo.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Jul 24 12:33:32 EDT 2025 x86_64
User: root (0)
PHP: 8.0.30.4
Disabled: NONE
Upload Files
File: /var/www/wp-content/plugins/pojo-accessibility/modules/remediation/assets/js/actions/replace.js
import { RemediationBase } from './base';

export class ReplaceRemediation extends RemediationBase {
	replaceIgnoreCase(outerHtml, find, replace, lowerOuterHTML, lowerFind) {
		const index = lowerOuterHTML.indexOf(lowerFind);
		if (index === -1) {
			return outerHtml;
		}
		return (
			outerHtml.substring(0, index) +
			replace +
			outerHtml.substring(index + find.length)
		);
	}

	run() {
		const { xpath, find, replace } = this.data;
		const el = this.getElementByXPath(xpath);
		if (!el) {
			return false;
		}
		const outerHTML = el.outerHTML;
		if (typeof find !== 'string' || typeof replace !== 'string') {
			return false;
		}
		const lowerOuterHTML = outerHTML.toLowerCase();
		const lowerFind = find.toLowerCase();
		if (!lowerOuterHTML.includes(lowerFind)) {
			return false;
		}

		const updatedHTML = this.replaceIgnoreCase(
			outerHTML,
			find,
			replace,
			lowerOuterHTML,
			lowerFind,
		);

		if (updatedHTML === outerHTML) {
			return false;
		}

		// Create a temporary container to parse the HTML string
		const tmp = document.createElement('div');
		tmp.innerHTML = updatedHTML;
		const newNode = tmp.firstElementChild;
		if (newNode && el.parentNode) {
			el.parentNode.replaceChild(newNode, el);
		}
		return true;
	}
}