Skip to content

Commit

Permalink
use libxslt.parse instead of libxslt.stylesheet, feels closer to the …
Browse files Browse the repository at this point in the history
…original lib
  • Loading branch information
Alban Mouton committed Aug 20, 2014
1 parent 8e632a5 commit 22c1bc6
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 64 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -18,7 +18,7 @@ Basic usage
```js
var lixslt = require('libxslt');

var stylesheet = libxslt.stylesheet(stylesheetString);
var stylesheet = libxslt.parse(stylesheetString);

var params = {
MyParam: 'my value'
Expand Down Expand Up @@ -47,7 +47,7 @@ var lixslt = require('libxslt');
var libxmljs = require('libxmljs');

var stylesheetObj = libxmljs.parseXml(stylesheetString);
var stylesheet = libxslt.stylesheet(stylesheetObj);
var stylesheet = libxslt.parse(stylesheetObj);

var document = libxmljs.parseXml(documentString);
stylesheet.apply(document, function(err, result){
Expand All @@ -69,7 +69,7 @@ In this case if a parsing error occurs it will be thrown.
```js
var lixslt = require('libxslt');

var stylesheet = libxslt.stylesheet(stylesheetString);
var stylesheet = libxslt.parse(stylesheetString);

var result = stylesheet.apply(documentString);

Expand Down
6 changes: 3 additions & 3 deletions benchmark.js
Expand Up @@ -5,7 +5,7 @@ var libxslt = require('./index');

var stylesheetStr = fs.readFileSync('./test/resources/cd.xsl', 'utf8');
var stylesheetObj = libxmljs.parseXml(stylesheetStr);
var stylesheet = libxslt.stylesheet(stylesheetObj);
var stylesheet = libxslt.parse(stylesheetObj);
var docStr = fs.readFileSync('./test/resources/cd.xml', 'utf8');
var docObj = libxmljs.parseXml(docStr);

Expand All @@ -22,14 +22,14 @@ var bench = function(name, iterations, f) {

var stylesheetParsingStr = function(iterations, callback) {
for (var i = 0; i < iterations; i++) {
libxslt.stylesheet(stylesheetStr);
libxslt.parse(stylesheetStr);
}
callback();
};

var stylesheetParsingObj = function(iterations, callback) {
for (var i = 0; i < iterations; i++) {
libxslt.stylesheet(stylesheetObj);
libxslt.parse(stylesheetObj);
}
callback();
};
Expand Down
2 changes: 1 addition & 1 deletion example.js
Expand Up @@ -6,7 +6,7 @@ var libxslt = require('./index');
var stylesheetSource = fs.readFileSync('./test/resources/cd.xsl', 'utf8');
var docSource = fs.readFileSync('./test/resources/cd.xml', 'utf8');

var stylesheet = libxslt.stylesheet(stylesheetSource);
var stylesheet = libxslt.parse(stylesheetSource);
//var result = stylesheet.apply(docSource);

//console.log(result);
Expand Down
56 changes: 2 additions & 54 deletions index.js
Expand Up @@ -55,7 +55,7 @@ Stylesheet.prototype.apply = function(source, params, callback) {
}
};

exports.stylesheet = function(source, callback) {
exports.parse = function(source, callback) {
// stylesheet can be given as a string or a pre-parsed xml document
if (typeof source === 'string') source = libxmljs.parseXml(source);

Expand All @@ -64,56 +64,4 @@ exports.stylesheet = function(source, callback) {
} else {
return new Stylesheet(source, binding.stylesheetSync(source));
}
};

/*
var apply = function(stylesheet, source, params, callback) {
// xml can be given as a string or a pre-parsed xml document
var outputString = false;
if (typeof source === 'string') {
source = libxmljs.parseXml(source);
outputString = true;
}
// params are optional
if (typeof params === 'function') {
callback = params;
params = {};
}
params = params || {};
// flatten the params object in an array
var paramsArray = [];
for(var key in params) {
paramsArray.push(key);
paramsArray.push(params[key]);
}
// for some obscure reason I didn't manage to create a new libxmljs document in applySync,
// but passing a document by reference and modifying its content works fine
var result = new libxmljs.Document();
if (callback) {
binding.applyAsync(stylesheet, source, paramsArray, result, function(err){
if (err) return callback(err);
callback(null, outputString ? result.toString() : result);
});
} else {
binding.applySync(stylesheet, source, paramsArray, result);
return outputString ? result.toString() : result;
}
};
exports.stylesheet = function(stylesheetSource) {
// stylesheet can be given as a string or a pre-parsed xml document
if (typeof stylesheetSource === 'string') stylesheetSource = libxmljs.parseXml(stylesheetSource);
var stylesheet = binding.stylesheetSync(stylesheetSource);
stylesheetSource.stylesheet = stylesheet;
stylesheetSource.apply = function(source, params, callback){
return apply(stylesheet, source, params, callback);
};
return stylesheetSource;
};
*/
};
6 changes: 3 additions & 3 deletions test/libxslt.js
Expand Up @@ -25,16 +25,16 @@ describe('node-libxslt bindings', function() {
describe('stylesheet function', function() {
it('should parse a stylesheet from a libxmljs xml document', function() {
var stylesheetDoc = libxmljs.parseXml(stylesheetSource);
stylesheet = libxslt.stylesheet(stylesheetDoc);
stylesheet = libxslt.parse(stylesheetDoc);
stylesheet.should.be.type('object');
});
it('should parse a stylesheet from a xml string', function() {
stylesheet = libxslt.stylesheet(stylesheetSource);
stylesheet = libxslt.parse(stylesheetSource);
stylesheet.should.be.type('object');
});
it('should throw an error when parsing invalid stylesheet', function() {
(function() {
libxslt.stylesheet('this is not a stylesheet!');
libxslt.parse('this is not a stylesheet!');
}).should.throw();
});
});
Expand Down

0 comments on commit 22c1bc6

Please sign in to comment.