Commit 9fc30f66 authored by S Anand's avatar S Anand

ENH: G.style() returns last matching style. G.styles() returns all

parent 312c20c9
// Return all CSS rules matching a selector.
// e.g. G.style(/^\.item$/i) returns all rules matching '.item'
// e.g. G.style('.item') returns all rules exactly matching '.item'
// e.g. G.style(/^\.item$/i) returns last rule matching '.item'
// e.g. G.style('.item') returns last rule exactly matching '.item'
G.style = function(selector) {
var styles = G.styles(selector)
return styles[styles.length - 1]
}
// e.g. G.styles(/^\.item$/i) returns all rules matching '.item'
// e.g. G.styles('.item') returns all rules exactly matching '.item'
G.styles = function(selector) {
var stylesheets = global.document.styleSheets,
matches = [],
string_selector = typeof selector == 'string'
......
describe('G.style', function() {
describe('G.style and G.styles', function() {
it('finds CSS rules as strings', function() {
$('<style>.root {} .root h1 {}</style>').appendTo('head')
var result = G.style('.root')
var result = G.styles('.root')
assert.equal(result.length, 1, 'Only 1 rule matching .root')
var result = G.style(/^\.root/i)
var result = G.styles(/^\.root/i)
assert.ok(result.length > 1, 'Multiple rules start with .root')
for (var i=0, selector; selector=result[i]; i++)
assert.ok(selector.selectorText.match(/^\.root/i), 'All matching rules start with .root')
assert.equal(result[result.length - 1], G.style(/^\.root/i))
$('<link id="styleTest" rel="stylesheet" type="text/css" href="style.test.css">')
.on('load', function() {
var result = G.style('#id')
var result = G.styles('#id')
assert.equal(result.length, 1, 'Only 1 rule matching #id')
assert.equal(result[0].selectorText, '#id', 'Rule matches #id')
var result = G.style(/^#id/i)
var result = G.styles(/^#id/i)
assert.ok(result.length > 1, 'Multiple rules start with #id')
for (var i=0, selector; selector=result[i]; i++)
assert.ok(selector.selectorText.match(/^#id/i), 'All matching rules start with #id')
assert.equal(result[result.length - 1], G.style(/^#id/i))
$('#styleTest').remove()
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment