Skip to content

Commit 8a65eab

Browse files
committed
zest: many methods should not accept a css-selector.. helper's argsToElements method now has an acceptSelector param
1 parent 8a587c6 commit 8a65eab

9 files changed

Lines changed: 56 additions & 35 deletions

File tree

src/Debug/Plugin/Channel.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Channel implements SubscriberInterface
5454
*
5555
* @since 2.3
5656
*/
57-
public function getChannel($path, $config = array())
57+
public function getChannel($path, array $config = array())
5858
{
5959
$path = $this->normalizePath($path);
6060
$key = \array_shift($path);
@@ -144,7 +144,7 @@ public function getChannelsTop()
144144
*
145145
* @return array
146146
*/
147-
public function getPropagateValues($cfg)
147+
public function getPropagateValues(array $cfg)
148148
{
149149
$cfg = \array_diff_key($cfg, \array_flip([
150150
'errorHandler',
@@ -226,7 +226,7 @@ public function sortChannelCallback(Debug $channelA, Debug $channelB)
226226
*
227227
* @return array
228228
*/
229-
private function createChannel($key, $config)
229+
private function createChannel($key, array $config)
230230
{
231231
$cfg = $this->debug->getCfg(null, Debug::CONFIG_INIT);
232232
$channelKeyCur = $cfg['debug']['channelKey'];
@@ -274,7 +274,7 @@ private function normalizePath($path)
274274
*
275275
* @return Debug new or existing `Debug` instance
276276
*/
277-
private function upsertChannel($key, $path, $config)
277+
private function upsertChannel($key, $path, array $config)
278278
{
279279
$curChannelKey = $this->debug->getCfg('channelKey', Debug::CONFIG_DEBUG);
280280
if (!isset($this->channels[$curChannelKey][$key])) {

src/Debug/js/zest.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,16 @@ var zest = (function () {
215215
*
216216
* accepts text/html/css-selector, Node, NodeList, function, iterable
217217
*/
218-
const argsToElements = function (args, el, index) {
218+
const argsToElements = function (args, acceptSelector, el, index) {
219219
const elements = [];
220220
args = Array.from(args); // shallow copy so not affecting original
221221
while (args.length) {
222222
const arg = args.shift();
223223
if (typeof arg === 'string') {
224-
let elementsAppend = isCssSelector(arg);
224+
let elementsAppend = false;
225+
if (acceptSelector) {
226+
elementsAppend = isCssSelector(arg);
227+
}
225228
if (elementsAppend === false) {
226229
elementsAppend = createElements(arg);
227230
}
@@ -335,6 +338,7 @@ var zest = (function () {
335338
}
336339
*/
337340

341+
// return found elements or false
338342
const isCssSelector = function (val)
339343
{
340344
if (val.includes('<')) {
@@ -807,7 +811,7 @@ var zest = (function () {
807811
} else if (typeof mixed === 'function') {
808812
isMatch = mixed.call(el, el, i);
809813
} else {
810-
isMatch = argsToElements([mixed]).includes(el);
814+
isMatch = argsToElements([mixed], true).includes(el);
811815
}
812816
if (notFilter) {
813817
isMatch = !isMatch;
@@ -844,6 +848,7 @@ var zest = (function () {
844848
}
845849
return false
846850
}
851+
// Node, NodeList, function, iterable
847852
const elements = argsToElements([mixed]);
848853
for (let el of this) {
849854
for (let i = 0, len = elements.length; i < len; i++) {
@@ -1061,7 +1066,7 @@ var zest = (function () {
10611066
if (typeof mixed === 'string') {
10621067
return this.alter((el) => querySelectorAll(mixed, el))
10631068
}
1064-
const elements = argsToElements([mixed]);
1069+
const elements = argsToElements([mixed], true);
10651070
return this.alter((el) => {
10661071
const collected = [];
10671072
for (const el2 of elements) {
@@ -1491,7 +1496,7 @@ var zest = (function () {
14911496

14921497
after( ...args ) {
14931498
return this.each((el, i) => {
1494-
const elementsNew = argsToElements(args, el, i);
1499+
const elementsNew = argsToElements(args, false, el, i);
14951500
elementsNew.reverse();
14961501
for (const elNew of elementsNew) {
14971502
el.after(elNew);
@@ -1500,15 +1505,15 @@ var zest = (function () {
15001505
}
15011506
append( ...args ) {
15021507
return this.each((el, i) => {
1503-
const elementsNew = argsToElements(args, el, i);
1508+
const elementsNew = argsToElements(args, false, el, i);
15041509
for (const elNew of elementsNew) {
15051510
el.append(elNew);
15061511
}
15071512
})
15081513
}
15091514
before( ...args ) {
15101515
return this.each((el, i) => {
1511-
const elementsNew = argsToElements(args, el, i);
1516+
const elementsNew = argsToElements(args, false, el, i);
15121517
for (const elNew of elementsNew) {
15131518
el.before(elNew);
15141519
}
@@ -1565,7 +1570,7 @@ var zest = (function () {
15651570
}
15661571
prepend( ...args ) {
15671572
return this.each((el, i) => {
1568-
const elementsNew = argsToElements(args, el, i);
1573+
const elementsNew = argsToElements(args, false, el, i);
15691574
elementsNew.reverse();
15701575
for (const elNew of elementsNew) {
15711576
el.prepend(elNew);
@@ -1584,7 +1589,7 @@ var zest = (function () {
15841589
}
15851590
replaceWith( ...args ) {
15861591
return this.each((el, i) => {
1587-
const elementsNew = argsToElements(args, el, i);
1592+
const elementsNew = argsToElements(args, false, el, i);
15881593
el.replaceWith(...elementsNew);
15891594
})
15901595
}
@@ -1637,8 +1642,8 @@ var zest = (function () {
16371642
if (typeof mixed === 'function') {
16381643
mixed = mixed.call(el, el, i);
16391644
}
1640-
// mixed can be a string, MicroDom instance, or a DOM element
1641-
const elements = argsToElements([mixed]);
1645+
// mixed can be a string (but not css selector), MicroDom instance, or a DOM element
1646+
const elements = argsToElements([mixed], false);
16421647
const wrapperElement = elements[0].cloneNode(true);
16431648
const wrapperElementDeepest = findDeepest(wrapperElement);
16441649
el.replaceWith(wrapperElement);
@@ -1650,8 +1655,8 @@ var zest = (function () {
16501655
if (typeof mixed === 'function') {
16511656
mixed = mixed.call(el, el, i);
16521657
}
1653-
// mixed can be a string, MicroDom instance, or a DOM element
1654-
const elements = argsToElements([mixed]);
1658+
// mixed can be a string (but not css selector), MicroDom instance, or a DOM element
1659+
const elements = argsToElements([mixed], false);
16551660
const wrapperElement = elements[0].cloneNode(true);
16561661
const wrapperElementDeepest = findDeepest(wrapperElement);
16571662
// Move the element's children (incl text/comment nodes) into the wrapper

src/Debug/js/zest.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Debug/js_src/zest/helper.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,16 @@ const getDisplayValue = function (el) {
5454
*
5555
* accepts text/html/css-selector, Node, NodeList, function, iterable
5656
*/
57-
export const argsToElements = function (args, el, index) {
57+
export const argsToElements = function (args, acceptSelector, el, index) {
5858
const elements = []
5959
args = Array.from(args) // shallow copy so not affecting original
6060
while (args.length) {
6161
const arg = args.shift()
6262
if (typeof arg === 'string') {
63-
let elementsAppend = isCssSelector(arg)
63+
let elementsAppend = false
64+
if (acceptSelector) {
65+
elementsAppend = isCssSelector(arg)
66+
}
6467
if (elementsAppend === false) {
6568
elementsAppend = createElements(arg)
6669
}
@@ -174,6 +177,7 @@ function hash (str) {
174177
}
175178
*/
176179

180+
// return found elements or false
177181
const isCssSelector = function (val)
178182
{
179183
if (val.includes('<')) {

src/Debug/js_src/zest/microDom/MicroDom.filter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function filter (mixed, notFilter = false) {
99
} else if (typeof mixed === 'function') {
1010
isMatch = mixed.call(el, el, i)
1111
} else {
12-
isMatch = helper.argsToElements([mixed]).includes(el)
12+
isMatch = helper.argsToElements([mixed], true).includes(el)
1313
}
1414
if (notFilter) {
1515
isMatch = !isMatch
@@ -46,6 +46,7 @@ function is (mixed) {
4646
}
4747
return false
4848
}
49+
// Node, NodeList, function, iterable
4950
const elements = helper.argsToElements([mixed])
5051
for (let el of this) {
5152
for (let i = 0, len = elements.length; i < len; i++) {

src/Debug/js_src/zest/microDom/MicroDom.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default class MicroDom extends Array {
7373

7474
after( ...args ) {
7575
return this.each((el, i) => {
76-
const elementsNew = helper.argsToElements(args, el, i)
76+
const elementsNew = helper.argsToElements(args, false, el, i)
7777
elementsNew.reverse()
7878
for (const elNew of elementsNew) {
7979
el.after(elNew)
@@ -82,15 +82,15 @@ export default class MicroDom extends Array {
8282
}
8383
append( ...args ) {
8484
return this.each((el, i) => {
85-
const elementsNew = helper.argsToElements(args, el, i)
85+
const elementsNew = helper.argsToElements(args, false, el, i)
8686
for (const elNew of elementsNew) {
8787
el.append(elNew)
8888
}
8989
})
9090
}
9191
before( ...args ) {
9292
return this.each((el, i) => {
93-
const elementsNew = helper.argsToElements(args, el, i)
93+
const elementsNew = helper.argsToElements(args, false, el, i)
9494
for (const elNew of elementsNew) {
9595
el.before(elNew)
9696
}
@@ -147,7 +147,7 @@ export default class MicroDom extends Array {
147147
}
148148
prepend( ...args ) {
149149
return this.each((el, i) => {
150-
const elementsNew = helper.argsToElements(args, el, i)
150+
const elementsNew = helper.argsToElements(args, false, el, i)
151151
elementsNew.reverse()
152152
for (const elNew of elementsNew) {
153153
el.prepend(elNew)
@@ -166,7 +166,7 @@ export default class MicroDom extends Array {
166166
}
167167
replaceWith( ...args ) {
168168
return this.each((el, i) => {
169-
const elementsNew = helper.argsToElements(args, el, i)
169+
const elementsNew = helper.argsToElements(args, false, el, i)
170170
el.replaceWith(...elementsNew)
171171
})
172172
}
@@ -219,8 +219,8 @@ export default class MicroDom extends Array {
219219
if (typeof mixed === 'function') {
220220
mixed = mixed.call(el, el, i)
221221
}
222-
// mixed can be a string, MicroDom instance, or a DOM element
223-
const elements = helper.argsToElements([mixed])
222+
// mixed can be a string (but not css selector), MicroDom instance, or a DOM element
223+
const elements = helper.argsToElements([mixed], false)
224224
const wrapperElement = elements[0].cloneNode(true)
225225
const wrapperElementDeepest = helper.findDeepest(wrapperElement)
226226
el.replaceWith(wrapperElement)
@@ -232,8 +232,8 @@ export default class MicroDom extends Array {
232232
if (typeof mixed === 'function') {
233233
mixed = mixed.call(el, el, i)
234234
}
235-
// mixed can be a string, MicroDom instance, or a DOM element
236-
const elements = helper.argsToElements([mixed])
235+
// mixed can be a string (but not css selector), MicroDom instance, or a DOM element
236+
const elements = helper.argsToElements([mixed], false)
237237
const wrapperElement = elements[0].cloneNode(true)
238238
const wrapperElementDeepest = helper.findDeepest(wrapperElement)
239239
// Move the element's children (incl text/comment nodes) into the wrapper

src/Debug/js_src/zest/microDom/MicroDom.traverse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function find(mixed) {
4848
if (typeof mixed === 'string') {
4949
return this.alter((el) => customSelectors.querySelectorAll(mixed, el))
5050
}
51-
const elements = helper.argsToElements([mixed])
51+
const elements = helper.argsToElements([mixed], true)
5252
return this.alter((el) => {
5353
const collected = []
5454
for (const el2 of elements) {

src/Debug/js_tests/zest/microDom/MicroDom.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('MicroDom core methods', () => {
3838
test('add method', () => {
3939
const $div1 = zest('#div1');
4040
// Add element by selector
41-
const $combined = $div1.add('#div2');
41+
const $combined = $div1.add(zest('#div2'));
4242
expect($combined.length).toBe(2);
4343
expect($combined[1].id).toBe('div2');
4444

@@ -62,7 +62,7 @@ describe('MicroDom core methods', () => {
6262
expect($addArray.length).toBe(3);
6363

6464
// Should remove duplicates
65-
const $noDupes = $div1.add('#div1');
65+
const $noDupes = $div1.add(zest('#div1'));
6666
expect($noDupes.length).toBe(1);
6767
});
6868

src/Debug/js_tests/zest/zest.test.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,21 @@ describe('helper.elInitMicroDomInfo function', () => {
380380
});
381381

382382
describe('helper.argsToElements function', () => {
383-
test('converts string selectors to elements', () => {
383+
test('does not convert selectors to elements (default)', () => {
384384
document.body.innerHTML = '<div id="test"></div>';
385385
const elements = helper.argsToElements(['.non-existent', '#test']);
386386

387+
expect(elements.length).toBe(2);
388+
expect(elements[0].nodeType).toBe(Node.TEXT_NODE);
389+
expect(elements[0].textContent).toBe('.non-existent');
390+
expect(elements[1].nodeType).toBe(Node.TEXT_NODE);
391+
expect(elements[1].textContent).toBe('#test');
392+
});
393+
394+
test('converts css selectors to elements', () => {
395+
document.body.innerHTML = '<div id="test"></div>';
396+
const elements = helper.argsToElements(['.non-existent', '#test'], true);
397+
387398
expect(elements.length).toBe(1);
388399
expect(elements[0].id).toBe('test');
389400
});
@@ -441,7 +452,7 @@ describe('helper.argsToElements function', () => {
441452
div1,
442453
'<div id="test2"></div>',
443454
'#test3'
444-
]);
455+
], true);
445456

446457
expect(elements.length).toBe(3);
447458
expect(elements[0]).toBe(div1);

0 commit comments

Comments
 (0)