|
1 | | -/* jsCore JavaScript library v0.6.1 IE10+ |
| 1 | +/* jsCore JavaScript library v0.7.0 IE10+ |
2 | 2 | * © 2014 Dmitry Korobkin |
3 | 3 | * Released under the MIT license |
4 | 4 | * github.com/Octane/jsCore |
@@ -159,7 +159,7 @@ if (!Array.prototype.contains) { |
159 | 159 | } |
160 | 160 | return false; |
161 | 161 | } |
162 | | - return -1 != this.indexOf(anything, position); |
| 162 | + return -1 != Array.prototype.indexOf.call(this, anything, position); |
163 | 163 | }; |
164 | 164 | } |
165 | 165 |
|
@@ -1043,138 +1043,52 @@ Object.defineProperty(HTMLElement.prototype, 'dataset', { |
1043 | 1043 |
|
1044 | 1044 | }; |
1045 | 1045 |
|
1046 | | -//DOM4 w3c.github.io/dom |
1047 | | -'append' in document.createDocumentFragment() || new function () { |
| 1046 | +document.documentElement.matches || new function () { |
1048 | 1047 |
|
1049 | | - var ELEMENT_NODE = 1, |
1050 | | - proto = HTMLElement.prototype, |
1051 | | - api = { |
1052 | | - |
1053 | | - before: function (/* ...nodes */) { |
1054 | | - //todo IE8 removedNode.parentNode ≠ null |
1055 | | - var parentNode = this.parentNode; |
1056 | | - if (parentNode) { |
1057 | | - parentNode.insertBefore(mutationMacro(arguments), this); |
1058 | | - } |
1059 | | - }, |
1060 | | - |
1061 | | - after: function (/* ...nodes */) { |
1062 | | - var parentNode = this.parentNode, |
1063 | | - nextSibling, |
1064 | | - nodes; |
1065 | | - if (parentNode) { |
1066 | | - nodes = mutationMacro(arguments); |
1067 | | - nextSibling = this.nextSibling; |
1068 | | - if (nextSibling) { |
1069 | | - parentNode.insertBefore(nodes, nextSibling); |
1070 | | - } else { |
1071 | | - parentNode.appendChild(nodes); |
1072 | | - } |
1073 | | - } |
1074 | | - }, |
1075 | | - |
1076 | | - replace: function (/* ...nodes */) { |
1077 | | - var parentNode = this.parentNode; |
1078 | | - if (parentNode) { |
1079 | | - parentNode.replaceChild(mutationMacro(arguments), this); |
1080 | | - } |
1081 | | - }, |
1082 | | - |
1083 | | - remove: function () { |
1084 | | - var parentNode = this.parentNode; |
1085 | | - if (parentNode) { |
1086 | | - parentNode.removeChild(this); |
1087 | | - } |
1088 | | - }, |
1089 | | - |
1090 | | - append: function (/* ...nodes */) { |
1091 | | - this.appendChild(mutationMacro(arguments)); |
1092 | | - }, |
| 1048 | + var proto = HTMLElement.prototype; |
1093 | 1049 |
|
1094 | | - prepend: function () { |
1095 | | - this.insertBefore(mutationMacro(arguments), this.firstChild); |
1096 | | - }, |
| 1050 | + proto.matches = [ |
| 1051 | + proto.matchesSelector, |
| 1052 | + proto.oMatchesSelector, |
| 1053 | + proto.msMatchesSelector, |
| 1054 | + proto.mozMatchesSelector, |
| 1055 | + proto.webkitMatchesSelector |
| 1056 | + ].find(Boolean); |
1097 | 1057 |
|
1098 | | - querySelector: proto.querySelector, |
1099 | | - |
1100 | | - querySelectorAll: proto.querySelectorAll, |
1101 | | - |
1102 | | - matches: [ |
1103 | | - proto.matchesSelector, |
1104 | | - proto.oMatchesSelector, |
1105 | | - proto.msMatchesSelector, |
1106 | | - proto.mozMatchesSelector, |
1107 | | - proto.webkitMatchesSelector, |
1108 | | - function (selector) { |
1109 | | - var contains, |
1110 | | - root; |
1111 | | - if (this === document) { |
1112 | | - //if docFragment.constructor ≡ document.constructor |
1113 | | - return false; |
1114 | | - } |
| 1058 | + if (!proto.matches) { |
| 1059 | + proto.matches = new function () { |
| 1060 | + var ELEMENT_NODE = 1; |
| 1061 | + function isContains(root, element, selector) { |
| 1062 | + return Array.contains(root.querySelectorAll(selector), element); |
| 1063 | + } |
| 1064 | + return function (selector) { |
| 1065 | + var contains, |
1115 | 1066 | root = this.parentNode; |
1116 | | - if (root) { |
1117 | | - if (ELEMENT_NODE == root.nodeType) { |
1118 | | - root = root.ownerDocument; |
1119 | | - } |
1120 | | - return isContains(root, this, selector); |
| 1067 | + if (root) { |
| 1068 | + if (ELEMENT_NODE == root.nodeType) { |
| 1069 | + root = root.ownerDocument; |
1121 | 1070 | } |
1122 | | - root = document.createDocumentFragment(); |
1123 | | - root.appendChild(this); |
1124 | | - contains = isContains(root, this, selector); |
1125 | | - root.removeChild(this); |
| 1071 | + return isContains(root, this, selector); |
1126 | 1072 | } |
1127 | | - ].find(Boolean) |
1128 | | - |
| 1073 | + root = document.createDocumentFragment(); |
| 1074 | + root.appendChild(this); |
| 1075 | + contains = isContains(root, this, selector); |
| 1076 | + root.removeChild(this); |
| 1077 | + }; |
1129 | 1078 | }; |
1130 | | - |
1131 | | - function isContains(root, element, selector) { |
1132 | | - return -1 != Array.indexOf(root.querySelectorAll(selector), element); |
1133 | 1079 | } |
1134 | 1080 |
|
1135 | | - function mutationMacro(nodes) { |
1136 | | - var node, |
1137 | | - fragment, |
1138 | | - length = nodes.length, |
1139 | | - i; |
1140 | | - if (1 == length) { |
1141 | | - node = nodes[0]; |
1142 | | - if ('string' == typeof node) { |
1143 | | - return document.createTextNode(node); |
1144 | | - } |
1145 | | - return node; |
1146 | | - } |
1147 | | - fragment = document.createDocumentFragment(); |
1148 | | - nodes = Array.from(nodes); |
1149 | | - i = 0; |
1150 | | - while (i < length) { |
1151 | | - node = nodes[i]; |
1152 | | - if ('string' == typeof node) { |
1153 | | - node = document.createTextNode(node); |
1154 | | - } |
1155 | | - fragment.appendChild(node); |
1156 | | - i++; |
1157 | | - } |
1158 | | - return fragment; |
1159 | | - } |
1160 | | - |
1161 | | - function implement(key) { |
1162 | | - if (!(key in proto)) { |
1163 | | - proto[key] = api[key]; |
1164 | | - } |
1165 | | - } |
1166 | | - |
1167 | | - Object.keys(api).forEach(implement); |
| 1081 | +}; |
1168 | 1082 |
|
1169 | | - proto = document.constructor.prototype; |
1170 | | - ['querySelector', 'querySelectorAll'].forEach(implement); |
| 1083 | +new function () { |
1171 | 1084 |
|
1172 | | - proto = document.createDocumentFragment().constructor.prototype; |
1173 | | - [ |
1174 | | - 'append', 'prepend', |
1175 | | - 'querySelector', 'querySelectorAll', |
1176 | | - 'matches' |
1177 | | - ].forEach(implement); |
| 1085 | + var docFragment = document.createDocumentFragment(), |
| 1086 | + target = docFragment.constructor.prototype, |
| 1087 | + source = HTMLElement.prototype; |
| 1088 | + if (!docFragment.querySelector) { |
| 1089 | + target.querySelector = source.querySelector; |
| 1090 | + target.querySelectorAll = source.querySelectorAll; |
| 1091 | + } |
1178 | 1092 |
|
1179 | 1093 | }; |
1180 | 1094 |
|
@@ -1332,26 +1246,24 @@ lib.html = { |
1332 | 1246 |
|
1333 | 1247 | }; |
1334 | 1248 |
|
1335 | | -//example: new lib.Template('Hi, {NAME}').match({name: 'John'}) → 'Hi, John' |
| 1249 | +//example: var tmpl = new lib.Template('Hi, {NAME}'); |
| 1250 | +// tmpl({name: 'John'}) → 'Hi, John' |
1336 | 1251 | lib.Template = new function () { |
1337 | 1252 |
|
1338 | | - function Template(template) { |
1339 | | - this.template = template; |
1340 | | - } |
1341 | | - |
1342 | | - Template.match = function (template, replacements) { |
1343 | | - if (Array.isArray(template)) { |
1344 | | - template = template.join(''); |
1345 | | - } |
| 1253 | + function match(template, replacements) { |
1346 | 1254 | return Object.keys(replacements).reduceRight(function (template, key) { |
1347 | 1255 | var value = replacements[key]; |
1348 | 1256 | return template.split('{' + key.toUpperCase() + '}').join(value); |
1349 | 1257 | }, template); |
1350 | | - }; |
| 1258 | + } |
1351 | 1259 |
|
1352 | | - Template.prototype.match = function (replacements) { |
1353 | | - return Template.match(this.template, replacements); |
1354 | | - }; |
| 1260 | + function Template(template) { |
| 1261 | + return function (replacements) { |
| 1262 | + return match(template, replacements) |
| 1263 | + }; |
| 1264 | + } |
| 1265 | + |
| 1266 | + Template.match = match; |
1355 | 1267 |
|
1356 | 1268 | return Template; |
1357 | 1269 |
|
|
0 commit comments