Show:

Create an instance of StyleSheet to encapsulate a css stylesheet. The constructor can be called using function or constructor syntax. <pre><code>var sheet = Y.StyleSheet(..);</pre></code> or <pre><code>var sheet = new Y.StyleSheet(..);</pre></code>

The first parameter passed can be any of the following things: <ul> <li>The desired string name to register a new empty sheet</li> <li>The string name of an existing StyleSheet instance</li> <li>The unique guid generated for an existing StyleSheet instance</li> <li>The id of an existing <code><link></code> or <code><style></code> node</li> <li>The node reference for an existing <code><link></code> or <code><style></code> node</li> <li>The Y.Node instance wrapping an existing <code><link></code> or <code><style></code> node</li> <li>A chunk of css text to create a new stylesheet from</li> </ul>

<p>If a string is passed, StyleSheet will first look in its static name registry for an existing sheet, then in the DOM for an element with that id. If neither are found and the string contains the { character, it will be used as a the initial cssText for a new StyleSheet. Otherwise, a new empty StyleSheet is created, assigned the string value as a name, and registered statically by that name.</p>

<p>The optional second parameter is a string name to register the sheet as. This param is largely useful when providing a node id/ref or chunk of css text to create a populated instance.</p>

Constructor

StyleSheet

(
  • seed
  • name
)

Parameters:

  • seed String | HTMLElement | Node

    a style or link node, its id, or a name or guid of a StyleSheet, or a string of css text

  • name String

    (optional) name to register instance for future static access

Methods

disable

() StyleSheet chainable

Disable all the rules in the sheet. Rules may be changed while the StyleSheet is disabled.

Returns:

enable

() StyleSheet chainable

Enable all the rules in the sheet

Returns:

getCssText

(
  • sel
)
String

Get the current cssText for a rule or the entire sheet. If the selector param is supplied, only the cssText for that rule will be returned, if found. If the selector string targets multiple selectors separated by commas, the cssText of the first rule only will be returned. If no selector string, the stylesheet's full cssText will be returned.

Parameters:

  • sel String

    Selector string

Returns:

String:

getId

() Number

Get the unique stamp for this StyleSheet instance

Returns:

Number:

the static id

isEnabled

() Boolean

Returns false if the StyleSheet is disabled. Otherwise true.

Returns:

Boolean:

isValidSelector

(
  • sel
)
Boolean static

<p>Determines if a selector string is safe to use. Used internally in set to prevent IE from locking up when attempting to add a rule for a "bad selector".</p>

<p>Bad selectors are considered to be any string containing unescaped `~!@$%^&()+=|{}[];'"?< or space. Also forbidden are . or # followed by anything other than an alphanumeric. Additionally -abc or .-abc or #_abc or '# ' all fail. There are likely more failure cases, so please file a bug if you encounter one.</p>

Parameters:

  • sel String

    the selector string

Returns:

Boolean:

register

(
  • name
  • sheet
)
Boolean static

Registers a StyleSheet instance in the static registry by the given name

Parameters:

  • name String

    the name to assign the StyleSheet in the registry

  • sheet StyleSheet

    The StyleSheet instance

Returns:

Boolean:

false if no name or sheet is not a StyleSheet instance. true otherwise.

set

(
  • sel
  • css
)
StyleSheet chainable

<p>Set style properties for a provided selector string. If the selector includes commas, it will be split into individual selectors and applied accordingly. If the selector string does not have a corresponding rule in the sheet, it will be added.</p>

<p>The object properties in the second parameter must be the JavaScript names of style properties. E.g. fontSize rather than font-size.</p>

<p>The float style property will be set by any of "float", "styleFloat", or "cssFloat".</p>

Parameters:

  • sel String

    the selector string to apply the changes to

  • css Object

    Object literal of style properties and new values

Returns:

toCssText

(
  • css
  • cssText
)
String static

<p>Converts an object literal of style properties and values into a string of css text. This can then be assigned to el.style.cssText.</p>

<p>The optional second parameter is a cssText string representing the starting state of the style prior to alterations. This is most often extracted from the eventual target's current el.style.cssText.</p>

Parameters:

  • css Object

    object literal of style properties and values

  • cssText String

    (optional) starting cssText value

Returns:

String:

the resulting cssText string

unset

(
  • sel
  • css
)
StyleSheet chainable

<p>Unset style properties for a provided selector string, removing their effect from the style cascade.</p>

<p>If the selector includes commas, it will be split into individual selectors and applied accordingly. If there are no properties remaining in the rule after unsetting, the rule is removed.</p>

<p>The style property or properties in the second parameter must be the JavaScript style property names. E.g. fontSize rather than font-size.</p>

<p>The float style property will be unset by any of "float", "styleFloat", or "cssFloat".</p>

Parameters:

  • sel String

    the selector string to apply the changes to

  • css String | Array

    style property name or Array of names

Returns: