@@ -4,10 +4,9 @@ const util = require('./util');
44
55const defaultOptions = {
66 allowBooleanAttributes : false , //A tag can have attributes without any value
7- localeRange : 'a-zA-Z' ,
87} ;
98
10- const props = [ 'allowBooleanAttributes' , 'localeRange' ] ;
9+ const props = [ 'allowBooleanAttributes' ] ;
1110
1211//const tagsPattern = new RegExp("<\\/?([\\w:\\-_\.]+)\\s*\/?>","g");
1312exports . validate = function ( xmlData , options ) {
@@ -16,12 +15,6 @@ exports.validate = function (xmlData, options) {
1615 //xmlData = xmlData.replace(/(\r\n|\n|\r)/gm,"");//make it single line
1716 //xmlData = xmlData.replace(/(^\s*<\?xml.*?\?>)/g,"");//Remove XML starting tag
1817 //xmlData = xmlData.replace(/(<!DOCTYPE[\s\w\"\.\/\-\:]+(\[.*\])*\s*>)/g,"");//Remove DOCTYPE
19- const localRangeRegex = new RegExp ( `[${ options . localeRange } ]` ) ;
20-
21- if ( localRangeRegex . test ( "<#$'\"\\\/:0" ) ) {
22- return getErrorObject ( 'InvalidOptions' , 'Invalid localeRange' , 1 ) ;
23- }
24-
2518 const tags = [ ] ;
2619 let tagFound = false ;
2720
@@ -32,8 +25,7 @@ exports.validate = function (xmlData, options) {
3225 // check for byte order mark (BOM)
3326 xmlData = xmlData . substr ( 1 ) ;
3427 }
35- const regxAttrName = new RegExp ( `^[${ options . localeRange } _][${ options . localeRange } 0-9_\\-\\.:]*$` ) ;
36- const regxTagName = new RegExp ( `^([${ options . localeRange } _])[${ options . localeRange } 0-9\\.\\-_:]*$` ) ;
28+
3729 for ( let i = 0 ; i < xmlData . length ; i ++ ) {
3830 if ( xmlData [ i ] === '<' ) {
3931 //starting of tag
@@ -78,7 +70,7 @@ exports.validate = function (xmlData, options) {
7870 //continue;
7971 i -- ;
8072 }
81- if ( ! validateTagName ( tagName , regxTagName ) ) {
73+ if ( ! validateTagName ( tagName ) ) {
8274 let msg ;
8375 if ( tagName . trim ( ) . length === 0 ) {
8476 msg = "There is an unnecessary space between tag name and backward slash '</ ..'." ;
@@ -98,7 +90,7 @@ exports.validate = function (xmlData, options) {
9890 if ( attrStr [ attrStr . length - 1 ] === '/' ) {
9991 //self closing tag
10092 attrStr = attrStr . substring ( 0 , attrStr . length - 1 ) ;
101- const isValid = validateAttributeString ( attrStr , options , regxAttrName ) ;
93+ const isValid = validateAttributeString ( attrStr , options ) ;
10294 if ( isValid === true ) {
10395 tagFound = true ;
10496 //continue; //text may presents after self closing tag
@@ -126,7 +118,7 @@ exports.validate = function (xmlData, options) {
126118 }
127119 }
128120 } else {
129- const isValid = validateAttributeString ( attrStr , options , regxAttrName ) ;
121+ const isValid = validateAttributeString ( attrStr , options ) ;
130122 if ( isValid !== true ) {
131123 //the result from the nested function returns the position of the error within the attribute
132124 //in order to get the 'true' error line, we need to calculate the position where the attribute begins (i - attrStr.length) and then add the position within the attribute
@@ -303,7 +295,7 @@ const validAttrStrRegxp = new RegExp('(\\s*)([^\\s=]+)(\\s*=)?(\\s*([\'"])(([\\s
303295
304296//attr, ="sd", a="amit's", a="sd"b="saf", ab cd=""
305297
306- function validateAttributeString ( attrStr , options , regxAttrName ) {
298+ function validateAttributeString ( attrStr , options ) {
307299 //console.log("start:"+attrStr+":end");
308300
309301 //if(attrStr.trim().length === 0) return true; //empty string
@@ -323,7 +315,7 @@ function validateAttributeString(attrStr, options, regxAttrName) {
323315 return { err: { code:"InvalidAttr",msg:"attribute " + matches[i][2] + " has no value assigned."}};
324316 } */
325317 const attrName = matches [ i ] [ 2 ] ;
326- if ( ! validateAttrName ( attrName , regxAttrName ) ) {
318+ if ( ! validateAttrName ( attrName ) ) {
327319 return getErrorObject ( 'InvalidAttr' , `Attribute '${ attrName } ' is an invalid name.` , getPositionFromMatch ( attrStr , matches [ i ] [ 0 ] ) ) ;
328320 }
329321 if ( ! attrNames . hasOwnProperty ( attrName ) ) {
@@ -382,19 +374,18 @@ function getErrorObject(code, message, lineNumber) {
382374 } ;
383375}
384376
385- function validateAttrName ( attrName , regxAttrName ) {
386- // const validAttrRegxp = new RegExp(regxAttrName);
387- return util . doesMatch ( attrName , regxAttrName ) ;
377+ function validateAttrName ( attrName ) {
378+ return util . isName ( attrName ) ;
388379}
389380
390381//const startsWithXML = new RegExp("^[Xx][Mm][Ll]");
391382// startsWith = /^([a-zA-Z]|_)[\w.\-_:]*/;
392383
393- function validateTagName ( tagname , regxTagName ) {
384+ function validateTagName ( tagname ) {
394385 /*if(util.doesMatch(tagname,startsWithXML)) return false;
395386 else*/
396387 //return !tagname.toLowerCase().startsWith("xml") || !util.doesNotMatch(tagname, regxTagName);
397- return ! util . doesNotMatch ( tagname , regxTagName ) ;
388+ return util . isName ( tagname ) ;
398389}
399390
400391//this function returns the line number for the character at the given index
0 commit comments