﻿/*
(c) http://kheladi.com
All Rights Reserved

Checks whether a string contains tag or not. It also
allows to allow certain tags.
*/

function HtmlValidator() {

    this.allowedTags = new Array();

    this.allowedTags[0] = "br";
    this.allowedTags[1] = "strong";
    this.allowedTags[2] = "em";
    this.allowedTags[3] = "i";
}

HtmlValidator.prototype.IsValid = function(html) {
    var valid = true;
    if (html != null) {

        var start = 0;

        //search for
        var sindex = html.indexOf('<', start);
        var eindex = 0;
        while (sindex != -1) {
            eindex = html.indexOf('>', sindex);
            if (eindex != -1) {
                var tag = html.substr(sindex + 1, eindex - sindex - 1);

                //if the tag starts with '/' or ends with '/', remove it
                if (tag != null) {
                    if (tag[0] == '/') {
                        tag = tag.substr(1, tag.length - 1);
                    }
                    else if (tag[tag.length - 1] == '/') {
                        tag = tag.substr(0, tag.length - 1);
                    }
                }

                if (!this.IsTagAllowed(tag)) {
                    valid = false;
                    break;
                }
                sindex = html.indexOf('<', eindex);
            }
            else {
                //not valid html as there is no ending tag
                valid = false;
                break;
            }

        } //while

    }
    return valid;
}

HtmlValidator.prototype.IsTagAllowed = function(tag) {
    var allowed = false;
    for (var i = 0; i < this.allowedTags.length; i++) {
        if (this.allowedTags[i] == tag) {
            allowed = true;
            break;
        }
    }
    return allowed;
}

HtmlValidator.prototype.GetAllowedTags = function() {
    var allowed = "";

    for (var i = 0; i < this.allowedTags.length; i++) {
        if (i != 0) {
            allowed += ", ";
        }
        allowed += this.allowedTags[i];
    }

    return allowed;
}
