/*
 * Copyright (c) 2018, 2025, 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="oj-html-attr-json";const DOUBLE_BRACE="{{";const DOUBLE_BRACKET="[[";const EXPLAIN="When defining a JSON string as an attribute value for a component tag, the JSON must be valid. Expressions enclosed by [[ ]] or {{ }} are not validated."+"<p>"+"When defining a JSON string as an attribute value, the JSON property names (and values where appropriate) must be enclosed with double quotes. "+"This requires that the entire JSON string be delimited with single quotes<br>. For example:<br>"+"<i>options-keys=\"{'label': 'email', 'value': 'id'}\"</i> is invalid, and should be specified as "+"<i>options-keys='{\"label\": \"email\", \"value\": \"id\"}'</i>.<p>"+"Note: this does not apply when an expression is enclosed within [[ ]] or {{ }}, so these are valid:<br>"+"options-keys='[[ {label: 'email', value: 'id'} ]]'<br>"+"options-keys='[[ {label: \"email\", value: \"id\"} ]]'";function getName(){return RULE_NAME};function getShortDescription(){return"Check attribute for starting quote and containing well formed json"};function getDescription(){return EXPLAIN};function register(){return{"webcomp":_onWComp}};function _onWComp(ruleCtx,tag){var attrs,attr;if(ruleCtx.tagNode.attribs){attrs=ruleCtx.tagNode.attribs;for(attr in attrs){_checkAttr(ruleCtx,tag,attr,attrs[attr])}}};function _checkAttr(ruleCtx,tag,attr,attrValue){var rc,issue,val,attrRawVal,start;val=attrValue.replace(/^\s+/g,"");start=val.charAt(0);if(start!=="{"&&start!=="["||_isExprStart(attrValue)||attr=="data-bind"){return}rc=ruleCtx.utils.DomUtils.isValidJson(attrValue);if(typeof rc==="string"){attrRawVal=ruleCtx.utils.DomUtils.getAttribRawValue(ruleCtx,attr);if(attrRawVal.charAt(0)!=="'"){issue=new ruleCtx.Issue(`<${tag}> attribute '${attr}' containing JSON must be delimited with single-quotes.`);issue.setMsgKey(RULE_NAME+"_1")}else{issue=new ruleCtx.Issue(`<${tag}> attribute '${attr}' JSON error : (${rc})`);issue.setMsgKey(RULE_NAME+"_2")}}if(issue){let attrPos=ruleCtx.utils.DomUtils.getAttribPosition(ruleCtx.data,ruleCtx.tagNode,attr);issue.setPosition(attrPos.row,attrPos.col,attrPos.start,attrPos.end);ruleCtx.reporter.addIssue(issue,ruleCtx)}};function _isExprStart(attrValue){return attrValue.startsWith(DOUBLE_BRACKET)||attrValue.startsWith(DOUBLE_BRACE)};module.exports={getName,getDescription,getShortDescription,register};