Skip to content
This repository has been archived by the owner on Oct 1, 2022. It is now read-only.

Commit

Permalink
added: count-non-empty() support, closes #66
Browse files Browse the repository at this point in the history
  • Loading branch information
MartijnR committed May 26, 2017
1 parent 80ee9f2 commit 03fe386
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 41 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

[Unreleased]
--------------------
##### Added
- Count-non-empty() support.

[1.5.0] - 2017-04-25
--------------------
##### Added
Expand Down
36 changes: 17 additions & 19 deletions dist/enketo-xpathjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4002,33 +4002,32 @@ var XPathJS = (function(){
//},

/********************************************************************/
/**** JAVAROSA-specific XPath functions (or XPath 2.0 functions) ****/
/**** OpenRosa-specific XPath functions (or XPath 2.0 functions) ****/
/********************************************************************/

sum_jr: {
'count-non-empty': {
/**
* The JavaRosa version of the sum function is the same as the XPath 1.0 function
* EXCEPT that it evaluates an empty node ('') to 0 instead of NaN.
* @obsolete
* @see
* @param {NodeSetType}
* The count-non-empty function returns the number of non-empty nodes in argument node-set.
* A node is considered non-empty if it is convertible into a string with a greater-than zero length.
*
* @see https://www.w3.org/TR/2003/REC-xforms-20031014/slice7.html#fn-count-non-empty
* @param {NodeSetType} nodeset
* @return {NumberType}
*/
fn: function(nodeset)
{
var i, value
sum = 0;
;

var i;
var count=0;

nodeset = nodeset.toNodeSet();
for(i = 0; i < nodeset.length; i++)
{
value = ( nodeStringValue(nodeset[i]) == '' ) ? '0' : nodeStringValue(nodeset[i]);
sum += (new StringType(value)).toNumber();

for ( i=0 ; i < nodeset.length ; i++){
if ((new StringType(nodeStringValue(nodeset[i]))).toString().length > 0) {
count++;
}
}
return new NumberType(sum);

return new NumberType(count);
},

args: [
Expand All @@ -4038,7 +4037,6 @@ var XPathJS = (function(){
ret: 'number'
},


position: {
/**
* Hacked OpenRosa function to return the position of a nodeset argument
Expand Down
4 changes: 1 addition & 3 deletions dist/enketo-xpathjs.min.js

Large diffs are not rendered by default.

36 changes: 17 additions & 19 deletions src/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -4002,33 +4002,32 @@ var XPathJS = (function(){
//},

/********************************************************************/
/**** JAVAROSA-specific XPath functions (or XPath 2.0 functions) ****/
/**** OpenRosa-specific XPath functions (or XPath 2.0 functions) ****/
/********************************************************************/

sum_jr: {
'count-non-empty': {
/**
* The JavaRosa version of the sum function is the same as the XPath 1.0 function
* EXCEPT that it evaluates an empty node ('') to 0 instead of NaN.
* @obsolete
* @see
* @param {NodeSetType}
* The count-non-empty function returns the number of non-empty nodes in argument node-set.
* A node is considered non-empty if it is convertible into a string with a greater-than zero length.
*
* @see https://www.w3.org/TR/2003/REC-xforms-20031014/slice7.html#fn-count-non-empty
* @param {NodeSetType} nodeset
* @return {NumberType}
*/
fn: function(nodeset)
{
var i, value
sum = 0;
;

var i;
var count=0;

nodeset = nodeset.toNodeSet();
for(i = 0; i < nodeset.length; i++)
{
value = ( nodeStringValue(nodeset[i]) == '' ) ? '0' : nodeStringValue(nodeset[i]);
sum += (new StringType(value)).toNumber();

for ( i=0 ; i < nodeset.length ; i++){
if ((new StringType(nodeStringValue(nodeset[i]))).toString().length > 0) {
count++;
}
}
return new NumberType(sum);

return new NumberType(count);
},

args: [
Expand All @@ -4038,7 +4037,6 @@ var XPathJS = (function(){
ret: 'number'
},


position: {
/**
* Hacked OpenRosa function to return the position of a nodeset argument
Expand Down
15 changes: 15 additions & 0 deletions test/doc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,21 @@

<div id="FunctionWeightedChecklist">3</div>


<div id="FunctionCountNonEmpty">
<div>-5</div>
<div>-15</div>
<div></div>
<p>
<div></div>
<div><!--comment--></div>
<span> </span>
<span>
</span>
</p>
<p></p>
</div>

<!-- *****************************-->


Expand Down
30 changes: 30 additions & 0 deletions test/spec/functions-openrosa.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,36 @@ describe('Custom "OpenRosa" functions', function() {
});
});

it('count-non-empty', function() {
[
['count-non-empty(//xhtml:div[@id="FunctionCountNonEmpty"]/xhtml:div)', 2],
['count-non-empty(//xhtml:div[@id="FunctionCountNonEmpty"]/xhtml:p)', 1],
['count-non-empty(//xhtml:div[@id="FunctionCountNonEmpty"]/xhtml:p/xhtml:div)', 0],
['count-non-empty(//xhtml:div[@id="FunctionCountNonEmpty"]/xhtml:p/xhtml:span)', 2],
['count-non-empty(//xhtml:div[@id="FunctionCountNonEmpty"]//*)', 5],
['count-non-empty(//xhtml:div[@id="NoExist"]/xhtml:div)', 0],

].forEach(function(t) {
var result = documentEvaluate(t[0], doc, helpers.xhtmlResolver, win.XPathResult.NUMBER_TYPE, null);
expect(result.numberValue).to.deep.equal(t[1]);
});
});

it('count-non-empty fails when too few, too many, or incorrect arguments are provided', function() {
[
'count-non-empty()',
'count-non-empty(2)',
'count-non-empty(0)',
'count-non-empty("a")',
].forEach(function(t) {
var test = function(){
documentEvaluate(t, doc, helpers.xhtmlResolver, win.XPathResult.NUMBER_TYPE, null);
};
expect(test).to.throw(win.Error);
});

});

/*
This function is now supported by translating it into regular XPath before passing to this evaluator.
it('indexed-repeat()', function() {
Expand Down

0 comments on commit 03fe386

Please sign in to comment.