Library to manipulate with stylesheet rules using JavaScript.
This is a powerfull library which is about parsing and changing all CSS rules available in a webpage.
API reference.
This library iterates CSS rules asyncronousely to prevent lagging.
It also detects rule changes, stylesheet adds, deletes and disables, media rule activations/deactivations.
CSSRules.forEach( handlers, flags )
Use this function to iterate through all available rules right now.
handlers is an object which gives callstart(property start), callback(property end) and step(property step) handlers.
If function given, it will be considered as a step handler.
Pass rule types with flags argument.
If you want to iterate only throught some types of rules pass that types with flags.
Available type flags`
- CSSRules.STYLE_RULE
- CSSRules.MEDIA_RULE
- CSSRules.KEYFRAMES_RULE
- CSSRules.KEYFRAME_RULE
- CSSRules.IMPORT_RULE
Also if you dont want to iterate throught inactive rules, pass CSSRules.SKIP_INACTIVES flag.
Usage`
CSSRules;
As you can see, listeners gets 3 arguments.
rule is the current rule object, type is the type of that rule and key_text is the selector string.
In case of keyframe rule, key_text is the animation percentage selector of current keyframe.
In case of keyframes rule, key_text is the name of that animation.
In case of media rule, key_text is the media query string of that rule.
You can also control iteration flow.
CSSRules;
CSSRules;
var i = 0;CSSRules;
More functions to get information about the current iteration.
- this.active() tells if the container of current rule is currently active. Container is active, if it's styles are applied.
- this.list() gives a list of CSS rules of current container.
- this.index() gives an index of this rule in the list of rules.
CSSRules.addListener( id, listener, types )
Adds stylesheet change listener.
id is just an identificator for given listener.
You can filter rules with types. Available types flags are mentioned above.
CSSRules;
You can have multiple listeners with the same id.
This handler will be called on every stylesheet add/delete, rule add/delete and media query change.
CSSRules.removeListener( id )
Removes stylesheet change listeners with given id.
// This will stop listening to rule changes.CSSRules;
Changing available rules.
CSSRules.change( changer, handlers, types )
Iterates throught all available active rules trying to change them.
changer is the callstep handler, which changes given rules if there is a need.
handlers is an object which gives callstart(property start), callback(property end) and step(property step) handlers.
types is types of rules which you need to change. By default this will iterate only throught style rules. Available types are.
- CSSRules.KEYFRAME_RULE
- CSSRules.STYLE_RULE
// This parses selector and finds wanted pseudoclass.var ParsePseudo = { return { var result = false match pre_index = -1; matcherlastIndex = 0; while pre_index = matcherlastIndex match = matcher if match0 == ':' && name == match7 result = pre_index matcherlastIndex ; break; return result; };} /[\w\u00a1-\uFFFF][\w\u00a1-\uFFFF-]*|[#.]+|[ \t\r\n\f]|[ \t\r\n\f]*[ \t\r\n\f]*|\[[ \t\r\n\f]*?]|:?|\*|/g ; // This just replaces a string in between of the source. { return source + to + source;};CSSRules;
You can add rules too.
var is_first = true;CSSRules;
You can have only one rule changer at a time.
CSSRules.undo( callback )
This will restore all applied changes, and cancelles current rule changer, so you can add a new one.
This also works asyncronousely and callback will be called after all changes are undone.
Browser support
Chrome | Firefox | IE | Opera | Safari |
---|---|---|---|---|
18 | 14 | 11 | 15 | 6.0 |