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

Commit

Permalink
Merge branch 'next'
Browse files Browse the repository at this point in the history
  • Loading branch information
Octane committed Nov 18, 2014
2 parents 381b4f7 + c6c187f commit 0d07722
Show file tree
Hide file tree
Showing 21 changed files with 367 additions and 1,014 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ Download the [latest jsCore from GitHub](https://raw.githubusercontent.com/Octan
bower install jscore
```

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.

##Contents
- [`Polyfill`](#polyfill)
- [`Notes/known issues`](#notesknown-issues)
- [Namespace `lib`](#lib)
- [Method `.classExtends`](#libclassextends)
- [Constructor `.Template()`](#libtemplate)
- [Instance method `.match()`](#libtemplate)
- [Constructor `.I18n()`](#libi18n)
- [Instance methods `.add()` and `.use()`](#libi18n)
- [Namespace `.html`](#libhtml)
Expand Down Expand Up @@ -86,7 +87,7 @@ global | `FormData()`<sup>[12](#FormData)</sup>, `Set()`, `Map()`, `WeakSet()`,
`Math` generics | `.trunc()`, `.sign()`
`Function.prototype` | `.bind()`
`Text.prototype` | `.textContent`
`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>
`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>
`HTMLScriptElement.prototype` | `.onload()`<sup>[10](#HTMLScriptElement.prototype.onload)</sup>, `.onerror()`<sup>[10](#HTMLScriptElement.prototype.onload)</sup>
`CSSStyleDeclaration.prototype` | `.getPropertyValue()`, `.removeProperty()`, `.setProperty()`, `.cssFloat`, `.opacity`
`document` | `.head`, `.createEvent()`<sup>[11](#document.createEvent)</sup>
Expand Down Expand Up @@ -162,8 +163,8 @@ Class.Super //→ SuperClass
`.Template()` is a very simple string templating tool (not to be confused with HTML templating)
```javascript
var tmpl = new lib.Template('Hi, {NAME}');
tmpl.match({name: 'John'}) //→ 'Hi, John'
tmpl.match({name: 'Luke'}) //→ 'Hi, Luke'
tmpl({name: 'John'}) //→ 'Hi, John'
tmpl({name: 'Luke'}) //→ 'Hi, Luke'
```

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

####lib.html.escape()
Expand Down
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jsCore",
"main": "jscore.js",
"version": "0.6.1",
"version": "0.7.0",
"description": "Complex JavaScript polyfill and set of methods",
"keywords": [
"browser",
Expand Down Expand Up @@ -29,4 +29,4 @@
"src",
"test"
]
}
}
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#jsCore changelog

##v0.7.0

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

##v0.6.1

- fix: `.find()` and `.findIndex()` no longer ignore array element holes
Expand Down
184 changes: 48 additions & 136 deletions dev/jscore-ie10.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* jsCore JavaScript library v0.6.1 IE10+
/* jsCore JavaScript library v0.7.0 IE10+
* © 2014 Dmitry Korobkin
* Released under the MIT license
* github.com/Octane/jsCore
Expand Down Expand Up @@ -159,7 +159,7 @@ if (!Array.prototype.contains) {
}
return false;
}
return -1 != this.indexOf(anything, position);
return -1 != Array.prototype.indexOf.call(this, anything, position);
};
}

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

};

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

var ELEMENT_NODE = 1,
proto = HTMLElement.prototype,
api = {

before: function (/* ...nodes */) {
//todo IE8 removedNode.parentNode ≠ null
var parentNode = this.parentNode;
if (parentNode) {
parentNode.insertBefore(mutationMacro(arguments), this);
}
},

after: function (/* ...nodes */) {
var parentNode = this.parentNode,
nextSibling,
nodes;
if (parentNode) {
nodes = mutationMacro(arguments);
nextSibling = this.nextSibling;
if (nextSibling) {
parentNode.insertBefore(nodes, nextSibling);
} else {
parentNode.appendChild(nodes);
}
}
},

replace: function (/* ...nodes */) {
var parentNode = this.parentNode;
if (parentNode) {
parentNode.replaceChild(mutationMacro(arguments), this);
}
},

remove: function () {
var parentNode = this.parentNode;
if (parentNode) {
parentNode.removeChild(this);
}
},

append: function (/* ...nodes */) {
this.appendChild(mutationMacro(arguments));
},
var proto = HTMLElement.prototype;

prepend: function () {
this.insertBefore(mutationMacro(arguments), this.firstChild);
},
proto.matches = [
proto.matchesSelector,
proto.oMatchesSelector,
proto.msMatchesSelector,
proto.mozMatchesSelector,
proto.webkitMatchesSelector
].find(Boolean);

querySelector: proto.querySelector,

querySelectorAll: proto.querySelectorAll,

matches: [
proto.matchesSelector,
proto.oMatchesSelector,
proto.msMatchesSelector,
proto.mozMatchesSelector,
proto.webkitMatchesSelector,
function (selector) {
var contains,
root;
if (this === document) {
//if docFragment.constructor ≡ document.constructor
return false;
}
if (!proto.matches) {
proto.matches = new function () {
var ELEMENT_NODE = 1;
function isContains(root, element, selector) {
return Array.contains(root.querySelectorAll(selector), element);
}
return function (selector) {
var contains,
root = this.parentNode;
if (root) {
if (ELEMENT_NODE == root.nodeType) {
root = root.ownerDocument;
}
return isContains(root, this, selector);
if (root) {
if (ELEMENT_NODE == root.nodeType) {
root = root.ownerDocument;
}
root = document.createDocumentFragment();
root.appendChild(this);
contains = isContains(root, this, selector);
root.removeChild(this);
return isContains(root, this, selector);
}
].find(Boolean)

root = document.createDocumentFragment();
root.appendChild(this);
contains = isContains(root, this, selector);
root.removeChild(this);
};
};

function isContains(root, element, selector) {
return -1 != Array.indexOf(root.querySelectorAll(selector), element);
}

function mutationMacro(nodes) {
var node,
fragment,
length = nodes.length,
i;
if (1 == length) {
node = nodes[0];
if ('string' == typeof node) {
return document.createTextNode(node);
}
return node;
}
fragment = document.createDocumentFragment();
nodes = Array.from(nodes);
i = 0;
while (i < length) {
node = nodes[i];
if ('string' == typeof node) {
node = document.createTextNode(node);
}
fragment.appendChild(node);
i++;
}
return fragment;
}

function implement(key) {
if (!(key in proto)) {
proto[key] = api[key];
}
}

Object.keys(api).forEach(implement);
};

proto = document.constructor.prototype;
['querySelector', 'querySelectorAll'].forEach(implement);
new function () {

proto = document.createDocumentFragment().constructor.prototype;
[
'append', 'prepend',
'querySelector', 'querySelectorAll',
'matches'
].forEach(implement);
var docFragment = document.createDocumentFragment(),
target = docFragment.constructor.prototype,
source = HTMLElement.prototype;
if (!docFragment.querySelector) {
target.querySelector = source.querySelector;
target.querySelectorAll = source.querySelectorAll;
}

};

Expand Down Expand Up @@ -1332,26 +1246,24 @@ lib.html = {

};

//example: new lib.Template('Hi, {NAME}').match({name: 'John'}) → 'Hi, John'
//example: var tmpl = new lib.Template('Hi, {NAME}');
// tmpl({name: 'John'}) → 'Hi, John'
lib.Template = new function () {

function Template(template) {
this.template = template;
}

Template.match = function (template, replacements) {
if (Array.isArray(template)) {
template = template.join('');
}
function match(template, replacements) {
return Object.keys(replacements).reduceRight(function (template, key) {
var value = replacements[key];
return template.split('{' + key.toUpperCase() + '}').join(value);
}, template);
};
}

Template.prototype.match = function (replacements) {
return Template.match(this.template, replacements);
};
function Template(template) {
return function (replacements) {
return match(template, replacements)
};
}

Template.match = match;

return Template;

Expand Down
Loading

0 comments on commit 0d07722

Please sign in to comment.