/*
 * Copyright (c) 2018, 2026, Oracle and/or its affiliates.
 * Licensed under The Universal Permissive License (UPL), Version 1.0
 * as shown at https://oss.oracle.com/licenses/upl/
 */
const RULE_NAME="spoc-dom-manip-tag-pos";const PROP_NAMES=[".innerHTML",".outerHTML"];const FUNC_NAMES=[".html",".prepend",".prependTo",".before",".after",".insertBefore",".insertAfter",".append",".appendTo",".wrap",".wrapAll",".wrapInner",".replaceAll",".replaceWith",".write",".writeln",".attr",".setAttribute",".cookie","eval","Function","setTimeout","setInterval","random","script","createElement",".appendChild","XMLHttpRequest","ActiveXObject"];const REGEX=/@HTML/gi;const REGEX2=/@XSS/gi;const REGEX3=/@Rand/gi;function Rule(){};Rule.prototype.getName=function(){return RULE_NAME};Rule.prototype.getShortDescription=function(){return"Finds all SPOC tags with invalid placement"};Rule.prototype.getDescription=function(){return"Finds all declarations of SPOC tags '@HTMLUpdateOK' and '@XSS_FALSE_POSITIVE' on lines that do not containg DOM manipulation properties or functions."};Rule.prototype.register=function(){return{"file":_scan}};function _scan(context){var data=context.data,tag,line,hit,issue,result,indices=[],i,j,x;REGEX.lastIndex=0;while(result=REGEX.exec(data)){indices.push(result.index)}REGEX2.lastIndex=0;while(result=REGEX2.exec(data)){indices.push(result.index)}REGEX3.lastIndex=0;while(result=REGEX3.exec(data)){indices.push(result.index)}for(i=0;i<indices.length;i++){x=indices[i];tag=_getTag(context,data,x);line=_getLineStart(context,data,x);hit=false;for(j=0;j<FUNC_NAMES.length;j++){if(line.includes(FUNC_NAMES[j])){hit=true;break}}if(!hit){for(j=0;j<PROP_NAMES.length;j++){if(line.includes(PROP_NAMES[j])){hit=true;break}}}if(tag.toLowerCase().includes("review")){issue=new context.Issue("obsolete tag '@HTMLUpdateReview' - remove");_updatePosition(context,issue,data,x,x+tag.length);context.reporter.addIssue(issue,context)}else if(!hit){issue=new context.Issue("Incorrect placement of '"+tag+"'");_updatePosition(context,issue,data,x,x+tag.length);context.reporter.addIssue(issue,context)}}};function _getTag(context,data,x){var i=context.utils.utils.getIndexToWhitespace(data,x);return data.substring(x,i)};function _getLineStart(context,data,x){var i,ret=null;for(i=x;i>=0;i--){if(data.charAt(i)==="\n"){ret=data.substring(i,x);break}}return ret};function _updatePosition(context,issue,data,indexStart,indexEnd){var rowcol=context.utils.utils.getRowColFromIndex(data,indexStart);issue.setPosition(rowcol.row,rowcol.col,indexStart,indexEnd)};module.exports=Rule;