Skip to content

Commit 403db69

Browse files
committed
release v0.4.0
1 parent 6c70958 commit 403db69

File tree

4 files changed

+123
-66
lines changed

4 files changed

+123
-66
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ node_modules/
3939
jspm_packages/
4040
test/
4141
build/
42+
dist/
4243

4344
# Typescript v1 declaration files
4445
typings/

README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ All of the following strings are valid wrapper definitions
193193
* `<div class='wrapper' id="container"></div>`
194194
195195
### Element Insertions
196-
By using `before`, `after`, `prepend` and `append` you can control where to insert newly created elements.
196+
By using `before`, `after`, `prepend` and `append` you can control where to insert newly created elements. Also with `replaceWith` you can swap the element with a new one.
197197
198198
``` js
199199
let el = uxr('.container');
@@ -213,8 +213,46 @@ el.append(uxr('#new'));
213213
// appends an element add the beginning of selection's content
214214
el.prepend('<p>This will before "el"</p>');
215215
el.prepend(uxr('#new'));
216+
217+
// replaces the element with new one
218+
el.replaceWith('<div id="replaced">Previous element replaced</div>');
216219
```
217220
221+
### Traversing
222+
With traversal methods, you can find adjacent or parent elements accordingly. Almost all traversal methods returns a `uxr` object. You can return the previous `uxr` by chaining `end()`
223+
224+
```javascript
225+
226+
let el = uxr('li');
227+
228+
// get the immediate parent
229+
el.closest();
230+
231+
// get the grandparent
232+
el.closest().closest();
233+
234+
// filter the parents and get the first matched
235+
el.closest(selector);
236+
237+
// get the next sibling
238+
el.next();
239+
240+
// get the next sibling if matched
241+
el.next(selector);
242+
243+
// get the previous sibling
244+
el.prev();
245+
246+
// get the previous sibling if matched
247+
el.prev(selector);
248+
249+
// get the first element in uxr object - selection
250+
el.first();
251+
252+
// get the last element in uxr object - selection
253+
el.last();
254+
```
255+
218256
[npm]: https://img.shields.io/npm/v/uxr.svg
219257
[npm-url]: https://npmjs.com/package/uxr
220258

dist/uxr.js

