regexjsouprailocfml

jSoup - How to get elements with background style (inline CSS)?


I'm building an app in Railo, which uses the jSoup .jar library. It all works really well in my CFML language.

Anyhow, I can grab every element with a "style" attribute doing:

<cfset variables.mySelection = variables.myDocument.select("*[style]") />

But this returns an array which contains elements that sometimes do not have a "background" or "background-image" style on them. As an example, the HTML might looks like so:

<p style="color: red;">I should not be selected</p>
<p style="background: green">I **should** be selected</p>
<p style="text-align: left;">I should not be selected</p>
<p style="background-image: url("/path/to/image.jpg");">I **should** be selected</p>

So I can get these elements above, but I don't want the 1st and 3rd in my array, as they don't have a background style...do you know how I can only grab and work with these?

Please note, I'm not after a COMPUTATED style, or anything that complicated, I'm just wondering if I can filter based on the properties of an inline CSS style. Perhaps some regex after the fact? I'm open to ideas!

I tried messing with :contains(background) as a key word, but I wasn't sure if that was the correct path?

Many thanks for your help. Michael.


Solution

  • Try with:

    variables.myDocument.select("*[style*='background']")
    

    As *= is the standard selector to match a substring in the attribute content.