using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace mustache
{
///
/// Defines a tag that conditionally prints its content.
///
internal abstract class ConditionTagDefinition : TagDefinition
{
private const string conditionParameter = "condition";
///
/// Initializes a new instance of a ConditionTagDefinition.
///
/// The name of the tag.
protected ConditionTagDefinition(string tagName)
: base(tagName, true)
{
}
///
/// Gets the parameters that can be passed to the tag.
///
/// The parameters.
protected override TagParameter[] GetParameters()
{
return new TagParameter[] { new TagParameter(conditionParameter) { IsRequired = true } };
}
///
/// Gets whether the tag will contain content.
///
public override bool HasBody
{
get { return true; }
}
///
/// Gets the tags that come into scope within the context of the current tag.
///
/// The child tag definitions.
protected override TagDefinition[] GetChildTags()
{
return new TagDefinition[]
{
new ElifTagDefinition(),
new ElseTagDefinition(),
};
}
///
/// Gets whether the given tag's generator should be used for a secondary (or substitute) text block.
///
/// The tag to inspect.
/// True if the tag's generator should be used as a secondary generator.
public override bool ShouldCreateSecondaryGroup(TagDefinition definition)
{
return (definition is ElifTagDefinition) || (definition is ElseTagDefinition);
}
///
/// Gets whether the primary generator group should be used to render the tag.
///
/// The arguments passed to the tag.
///
/// True if the primary generator group should be used to render the tag;
/// otherwise, false to use the secondary group.
///
public override bool ShouldGeneratePrimaryGroup(Dictionary arguments)
{
object condition = arguments[conditionParameter];
return isConditionSatisfied(condition);
}
private bool isConditionSatisfied(object condition)
{
if (condition == null)
{
return false;
}
IEnumerable enumerable = condition as IEnumerable;
if (enumerable != null)
{
return enumerable.Cast