Exemplo n.º 1
0
        public void Fieldset(ComponentTestCaseData <OptionsJson.Fieldset> data) =>
        CheckComponentHtmlMatchesExpectedHtml(
            data,
            (generator, options) =>
        {
            var describedBy = options.DescribedBy;
            var role        = options.Role;

            var legendIsPageHeading = options.Legend?.IsPageHeading ??
                                      ComponentGenerator.FieldsetLegendDefaultIsPageHeading;

            var legendContent = options.Legend != null ?
                                TextOrHtmlHelper.GetHtmlContent(options.Legend.Text, options.Legend.Html) :
                                null;

            var legendAttributes = new AttributeDictionary()
                                   .MergeAttribute("class", options.Legend?.Classes);

            var content = TextOrHtmlHelper.GetHtmlContent(options.Text, options.Html) ?? _emptyContent;

            var attributes = options.Attributes.ToAttributesDictionary()
                             .MergeAttribute("class", options.Classes);

            return(generator.GenerateFieldset(
                       describedBy,
                       role,
                       legendIsPageHeading,
                       legendContent,
                       legendAttributes,
                       content,
                       attributes)
                   .RenderToString());
        });
        public void TextInput(ComponentTestCaseData <OptionsJson.TextInput> data) =>
        CheckComponentHtmlMatchesExpectedHtml(
            data,
            (generator, options) =>
        {
            var type = options.Type ?? ComponentGenerator.InputDefaultType;

            var prefixContent = options.Prefix != null ?
                                TextOrHtmlHelper.GetHtmlContent(options.Prefix.Text, options.Prefix.Html) :
                                null;

            var prefixAttributes = options.Prefix?.Attributes.ToAttributesDictionary()
                                   .MergeAttribute("class", options.Prefix?.Classes);

            var suffixContent = options.Suffix != null ?
                                TextOrHtmlHelper.GetHtmlContent(options.Suffix.Text, options.Suffix.Html) :
                                null;

            var suffixAttributes = options.Suffix?.Attributes.ToAttributesDictionary()
                                   .MergeAttribute("class", options.Suffix?.Classes);

            var attributes = options.Attributes.ToAttributesDictionary()
                             .MergeAttribute("class", options.Classes);

            var labelOptions = options.Label != null ?
                               options.Label with {
                For = options.Id
            } :
            null;
        public void Button(ComponentTestCaseData <OptionsJson.Button> data) =>
        CheckComponentHtmlMatchesExpectedHtml(
            data,
            (generator, options) =>
        {
            var isButtonLink = options.Element == "a" || options.Href != null;

            var isStartButton      = options.IsStartButton ?? ComponentGenerator.ButtonDefaultIsStartButton;
            var disabled           = options.Disabled ?? ComponentGenerator.ButtonDefaultDisabled;
            var preventDoubleClick = options.PreventDoubleClick ?? ComponentGenerator.ButtonDefaultPreventDoubleClick;
            var content            = TextOrHtmlHelper.GetHtmlContent(options.Text, options.Html) ?? _emptyContent;

            var attributes = options.Attributes.ToAttributesDictionary()
                             .MergeAttribute("class", options.Classes);

            if (isButtonLink)
            {
                attributes.MergeAttribute("href", options.Href ?? "#");
            }
            else
            {
                attributes.MergeAttribute("name", options.Name);
                attributes.MergeAttribute("type", options.Type);
                attributes.MergeAttribute("value", options.Value);
            }

            return((
                       isButtonLink ?
                       generator.GenerateButtonLink(isStartButton, disabled, content, attributes) :
                       generator.GenerateButton(isStartButton, disabled, preventDoubleClick, content, attributes)
                       ).RenderToString());
        });
        private static IHtmlContent BuildHint(ComponentGenerator generator, OptionsJson.Hint options)
        {
            var content = TextOrHtmlHelper.GetHtmlContent(options.Text, options.Html);

            var attributes = options.Attributes.ToAttributesDictionary()
                             .MergeAttribute("class", options.Classes);

            return(generator.GenerateHint(options.Id, content, attributes));
        }
Exemplo n.º 5
0
        public void Tag(ComponentTestCaseData <OptionsJson.Tag> data) =>
        CheckComponentHtmlMatchesExpectedHtml(
            data,
            (generator, options) =>
        {
            var content = TextOrHtmlHelper.GetHtmlContent(options.Text, options.Html);

            var attributes = options.Attributes.ToAttributesDictionary()
                             .MergeAttribute("class", options.Classes);

            return(generator.GenerateTag(content, attributes)
                   .RenderToString());
        });
        private static IHtmlContent BuildLabel(ComponentGenerator generator, OptionsJson.Label options)
        {
            var content = TextOrHtmlHelper.GetHtmlContent(options.Text, options.Html);

            var attributes = options.Attributes.ToAttributesDictionary()
                             .MergeAttribute("class", options.Classes);

            return(generator.GenerateLabel(
                       options.For,
                       options.IsPageHeading ?? ComponentGenerator.LabelDefaultIsPageHeading,
                       content,
                       attributes));
        }
        public void Checkboxes(ComponentTestCaseData <OptionsJson.Checkboxes> data) =>
        CheckComponentHtmlMatchesExpectedHtml(
            data,
            (generator, options) =>
        {
            var idPrefix = options.IdPrefix ?? options.Name;

            var items = options.Items
                        .Select((item, index) =>
                                item.Divider != null ?
                                (CheckboxesItemBase) new CheckboxesItemDivider()
            {
                Content = new HtmlString(item.Divider)
            } :
                                (CheckboxesItemBase) new CheckboxesItem()
            {
                Behavior    = item.Behaviour == "exclusive" ? CheckboxesItemBehavior.Exclusive : CheckboxesItemBehavior.Default,
                Conditional = item.Conditional?.Html != null ?
                              new CheckboxesItemConditional()
                {
                    Content = new HtmlString(item.Conditional.Html)
                } :
                null,
                LabelContent = TextOrHtmlHelper.GetHtmlContent(item.Text, item.Html),
                Hint         = item.Hint != null ?
                               new CheckboxesItemHint()
                {
                    Attributes = item.Hint.Attributes?.ToAttributesDictionary(),
                    Content    = TextOrHtmlHelper.GetHtmlContent(item.Hint.Text, item.Hint.Html)
                } :
                null,
                Id = item.Id,
                InputAttributes = item.Attributes.ToAttributesDictionary(),
                LabelAttributes = item.Label?.Attributes.ToAttributesDictionary()
                                  .MergeAttribute("class", item.Label?.Classes),
                Checked  = item.Checked ?? ComponentGenerator.CheckboxesItemDefaultChecked,
                Disabled = item.Disabled ?? ComponentGenerator.CheckboxesItemDefaultDisabled,
                Name     = item.Name,
                Value    = item.Value ?? string.Empty
            }
                                );

            var attributes = options.Attributes.ToAttributesDictionary()
                             .MergeAttribute("class", options.Classes);

            var hintOptions = options.Hint != null ?
                              options.Hint with {
                Id = idPrefix + "-hint"
            } :
            null;
Exemplo n.º 8
0
        public void WarningText(ComponentTestCaseData <OptionsJson.WarningText> data) =>
        CheckComponentHtmlMatchesExpectedHtml(
            data,
            (generator, options) =>
        {
            var iconFallbackText = options.IconFallbackText;
            var content          = TextOrHtmlHelper.GetHtmlContent(options.Text, options.Html) ?? _emptyContent;

            var attributes = options.Attributes.ToAttributesDictionary()
                             .MergeAttribute("class", options.Classes);

            return(generator.GenerateWarningText(iconFallbackText, content, attributes)
                   .RenderToString());
        });
Exemplo n.º 9
0
        public void SkipLink(ComponentTestCaseData <OptionsJson.SkipLink> data) =>
        CheckComponentHtmlMatchesExpectedHtml(
            data,
            (generator, options) =>
        {
            var href = options.Href ?? ComponentGenerator.SkipLinkDefaultHref;

            var attributes = options.Attributes.ToAttributesDictionary()
                             .MergeAttribute("class", options.Classes);

            var content = TextOrHtmlHelper.GetHtmlContent(options.Text, options.Html) ?? new HtmlString("");

            return(generator.GenerateSkipLink(href, content, attributes)
                   .RenderToString());
        });
        public void Panel(ComponentTestCaseData <OptionsJson.Panel> data) =>
        CheckComponentHtmlMatchesExpectedHtml(
            data,
            (generator, options) =>
        {
            var headingLevel = options.HeadingLevel.GetValueOrDefault(ComponentGenerator.PanelDefaultHeadingLevel);
            var titleContent = TextOrHtmlHelper.GetHtmlContent(options.TitleText, options.TitleHtml);
            var content      = TextOrHtmlHelper.GetHtmlContent(options.Text, options.Html);

            var attributes = options.Attributes.ToAttributesDictionary()
                             .MergeAttribute("class", options.Classes);

            return(generator.GeneratePanel(headingLevel, titleContent, content, attributes)
                   .RenderToString());
        });
        private static IHtmlContent BuildErrorMessage(ComponentGenerator generator, OptionsJson.ErrorMessage options)
        {
            var visuallyHiddenText = options.VisuallyHiddenText switch
            {
                bool flag when flag == false => string.Empty,
                string str => str,
                _ => ComponentGenerator.ErrorMessageDefaultVisuallyHiddenText
            };

            var content = TextOrHtmlHelper.GetHtmlContent(options.Text, options.Html);

            var attributes = options.Attributes.ToAttributesDictionary()
                             .MergeAttribute("class", options.Classes)
                             .MergeAttribute("id", options.Id);

            return(generator.GenerateErrorMessage(visuallyHiddenText, content, attributes));
        }
    }
        public void PhaseBanner(ComponentTestCaseData <OptionsJson.PhaseBanner> data) =>
        CheckComponentHtmlMatchesExpectedHtml(
            data,
            (generator, options) =>
        {
            var tagContent = TextOrHtmlHelper.GetHtmlContent(options.Tag?.Text, options.Tag?.Html) ??
                             _emptyContent;

            var tagAttributes = options.Tag?.Attributes.ToAttributesDictionary()
                                .MergeAttribute("class", options.Tag?.Classes);

            var content = TextOrHtmlHelper.GetHtmlContent(options.Text, options.Html);

            var attributes = options.Attributes.ToAttributesDictionary()
                             .MergeAttribute("class", options.Classes);

            return(generator.GeneratePhaseBanner(tagContent, tagAttributes, content, attributes)
                   .RenderToString());
        });
Exemplo n.º 13
0
        public void BackLink(ComponentTestCaseData <OptionsJson.BackLink> data) =>
        CheckComponentHtmlMatchesExpectedHtml(
            data,
            (generator, options) =>
        {
            var attributes = options.Attributes.ToAttributesDictionary()
                             .MergeAttribute("class", options.Classes);

            if (!attributes.ContainsKey("href"))
            {
                attributes.Add("href", "#");
            }

            var content = TextOrHtmlHelper.GetHtmlContent(options.Text, options.Html) ??
                          new HtmlString(ComponentGenerator.BackLinkDefaultContent);

            return(generator.GenerateBackLink(content, attributes)
                   .RenderToString());
        });
        public void SummaryList(ComponentTestCaseData <OptionsJson.SummaryList> data) =>
        CheckComponentHtmlMatchesExpectedHtml(
            data,
            (generator, options) =>
        {
            var attributes = options.Attributes.ToAttributesDictionary()
                             .MergeAttribute("class", options.Classes);

            var rows = options.Rows
                       .Select(r => new SummaryListRow()
            {
                Actions = new SummaryListRowActions()
                {
                    Items = (r.Actions?.Items
                             .Select(a => new SummaryListRowAction()
                    {
                        Attributes = a.Attributes.ToAttributesDictionary()
                                     .MergeAttribute("class", a.Classes)
                                     .MergeAttribute("href", a.Href),
                        Content = TextOrHtmlHelper.GetHtmlContent(a.Text, a.Html),
                        VisuallyHiddenText = a.VisuallyHiddenText
                    })).OrEmpty().ToList(),
                    Attributes = new AttributeDictionary().MergeAttribute("class", r.Actions?.Classes)
                },
                Attributes = new AttributeDictionary().MergeAttribute("class", r.Classes),
                Key        = new SummaryListRowKey()
                {
                    Content    = TextOrHtmlHelper.GetHtmlContent(r.Key.Text, r.Key.Html),
                    Attributes = new AttributeDictionary().MergeAttribute("class", r.Key.Classes)
                },
                Value = new SummaryListRowValue()
                {
                    Content    = TextOrHtmlHelper.GetHtmlContent(r.Value.Text, r.Value.Html),
                    Attributes = new AttributeDictionary().MergeAttribute("class", r.Value.Classes)
                }
            })
                       .OrEmpty();

            return(generator.GenerateSummaryList(attributes, rows)
                   .RenderToString());
        });
Exemplo n.º 15
0
        public void Breadcrumbs(ComponentTestCaseData <OptionsJson.Breadcrumbs> data) =>
        CheckComponentHtmlMatchesExpectedHtml(
            data,
            (generator, options) =>
        {
            var collapseOnMobile = options.CollapseOnMobile ?? ComponentGenerator.BreadcrumbsDefaultCollapseOnMobile;

            var attributes = options.Attributes.ToAttributesDictionary()
                             .MergeAttribute("class", options.Classes);

            var items = options.Items
                        .Select(i => new BreadcrumbsItem()
            {
                Content        = TextOrHtmlHelper.GetHtmlContent(i.Text, i.Html),
                Href           = i.Href,
                LinkAttributes = i.Attributes.ToAttributesDictionary()
            });

            return(generator.GenerateBreadcrumbs(collapseOnMobile, attributes, items)
                   .RenderToString());
        });
        public void NotificationBanner(ComponentTestCaseData <OptionsJson.NotificationBanner> data) =>
        CheckComponentHtmlMatchesExpectedHtml(
            data,
            (generator, options) =>
        {
            var type = options.Type == "success" ?
                       NotificationBannerType.Success :
                       NotificationBannerType.Default;

            var disableAutoFocus = options.DisableAutoFocus ??
                                   ComponentGenerator.NotificationBannerDefaultDisableAutoFocus;

            var titleContent = TextOrHtmlHelper.GetHtmlContent(options.TitleText, options.TitleHtml);

            // The 'text' option gets wrapped in a <p>
            var content = options.Html != null ?
                          new HtmlString(options.Html) :
                          options.Text != null ?
                          new HtmlString(
                "<p class=\"govuk-notification-banner__heading\">" +
                HtmlEncoder.Default.Encode(options.Text) +
                "</p>") :
                          null;

            var attributes = options.Attributes.ToAttributesDictionary()
                             .MergeAttribute("class", options.Classes);

            return(generator.GenerateNotificationBanner(
                       type,
                       options.Role,
                       disableAutoFocus,
                       options.TitleId,
                       options.TitleHeadingLevel ?? ComponentGenerator.NotificationBannerDefaultTitleHeadingLevel,
                       titleContent,
                       content,
                       attributes)
                   .RenderToString());
        });
Exemplo n.º 17
0
        public void Accordion(ComponentTestCaseData <OptionsJson.Accordion> data) =>
        CheckComponentHtmlMatchesExpectedHtml(
            data,
            (generator, options) =>
        {
            var headingLevel = options.HeadingLevel.GetValueOrDefault(ComponentGenerator.AccordionDefaultHeadingLevel);

            var attributes = options.Attributes.ToAttributesDictionary()
                             .MergeAttribute("class", options.Classes);

            var items = options.Items
                        .Select(i => new AccordionItem()
            {
                Content        = TextOrHtmlHelper.GetHtmlContent(i.Content.Text, i.Content.Html),
                Expanded       = i.Expanded ?? ComponentGenerator.AccordionItemDefaultExpanded,
                HeadingContent = i.Heading != null ? TextOrHtmlHelper.GetHtmlContent(i.Heading.Text, i.Heading.Html) : null,
                SummaryContent = i.Summary != null ? TextOrHtmlHelper.GetHtmlContent(i.Summary.Text, i.Summary.Html) : null
            })
                        .OrEmpty();

            return(generator.GenerateAccordion(options.Id, headingLevel, attributes, items)
                   .RenderToString());
        });
        public void ErrorSummary(ComponentTestCaseData <OptionsJson.ErrorSummary> data) =>
        CheckComponentHtmlMatchesExpectedHtml(
            data,
            (generator, options) =>
        {
            var titleContent = TextOrHtmlHelper.GetHtmlContent(options.TitleText, options.TitleHtml);

            var descriptionContent = TextOrHtmlHelper.GetHtmlContent(options.DescriptionText, options.DescriptionHtml);

            var items = options.ErrorList?.Select(i =>
            {
                var content = TextOrHtmlHelper.GetHtmlContent(i.Text, i.Html);

                var attributes = i.Attributes.ToAttributesDictionary();

                return(new ErrorSummaryItem()
                {
                    Content = content,
                    Href = i.Href,
                    LinkAttributes = attributes
                });
            }) ?? Enumerable.Empty <ErrorSummaryItem>();

            var attributes = options.Attributes.ToAttributesDictionary()
                             .MergeAttribute("class", options.Classes);

            return(generator.GenerateErrorSummary(
                       options.DisableAutoFocus ?? ComponentGenerator.ErrorSummaryDefaultDisableAutoFocus,
                       titleContent,
                       titleAttributes: null,
                       descriptionContent,
                       descriptionAttributes: null,
                       attributes,
                       items)
                   .RenderToString());
        });
Exemplo n.º 19
0
        public void Details(ComponentTestCaseData <OptionsJson.Details> data) =>
        CheckComponentHtmlMatchesExpectedHtml(
            data,
            (generator, options) =>
        {
            var open = options.Open ?? ComponentGenerator.DetailsDefaultOpen;

            var summaryContent = TextOrHtmlHelper.GetHtmlContent(options.SummaryText, options.SummaryHtml);

            var textContent = TextOrHtmlHelper.GetHtmlContent(options.Text, options.Html);

            var attributes = options.Attributes.ToAttributesDictionary()
                             .MergeAttribute("class", options.Classes)
                             .MergeAttribute("id", options.Id);

            return(generator.GenerateDetails(
                       open,
                       summaryContent,
                       summaryAttributes: null,
                       textContent,
                       textAttributes: null,
                       attributes)
                   .RenderToString());
        });
        private string GenerateFormGroup(
            Label label,
            Hint hint,
            ErrorMessage errorMessage,
            FormGroup formGroup,
            Fieldset fieldset,
            GenerateFormGroupElement generateElement)
        {
            var haveError = errorMessage != null;

            string describedBy = null;

            var attributes = new AttributeDictionary()
                             .MergeAttribute("class", formGroup?.Classes);

            var contentBuilder = new HtmlContentBuilder();

            if (label != null)
            {
                var labelTagBuilder = BuildLabel(_componentGenerator, label);
                contentBuilder.AppendHtml(labelTagBuilder);
            }

            if (hint != null)
            {
                var hintTagBuilder = BuildHint(_componentGenerator, hint);
                contentBuilder.AppendHtml(hintTagBuilder);

                AppendToDescribedBy(ref describedBy, hint.Id);
            }

            if (errorMessage != null)
            {
                var errorMessageTagBuilder = BuildErrorMessage(_componentGenerator, errorMessage);
                contentBuilder.AppendHtml(errorMessageTagBuilder);

                AppendToDescribedBy(ref describedBy, errorMessage.Id);
            }

            var element = generateElement(haveError, describedBy);

            contentBuilder.AppendHtml(element);

            IHtmlContent content = contentBuilder;

            if (fieldset != null)
            {
                AppendToDescribedBy(ref describedBy, fieldset.DescribedBy);

                content = _componentGenerator.GenerateFieldset(
                    describedBy,
                    role: fieldset.Role,
                    fieldset.Legend?.IsPageHeading ?? ComponentGenerator.FieldsetLegendDefaultIsPageHeading,
                    legendContent: TextOrHtmlHelper.GetHtmlContent(
                        fieldset.Legend?.Text,
                        fieldset.Legend?.Html),
                    legendAttributes: new AttributeDictionary()
                    .MergeAttribute("class", fieldset.Legend?.Classes),
                    content: contentBuilder,
                    attributes: fieldset.Attributes.ToAttributesDictionary()
                    .MergeAttribute("class", fieldset.Classes));
            }

            return(_componentGenerator.GenerateFormGroup(
                       haveError,
                       content,
                       attributes)
                   .RenderToString());
        }