Skip to content

Commit 10fe89f

Browse files
committed
release v0.2.0
1 parent ee99893 commit 10fe89f

File tree

4 files changed

+109
-19
lines changed

4 files changed

+109
-19
lines changed

build/concat.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ const concat = async (opts = {}) => {
1919
let dist = opts.dist || distPath;
2020
let release = opts.release || false;
2121

22-
console.log(files);
23-
2422
if (!files.includes(entry)) {
2523
files.unshift(entry);
2624
}

dist/uxr.js

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ _.extend.value = function (value) {
9393
* css-classes
9494
**/
9595

96+
/* global normalizeClassName */
97+
9698
const _class = function (stack, className, type) {
97-
return stack.el[0].nodeType === 1 && stack.el.forEach(item => _.internal.maybeMultiple(className).map(className => item.classList[type](className)));
99+
return stack.el[0].nodeType === 1 && stack.el.forEach(item => _.internal.maybeMultiple(className).map(className => item.classList[type](normalizeClassName(className))));
98100
};
99101

100102
_.extend.addClass = function (className) {
@@ -106,15 +108,15 @@ _.extend.removeClass = function (className) {
106108
};
107109

108110
_.extend.hasClass = function (className) {
109-
return this.el[0].nodeType === 1 && this.filter('.' + className).length > 0;
111+
return this.el[0].nodeType === 1 && this.filter('.' + normalizeClassName(className)).length > 0;
110112
};
111113

112114
_.extend.toggleClass = function (className) {
113115
return this.el.forEach(item => {
114116
let classNames = _.internal.maybeMultiple(className);
115117

116118
if (item.nodeType === 1) {
117-
classNames.forEach(_className => item.classList.toggle(_className));
119+
classNames.forEach(className => item.classList.toggle(normalizeClassName(className)));
118120
}
119121
});
120122
};
@@ -309,15 +311,103 @@ _.ready = function (fn) {
309311
document.addEventListener('DOMContentLoaded', fn);
310312
}
311313
};
314+
/**
315+
* utils
316+
**/
317+
318+
/* exported createElementFromString */
319+
320+
// eslint-disable-next-line
321+
const createElementFromString = str => {
322+
let elementString = str.toString();
323+
324+
const tagRegex = /([<])?([a-zA-Z]+)/;
325+
const theTag = elementString.match(tagRegex);
326+
const attrRegex = /(\S+)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/g;
327+
const mayHaveAttributes = elementString.match(attrRegex) || [];
328+
329+
const newElement = document.createElement(theTag[2]);
330+
331+
mayHaveAttributes.forEach(attr => {
332+
let singleAttrRegex = /([a-zA-Z-]+)=(?:['"])(.*)(?:['"])/g;
333+
let theAttr = singleAttrRegex.exec(attr);
334+
335+
newElement.setAttribute(theAttr[1], theAttr[2]);
336+
});
337+
338+
return newElement;
339+
};
340+
341+
// eslint-disable-next-line
342+
const normalizeClassName = className => className.charAt(0) === '.' ? className.substr(1) : className;
312343
/**
313344
* wrap
314345
**/
315346

316-
_.extend.wrap = function () {
347+
/* global createElementFromString */
348+
349+
_.extend.wrap = function (wrapper) {
350+
let newWrap = createElementFromString(wrapper);
351+
352+
let parent = this.el[0].parentNode;
353+
let siblings = this.el[0].nextSibling;
354+
355+
newWrap.appendChild(this.el[0]);
356+
357+
if (siblings) {
358+
parent.insertBefore(newWrap, siblings);
359+
}
360+
else {
361+
parent.appendChild(newWrap);
362+
}
363+
317364
return this;
318365
};
319366

320-
_.extend.unwrap = function () {
367+
_.extend.wrapAll = function (wrapper) {
368+
let firstSibling = true;
369+
let newWrap = createElementFromString(wrapper);
370+
371+
this.el.forEach(item => {
372+
if (firstSibling) {
373+
let parent = item.parentNode;
374+
let siblings = item.nextSibling;
375+
376+
newWrap.appendChild(item);
377+
378+
if (siblings) {
379+
parent.insertBefore(newWrap, siblings);
380+
}
381+
else {
382+
parent.appendChild(newWrap);
383+
}
384+
385+
firstSibling = false;
386+
}
387+
388+
else {
389+
newWrap.appendChild(item);
390+
}
391+
});
392+
393+
return this;
394+
};
395+
396+
_.extend.unwrap = function (selector) {
397+
let parent = this.el[0].parentNode;
398+
399+
// if the parent is not the desired one, skip unwrapping
400+
if (selector && !parent.matches(selector.toString())) {
401+
return this;
402+
}
403+
404+
parent.parentNode.appendChild(this.el[0]);
405+
406+
if (parent.children.length === 0) {
407+
parent.parentNode.removeChild(parent);
408+
}
409+
321410
return this;
322411
};
412+
_.uxr = { version: '0.2.0' };
323413
})();

dist/uxr.min.js

Lines changed: 13 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "uxr",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "DOM utilities",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)