Forum Moderators: open

Message Too Old, No Replies

Removing a class if it doesn't exist in CSS

         

csdude55

8:34 pm on Jul 31, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I can scrape a string that's made of HTML, and pull out a list of classes that are assigned to elements in that string. But can you guys suggest a way that I might determine whether that class actually exists in MY CSS?

This is still with a contenteditable... users copy and paste something from another site and it brings over class names that aren't defined on my end. It doesn't break anything, but it's just a waste of space.

I thought about doing this in PHP by actually reading the CSS file, make an array of the classes, and then removing the ones that don't exist. But I'd rather do it client-side so that the user sees the finished product before they submit.

I'm using jQuery 3.3.1 if that helps.

tangor

1:52 am on Aug 1, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why identify? Strip all but text would be my first option... cut and paste is not "new content", and even if that is not a requirement/consideration, cut and paste can carry all kinds of nasties. Just my two cents.

csdude55

2:53 am on Aug 1, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's a message board, so they can pretty much discuss whatever they want. Sometimes they copy things from other parts of my site (like weather) to talk about it and I want to allow those classes. But sometimes they copy articles from news sites, and their classes don't usually translate.

tangor

4:56 am on Aug 1, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Again ... (dead horse beating) ... I never allow cut and paste with any kind of html/css included. "Just the Text, Ma'am" to paraphrase a popular tvi cop back in the day. Source makes no difference ONLY THE CONTENT does (hint, html or css is NOT content).\

After any insert to my site(s) (of which I allow few, then again my niche is not yours and I get what you are trying to do) I allow USER MARKUP--after the fact--(bold, large, italic) much like you can find here on Webmasterworld. Keeps it clean and simple ... and avoids legal issues possible if the content turns out malevolent. One can only know that if one moderates ... or strips out all the possible BEFORE content is posted.

Just sayin'.

Make it pure text and go from there.

csdude55

6:26 am on Aug 1, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've been allowing HTML and CSS for about 5 years with no real problems. I strip Javascript, of course, and remove all HTML tags except for

a|div|p|span|font|img|q|br|b|u|i


If I can figure out an easy way to exclude tags that are copied from my own sites then I would consider stripping out div|p|span, too, but I would need to leave the others regardless.

I'm tempted to go back and change all of my class names to prepend with something unique like .csdude55-className { }, and then I can strip any class that doesn't start with the marker. But that'll be a huge pain, so I was hoping there would be a Javascript or jQuery solution that's easier.