Lines changed: 68 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -255,25 +255,15 @@ _.extend.trigger = function (eventName, selector) {
255255
* filter
256256
**/
257257

258-
_.extend.filter = function (criteria) {
259-
let _this = this;
260-
let filtered = _(_this.el.filter(item => item.matches(criteria)));
261-
262-
filtered.prevObj = this;
258+
/* global mutated */
263259

264-
return filtered;
260+
_.extend.filter = function (criteria) {
261+
return mutated(this, this.el.filter(item => item.matches(criteria)));
265262
};
266263

267264
_.extend.find = function (criteria) {
268-
let _this = this;
269-
let found = _(_this.el[0].querySelectorAll(criteria));
270-
271-
found.prevObj = this;
272-
273-
return found;
265+
return mutated(this, this.el[0].querySelectorAll(criteria));
274266
};
275-
276-
Element.prototype.matches = Element.prototype.matches ? Element.prototype.matches : Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
277267
/**
278268
* manipulation
279269
**/
@@ -282,42 +272,36 @@ Element.prototype.matches = Element.prototype.matches ? Element.prototype.matche
282272
/* global insertBefore */
283273

284274
_.extend.empty = function () {
285-
this.el.forEach(item => item.innerHTML = '');
286-
287-
return this;
275+
return this.el.forEach(item => item.innerHTML = '');
288276
};
289277

290278
_.extend.remove = function () {
291-
this.el.forEach(item => item.parentNode.removeChild(item));
292-
293-
return this;
279+
return this.el.forEach(item => item.parentNode.removeChild(item));
294280
};
295281

296282
_.extend.append = function (stringOrObject) {
297-
this.el.forEach(item => item.appendChild(getInsertableElement(stringOrObject)));
298-
299-
return this;
283+
return this.el.forEach(item => item.appendChild(getInsertableElement(stringOrObject)));
300284
};
301285

302286
_.extend.prepend = function (stringOrObject) {
303-
this.el.forEach(
287+
return this.el.forEach(
304288
item => insertBefore(stringOrObject, item, 'firstChild'));
305-
306-
return this;
307289
};
308290

309291
_.extend.after = function (stringOrObject) {
310-
this.el.forEach(
292+
return this.el.forEach(
311293
item => insertBefore(stringOrObject, item, 'nextSibling', true));
312-
313-
return this;
314294
};
315295

316296
_.extend.before = function (stringOrObject) {
317-
this.el.forEach(
297+
return this.el.forEach(
318298
item => insertBefore(stringOrObject, item, 'self', true));
299+
};
319300

320-
return this;
301+
_.extend.replaceWith = function (stringOrObject) {
302+
return this.el.map(
303+
item => item.parentNode.replaceChild(getInsertableElement(stringOrObject), item)
304+
);
321305
};
322306
/**
323307
* ready
@@ -331,32 +315,47 @@ _.ready = function (fn) {
331315
}
332316
};
333317
/**
334-
* utils
318+
* traversing
335319
**/
336320

337-
/* exported createElementFromString */
321+
/* global mutated */
338322

339-
// eslint-disable-next-line
340-
const createElementFromString = str => {
341-
let elementString = str.toString();
323+
_.extend.closest = function (selector) {
324+
let el = this.el[0];
342325

343-
const tagRegex = /([<])?([a-zA-Z]+)/;
344-
const theTag = elementString.match(tagRegex);
345-
const attrRegex = /(\S+)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/g;
346-
const mayHaveAttributes = elementString.match(attrRegex) || [];
326+
while (el !== null && el.nodeType === 1) {
327+
if (el.matches(selector)) {
328+
return el;
329+
}
347330

348-
const newElement = document.createElement(theTag[2]);
331+
el = el.parentNode;
332+
}
349333

350-
mayHaveAttributes.forEach(attr => {
351-
let singleAttrRegex = /([a-zA-Z-]+)=(?:['"])(.*)(?:['"])/g;
352-
let theAttr = singleAttrRegex.exec(attr);
334+
return null;
335+
};
353336

354-
newElement.setAttribute(theAttr[1], theAttr[2]);
355-
});
337+
_.extend.next = function (selector) {
338+
return mutated(this, this.el.map(item => item.nextElementSibling).filter(item => selector ? item.matches(selector) : item));
339+
};
340+
341+
_.extend.prev = function (selector) {
342+
return mutated(this, this.el.map(item => item.previousElementSibling).filter(item => selector ? item.matches(selector) : item));
343+
};
356344

357-
return newElement;
345+
_.extend.first = function () {
346+
return mutated(this, this.el.filter((item, index) => index === 0));
358347
};
359348

349+
_.extend.last = function () {
350+
let last = this.length - 1;
351+
return mutated(this, this.el.filter((item, index) => index === last));
352+
};
353+
/**
354+
* utils
355+
**/
356+
357+
Element.prototype.matches = Element.prototype.matches ? Element.prototype.matches : Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
358+
360359
// eslint-disable-next-line
361360
const normalizeClassName = className => className.charAt(0) === '.' ? className.substr(1) : className;
362361

@@ -412,14 +411,32 @@ const insertBefore = (insert, target, ref, parent) => {
412411

413412
to.insertBefore(getInsertableElement(insert), where);
414413
};
414+
415+
// mutatedObj
416+
// eslint-disable-next-line
417+
const mutated = (orgObj, newSet) => {
418+
let obj = _(newSet);
419+
420+
obj.prevObj = orgObj;
421+
422+
return obj;
423+
};
415424
/**
416425
* wrap
417426
**/
418427

419-
/* global createElementFromString */
428+
/* global elementFromString */
429+
430+
const getWrapper = wrapperStr => {
431+
let wrapperString = wrapperStr.toString();
432+
433+
return wrapperString.charAt(0) !== '<' ?
434+
document.createElement(wrapperString) :
435+
elementFromString(wrapperString);
436+
};
420437

421438
_.extend.wrap = function (wrapper) {
422-
let newWrap = createElementFromString(wrapper);
439+
let newWrap = getWrapper(wrapper);
423440

424441
let parent = this.el[0].parentNode;
425442
let siblings = this.el[0].nextSibling;
@@ -438,7 +455,7 @@ _.extend.wrap = function (wrapper) {
438455

439456
_.extend.wrapAll = function (wrapper) {
440457
let firstSibling = true;
441-
let newWrap = createElementFromString(wrapper);
458+
let newWrap = getWrapper(wrapper);
442459

443460
this.el.forEach(item => {
444461
if (firstSibling) {
@@ -481,5 +498,5 @@ _.extend.unwrap = function (selector) {
481498

482499
return this;
483500
};
484-
_.uxr = { version: '0.3.0' };
501+
_.uxr = { version: '0.4.0' };
485502
})();

dist/uxr.min.js

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

0 commit comments

Comments
 (0)