-
Notifications
You must be signed in to change notification settings - Fork 64
/
html5.js
121 lines (97 loc) · 2.97 KB
/
html5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// For discussion and comments, see: http://remysharp.com/2009/01/07/html5-enabling-script/
(function(){if(!/*@cc_on!@*/0)return;var e = "abbr,article,aside,audio,canvas,datalist,details,eventsource,figure,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,time,video".split(','),i=e.length;while(i--){document.createElement(e[i])}})()
var html5 = new function () {
var me = this;
/**
* Given an HTML or XML object, find the an attribute by name.
*
* @param {Object} obj - a DOM object.
* @param {String} attrName - the name of an attribute inside the DOM object.
* @return {Object} - the attribute object or null if there isn't one.
*/
me.getAttributeByName = function (obj, attrName) {
var i;
var attributes = obj.attributes;
for (i=0; i<attributes.length; i++) {
var attr = attributes[i]
if (attr.nodeName == attrName && attr.specified) {
return attr;
}
}
return null;
}
/**
* Given an HTML or XML object, find the value of an attribute.
*
* @param {Object} obj - a DOM object.
* @param {String} attrName - the name of an attribute inside the DOM object.
* @return {String} - the value of the attribute.
*/
me.getAttributeValue = function (obj, attrName) {
var attr = me.getAttributeByName(obj, attrName);
if (attr != null) {
return attr.nodeValue;
} else {
return null;
}
}
/**
* Given an HTML or XML object, set the value of an attribute.
*
* @param {Object} obj - a DOM object.
* @param {String} attrName - the name of an attribute inside the DOM object.
* @param {String} attrValue - the value of the attribute.
*/
me.setAttributeValue = function (obj, attrName, attrValue) {
var attr = me.getAttributeByName(obj, attrName);
if (attr != null) {
attr.nodeValue = attrValue;
} else {
return;
}
}
me.getDefinedAttributes = function (obj) {
var attrs = obj.attributes;
var r = new Array();
for (var i=0; i<attrs.length; i++) {
attr = attrs[i];
if (attr.specified) {
r[attr.name] = attr.value;
}
}
return r;
}
/*
* HTML5 dataset
*/
me.getDataset = function (obj) {
var r = new Array();
var attributes = me.getDefinedAttributes(obj);
//jslog.debug('entered')
for (var i=0; i<attributes.length; i++) {
var attr = attributes[i];
if (attr.indexOf('data-') == 0) {
//jslog.debug('adding ' + name)
var name = attr.substring(5);
//jslog.debug('adding ' + name)
r[name] = attr.value;
}
}
//jslog.debug('dataset = ' + DebugHelpers.getProperties(r))
return r;
}
me.getDatasetItem = function (obj, name) {
var r = me.getAttributeValue(obj, 'data-' + name);
if (!r) {
r = me.getAttributeValue(obj, 'data-' + name.toLowerCase())
}
return r;
}
me.setDatasetItem = function (obj, name, value) {
var attrName = 'data-' + name.toLowerCase();
var val = me.setAttributeValue(obj, attrName, value);
if (me.getAttributeValue(obj, attrName) == null) {
obj[attrName] = value;
}
}
}