Print Templates, Part IV: User's Settings

Converting Style Rules to Objects

You can access style definitions in their object form. The STYLE tags are represented in a zero-based collection, document.styleSheets. Each STYLE definition may include one or more rules. Each of the STYLE definition's rules are represented as a zero-based collection, document.styleSheets[i].rules. The style rule's name is stored as a property of the rules[j] object. The property name is selectorText. Our print template includes the following STYLE definition:

We have four style rule objects: document.styleSheets[0].rules[0], document.styleSheets[0].rules[1], document.styleSheets[0].rules[2], and document.styleSheets[0].rules[3]. Their corresponding rule names can be accessed as follows:

The values of these properties are ".contentstyle", ".masterstyle", ".headerstyle", and ".footerstyle".

We want to store the above four objects in four object variables, oMasterStyleClass, oContentStyleClass, oFooterStyleClass, and oHeaderStyleClass, respectively. One way to do it is to go over all rule objects and search for the one you need, by name. The following findStyleRule() function does exactly that. It accepts a style rule name and returns the style rule object, styleSheets[i].rules[j]: