-
Notifications
You must be signed in to change notification settings - Fork 1
/
CobaltElement.cs
1427 lines (1188 loc) · 48.2 KB
/
CobaltElement.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Cobalt.Html;
using Cobalt.Interfaces;
using Cobalt.Css;
using Cobalt.Web;
using System.Web.UI;
using System.Web.Mvc;
namespace Cobalt {
/// <summary>
/// Contains functionality for working with sets of elements
/// </summary>
public class CobaltElement : ICobaltElement {
#region Constructors
/// <summary>
/// Creates an empty CobaltElement
/// </summary>
public CobaltElement()
: this (new HtmlNode[] { }) {
}
/// <summary>
/// Creates a new CobaltElement using the node provided
/// </summary>
public CobaltElement(ICobaltElement element)
: this(element.AsElement()) {
}
/// <summary>
/// Creates a new CobaltElement using the node provided
/// </summary>
public CobaltElement(IEnumerable<ICobaltElement> elements)
: this(elements.SelectMany(element => element.AsElement().Selected)) {
}
/// <summary>
/// Creates a new CobaltElement using the node provided
/// </summary>
public CobaltElement(CobaltElement element)
: this(element.Selected) {
}
/// <summary>
/// Creates a new CobaltElement using the node provided
/// </summary>
public CobaltElement(IEnumerable<CobaltElement> elements)
: this(elements.SelectMany(item => item.Selected)) {
}
/// <summary>
/// Creates a new CobaltElement using the html provided
/// </summary>
public CobaltElement(string html)
: this(HtmlNode.Parse(html)) {
}
/// <summary>
/// Creates a new CobaltElement using the node provided
/// </summary>
public CobaltElement(HtmlNode element)
: this(new HtmlNode[] { element }) {
}
/// <summary>
/// Creates a new CobaltElement using the nodes provided
/// </summary>
public CobaltElement(IEnumerable<HtmlNode> elements) {
this._Selected = elements.ToArray();
}
#endregion
#region Properties
/// <summary>
/// Is this CobaltElement empty of nodes
/// </summary>
public bool IsEmpty {
get { return !this._Selected.Any(); }
}
/// <summary>
/// Returns the currently selected elements
/// </summary>
public virtual IEnumerable<HtmlNode> Selected {
get { return this._Selected; }
set { this._Selected = value; }
}
private IEnumerable<HtmlNode> _Selected;
#endregion
#region Static Creation
/// <summary>
/// Creates a new CobaltElement from the HTML provided
/// </summary>
public static CobaltElement Create(string html) {
return CobaltElement.Create(new CobaltElement(html));
}
/// <summary>
/// Creates a new CobaltElement using the element provided
/// </summary>
public static CobaltElement Create(ICobaltElement element) {
return CobaltElement.Create(new ICobaltElement[] { element });
}
/// <summary>
/// Creates a new CobaltElement using the elements provided
/// </summary>
public static CobaltElement Create(IEnumerable<ICobaltElement> elements) {
return new CobaltElement(elements);
}
#endregion
#region Generation
/// <summary>
/// Clones and returns a new instance of the CobaltElement
/// </summary>
public virtual CobaltElement Clone() {
return new CobaltElement(this.Selected.Select(node => node.Clone()));
}
/// <summary>
/// Clones the matching elements and returns a new CobaltElement instance
/// </summary>
public virtual CobaltElement Clone(string selector) {
return this.Find(selector).Clone();
}
#endregion
#region Finding and Selecting
/// <summary>
/// Uses the CSS selector to select the correct elements
/// </summary>
public virtual CobaltElement Find(string selector) {
return this.Find(selector, null);
}
/// <summary>
/// Uses the CSS selector to select the correct elements
/// </summary>
public virtual CobaltElement Find(string selector, Action<CobaltElement> with) {
return this.FilterAction(selector, this.Selected, with);
}
/// <summary>
/// Adds additional elements to this selection, but doesn't
/// move or append them to the document
/// </summary>
public virtual CobaltElement Include(ICobaltElement element) {
return this.Include(new CobaltElement(element));
}
/// <summary>
/// Adds additional elements to this selection, but doesn't
/// move or append them to the document
/// </summary>
public virtual CobaltElement Include(IEnumerable<ICobaltElement> element) {
return this.Include(new CobaltElement(element));
}
/// <summary>
/// Adds additional elements to this selection, but doesn't
/// move or append them to the document
/// </summary>
public virtual CobaltElement Include(IEnumerable<CobaltElement> element) {
return this.Include(new CobaltElement(element));
}
/// <summary>
/// Adds additional elements to this selection, but doesn't
/// move or append them to the document
/// </summary>
public virtual CobaltElement Include(CobaltElement element) {
return this.Include(element.Selected);
}
/// <summary>
/// Adds additional elements to this selection, but doesn't
/// move or append them to the document
/// </summary>
public virtual CobaltElement Include(HtmlNode element) {
return this.Include(new HtmlNode[] { element });
}
/// <summary>
/// Adds additional elements to this selection, but doesn't
/// move or append them to the document
/// </summary>
public virtual CobaltElement Include(IEnumerable<HtmlNode> elements) {
this.Selected = this.Selected.Union(elements);
return this;
}
/// <summary>
/// Deselects all nodes for this element
/// </summary>
public virtual CobaltElement Deselect() {
this.Selected = new HtmlNode[] { };
return this;
}
/// <summary>
/// Deselects all matching nodes for this element
/// </summary>
public virtual CobaltElement Filter(HtmlNode node) {
return this.Filter(new HtmlNode[] { node });
}
/// <summary>
/// Deselects all matching nodes for this element
/// </summary>
public virtual CobaltElement Filter(CobaltElement element) {
return this.Filter(element.Selected);
}
/// <summary>
/// Deselects all matching nodes for this element
/// </summary>
public virtual CobaltElement Filter(IEnumerable<HtmlNode> deselect) {
this.Selected = this.Selected.Except(deselect);
return this;
}
/// <summary>
/// Removes selected nodes that match the selector
/// </summary>
public virtual CobaltElement Filter(string selector) {
return this.Filter(this.Find(selector));
}
#endregion
#region Working With Elements
/// <summary>
/// Applies an action to each of the selected nodes
/// </summary>
public virtual CobaltElement Each(Action<CobaltElement> each) {
IEnumerable<CobaltElement> selected = this.Selected.Select(item => new CobaltElement(item));
foreach (CobaltElement node in selected) {
each(node);
}
return this;
}
#endregion
#region Styles and Css
/// <summary>
/// Adds a class to the selected elements
/// </summary>
public virtual CobaltElement AddClass(string @class) {
return this.Apply(node => node.AddCss(@class));
}
/// <summary>
/// Adds a class to the selected elements
/// </summary>
public virtual CobaltElement AddClass(params string[] classes) {
return this.Apply(node => node.AddCss(classes));
}
/// <summary>
/// Removes a class from the selected elements
/// </summary>
public virtual CobaltElement RemoveClass(string @class) {
return this.Apply(node => node.RemoveCss(@class));
}
/// <summary>
/// Removes classes from the selected elements
/// </summary>
public virtual CobaltElement RemoveClass(params string[] classes) {
return this.Apply(node => node.RemoveCss(classes));
}
#endregion
#region Manipulation
/// <summary>
/// Performs an action with the selected element
/// </summary>
public virtual CobaltElement With(Action<CobaltElement> with) {
this.FilterAction(string.Empty, this.Selected, with);
return this;
}
/// <summary>
/// Performs an action with the selected element
/// </summary>
public virtual CobaltElement With(string selector, Action<CobaltElement> with) {
this.FilterAction(selector, this.Selected, with);
return this;
}
/// <summary>
/// Appends items to this a different element
/// </summary>
public virtual CobaltElement AppendTo(CobaltElement target) {
//if this is already empty, add an element now
if (target.IsEmpty) {
target.Include(this);
return this;
}
//moves a node into another container
return this.Apply(child => {
HtmlNode node = target.Selected.HtmlNodes().LastOrDefault();
if (node == null) { return; }
node.Append(child);
});
}
/// <summary>
/// Appends items to this Cobalt element
/// </summary>
public virtual CobaltElement AppendTo(IEnumerable<HtmlNode> nodes) {
return this.AppendTo(new CobaltElement(nodes));
}
/// <summary>
/// Appends items to this Cobalt element
/// </summary>
public virtual CobaltElement AppendTo(IEnumerable<CobaltElement> elements) {
return this.AppendTo(new CobaltElement(elements));
}
/// <summary>
/// Appends items to this Cobalt element
/// </summary>
public virtual CobaltElement AppendTo(HtmlNode node) {
return this.AppendTo(new CobaltElement(node));
}
/// <summary>
/// Appends items to this Cobalt element
/// </summary>
public virtual CobaltElement AppendTo<T>(T node) where T : ICobaltElement {
return this.PrependTo(node.AsElement());
}
/// <summary>
/// Appends items to this Cobalt element
/// </summary>
public virtual CobaltElement AppendTo<T>(IEnumerable<T> elements) where T : ICobaltElement {
ICobaltElement target = elements.LastOrDefault();
return target is ICobaltElement ? this.AppendTo(target) : this;
}
/// <summary>
/// Appends this html to another element
/// </summary>
public virtual CobaltElement Append(string html) {
return this.Append(new CobaltElement(html));
}
/// <summary>
/// Appends the html content to another element
/// </summary>
public virtual CobaltElement Append(IEnumerable<string> content) {
return this.Append(content.Select(html => new CobaltElement(html)));
}
/// <summary>
/// Appends a CobaltElement to another element
/// </summary>
public virtual CobaltElement Append(CobaltElement element) {
//if this is already empty, add an element now
if (this.IsEmpty) { return this.Include(element); }
//moves a node into another container
HtmlNode target = this.Selected.HtmlNodes().LastOrDefault();
if (target is HtmlNode) {
target.Append(element.Selected);
}
//return this element
return this;
}
/// <summary>
/// Appends a CobaltElement to another element
/// </summary>
public virtual CobaltElement Append(IEnumerable<CobaltElement> elements) {
return this.Append(new CobaltElement(elements));
}
/// <summary>
/// Appends a CobaltElement to another element
/// </summary>
public virtual CobaltElement Append(IEnumerable<HtmlNode> elements) {
return this.Append(new CobaltElement(elements));
}
/// <summary>
/// Appends an ICobaltElement to another element
/// </summary>
public virtual CobaltElement Append<T>(T element) where T : ICobaltElement {
return this.Append(new CobaltElement(element));
}
/// <summary>
/// Appends a collection of ICobaltElements to another element
/// </summary>
public virtual CobaltElement Append<T>(IEnumerable<T> elements) where T : ICobaltElement {
return this.Append(elements.AsElement());
}
/// <summary>
/// Appends this CobaltElement to another element
/// </summary>
public virtual CobaltElement Append(HtmlNode element) {
return this.Append(new CobaltElement(element));
}
/// <summary>
/// Prepends items to this Cobalt element
/// </summary>
public virtual CobaltElement PrependTo(CobaltElement target) {
//if this is already empty, add an element now
if (target.IsEmpty) {
target.Include(this);
return this;
}
//moves a node into another container
return this.Apply(child => {
HtmlNode node = target.Selected.HtmlNodes().LastOrDefault();
if (node == null) { return; }
node.Prepend(child);
});
}
/// <summary>
/// Prepends items to this Cobalt element
/// </summary>
public virtual CobaltElement PrependTo(IEnumerable<HtmlNode> nodes) {
return this.PrependTo(new CobaltElement(nodes));
}
/// <summary>
/// Prepends items to this Cobalt element
/// </summary>
public virtual CobaltElement PrependTo(IEnumerable<CobaltElement> elements) {
return this.PrependTo(new CobaltElement(elements));
}
/// <summary>
/// Prepends items to this Cobalt element
/// </summary>
public virtual CobaltElement PrependTo<T>(T node) where T : ICobaltElement {
return this.PrependTo(node.AsElement());
}
/// <summary>
/// Prepends items to this Cobalt element
/// </summary>
public virtual CobaltElement PrependTo<T>(IEnumerable<T> elements) where T : ICobaltElement {
ICobaltElement target = elements.LastOrDefault();
return target is ICobaltElement ? this.PrependTo(target) : this;
}
/// <summary>
/// Prepends items to this Cobalt element
/// </summary>
public virtual CobaltElement PrependTo(HtmlNode node) {
return this.PrependTo(new CobaltElement(node));
}
/// <summary>
/// Prepends this html to another element
/// </summary>
public virtual CobaltElement Prepend(string html) {
return this.Prepend(new CobaltElement(html));
}
/// <summary>
/// Prepends the html content to another element
/// </summary>
public virtual CobaltElement Prepend(IEnumerable<string> content) {
return this.Prepend(content.Select(html => new CobaltElement(html)));
}
/// <summary>
/// Prepends this CobaltElement to another element
/// </summary>
public virtual CobaltElement Prepend(CobaltElement element) {
//if this is already empty, add an element now
if (this.IsEmpty) { return this.Include(element); }
//moves a node into another container
HtmlNode target = this.Selected.HtmlNodes().LastOrDefault();
if (target is HtmlNode) {
target.Prepend(element.Selected);
}
//return this element
return this;
}
/// <summary>
/// Prepends this CobaltElement to another element
/// </summary>
public virtual CobaltElement Prepend(IEnumerable<CobaltElement> elements) {
return this.Prepend(new CobaltElement(elements));
}
/// <summary>
/// Prepends a CobaltElement to another element
/// </summary>
public virtual CobaltElement Prepend(IEnumerable<HtmlNode> elements) {
return this.Prepend(new CobaltElement(elements));
}
/// <summary>
/// Prepends a CobaltElement to another element
/// </summary>
public virtual CobaltElement Prepend(HtmlNode element) {
return this.Prepend(new CobaltElement(element));
}
/// <summary>
/// Prepends an ICobaltElement to another element
/// </summary>
public virtual CobaltElement Prepend<T>(T element) where T : ICobaltElement {
return this.Prepend(new CobaltElement(element));
}
/// <summary>
/// Prepends a collection of ICobaltElements to another element
/// </summary>
public virtual CobaltElement Prepend<T>(IEnumerable<T> elements) where T : ICobaltElement {
return this.Prepend(elements.AsElement());
}
#endregion
#region Positional Insertion
/* Not sure these are should be included since they could be
* inserted into the void...
* After(IEnumerable<HtmlNode> elements)
* After(HtmlNode elements)
* Before(IEnumerable<HtmlNode> elements)
* Before(HtmlNode elements)
*/
/// <summary>
/// Inserts the current CobaltElement after the target
/// </summary>
public virtual CobaltElement InsertAfter(ICobaltElement target) {
return this.InsertAfter(new CobaltElement(target));
}
/// <summary>
/// Inserts the current CobaltElement after the last target
/// </summary>
public virtual CobaltElement InsertAfter<T>(IEnumerable<T> targets) where T : ICobaltElement {
return this.InsertAfter(targets.AsElement());
}
/// <summary>
/// Inserts the current CobaltElement after the last target
/// </summary>
public virtual CobaltElement InsertAfter(IEnumerable<CobaltElement> targets) {
return this.InsertAfter(new CobaltElement(targets));
}
/// <summary>
/// Inserts the current CobaltElement after the target
/// </summary>
public virtual CobaltElement InsertAfter(HtmlNode target) {
return this.InsertAfter(new CobaltElement(target));
}
/// <summary>
/// Inserts the current CobaltElement after the last target
/// </summary>
public virtual CobaltElement InsertAfter(IEnumerable<HtmlNode> targets) {
return this.InsertAfter(new CobaltElement(targets));
}
/// <summary>
/// Inserts the current CobaltElement after the target
/// </summary>
public virtual CobaltElement InsertAfter(CobaltElement target) {
//get the last target since it is the actual one to attach to
HtmlNode element = target.Selected.HtmlNodes().LastOrDefault()
?? HtmlNode.Parse(string.Empty).FirstOrDefault();
//move the elements
return this.Apply(node => node.InsertAfter(element));
}
/// <summary>
/// Inserts the current CobaltElement after the last target
/// </summary>
public virtual CobaltElement After(ICobaltElement element) {
return this.After(new CobaltElement(element));
}
/// <summary>
/// Inserts the current CobaltElement after the last target
/// </summary>
public virtual CobaltElement After<T>(IEnumerable<T> elements) where T : ICobaltElement {
return this.After(elements.AsElement());
}
/// <summary>
/// Inserts the provided element after the current CobaltElement
/// </summary>
public virtual CobaltElement After(IEnumerable<CobaltElement> elements) {
return this.After(new CobaltElement(elements));
}
/// <summary>
/// Inserts the provided element after the current CobaltElement
/// </summary>
public virtual CobaltElement After(CobaltElement element) {
//get the last target since it is the actual one to attach to
HtmlNode target = this.Selected.HtmlNodes().LastOrDefault()
?? HtmlNode.Parse(string.Empty).FirstOrDefault();
//move the elements
element.Apply(node => node.InsertAfter(target));
return this;
}
/// <summary>
/// Inserts the current CobaltElement before the target
/// </summary>
public virtual CobaltElement InsertBefore(ICobaltElement target) {
return this.InsertBefore(new CobaltElement(target));
}
/// <summary>
/// Inserts the current CobaltElement before the last target
/// </summary>
public virtual CobaltElement InsertBefore<T>(IEnumerable<T> targets) where T : ICobaltElement {
return this.InsertBefore(targets.AsElement());
}
/// <summary>
/// Inserts the current CobaltElement before the target
/// </summary>
public virtual CobaltElement InsertBefore(HtmlNode target) {
return this.InsertBefore(new CobaltElement(target));
}
/// <summary>
/// Inserts the current CobaltElement before the last target
/// </summary>
public virtual CobaltElement InsertBefore(IEnumerable<HtmlNode> targets) {
return this.InsertBefore(new CobaltElement(targets));
}
/// <summary>
/// Inserts the current CobaltElement before the last target
/// </summary>
public virtual CobaltElement InsertBefore(IEnumerable<CobaltElement> targets) {
return this.InsertBefore(new CobaltElement(targets));
}
/// <summary>
/// Inserts the current CobaltElement before the target
/// </summary>
public virtual CobaltElement InsertBefore(CobaltElement target) {
//get the last target since it is the actual one to attach to
HtmlNode element = target.Selected.HtmlNodes().LastOrDefault()
?? HtmlNode.Parse(string.Empty).FirstOrDefault();
//move the elements
return this.Apply(node => node.InsertBefore(element));
}
/// <summary>
/// Inserts the current CobaltElement before the last target
/// </summary>
public virtual CobaltElement Before(ICobaltElement element) {
return this.Before(new CobaltElement(element));
}
/// <summary>
/// Inserts the current CobaltElement before the last target
/// </summary>
public virtual CobaltElement Before<T>(IEnumerable<T> elements) where T : ICobaltElement {
return this.Before(elements.AsElement());
}
/// <summary>
/// Inserts the provided element before the current CobaltElement
/// </summary>
public virtual CobaltElement Before(IEnumerable<CobaltElement> elements) {
return this.Before(new CobaltElement(elements));
}
/// <summary>
/// Inserts the provided element before the current CobaltElement
/// </summary>
public virtual CobaltElement Before(CobaltElement element) {
//get the last target since it is the actual one to attach to
HtmlNode target = this.Selected.HtmlNodes().LastOrDefault()
?? HtmlNode.Parse(string.Empty).FirstOrDefault();
//move the elements
element.Apply(node => node.InsertBefore(target));
return this;
}
#endregion
#region Changing Elements
/// <summary>
/// Removes all of the selected items
/// </summary>
public virtual CobaltElement Remove() {
return this.Apply(node => node.Remove());
}
/// <summary>
/// Removes all of the selected items
/// </summary>
public virtual CobaltElement Remove(string selector) {
return this.Apply(selector, node => node.Remove());
}
/// <summary>
/// Clears all attributes and children from the selected nodes
/// </summary>
public virtual CobaltElement ClearAll() {
return this.Empty().ClearAttributes();
}
/// <summary>
/// Removes all content from within the selected elements
/// </summary>
public virtual CobaltElement Empty() {
return this.Apply(node => node.Children.Each(child => child.Remove()));
}
/// <summary>
/// Wraps each of the selected elements with the provided CobaltElement
/// </summary>
public virtual CobaltElement Wrap(CobaltElement element) {
//include the new element
element.InsertBefore(this.Selected);
element.Append(this.Selected);
//update the selected items
this.Selected = element.Selected;
return this;
}
/// <summary>
/// Wraps each of the selected elements with the provided CobaltElement
/// </summary>
public virtual CobaltElement Wrap(ICobaltElement element) {
return this.Wrap(new CobaltElement(element));
}
/// <summary>
/// Wraps each of the selected elements with the provided html node
/// </summary>
public virtual CobaltElement Wrap(HtmlNode node) {
return this.Wrap(new CobaltElement(node));
}
/// <summary>
/// Wraps each of the selected elements with the provided html
/// </summary>
public virtual CobaltElement Wrap(string html) {
return this.Wrap(new CobaltElement(html));
}
/// <summary>
/// Wraps the entire CobaltElement with the provided CobaltElement
/// </summary>
public virtual CobaltElement WrapAll(CobaltElement element) {
if (!this.Selected.Any()) { return this; }
element.Append(this.Selected);
this.Selected = element.Selected;
return this;
}
/// <summary>
/// Wraps the entire CobaltElement with the provided CobaltElement
/// </summary>
public virtual CobaltElement WrapAll(ICobaltElement element) {
return this.WrapAll(new CobaltElement(element));
}
/// <summary>
/// Wraps the entire CobaltElement with the provided html node
/// </summary>
public virtual CobaltElement WrapAll(HtmlNode node) {
return this.WrapAll(new CobaltElement(node));
}
/// <summary>
/// Wraps the entire CobaltElement with the provided html
/// </summary>
public virtual CobaltElement WrapAll(string html) {
return this.WrapAll(new CobaltElement(html));
}
/// <summary>
/// Wraps each of the child elements with the provided CobaltElement
/// </summary>
public virtual CobaltElement WrapInner(CobaltElement element) {
IEnumerable<HtmlNode> nodes = this.Children().Selected.HtmlNodes();
CobaltElement wrapped = new CobaltElement(nodes.Select(child => element.Clone().InsertBefore(child).Append(child)));
this.Selected = wrapped.Selected.Except(nodes);
return this;
}
/// <summary>
/// Wraps each of the child elements with the provided CobaltElement
/// </summary>
public virtual CobaltElement WrapInner(ICobaltElement element) {
return this.WrapInner(new CobaltElement(element));
}
/// <summary>
/// Wraps each of the child elements with the provided html node
/// </summary>
public virtual CobaltElement WrapInner(HtmlNode node) {
return this.WrapInner(new CobaltElement(node));
}
/// <summary>
/// Wraps each of the child elements with the provided html
/// </summary>
public virtual CobaltElement WrapInner(string html) {
return this.WrapInner(new CobaltElement(html));
}
/// <summary>
/// Detatches the selected elements from the document (but does not
/// remove them completely)
/// </summary>
public virtual CobaltElement Detach() {
return new CobaltElement(this.Selected.Each(node => node.Detatch()));
}
#endregion
#region Text and Html Content
/// <summary>
/// Sets the text value for all selected elements
/// </summary>
public virtual CobaltElement Text(string value) {
return this.Apply(node => node.InnerHtml = HttpUtility.HtmlEncode(value ?? string.Empty));
}
/// <summary>
/// Sets the text value for all selected elements
/// </summary>
public virtual CobaltElement Text(object value) {
return this.Text((value ?? string.Empty).ToString());
}
/// <summary>
/// Sets the text value for all selected elements
/// </summary>
public virtual CobaltElement Text(string value, params object[] args) {
return this.Text(string.Format(value ?? string.Empty, args));
}
/// <summary>
/// Gets the text value for all selected elements
/// </summary>
public virtual string Text() {
return string.Join(string.Empty, this.Selected.Select(node => node.InnerText).ToArray());
}
/// <summary>
/// Sets the html value for all selected elements
/// </summary>
public virtual CobaltElement Html(object value) {
return this.Html((value ?? string.Empty).ToString());
}
/// <summary>
/// Sets the html value for all selected elements
/// </summary>
public virtual CobaltElement Html(string value) {
return this.Apply(node => node.InnerHtml = value);
}
/// <summary>
/// Sets the html value for all selected elements
/// </summary>
public virtual CobaltElement Html(string value, params object[] args) {
return this.Value(string.Format(value ?? string.Empty, args));
}
/// <summary>
/// Gets the text value for all selected elements
/// </summary>
public virtual string Html() {
return string.Join(string.Empty, this.Selected.Select(node => node.InnerHtml).ToArray());
}
/// <summary>
/// Sets the value for all selected elements
/// </summary>
public virtual CobaltElement Value(object value) {
return this.Value((value ?? string.Empty).ToString());
}
/// <summary>
/// Sets the value for all selected elements
/// </summary>
public virtual CobaltElement Value(string value, params object[] args) {
return this.Value(string.Format(value ?? string.Empty, args));
}
/// <summary>
/// Sets the value for all selected elements
/// </summary>
public virtual CobaltElement Value(string value) {
return this.Apply(node => node.SetValue(value));
}
/// <summary>
/// Gets the value for all selected elements
/// </summary>
public virtual string Value() {
return string.Join(string.Empty, this.Selected.Select(node => node.GetValue()).ToArray());
}
#endregion
#region Attributes and CSS
/// <summary>
/// Sets the value of attributes for the selected elements
/// </summary>
public virtual CobaltElement Attr(object attributes) {
CobaltAttributePairs created = CobaltAttributePairs.CreateSetFromObject(attributes);
return this.Attr(created);
}
/// <summary>
/// Sets the value of attributes for the selected elements
/// </summary>
public virtual CobaltElement Attr(CobaltAttributePairs attributes) {
return this.Apply(node => {
foreach (string key in attributes.Keys) {
//format the values
string name = (key ?? string.Empty).Trim();
object value = attributes[name];
//update the attributes
node[name] = null;
if (value == null) { continue; }
node[name] = value;
}
});
}
/// <summary>
/// Sets the value of attributes for the selected elements
/// </summary>
public virtual CobaltElement Attr(string name, object value) {
return this.Attr(new CobaltAttributePairs {
{ name, value }
});
}