Last week, I was asked about getting CSS class names from a CSS file. This data was going to be used to create an XML file that populated the style menu of a rich text editor. I am not sure how to go from CSS class names to XML file (how do you automate the translation of CSS classes to "human-friendly" style options??). I can't figure that one out, but getting the CSS class names is relatively easy.
If you stop and think about how a CSS file is put together, you have your class names followed by rules. You can have multiple class names for the same rule such as "ul, ol { font-size: 11px ; }" where both UL and OL share the same rule. If we then think about the rule "{...}" as a single item, you can look as CSS data as dual-delimited list. The first delimiter is the rule construct - this separates the class names. Then, within each of those sub lists, each class name is delimited by a comma.
Using that, we can clean the CSS data and treat it like a simple list:
Launch code in new window » Download code as text file »
Running the above code gets us the two CFDumps for the master class array:
| | | | ||
| | ![]() | | ||
| | | |
... and the unique class array:
| | | | ||
| | ![]() | | ||
| | | |
That's all there is to it. Frankly, that was the easy step. How do you take that list and create a meaningful XML file? That I cannot answer.
Download Code Snippet ZIP File
Comments (14) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Calling ColdFusion Function Literals Like You Do In Javascript
When Does jQuery Consider The DOM Loaded And Can I Use It Beforehand
In your example, you technically do not have any class names. In CSS class names begin with a '.'
.header will style elements with an attribute of class equal to 'header'
#header will style elements with an attribute of id equal to 'header'
In your resulting array, there really were no classes shown. To use this list in a Rich Text Editor, you would probably want only the classes and not ids, or tags since the text editor would most likely use class="className" for an element.
Posted by Scott Stroz on Mar 16, 2007 at 8:47 AM
What Scott Said ;)
What you are getting are the selectors basically what rules are in the CSS file.
Posted by Sandra Clark on Mar 16, 2007 at 9:24 AM
The following code might do the trick as well:
<!--- Create an array to hold the CSS classes --->
<cfset aCSS = ArrayNew(1)>
<cfset start=1>
<!--- Find CSS class occurances (.something) --->
<cfloop condition="start lt len(strCSSData)">
<cfset temp = reFind("\.[-]?[_a-zA-Z][_a-zA-Z0-9-]*|[^\0-\177]*\\[0-9a-f]{1,6}(\r\n[ \n\r\t\f])?|\\[^\n\r\f0-9a-f]*",strCSSData,start,true)>
<cfif temp.pos[1]>
<cfset arrayappend(aCSS,mid(strCSSData,temp.pos[1],temp.len[1]))>
<cfset start = temp.pos[1]+temp.len[1]>
<cfelse>
<cfbreak>
</cfif>
</cfloop>
<cfdump var="#aCSS#">
Posted by Shlomy Gantz on Mar 16, 2007 at 9:51 AM
Big post. Please make it available full after clicking more and leave several lines for preview
Posted by Tourist on Mar 16, 2007 at 10:24 AM
Oh, by the way .... I am deeply offended by this entry... it does not contain any semi-nude photos whatsoever !!!
reference:http://www.bennadel.com/blog/574-How-Do-I-Offend-You-Please-Let-Me-Know.htm
Posted by Shlomy Gantz on Mar 16, 2007 at 10:31 AM
Yeah, I call them class names, but to be honest, I wasn't really thinking about what actually was a class name. I was thinking in my head "selector" but calling it class name.
If you think about it, class names are generally not enough for the ultimate goal here. Many "selectors" don't even have class names but should be totally valid markup (think about all the H tags).... this is part of why I would not be able to figure out the next step: translating this list to some sort of usable XML file.
Posted by Ben Nadel on Mar 16, 2007 at 11:34 AM
Ben -
If you have a CSS file that defined styles for tag based selectors, you wouldn't want to use those selectors as style options in a drop down for a rich text editor. Rather, you'd have options in the RTE for h1,h2,h3 (or large header, medium header, small header), which the user could then use. The user is going to define the formatting for their content (whether they want lists, or paragraphs, or whatever). The fact that you have style definitions for these doesn't matter (IMHO).
However, if you have custom class definitions -- say "caption" (small, bold letters), or "important" (large red letters) -- these would make sense to have available in a drop down pulled from the CSS file. In this case, you could actually use a comment string to give a "user-friendly" name for the class which would be used to populate the drop down.
Posted by Toby on Mar 16, 2007 at 1:22 PM
@Toby,
While I agree with in the most part, I think that letting the user choose H1/2/3/etc. might not be the best idea. As much as they are "standard", they are still left up to interpretation. So, your rich editor might have a style called "Page Title" or "Section Title" or "Item Title" or "Sub Title" or something along those lines. These might just put in the appropriate H tags. By letting the user arbitrarily choose what H tag they think is most appropriate, you might get screwy results. However, if you only allow them to choose "descriptive" titles (ie. Section Titlte), it might help to standardize (but of course, nothing is for sure).
Posted by Ben Nadel on Mar 16, 2007 at 1:34 PM
Wow, lots of food for thought!!! I was the guy who asked Ben if he had any clue how to go about doing this and who was more than a little surprised to get a complete working app about 20 minutes later (thanks Ben!) :->
Goal is indeed to get a drop down list of unique styles for a WYSIWYG editor, and ideal would be something like "H1,H2,H3,p,MyStyle1,MyStyle2,Mystyle3" which I can then drop into the trivial FCK Editor XML file which doesn't know anything about the properties of each class, but just allows you to tell it what the class names are so people can select them from a list.
Posted by Peter Bell on Mar 16, 2007 at 2:29 PM
I have never used FCK editor, so I leave advice up to the rest of you :)
Posted by Ben Nadel on Mar 16, 2007 at 2:30 PM
The problem with trying to allow your users to style their content in this way is that items get misapplied.
Its much better and easier for an end user not to have to worry about styles and simply apply structural HTML rather than CSS.
Not sure how you can do it with a WYSIWYG editor, but its a lot easier to have simple HTML for your users to create and leave the complexity of the styling to the style sheet itself.
One of my sessions at CFUNITED this year (CSS: Back to the Basics) is going to be covering Structural HTML and how to select it using advanced CSS Selectors - strangely enough ;)
Posted by Sandra Clark on Mar 16, 2007 at 2:35 PM
Another potential issue of allowing a user to use any/all selectors in a CSS file is the fact that, depending on the editor, it may assign class="p" to an element when chosen from the list of styles, yet if the style is only p{CSS stuff;} then the style would not be applied.
Peter, if FCK applies the styles as class="className", and the class is chosen from the drop down, then you would need to make sure only cvalid class names appear in the drop down, and not every selector.
Posted by Scott Stroz on Mar 16, 2007 at 3:48 PM
It might also be wise to have your CSS styles that will typically be applied by an end user in a separate CSS file, rather than in your layout CSS file(s) or others. You would then be running your regex on a much smaller file and you would have the added benefit of always knowing where to go to specify your user selectable styles too :)
Another thing to consider is that typically your css for a content area on a site might look like this:
#content p.extract {}
#content blockquote.feature {}
#content div.col1 {}
etc... or instead of using the id #content it might just be a class name .content. However, when you go to apply this to FCK editor the id/class will need to be stripped from the start of the selector - not terribly difficult, just make sure you can configure which id's/classes need to be stripped, so that you don't strip too much out of a rule, as in:
#content table.alt td.odd {}
You don't want to lose the "table.alt" part out of that selector, only the id (#content) part!
Depending on the project and the size of your CSS (this is gonna hurt), it's almost just easier to keep your CSS well organised and hand edit it. I realise that's not the point of this post, and Peter will kill me for even suggesting that (haha!), but sometimes (for simple projects) it's the truth :P
For a larger project though, or one where your users are competent enough and/or have the ability to edit the CSS themselves (for e.g. maybe in a FarCry site where CSS can be a content object, editable by users), a generated solution would be excellent.
Posted by Justin Carter on Mar 16, 2007 at 5:54 PM
Err, actually I should have said there is kinda 2 steps to applying styles to FCK editor. One is actually providing the CSS for the editor to apply when the user is editing the content, which is kinda what I was on about above (in regards to stripping the id/class from some selectors so that FCK editor can apply the styles - obviously you need to leave the stuff in the curly braces alone*), but the second part of the process is the XML, which I didn't realise (until now) was such a pain to edit... I am totally going to need a generated solution for this for use with a FarCry site of mine :P
Posted by Justin Carter on Mar 16, 2007 at 6:04 PM