Fix retrieving style values in node.js

parent c94cc612
......@@ -172,7 +172,7 @@ function c3_axis(d3, params, $$) {
tspan.enter().append('tspan');
tspan.exit().remove();
var fontSize = parseFloat(getStyleValue('.c3-axis-y .tick', 'font-size'));
var fontSize = parseFloat(getStyleValue($$, '.c3-axis-y .tick', 'font-size'));
tspan.text(function (d) {
if (d.splitted.length) {
......
var __filename; // used in Node.js to get css file using path
var c3_chart_fn, c3_chart_internal_fn;
var c3 = { version: "0.4.9" };
var c3_chart_fn, c3_chart_internal_fn;
var c3init = function(module){
__filename = module.uri;
return c3;
}
function Chart(config) {
var $$ = this.internal = new ChartInternal(this);
......@@ -43,9 +48,52 @@ c3_chart_fn = c3.chart.fn;
c3_chart_internal_fn = c3.chart.internal.fn;
var nodeCss;
var styleCache = {};
// Seek a specific value in the stylesheets of a document
function getStyleValue(selector, property) {
function getStyleValue($$, selector, property) {
function cache(value){
if(!styleCache[selector]){
styleCache[selector] = {};
}
styleCache[selector][property] = value.trim();
return styleCache[selector][property];
}
if(styleCache[selector] && styleCache[selector][property]){
return styleCache[selector][property];
}
var css, rule, sheet, value;
if(isNode()){
if(!nodeCss){
var style = require('fs').readFileSync(__filename.replace('c3.js', 'c3.css'), 'utf-8');
nodeCss = [require('css').parse(style), require('css').parse($$.ed3Config.style)];
}
var obj, rule, sel, decl;
for(var i = 0; i<nodeCss.length; i++){
obj = nodeCss[i];
for(var j = 0; j<obj.stylesheet.rules.length; j++){
rule = obj.stylesheet.rules[j];
if(!rule.selectors) continue;
for(var k = 0; k<rule.selectors.length; k++){
sel = rule.selectors[k];
for(var l = 0; l<rule.declarations.length; l++){
decl = rule.declarations[l];
if(decl.property === property){
return cache(decl.value);
}
}
}
}
}
console.log('Style not found: ', selector, '->', property);
return undefined;
}
for (var styleId = 0; styleId < document.styleSheets.length; styleId++) {
sheet = document.styleSheets[styleId];
for (var ruleId = 0; ruleId < sheet.cssRules.length; ruleId++) {
......@@ -55,7 +103,7 @@ function getStyleValue(selector, property) {
if (css.indexOf(property) !== -1) {
try {
value = css.split(property)[1].split(':')[1].split(';')[0];
return value.trim();
return cache(value);
} catch (e) {
// Error will be caught in case of incorrect CSS
return undefined;
......
......@@ -132,7 +132,7 @@ c3_chart_internal_fn.updateLegend = function (targetIds, options, transitions) {
var hasFocused = $$.legend.selectAll('.' + CLASS.legendItemFocused).size();
var texts, rects, tiles, background;
var nodeOffset = isNode() ? 3 : 1;
var nodeOffset = 1;
options = options || {};
withTransition = getOption(options, "withTransition", true);
......
if (typeof define === 'function' && define.amd) {
define("c3", ["d3"], c3);
define("c3", ["module", "d3"], c3init);
} else if ('undefined' !== typeof exports && 'undefined' !== typeof module) {
module.exports = c3;
} else {
......
......@@ -48,7 +48,7 @@ c3_chart_internal_fn.redrawText = function (xForText, yForText, forFlow, withTra
];
};
c3_chart_internal_fn.getTextRect = function (text, cls) {
var fontSize = parseFloat(getStyleValue(cls, 'font-size'));
var fontSize = parseFloat(getStyleValue(this, cls, 'font-size'));
return {
height: fontSize,
......
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