libcss-w3d
Parses CSS and lets you query the computed style of elements in your document tree. Uses the NetSurf browser's LibCSS, transpiled to JavaScript. This fork of libcss-js adds support to properties of the w3d framework.
Installation
npm install libcss-w3d
Usage
Consider the following document tree:
Some text.
Note: libcss-js will work with any document tree, not only HTML!
var libcss = ; var handlers = /* see below */ ;libcss; var css1 = 'div { color: red; width: 70em; display: flex; }'var css2 = '#box { color: green; } #parent > .floaty { float: left; }'libcss;libcss; var parStyle = libcss;var boxStyle = libcss;console;// '#ff00ff00', formated as #aarrggbbconsole;// '70em' libcssparStyle = libcss;boxStyle = libcss;console;// '#ff000000'console;// 'auto'
API
libcss.init(config)
This method enables libcss-js to navigate your document tree. It must be called before queries are made.
config <object>
All members of this object must be callback functions. The identifier parameter is a string that uniquely identifies an element in your document tree.
The following example provides all the required handlers (callback functions) to libcss-js. For simplicity, we will use the element id as the identifier string, but you should use something more consistent, such as a hash or a UUID:
libcss;
libcss.addSheet(sheet, ?options)
Adds a CSS stylesheet to the selection context.
sheet <string> The CSS text to be parsed.
options <object>
-
options.level <string>
CSS level. Accepted values are
'1'
,'2'
,'2.1'
and'3'
. Defaults to'3'
. -
options.origin <string>
CSS origin. Accepted values are
'author'
,'user'
,'ua'
,'UA'
,'user agent'
,'user-agent'
(the last four are equivalent). Defaults to'author'
. -
options.url <string>
URL from which the CSS file was retrieved. Useful for loading resources with relative paths.
-
options.media <array of strings>
The media to apply the CSS rules. Accepted values of the string array are
'tv'
,'tty'
,'aural'
,'print'
,'screen'
,'speech'
,'braille'
,'embossed'
,'handheld'
,'projection'
and'all'
. Defaults to'all'
. -
options.allow_quirks <boolean>
Attempt to parse poorly-formatted CSS. Use only if needed, because it may parse shorthands with undefined behavior. Defaults to
false
.
libcss.dropSheets()
Deletes all CSS from the selection context.
libcss.getStyle(identifier, ?options)
Returns an object containing all the computed style for the element that corresponds to the identifier.
The returned object has the format { 'property': 'value', ... }
. The available properties are defined upstream, by NetSurf's LibCSS.
options <object>
-
options.pseudo <string>
Pseudo element to be queried. Accepted values are
'none'
,'first-line'
,'first-letter'
,'before'
and'after'
. Defaults to'none'
. -
options.media <string>
The medium being queried. Accepted values are
'tv'
,'tty'
,'aural'
,'print'
,'screen'
,'speech'
,'braille'
,'embossed'
,'handheld'
,'projection'
and'all'
. Defaults to'all'
.
Testing
npm run test