1

I know that some would critic what I want, but I will ask my question.


I know that we can't get the style of an element if their css weren't formated inline. So I want a regex (because I am not very good to make one) that will get all the style tags and will apply a regex for their innerHTML to make an array like this one :

css["myDiv"]["color"] -> "red"

Without using getComputedStyle because of the browser incompatibility.

user1365010
  • 3,185
  • 8
  • 24
  • 43

1 Answers1

3

There is no way you achieve anything remotely close to this using regex. CSS rules are applied using a complicated algorithm and rules may be inherited, prioritized and overridden in various ways. It is no coincidence that it took the major browsers years to come up with a compliant implementation.

You should exercise exactly that implementation instead. The tool you need is JavaScript and the API you need is the DOM. For each node in the DOM there is a style object which contains all the applied styles and which has a format very close to what you are trying to generate. You can access this via getComputedStyle and currentStyle.

Mathias Schwarz
  • 7,099
  • 23
  • 28
  • can you add the cross-browser combination of getComputedStyle and currentStyle like snuffleupagus said and I will check your answer as accepted! – user1365010 Jun 06 '12 at 21:03
  • I will give you a link to a useful page that has the info and the code you need: http://www.quirksmode.org/dom/getstyles.html – Mathias Schwarz Jun 06 '12 at 21:10