Converting A Single CSS Element Selector To XPath Using ColdFusion

Posted March 13, 2009 at 4:09 PM by Ben Nadel

Tags: ColdFusion

I wanted to explore the idea of converting natural CSS to XPath selectors for use in merging CSS with XHTML content. I don't think the concept is very complicated, but it does seem to have a bunch of little, moving parts. So, rather than solve all the problems at one time, I thought it would be good to break it down and start with the smallest problem first and then build on that to eventually convert natural CSS to XPath.

So, the smallest problem that I can see is converting a single element selector into XPath. For example, converting:

div

... into:

div

Or, converting:

div.form

... into:

div[ contains( @class, 'form' ) ]

This doesn't get into how these are grouped when it comes to contextual selecting (ex. P within a Div tag) - that's the next level up. Let's tackle the lowest level problem first, then we can worry about how to join these together to perform more complex searches.

When it comes to viable CSS selectors, I think we are dealing with about 6 different possibilties:

  • element#id
  • element.class
  • #id
  • .class
  • *.class
  • *

Thanks to the finite nature of this set, we can easily match these patterns against a simple CFIF / CFELSE statement with regular expressions. And so, I have taken that idea and wrapped it up in a ColdFusion user defined function, CSSElementSelectorToXPath():

  • <cffunction
  • name="CSSElementSelectorToXPath"
  • access="public"
  • returntype="string"
  • output="false"
  • hint="I convert a single element selector to XPath (ex. div.header).">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Selector"
  • type="string"
  • required="true"
  • hint="I am the single element selector."
  • />
  •  
  • <!--- Define the local scope. --->
  • <cfset var LOCAL = {} />
  •  
  • <!---
  • Trim the selector and remove any pseudo selector
  • information. While some of these can be applied
  • (ex. :event), for our purposes, we are not going to
  • apply them at this time.
  • --->
  • <cfset LOCAL.Selector = Trim(
  • REReplace(
  • ARGUMENTS.Selector,
  • ":[^\s]*",
  • "",
  • "one"
  • )
  • ) />
  •  
  • <!---
  • Check to see what kind of pattern we have here. As far
  • as CSS is concerned, we really only have six different
  • selectors to care about:
  •  
  • element#id
  • element.class
  • #id
  • .class
  • *.class
  • *
  • --->
  • <cfif REFind( "^\w+##.+$", LOCAL.Selector )>
  •  
  • <!--- Return ID selector. --->
  • <cfreturn (
  • ListFirst( LOCAL.Selector, "##" ) &
  • "[ @id = """ &
  • ListLast( LOCAL.Selector, "##" ) &
  • """ ]"
  • ) />
  •  
  • <cfelseif REFind( "^\w+\..+$", LOCAL.Selector )>
  •  
  • <!--- Return class selector. --->
  • <cfreturn (
  • ListFirst( LOCAL.Selector, "." ) &
  • "[ contains( @class, """ &
  • ListLast( LOCAL.Selector, "." ) &
  • """ ) ]"
  • ) />
  •  
  • <cfelseif REFind( "^##.+$", LOCAL.Selector )>
  •  
  • <!--- Return ANY ID selector. --->
  • <cfreturn (
  • "*[ @id = """ &
  • ListLast( LOCAL.Selector, "##" ) &
  • """ ) ]"
  • ) />
  •  
  • <cfelseif REFind( "^\..+$", LOCAL.Selector )>
  •  
  • <!--- Return ANY class selector. --->
  • <cfreturn (
  • "*[ contains( @class, """ &
  • ListLast( LOCAL.Selector, "." ) &
  • """ ) ]"
  • ) />
  •  
  • <cfelseif REFind( "^\*\..+$", LOCAL.Selector )>
  •  
  • <!--- Return ANY class selector. --->
  • <cfreturn (
  • "*[ contains( @class, """ &
  • ListLast( LOCAL.Selector, "." ) &
  • """ ) ]"
  • ) />
  •  
  • <cfelseif REFind( "^\w+$", LOCAL.Selector )>
  •  
  • <!--- Return element selector. --->
  • <cfreturn LOCAL.Selector />
  •  
  • <cfelse>
  •  
  • <!--- Not valid - return ANY selector. --->
  • <cfreturn "*" />
  •  
  • </cfif>
  • </cffunction>

As you can see, I am stripping out any pseudo selectors before the conversion. It is true that some pseudo selectors can be used (ex. :even, :odd, :first), but for now, we'll strip them out. Really, it wouldn't be that hard to put them in; just as with the element selectors, each one would have to be mapped to a specific XPath predicate:

:first ==> [ position() = 1 ]

More on that late. Now, to test to see if the above UDF works, I am gonna run some various CSS selectors through it:

  • <cfoutput>
  •  
  • div ==>
  • #CSSElementSelectorToXPath( "div" )#<br />
  •  
  • div##data-form ==>
  • #CSSElementSelectorToXPath( "div##data-form" )#<br />
  •  
  • div.data-form ==>
  • #CSSElementSelectorToXPath( "div.data-form" )#<br />
  •  
  • ##data-form ==>
  • #CSSElementSelectorToXPath( "##data-form" )#<br />
  •  
  • .data-form ==>
  • #CSSElementSelectorToXPath( ".data-form" )#<br />
  •  
  • *.data-form ==>
  • #CSSElementSelectorToXPath( "*.data-form" )#<br />
  •  
  • </cfoutput>

When the above code, we get the following output:

div ==> div
div#data-form ==> div[ @id = "data-form" ]
div.data-form ==> div[ contains( @class, "data-form" ) ]
#data-form ==> *[ @id = "data-form" ) ]
.data-form ==> *[ contains( @class, "data-form" ) ]
*.data-form ==> *[ contains( @class, "data-form" ) ]

Looks like it's working nice. Next step will be to combine the above and create contextual selectors.




Reader Comments

There are no comments posted for this web log entry.

Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Jun 19, 2013 at 2:01 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
I have coincidentally been beating my head against the S3 API for the last week or so. One big "gotcha" I had to work around was file names and paths containing spaces. Remember to URL Enco ... read »
Jun 19, 2013 at 1:27 PM
Using Slice(), Substring(), And Substr() In Javascript
very good article. By the way IE supports negative values in substr or slice in verson 10. ... read »
Jun 19, 2013 at 11:33 AM
Filter vs. ngHide With ngRepeat In AngularJS
In your assessment, is it correct to say that given a list of say 500 items its more performant to use the `ngHide` method over the `filter` method? ... read »
Jun 19, 2013 at 10:18 AM
ColdFusion Path Usage And Manipulation Overview
Anyone happen to know if the file created by getTempFile will be automatically removed at any point? Nothing mentioned in the docs, and restarting CF doesn't remove them, so it seems it needs manu ... read »
Jun 19, 2013 at 9:41 AM
Working With Inherited Collections In AngularJS
I actually just ran into this same situation with a demo I was putting together. Your implementation of multi-lvl $scope's > Mine :) ... read »
Jun 19, 2013 at 8:17 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Prateek, to match a word or text you should use .toContain('word') that's a jasmine reference. website is : http://pivotal.github.io/jasmine/ ... read »
Jun 19, 2013 at 8:10 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Hi Guys, Actually i am doing e2e test of angular js of my project but i am not getting one thing that is how to press enter key through the test when my form is filled as i am not using a button but ... read »
Jun 18, 2013 at 9:20 PM
Mapping AngularJS Routes Onto URL Parameters And Client-Side Events
I couldn't find examples of passing multiple arguments using the when() routing statement so figured out through trial and error that you can pass multiple arguments using the following format: .whe ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools