'Checks that the JQuery function_names (e.g. append(), prepend()) calls
are safe.
Arguments:
file_contents: The contents of the JavaScript file.
function_names: A pipe delimited list of names of the functions
(e.g. "wrap|after|before").
rule: The name of the rule to use for validation errors (e.g.
Rules.javascript_jquery_append).
is_caller_safe: A function to test if caller of the JQuery function
is safe.
is_argument_safe: A function to test if the argument passed to the
JQuery function is safe.
results: A file results objects to which violations will be added.'

def _check_jquery_function(self, file_contents, function_names, rule, is_caller_safe, is_argument_safe, results):

    regex = re.compile('(?<!HtmlUtils).(?:{})\\('.format(function_names))
    for function_match in regex.finditer(file_contents):
        is_violation = True
        expression = self._get_expression_for_function(file_contents, function_match)
        if (expression.end_index is not None):
            start_index = expression.start_index
            inner_start_index = function_match.end()
            close_paren_index = (expression.end_index - 1)
            function_argument = file_contents[inner_start_index:close_paren_index].strip()
            if ((is_argument_safe is not None) and (is_caller_safe is None)):
                is_violation = (is_argument_safe(function_argument) is False)
            elif ((is_caller_safe is not None) and (is_argument_safe is None)):
                line_start_index = StringLines(file_contents).index_to_line_start_index(start_index)
                caller_line_start = file_contents[line_start_index:start_index]
                is_violation = (is_caller_safe(caller_line_start) is False)
            else:
                raise ValueError('Must    supply    either    is_argument_safe,    or    is_caller_safe,    but    not    both.')
        if is_violation:
            results.violations.append(ExpressionRuleViolation(rule, expression))
