Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Taghelpers overwrite class attribute #17

@conorsheehansw

Description

@conorsheehansw

If an external tag helper (or if the user sets the class attribute on a tag directly) has set values for the class attribute, use of any of the taghelpers here will overwrite those values.

Have a change available to remedy this, can raise a pull request if I can be made a contributor here - or alternatively the suggested code change is as follows:

protected void SetClassAttribute(TagHelperOutput output, string attributeValue)
    {
      if (output.Attributes.TryGetAttribute(HtmlAttributes.ClassAttribute, out var currentClassValue))
      {
        attributeValue = $"{attributeValue} {currentClassValue.Value}";
      }

      output.Attributes.SetAttribute(HtmlAttributes.ClassAttribute, attributeValue);
    }

    protected void UpdateClasses(TagHelperOutput output)
    {
      const string space = " ";

      var prepended = "";
      var classesFromUser = string.IsNullOrWhiteSpace(Classes) ? "" : Classes;

      foreach (var item in ClassesToPrepend)
      {
        prepended += item + space;
      }

      var classes = prepended.Trim()
                    + (string.IsNullOrWhiteSpace(classesFromUser) ? "" : space + classesFromUser);

      SetClassAttribute(output, classes);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions