From 5f79ad4205d060e1e6571bbbe0b537f4ac8674ff Mon Sep 17 00:00:00 2001 From: Glenn Jones Date: Sun, 14 Jul 2013 15:04:01 +0100 Subject: [PATCH] Added simple count function --- .gitignore | 2 + dist/microformat-shiv.js | 56 +++++++++++++++++++-- dist/microformat-shiv.min.js | 8 ++- dist/microformat-shiv.min.js.map | 2 +- examples/chrome/microformat-shiv.js | 52 ++++++++++++++++++- examples/firefox/data/microformat-shiv.js | 52 ++++++++++++++++++- examples/opera/includes/microformat-shiv.js | 52 ++++++++++++++++++- lib/parser.js | 52 ++++++++++++++++++- package.json | 2 +- 9 files changed, 267 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 295e83c..e3182f1 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,5 @@ _site /static/testsuite/ *.svn-base test/test.html + +testlink.html diff --git a/dist/microformat-shiv.js b/dist/microformat-shiv.js index 2a44fa0..60b88c5 100644 --- a/dist/microformat-shiv.js +++ b/dist/microformat-shiv.js @@ -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; @@ -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) { @@ -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]); @@ -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') { diff --git a/dist/microformat-shiv.min.js b/dist/microformat-shiv.min.js index 20d0a49..fd1c58e 100644 --- a/dist/microformat-shiv.min.js +++ b/dist/microformat-shiv.min.js @@ -6,7 +6,10 @@ data.push(ufs[z]);} z++;}} x++;}} if(this.options.rel){rels=this.findRels(dom,rootNode);if(rels&&this.shouldInclude(rels,this.options.filters)){data.push(rels);}}} -this.clearUpDom(dom);return{'items':data};},shouldInclude:function(uf,filters){var i;if(this.utils.isArray(filters)&&filters.length>0){i=filters.length;while(i--){if(uf.type[0]===filters[i]){return true;}} +this.clearUpDom(dom);return{'items':data};},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--){if(this.utils.startWith(classItems[x],'h-')){append(classItems[x],1);} +for(key in this.maps){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;},shouldInclude:function(uf,filters){var i;if(this.utils.isArray(filters)&&filters.length>0){i=filters.length;while(i--){if(uf.type[0]===filters[i]){return true;}} return false;}else{return true;}},findChildItems:function(dom,rootNode,ufName){var items,out=[],ufs=[],x,i,z,y,rels;items=this.findRootNodes(dom,rootNode,true);if(items.length>0){i=items.length;x=0;while(x0&&classes.properties.length===0){ufs=this.walkTree(dom,items[x],true);y=ufs.length;z=0;while(z-1){out=attList.split(' ');}else{out.push(attList);}} return out;},hasAttributeValue:function(dom,node,attributeName,value){var attList=this.getAttributeList(dom,node,attributeName);return(attList.indexOf(value)>-1);},hasAttributeValueByPrefix:function(dom,node,attributeName,value){var attList=[],x=0,i;attList=this.getAttributeList(dom,node,attributeName);i=attList.length;while(x -1) { out.push(arr[x]); break; - } + // match v2 root name prefix if(this.utils.startWith(items[y], 'h-')) { out.push(arr[x]); @@ -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') { diff --git a/examples/firefox/data/microformat-shiv.js b/examples/firefox/data/microformat-shiv.js index 587181b..60b88c5 100644 --- a/examples/firefox/data/microformat-shiv.js +++ b/examples/firefox/data/microformat-shiv.js @@ -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) { @@ -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]); @@ -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') { diff --git a/examples/opera/includes/microformat-shiv.js b/examples/opera/includes/microformat-shiv.js index 587181b..60b88c5 100644 --- a/examples/opera/includes/microformat-shiv.js +++ b/examples/opera/includes/microformat-shiv.js @@ -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) { @@ -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]); @@ -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') { diff --git a/lib/parser.js b/lib/parser.js index 01b2f54..19152ec 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -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) { @@ -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]); @@ -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') { diff --git a/package.json b/package.json index e127f55..e901044 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Glenn Jones", "name": "microformat-shiv", "description": "A cross browser JavaScript Microformats 2 parser", - "version": "0.3.1", + "version": "0.3.2", "license": "MIT", "homepage": "http://microformat-shiv.com", "repository": {