Exemplo n.º 1
0
        /// <summary>
        /// Create a paragraph element with the specified text content.
        /// </summary>
        public static Element CreateText(string text, WebColor color)
        {
            var textElement = new Element {
                Tag           = Tag.Paragraph,
                ContentString = text
            };

            textElement.Style[StyleKey.Color] = color;
            return(textElement);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Validate the parameter. Error messages are added to the errors collection
        /// if there are issues with the parameter.
        /// </summary>
        public void Validate(IHttpPostParam parameter, ArrayRig <string> errors)
        {
            // is default validatation enabled?
            if (DefaultValidation)
            {
                // yes, is the parameter required?
                bool required = this._attributes.ContainsKey("required");

                if (required)
                {
                    // yes, was the parameter retrieved?
                    if (parameter == null)
                    {
                        // no, add an error
                        errors.Add("Missing a required parameter.");

                        // is the custom validation set?
                        if (OnValidate != null)
                        {
                            // yes, run the custom validation
                            OnValidate.Add(parameter, errors);
                            OnValidate.Run();
                        }

                        return;
                    }
                }
                else if (parameter == null)
                {
                    return;
                }

                string name;

                // validate the input value name
                if (parameter.Params.TryGetValue("name", out name))
                {
                    if (!name.Equals(this["name"], StringComparison.Ordinal))
                    {
                        errors.Add("Input element name mismatch.");
                        return;
                    }
                }
                else
                {
                    name = this["name"];
                    if (name == null)
                    {
                        errors.Add("Attribute 'name' missing.");
                        return;
                    }
                }

                // has the friendly name been assigned? yes, replace it in messages.
                if (!string.IsNullOrEmpty(FriendlyName))
                {
                    name = FriendlyName;
                }

                // get the post parameter as its correct type
                HttpPostParam <string>           paramString = parameter as HttpPostParam <string>;
                HttpPostParam <ArrayRig <byte> > paramBinary = null;
                if (paramString == null)
                {
                    paramBinary = parameter as HttpPostParam <ArrayRig <byte> >;
                }
                else if (string.IsNullOrEmpty(paramString.Value))
                {
                    // is the parameter required? yes, add an error
                    if (required)
                    {
                        errors.Add(name + " is required.");
                    }
                    // no, skip validation
                    else
                    {
                        return;
                    }
                }
                else if (paramString.Value.Length > MaxLength)
                {
                    errors.Add("Exceeded maximum characters (" + MaxLength + ") for " + name + ".");
                    return;
                }
                else if (paramString.Value.Length < MinLength)
                {
                    errors.Add("Entered value was shorter than the minimum length (" + MinLength + ") for " + name + ".");
                    return;
                }
                else if (Regex != null && !Regex.IsMatch(paramString.Value))
                {
                    // has the title for the input been specified?
                    errors.Add(this["title"] ?? "Invalid entry for " + name + ".");
                    return;
                }

                if ((paramString == null || paramString.Value == string.Empty) &&
                    (paramBinary == null || paramBinary.Value.Count == 0))
                {
                    if (required || _type == InputType.Hidden)
                    {
                        errors.Add(name + " is required.");
                    }
                    return;
                }

                // perform default validation based on type
                switch (_type)
                {
                case InputType.Checkbox:
                    if (paramString == null)
                    {
                        errors.Add(name + " is required.");
                        return;
                    }
                    if (!paramString.Value.Equals(this["value"]))
                    {
                        errors.Add(name + " has an incorrect value.");
                        return;
                    }
                    break;

                case InputType.Color:
                    if (paramString == null)
                    {
                        errors.Add("Please select a color for " + name + ".");
                        return;
                    }
                    WebColor color;
                    if (!WebColor.TryParse(paramString.Value, out color))
                    {
                        errors.Add("Incorrect hexadecimal color format. Should be #XXXXXXXX or #XXXXXX or #XXX.");
                        return;
                    }
                    break;

                case InputType.Date:
                    if (paramString == null)
                    {
                        errors.Add("Please select " + name + ".");
                        return;
                    }
                    DateTime date;
                    if (!DateTime.TryParse(paramString.Value, out date))
                    {
                        errors.Add("Received an incorrect date format.");
                        return;
                    }
                    break;

                case InputType.DatetimeLocal:
                    if (paramString == null)
                    {
                        errors.Add("Please select a date-time for " + name + ".");
                        return;
                    }
                    DateTime dateTime;
                    if (!DateTime.TryParse(paramString.Value, out dateTime))
                    {
                        errors.Add("Received an incorrect date-time format.");
                        return;
                    }
                    break;

                case InputType.Email:
                    if (paramString == null)
                    {
                        errors.Add(name + " is required.");
                        return;
                    }
                    if (!Efz.Validate.Email(paramString.Value))
                    {
                        errors.Add("Email appears malformed.");
                        return;
                    }
                    break;

                case InputType.File:
                    if (paramBinary == null)
                    {
                        errors.Add("You didn't choose a file for " + name + ".");
                        return;
                    }
                    if (paramBinary.Value.Count == 0)
                    {
                        errors.Add("Required file parameter is missing.");
                    }
                    string filename;
                    if (!paramBinary.Params.TryGetValue("filename", out filename) ||
                        !Fs.ParseFileName(ref filename))
                    {
                        errors.Add("The file name of " + name + " was invalid.");
                        return;
                    }
                    // ensure the content type matches accepted types
                    string contentType;
                    if (!paramBinary.Params.TryGetValue("content-type", out contentType))
                    {
                        errors.Add("Content type of " + name + " was not provided.");
                        return;
                    }
                    // check the accepted file types
                    string accepted;
                    if (_attributes.TryGetValue("accept", out accepted))
                    {
                        var extension = Mime.GetExtension(contentType);
                        if (extension == string.Empty)
                        {
                            errors.Add("Content type '" + contentType + "' was invalid.");
                            return;
                        }
                        if (!accepted.Contains(Chars.Stop + extension + Chars.Comma) && !accepted.EndsWith(Chars.Stop + extension, StringComparison.Ordinal))
                        {
                            errors.Add("Accepted file types include '" + accepted + "'.");
                            return;
                        }
                    }
                    break;

                case InputType.Month:
                    if (paramString == null)
                    {
                        errors.Add("You didn't choose a month for " + name + ".");
                        return;
                    }
                    if (paramString.Value.Length != 7 || !RegexMonth.IsMatch(paramString.Value))
                    {
                        errors.Add(name + " seems malformed.");
                        return;
                    }
                    if (paramString.Value[4] == Chars.Dash)
                    {
                        paramString.Value = paramString.Value.Substring(5) + Chars.Dash + paramString.Value.Substring(0, 4);
                    }
                    break;

                case InputType.Number:
                    int number;
                    if (!int.TryParse(paramString.Value, out number))
                    {
                        errors.Add("The " + name + " seems malformed.");
                        return;
                    }
                    if (_attributes.ContainsKey("min"))
                    {
                        int min = _attributes["min"].ToInt32();
                        if (number < min)
                        {
                            errors.Add(name + " was not within a valid range.");
                            return;
                        }
                    }
                    if (_attributes.ContainsKey("max"))
                    {
                        int max = _attributes["max"].ToInt32();
                        if (number > max)
                        {
                            errors.Add(name + " was not within a valid range.");
                            return;
                        }
                    }
                    if (_attributes.ContainsKey("step"))
                    {
                        int step = _attributes["step"].ToInt32();
                        if (number % step != 0)
                        {
                            errors.Add(name + " was not a valid value.");
                            return;
                        }
                    }
                    break;

                case InputType.Password:
                    if (paramString == null)
                    {
                        errors.Add(name + " is required.");
                        return;
                    }
                    if (!RegexPassword.IsMatch(paramString.Value))
                    {
                        errors.Add("Enter a password with at least four(4) characters.");
                        return;
                    }
                    break;

                case InputType.Range:
                    if (paramString == null)
                    {
                        errors.Add("Value required for " + name + ".");
                        return;
                    }
                    int range;
                    if (!int.TryParse(paramString.Value, out range))
                    {
                        errors.Add("Invalid " + name + " value received.");
                        return;
                    }
                    if (_attributes.ContainsKey("min"))
                    {
                        int min = _attributes["min"].ToInt32();
                        if (range < min)
                        {
                            errors.Add(name + " was not within a valid range.");
                            return;
                        }
                    }
                    if (_attributes.ContainsKey("max"))
                    {
                        int max = _attributes["max"].ToInt32();
                        if (range > max)
                        {
                            errors.Add(name + " was not within a valid range.");
                            return;
                        }
                    }
                    if (_attributes.ContainsKey("step"))
                    {
                        int step = _attributes["step"].ToInt32();
                        if (range % step != 0)
                        {
                            errors.Add(name + " was not a valid value.");
                            return;
                        }
                    }
                    break;

                case InputType.Tel:
                    if (paramString == null)
                    {
                        errors.Add(name + " is required.");
                        return;
                    }
                    if (!Efz.Validate.PhoneNumber(paramString.Value))
                    {
                        errors.Add(name + " was suspected of being invalid.");
                        return;
                    }
                    break;

                case InputType.Text:
                    if (paramString == null || paramString.Value.Length == 0)
                    {
                        errors.Add(name + " is required.");
                        return;
                    }
                    break;

                case InputType.Time:
                    if (paramString == null)
                    {
                        errors.Add(name + " is required.");
                        return;
                    }
                    if (paramString.Value.Length != 5 || !RegexTime.IsMatch(paramString.Value))
                    {
                        errors.Add(name + " was found to be invalid.");
                        return;
                    }
                    break;

                case InputType.Url:
                    if (paramString == null)
                    {
                        errors.Add(name + " is required.");
                        return;
                    }
                    if (paramString.Value.Length < 4 || paramString.Value.Length > 200 ||
                        !RegexUrl.IsMatch(paramString.Value))
                    {
                        errors.Add("Url was invalid.");
                        return;
                    }
                    break;

                case InputType.Week:
                    if (paramString == null)
                    {
                        errors.Add("Please select " + name + ".");
                        return;
                    }
                    if (paramString.Value.Length != 8 || !RegexWeek.IsMatch(paramString.Value))
                    {
                        errors.Add(name + " was malformed.");
                        return;
                    }
                    break;
                }
            }

            // is the custom validation set? no, complete
            if (OnValidate == null)
            {
                return;
            }

            // run the custom validation
            OnValidate.Add(parameter, errors);
            OnValidate.Run();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create a web color from the specified string representation.
        /// Returns '0' if the color isn't valid or the number of characters that constitute the color.
        /// </summary>
        public static int TryParse(string str, int index, out WebColor color)
        {
            if (str == null || str.Length - index < 3)
            {
                color = WebColor.Black;
                return(0);
            }

            byte[] bytes;
            if (str[index] == Chars.Hash)
            {
                if (str.Length - index < 4 ||
                    !Hex.IsHex(str[index + 1]) ||
                    !Hex.IsHex(str[index + 2]) ||
                    !Hex.IsHex(str[index + 3]))
                {
                    color = WebColor.Black;
                    return(0);
                }

                if (str.Length - index < 7 ||
                    !Hex.IsHex(str[index + 4]) ||
                    !Hex.IsHex(str[index + 5]) ||
                    !Hex.IsHex(str[index + 6]))
                {
                    byte bit = (byte)str[index + 1];
                    color.R = (byte)(bit < Ascii.Colon ? bit - Ascii.n0 : (bit < Ascii.G ? bit - 55 : bit - 87) * 16);
                    bit     = (byte)str[index + 2];
                    color.G = (byte)(bit < Ascii.Colon ? bit - Ascii.n0 : (bit < Ascii.G ? bit - 55 : bit - 87) * 16);
                    bit     = (byte)str[index + 3];
                    color.B = (byte)(bit < Ascii.Colon ? bit - Ascii.n0 : (bit < Ascii.G ? bit - 55 : bit - 87) * 16);
                    color.A = 255;
                    return(4);
                }

                if (str.Length - index < 9 ||
                    !Hex.IsHex(str[index + 7]) ||
                    !Hex.IsHex(str[index + 8]))
                {
                    bytes   = str.Substring(1, 6).ToBytes();
                    color.R = bytes[0];
                    color.G = bytes[1];
                    color.B = bytes[2];
                    color.A = 255;
                    return(7);
                }

                bytes   = str.Substring(1, 8).ToBytes();
                color.R = bytes[0];
                color.G = bytes[1];
                color.B = bytes[2];
                color.A = bytes[3];
                return(9);
            }

            if (str.Length - index < 3 ||
                !Hex.IsHex(str[index]) ||
                !Hex.IsHex(str[index + 1]) ||
                !Hex.IsHex(str[index + 2]))
            {
                color = WebColor.Black;
                return(0);
            }

            if (str.Length - index < 6 ||
                !Hex.IsHex(str[index + 3]) ||
                !Hex.IsHex(str[index + 4]) ||
                !Hex.IsHex(str[index + 5]))
            {
                byte bit = (byte)str[index + 1];
                color.R = (byte)(bit < Ascii.Colon ? bit - Ascii.n0 : (bit < Ascii.G ? bit - 55 : bit - 87) * 16);
                bit     = (byte)str[index + 2];
                color.G = (byte)(bit < Ascii.Colon ? bit - Ascii.n0 : (bit < Ascii.G ? bit - 55 : bit - 87) * 16);
                bit     = (byte)str[index + 3];
                color.B = (byte)(bit < Ascii.Colon ? bit - Ascii.n0 : (bit < Ascii.G ? bit - 55 : bit - 87) * 16);
                color.A = 255;
            }

            if (str.Length - index < 8 ||
                !Hex.IsHex(str[index + 6]) ||
                !Hex.IsHex(str[index + 7]))
            {
                bytes   = str.Substring(index, 6).ToBytes();
                color.R = bytes[0];
                color.G = bytes[1];
                color.B = bytes[2];
                color.A = 255;
                return(6);
            }

            bytes   = str.Substring(index, 8).ToBytes();
            color.R = bytes[0];
            color.G = bytes[1];
            color.B = bytes[2];
            color.A = bytes[3];

            return(8);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Create a web color from the specified string representation. Will throw
        /// if the string isn't valid.
        /// </summary>
        public static bool TryParse(string str, out WebColor color)
        {
            if (string.IsNullOrEmpty(str))
            {
                color = WebColor.Black;
                return(false);
            }
            if (str[0] == Chars.Hash)
            {
                if (str.Length == 4)
                {
                    if (!Hex.IsHex(str[1]) ||
                        !Hex.IsHex(str[2]) ||
                        !Hex.IsHex(str[3]))
                    {
                        color = WebColor.Black;
                        return(false);
                    }

                    byte bit = (byte)str[1];
                    color.R = (byte)(bit < Ascii.Colon ? bit - Ascii.n0 : (bit < Ascii.G ? bit - 55 : bit - 87) * 16);
                    bit     = (byte)str[2];
                    color.G = (byte)(bit < Ascii.Colon ? bit - Ascii.n0 : (bit < Ascii.G ? bit - 55 : bit - 87) * 16);
                    bit     = (byte)str[3];
                    color.B = (byte)(bit < Ascii.Colon ? bit - Ascii.n0 : (bit < Ascii.G ? bit - 55 : bit - 87) * 16);
                    color.A = 255;
                }
                else if (str.Length == 7)
                {
                    if (!Hex.IsHex(str[1]) ||
                        !Hex.IsHex(str[2]) ||
                        !Hex.IsHex(str[3]) ||
                        !Hex.IsHex(str[4]) ||
                        !Hex.IsHex(str[5]) ||
                        !Hex.IsHex(str[6]))
                    {
                        color = WebColor.Black;
                        return(false);
                    }

                    var bytes = str.Substring(1).ToBytes();
                    color.R = bytes[0];
                    color.G = bytes[1];
                    color.B = bytes[2];
                    if (bytes.Length == 4)
                    {
                        color.A = bytes[3];
                    }
                    else
                    {
                        color.A = 255;
                    }
                }
                else
                {
                    color = WebColor.Black;
                    return(false);
                }
            }
            else
            {
                if (str.Length == 3)
                {
                    if (!Hex.IsHex(str[0]) ||
                        !Hex.IsHex(str[1]) ||
                        !Hex.IsHex(str[2]))
                    {
                        color = WebColor.Black;
                        return(false);
                    }

                    byte bit = (byte)str[0];
                    color.R = (byte)(bit < Ascii.Colon ? bit - Ascii.n0 : (bit < Ascii.G ? bit - 55 : bit - 87) * 16);
                    bit     = (byte)str[1];
                    color.G = (byte)(bit < Ascii.Colon ? bit - Ascii.n0 : (bit < Ascii.G ? bit - 55 : bit - 87) * 16);
                    bit     = (byte)str[2];
                    color.B = (byte)(bit < Ascii.Colon ? bit - Ascii.n0 : (bit < Ascii.G ? bit - 55 : bit - 87) * 16);
                    color.A = 255;
                }
                else if (str.Length == 6)
                {
                    if (!Hex.IsHex(str[0]) ||
                        !Hex.IsHex(str[1]) ||
                        !Hex.IsHex(str[2]) ||
                        !Hex.IsHex(str[3]) ||
                        !Hex.IsHex(str[4]) ||
                        !Hex.IsHex(str[5]))
                    {
                        color = WebColor.Black;
                        return(false);
                    }

                    var bytes = str.ToBytes();
                    color.R = bytes[0];
                    color.G = bytes[1];
                    color.B = bytes[2];
                    if (bytes.Length > 3)
                    {
                        color.A = bytes[3];
                    }
                    else
                    {
                        color.A = 255;
                    }
                }
                else
                {
                    color = WebColor.Black;
                    return(false);
                }
            }
            return(true);
        }