/// <summary> /// Ctor /// </summary> /// <remarks></remarks> public EcmaScriptIdentifier(string ident) { this.identifier = String.IsNullOrEmpty(ident) ? String.Empty : EcmaScriptIdentifier.VerifyIdentifier(ident, true); }
protected override void WritePropertyName(TextWriter writer, string propertyName) { if (EcmaScriptIdentifier.IsValidIdentifier(propertyName, false)) { // write out without quoting writer.Write(propertyName); } else { // write out as an escaped string base.WritePropertyName(writer, propertyName); } }
/// <summary> /// Verifies is a valid EcmaScript variable expression /// </summary> /// <param name="ident">the identifier</param> /// <returns>identifier</returns> /// <remarks> /// http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf /// /// IdentifierName = /// IdentifierStart | IdentifierName IdentifierPart /// IdentifierStart = /// Letter | '$' | '_' /// IdentifierPart = /// IdentifierStart | Digit /// </remarks> public static bool IsValidIdentifier(string ident, bool nested) { if (String.IsNullOrEmpty(ident)) { return(false); } if (nested) { string[] parts = ident.Split('.'); foreach (string part in parts) { if (!EcmaScriptIdentifier.IsValidIdentifier(part, false)) { return(false); } } return(true); } if (EcmaScriptIdentifier.IsReservedWord(ident)) { return(false); } bool indentPart = false; for (int i = 0, length = ident.Length; i < length; i++) { char ch = ident[i]; if (indentPart && ((ch >= '0') && (ch <= '9'))) { // digits are only allowed after first char continue; } // can be start or part if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) || (ch == '_') || (ch == '$')) { indentPart = true; continue; } return(false); } return(true); }
/// <summary> /// Compares identifiers /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Equals(object obj) { EcmaScriptIdentifier that = obj as EcmaScriptIdentifier; if (that == null) { return(base.Equals(obj)); } if (String.IsNullOrEmpty(this.identifier) && String.IsNullOrEmpty(that.identifier)) { // null and String.Empty are equivalent return(true); } return(StringComparer.Ordinal.Equals(this.identifier, that.identifier)); }
/// <summary> /// Verifies is a valid EcmaScript identifier /// </summary> /// <param name="ident">the identifier</param> /// <returns>identifier</returns> public static string VerifyIdentifier(string ident, bool nested, bool throwOnEmpty) { if (String.IsNullOrEmpty(ident)) { if (throwOnEmpty) { throw new ArgumentException("Identifier is empty."); } return(String.Empty); } ident = ident.Replace(" ", ""); if (!EcmaScriptIdentifier.IsValidIdentifier(ident, nested)) { throw new ArgumentException("Identifier \"" + ident + "\" is not supported."); } return(ident); }
/// <summary> /// Verifies is a valid EcmaScript identifier /// </summary> /// <param name="ident">the identifier</param> /// <returns>identifier</returns> public static string VerifyIdentifier(string ident, bool nested) { return(EcmaScriptIdentifier.VerifyIdentifier(ident, nested, true)); }