Giter Site home page Giter Site logo

g3css's Introduction

g3css

An abstract set of custom classes that handle the CSSOM objects in different windows and browsers. What we have here is basically, three classes:

  • the list of all style sheets of a window, represented by g3.css.StyleSheetList
  • a specific style sheet, represented by g3.css.StyleSheet and
  • a specific rule of a style sheet, represented by g3.css.Rule.

Native properties are exposed as custom object properties defined as accessors. So, setting properties on custom objects results in similar changes at the equivalent native ones. Pay attention though, that the other way round, changes on native style object properties through native code are not reflected in custom objects unless they are rebuilt!

Similarly, reading the properties of the custom objects reflects the real values of the native ones.

Updates in v.0.5

  • a bug in static method g3.css.StyleSheetList.filter() was corrected
  • filter on declaration part is possible
  • custom errors g3.Error are thrown instead of alert().

Updates in v.0.4

  • now, filtering is capable through simple or chained(!) filters
  • user can override the static function filter that belongs to class g3.css.StyleSheetList
  • we can move back and forth between different views of the sheet/rule collection based on filters or not and never re-build anything again(!)
  • added the ability to build a full tree of style sheets and rules by passing on construction a second argument, new g3.css.StyleSheetList(win, infinite) (the first filter with deep: true will certainly make a deep re-build if not asked from start but only once during object's g3.css.StyleSheetList lifetime!)
  • if you really want to re-built it just call new g3.css.StyleSheetList(win, infinite)
  • if you don't want to re-built it just call g3.css.StyleSheetList.get(win)
  • inserted rules re-build silently only the specific style sheet they belong to(!)
  • complex filtering cases tested with success even with IE8 which interprets rules differently
  • late construction of style sheets and rules on user demand still holds
  • build-and-forget still holds (call g3.css.StyleSheetList.get(window)) except if user decides to re-build.

Usage

Now, when you create a g3.css.StyleSheetList object, all sub-objects of type g3.css.StyleSheet and g3.css.Rule are created automatically resulting in a 3-level structure and no deeper!

Of course, in reality it can go much deeper but it's very simple to proceed from 4-level and higher with the use of method g3.css.Rule.getNative

On v.0.4 this is done with a second argument true passed during construction: new g3.css.StyleSheetList(win, infinite) or, on the first filter with deep: true.

Imagine an html file:

   test-g3css-stub1.html
      /     \  <HEAD>
      |     |    ...
  L   |     |    <link rel="stylesheet" type="text/css" media="all" href="test-g3css-stub-link1.css" />
  E   |     |    <style media="screen,print">...</style>
  V  /  list \   <style media="print">...</style>
  E  \       /    ...
  L   |     |  </HEAD>
      |     |  <BODY>
  1   |     |    ...
      \     /  </BODY>

Level-1 style list object is apparent: var csslist = new g3.css.StyleSheetList(win);

Level-2 objects represent style sheets: var sheet0 = csslist.item(0); ...

   test-g3css-stub1.html
  L   / sheet0 \   <link rel="stylesheet" type="text/css" media="all" href="test-g3css-stub-link1.css" />
  E   |        |
  V  /  sheet1  \  <style media="screen,print">...</style>
  E  \          /
  L   | sheet2 |   <style media="print">...</style>
  2   \        /

Level-3 objects represent rules of a style sheet: var rule00 = sheet0.cssRules[0];

   test-g3css-stub-link1.css
  L   / rule00 \   @import url("test-g3css-stub-link2.css") screen,print;
  E   |        |
  V  /  rule01  \  .container_12 {...}
  E  \          /
  L   | rule02 |   .grid, .grid_12 {...}
  3   \        /

And that's it!

Going deeper ============

Natively, the mechanism is applied with imported rules as in rule00. So, how can we scan a whole style sheet chain coming from imported links?

Programmatically, we could have added a public property: g3.css.Rule.styleSheet that would be a new custom g3.css.StyleSheet object and that would be enough to start a new circle of Level-2 ==> Level-3 objects!

Instead, they are built on demand as follows: var sheet00 = new g3.css.Rule.styleSheet(rule00.getNative()), i.e. build the style sheet that comes from rule rule00. And that is enough to start a new circle of construction automatically that will stop when the rules of the new sheet are built!

On v.0.4 this is done with a second argument true passed during construction: new g3.css.StyleSheetList(win, infinite) or, on the first filter with deep: true.

  test-g3css-stub-link2.css
  L   / rule000 \   ... {...}
  E   |         |
  V  /  rule001  \ @import url("test-g3css-stub-link3.css");
  E  \           /
  L   | rule002 |   ... {...}
  4   \        /

Levels 4-5 are built on demand. The same applies to 6-7 etc.

Dependencies

Just my.class.js and my custom error class g3Error.js(included).

Classes

1. g3.css.StyleSheetList

Methods

  1. Constructor: var list = new g3.css.StyleSheetList(win, infinite)
  2. Get a style sheet: list.item(n)
  3. Filter rules with list.filter(object) where
    object = {deep: <boolean>, 
    href: <uri>, 
    rule: <RegExp|String|Array[String]>, 
    selector: <RegExp|String|Array[String]>, 
    declaration: <RegExp|String|Array[String]>, 
    wordPart: <Boolean>, 
    style: <Boolean>, 
    link: <Boolean>, 
    id: <String>}
  4. Get a 2D array with list.getFilterRules() where at index i a style sheet list.get(i) is found with matched rules list.get(i).cssRules[j] that have indeces the values of the array list.getFilterRules()[i]
  5. Stop filtering and return to full style sheet/rule view with list.end()
  6. Get a constructed style sheet list with g3.css.StyleSheetList.get(window, infinite) or, force to create one if not already there, finally,
  7. build a new custom filter at g3.css.StyleSheetList.filter(text, selectors, wordPart) (see code comments).

Properties

  1. Length of list: list.length

2. g3.css.StyleSheet

Methods

  1. Constructor: var sheet = list.item(n)
  2. Get the native style sheet: sheet.getNative()
  3. Set a native style property: sheet.set('media')
  4. Insert a rule at index 3: sheet.insertRule('.container_12{width: 50%;}', 3)
  5. Delete a rule at index 3: sheet.deleteRule(3)
  6. Replace a rule at index 3: sheet.replaceRule('.grid, .grid_12{width: 50%;}', 3)

Properties

  1. The style or link node: sheet.ownerNode
  2. The @import rule object: sheet.ownerRule
  3. The type of style sheet: sheet.type
  4. The css text: sheet.cssText
  5. An array of g3.css.Rule objects: sheet.cssRules
  6. The length of sheet.cssRules: sheet.length
  7. The real cssRules array in <=IE8 starts at index higher by (0 for other browsers): sheet.disposition
  8. The disabled property: sheet.disabled
  9. The href property: sheet.href
  10. The media property: sheet.media
  11. The title property: sheet.title

2. g3.css.Rule

Methods

  1. Constructor: var rule = list.item(n).cssRules[m]
  2. Get the native rule: rule.getNative()
  3. Get a descriptive type name of the type property: rule.getTypeName()

Properties

  1. The css text: rule.cssText
  2. The selector part of a rule: rule.selector
  3. The declaration part of a rule: rule.declaration
  4. The type property of a rule: rule.type
  5. The href property of a rule: rule.href
  6. The media property of a rule: rule.media

Evaluator test page

See: g3css

Have fun!

g3css's People

Contributors

centurianii avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.