MCPcopy Create free account
hub / github.com/NaturalIntelligence/fast-xml-parser / validateAttributeString

Function validateAttributeString

src/validator.js:319–353  ·  view source on GitHub ↗
(attrStr, options)

Source from the content-addressed store, hash-verified

317//attr, ="sd", a="amit's", a="sd"b="saf", ab cd=""
318
319function validateAttributeString(attrStr, options) {
320 //console.log("start:"+attrStr+":end");
321
322 //if(attrStr.trim().length === 0) return true; //empty string
323
324 const matches = getAllMatches(attrStr, validAttrStrRegxp);
325 const attrNames = {};
326
327 for (let i = 0; i < matches.length; i++) {
328 if (matches[i][1].length === 0) {
329 //nospace before attribute name: a="sd"b="saf"
330 return getErrorObject('InvalidAttr', "Attribute '" + matches[i][2] + "' has no space in starting.", getPositionFromMatch(matches[i]))
331 } else if (matches[i][3] !== undefined && matches[i][4] === undefined) {
332 return getErrorObject('InvalidAttr', "Attribute '" + matches[i][2] + "' is without value.", getPositionFromMatch(matches[i]));
333 } else if (matches[i][3] === undefined && !options.allowBooleanAttributes) {
334 //independent attribute: ab
335 return getErrorObject('InvalidAttr', "boolean attribute '" + matches[i][2] + "' is not allowed.", getPositionFromMatch(matches[i]));
336 }
337 /* else if(matches[i][6] === undefined){//attribute without value: ab=
338 return { err: { code:"InvalidAttr",msg:"attribute " + matches[i][2] + " has no value assigned."}};
339 } */
340 const attrName = matches[i][2];
341 if (!validateAttrName(attrName)) {
342 return getErrorObject('InvalidAttr', "Attribute '" + attrName + "' is an invalid name.", getPositionFromMatch(matches[i]));
343 }
344 if (!Object.prototype.hasOwnProperty.call(attrNames, attrName)) {
345 //check for duplicate attribute.
346 attrNames[attrName] = 1;
347 } else {
348 return getErrorObject('InvalidAttr', "Attribute '" + attrName + "' is repeated.", getPositionFromMatch(matches[i]));
349 }
350 }
351
352 return true;
353}
354
355function validateNumberAmpersand(xmlData, i) {
356 let re = /\d/;

Callers 1

validateFunction · 0.85

Calls 4

getAllMatchesFunction · 0.90
getErrorObjectFunction · 0.85
getPositionFromMatchFunction · 0.85
validateAttrNameFunction · 0.85

Tested by

no test coverage detected