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

Taghelpers overwrite class attribute #17

Open
conorsheehansw opened this issue Oct 16, 2019 · 0 comments
Open

Taghelpers overwrite class attribute #17

conorsheehansw opened this issue Oct 16, 2019 · 0 comments

Comments

@conorsheehansw
Copy link

conorsheehansw commented Oct 16, 2019

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);
    }
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant