Skip to content

Commit f0796fa

Browse files
$.fn.text: ensuring it reads text from every node in the collection, not just the first one, for better alignment with jQuery
1 parent ffde249 commit f0796fa

8 files changed

Lines changed: 96 additions & 60 deletions

File tree

dist/cash.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ interface Cash {
5252
interface Cash {
5353
empty(): this;
5454
}
55-
interface Cash {
56-
text(): string;
57-
text(text: string): this;
58-
}
5955
interface CashStatic {
6056
extend(): any;
6157
extend(deep: true, target: any, ...sources: any[]): any;
@@ -99,6 +95,10 @@ interface Cash {
9995
interface Cash {
10096
last(): Cash;
10197
}
98+
interface Cash {
99+
text(): string;
100+
text(text: string): this;
101+
}
102102
interface Cash {
103103
filter(comparator?: Comparator): Cash;
104104
}

dist/cash.esm.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ function isDocumentFragment(value) {
7979
function isElement(value) {
8080
return !!value && value.nodeType === 1;
8181
}
82+
function isText(value) {
83+
return !!value && value.nodeType === 3;
84+
}
8285
function isBoolean(value) {
8386
return typeof value === 'boolean';
8487
}
@@ -143,17 +146,6 @@ fn.empty = function () {
143146
}
144147
});
145148
};
146-
function text(text) {
147-
if (isUndefined(text))
148-
return this[0] ? this[0].textContent : '';
149-
return this.each((i, ele) => {
150-
if (!isElement(ele))
151-
return;
152-
ele.textContent = text;
153-
});
154-
}
155-
;
156-
fn.text = text;
157149
function extend(...sources) {
158150
const deep = isBoolean(sources[0]) ? sources.shift() : false;
159151
const target = sources.shift();
@@ -264,6 +256,17 @@ fn.first = function () {
264256
fn.last = function () {
265257
return this.eq(-1);
266258
};
259+
function text(text) {
260+
if (isUndefined(text)) {
261+
return this.get().map(ele => isElement(ele) || isText(ele) ? ele.textContent : '').join('');
262+
}
263+
return this.each((i, ele) => {
264+
if (!isElement(ele))
265+
return;
266+
ele.textContent = text;
267+
});
268+
}
269+
fn.text = text;
267270
// @require core/type_checking.ts
268271
// @require core/variables.ts
269272
function computeStyle(ele, prop, isVariable) {

dist/cash.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ function isDocumentFragment(value) {
8282
function isElement(value) {
8383
return !!value && value.nodeType === 1;
8484
}
85+
function isText(value) {
86+
return !!value && value.nodeType === 3;
87+
}
8588
function isBoolean(value) {
8689
return typeof value === 'boolean';
8790
}
@@ -146,17 +149,6 @@ fn.empty = function () {
146149
}
147150
});
148151
};
149-
function text(text) {
150-
if (isUndefined(text))
151-
return this[0] ? this[0].textContent : '';
152-
return this.each(function (i, ele) {
153-
if (!isElement(ele))
154-
return;
155-
ele.textContent = text;
156-
});
157-
}
158-
;
159-
fn.text = text;
160152
function extend() {
161153
var sources = [];
162154
for (var _i = 0; _i < arguments.length; _i++) {
@@ -271,6 +263,17 @@ fn.first = function () {
271263
fn.last = function () {
272264
return this.eq(-1);
273265
};
266+
function text(text) {
267+
if (isUndefined(text)) {
268+
return this.get().map(function (ele) { return isElement(ele) || isText(ele) ? ele.textContent : ''; }).join('');
269+
}
270+
return this.each(function (i, ele) {
271+
if (!isElement(ele))
272+
return;
273+
ele.textContent = text;
274+
});
275+
}
276+
fn.text = text;
274277
// @require core/type_checking.ts
275278
// @require core/variables.ts
276279
function computeStyle(ele, prop, isVariable) {

dist/cash.min.js

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

dist/cash.ts

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ function isElement ( value: unknown ): value is HTMLElement {
176176

177177
}
178178

179+
function isText ( value: unknown ): value is Text {
180+
181+
return !!value && value.nodeType === 3;
182+
183+
}
184+
179185
function isBoolean ( value: unknown ): value is boolean {
180186

181187
return typeof value === 'boolean';
@@ -319,34 +325,6 @@ fn.empty = function ( this: Cash ) {
319325
};
320326

321327

322-
// @require core/cash.ts
323-
// @require core/type_checking.ts
324-
// @require collection/each.ts
325-
326-
interface Cash {
327-
text (): string;
328-
text ( text: string ): this;
329-
}
330-
331-
function text ( this: Cash ): string;
332-
function text ( this: Cash, text: string ): Cash;
333-
function text ( this: Cash, text?: string ) {
334-
335-
if ( isUndefined ( text ) ) return this[0] ? this[0].textContent : '';
336-
337-
return this.each ( ( i, ele ) => {
338-
339-
if ( !isElement ( ele ) ) return;
340-
341-
ele.textContent = text
342-
343-
});
344-
345-
};
346-
347-
fn.text = text;
348-
349-
350328
// @require ./cash.ts
351329
// @require ./type_checking.ts
352330

@@ -650,6 +628,39 @@ fn.last = function ( this: Cash ) {
650628
};
651629

652630

631+
// @require core/cash.ts
632+
// @require core/type_checking.ts
633+
// @require collection/each.ts
634+
// @require collection/get.ts
635+
636+
interface Cash {
637+
text (): string;
638+
text ( text: string ): this;
639+
}
640+
641+
function text ( this: Cash ): string;
642+
function text ( this: Cash, text: string ): Cash;
643+
function text ( this: Cash, text?: string ) {
644+
645+
if ( isUndefined ( text ) ) {
646+
647+
return this.get ().map ( ele => isElement ( ele ) || isText ( ele ) ? ele.textContent : '' ).join ( '' );
648+
649+
}
650+
651+
return this.each ( ( i, ele ) => {
652+
653+
if ( !isElement ( ele ) ) return;
654+
655+
ele.textContent = text;
656+
657+
});
658+
659+
}
660+
661+
fn.text = text;
662+
663+
653664
// @require core/type_checking.ts
654665
// @require core/variables.ts
655666

src/core/type_checking.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ function isElement ( value: unknown ): value is HTMLElement {
4040

4141
}
4242

43+
function isText ( value: unknown ): value is Text {
44+
45+
return !!value && value.nodeType === 3;
46+
47+
}
48+
4349
function isBoolean ( value: unknown ): value is boolean {
4450

4551
return typeof value === 'boolean';

src/manipulation/text.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// @require core/cash.ts
33
// @require core/type_checking.ts
44
// @require collection/each.ts
5+
// @require collection/get.ts
56

67
interface Cash {
78
text (): string;
@@ -12,16 +13,20 @@ function text ( this: Cash ): string;
1213
function text ( this: Cash, text: string ): Cash;
1314
function text ( this: Cash, text?: string ) {
1415

15-
if ( isUndefined ( text ) ) return this[0] ? this[0].textContent : '';
16+
if ( isUndefined ( text ) ) {
17+
18+
return this.get ().map ( ele => isElement ( ele ) || isText ( ele ) ? ele.textContent : '' ).join ( '' );
19+
20+
}
1621

1722
return this.each ( ( i, ele ) => {
1823

1924
if ( !isElement ( ele ) ) return;
2025

21-
ele.textContent = text
26+
ele.textContent = text;
2227

2328
});
2429

25-
};
30+
}
2631

2732
fn.text = text;

test/modules/manipulation.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,14 +545,22 @@ describe ( 'Manipulation', { beforeEach: getFixtureInit ( fixture ) }, function
545545

546546
describe ( '$.fn.text', function ( it ) {
547547

548-
it ( 'gets the text', function ( t ) {
548+
it ( 'gets the text from a single child', function ( t ) {
549549

550550
var anchor = $('.anchor');
551551

552552
t.is ( anchor.text (), 'content' );
553553

554554
});
555555

556+
// it ( 'gets the text from a single child', function ( t ) {
557+
558+
// var value = $('<p>1</p>\n<p>2</p>').text ();
559+
560+
// t.is ( value, '1\n2 ' );
561+
562+
// });
563+
556564
it ( 'sets html strings', function ( t ) {
557565

558566
var anchor = $('.anchor');

0 commit comments

Comments
 (0)