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

Function validateAttributeString

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

Source from the content-addressed store, hash-verified

409//attr, ="sd", a="amit's", a="sd"b="saf", ab cd=""
410
411function validateAttributeString(attrStr, options) {
412 //console.log("start:"+attrStr+":end");
413
414 //if(attrStr.trim().length === 0) return true; //empty string
415
416 const matches = scanAttributeTokens(attrStr);
417 const attrNames = {};
418
419 for (let i = 0; i < matches.length; i++) {
420 if (matches[i][1].length === 0) {
421 //nospace before attribute name: a="sd"b="saf"
422 return getErrorObject('InvalidAttr', "Attribute '" + matches[i][2] + "' has no space in starting.", getPositionFromMatch(matches[i]))
423 } else if (matches[i][3] !== undefined && matches[i][4] === undefined) {
424 return getErrorObject('InvalidAttr', "Attribute '" + matches[i][2] + "' is without value.", getPositionFromMatch(matches[i]));
425 } else if (matches[i][3] === undefined && !options.allowBooleanAttributes) {
426 //independent attribute: ab
427 return getErrorObject('InvalidAttr', "boolean attribute '" + matches[i][2] + "' is not allowed.", getPositionFromMatch(matches[i]));
428 }
429 /* else if(matches[i][6] === undefined){//attribute without value: ab=
430 return { err: { code:"InvalidAttr",msg:"attribute " + matches[i][2] + " has no value assigned."}};
431 } */
432 const attrName = matches[i][2];
433 if (!validateAttrName(attrName)) {
434 return getErrorObject('InvalidAttr', "Attribute '" + attrName + "' is an invalid name.", getPositionFromMatch(matches[i]));
435 }
436 if (!Object.prototype.hasOwnProperty.call(attrNames, attrName)) {
437 //check for duplicate attribute.
438 attrNames[attrName] = 1;
439 } else {
440 return getErrorObject('InvalidAttr', "Attribute '" + attrName + "' is repeated.", getPositionFromMatch(matches[i]));
441 }
442 }
443
444 return true;
445}
446
447function validateNumberAmpersand(xmlData, i) {
448 let re = /\d/;

Callers 1

validateFunction · 0.85

Calls 4

scanAttributeTokensFunction · 0.85
getErrorObjectFunction · 0.85
getPositionFromMatchFunction · 0.85
validateAttrNameFunction · 0.85

Tested by

no test coverage detected