Skip to content

Commit

Permalink
Added simple count function
Browse files Browse the repository at this point in the history
  • Loading branch information
glennjones committed Jul 14, 2013
1 parent 7872dbb commit 5f79ad4
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -19,3 +19,5 @@ _site
/static/testsuite/
*.svn-base
test/test.html

testlink.html
56 changes: 53 additions & 3 deletions dist/microformat-shiv.js
Expand Up @@ -60,8 +60,8 @@ microformats.Parser.prototype = {
}

// find base tag to set baseUrl
baseTag = dom.querySelector('base');
if(baseTag) {
baseTag = dom.querySelectorAll('base');
if(baseTag.length > 0) {
href = this.domUtils.getAttribute(dom, baseTag, 'href');
if(href){
this.options.baseUrl = href;
Expand Down Expand Up @@ -112,6 +112,45 @@ microformats.Parser.prototype = {
},


// get the count of items
count: function(dom, rootNode) {
var out = {},
keys = [],
count,
x,
i;

items = this.findRootNodes(dom, rootNode);
i = items.length;
while(i--) {
classItems = this.domUtils.getAttributeList(dom, items[i], 'class');
x = classItems.length;
while(x--) {
// find v2 names
if(this.utils.startWith( classItems[x], 'h-' )){
append(classItems[x], 1);
}
// find v1 names
for(key in this.maps) {
// has v1 root but not also a v2 root so we dont double count
if(this.maps[key].root === classItems[x] && classItems.indexOf(key) === -1) {
append(key, 1);
}
}
}
}

function append(name, count){
if(out[name]){
out[name] = out[name] + count;
}else{
out[name] = count;
}
}

return out;
},


// is the uf type in the filter list
shouldInclude: function(uf, filters) {
Expand Down Expand Up @@ -221,8 +260,8 @@ microformats.Parser.prototype = {
if(classList.indexOf(items[y]) > -1) {
out.push(arr[x]);
break;

}

// match v2 root name prefix
if(this.utils.startWith(items[y], 'h-')) {
out.push(arr[x]);
Expand Down Expand Up @@ -1204,6 +1243,17 @@ microformats.getItems = function(options){
return this.parser.get(dom, node, options);
};

microformats.getCounts = function(dom, rootNode, options) {
var dom,
node;

dom = (options && options.document)? options.document : document;
node = (options && options.node)? options.node : document;
options = (options)? options : {};

return this.parser.count(dom, node, options);
}


// Simple support for CommonJS
if (typeof exports !== 'undefined') {
Expand Down
8 changes: 6 additions & 2 deletions dist/microformat-shiv.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/microformat-shiv.min.js.map

Large diffs are not rendered by default.

52 changes: 51 additions & 1 deletion examples/chrome/microformat-shiv.js
Expand Up @@ -112,6 +112,45 @@ microformats.Parser.prototype = {
},


// get the count of items
count: function(dom, rootNode) {
var out = {},
keys = [],
count,
x,
i;

items = this.findRootNodes(dom, rootNode);
i = items.length;
while(i--) {
classItems = this.domUtils.getAttributeList(dom, items[i], 'class');
x = classItems.length;
while(x--) {
// find v2 names
if(this.utils.startWith( classItems[x], 'h-' )){
append(classItems[x], 1);
}
// find v1 names
for(key in this.maps) {
// has v1 root but not also a v2 root so we dont double count
if(this.maps[key].root === classItems[x] && classItems.indexOf(key) === -1) {
append(key, 1);
}
}
}
}

function append(name, count){
if(out[name]){
out[name] = out[name] + count;
}else{
out[name] = count;
}
}

return out;
},


// is the uf type in the filter list
shouldInclude: function(uf, filters) {
Expand Down Expand Up @@ -221,8 +260,8 @@ microformats.Parser.prototype = {
if(classList.indexOf(items[y]) > -1) {
out.push(arr[x]);
break;

}

// match v2 root name prefix
if(this.utils.startWith(items[y], 'h-')) {
out.push(arr[x]);
Expand Down Expand Up @@ -1204,6 +1243,17 @@ microformats.getItems = function(options){
return this.parser.get(dom, node, options);
};

microformats.getCounts = function(dom, rootNode, options) {
var dom,
node;

dom = (options && options.document)? options.document : document;
node = (options && options.node)? options.node : document;
options = (options)? options : {};

return this.parser.count(dom, node, options);
}


// Simple support for CommonJS
if (typeof exports !== 'undefined') {
Expand Down
52 changes: 51 additions & 1 deletion examples/firefox/data/microformat-shiv.js
Expand Up @@ -112,6 +112,45 @@ microformats.Parser.prototype = {
},


// get the count of items
count: function(dom, rootNode) {
var out = {},
keys = [],
count,
x,
i;

items = this.findRootNodes(dom, rootNode);
i = items.length;
while(i--) {
classItems = this.domUtils.getAttributeList(dom, items[i], 'class');
x = classItems.length;
while(x--) {
// find v2 names
if(this.utils.startWith( classItems[x], 'h-' )){
append(classItems[x], 1);
}
// find v1 names
for(key in this.maps) {
// has v1 root but not also a v2 root so we dont double count
if(this.maps[key].root === classItems[x] && classItems.indexOf(key) === -1) {
append(key, 1);
}
}
}
}

function append(name, count){
if(out[name]){
out[name] = out[name] + count;
}else{
out[name] = count;
}
}

return out;
},


// is the uf type in the filter list
shouldInclude: function(uf, filters) {
Expand Down Expand Up @@ -221,8 +260,8 @@ microformats.Parser.prototype = {
if(classList.indexOf(items[y]) > -1) {
out.push(arr[x]);
break;

}

// match v2 root name prefix
if(this.utils.startWith(items[y], 'h-')) {
out.push(arr[x]);
Expand Down Expand Up @@ -1204,6 +1243,17 @@ microformats.getItems = function(options){
return this.parser.get(dom, node, options);
};

microformats.getCounts = function(dom, rootNode, options) {
var dom,
node;

dom = (options && options.document)? options.document : document;
node = (options && options.node)? options.node : document;
options = (options)? options : {};

return this.parser.count(dom, node, options);
}


// Simple support for CommonJS
if (typeof exports !== 'undefined') {
Expand Down
52 changes: 51 additions & 1 deletion examples/opera/includes/microformat-shiv.js
Expand Up @@ -112,6 +112,45 @@ microformats.Parser.prototype = {
},


// get the count of items
count: function(dom, rootNode) {
var out = {},
keys = [],
count,
x,
i;

items = this.findRootNodes(dom, rootNode);
i = items.length;
while(i--) {
classItems = this.domUtils.getAttributeList(dom, items[i], 'class');
x = classItems.length;
while(x--) {
// find v2 names
if(this.utils.startWith( classItems[x], 'h-' )){
append(classItems[x], 1);
}
// find v1 names
for(key in this.maps) {
// has v1 root but not also a v2 root so we dont double count
if(this.maps[key].root === classItems[x] && classItems.indexOf(key) === -1) {
append(key, 1);
}
}
}
}

function append(name, count){
if(out[name]){
out[name] = out[name] + count;
}else{
out[name] = count;
}
}

return out;
},


// is the uf type in the filter list
shouldInclude: function(uf, filters) {
Expand Down Expand Up @@ -221,8 +260,8 @@ microformats.Parser.prototype = {
if(classList.indexOf(items[y]) > -1) {
out.push(arr[x]);
break;

}

// match v2 root name prefix
if(this.utils.startWith(items[y], 'h-')) {
out.push(arr[x]);
Expand Down Expand Up @@ -1204,6 +1243,17 @@ microformats.getItems = function(options){
return this.parser.get(dom, node, options);
};

microformats.getCounts = function(dom, rootNode, options) {
var dom,
node;

dom = (options && options.document)? options.document : document;
node = (options && options.node)? options.node : document;
options = (options)? options : {};

return this.parser.count(dom, node, options);
}


// Simple support for CommonJS
if (typeof exports !== 'undefined') {
Expand Down

0 comments on commit 5f79ad4

Please sign in to comment.