Skip to content
This repository was archived by the owner on Aug 21, 2022. It is now read-only.

Commit 0d07722

Browse files
committed
Merge branch 'next'
2 parents 381b4f7 + c6c187f commit 0d07722

21 files changed

+367
-1014
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ Download the [latest jsCore from GitHub](https://raw.githubusercontent.com/Octan
1111
bower install jscore
1212
```
1313

14+
This is designed to be run in a browser and it depends on there being a document. It does not work in a Node.js or worker environment.
15+
1416
##Contents
1517
- [`Polyfill`](#polyfill)
1618
- [`Notes/known issues`](#notesknown-issues)
1719
- [Namespace `lib`](#lib)
1820
- [Method `.classExtends`](#libclassextends)
1921
- [Constructor `.Template()`](#libtemplate)
20-
- [Instance method `.match()`](#libtemplate)
2122
- [Constructor `.I18n()`](#libi18n)
2223
- [Instance methods `.add()` and `.use()`](#libi18n)
2324
- [Namespace `.html`](#libhtml)
@@ -86,7 +87,7 @@ global | `FormData()`<sup>[12](#FormData)</sup>, `Set()`, `Map()`, `WeakSet()`,
8687
`Math` generics | `.trunc()`, `.sign()`
8788
`Function.prototype` | `.bind()`
8889
`Text.prototype` | `.textContent`
89-
`HTMLElement.prototype` | `.append()`, `.prepend()`, `.after()`, `.before()`, `.replace()`, `.remove()`, `.matches()`, `.addEventListener()`, `.removeEventListner()`, `.dispatchEvent()`, `.children`<sup>[7](#HTMLElement.prototype.children)</sup>, `.firstElementChild`, `.lastElementChild`, `.childElementCount`, `.nextElementSibling`, `.previousElementSibling`, `.textContent`, `.classList`<sup>[8](#HTMLElement.prototype.classList)</sup>, `.dataset`<sup>[9](#HTMLElement.prototype.dataset)</sup>
90+
`HTMLElement.prototype` | `.matches()`, `.addEventListener()`, `.removeEventListner()`, `.dispatchEvent()`, `.children`<sup>[7](#HTMLElement.prototype.children)</sup>, `.firstElementChild`, `.lastElementChild`, `.childElementCount`, `.nextElementSibling`, `.previousElementSibling`, `.textContent`, `.classList`<sup>[8](#HTMLElement.prototype.classList)</sup>, `.dataset`<sup>[9](#HTMLElement.prototype.dataset)</sup>
9091
`HTMLScriptElement.prototype` | `.onload()`<sup>[10](#HTMLScriptElement.prototype.onload)</sup>, `.onerror()`<sup>[10](#HTMLScriptElement.prototype.onload)</sup>
9192
`CSSStyleDeclaration.prototype` | `.getPropertyValue()`, `.removeProperty()`, `.setProperty()`, `.cssFloat`, `.opacity`
9293
`document` | `.head`, `.createEvent()`<sup>[11](#document.createEvent)</sup>
@@ -162,8 +163,8 @@ Class.Super //→ SuperClass
162163
`.Template()` is a very simple string templating tool (not to be confused with HTML templating)
163164
```javascript
164165
var tmpl = new lib.Template('Hi, {NAME}');
165-
tmpl.match({name: 'John'}) //→ 'Hi, John'
166-
tmpl.match({name: 'Luke'}) //→ 'Hi, Luke'
166+
tmpl({name: 'John'}) //→ 'Hi, John'
167+
tmpl({name: 'Luke'}) //→ 'Hi, Luke'
167168
```
168169

169170
###lib.I18n()
@@ -202,7 +203,7 @@ i18n('currency', {cost: 100}) // → '100 руб.'
202203
`.parse()` converts a HTML code into a document fragment
203204
```javascript
204205
var docFragment = lib.html.parse('<h1>Example</h1><p>...</p>');
205-
document.body.append(docFragment);
206+
document.body.appendChild(docFragment);
206207
```
207208

208209
####lib.html.escape()

bower.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "jsCore",
33
"main": "jscore.js",
4-
"version": "0.6.1",
4+
"version": "0.7.0",
55
"description": "Complex JavaScript polyfill and set of methods",
66
"keywords": [
77
"browser",
@@ -29,4 +29,4 @@
2929
"src",
3030
"test"
3131
]
32-
}
32+
}

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#jsCore changelog
22

3+
##v0.7.0
4+
5+
- fix: `Array.contains()` for non-Array objects
6+
- del: `.prepend()`, `.append()`, `.before()`, `.after()` and `.replace()` methods ([excluded from the W3C DOM 4 editor's draft](https://github.com/w3c/dom/commit/87000ba632e83f40a2384a4a15f3b913ef167653))
7+
- `lib.Template()` now returns a function
8+
39
##v0.6.1
410

511
- fix: `.find()` and `.findIndex()` no longer ignore array element holes

dev/jscore-ie10.js

Lines changed: 48 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* jsCore JavaScript library v0.6.1 IE10+
1+
/* jsCore JavaScript library v0.7.0 IE10+
22
* © 2014 Dmitry Korobkin
33
* Released under the MIT license
44
* github.com/Octane/jsCore
@@ -159,7 +159,7 @@ if (!Array.prototype.contains) {
159159
}
160160
return false;
161161
}
162-
return -1 != this.indexOf(anything, position);
162+
return -1 != Array.prototype.indexOf.call(this, anything, position);
163163
};
164164
}
165165

@@ -1043,138 +1043,52 @@ Object.defineProperty(HTMLElement.prototype, 'dataset', {
10431043

10441044
};
10451045

1046-
//DOM4 w3c.github.io/dom
1047-
'append' in document.createDocumentFragment() || new function () {
1046+
document.documentElement.matches || new function () {
10481047

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;
10931049

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);
10971057

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,
11151066
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;
11211070
}
1122-
root = document.createDocumentFragment();
1123-
root.appendChild(this);
1124-
contains = isContains(root, this, selector);
1125-
root.removeChild(this);
1071+
return isContains(root, this, selector);
11261072
}
1127-
].find(Boolean)
1128-
1073+
root = document.createDocumentFragment();
1074+
root.appendChild(this);
1075+
contains = isContains(root, this, selector);
1076+
root.removeChild(this);
1077+
};
11291078
};
1130-
1131-
function isContains(root, element, selector) {
1132-
return -1 != Array.indexOf(root.querySelectorAll(selector), element);
11331079
}
11341080

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+
};
11681082

1169-
proto = document.constructor.prototype;
1170-
['querySelector', 'querySelectorAll'].forEach(implement);
1083+
new function () {
11711084

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+
}
11781092

11791093
};
11801094

@@ -1332,26 +1246,24 @@ lib.html = {
13321246

13331247
};
13341248

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'
13361251
lib.Template = new function () {
13371252

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) {
13461254
return Object.keys(replacements).reduceRight(function (template, key) {
13471255
var value = replacements[key];
13481256
return template.split('{' + key.toUpperCase() + '}').join(value);
13491257
}, template);
1350-
};
1258+
}
13511259

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;
13551267

13561268
return Template;
13571269

0 commit comments

Comments
 (0)