# Post HTML Element Class Being Removed

**URL:** https://meta.discourse.org/t/post-html-element-class-being-removed/32644
**Category:** Support
**Created:** [August 28, 2015, 1:00pm UTC](https://meta.discourse.org/t/post-html-element-class-being-removed/32644 "2015-08-28T13:00:57Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![pnewelljr](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@pnewelljr](https://meta.discourse.org/u/pnewelljr)
#### Post date: [August 28, 2015, 1:00pm UTC](https://meta.discourse.org/t/post-html-element-class-being-removed/32644/1 "2015-08-28T13:00:57Z")

</div>

Anytime I try to add a class to an HTML element (div) in a post on the forum, the class is stripped when the post is displayed. How can I stop this from happening?

---

<div class="post-metadata">

### Author: ![pnewelljr](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@pnewelljr](https://meta.discourse.org/u/pnewelljr)
#### Post date: [August 28, 2015, 1:09pm UTC](https://meta.discourse.org/t/post-html-element-class-being-removed/32644/2 "2015-08-28T13:09:19Z")

</div>

I see I may need to “whitelist” the attribute or class? How exactly do I go about doing that?

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [August 28, 2015, 6:40pm UTC](https://meta.discourse.org/t/post-html-element-class-being-removed/32644/3 "2015-08-28T18:40:36Z")

</div>

> [@pnewelljr](#):
>
> How exactly do I go about doing that?

You could try putting together a plugin eg.

**plugins/progress/plugin.rb**

```plaintext
# name: progress
# about: allow progress tags in posts
# version: 0.1 
# authors: Mittineague 
# url: http://localhost.com:4000/ 

register_asset "javascripts/progress.js.es6", :server_side

```

**plugins/progress/javascripts/progress.js.es6**

```plaintext
Discourse.Markdown.whiteListTag("progress", "class", /d-prog/);
Discourse.Markdown.whiteListTag("progress", "max", /[0-9\.]+/);
Discourse.Markdown.whiteListTag("progress", "value", /[0-9\.]+/);

```

i.e. “tag”, “attribute”, /acceptable values/

\* the trickiest part may be getting the regex right

---

<div class="post-metadata">

### Author: ![pnewelljr](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@pnewelljr](https://meta.discourse.org/u/pnewelljr)
#### Post date: [September 3, 2015, 2:18pm UTC](https://meta.discourse.org/t/post-html-element-class-being-removed/32644/4 "2015-09-03T14:18:03Z")

</div>

I apreciate your response, is this a complete thing? Can I try implementing it? I am new to plugins and have no experience with ruby or regex.

[https://github.com/pnewell/discourse-html-whitelist/](https://github.com/pnewell/discourse-html-whitelist/)

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [September 3, 2015, 4:39pm UTC](https://meta.discourse.org/t/post-html-element-class-being-removed/32644/5 "2015-09-03T16:39:35Z")

</div>

It should work, but note the

> i.e. “tag”, “attribute”, /acceptable values/

The example code I posted allows something like

```plaintext
<progress class="d-prog" max="8" value="2">26%</progress>

```

For div tag class values you will need to use something more like

```plaintext
Discourse.Markdown.whiteListTag("div", "class", /[a-z]+/);

```

Except instead of allowing “a to z” which might allow too much you should fine-tune the regex to meet your needs. eg. if it will always only be “my-special-value” then this would be best

```plaintext
Discourse.Markdown.whiteListTag("div", "class", /my-special-value/);

```

---

<div class="post-metadata">

### Author: ![pnewelljr](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@pnewelljr](https://meta.discourse.org/u/pnewelljr)
#### Post date: [September 3, 2015, 4:43pm UTC](https://meta.discourse.org/t/post-html-element-class-being-removed/32644/6 "2015-09-03T16:43:37Z")

</div>

Great! Thanks, also to check, the folder structure should be discourse-html-whitelist/assets/javascript/progress.js.es6? Or discourse-html-whitelist/javascript/progress.js.es6?

My register line is:

register\_asset “javascript/progress.js.es6”, :server\_side

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [September 3, 2015, 5:17pm UTC](https://meta.discourse.org/t/post-html-element-class-being-removed/32644/7 "2015-09-03T17:17:40Z")

</div>

I don’t know if it _needs_ to be though I suspect it does (I went by other plugins that work)  
i.e. `register_asset` “auto-magically” looks for an “assets” sub-folder, so it exists in the folder structure but you don’t need to (shouldn’t) have that in the register\_asset path

Also, I guess you could name the folder “javascript” but every example I’ve seen uses the plural “javascripts”  
I imagine it is only because the folder often contains more, but it might also have something to do with the “auto-magically” loading of es6 files.

Finally, I named it “progress” but you should probably use a name more meaningful to what it’s doing. (though AFAIK any name should work)

---

<div class="post-metadata">

### Author: ![pnewelljr](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@pnewelljr](https://meta.discourse.org/u/pnewelljr)
#### Post date: [September 3, 2015, 5:26pm UTC](https://meta.discourse.org/t/post-html-element-class-being-removed/32644/8 "2015-09-03T17:26:54Z")

</div>

Great, thanks!

Finally, due to the other plugin I am working on, I actually need to also allow script and and link elements as well. I need some javascript to run on my posts, and it seems like if I add it to the head through Custom it only loads on posts when I refresh the page.

Anyways, would whitelisting those two work similarly?

The code I need to add looks like this:

```
<!-- Styles for the Mermaid (Flowchart) Plugin -->
<link rel="stylesheet" type="text/css" href="https://a1b162c9ba316380004223e5ebcd9fd144cbffea.googledrive.com/host/0B94LsQPLeilLfkxHY3djaE53TkEwcUZfbVY1MkxTa3Bpb25aclNhLWUzV2dLeEt4VlVFcWM/mermaid.min.css">

<!-- Custom Code to Remove <br> Tags from Mermaid (Flowchart) Posts -->
<script src="https://a1b162c9ba316380004223e5ebcd9fd144cbffea.googledrive.com/host/0B94LsQPLeilLfkxHY3djaE53TkEwcUZfbVY1MkxTa3Bpb25aclNhLWUzV2dLeEt4VlVFcWM/remove.mermaid.div.js"></script>

<!-- Code for the Mermaid (Flowchart) Plugin -->
<script src="https://a1b162c9ba316380004223e5ebcd9fd144cbffea.googledrive.com/host/0B94LsQPLeilLfkxHY3djaE53TkEwcUZfbVY1MkxTa3Bpb25aclNhLWUzV2dLeEt4VlVFcWM/mermaid.min.js"></script>

<!-- Code to Initialize the Mermaid (Flowchart) Plugin -->
<script>mermaid.initialize({startOnLoad:true});</script>

```

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [September 3, 2015, 5:35pm UTC](https://meta.discourse.org/t/post-html-element-class-being-removed/32644/9 "2015-09-03T17:35:06Z")

</div>

> [@pnewelljr](#):
>
> would whitelisting those two work similarly?

AFAIK whitelisting is for post content and is not involved with what is put into Admin → Customize

I am unfamiliar with the Mermaid (Flowchart) Plugin but my guess would be is that if you are having problems it is more likely a “onebox” issue.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [November 3, 2018, 9:13pm UTC](https://meta.discourse.org/t/post-html-element-class-being-removed/32644/10 "2018-11-03T21:13:23Z")

</div>



---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [November 3, 2018, 9:33pm UTC](https://meta.discourse.org/t/post-html-element-class-being-removed/32644/11 "2018-11-03T21:33:08Z")

</div>

Is this still a valid technique in late 2018 @Mittineague?

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [August 30, 2022, 3:54am UTC](https://meta.discourse.org/t/post-html-element-class-being-removed/32644/14 "2022-08-30T03:54:07Z")

</div>


