From 3695d508127b5855592996db9eee19068cec0f90 Mon Sep 17 00:00:00 2001 From: Thomas Aylott Date: Mon, 19 Sep 2011 00:28:42 -0700 Subject: [PATCH 1/4] Copied basic attr code to new AMD module --- lib/attr.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 lib/attr.js diff --git a/lib/attr.js b/lib/attr.js new file mode 100644 index 00000000..f53f0e9b --- /dev/null +++ b/lib/attr.js @@ -0,0 +1,23 @@ +define({ + + getters: {}, + + define: function(name, fn){ + this.getters[name] = fn; + return this; + }, + + lookup: function(name){ + return this.getters[name]; + }, + + has: function(node, attribute){ + return node.hasAttribute(attribute); + }, + + get: function(node, attribute){ + var getter = this.getters[attribute]; + if (getter) return getter.call(node, attribute); + return node.getAttribute(name); + } +}); From 6b489237042aeab0353c391e7c73c32f34d434bf Mon Sep 17 00:00:00 2001 From: Thomas Aylott Date: Mon, 19 Sep 2011 00:29:07 -0700 Subject: [PATCH 2/4] Copied oldIE attr code to new AMD module --- lib/attr-oldie.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 lib/attr-oldie.js diff --git a/lib/attr-oldie.js b/lib/attr-oldie.js new file mode 100644 index 00000000..fbf9bb14 --- /dev/null +++ b/lib/attr-oldie.js @@ -0,0 +1,18 @@ +define(['./attr'], function(attr){ + + // in IE6-7, if a form has an input of id x, form.getAttribute(x) returns a reference to the input + + attr.get = function(node, attribute){ + var getter = this.getters[attribute]; + if (getter) return getter.call(node, attribute); + var attributeNode = node.getAttributeNode(attribute); + return attributeNode ? attributeNode.nodeValue : null; + }; + + attr.has = function(node, attribute){ + node = node.getAttributeNode(attribute); + return !!(node && (node.specified || node.nodeValue)); + }; + + return attr; +}); From e6e85936eac28d8eb7e0a262ede06fb76d8757a5 Mon Sep 17 00:00:00 2001 From: Thomas Aylott Date: Mon, 19 Sep 2011 00:29:46 -0700 Subject: [PATCH 3/4] Copied default attr getters to new AMD module --- lib/attr-default-getters.js | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 lib/attr-default-getters.js diff --git a/lib/attr-default-getters.js b/lib/attr-default-getters.js new file mode 100644 index 00000000..711e1781 --- /dev/null +++ b/lib/attr-default-getters.js @@ -0,0 +1,39 @@ +define(function(){ + var getters = { + + 'class': function(){ + return this.getAttribute('class') || this.className; + }, + + 'for': function(){ + return ('htmlFor' in this) ? this.htmlFor : this.getAttribute('for'); + }, + + href: function(){ + return ('href' in this) ? this.getAttribute('href', 2) : this.getAttribute('href'); + }, + + style: function(){ + return (this.style) ? this.style.cssText : this.getAttribute('style'); + }, + + tabindex: function(){ + var attributeNode = this.getAttributeNode('tabindex'); + return (attributeNode && attributeNode.specified) ? attributeNode.nodeValue : null; + }, + + type: function(){ + return this.getAttribute('type'); + }, + + maxLength: function(){ + var attributeNode = this.getAttributeNode('maxLength'); + return (attributeNode && attributeNode.specified) ? attributeNode.nodeValue : null; + } + + }; + + getters.MAXLENGTH = getters.maxlength = getters.maxLength; + + return getters; +}); From 5eeecbb3892495237fe4f8b30d43bf502a9a3ef7 Mon Sep 17 00:00:00 2001 From: Thomas Aylott Date: Fri, 23 Sep 2011 11:01:07 -0700 Subject: [PATCH 4/4] still trying to decide how best to use AMD so that it can be built out --- attr.built.no-oldie.js | 79 +++++++++++++++++++++++++++++++++++++ build/attr.build.js | 34 ++++++++++++++++ build/slick.build.js | 16 ++++++++ example/example.html | 24 +++++++++++ lib/attr-default-getters.js | 39 ------------------ lib/attr-oldie.js | 18 --------- lib/attr.getters.js | 34 ++++++++++++++++ lib/attr.has.js | 14 +++++++ lib/attr.js | 60 +++++++++++++++++++--------- 9 files changed, 242 insertions(+), 76 deletions(-) create mode 100644 attr.built.no-oldie.js create mode 100644 build/attr.build.js create mode 100755 build/slick.build.js create mode 100644 example/example.html delete mode 100644 lib/attr-default-getters.js delete mode 100644 lib/attr-oldie.js create mode 100644 lib/attr.getters.js create mode 100644 lib/attr.has.js diff --git a/attr.built.no-oldie.js b/attr.built.no-oldie.js new file mode 100644 index 00000000..efacc330 --- /dev/null +++ b/attr.built.no-oldie.js @@ -0,0 +1,79 @@ + +define('lib/attr.getters',['require','exports','module'],function(require, get){ + + get['class'] = function(){ + return this.getAttribute('class') || this.className + } + + get['for'] = function(){ + return ('htmlFor' in this) ? this.htmlFor : this.getAttribute('for') + } + + get.href = function(){ + return ('href' in this) ? this.getAttribute('href', 2) : this.getAttribute('href') + } + + get.style = function(){ + return (this.style) ? this.style.cssText : this.getAttribute('style') + } + + get.type = function(){ + return this.getAttribute('type') + } + + get.tabindex = function(){ + var attributeNode = this.getAttributeNode('tabindex') + return (attributeNode && attributeNode.specified) ? attributeNode.nodeValue : null + } + + get.maxlength = + get.maxLength = function(){ + var attributeNode = this.getAttributeNode('maxLength') + return (attributeNode && attributeNode.specified) ? attributeNode.nodeValue : null + } + +}) + +define('lib/attr',['require','exports','module','./attr.has','./attr.getters'],function(require, attr){ + + var has = require('./attr.has') + + var getters = require('./attr.getters') || {} + + attr.define = function(name, fn){ + getters[name] = fn + return this + } + + attr.lookup = function(name){ + return getters[name] + } + + true + ? + attr.has = function(node, attribute){ + return node.hasAttribute(attribute) + } + : + attr.has = function(node, attribute){ + // IE6-7 (at least) doesn't implement hasAttribute + node = node.getAttributeNode(attribute) + return !!(node && (node.specified || node.nodeValue)) + } + + true + ? + attr.get = function(node, attribute){ + var getter = getters[attribute] + if (getter) return getter.call(node, attribute) + return node.getAttribute(name) + } + : + attr.get = function(node, attribute){ + // in IE6-7, if a form has an input of id x, form.getAttribute(x) returns a reference to the input + var getter = this.getters[attribute] + if (getter) return getter.call(node, attribute) + var attributeNode = node.getAttributeNode(attribute) + return attributeNode ? attributeNode.nodeValue : null + } +}) diff --git a/build/attr.build.js b/build/attr.build.js new file mode 100644 index 00000000..9592e1c4 --- /dev/null +++ b/build/attr.build.js @@ -0,0 +1,34 @@ +// more info: https://github.com/jrburke/r.js/blob/master/build/example.build.js + +module.exports = { + baseUrl: '../' + , + name: 'lib/attr' + , + out: '../attr.built.no-oldie.js' + , + paths:{ + 'lib/attr.has': 'empty:' + } + , + has:{ + 'has': false, + 'dom.hasAttribute': true, + 'dom.getAttribute': true + } + , + optimize:'none' + // , + // pragmas:{// true = yes, remove + // 'has':true, + // 'oldie':true + // } + , + wrap:true + // , + // uglify:{ + // beautify:true + // } +} + +if (module.id == '.') require('requirejs').optimize(module.exports, console.log) diff --git a/build/slick.build.js b/build/slick.build.js new file mode 100755 index 00000000..91c42cd7 --- /dev/null +++ b/build/slick.build.js @@ -0,0 +1,16 @@ +#!/usr/bin/env node +var requirejs = require('requirejs') + +var configs = [ + require('./attr.build') +] + +configs.forEach(function(config){ + requirejs.optimize(config, function (buildResponse) { + //buildResponse is just a text output of the modules + //included. Load the built file for the contents. + //Use config.out to get the optimized file contents. + // var contents = fs.readFileSync(config.out, 'utf8') + console.log(buildResponse) + }) +}) diff --git a/example/example.html b/example/example.html new file mode 100644 index 00000000..9dbdb0fe --- /dev/null +++ b/example/example.html @@ -0,0 +1,24 @@ + + + + + example + + + + + + + + + + diff --git a/lib/attr-default-getters.js b/lib/attr-default-getters.js deleted file mode 100644 index 711e1781..00000000 --- a/lib/attr-default-getters.js +++ /dev/null @@ -1,39 +0,0 @@ -define(function(){ - var getters = { - - 'class': function(){ - return this.getAttribute('class') || this.className; - }, - - 'for': function(){ - return ('htmlFor' in this) ? this.htmlFor : this.getAttribute('for'); - }, - - href: function(){ - return ('href' in this) ? this.getAttribute('href', 2) : this.getAttribute('href'); - }, - - style: function(){ - return (this.style) ? this.style.cssText : this.getAttribute('style'); - }, - - tabindex: function(){ - var attributeNode = this.getAttributeNode('tabindex'); - return (attributeNode && attributeNode.specified) ? attributeNode.nodeValue : null; - }, - - type: function(){ - return this.getAttribute('type'); - }, - - maxLength: function(){ - var attributeNode = this.getAttributeNode('maxLength'); - return (attributeNode && attributeNode.specified) ? attributeNode.nodeValue : null; - } - - }; - - getters.MAXLENGTH = getters.maxlength = getters.maxLength; - - return getters; -}); diff --git a/lib/attr-oldie.js b/lib/attr-oldie.js deleted file mode 100644 index fbf9bb14..00000000 --- a/lib/attr-oldie.js +++ /dev/null @@ -1,18 +0,0 @@ -define(['./attr'], function(attr){ - - // in IE6-7, if a form has an input of id x, form.getAttribute(x) returns a reference to the input - - attr.get = function(node, attribute){ - var getter = this.getters[attribute]; - if (getter) return getter.call(node, attribute); - var attributeNode = node.getAttributeNode(attribute); - return attributeNode ? attributeNode.nodeValue : null; - }; - - attr.has = function(node, attribute){ - node = node.getAttributeNode(attribute); - return !!(node && (node.specified || node.nodeValue)); - }; - - return attr; -}); diff --git a/lib/attr.getters.js b/lib/attr.getters.js new file mode 100644 index 00000000..854abb03 --- /dev/null +++ b/lib/attr.getters.js @@ -0,0 +1,34 @@ +define(function(require, get){ + + get['class'] = function(){ + return this.getAttribute('class') || this.className + } + + get['for'] = function(){ + return ('htmlFor' in this) ? this.htmlFor : this.getAttribute('for') + } + + get.href = function(){ + return ('href' in this) ? this.getAttribute('href', 2) : this.getAttribute('href') + } + + get.style = function(){ + return (this.style) ? this.style.cssText : this.getAttribute('style') + } + + get.type = function(){ + return this.getAttribute('type') + } + + get.tabindex = function(){ + var attributeNode = this.getAttributeNode('tabindex') + return (attributeNode && attributeNode.specified) ? attributeNode.nodeValue : null + } + + get.maxlength = + get.maxLength = function(){ + var attributeNode = this.getAttributeNode('maxLength') + return (attributeNode && attributeNode.specified) ? attributeNode.nodeValue : null + } + +}) diff --git a/lib/attr.has.js b/lib/attr.has.js new file mode 100644 index 00000000..a7870ca2 --- /dev/null +++ b/lib/attr.has.js @@ -0,0 +1,14 @@ +define(function(require, exports){ + var has = exports = require('has') + + has.add('dom.getAttribute', function(global, document, testEl){ + return has.isHostType(testEl, 'hasAttribute') + }) + + has.add('dom.hasAttribute', function(global, document, testEl){ + // FIXME: add hasAttribute feature test + return true + }) + + return has +}) diff --git a/lib/attr.js b/lib/attr.js index f53f0e9b..6a335ea6 100644 --- a/lib/attr.js +++ b/lib/attr.js @@ -1,23 +1,45 @@ -define({ +define(function(require, attr){ - getters: {}, - - define: function(name, fn){ - this.getters[name] = fn; - return this; - }, + //>>excludeStart("has", pragmas.has); + var has = require('./attr.has') + //>>excludeEnd("has"); - lookup: function(name){ - return this.getters[name]; - }, + var getters = require('./attr.getters') || {} - has: function(node, attribute){ - return node.hasAttribute(attribute); - }, + attr.define = function(name, fn){ + getters[name] = fn + return this + } - get: function(node, attribute){ - var getter = this.getters[attribute]; - if (getter) return getter.call(node, attribute); - return node.getAttribute(name); - } -}); + attr.lookup = function(name){ + return getters[name] + } + + has('dom.hasAttribute') + ? + attr.has = function(node, attribute){ + return node.hasAttribute(attribute) + } + : + attr.has = function(node, attribute){ + // IE6-7 (at least) doesn't implement hasAttribute + node = node.getAttributeNode(attribute) + return !!(node && (node.specified || node.nodeValue)) + } + + has('dom.getAttribute') + ? + attr.get = function(node, attribute){ + var getter = getters[attribute] + if (getter) return getter.call(node, attribute) + return node.getAttribute(name) + } + : + attr.get = function(node, attribute){ + // in IE6-7, if a form has an input of id x, form.getAttribute(x) returns a reference to the input + var getter = this.getters[attribute] + if (getter) return getter.call(node, attribute) + var attributeNode = node.getAttributeNode(attribute) + return attributeNode ? attributeNode.nodeValue : null + } +})