aboutsummaryrefslogblamecommitdiffstats
path: root/e-util/e-html-editor-selection.c
blob: d3294899223a9c71e5f01134c9808bcbd3f8ce38 (plain) (tree)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                       































                                                                                          





































































































                                                                                          


















                                                                        
           



















                                                                                   
                                              
















































































































































































































































                                                                                              
                                                      












                                                                                         

                              

































                                                                                                 
                                                                                                       























                                                                                                         









                                                                                                                 


























































                                                                                     

                                                        








                                                                   

                                                              

 

                                                       
 
                        
                                



                                                        
                                       
                                                                   




                                                                

                                                                               
 
                                                              
                                                       


                                                                       
                                      
 
                                   

         





                                                                   



























                                                                                           














                                                                                          






                                                                     
                                                







                                                                                  





























                                                                                  
                                                  
                                                          

                                                            


                                                                     
                                        

                                       

                                     


                                                                                  
                                                                                 


                               
                                                                                  
 


                                                                       
                                                                      







                                                                                 











                                                                                          













                                                                         

                                                                                             
 

                                                                                        
 


                                                                                               
 

                                                                                    
 


                                        




                                                    




                                                                    
                                                       









                                                                                      





























                                                                                  
                                                  



                                                                               

                                                                          

                                          
                                
 






                                                                                   












                                                                                                  
                                                          











                                                                     
                                              






                                                                       

                                                                          
 




















                                                                                                  
                                                                 

                                    















                                                                                              







                                                         
                                                    
                                                    




















                                                                               

                                                          
 

                                                          
 

                                                        









































































                                                                                              

                                                   








































































                                                                                                                     

























































                                                                              































                                                                                        
                                                    





                                                                                      

                                                    











































































































































































































































                                                                                        

















                                                                                       





                                                                
                                                                     

 












                                                                       
                                  
                              






                                                                             








                                                                                  
 


                                                                               
 








                                                                              






























                                                                                         



















































                                                                                         



























                                                                               








                                                                
                              
                                                                            


                                                                        





                                                                        












                                                                                  
 
























                                                                                 
                                           
                                                          


                                                                  

                       
                                                        
                                                                          
                                          
                                        
 
                                                                      
 



                                                                             
 

                                                                       
 








                                                                
 




                                                                                         

                                                           
                                 
                         
 
                                                                                   
 








                                                                                              
                                                                               
 


                                                
 
                                                 

                                                        
                                                                                
 

                                                                                          
 









                                                                                  
 


                                                                                              
 
                                                                                          
 




                                                
         
     
                                                    
                                                    


























                                                                  
                                                                        
                                                                             
                                 









                                                                      
                                                 















                                                                                     

                                



                                                                                   

                                                                          
                                                                                         


                                                                                    
                                             
                                                                                     
                                              

















                                                                                            

































































































                                                                                                      








                                                                  
                              
                                                                            


                                                                        





                                                                        
                                                                             
 
                                                 
 



                                                                      
 



























                                                                                  
 
                                           
                                                          


                                                                  
 
                       


                                          
 
                                                                      
 



                                                                             
 

                                                                       
 

                                                                
                                                            




                                                   

                 





                                                                                         

                                                 
                         
 
                                                                    
 


                                                
 

                                                        
 
                                                                                
 









                                                                                  
                         
 
                                                                               
 



                                                
         
     
                                                    
                                                    












































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                    






                                                                        
                                                                            

























                                                                                
                                                      
                                                                           




                                                                    

                                                                          



                              















                                                                                   




































































                                                                                   









                                                                


















































































































































































































































































































                                                                                               
                                      

                                         


                                                                                 


                                                                           
                                                                     



                                                                  
                                                                      



                                                          
                                                                       










                                                                               
                                                                               


                                          
                                                                            












































































































































































































































































                                                                                                        
                                                      































                                                                                     



                                                                                        









































































































































































                                                                                                      

                                                                                  
                                                                               







                                                                                               
































                                                                                                       




                                                              


                                                                           
                                                                    































































































































                                                                                                                





                                                                        





































































































































































































































































































                                                                                                         
                                                                













































                                                                                     
                                                        







                                                                        































                                                                                                 




























                                                                                 
                                                









                                                                        
                              




























                                                                               

                                                          
 










                                                                                    
                                                                      
                                                                     

                                                         
                                                                            
                                      

                                        









                                                                                            
                                                                                





                                                                                    
                                                                                   













                                                                                            
                                                       
                         

                 



                                                                 
                                  
                                                                     

















                                                                                   









                                                                               


                                                                             













                                                                                    
 
                         


                                                                           

                               









                                                                                            











                                                                                   







                                                                           












                                                                                   














                                                                                                                  
 
         






























                                                                                 
                              



































                                                                           













































































                                                                                 




                                                                   


                                                                        

                       
 


















                                                                                     


                                                                        
/*
 * e-html-editor-selection.c
 *
 * Copyright (C) 2012 Dan Vrátil <dvratil@redhat.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-html-editor-selection.h"
#include "e-html-editor-view.h"
#include "e-html-editor.h"
#include "e-html-editor-utils.h"

#include <e-util/e-util.h>

#include <webkit/webkit.h>
#include <webkit/webkitdom.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define E_HTML_EDITOR_SELECTION_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_HTML_EDITOR_SELECTION, EHTMLEditorSelectionPrivate))

#define UNICODE_ZERO_WIDTH_SPACE "\xe2\x80\x8b"
#define UNICODE_NBSP "\xc2\xa0"

#define SPACES_PER_INDENTATION 4
#define SPACES_PER_LIST_LEVEL 8

/**
 * EHTMLEditorSelection
 *
 * The #EHTMLEditorSelection object represents current position of the cursor
 * with the editor or current text selection within the editor. To obtain
 * valid #EHTMLEditorSelection, call e_html_editor_view_get_selection().
 */

struct _EHTMLEditorSelectionPrivate {

    GWeakRef html_editor_view;
    gulong selection_changed_handler_id;

    gchar *text;

    gboolean is_bold;
    gboolean is_italic;
    gboolean is_underline;
    gboolean is_monospaced;
    gboolean is_strikethrough;

    gchar *background_color;
    gchar *font_color;
    gchar *font_family;

    gulong selection_offset;

    gint word_wrap_length;
    guint font_size;

    EHTMLEditorSelectionAlignment alignment;
};

enum {
    PROP_0,
    PROP_ALIGNMENT,
    PROP_BACKGROUND_COLOR,
    PROP_BLOCK_FORMAT,
    PROP_BOLD,
    PROP_HTML_EDITOR_VIEW,
    PROP_FONT_COLOR,
    PROP_FONT_NAME,
    PROP_FONT_SIZE,
    PROP_INDENTED,
    PROP_ITALIC,
    PROP_MONOSPACED,
    PROP_STRIKETHROUGH,
    PROP_SUBSCRIPT,
    PROP_SUPERSCRIPT,
    PROP_TEXT,
    PROP_UNDERLINE
};

static const GdkRGBA black = { 0 };

G_DEFINE_TYPE (
    EHTMLEditorSelection,
    e_html_editor_selection,
    G_TYPE_OBJECT
);

static WebKitDOMRange *
html_editor_selection_get_current_range (EHTMLEditorSelection *selection)
{
    EHTMLEditorView *view;
    WebKitWebView *web_view;
    WebKitDOMDocument *document;
    WebKitDOMDOMWindow *window;
    WebKitDOMDOMSelection *dom_selection;
    WebKitDOMRange *range = NULL;

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_val_if_fail (view != NULL, NULL);

    web_view = WEBKIT_WEB_VIEW (view);

    document = webkit_web_view_get_dom_document (web_view);
    window = webkit_dom_document_get_default_view (document);
    if (!window)
        goto exit;

    dom_selection = webkit_dom_dom_window_get_selection (window);
    if (!WEBKIT_DOM_IS_DOM_SELECTION (dom_selection))
        goto exit;

    if (webkit_dom_dom_selection_get_range_count (dom_selection) < 1)
        goto exit;

    range = webkit_dom_dom_selection_get_range_at (dom_selection, 0, NULL);

 exit:
    g_object_unref (view);

    return range;
}

static gboolean
get_has_style (EHTMLEditorSelection *selection,
               const gchar *style_tag)
{
    WebKitDOMNode *node;
    WebKitDOMElement *element;
    WebKitDOMRange *range;
    gboolean result;
    gint tag_len;

    range = html_editor_selection_get_current_range (selection);
    if (!range)
        return FALSE;

    node = webkit_dom_range_get_start_container (range, NULL);
    if (WEBKIT_DOM_IS_ELEMENT (node))
        element = WEBKIT_DOM_ELEMENT (node);
    else
        element = webkit_dom_node_get_parent_element (node);

    tag_len = strlen (style_tag);
    result = FALSE;
    while (!result && element) {
        gchar *element_tag;
        gboolean accept_citation = FALSE;

        element_tag = webkit_dom_element_get_tag_name (element);

        if (g_ascii_strncasecmp (style_tag, "citation", 8) == 0) {
            accept_citation = TRUE;
            result = ((strlen (element_tag) == 10 /* strlen ("blockquote") */) &&
                (g_ascii_strncasecmp (element_tag, "blockquote", 10) == 0));
            if (element_has_class (element, "-x-evo-indented"))
                result = FALSE;
        } else {
            result = ((tag_len == strlen (element_tag)) &&
                (g_ascii_strncasecmp (element_tag, style_tag, tag_len) == 0));
        }

        /* Special case: <blockquote type=cite> marks quotation, while
         * just <blockquote> is used for indentation. If the <blockquote>
         * has type=cite, then ignore it unless style_tag is "citation" */
        if (result && g_ascii_strncasecmp (element_tag, "blockquote", 10) == 0) {
            if (webkit_dom_element_has_attribute (element, "type")) {
                gchar *type;
                type = webkit_dom_element_get_attribute (element, "type");
                if (!accept_citation && (g_ascii_strncasecmp (type, "cite", 4) == 0)) {
                    result = FALSE;
                }
                g_free (type);
            } else {
                if (accept_citation)
                    result = FALSE;
            }
        }

        g_free (element_tag);

        if (result)
            break;

        element = webkit_dom_node_get_parent_element (
            WEBKIT_DOM_NODE (element));
    }

    return result;
}

static gchar *
get_font_property (EHTMLEditorSelection *selection,
                   const gchar *font_property)
{
    WebKitDOMRange *range;
    WebKitDOMNode *node;
    WebKitDOMElement *element;
    gchar *value;

    range = html_editor_selection_get_current_range (selection);
    if (!range)
        return NULL;

    node = webkit_dom_range_get_common_ancestor_container (range, NULL);
    element = e_html_editor_dom_node_find_parent_element (node, "FONT");
    if (!element)
        return NULL;

    g_object_get (G_OBJECT (element), font_property, &value, NULL);

    return value;
}

static void
html_editor_selection_selection_changed_cb (WebKitWebView *webview,
                                            EHTMLEditorSelection *selection)
{
    g_object_freeze_notify (G_OBJECT (selection));

    g_object_notify (G_OBJECT (selection), "alignment");
    g_object_notify (G_OBJECT (selection), "background-color");
    g_object_notify (G_OBJECT (selection), "bold");
    g_object_notify (G_OBJECT (selection), "font-name");
    g_object_notify (G_OBJECT (selection), "font-size");
    g_object_notify (G_OBJECT (selection), "font-color");
    g_object_notify (G_OBJECT (selection), "block-format");
    g_object_notify (G_OBJECT (selection), "indented");
    g_object_notify (G_OBJECT (selection), "italic");
    g_object_notify (G_OBJECT (selection), "monospaced");
    g_object_notify (G_OBJECT (selection), "strikethrough");
    g_object_notify (G_OBJECT (selection), "subscript");
    g_object_notify (G_OBJECT (selection), "superscript");
    g_object_notify (G_OBJECT (selection), "text");
    g_object_notify (G_OBJECT (selection), "underline");

    g_object_thaw_notify (G_OBJECT (selection));
}

void
e_html_editor_selection_block_selection_changed (EHTMLEditorSelection *selection)
{
    EHTMLEditorView *view;

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_signal_handlers_block_by_func (
        view, html_editor_selection_selection_changed_cb, selection);
    g_object_unref (view);
}

void
e_html_editor_selection_unblock_selection_changed (EHTMLEditorSelection *selection)
{
    EHTMLEditorView *view;

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_signal_handlers_unblock_by_func (
        view, html_editor_selection_selection_changed_cb, selection);
    g_object_unref (view);
}

static void
html_editor_selection_set_html_editor_view (EHTMLEditorSelection *selection,
                                            EHTMLEditorView *view)
{
    gulong handler_id;

    g_return_if_fail (E_IS_HTML_EDITOR_VIEW (view));

    g_weak_ref_set (&selection->priv->html_editor_view, view);

    handler_id = g_signal_connect (
        view, "selection-changed",
        G_CALLBACK (html_editor_selection_selection_changed_cb),
        selection);

    selection->priv->selection_changed_handler_id = handler_id;
}

static void
html_editor_selection_get_property (GObject *object,
                                    guint property_id,
                                    GValue *value,
                                    GParamSpec *pspec)
{
    GdkRGBA rgba = { 0 };

    switch (property_id) {
        case PROP_ALIGNMENT:
            g_value_set_int (
                value,
                e_html_editor_selection_get_alignment (
                E_HTML_EDITOR_SELECTION (object)));
            return;

        case PROP_BACKGROUND_COLOR:
            g_value_set_string (
                value,
                e_html_editor_selection_get_background_color (
                E_HTML_EDITOR_SELECTION (object)));
            return;

        case PROP_BLOCK_FORMAT:
            g_value_set_int (
                value,
                e_html_editor_selection_get_block_format (
                E_HTML_EDITOR_SELECTION (object)));
            return;

        case PROP_BOLD:
            g_value_set_boolean (
                value,
                e_html_editor_selection_is_bold (
                E_HTML_EDITOR_SELECTION (object)));
            return;

        case PROP_HTML_EDITOR_VIEW:
            g_value_take_object (
                value,
                e_html_editor_selection_ref_html_editor_view (
                E_HTML_EDITOR_SELECTION (object)));
            return;

        case PROP_FONT_COLOR:
            e_html_editor_selection_get_font_color (
                E_HTML_EDITOR_SELECTION (object), &rgba);
            g_value_set_boxed (value, &rgba);
            return;

        case PROP_FONT_NAME:
            g_value_set_string (
                value,
                e_html_editor_selection_get_font_name (
                E_HTML_EDITOR_SELECTION (object)));
            return;

        case PROP_FONT_SIZE:
            g_value_set_int (
                value,
                e_html_editor_selection_get_font_size (
                E_HTML_EDITOR_SELECTION (object)));
            return;

        case PROP_INDENTED:
            g_value_set_boolean (
                value,
                e_html_editor_selection_is_indented (
                E_HTML_EDITOR_SELECTION (object)));
            return;

        case PROP_ITALIC:
            g_value_set_boolean (
                value,
                e_html_editor_selection_is_italic (
                E_HTML_EDITOR_SELECTION (object)));
            return;

        case PROP_MONOSPACED:
            g_value_set_boolean (
                value,
                e_html_editor_selection_is_monospaced (
                E_HTML_EDITOR_SELECTION (object)));
            return;

        case PROP_STRIKETHROUGH:
            g_value_set_boolean (
                value,
                e_html_editor_selection_is_strikethrough (
                E_HTML_EDITOR_SELECTION (object)));
            return;

        case PROP_SUBSCRIPT:
            g_value_set_boolean (
                value,
                e_html_editor_selection_is_subscript (
                E_HTML_EDITOR_SELECTION (object)));
            return;

        case PROP_SUPERSCRIPT:
            g_value_set_boolean (
                value,
                e_html_editor_selection_is_superscript (
                E_HTML_EDITOR_SELECTION (object)));
            return;

        case PROP_TEXT:
            g_value_set_string (
                value,
                e_html_editor_selection_get_string (
                E_HTML_EDITOR_SELECTION (object)));
            break;

        case PROP_UNDERLINE:
            g_value_set_boolean (
                value,
                e_html_editor_selection_is_underline (
                E_HTML_EDITOR_SELECTION (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
html_editor_selection_set_property (GObject *object,
                                    guint property_id,
                                    const GValue *value,
                                    GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_ALIGNMENT:
            e_html_editor_selection_set_alignment (
                E_HTML_EDITOR_SELECTION (object),
                g_value_get_int (value));
            return;

        case PROP_BACKGROUND_COLOR:
            e_html_editor_selection_set_background_color (
                E_HTML_EDITOR_SELECTION (object),
                g_value_get_string (value));
            return;

        case PROP_BOLD:
            e_html_editor_selection_set_bold (
                E_HTML_EDITOR_SELECTION (object),
                g_value_get_boolean (value));
            return;

        case PROP_HTML_EDITOR_VIEW:
            html_editor_selection_set_html_editor_view (
                E_HTML_EDITOR_SELECTION (object),
                g_value_get_object (value));
            return;

        case PROP_FONT_COLOR:
            e_html_editor_selection_set_font_color (
                E_HTML_EDITOR_SELECTION (object),
                g_value_get_boxed (value));
            return;

        case PROP_BLOCK_FORMAT:
            e_html_editor_selection_set_block_format (
                E_HTML_EDITOR_SELECTION (object),
                g_value_get_int (value));
            return;

        case PROP_FONT_NAME:
            e_html_editor_selection_set_font_name (
                E_HTML_EDITOR_SELECTION (object),
                g_value_get_string (value));
            return;

        case PROP_FONT_SIZE:
            e_html_editor_selection_set_font_size (
                E_HTML_EDITOR_SELECTION (object),
                g_value_get_int (value));
            return;

        case PROP_ITALIC:
            e_html_editor_selection_set_italic (
                E_HTML_EDITOR_SELECTION (object),
                g_value_get_boolean (value));
            return;

        case PROP_MONOSPACED:
            e_html_editor_selection_set_monospaced (
                E_HTML_EDITOR_SELECTION (object),
                g_value_get_boolean (value));
            return;

        case PROP_STRIKETHROUGH:
            e_html_editor_selection_set_strikethrough (
                E_HTML_EDITOR_SELECTION (object),
                g_value_get_boolean (value));
            return;

        case PROP_SUBSCRIPT:
            e_html_editor_selection_set_subscript (
                E_HTML_EDITOR_SELECTION (object),
                g_value_get_boolean (value));
            return;

        case PROP_SUPERSCRIPT:
            e_html_editor_selection_set_superscript (
                E_HTML_EDITOR_SELECTION (object),
                g_value_get_boolean (value));
            return;

        case PROP_UNDERLINE:
            e_html_editor_selection_set_underline (
                E_HTML_EDITOR_SELECTION (object),
                g_value_get_boolean (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
html_editor_selection_dispose (GObject *object)
{
    EHTMLEditorSelectionPrivate *priv;
    EHTMLEditorView *view;

    priv = E_HTML_EDITOR_SELECTION_GET_PRIVATE (object);

    view = g_weak_ref_get (&priv->html_editor_view);
    if (view != NULL) {
        g_signal_handler_disconnect (
            view, priv->selection_changed_handler_id);
        priv->selection_changed_handler_id = 0;
        g_object_unref (view);
    }

    g_weak_ref_set (&priv->html_editor_view, NULL);

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (e_html_editor_selection_parent_class)->dispose (object);
}

static void
html_editor_selection_finalize (GObject *object)
{
    EHTMLEditorSelection *selection = E_HTML_EDITOR_SELECTION (object);

    g_free (selection->priv->text);
    g_free (selection->priv->background_color);
    g_free (selection->priv->font_color);
    g_free (selection->priv->font_family);

    /* Chain up to parent's finalize() method. */
    G_OBJECT_CLASS (e_html_editor_selection_parent_class)->finalize (object);
}

static void
e_html_editor_selection_class_init (EHTMLEditorSelectionClass *class)
{
    GObjectClass *object_class;

    g_type_class_add_private (class, sizeof (EHTMLEditorSelectionPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->get_property = html_editor_selection_get_property;
    object_class->set_property = html_editor_selection_set_property;
    object_class->dispose = html_editor_selection_dispose;
    object_class->finalize = html_editor_selection_finalize;

    /**
     * EHTMLEditorSelectionalignment
     *
     * Holds alignment of current paragraph.
     */
    /* FIXME: Convert the enum to a proper type */
    g_object_class_install_property (
        object_class,
        PROP_ALIGNMENT,
        g_param_spec_int (
            "alignment",
            NULL,
            NULL,
            E_HTML_EDITOR_SELECTION_ALIGNMENT_LEFT,
            E_HTML_EDITOR_SELECTION_ALIGNMENT_RIGHT,
            E_HTML_EDITOR_SELECTION_ALIGNMENT_LEFT,
            G_PARAM_READWRITE));

    /**
     * EHTMLEditorSelectionbackground-color
     *
     * Holds background color of current selection or at current cursor
     * position.
     */
    g_object_class_install_property (
        object_class,
        PROP_BACKGROUND_COLOR,
        g_param_spec_string (
            "background-color",
            NULL,
            NULL,
            NULL,
            G_PARAM_READWRITE));

    /**
     * EHTMLEditorSelectionblock-format
     *
     * Holds block format of current paragraph. See
     * #EHTMLEditorSelectionBlockFormat for valid values.
     */
    /* FIXME Convert the EHTMLEditorSelectionBlockFormat
     *       enum to a proper type. */
    g_object_class_install_property (
        object_class,
        PROP_BLOCK_FORMAT,
        g_param_spec_int (
            "block-format",
            NULL,
            NULL,
            0,
            G_MAXINT,
            0,
            G_PARAM_READWRITE |
            G_PARAM_STATIC_STRINGS));

    /**
     * EHTMLEditorSelectionbold
     *
     * Holds whether current selection or text at current cursor position
     * is bold.
     */
    g_object_class_install_property (
        object_class,
        PROP_BOLD,
        g_param_spec_boolean (
            "bold",
            NULL,
            NULL,
            FALSE,
            G_PARAM_READWRITE |
            G_PARAM_STATIC_STRINGS));

    g_object_class_install_property (
        object_class,
        PROP_HTML_EDITOR_VIEW,
        g_param_spec_object (
            "html-editor-view",
            NULL,
            NULL,
            E_TYPE_HTML_EDITOR_VIEW,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT_ONLY |
            G_PARAM_STATIC_STRINGS));

    /**
     * EHTMLEditorSelectionfont-color
     *
     * Holds font color of current selection or at current cursor position.
     */
    g_object_class_install_property (
        object_class,
        PROP_FONT_COLOR,
        g_param_spec_boxed (
            "font-color",
            NULL,
            NULL,
            GDK_TYPE_RGBA,
            G_PARAM_READWRITE |
            G_PARAM_STATIC_STRINGS));

    /**
     * EHTMLEditorSelectionfont-name
     *
     * Holds name of font in current selection or at current cursor
     * position.
     */
    g_object_class_install_property (
        object_class,
        PROP_FONT_NAME,
        g_param_spec_string (
            "font-name",
            NULL,
            NULL,
            NULL,
            G_PARAM_READWRITE |
            G_PARAM_STATIC_STRINGS));

    /**
     * EHTMLEditorSelectionfont-size
     *
     * Holds point size of current selection or at current cursor position.
     */
    g_object_class_install_property (
        object_class,
        PROP_FONT_SIZE,
        g_param_spec_int (
            "font-size",
            NULL,
            NULL,
            1,
            7,
            3,
            G_PARAM_READWRITE |
            G_PARAM_STATIC_STRINGS));

    /**
     * EHTMLEditorSelectionindented
     *
     * Holds whether current paragraph is indented. This does not include
     * citations.
     */
    g_object_class_install_property (
        object_class,
        PROP_INDENTED,
        g_param_spec_boolean (
            "indented",
            NULL,
            NULL,
            FALSE,
            G_PARAM_READABLE |
            G_PARAM_STATIC_STRINGS));

    /**
     * EHTMLEditorSelectionitalic
     *
     * Holds whether current selection or letter at current cursor position
     * is italic.
     */
    g_object_class_install_property (
        object_class,
        PROP_ITALIC,
        g_param_spec_boolean (
            "italic",
            NULL,
            NULL,
            FALSE,
            G_PARAM_READWRITE |
            G_PARAM_STATIC_STRINGS));

    /**
     * EHTMLEditorSelectionmonospaced
     *
     * Holds whether current selection or letter at current cursor position
     * is monospaced.
     */
    g_object_class_install_property (
        object_class,
        PROP_MONOSPACED,
        g_param_spec_boolean (
            "monospaced",
            NULL,
            NULL,
            FALSE,
            G_PARAM_READWRITE |
            G_PARAM_STATIC_STRINGS));

    /**
     * EHTMLEditorSelectionstrikethrough
     *
     * Holds whether current selection or letter at current cursor position
     * is strikethrough.
     */
    g_object_class_install_property (
        object_class,
        PROP_STRIKETHROUGH,
        g_param_spec_boolean (
            "strikethrough",
            NULL,
            NULL,
            FALSE,
            G_PARAM_READWRITE |
            G_PARAM_STATIC_STRINGS));

    /**
     * EHTMLEditorSelectionsuperscript
     *
     * Holds whether current selection or letter at current cursor position
     * is in superscript.
     */
    g_object_class_install_property (
        object_class,
        PROP_SUPERSCRIPT,
        g_param_spec_boolean (
            "superscript",
            NULL,
            NULL,
            FALSE,
            G_PARAM_READWRITE |
            G_PARAM_STATIC_STRINGS));

    /**
     * EHTMLEditorSelectionsubscript
     *
     * Holds whether current selection or letter at current cursor position
     * is in subscript.
     */
    g_object_class_install_property (
        object_class,
        PROP_SUBSCRIPT,
        g_param_spec_boolean (
            "subscript",
            NULL,
            NULL,
            FALSE,
            G_PARAM_READWRITE |
            G_PARAM_STATIC_STRINGS));

    /**
     * EHTMLEditorSelectiontext
     *
     * Holds always up-to-date text of current selection.
     */
    g_object_class_install_property (
        object_class,
        PROP_TEXT,
        g_param_spec_string (
            "text",
            NULL,
            NULL,
            NULL,
            G_PARAM_READABLE |
            G_PARAM_STATIC_STRINGS));

    /**
     * EHTMLEditorSelectionunderline
     *
     * Holds whether current selection or letter at current cursor position
     * is underlined.
     */
    g_object_class_install_property (
        object_class,
        PROP_UNDERLINE,
        g_param_spec_boolean (
            "underline",
            NULL,
            NULL,
            FALSE,
            G_PARAM_READWRITE |
            G_PARAM_STATIC_STRINGS));
}

static void
e_html_editor_selection_init (EHTMLEditorSelection *selection)
{
    GSettings *g_settings;

    selection->priv = E_HTML_EDITOR_SELECTION_GET_PRIVATE (selection);

    g_settings = g_settings_new ("org.gnome.evolution.mail");
    selection->priv->word_wrap_length =
        g_settings_get_int (g_settings, "composer-word-wrap-length");
    g_object_unref (g_settings);
}

gint
e_html_editor_selection_get_word_wrap_length (EHTMLEditorSelection *selection)
{
    g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), 72);

    return selection->priv->word_wrap_length;
}

/**
 * e_html_editor_selection_ref_html_editor_view:
 * @selection: an #EHTMLEditorSelection
 *
 * Returns a new reference to @selection's #EHTMLEditorView.  Unreference
 * the #EHTMLEditorView with g_object_unref() when finished with it.
 *
 * Returns: an #EHTMLEditorView
 **/
EHTMLEditorView *
e_html_editor_selection_ref_html_editor_view (EHTMLEditorSelection *selection)
{
    g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), NULL);

    return g_weak_ref_get (&selection->priv->html_editor_view);
}

/**
 * e_html_editor_selection_has_text:
 * @selection: an #EHTMLEditorSelection
 *
 * Returns whether current selection contains any text.
 *
 * Returns: @TRUE when current selection contains text, @FALSE otherwise.
 */
gboolean
e_html_editor_selection_has_text (EHTMLEditorSelection *selection)
{
    WebKitDOMRange *range;
    WebKitDOMNode *node;

    g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), FALSE);

    range = html_editor_selection_get_current_range (selection);

    node = webkit_dom_range_get_start_container (range, NULL);
    if (webkit_dom_node_get_node_type (node) == 3)
        return TRUE;

    node = webkit_dom_range_get_end_container (range, NULL);
    if (webkit_dom_node_get_node_type (node) == 3)
        return TRUE;

    node = WEBKIT_DOM_NODE (webkit_dom_range_clone_contents (range, NULL));
    while (node) {
        if (webkit_dom_node_get_node_type (node) == 3)
            return TRUE;

        if (webkit_dom_node_has_child_nodes (node)) {
            node = webkit_dom_node_get_first_child (node);
        } else if (webkit_dom_node_get_next_sibling (node)) {
            node = webkit_dom_node_get_next_sibling (node);
        } else {
            node = webkit_dom_node_get_parent_node (node);
            if (node) {
                node = webkit_dom_node_get_next_sibling (node);
            }
        }
    }

    return FALSE;
}

/**
 * e_html_editor_selection_get_caret_word:
 * @selection: an #EHTMLEditorSelection
 *
 * Returns word under cursor.
 *
 * Returns: A newly allocated string with current caret word or @NULL when there
 * is no text under cursor or when selection is active. [transfer-full].
 */
gchar *
e_html_editor_selection_get_caret_word (EHTMLEditorSelection *selection)
{
    WebKitDOMRange *range;

    g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), NULL);

    range = html_editor_selection_get_current_range (selection);

    /* Don't operate on the visible selection */
    range = webkit_dom_range_clone_range (range, NULL);
    webkit_dom_range_expand (range, "word", NULL);

    return webkit_dom_range_to_string (range, NULL);
}

/**
 * e_html_editor_selection_replace_caret_word:
 * @selection: an #EHTMLEditorSelection
 * @replacement: a string to replace current caret word with
 *
 * Replaces current word under cursor with @replacement.
 */
void
e_html_editor_selection_replace_caret_word (EHTMLEditorSelection *selection,
                                            const gchar *replacement)
{
    EHTMLEditorView *view;
    WebKitWebView *web_view;
    WebKitDOMDocument *document;
    WebKitDOMDOMWindow *window;
    WebKitDOMDOMSelection *dom_selection;
    WebKitDOMRange *range;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));
    g_return_if_fail (replacement != NULL);

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    web_view = WEBKIT_WEB_VIEW (view);

    range = html_editor_selection_get_current_range (selection);
    document = webkit_web_view_get_dom_document (web_view);
    window = webkit_dom_document_get_default_view (document);
    dom_selection = webkit_dom_dom_window_get_selection (window);

    webkit_dom_range_expand (range, "word", NULL);
    webkit_dom_dom_selection_add_range (dom_selection, range);

    e_html_editor_selection_insert_html (selection, replacement);

    g_object_unref (view);
}

/**
 * e_html_editor_selection_get_string:
 * @selection: an #EHTMLEditorSelection
 *
 * Returns currently selected string.
 *
 * Returns: A pointer to content of current selection. The string is owned by
 * #EHTMLEditorSelection and should not be free'd.
 */
const gchar *
e_html_editor_selection_get_string (EHTMLEditorSelection *selection)
{
    WebKitDOMRange *range;

    g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), NULL);

    range = html_editor_selection_get_current_range (selection);
    if (!range)
        return NULL;

    g_free (selection->priv->text);
    selection->priv->text = webkit_dom_range_get_text (range);

    return selection->priv->text;
}

/**
 * e_html_editor_selection_replace:
 * @selection: an #EHTMLEditorSelection
 * @new_string: a string to replace current selection with
 *
 * Replaces currently selected text with @new_string.
 */
void
e_html_editor_selection_replace (EHTMLEditorSelection *selection,
                                 const gchar *new_string)
{
    EHTMLEditorView *view;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    e_html_editor_view_exec_command (
        view, E_HTML_EDITOR_VIEW_COMMAND_INSERT_TEXT, new_string);

    g_object_unref (view);
}

/**
 * e_html_editor_selection_get_list_alignment_from_node:
 * @node: #an WebKitDOMNode
 *
 * Returns alignment of given list.
 *
 * Returns: #EHTMLEditorSelectionAlignment
 */
EHTMLEditorSelectionAlignment
e_html_editor_selection_get_list_alignment_from_node (WebKitDOMNode *node)
{
    if (element_has_class (WEBKIT_DOM_ELEMENT (node), "-x-evo-list-item-align-left"))
        return E_HTML_EDITOR_SELECTION_ALIGNMENT_LEFT;
    if (element_has_class (WEBKIT_DOM_ELEMENT (node), "-x-evo-list-item-align-center"))
        return E_HTML_EDITOR_SELECTION_ALIGNMENT_CENTER;
    if (element_has_class (WEBKIT_DOM_ELEMENT (node), "-x-evo-list-item-align-right"))
        return E_HTML_EDITOR_SELECTION_ALIGNMENT_RIGHT;

    return E_HTML_EDITOR_SELECTION_ALIGNMENT_LEFT;
}

static EHTMLEditorSelectionAlignment
e_html_editor_selection_get_alignment_from_node (WebKitDOMNode *node)
{
    EHTMLEditorSelectionAlignment alignment;
    gchar *value;
    WebKitDOMCSSStyleDeclaration *style;
    WebKitDOMDocument *document;
    WebKitDOMDOMWindow *window;

    document = webkit_dom_node_get_owner_document (node);
    window = webkit_dom_document_get_default_view (document);

    style = webkit_dom_dom_window_get_computed_style (
        window, WEBKIT_DOM_ELEMENT (node), NULL);
    value = webkit_dom_css_style_declaration_get_property_value (style, "text-align");

    if (!value || !*value ||
        (g_ascii_strncasecmp (value, "left", 4) == 0)) {
        alignment = E_HTML_EDITOR_SELECTION_ALIGNMENT_LEFT;
    } else if (g_ascii_strncasecmp (value, "center", 6) == 0) {
        alignment = E_HTML_EDITOR_SELECTION_ALIGNMENT_CENTER;
    } else if (g_ascii_strncasecmp (value, "right", 5) == 0) {
        alignment = E_HTML_EDITOR_SELECTION_ALIGNMENT_RIGHT;
    } else {
        alignment = E_HTML_EDITOR_SELECTION_ALIGNMENT_LEFT;
    }

    g_free (value);

    return alignment;
}

/**
 * e_html_editor_selection_get_alignment:
 * @selection: #an EHTMLEditorSelection
 *
 * Returns alignment of current paragraph
 *
 * Returns: #EHTMLEditorSelectionAlignment
 */
EHTMLEditorSelectionAlignment
e_html_editor_selection_get_alignment (EHTMLEditorSelection *selection)
{
    EHTMLEditorSelectionAlignment alignment;
    EHTMLEditorView *view;
    gchar *value;
    WebKitDOMCSSStyleDeclaration *style;
    WebKitDOMDocument *document;
    WebKitDOMDOMWindow *window;
    WebKitDOMElement *element;
    WebKitDOMNode *node;
    WebKitDOMRange *range;

    g_return_val_if_fail (
        E_IS_HTML_EDITOR_SELECTION (selection),
        E_HTML_EDITOR_SELECTION_ALIGNMENT_LEFT);

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_val_if_fail (view != NULL, FALSE);

    document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
    g_object_unref (view);
    window = webkit_dom_document_get_default_view (document);
    range = html_editor_selection_get_current_range (selection);
    if (!range)
        return E_HTML_EDITOR_SELECTION_ALIGNMENT_LEFT;

    node = webkit_dom_range_get_start_container (range, NULL);
    if (!node)
        return E_HTML_EDITOR_SELECTION_ALIGNMENT_LEFT;

    if (WEBKIT_DOM_IS_ELEMENT (node))
        element = WEBKIT_DOM_ELEMENT (node);
    else
        element = webkit_dom_node_get_parent_element (node);

    style = webkit_dom_dom_window_get_computed_style (window, element, NULL);
    value = webkit_dom_css_style_declaration_get_property_value (style, "text-align");

    if (!value || !*value ||
        (g_ascii_strncasecmp (value, "left", 4) == 0)) {
        alignment = E_HTML_EDITOR_SELECTION_ALIGNMENT_LEFT;
    } else if (g_ascii_strncasecmp (value, "center", 6) == 0) {
        alignment = E_HTML_EDITOR_SELECTION_ALIGNMENT_CENTER;
    } else if (g_ascii_strncasecmp (value, "right", 5) == 0) {
        alignment = E_HTML_EDITOR_SELECTION_ALIGNMENT_RIGHT;
    } else {
        alignment = E_HTML_EDITOR_SELECTION_ALIGNMENT_LEFT;
    }

    g_free (value);

    return alignment;
}

static void
set_ordered_list_type_to_element (WebKitDOMElement *list,
                                  EHTMLEditorSelectionBlockFormat format)
{
    if (format == E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST)
        webkit_dom_element_remove_attribute (list, "type");
    else if (format == E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ALPHA)
        webkit_dom_element_set_attribute (list, "type", "A", NULL);
    else if (format == E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ROMAN)
        webkit_dom_element_set_attribute (list, "type", "I", NULL);
}

static WebKitDOMElement *
create_list_element (EHTMLEditorSelection *selection,
                     WebKitDOMDocument *document,
                     EHTMLEditorSelectionBlockFormat format,
             gint level,
                     gboolean html_mode)
{
    WebKitDOMElement *list;
    gint offset = -SPACES_PER_LIST_LEVEL;
    gboolean inserting_unordered_list =
        format == E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_UNORDERED_LIST;

    list = webkit_dom_document_create_element (
        document, inserting_unordered_list  ? "UL" : "OL", NULL);

    set_ordered_list_type_to_element (list, format);

    if (level >= 0)
        offset = (level + 1) * -SPACES_PER_LIST_LEVEL;

    if (!html_mode)
        e_html_editor_selection_set_paragraph_style (
            selection, list, -1, offset, "");

    return list;
}

static WebKitDOMNode *
get_list_item_node_from_child (WebKitDOMNode *child)
{
    WebKitDOMNode *parent = webkit_dom_node_get_parent_node (child);

    while (parent && !WEBKIT_DOM_IS_HTMLLI_ELEMENT (parent))
        parent = webkit_dom_node_get_parent_node (parent);

    return parent;
}

static WebKitDOMNode *
get_list_node_from_child (WebKitDOMNode *child)
{
    WebKitDOMNode *parent = get_list_item_node_from_child (child);

    return webkit_dom_node_get_parent_node (parent);
}

static void
format_change_list_from_list (EHTMLEditorSelection *selection,
                              WebKitDOMDocument *document,
                              EHTMLEditorSelectionBlockFormat to,
                              gboolean html_mode)
{
    gboolean after_selection_end = FALSE;
    WebKitDOMElement *selection_start_marker, *selection_end_marker, *new_list;
    WebKitDOMNode *source_list, *source_list_clone, *current_list, *item;

    selection_start_marker = webkit_dom_document_query_selector (
        document, "span#-x-evo-selection-start-marker", NULL);
    selection_end_marker = webkit_dom_document_query_selector (
        document, "span#-x-evo-selection-end-marker", NULL);

    if (!selection_start_marker || !selection_end_marker)
        return;

    new_list = create_list_element (selection, document, to, 0, html_mode);

    /* Copy elements from previous block to list */
    item = get_list_item_node_from_child (
        WEBKIT_DOM_NODE (selection_start_marker));
    source_list = webkit_dom_node_get_parent_node (item);
    current_list = source_list;
    source_list_clone = webkit_dom_node_clone_node (source_list, FALSE);

    if (element_has_class (WEBKIT_DOM_ELEMENT (source_list), "-x-evo-indented"))
        element_add_class (WEBKIT_DOM_ELEMENT (new_list), "-x-evo-indented");

    while (item) {
        WebKitDOMNode *next_item = webkit_dom_node_get_next_sibling (item);

        if (WEBKIT_DOM_IS_HTMLLI_ELEMENT (item)) {
            webkit_dom_node_append_child (
                after_selection_end ?
                    source_list_clone : WEBKIT_DOM_NODE (new_list),
                WEBKIT_DOM_NODE (item),
                NULL);
        }

        if (webkit_dom_node_contains (item, WEBKIT_DOM_NODE (selection_end_marker))) {
            source_list_clone = webkit_dom_node_clone_node (current_list, FALSE);
            after_selection_end = TRUE;
        }

        if (!next_item) {
            if (after_selection_end)
                break;
            current_list = webkit_dom_node_get_next_sibling (current_list);
            next_item = webkit_dom_node_get_first_child (current_list);
        }
        item = next_item;
    }

    if (webkit_dom_node_has_child_nodes (source_list_clone))
        webkit_dom_node_insert_before (
            webkit_dom_node_get_parent_node (source_list),
            WEBKIT_DOM_NODE (source_list_clone),
            webkit_dom_node_get_next_sibling (source_list), NULL);
    if (webkit_dom_node_has_child_nodes (WEBKIT_DOM_NODE (new_list)))
        webkit_dom_node_insert_before (
            webkit_dom_node_get_parent_node (source_list),
            WEBKIT_DOM_NODE (new_list),
            webkit_dom_node_get_next_sibling (source_list), NULL);
    if (!webkit_dom_node_has_child_nodes (source_list))
        remove_node (source_list);
}

/**
 * e_html_editor_selection_set_alignment:
 * @selection: an #EHTMLEditorSelection
 * @alignment: an #EHTMLEditorSelectionAlignment value to apply
 *
 * Sets alignment of current paragraph to give @alignment.
 */
void
e_html_editor_selection_set_alignment (EHTMLEditorSelection *selection,
                                       EHTMLEditorSelectionAlignment alignment)
{
    EHTMLEditorView *view;
    EHTMLEditorViewCommand command;
    const gchar *class = "";
    WebKitDOMDocument *document;
    WebKitDOMElement *selection_start_marker;
    WebKitDOMNode *parent;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    if (e_html_editor_selection_get_alignment (selection) == alignment)
        return;

    switch (alignment) {
        case E_HTML_EDITOR_SELECTION_ALIGNMENT_CENTER:
            command = E_HTML_EDITOR_VIEW_COMMAND_JUSTIFY_CENTER;
            class  = "-x-evo-list-item-align-center";
            break;

        case E_HTML_EDITOR_SELECTION_ALIGNMENT_LEFT:
            command = E_HTML_EDITOR_VIEW_COMMAND_JUSTIFY_LEFT;
            break;

        case E_HTML_EDITOR_SELECTION_ALIGNMENT_RIGHT:
            command = E_HTML_EDITOR_VIEW_COMMAND_JUSTIFY_RIGHT;
            class  = "-x-evo-list-item-align-right";
            break;
    }

    selection->priv->alignment = alignment;

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));

    e_html_editor_selection_save (selection);

    selection_start_marker = webkit_dom_document_query_selector (
        document, "span#-x-evo-selection-start-marker", NULL);

    if (!selection_start_marker) {
        g_object_unref (view);
        return;
    }

    parent = webkit_dom_node_get_parent_node (
        WEBKIT_DOM_NODE (selection_start_marker));

    if (WEBKIT_DOM_IS_HTMLLI_ELEMENT (parent)) {
        element_remove_class (
            WEBKIT_DOM_ELEMENT (parent),
            "-x-evo-list-item-align-center");
        element_remove_class (
            WEBKIT_DOM_ELEMENT (parent),
            "-x-evo-list-item-align-right");

        element_add_class (WEBKIT_DOM_ELEMENT (parent), class);
    } else {
        e_html_editor_view_exec_command (view, command, NULL);
    }

    e_html_editor_selection_restore (selection);

    g_object_unref (view);

    g_object_notify (G_OBJECT (selection), "alignment");
}

/**
 * e_html_editor_selection_get_background_color:
 * @selection: an #EHTMLEditorSelection
 *
 * Returns background color of currently selected text or letter at current
 * cursor position.
 *
 * Returns: A string with code of current background color.
 */
const gchar *
e_html_editor_selection_get_background_color (EHTMLEditorSelection *selection)
{
    WebKitDOMNode *ancestor;
    WebKitDOMRange *range;
    WebKitDOMCSSStyleDeclaration *css;

    g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), NULL);

    range = html_editor_selection_get_current_range (selection);

    ancestor = webkit_dom_range_get_common_ancestor_container (range, NULL);

    css = webkit_dom_element_get_style (WEBKIT_DOM_ELEMENT (ancestor));
    selection->priv->background_color =
        webkit_dom_css_style_declaration_get_property_value (
            css, "background-color");

    return selection->priv->background_color;
}

/**
 * e_html_editor_selection_set_background_color:
 * @selection: an #EHTMLEditorSelection
 * @color: code of new background color to set
 *
 * Changes background color of current selection or letter at current cursor
 * position to @color.
 */
void
e_html_editor_selection_set_background_color (EHTMLEditorSelection *selection,
                                              const gchar *color)
{
    EHTMLEditorView *view;
    EHTMLEditorViewCommand command;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));
    g_return_if_fail (color != NULL && *color != '\0');

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    command = E_HTML_EDITOR_VIEW_COMMAND_BACKGROUND_COLOR;
    e_html_editor_view_exec_command (view, command, color);

    g_object_unref (view);

    g_object_notify (G_OBJECT (selection), "background-color");
}

static gint
get_indentation_level (WebKitDOMElement *element)
{
    WebKitDOMElement *parent;
    gint level = 0;

    if (element_has_class (element, "-x-evo-indented"))
        level++;

    parent = webkit_dom_node_get_parent_element (WEBKIT_DOM_NODE (element));
    /* Count level of indentation */
    while (parent && !WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent)) {
        if (element_has_class (parent, "-x-evo-indented"))
            level++;

        parent = webkit_dom_node_get_parent_element (WEBKIT_DOM_NODE (parent));
    }

    return level;
}

static WebKitDOMNode *
get_block_node (WebKitDOMRange *range)
{
    WebKitDOMNode *node;

    node = webkit_dom_range_get_common_ancestor_container (range, NULL);
    if (!WEBKIT_DOM_IS_ELEMENT (node))
        node = WEBKIT_DOM_NODE (webkit_dom_node_get_parent_element (node));

    if (element_has_class (WEBKIT_DOM_ELEMENT (node), "-x-evo-temp-text-wrapper"))
        node = WEBKIT_DOM_NODE (webkit_dom_node_get_parent_element (node));

    return node;
}

/**
 * e_html_editor_selection_get_list_format_from_node:
 * @node: an #WebKitDOMNode
 *
 * Returns block format of given list.
 *
 * Returns: #EHTMLEditorSelectionBlockFormat
 */
EHTMLEditorSelectionBlockFormat
e_html_editor_selection_get_list_format_from_node (WebKitDOMNode *node)
{
    EHTMLEditorSelectionBlockFormat format =
        E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_UNORDERED_LIST;

    if (WEBKIT_DOM_IS_HTMLLI_ELEMENT (node))
        return -1;

    if (WEBKIT_DOM_IS_HTMLU_LIST_ELEMENT (node))
        return format;

    if (WEBKIT_DOM_IS_HTMLO_LIST_ELEMENT (node)) {
        gchar *type_value = webkit_dom_element_get_attribute (
            WEBKIT_DOM_ELEMENT (node), "type");

        if (!type_value)
            return E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST;

        if (!*type_value)
            format = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST;
        else if (g_ascii_strcasecmp (type_value, "A") == 0)
            format = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ALPHA;
        else if (g_ascii_strcasecmp (type_value, "I") == 0)
            format = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ROMAN;
        g_free (type_value);

        return format;
    }

    return -1;
}

/**
 * e_html_editor_selection_get_block_format:
 * @selection: an #EHTMLEditorSelection
 *
 * Returns block format of current paragraph.
 *
 * Returns: #EHTMLEditorSelectionBlockFormat
 */
EHTMLEditorSelectionBlockFormat
e_html_editor_selection_get_block_format (EHTMLEditorSelection *selection)
{
    WebKitDOMNode *node;
    WebKitDOMRange *range;
    WebKitDOMElement *element;
    EHTMLEditorSelectionBlockFormat result;

    g_return_val_if_fail (
        E_IS_HTML_EDITOR_SELECTION (selection),
        E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_PARAGRAPH);

    range = html_editor_selection_get_current_range (selection);
    if (!range)
        return E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_PARAGRAPH;

    node = webkit_dom_range_get_start_container (range, NULL);

    if (e_html_editor_dom_node_find_parent_element (node, "UL")) {
        result = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_UNORDERED_LIST;
    } else if ((element = e_html_editor_dom_node_find_parent_element (node, "OL")) != NULL) {
        result = e_html_editor_selection_get_list_format_from_node (WEBKIT_DOM_NODE (element));
    } else if (e_html_editor_dom_node_find_parent_element (node, "PRE")) {
        result = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_PRE;
    } else if (e_html_editor_dom_node_find_parent_element (node, "ADDRESS")) {
        result = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ADDRESS;
    } else if (e_html_editor_dom_node_find_parent_element (node, "H1")) {
        result = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_H1;
    } else if (e_html_editor_dom_node_find_parent_element (node, "H2")) {
        result = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_H2;
    } else if (e_html_editor_dom_node_find_parent_element (node, "H3")) {
        result = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_H3;
    } else if (e_html_editor_dom_node_find_parent_element (node, "H4")) {
        result = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_H4;
    } else if (e_html_editor_dom_node_find_parent_element (node, "H5")) {
        result = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_H5;
    } else if (e_html_editor_dom_node_find_parent_element (node, "H6")) {
        result = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_H6;
    } else if ((element = e_html_editor_dom_node_find_parent_element (node, "BLOCKQUOTE")) != NULL) {
        if (element_has_class (element, "-x-evo-indented"))
            result = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_PARAGRAPH;
        else {
            WebKitDOMNode *block = get_block_node (range);

            if (element_has_class (WEBKIT_DOM_ELEMENT (block), "-x-evo-paragraph"))
                result = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_PARAGRAPH;
            else {
                /* Paragraphs inside quote */
                if ((element = e_html_editor_dom_node_find_parent_element (node, "DIV")) != NULL)
                    if (element_has_class (element, "-x-evo-paragraph"))
                        result = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_PARAGRAPH;
                    else
                        result = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_BLOCKQUOTE;
                else
                    result = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_BLOCKQUOTE;
            }
        }
    } else if (e_html_editor_dom_node_find_parent_element (node, "P")) {
        result = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_PARAGRAPH;
    } else {
        result = E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_PARAGRAPH;
    }

    return result;
}

static gboolean
is_caret_position_node (WebKitDOMNode *node)
{
    return element_has_id (WEBKIT_DOM_ELEMENT (node), "-x-evo-caret-position");
}

static void
merge_list_into_list (WebKitDOMNode *from,
                      WebKitDOMNode *to,
                      gboolean insert_before)
{
    WebKitDOMNode *item;

    if (!(to && from))
        return;

    while ((item = webkit_dom_node_get_first_child (from)) != NULL) {
        if (insert_before)
            webkit_dom_node_insert_before (
                to, item, webkit_dom_node_get_last_child (to), NULL);
        else
            webkit_dom_node_append_child (to, item, NULL);
    }

    if (!webkit_dom_node_has_child_nodes (from))
        remove_node (from);

}

static void
merge_lists_if_possible (WebKitDOMNode *list)
{
    EHTMLEditorSelectionBlockFormat format, prev, next;
    WebKitDOMNode *prev_sibling, *next_sibling;

    prev_sibling = webkit_dom_node_get_previous_sibling (WEBKIT_DOM_NODE (list));
    next_sibling = webkit_dom_node_get_next_sibling (WEBKIT_DOM_NODE (list));

    format = e_html_editor_selection_get_list_format_from_node (list),
    prev = e_html_editor_selection_get_list_format_from_node (prev_sibling);
    next = e_html_editor_selection_get_list_format_from_node (next_sibling);

    if (format == prev && format != -1 && prev != -1)
        merge_list_into_list (prev_sibling, list, TRUE);

    if (format == next && format != -1 && next != -1)
        merge_list_into_list (next_sibling, list, FALSE);
}

void
remove_wrapping_from_element (WebKitDOMElement *element)
{
    WebKitDOMNodeList *list;
    gint ii, length;

    list = webkit_dom_element_query_selector_all (
        element, "br.-x-evo-wrap-br", NULL);
    length = webkit_dom_node_list_get_length (list);
    for (ii = 0; ii < length; ii++)
        remove_node (webkit_dom_node_list_item (list, ii));

    webkit_dom_node_normalize (WEBKIT_DOM_NODE (element));
}

void
remove_quoting_from_element (WebKitDOMElement *element)
{
    gint ii, length;
    WebKitDOMNodeList *list;

    list = webkit_dom_element_query_selector_all (
        element, "span.-x-evo-quoted", NULL);
    length = webkit_dom_node_list_get_length (list);
    for (ii = 0; ii < length; ii++)
        remove_node (webkit_dom_node_list_item (list, ii));

    list = webkit_dom_element_query_selector_all (
        element, "span.-x-evo-temp-text-wrapper", NULL);
    length = webkit_dom_node_list_get_length (list);
    for (ii = 0; ii < length; ii++) {
        WebKitDOMNode *node = webkit_dom_node_list_item (list, ii);
        WebKitDOMNode *parent = webkit_dom_node_get_parent_node (node);

        while (webkit_dom_node_get_first_child (node))
            webkit_dom_node_insert_before (
                parent,
                webkit_dom_node_get_first_child (node),
                node,
                NULL);

        remove_node (node);
    }

    list = webkit_dom_element_query_selector_all (
        element, "br.-x-evo-temp-br", NULL);
    length = webkit_dom_node_list_get_length (list);
    for (ii = 0; ii < length; ii++)
        remove_node (webkit_dom_node_list_item (list, ii));

    webkit_dom_node_normalize (WEBKIT_DOM_NODE (element));
}

static gboolean
node_is_list (WebKitDOMNode *node)
{
    return node && (
        WEBKIT_DOM_IS_HTMLO_LIST_ELEMENT (node) ||
        WEBKIT_DOM_IS_HTMLU_LIST_ELEMENT (node));
}

static gint
get_citation_level (WebKitDOMNode *node)
{
    WebKitDOMNode *parent = webkit_dom_node_get_parent_node (node);
    gint level = 0;

    while (parent && !WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent)) {
        if (WEBKIT_DOM_IS_HTML_QUOTE_ELEMENT (parent) &&
            webkit_dom_element_has_attribute (WEBKIT_DOM_ELEMENT (parent), "type"))
            level++;

        parent = webkit_dom_node_get_parent_node (parent);
    }

    return level;
}

static WebKitDOMNode *
get_parent_block_node_from_child (WebKitDOMNode *node)
{
    WebKitDOMNode *parent = webkit_dom_node_get_parent_node (node);

    if (element_has_class (WEBKIT_DOM_ELEMENT (parent), "-x-evo-temp-text-wrapper") ||
        WEBKIT_DOM_IS_HTML_ANCHOR_ELEMENT (parent) ||
        element_has_tag (WEBKIT_DOM_ELEMENT (parent), "b") ||
        element_has_tag (WEBKIT_DOM_ELEMENT (parent), "i") ||
        element_has_tag (WEBKIT_DOM_ELEMENT (parent), "u"))
        parent = webkit_dom_node_get_parent_node (parent);

    return parent;
}

static void
format_change_block_to_block (EHTMLEditorSelection *selection,
                              EHTMLEditorSelectionBlockFormat format,
                              EHTMLEditorView *view,
                              const gchar *value,
                              WebKitDOMDocument *document)
{
    gboolean after_selection_end, html_mode;
    WebKitDOMElement *selection_start_marker, *selection_end_marker, *element;
    WebKitDOMNode *block, *next_block;

    e_html_editor_selection_save (selection);
    selection_start_marker = webkit_dom_document_query_selector (
        document, "span#-x-evo-selection-start-marker", NULL);
    selection_end_marker = webkit_dom_document_query_selector (
        document, "span#-x-evo-selection-end-marker", NULL);

    /* If the selection was not saved, move it into the first child of body */
    if (!selection_start_marker || !selection_end_marker) {
        WebKitDOMHTMLElement *body;

        body = webkit_dom_document_get_body (document);
        selection_start_marker = webkit_dom_document_create_element (
            document, "SPAN", NULL);
        webkit_dom_element_set_id (
            selection_start_marker, "-x-evo-selection-start-marker");
        webkit_dom_node_insert_before (
            webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (body)),
            WEBKIT_DOM_NODE (selection_start_marker),
            webkit_dom_node_get_first_child (
                webkit_dom_node_get_first_child (
                    WEBKIT_DOM_NODE (body))),
            NULL);
        selection_end_marker = webkit_dom_document_create_element (
            document, "SPAN", NULL);
        webkit_dom_element_set_id (
            selection_end_marker, "-x-evo-selection-end-marker");
        webkit_dom_node_insert_before (
            webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (body)),
            WEBKIT_DOM_NODE (selection_end_marker),
            webkit_dom_node_get_first_child (
                webkit_dom_node_get_first_child (
                    WEBKIT_DOM_NODE (body))),
            NULL);
    }

    block = get_parent_block_node_from_child (
        WEBKIT_DOM_NODE (selection_start_marker));

    html_mode = e_html_editor_view_get_html_mode (view);

    /* Process all blocks that are in the selection one by one */
    while (block) {
        gboolean quoted = FALSE;
        gboolean empty = FALSE;
        gchar *content;
        WebKitDOMNode *child;

        if (webkit_dom_element_query_selector (
            WEBKIT_DOM_ELEMENT (block), "span.-x-evo-quoted", NULL)) {
            quoted = TRUE;
            remove_quoting_from_element (WEBKIT_DOM_ELEMENT (block));
        }

        if (!html_mode)
            remove_wrapping_from_element (WEBKIT_DOM_ELEMENT (block));

        after_selection_end = webkit_dom_node_contains (
            block, WEBKIT_DOM_NODE (selection_end_marker));

        next_block = webkit_dom_node_get_next_sibling (block);

        if (format == E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_PARAGRAPH)
            element = e_html_editor_selection_get_paragraph_element (
                selection, document, -1, 0);
        else
            element = webkit_dom_document_create_element (
                document, value, NULL);

        content = webkit_dom_node_get_text_content (block);

        empty = !*content || (g_strcmp0 (content, UNICODE_ZERO_WIDTH_SPACE) == 0);
        g_free (content);

        if (empty) {
            webkit_dom_html_element_set_inner_html (
                WEBKIT_DOM_HTML_ELEMENT (element),
                UNICODE_ZERO_WIDTH_SPACE,
                NULL);
        }

        while ((child = webkit_dom_node_get_first_child (block)))
            webkit_dom_node_append_child (
                WEBKIT_DOM_NODE (element), child, NULL);

        webkit_dom_node_insert_before (
            webkit_dom_node_get_parent_node (block),
            WEBKIT_DOM_NODE (element),
            block,
            NULL);

        remove_node (block);

        block = next_block;

        if (format == E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_PARAGRAPH && !html_mode) {
            gint citation_level, quote;

            citation_level = get_citation_level (WEBKIT_DOM_NODE (element));
            quote = citation_level ? citation_level * 2 : 0;

            element = e_html_editor_selection_wrap_paragraph_length (
                selection, element, selection->priv->word_wrap_length - quote);
        }

        if (quoted)
            e_html_editor_view_quote_plain_text_element (view, element);

        if (after_selection_end)
            break;
    }

    e_html_editor_selection_restore (selection);
}

static void
format_change_block_to_list (EHTMLEditorSelection *selection,
                             EHTMLEditorSelectionBlockFormat format,
                             EHTMLEditorView *view,
                             WebKitDOMDocument *document)
{
    gboolean after_selection_end, in_quote = FALSE;
    gboolean html_mode = e_html_editor_view_get_html_mode (view);
    WebKitDOMElement *selection_start_marker, *selection_end_marker, *item, *list;
    WebKitDOMNode *block, *next_block;

    e_html_editor_selection_save (selection);

    selection_start_marker = webkit_dom_document_query_selector (
        document, "span#-x-evo-selection-start-marker", NULL);
    selection_end_marker = webkit_dom_document_query_selector (
        document, "span#-x-evo-selection-end-marker", NULL);

    /* If the selection was not saved, move it into the first child of body */
    if (!selection_start_marker || !selection_end_marker) {
        WebKitDOMHTMLElement *body;

        body = webkit_dom_document_get_body (document);
        selection_start_marker = webkit_dom_document_create_element (
            document, "SPAN", NULL);
        webkit_dom_element_set_id (
            selection_start_marker, "-x-evo-selection-start-marker");
        webkit_dom_node_insert_before (
            webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (body)),
            WEBKIT_DOM_NODE (selection_start_marker),
            webkit_dom_node_get_first_child (
                webkit_dom_node_get_first_child (
                    WEBKIT_DOM_NODE (body))),
            NULL);
        selection_end_marker = webkit_dom_document_create_element (
            document, "SPAN", NULL);
        webkit_dom_element_set_id (
            selection_end_marker, "-x-evo-selection-end-marker");
        webkit_dom_node_insert_before (
            webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (body)),
            WEBKIT_DOM_NODE (selection_end_marker),
            webkit_dom_node_get_first_child (
                webkit_dom_node_get_first_child (
                    WEBKIT_DOM_NODE (body))),
            NULL);
    }

    block = get_parent_block_node_from_child (
        WEBKIT_DOM_NODE (selection_start_marker));

    list = create_list_element (selection, document, format, 0, html_mode);

    if (webkit_dom_element_query_selector (
        WEBKIT_DOM_ELEMENT (block), "span.-x-evo-quoted", NULL)) {
        WebKitDOMElement *element;

        in_quote = TRUE;

        webkit_dom_node_insert_before (
            webkit_dom_node_get_parent_node (block),
            e_html_editor_selection_get_caret_position_node (document),
            block,
            NULL);

        e_html_editor_selection_restore_caret_position (selection);

        e_html_editor_view_exec_command (
            view, E_HTML_EDITOR_VIEW_COMMAND_INSERT_NEW_LINE_IN_QUOTED_CONTENT, NULL);

        element = webkit_dom_document_query_selector (
            document, "body>br", NULL);

        webkit_dom_node_replace_child (
            webkit_dom_node_get_parent_node (WEBKIT_DOM_NODE (element)),
            WEBKIT_DOM_NODE (list),
            WEBKIT_DOM_NODE (element),
            NULL);

        block = get_parent_block_node_from_child (
            WEBKIT_DOM_NODE (selection_start_marker));
    } else
        webkit_dom_node_insert_before (
            webkit_dom_node_get_parent_node (block),
            WEBKIT_DOM_NODE (list),
            block,
            NULL);

    /* Process all blocks that are in the selection one by one */
    while (block) {
        gboolean empty = FALSE;
        gchar *content;
        WebKitDOMNode *child, *parent;

        after_selection_end = webkit_dom_node_contains (
            block, WEBKIT_DOM_NODE (selection_end_marker));

        next_block = webkit_dom_node_get_next_sibling (
            WEBKIT_DOM_NODE (block));

        remove_wrapping_from_element (WEBKIT_DOM_ELEMENT (block));
        remove_quoting_from_element (WEBKIT_DOM_ELEMENT (block));

        item = webkit_dom_document_create_element (document, "LI", NULL);
        content = webkit_dom_node_get_text_content (block);

        empty = !*content || (g_strcmp0 (content, UNICODE_ZERO_WIDTH_SPACE) == 0);
        g_free (content);

        /* We have to use again the hidden space to move caret into newly inserted list */
        if (empty) {
            webkit_dom_html_element_set_inner_html (
                WEBKIT_DOM_HTML_ELEMENT (item),
                UNICODE_ZERO_WIDTH_SPACE,
                NULL);
        }

        while ((child = webkit_dom_node_get_first_child (block)))
            webkit_dom_node_append_child (
                WEBKIT_DOM_NODE (item), child, NULL);

        webkit_dom_node_append_child (
            WEBKIT_DOM_NODE (list), WEBKIT_DOM_NODE (item), NULL);

        parent = webkit_dom_node_get_parent_node (block);
        remove_node (block);

        if (in_quote) {
            /* Remove all parents if previously removed node was the
             * only one with text content */
            content = webkit_dom_node_get_text_content (parent);
            while (parent && content && !*content) {
                WebKitDOMNode *tmp = webkit_dom_node_get_parent_node (parent);

                remove_node (parent);
                parent = tmp;

                g_free (content);
                content = webkit_dom_node_get_text_content (parent);
            }
            g_free (content);
        }

        block = next_block;

        if (after_selection_end)
            break;
    }

    merge_lists_if_possible (WEBKIT_DOM_NODE (list));

    e_html_editor_selection_restore (selection);
    e_html_editor_view_force_spell_check (view);
}

static void
format_change_list_to_list (EHTMLEditorSelection *selection,
                            EHTMLEditorSelectionBlockFormat format,
                            WebKitDOMDocument *document,
                            gboolean html_mode)
{
    EHTMLEditorSelectionBlockFormat prev = 0, next = 0;
    gboolean done = FALSE, indented = FALSE;
    gboolean selection_starts_in_first_child, selection_ends_in_last_child;
    WebKitDOMElement *selection_start_marker, *selection_end_marker;
    WebKitDOMNode *prev_list, *current_list, *next_list;

    e_html_editor_selection_save (selection);

    selection_start_marker = webkit_dom_document_query_selector (
        document, "span#-x-evo-selection-start-marker", NULL);
    selection_end_marker = webkit_dom_document_query_selector (
        document, "span#-x-evo-selection-end-marker", NULL);

    current_list = get_list_node_from_child (
        WEBKIT_DOM_NODE (selection_start_marker));

    prev_list = get_list_node_from_child (
        WEBKIT_DOM_NODE (selection_start_marker));

    next_list = get_list_node_from_child (
        WEBKIT_DOM_NODE (selection_end_marker));

    selection_starts_in_first_child =
        webkit_dom_node_contains (
            webkit_dom_node_get_first_child (current_list),
            WEBKIT_DOM_NODE (selection_start_marker));

    selection_ends_in_last_child =
        webkit_dom_node_contains (
            webkit_dom_node_get_last_child (current_list),
            WEBKIT_DOM_NODE (selection_end_marker));

    indented = element_has_class (WEBKIT_DOM_ELEMENT (current_list), "-x-evo-indented");

    if (!prev_list || !next_list || indented) {
        format_change_list_from_list (selection, document, format, html_mode);
        goto out;
    }

    if (webkit_dom_node_is_same_node (prev_list, next_list)) {
        prev_list = webkit_dom_node_get_previous_sibling (
            webkit_dom_node_get_parent_node (
                webkit_dom_node_get_parent_node (
                    WEBKIT_DOM_NODE (selection_start_marker))));
        next_list = webkit_dom_node_get_next_sibling (
            webkit_dom_node_get_parent_node (
                webkit_dom_node_get_parent_node (
                    WEBKIT_DOM_NODE (selection_end_marker))));
        if (!prev_list || !next_list) {
            format_change_list_from_list (selection, document, format, html_mode);
            goto out;
        }
    }

    prev = e_html_editor_selection_get_list_format_from_node (prev_list);
    next = e_html_editor_selection_get_list_format_from_node (next_list);

    if (format == prev && format != -1 && prev != -1) {
        if (selection_starts_in_first_child && selection_ends_in_last_child) {
            done = TRUE;
            merge_list_into_list (current_list, prev_list, FALSE);
        }
    }

    if (format == next && format != -1 && next != -1) {
        if (selection_starts_in_first_child && selection_ends_in_last_child) {
            done = TRUE;
            merge_list_into_list (next_list, prev_list, FALSE);
        }
    }

    if (done)
        goto out;

    format_change_list_from_list (selection, document, format, html_mode);
out:
    e_html_editor_selection_restore (selection);
}

static void
format_change_list_to_block (EHTMLEditorSelection *selection,
                             EHTMLEditorSelectionBlockFormat format,
                             WebKitDOMDocument *document)
{
    gboolean after_end = FALSE;
    WebKitDOMElement *selection_start, *element, *selection_end;
    WebKitDOMNode *source_list, *next_item, *item, *source_list_clone;

    e_html_editor_selection_save (selection);

    selection_start = webkit_dom_document_query_selector (
        document, "span#-x-evo-selection-start-marker", NULL);
    selection_end = webkit_dom_document_query_selector (
        document, "span#-x-evo-selection-end-marker", NULL);

    item = get_list_item_node_from_child (
        WEBKIT_DOM_NODE (selection_start));
    source_list = webkit_dom_node_get_parent_node (WEBKIT_DOM_NODE (item));
    source_list_clone = webkit_dom_node_clone_node (source_list, FALSE);

    webkit_dom_node_insert_before (
        webkit_dom_node_get_parent_node (source_list),
        WEBKIT_DOM_NODE (source_list_clone),
        webkit_dom_node_get_next_sibling (source_list),
        NULL);

    next_item = item;

    /* Process all nodes that are in selection one by one */
    while (next_item) {
        WebKitDOMNode *tmp;

        tmp = webkit_dom_node_get_next_sibling (WEBKIT_DOM_NODE (next_item));

        if (!after_end) {
            if (format == E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_PARAGRAPH)
                element = e_html_editor_selection_get_paragraph_element (selection, document, -1, 0);
            else if (format == E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_PRE)
                element = webkit_dom_document_create_element (document, "PRE", NULL);
            else if (format == E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_BLOCKQUOTE)
                element = webkit_dom_document_create_element (document, "BLOCKQUOTE", NULL);
            else
                element = e_html_editor_selection_get_paragraph_element (selection, document, -1, 0);

            after_end = webkit_dom_node_contains (next_item, WEBKIT_DOM_NODE (selection_end));

            while (webkit_dom_node_get_first_child (next_item)) {
                WebKitDOMNode *node = webkit_dom_node_get_first_child (next_item);

                webkit_dom_node_append_child (
                    WEBKIT_DOM_NODE (element), node, NULL);
            }

            webkit_dom_node_insert_before (
                webkit_dom_node_get_parent_node (source_list),
                WEBKIT_DOM_NODE (element),
                source_list_clone,
                NULL);

            remove_node (next_item);

            next_item = tmp;
        } else {
            webkit_dom_node_append_child (
                source_list_clone, next_item, NULL);

            next_item = tmp;
        }
    }

    remove_node_if_empty (source_list_clone);
    remove_node_if_empty (source_list);

    e_html_editor_selection_restore (selection);
}

/**
 * e_html_editor_selection_set_block_format:
 * @selection: an #EHTMLEditorSelection
 * @format: an #EHTMLEditorSelectionBlockFormat value
 *
 * Changes block format of current paragraph to @format.
 */
void
e_html_editor_selection_set_block_format (EHTMLEditorSelection *selection,
                                          EHTMLEditorSelectionBlockFormat format)
{
    EHTMLEditorView *view;
    EHTMLEditorSelectionBlockFormat current_format;
    const gchar *value;
    gboolean from_list = FALSE, to_list = FALSE, html_mode;
    WebKitDOMDocument *document;
    WebKitDOMRange *range;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    current_format = e_html_editor_selection_get_block_format (selection);
    if (current_format == format) {
        return;
    }

    switch (format) {
        case E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_BLOCKQUOTE:
            value = "BLOCKQUOTE";
            break;
        case E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_H1:
            value = "H1";
            break;
        case E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_H2:
            value = "H2";
            break;
        case E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_H3:
            value = "H3";
            break;
        case E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_H4:
            value = "H4";
            break;
        case E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_H5:
            value = "H5";
            break;
        case E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_H6:
            value = "H6";
            break;
        case E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_PARAGRAPH:
            value = "P";
            break;
        case E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_PRE:
            value = "PRE";
            break;
        case E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ADDRESS:
            value = "ADDRESS";
            break;
        case E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST:
        case E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ALPHA:
        case E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_ORDERED_LIST_ROMAN:
            to_list = TRUE;
            value = NULL;
            break;
        case E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_UNORDERED_LIST:
            to_list = TRUE;
            value = NULL;
            break;
        case E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_NONE:
        default:
            value = NULL;
            break;
    }

    /* H1 - H6 have bold font by default */
    if (format >= E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_H1 &&
        format <= E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_H6)
        selection->priv->is_bold = TRUE;

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    html_mode = e_html_editor_view_get_html_mode (view);
    document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));

    from_list =
        current_format >= E_HTML_EDITOR_SELECTION_BLOCK_FORMAT_UNORDERED_LIST;

    range = html_editor_selection_get_current_range (selection);
    if (!range) {
        g_object_unref (view);
        return;
    }

    if (from_list && to_list)
        format_change_list_to_list (selection, format, document, html_mode);

    if (!from_list && !to_list)
        format_change_block_to_block (selection, format, view, value, document);

    if (from_list && !to_list)
        format_change_list_to_block (selection, format, document);

    if (!from_list && to_list)
        format_change_block_to_list (selection, format, view, document);

    e_html_editor_view_force_spell_check (view);

    g_object_unref (view);

    /* When changing the format we need to re-set the alignment */
    e_html_editor_selection_set_alignment (selection, selection->priv->alignment);

    e_html_editor_view_set_changed (view, TRUE);

    g_object_notify (G_OBJECT (selection), "block-format");
}

/**
 * e_html_editor_selection_get_font_color:
 * @selection: an #EHTMLEditorSelection
 * @rgba: a #GdkRGBA object to be set to current font color
 *
 * Sets @rgba to contain color of current text selection or letter at current
 * cursor position.
 */
void
e_html_editor_selection_get_font_color (EHTMLEditorSelection *selection,
                                        GdkRGBA *rgba)
{
    gchar *color;
    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    if (g_strcmp0 (e_html_editor_selection_get_string (selection), "") == 0) {
        color = g_strdup (selection->priv->font_color);
    } else {
        color = get_font_property (selection, "color");
        if (!color) {
            *rgba = black;
            return;
        }
    }

    gdk_rgba_parse (rgba, color);
    g_free (color);
}

/**
 * e_html_editor_selection_set_font_color:
 * @selection: an #EHTMLEditorSelection
 * @rgba: a #GdkRGBA
 *
 * Sets font color of current selection or letter at current cursor position to
 * color defined in @rgba.
 */
void
e_html_editor_selection_set_font_color (EHTMLEditorSelection *selection,
                                        const GdkRGBA *rgba)
{
    EHTMLEditorView *view;
    EHTMLEditorViewCommand command;
    guint32 rgba_value;
    gchar *color;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    if (!rgba)
        rgba = &black;

    rgba_value = e_rgba_to_value ((GdkRGBA *) rgba);

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    command = E_HTML_EDITOR_VIEW_COMMAND_FORE_COLOR;
    color = g_strdup_printf ("#%06x", rgba_value);
    selection->priv->font_color = g_strdup (color);
    e_html_editor_view_exec_command (view, command, color);
    g_free (color);

    g_object_unref (view);

    g_object_notify (G_OBJECT (selection), "font-color");
}

/**
 * e_html_editor_selection_get_font_name:
 * @selection: an #EHTMLEditorSelection
 *
 * Returns name of font used in current selection or at letter at current cursor
 * position.
 *
 * Returns: A string with font name. [transfer-none]
 */
const gchar *
e_html_editor_selection_get_font_name (EHTMLEditorSelection *selection)
{
    WebKitDOMNode *node;
    WebKitDOMRange *range;
    WebKitDOMCSSStyleDeclaration *css;

    g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), NULL);

    range = html_editor_selection_get_current_range (selection);
    node = webkit_dom_range_get_common_ancestor_container (range, NULL);

    g_free (selection->priv->font_family);
    css = webkit_dom_element_get_style (WEBKIT_DOM_ELEMENT (node));
    selection->priv->font_family =
        webkit_dom_css_style_declaration_get_property_value (css, "fontFamily");

    return selection->priv->font_family;
}

/**
 * e_html_editor_selection_set_font_name:
 * @selection: an #EHTMLEditorSelection
 * @font_name: a font name to apply
 *
 * Sets font name of current selection or of letter at current cursor position
 * to @font_name.
 */
void
e_html_editor_selection_set_font_name (EHTMLEditorSelection *selection,
                                       const gchar *font_name)
{
    EHTMLEditorView *view;
    EHTMLEditorViewCommand command;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    command = E_HTML_EDITOR_VIEW_COMMAND_FONT_NAME;
    e_html_editor_view_exec_command (view, command, font_name);

    g_object_unref (view);

    g_object_notify (G_OBJECT (selection), "font-name");
}

/**
 * e_editor_Selection_get_font_size:
 * @selection: an #EHTMLEditorSelection
 *
 * Returns point size of current selection or of letter at current cursor position.
 */
 guint
e_html_editor_selection_get_font_size (EHTMLEditorSelection *selection)
{
    gchar *size;
    guint size_int;

    g_return_val_if_fail (
        E_IS_HTML_EDITOR_SELECTION (selection),
        E_HTML_EDITOR_SELECTION_FONT_SIZE_NORMAL);

    size = get_font_property (selection, "size");
    if (!size)
        return E_HTML_EDITOR_SELECTION_FONT_SIZE_NORMAL;

    size_int = atoi (size);
    g_free (size);

    if (size_int == 0)
        return E_HTML_EDITOR_SELECTION_FONT_SIZE_NORMAL;

    return size_int;
}

/**
 * e_html_editor_selection_set_font_size:
 * @selection: an #EHTMLEditorSelection
 * @font_size: point size to apply
 *
 * Sets font size of current selection or of letter at current cursor position
 * to @font_size.
 */
void
e_html_editor_selection_set_font_size (EHTMLEditorSelection *selection,
                                       guint font_size)
{
    EHTMLEditorView *view;
    EHTMLEditorViewCommand command;
    gchar *size_str;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    selection->priv->font_size = font_size;
    command = E_HTML_EDITOR_VIEW_COMMAND_FONT_SIZE;
    size_str = g_strdup_printf ("%d", font_size);
    e_html_editor_view_exec_command (view, command, size_str);
    g_free (size_str);

    g_object_unref (view);

    g_object_notify (G_OBJECT (selection), "font-size");
}

/**
 * e_html_editor_selection_is_citation:
 * @selection: an #EHTMLEditorSelection
 *
 * Returns whether current paragraph is a citation.
 *
 * Returns: @TRUE when current paragraph is a citation, @FALSE otherwise.
 */
gboolean
e_html_editor_selection_is_citation (EHTMLEditorSelection *selection)
{
    gboolean ret_val;
    gchar *value, *text_content;
    WebKitDOMNode *node;
    WebKitDOMRange *range;

    g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), FALSE);

    range = html_editor_selection_get_current_range (selection);
    if (!range)
        return FALSE;

    node = webkit_dom_range_get_common_ancestor_container (range, NULL);

    if (WEBKIT_DOM_IS_TEXT (node))
        return get_has_style (selection, "citation");

    /* If we are changing the format of block we have to re-set bold property,
     * otherwise it will be turned off because of no text in composer */
    text_content = webkit_dom_node_get_text_content (node);
    if (g_strcmp0 (text_content, "") == 0) {
        g_free (text_content);
        return FALSE;
    }
    g_free (text_content);

    value = webkit_dom_element_get_attribute (WEBKIT_DOM_ELEMENT (node), "type");

    /* citation == <blockquote type='cite'> */
    if (g_strstr_len (value, -1, "cite"))
        ret_val = TRUE;
    else
        ret_val = get_has_style (selection, "citation");

    g_free (value);
    return ret_val;
}

static WebKitDOMNode *
get_parent_indented_block (WebKitDOMNode *node)
{
    WebKitDOMNode *parent, *block = NULL;

    parent = webkit_dom_node_get_parent_node (node);
    if (element_has_class (WEBKIT_DOM_ELEMENT (parent), "-x-evo-indented"))
        block = parent;

    while (parent && !WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent)) {
        if (element_has_class (WEBKIT_DOM_ELEMENT (parent), "-x-evo-indented"))
            block = parent;
        parent = webkit_dom_node_get_parent_node (parent);
    }

    return block;
}

static WebKitDOMElement*
get_element_for_inspection (WebKitDOMRange *range)
{
    WebKitDOMNode *node;

    node = webkit_dom_range_get_end_container (range, NULL);
    return WEBKIT_DOM_ELEMENT (get_parent_indented_block (node));
}

/**
 * e_html_editor_selection_is_indented:
 * @selection: an #EHTMLEditorSelection
 *
 * Returns whether current paragraph is indented. This does not include
 * citations.  To check, whether paragraph is a citation, use
 * e_html_editor_selection_is_citation().
 *
 * Returns: @TRUE when current paragraph is indented, @FALSE otherwise.
 */
gboolean
e_html_editor_selection_is_indented (EHTMLEditorSelection *selection)
{
    WebKitDOMElement *element;
    WebKitDOMRange *range;

    g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), FALSE);

    range = html_editor_selection_get_current_range (selection);
    if (!range)
        return FALSE;

    if (g_strcmp0 (e_html_editor_selection_get_string (selection), "") == 0) {
        element = get_element_for_inspection (range);
        return element_has_class (element, "-x-evo-indented");
    } else {
        /* If there is a selection search in it and don't look just in
         * the end container */
        WebKitDOMDocumentFragment *fragment;

        fragment = webkit_dom_range_clone_contents (range, NULL);

        if (fragment) {
            element = webkit_dom_document_fragment_query_selector (
                fragment, ".-x-evo-indented", NULL);

            if (element)
                return TRUE;

            element = get_element_for_inspection (range);
            return element_has_class (element, "-x-evo-indented");
        }
    }

    return FALSE;
}

static gboolean
is_in_html_mode (EHTMLEditorSelection *selection)
{
    EHTMLEditorView *view = e_html_editor_selection_ref_html_editor_view (selection);
    gboolean ret_val;

    g_return_val_if_fail (view != NULL, FALSE);

    ret_val = e_html_editor_view_get_html_mode (view);

    g_object_unref (view);

    return ret_val;
}

static gint
get_list_level (WebKitDOMNode *node)
{
    gint level = 0;

    while (node && !WEBKIT_DOM_IS_HTML_BODY_ELEMENT (node)) {
        if (node_is_list (node))
            level++;
        node = webkit_dom_node_get_parent_node (node);
    }

    return level;
}

static void
indent_list (EHTMLEditorSelection *selection,
             WebKitDOMDocument *document)
{
    WebKitDOMElement *selection_start_marker, *selection_end_marker;
    WebKitDOMNode *item, *next_item;
    gboolean after_selection_end = FALSE;

    selection_start_marker = webkit_dom_document_query_selector (
        document, "span#-x-evo-selection-start-marker", NULL);

    selection_end_marker = webkit_dom_document_query_selector (
        document, "span#-x-evo-selection-end-marker", NULL);

    item = get_parent_block_node_from_child (
        WEBKIT_DOM_NODE (selection_start_marker));

    if (WEBKIT_DOM_IS_HTMLLI_ELEMENT (item)) {
        gboolean html_mode = is_in_html_mode (selection);
        WebKitDOMElement *list;
        WebKitDOMNode *source_list = webkit_dom_node_get_parent_node (item);
        EHTMLEditorSelectionBlockFormat format;

        format = e_html_editor_selection_get_list_format_from_node (source_list);

        list = create_list_element (
            selection, document, format, get_list_level (item), html_mode);

        element_add_class (list, "-x-evo-indented");

        webkit_dom_node_insert_before (
            source_list, WEBKIT_DOM_NODE (list), item, NULL);

        while (item) {
            after_selection_end = webkit_dom_node_contains (
                item, WEBKIT_DOM_NODE (selection_end_marker));

            next_item = webkit_dom_node_get_next_sibling (item);

            webkit_dom_node_append_child (
                WEBKIT_DOM_NODE (list), item, NULL);

            item = next_item;

            if (after_selection_end)
                break;
        }

        merge_lists_if_possible (WEBKIT_DOM_NODE (list));
    }
}

static void
indent_block (EHTMLEditorSelection *selection,
              WebKitDOMDocument *document,
              WebKitDOMNode *block,
              gint width)
{
    WebKitDOMElement *element;

    element = e_html_editor_selection_get_indented_element (
        selection, document, width);

    webkit_dom_node_insert_before (
        webkit_dom_node_get_parent_node (block),
        WEBKIT_DOM_NODE (element),
        block,
        NULL);

    /* Remove style and let the paragraph inherit it from parent */
    if (element_has_class (WEBKIT_DOM_ELEMENT (block), "-x-evo-paragraph"))
        webkit_dom_element_remove_attribute (
            WEBKIT_DOM_ELEMENT (block), "style");

    webkit_dom_node_append_child (
        WEBKIT_DOM_NODE (element),
        block,
        NULL);
}

/**
 * e_html_editor_selection_indent:
 * @selection: an #EHTMLEditorSelection
 *
 * Indents current paragraph by one level.
 */
void
e_html_editor_selection_indent (EHTMLEditorSelection *selection)
{
    EHTMLEditorView *view;
    gboolean after_selection_start = FALSE, after_selection_end = FALSE;
    WebKitDOMDocument *document;
    WebKitDOMElement *selection_start_marker, *selection_end_marker;
    WebKitDOMNode *block;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));

    e_html_editor_selection_save (selection);

    selection_start_marker = webkit_dom_document_query_selector (
        document, "span#-x-evo-selection-start-marker", NULL);

    selection_end_marker = webkit_dom_document_query_selector (
        document, "span#-x-evo-selection-end-marker", NULL);

    /* If the selection was not saved, move it into the first child of body */
    if (!selection_start_marker || !selection_end_marker) {
        WebKitDOMHTMLElement *body;

        body = webkit_dom_document_get_body (document);
        selection_start_marker = webkit_dom_document_create_element (
            document, "SPAN", NULL);
        webkit_dom_element_set_id (
            selection_start_marker, "-x-evo-selection-start-marker");
        webkit_dom_node_insert_before (
            webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (body)),
            WEBKIT_DOM_NODE (selection_start_marker),
            webkit_dom_node_get_first_child (
                webkit_dom_node_get_first_child (
                    WEBKIT_DOM_NODE (body))),
            NULL);
        selection_end_marker = webkit_dom_document_create_element (
            document, "SPAN", NULL);
        webkit_dom_element_set_id (
            selection_end_marker, "-x-evo-selection-end-marker");
        webkit_dom_node_insert_before (
            webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (body)),
            WEBKIT_DOM_NODE (selection_end_marker),
            webkit_dom_node_get_first_child (
                webkit_dom_node_get_first_child (
                    WEBKIT_DOM_NODE (body))),
            NULL);
    }

    block = get_parent_indented_block (
        WEBKIT_DOM_NODE (selection_start_marker));
    if (!block)
        block = get_parent_block_node_from_child (
            WEBKIT_DOM_NODE (selection_start_marker));

    while (block) {
        gint ii, length, level, final_width = 0;
        gint word_wrap_length = selection->priv->word_wrap_length;
        WebKitDOMNode *next_block;
        WebKitDOMNodeList *list;

        next_block = webkit_dom_node_get_next_sibling (block);

        list = webkit_dom_element_query_selector_all (
            WEBKIT_DOM_ELEMENT (block),
            ".-x-evo-indented > *:not(.-x-evo-indented):not(li)",
            NULL);

        after_selection_end = webkit_dom_node_contains (
            block, WEBKIT_DOM_NODE (selection_end_marker));

        length = webkit_dom_node_list_get_length (list);
        if (length == 0 && node_is_list (block)) {
            indent_list (selection, document);
            if (!after_selection_end) {
                block = next_block;
                continue;
            } else
                goto out;
        }

        if (length == 0) {
            if (!after_selection_start) {
                after_selection_start = webkit_dom_node_contains (
                    block, WEBKIT_DOM_NODE (selection_start_marker));
                if (!after_selection_start) {
                    block = next_block;
                    continue;
                }
            }

            level = get_indentation_level (WEBKIT_DOM_ELEMENT (block));

            final_width = word_wrap_length - SPACES_PER_INDENTATION * (level + 1);
            if (final_width < 10 && !is_in_html_mode (selection)) {
                if (!after_selection_end) {
                    block = next_block;
                    continue;
                } else
                    goto out;
            }

            indent_block (selection, document, block, final_width);

            if (after_selection_end)
                goto out;
        }

        for (ii = 0; ii < length; ii++) {
            WebKitDOMNode *block_to_process;

            block_to_process = webkit_dom_node_list_item (list, ii);

            after_selection_end = webkit_dom_node_contains (
                block_to_process, WEBKIT_DOM_NODE (selection_end_marker));

            if (!after_selection_start) {
                after_selection_start = webkit_dom_node_contains (
                    block_to_process,
                    WEBKIT_DOM_NODE (selection_start_marker));
                if (!after_selection_start)
                    continue;
            }

            level = get_indentation_level (
                WEBKIT_DOM_ELEMENT (block_to_process));

            final_width = word_wrap_length - SPACES_PER_INDENTATION * (level + 1);
            if (final_width < 10 && !is_in_html_mode (selection))
                continue;

            indent_block (selection, document, block_to_process, final_width);

            if (after_selection_end)
                goto out;
        }

        block = next_block;
    }
 out:
    e_html_editor_selection_restore (selection);
    e_html_editor_view_force_spell_check (view);

    g_object_unref (view);

    g_object_notify (G_OBJECT (selection), "indented");
}

static const gchar *
get_css_alignment_value (EHTMLEditorSelectionAlignment alignment)
{
    if (alignment == E_HTML_EDITOR_SELECTION_ALIGNMENT_LEFT)
        return ""; /* Left is by default on ltr */

    if (alignment == E_HTML_EDITOR_SELECTION_ALIGNMENT_CENTER)
        return  "text-align: center;";

    if (alignment == E_HTML_EDITOR_SELECTION_ALIGNMENT_RIGHT)
        return "text-align: right;";

    return "";
}

static void
unindent_list (EHTMLEditorSelection *selection,
               WebKitDOMDocument *document)
{
    gboolean after = FALSE;
    WebKitDOMElement *new_list;
    WebKitDOMElement *selection_start_marker, *selection_end_marker;
    WebKitDOMNode *source_list, *source_list_clone, *current_list, *item;
    WebKitDOMNode *prev_item;

    selection_start_marker = webkit_dom_document_query_selector (
        document, "span#-x-evo-selection-start-marker", NULL);
    selection_end_marker = webkit_dom_document_query_selector (
        document, "span#-x-evo-selection-end-marker", NULL);

    if (!selection_start_marker || !selection_end_marker)
        return;

    /* Copy elements from previous block to list */
    item = get_parent_block_node_from_child (
        WEBKIT_DOM_NODE (selection_start_marker));
    source_list = webkit_dom_node_get_parent_node (item);
    new_list = WEBKIT_DOM_ELEMENT (
        webkit_dom_node_clone_node (source_list, FALSE));
    current_list = source_list;
    source_list_clone = webkit_dom_node_clone_node (source_list, FALSE);

    webkit_dom_node_insert_before (
        webkit_dom_node_get_parent_node (source_list),
        WEBKIT_DOM_NODE (source_list_clone),
        webkit_dom_node_get_next_sibling (source_list),
        NULL);

    if (element_has_class (WEBKIT_DOM_ELEMENT (source_list), "-x-evo-indented"))
        element_add_class (WEBKIT_DOM_ELEMENT (new_list), "-x-evo-indented");

    prev_item = source_list;

    while (item) {
        WebKitDOMNode *next_item = webkit_dom_node_get_next_sibling (item);

        if (WEBKIT_DOM_IS_HTMLLI_ELEMENT (item)) {
            if (after)
                prev_item = webkit_dom_node_append_child (
                    source_list_clone, WEBKIT_DOM_NODE (item), NULL);
            else
                prev_item = webkit_dom_node_insert_before (
                    webkit_dom_node_get_parent_node (prev_item),
                    item,
                    webkit_dom_node_get_next_sibling (prev_item),
                    NULL);
        }

        if (webkit_dom_node_contains (item, WEBKIT_DOM_NODE (selection_end_marker)))
            after = TRUE;

        if (!next_item) {
            if (after)
                break;

            current_list = webkit_dom_node_get_next_sibling (current_list);
            next_item = webkit_dom_node_get_first_child (current_list);
        }
        item = next_item;
    }

    remove_node_if_empty (source_list_clone);
    remove_node_if_empty (source_list);
}

static void
unindent_block (EHTMLEditorSelection *selection,
                WebKitDOMDocument *document,
                WebKitDOMNode *block)
{
    gboolean before_node = TRUE;
    const gchar *align_value;
    gint word_wrap_length = selection->priv->word_wrap_length;
    gint level, width;
    EHTMLEditorSelectionAlignment alignment;
    WebKitDOMElement *element;
    WebKitDOMElement *prev_blockquote = NULL, *next_blockquote = NULL;
    WebKitDOMNode *block_to_process, *node_clone, *child;

    block_to_process = block;

    alignment = e_html_editor_selection_get_alignment_from_node (block_to_process);
    align_value = get_css_alignment_value (alignment);

    element = webkit_dom_node_get_parent_element (block_to_process);

    if (!WEBKIT_DOM_IS_HTML_QUOTE_ELEMENT (element))
        return;

    element_add_class (WEBKIT_DOM_ELEMENT (block_to_process), "-x-evo-to-unindent");

    level = get_indentation_level (element);
    width = word_wrap_length - SPACES_PER_INDENTATION * level;

    /* Look if we have previous siblings, if so, we have to
     * create new blockquote that will include them */
    if (webkit_dom_node_get_previous_sibling (block_to_process))
        prev_blockquote = e_html_editor_selection_get_indented_element (
            selection, document, width);

    /* Look if we have next siblings, if so, we have to
     * create new blockquote that will include them */
    if (webkit_dom_node_get_next_sibling (block_to_process))
        next_blockquote = e_html_editor_selection_get_indented_element (
            selection, document, width);

    /* Copy nodes that are before / after the element that we want to unindent */
    while ((child = webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (element)))) {
        if (webkit_dom_node_is_equal_node (child, block_to_process)) {
            before_node = FALSE;
            node_clone = webkit_dom_node_clone_node (child, TRUE);
            remove_node (child);
            continue;
        }

        webkit_dom_node_append_child (
            before_node ?
                WEBKIT_DOM_NODE (prev_blockquote) :
                WEBKIT_DOM_NODE (next_blockquote),
            child,
            NULL);
    }

    element_remove_class (WEBKIT_DOM_ELEMENT (node_clone), "-x-evo-to-unindent");

    /* Insert blockqoute with nodes that were before the element that we want to unindent */
    if (prev_blockquote) {
        if (webkit_dom_node_has_child_nodes (WEBKIT_DOM_NODE (prev_blockquote))) {
            webkit_dom_node_insert_before (
                webkit_dom_node_get_parent_node (WEBKIT_DOM_NODE (element)),
                WEBKIT_DOM_NODE (prev_blockquote),
                WEBKIT_DOM_NODE (element),
                NULL);
        }
    }

    if (level == 1 && element_has_class (WEBKIT_DOM_ELEMENT (node_clone), "-x-evo-paragraph"))
        e_html_editor_selection_set_paragraph_style (
            selection, WEBKIT_DOM_ELEMENT (node_clone), word_wrap_length, 0, align_value);

    /* Insert the unindented element */
    webkit_dom_node_insert_before (
        webkit_dom_node_get_parent_node (WEBKIT_DOM_NODE (element)),
        node_clone,
        WEBKIT_DOM_NODE (element),
        NULL);

    /* Insert blockqoute with nodes that were after the element that we want to unindent */
    if (next_blockquote) {
        if (webkit_dom_node_has_child_nodes (WEBKIT_DOM_NODE (next_blockquote))) {
            webkit_dom_node_insert_before (
                webkit_dom_node_get_parent_node (WEBKIT_DOM_NODE (element)),
                WEBKIT_DOM_NODE (next_blockquote),
                WEBKIT_DOM_NODE (element),
                NULL);
        }
    }

    /* Remove old blockquote */
    remove_node (WEBKIT_DOM_NODE (element));
}

/**
 * e_html_editor_selection_unindent:
 * @selection: an #EHTMLEditorSelection
 *
 * Unindents current paragraph by one level.
 */
void
e_html_editor_selection_unindent (EHTMLEditorSelection *selection)
{
    EHTMLEditorView *view;
    gboolean after_selection_start = FALSE, after_selection_end = FALSE;
    WebKitDOMDocument *document;
    WebKitDOMElement *selection_start_marker, *selection_end_marker;
    WebKitDOMNode *block;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));

    e_html_editor_selection_save (selection);

    selection_start_marker = webkit_dom_document_query_selector (
        document, "span#-x-evo-selection-start-marker", NULL);
    selection_end_marker = webkit_dom_document_query_selector (
        document, "span#-x-evo-selection-end-marker", NULL);

    /* If the selection was not saved, move it into the first child of body */
    if (!selection_start_marker || !selection_end_marker) {
        WebKitDOMHTMLElement *body;

        body = webkit_dom_document_get_body (document);
        selection_start_marker = webkit_dom_document_create_element (
            document, "SPAN", NULL);
        webkit_dom_element_set_id (
            selection_start_marker, "-x-evo-selection-start-marker");
        webkit_dom_node_insert_before (
            webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (body)),
            WEBKIT_DOM_NODE (selection_start_marker),
            webkit_dom_node_get_first_child (
                webkit_dom_node_get_first_child (
                    WEBKIT_DOM_NODE (body))),
            NULL);
        selection_end_marker = webkit_dom_document_create_element (
            document, "SPAN", NULL);
        webkit_dom_element_set_id (
            selection_end_marker, "-x-evo-selection-end-marker");
        webkit_dom_node_insert_before (
            webkit_dom_node_get_first_child (WEBKIT_DOM_NODE (body)),
            WEBKIT_DOM_NODE (selection_end_marker),
            webkit_dom_node_get_first_child (
                webkit_dom_node_get_first_child (
                    WEBKIT_DOM_NODE (body))),
            NULL);
    }

    block = get_parent_indented_block (
        WEBKIT_DOM_NODE (selection_start_marker));
    if (!block)
        block = get_parent_block_node_from_child (
            WEBKIT_DOM_NODE (selection_start_marker));

    while (block) {
        gint ii, length;
        WebKitDOMNode *next_block;
        WebKitDOMNodeList *list;

        next_block = webkit_dom_node_get_next_sibling (block);

        list = webkit_dom_element_query_selector_all (
            WEBKIT_DOM_ELEMENT (block),
            ".-x-evo-indented > *:not(.-x-evo-indented):not(li)",
            NULL);

        after_selection_end = webkit_dom_node_contains (
            block, WEBKIT_DOM_NODE (selection_end_marker));

        length = webkit_dom_node_list_get_length (list);
        if (length == 0 && node_is_list (block)) {
            unindent_list (selection, document);
            if (!after_selection_end) {
                block = next_block;
                continue;
            } else
                goto out;
        }

        if (length == 0) {
            if (!after_selection_start) {
                after_selection_start = webkit_dom_node_contains (
                    block, WEBKIT_DOM_NODE (selection_start_marker));
                if (!after_selection_start) {
                    block = next_block;
                    continue;
                }
            }

            unindent_block (selection, document, block);

            if (after_selection_end)
                goto out;
        }

        for (ii = 0; ii < length; ii++) {
            WebKitDOMNode *block_to_process;

            block_to_process = webkit_dom_node_list_item (list, ii);

            after_selection_end = webkit_dom_node_contains (
                block_to_process,
                WEBKIT_DOM_NODE (selection_end_marker));

            if (!after_selection_start) {
                after_selection_start = webkit_dom_node_contains (
                    block_to_process,
                    WEBKIT_DOM_NODE (selection_start_marker));
                if (!after_selection_start)
                    continue;
            }

            unindent_block (selection, document, block_to_process);

            if (after_selection_end)
                goto out;
        }
        block = next_block;
    }
 out:
    e_html_editor_selection_restore (selection);
    e_html_editor_view_force_spell_check (view);

    g_object_unref (view);

    g_object_notify (G_OBJECT (selection), "indented");
}

/**
 * e_html_editor_selection_is_bold:
 * @selection: an #EHTMLEditorSelection
 *
 * Returns whether current selection or letter at current cursor position
 * is bold.
 *
 * Returns @TRUE when selection is bold, @FALSE otherwise.
 */
gboolean
e_html_editor_selection_is_bold (EHTMLEditorSelection *selection)
{
    gboolean ret_val;
    gchar *value, *text_content;
    EHTMLEditorView *view;
    WebKitDOMCSSStyleDeclaration *style;
    WebKitDOMDocument *document;
    WebKitDOMDOMWindow *window;
    WebKitDOMNode *node;
    WebKitDOMElement *element;
    WebKitDOMRange *range;

    g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), FALSE);

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_val_if_fail (view != NULL, FALSE);

    if (!e_html_editor_view_get_html_mode (view)) {
        g_object_unref (view);
        return FALSE;
    }

    document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
    g_object_unref (view);
    window = webkit_dom_document_get_default_view (document);

    range = html_editor_selection_get_current_range (selection);
    if (!range)
        return FALSE;

    node = webkit_dom_range_get_common_ancestor_container (range, NULL);
    /* If we are changing the format of block we have to re-set bold property,
     * otherwise it will be turned off because of no text in composer */
    text_content = webkit_dom_node_get_text_content (node);
    if (g_strcmp0 (text_content, "") == 0) {
        g_free (text_content);
        return selection->priv->is_bold;
    }
    g_free (text_content);

    if (WEBKIT_DOM_IS_ELEMENT (node))
        element = WEBKIT_DOM_ELEMENT (node);
    else
        element = webkit_dom_node_get_parent_element (node);

    style = webkit_dom_dom_window_get_computed_style (window, element, NULL);
    value = webkit_dom_css_style_declaration_get_property_value (style, "font-weight");

    if (g_strstr_len (value, -1, "normal"))
        ret_val = FALSE;
    else
        ret_val = TRUE;

    g_free (value);
    return ret_val;
}

/**
 * e_html_editor_selection_set_bold:
 * @selection: an #EHTMLEditorSelection
 * @bold: @TRUE to enable bold, @FALSE to disable
 *
 * Toggles bold formatting of current selection or letter at current cursor
 * position, depending on whether @bold is @TRUE or @FALSE.
 */
void
e_html_editor_selection_set_bold (EHTMLEditorSelection *selection,
                                  gboolean bold)
{
    EHTMLEditorView *view;
    EHTMLEditorViewCommand command;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    if (e_html_editor_selection_is_bold (selection) == bold)
        return;

    selection->priv->is_bold = bold;

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    command = E_HTML_EDITOR_VIEW_COMMAND_BOLD;
    e_html_editor_view_exec_command (view, command, NULL);

    g_object_unref (view);

    g_object_notify (G_OBJECT (selection), "bold");
}

/**
 * e_html_editor_selection_is_italic:
 * @selection: an #EHTMLEditorSelection
 *
 * Returns whether current selection or letter at current cursor position
 * is italic.
 *
 * Returns @TRUE when selection is italic, @FALSE otherwise.
 */
gboolean
e_html_editor_selection_is_italic (EHTMLEditorSelection *selection)
{
    gboolean ret_val;
    gchar *value, *text_content;
    EHTMLEditorView *view;
    WebKitDOMCSSStyleDeclaration *style;
    WebKitDOMDocument *document;
    WebKitDOMDOMWindow *window;
    WebKitDOMNode *node;
    WebKitDOMElement *element;
    WebKitDOMRange *range;

    g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), FALSE);

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_val_if_fail (view != NULL, FALSE);

    if (!e_html_editor_view_get_html_mode (view)) {
        g_object_unref (view);
        return FALSE;
    }

    document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
    g_object_unref (view);
    window = webkit_dom_document_get_default_view (document);

    range = html_editor_selection_get_current_range (selection);
    if (!range)
        return FALSE;

    node = webkit_dom_range_get_common_ancestor_container (range, NULL);
    /* If we are changing the format of block we have to re-set italic property,
     * otherwise it will be turned off because of no text in composer */
    text_content = webkit_dom_node_get_text_content (node);
    if (g_strcmp0 (text_content, "") == 0) {
        g_free (text_content);
        return selection->priv->is_italic;
    }
    g_free (text_content);

    if (WEBKIT_DOM_IS_ELEMENT (node))
        element = WEBKIT_DOM_ELEMENT (node);
    else
        element = webkit_dom_node_get_parent_element (node);

    style = webkit_dom_dom_window_get_computed_style (window, element, NULL);
    value = webkit_dom_css_style_declaration_get_property_value (style, "font-style");

    if (g_strstr_len (value, -1, "italic"))
        ret_val = TRUE;
    else
        ret_val = FALSE;

    g_free (value);
    return ret_val;
}

/**
 * e_html_editor_selection_set_italic:
 * @selection: an #EHTMLEditorSelection
 * @italic: @TRUE to enable italic, @FALSE to disable
 *
 * Toggles italic formatting of current selection or letter at current cursor
 * position, depending on whether @italic is @TRUE or @FALSE.
 */
void
e_html_editor_selection_set_italic (EHTMLEditorSelection *selection,
                                    gboolean italic)
{
    EHTMLEditorView *view;
    EHTMLEditorViewCommand command;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    if (e_html_editor_selection_is_italic (selection) == italic)
        return;

    selection->priv->is_italic = italic;

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    command = E_HTML_EDITOR_VIEW_COMMAND_ITALIC;
    e_html_editor_view_exec_command (view, command, NULL);

    g_object_unref (view);

    g_object_notify (G_OBJECT (selection), "italic");
}

static gboolean
is_monospaced_element (WebKitDOMElement *element)
{
    gchar *value;
    gboolean ret_val = FALSE;

    if (!element)
        return FALSE;

    if (!WEBKIT_DOM_IS_HTML_FONT_ELEMENT (element))
        return FALSE;

    value = webkit_dom_element_get_attribute (element, "face");

    if (g_strcmp0 (value, "monospace") == 0)
        ret_val = TRUE;

    g_free (value);

    return ret_val;
}

/**
 * e_html_editor_selection_is_monospaced:
 * @selection: an #EHTMLEditorSelection
 *
 * Returns whether current selection or letter at current cursor position
 * is monospaced.
 *
 * Returns @TRUE when selection is monospaced, @FALSE otherwise.
 */
gboolean
e_html_editor_selection_is_monospaced (EHTMLEditorSelection *selection)
{
    gboolean ret_val;
    gchar *value, *text_content;
    EHTMLEditorView *view;
    WebKitDOMCSSStyleDeclaration *style;
    WebKitDOMDocument *document;
    WebKitDOMDOMWindow *window;
    WebKitDOMNode *node;
    WebKitDOMElement *element;
    WebKitDOMRange *range;

    g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), FALSE);

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_val_if_fail (view != NULL, FALSE);

    if (!e_html_editor_view_get_html_mode (view)) {
        g_object_unref (view);
        return FALSE;
    }

    document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
    g_object_unref (view);
    window = webkit_dom_document_get_default_view (document);

    range = html_editor_selection_get_current_range (selection);
    if (!range)
        return FALSE;

    node = webkit_dom_range_get_common_ancestor_container (range, NULL);
    /* If we are changing the format of block we have to re-set italic property,
     * otherwise it will be turned off because of no text in composer */
    text_content = webkit_dom_node_get_text_content (node);
    if (g_strcmp0 (text_content, "") == 0) {
        g_free (text_content);
        return selection->priv->is_monospaced;
    }
    g_free (text_content);

    if (WEBKIT_DOM_IS_ELEMENT (node))
        element = WEBKIT_DOM_ELEMENT (node);
    else
        element = webkit_dom_node_get_parent_element (node);

    style = webkit_dom_dom_window_get_computed_style (window, element, NULL);
    value = webkit_dom_css_style_declaration_get_property_value (style, "font-family");

    if (g_strstr_len (value, -1, "monospace"))
        ret_val = TRUE;
    else
        ret_val = FALSE;

    g_free (value);
    return ret_val;
}

static void
move_caret_into_element (WebKitDOMDocument *document,
             WebKitDOMElement *element)
{
    WebKitDOMDOMWindow *window;
    WebKitDOMDOMSelection *window_selection;
    WebKitDOMRange *new_range;

    if (!element)
        return;

    window = webkit_dom_document_get_default_view (document);
    window_selection = webkit_dom_dom_window_get_selection (window);
    new_range = webkit_dom_document_create_range (document);

    webkit_dom_range_select_node_contents (
            new_range, WEBKIT_DOM_NODE (element), NULL);
    webkit_dom_range_collapse (new_range, FALSE, NULL);
    webkit_dom_dom_selection_remove_all_ranges (window_selection);
    webkit_dom_dom_selection_add_range (window_selection, new_range);
}

/**
 * e_html_editor_selection_set_monospaced:
 * @selection: an #EHTMLEditorSelection
 * @monospaced: @TRUE to enable monospaced, @FALSE to disable
 *
 * Toggles monospaced formatting of current selection or letter at current cursor
 * position, depending on whether @monospaced is @TRUE or @FALSE.
 */
void
e_html_editor_selection_set_monospaced (EHTMLEditorSelection *selection,
                                        gboolean monospaced)
{
    EHTMLEditorView *view;
    WebKitWebView *web_view;
    WebKitDOMDocument *document;
    WebKitDOMRange *range;
    WebKitDOMDOMWindow *window;
    WebKitDOMDOMSelection *window_selection;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    if (e_html_editor_selection_is_monospaced (selection) == monospaced)
        return;

    selection->priv->is_monospaced = monospaced;

    range = html_editor_selection_get_current_range (selection);
    if (!range)
        return;

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    web_view = WEBKIT_WEB_VIEW (view);

    document = webkit_web_view_get_dom_document (web_view);
    window = webkit_dom_document_get_default_view (document);
    window_selection = webkit_dom_dom_window_get_selection (window);

    if (monospaced) {
        gchar *font_size_str;
        guint font_size;
        WebKitDOMElement *monospace;

        monospace = webkit_dom_document_create_element (
            document, "font", NULL);
        webkit_dom_element_set_attribute (
            monospace, "face", "monospace", NULL);

        font_size = selection->priv->font_size;
        if (font_size == 0)
            font_size = E_HTML_EDITOR_SELECTION_FONT_SIZE_NORMAL;
        font_size_str = g_strdup_printf ("%d", font_size);
        webkit_dom_element_set_attribute (
            monospace, "size", font_size_str, NULL);
        g_free (font_size_str);

        if (g_strcmp0 (e_html_editor_selection_get_string (selection), "") != 0) {
            gchar *html, *outer_html;

            webkit_dom_node_append_child (
                WEBKIT_DOM_NODE (monospace),
                WEBKIT_DOM_NODE (
                    webkit_dom_range_clone_contents (range, NULL)),
                NULL);

            outer_html = webkit_dom_html_element_get_outer_html (
                WEBKIT_DOM_HTML_ELEMENT (monospace));

            html = g_strconcat (
                /* Mark selection for restoration */
                "<span id=\"-x-evo-selection-start-marker\"></span>",
                outer_html,
                "<span id=\"-x-evo-selection-end-marker\"></span>",
                NULL),

            e_html_editor_selection_insert_html (selection, html);

            e_html_editor_selection_restore (selection);

            g_free (html);
            g_free (outer_html);
        } else {
            /* https://bugs.webkit.org/show_bug.cgi?id=15256 */
            webkit_dom_html_element_set_inner_html (
                WEBKIT_DOM_HTML_ELEMENT (monospace),
                UNICODE_ZERO_WIDTH_SPACE,
                NULL);
            webkit_dom_range_insert_node (
                range, WEBKIT_DOM_NODE (monospace), NULL);

            move_caret_into_element (document, monospace);
        }
    } else {
        gboolean is_bold, is_italic, is_underline, is_strikethrough;
        guint font_size;
        WebKitDOMElement *tt_element;
        WebKitDOMNode *node;

        node = webkit_dom_range_get_end_container (range, NULL);
        if (WEBKIT_DOM_IS_ELEMENT (node) &&
            is_monospaced_element (WEBKIT_DOM_ELEMENT (node))) {
            tt_element = WEBKIT_DOM_ELEMENT (node);
        } else {
            tt_element = e_html_editor_dom_node_find_parent_element (node, "FONT");

            if (!is_monospaced_element (tt_element)) {
                g_object_unref (view);
                return;
            }
        }

        /* Save current formatting */
        is_bold = selection->priv->is_bold;
        is_italic = selection->priv->is_italic;
        is_underline = selection->priv->is_underline;
        is_strikethrough = selection->priv->is_strikethrough;
        font_size = selection->priv->font_size;
        if (font_size == 0)
            font_size = E_HTML_EDITOR_SELECTION_FONT_SIZE_NORMAL;

        if (g_strcmp0 (e_html_editor_selection_get_string (selection), "") != 0) {
            gchar *html, *outer_html, *inner_html, *beginning, *end;
            gchar *start_position, *end_position, *font_size_str;
            WebKitDOMElement *wrapper;

            wrapper = webkit_dom_document_create_element (
                document, "SPAN", NULL);
            webkit_dom_element_set_id (wrapper, "-x-evo-remove-tt");
            webkit_dom_range_surround_contents (
                range, WEBKIT_DOM_NODE (wrapper), NULL);

            html = webkit_dom_html_element_get_outer_html (
                WEBKIT_DOM_HTML_ELEMENT (tt_element));

            start_position = g_strstr_len (
                html, -1, "<span id=\"-x-evo-remove-tt\"");
            end_position = g_strstr_len (start_position, -1, "</span>");

            beginning = g_utf8_substring (
                html, 0, g_utf8_pointer_to_offset (html, start_position));
            inner_html = webkit_dom_html_element_get_inner_html (
                WEBKIT_DOM_HTML_ELEMENT (wrapper));
            end = g_utf8_substring (
                html,
                g_utf8_pointer_to_offset (html, end_position) + 7,
                g_utf8_strlen (html, -1)),

            font_size_str = g_strdup_printf ("%d", font_size);

            outer_html =
                g_strconcat (
                    /* Beginning */
                    beginning,
                    /* End the previous FONT tag */
                    "</font>",
                    /* Mark selection for restoration */
                    "<span id=\"-x-evo-selection-start-marker\"></span>",
                    /* Inside will be the same */
                    inner_html,
                    "<span id=\"-x-evo-selection-end-marker\"></span>",
                    /* Start the new FONT element */
                    "<font face=\"monospace\" size=\"",
                    font_size_str,
                    "\">",
                    /* End - we have to start after </span> */
                    end,
                    NULL),

            g_free (font_size_str);

            webkit_dom_html_element_set_outer_html (
                WEBKIT_DOM_HTML_ELEMENT (tt_element),
                outer_html,
                NULL);

            e_html_editor_selection_restore (selection);

            g_free (html);
            g_free (outer_html);
            g_free (inner_html);
            g_free (beginning);
            g_free (end);
        } else {
            WebKitDOMRange *new_range;
            gchar *outer_html;
            gchar *tmp;

            webkit_dom_element_set_id (tt_element, "ev-tt");

                outer_html = webkit_dom_html_element_get_outer_html (
                WEBKIT_DOM_HTML_ELEMENT (tt_element));
            tmp = g_strconcat (outer_html, UNICODE_ZERO_WIDTH_SPACE, NULL);
            webkit_dom_html_element_set_outer_html (
                WEBKIT_DOM_HTML_ELEMENT (tt_element),
                tmp, NULL);

            /* We need to get that element again */
            tt_element = webkit_dom_document_get_element_by_id (
                document, "ev-tt");
            webkit_dom_element_remove_attribute (
                WEBKIT_DOM_ELEMENT (tt_element), "id");

            new_range = webkit_dom_document_create_range (document);
            webkit_dom_range_set_start_after (
                new_range, WEBKIT_DOM_NODE (tt_element), NULL);
            webkit_dom_range_set_end_after (
                new_range, WEBKIT_DOM_NODE (tt_element), NULL);

            webkit_dom_dom_selection_remove_all_ranges (
                window_selection);
            webkit_dom_dom_selection_add_range (
                window_selection, new_range);

            webkit_dom_dom_selection_modify (
                window_selection, "move", "right", "character");

            g_free (outer_html);
            g_free (tmp);

            e_html_editor_view_force_spell_check_for_current_paragraph (
                view);
        }

        /* Re-set formatting */
        if (is_bold)
            e_html_editor_selection_set_bold (selection, TRUE);
        if (is_italic)
            e_html_editor_selection_set_italic (selection, TRUE);
        if (is_underline)
            e_html_editor_selection_set_underline (selection, TRUE);
        if (is_strikethrough)
            e_html_editor_selection_set_strikethrough (selection, TRUE);

        e_html_editor_selection_set_font_size (selection, font_size);
    }

    g_object_unref (view);

    g_object_notify (G_OBJECT (selection), "monospaced");
}

/**
 * e_html_editor_selection_is_strikethrough:
 * @selection: an #EHTMLEditorSelection
 *
 * Returns whether current selection or letter at current cursor position
 * is striked through.
 *
 * Returns @TRUE when selection is striked through, @FALSE otherwise.
 */
gboolean
e_html_editor_selection_is_strikethrough (EHTMLEditorSelection *selection)
{
    gboolean ret_val;
    gchar *value, *text_content;
    EHTMLEditorView *view;
    WebKitDOMCSSStyleDeclaration *style;
    WebKitDOMDocument *document;
    WebKitDOMDOMWindow *window;
    WebKitDOMNode *node;
    WebKitDOMElement *element;
    WebKitDOMRange *range;

    g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), FALSE);

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_val_if_fail (view != NULL, FALSE);

    if (!e_html_editor_view_get_html_mode (view)) {
        g_object_unref (view);
        return FALSE;
    }

    document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
    g_object_unref (view);
    window = webkit_dom_document_get_default_view (document);

    range = html_editor_selection_get_current_range (selection);
    if (!range)
        return FALSE;

    node = webkit_dom_range_get_common_ancestor_container (range, NULL);
    /* If we are changing the format of block we have to re-set strikethrough property,
     * otherwise it will be turned off because of no text in composer */
    text_content = webkit_dom_node_get_text_content (node);
    if (g_strcmp0 (text_content, "") == 0) {
        g_free (text_content);
        return selection->priv->is_strikethrough;
    }
    g_free (text_content);

    if (WEBKIT_DOM_IS_ELEMENT (node))
        element = WEBKIT_DOM_ELEMENT (node);
    else
        element = webkit_dom_node_get_parent_element (node);

    style = webkit_dom_dom_window_get_computed_style (window, element, NULL);
    value = webkit_dom_css_style_declaration_get_property_value (style, "text-decoration");

    if (g_strstr_len (value, -1, "line-through"))
        ret_val = TRUE;
    else
        ret_val = get_has_style (selection, "strike");

    g_free (value);
    return ret_val;
}

/**
 * e_html_editor_selection_set_strikethrough:
 * @selection: an #EHTMLEditorSelection
 * @strikethrough: @TRUE to enable strikethrough, @FALSE to disable
 *
 * Toggles strike through formatting of current selection or letter at current
 * cursor position, depending on whether @strikethrough is @TRUE or @FALSE.
 */
void
e_html_editor_selection_set_strikethrough (EHTMLEditorSelection *selection,
                                           gboolean strikethrough)
{
    EHTMLEditorView *view;
    EHTMLEditorViewCommand command;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    if (e_html_editor_selection_is_strikethrough (selection) == strikethrough)
        return;

    selection->priv->is_strikethrough = strikethrough;

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    command = E_HTML_EDITOR_VIEW_COMMAND_STRIKETHROUGH;
    e_html_editor_view_exec_command (view, command, NULL);

    g_object_unref (view);

    g_object_notify (G_OBJECT (selection), "strikethrough");
}

/**
 * e_html_editor_selection_is_subscript:
 * @selection: an #EHTMLEditorSelection
 *
 * Returns whether current selection or letter at current cursor position
 * is in subscript.
 *
 * Returns @TRUE when selection is in subscript, @FALSE otherwise.
 */
gboolean
e_html_editor_selection_is_subscript (EHTMLEditorSelection *selection)
{
    EHTMLEditorView *view;
    WebKitDOMNode *node;
    WebKitDOMRange *range;

    g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), FALSE);

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_val_if_fail (view != NULL, FALSE);

    if (!e_html_editor_view_get_html_mode (view)) {
        g_object_unref (view);
        return FALSE;
    }

    g_object_unref (view);

    range = html_editor_selection_get_current_range (selection);
    node = webkit_dom_range_get_common_ancestor_container (range, NULL);

    while (node) {
        gchar *tag_name;

        tag_name = webkit_dom_element_get_tag_name (WEBKIT_DOM_ELEMENT (node));

        if (g_ascii_strncasecmp (tag_name, "sub", 3) == 0) {
            g_free (tag_name);
            break;
        }

        g_free (tag_name);
        node = webkit_dom_node_get_parent_node (node);
    }

    return (node != NULL);
}

/**
 * e_html_editor_selection_set_subscript:
 * @selection: an #EHTMLEditorSelection
 * @subscript: @TRUE to enable subscript, @FALSE to disable
 *
 * Toggles subscript of current selection or letter at current cursor position,
 * depending on whether @subscript is @TRUE or @FALSE.
 */
void
e_html_editor_selection_set_subscript (EHTMLEditorSelection *selection,
                                       gboolean subscript)
{
    EHTMLEditorView *view;
    EHTMLEditorViewCommand command;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    if (e_html_editor_selection_is_subscript (selection) == subscript)
        return;

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    command = E_HTML_EDITOR_VIEW_COMMAND_SUBSCRIPT;
    e_html_editor_view_exec_command (view, command, NULL);

    g_object_unref (view);

    g_object_notify (G_OBJECT (selection), "subscript");
}

/**
 * e_html_editor_selection_is_superscript:
 * @selection: an #EHTMLEditorSelection
 *
 * Returns whether current selection or letter at current cursor position
 * is in superscript.
 *
 * Returns @TRUE when selection is in superscript, @FALSE otherwise.
 */
gboolean
e_html_editor_selection_is_superscript (EHTMLEditorSelection *selection)
{
    EHTMLEditorView *view;
    WebKitDOMNode *node;
    WebKitDOMRange *range;

    g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), FALSE);

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_val_if_fail (view != NULL, FALSE);

    if (!e_html_editor_view_get_html_mode (view)) {
        g_object_unref (view);
        return FALSE;
    }

    g_object_unref (view);

    range = html_editor_selection_get_current_range (selection);
    node = webkit_dom_range_get_common_ancestor_container (range, NULL);

    while (node) {
        gchar *tag_name;

        tag_name = webkit_dom_element_get_tag_name (WEBKIT_DOM_ELEMENT (node));

        if (g_ascii_strncasecmp (tag_name, "sup", 3) == 0) {
            g_free (tag_name);
            break;
        }

        g_free (tag_name);
        node = webkit_dom_node_get_parent_node (node);
    }

    return (node != NULL);
}

/**
 * e_html_editor_selection_set_superscript:
 * @selection: an #EHTMLEditorSelection
 * @superscript: @TRUE to enable superscript, @FALSE to disable
 *
 * Toggles superscript of current selection or letter at current cursor position,
 * depending on whether @superscript is @TRUE or @FALSE.
 */
void
e_html_editor_selection_set_superscript (EHTMLEditorSelection *selection,
                                         gboolean superscript)
{
    EHTMLEditorView *view;
    EHTMLEditorViewCommand command;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    if (e_html_editor_selection_is_superscript (selection) == superscript)
        return;

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    command = E_HTML_EDITOR_VIEW_COMMAND_SUPERSCRIPT;
    e_html_editor_view_exec_command (view, command, NULL);

    g_object_unref (view);

    g_object_notify (G_OBJECT (selection), "superscript");
}

/**
 * e_html_editor_selection_is_underline:
 * @selection: an #EHTMLEditorSelection
 *
 * Returns whether current selection or letter at current cursor position
 * is underlined.
 *
 * Returns @TRUE when selection is underlined, @FALSE otherwise.
 */
gboolean
e_html_editor_selection_is_underline (EHTMLEditorSelection *selection)
{
    gboolean ret_val;
    gchar *value, *text_content;
    EHTMLEditorView *view;
    WebKitDOMCSSStyleDeclaration *style;
    WebKitDOMDocument *document;
    WebKitDOMDOMWindow *window;
    WebKitDOMNode *node;
    WebKitDOMElement *element;
    WebKitDOMRange *range;

    g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), FALSE);

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_val_if_fail (view != NULL, FALSE);

    if (!e_html_editor_view_get_html_mode (view)) {
        g_object_unref (view);
        return FALSE;
    }

    document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
    g_object_unref (view);
    window = webkit_dom_document_get_default_view (document);

    range = html_editor_selection_get_current_range (selection);
    if (!range)
        return FALSE;

    node = webkit_dom_range_get_common_ancestor_container (range, NULL);
    /* If we are changing the format of block we have to re-set underline property,
     * otherwise it will be turned off because of no text in composer */
    text_content = webkit_dom_node_get_text_content (node);
    if (g_strcmp0 (text_content, "") == 0) {
        g_free (text_content);
        return selection->priv->is_underline;
    }
    g_free (text_content);

    if (WEBKIT_DOM_IS_ELEMENT (node))
        element = WEBKIT_DOM_ELEMENT (node);
    else
        element = webkit_dom_node_get_parent_element (node);

    style = webkit_dom_dom_window_get_computed_style (window, element, NULL);
    value = webkit_dom_css_style_declaration_get_property_value (style, "text-decoration");

    if (g_strstr_len (value, -1, "underline"))
        ret_val = TRUE;
    else
        ret_val = get_has_style (selection, "u");

    g_free (value);
    return ret_val;
}

/**
 * e_html_editor_selection_set_underline:
 * @selection: an #EHTMLEditorSelection
 * @underline: @TRUE to enable underline, @FALSE to disable
 *
 * Toggles underline formatting of current selection or letter at current
 * cursor position, depending on whether @underline is @TRUE or @FALSE.
 */
void
e_html_editor_selection_set_underline (EHTMLEditorSelection *selection,
                                       gboolean underline)
{
    EHTMLEditorView *view;
    EHTMLEditorViewCommand command;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    if (e_html_editor_selection_is_underline (selection) == underline)
        return;

    selection->priv->is_underline = underline;

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    command = E_HTML_EDITOR_VIEW_COMMAND_UNDERLINE;
    e_html_editor_view_exec_command (view, command, NULL);

    g_object_unref (view);

    g_object_notify (G_OBJECT (selection), "underline");
}

/**
 * e_html_editor_selection_unlink:
 * @selection: an #EHTMLEditorSelection
 *
 * Removes any links (&lt;A&gt; elements) from current selection or at current
 * cursor position.
 */
void
e_html_editor_selection_unlink (EHTMLEditorSelection *selection)
{
    EHTMLEditorView *view;
    EHTMLEditorViewCommand command;
    WebKitDOMDocument *document;
    WebKitDOMDOMWindow *window;
    WebKitDOMDOMSelection *dom_selection;
    WebKitDOMRange *range;
    WebKitDOMElement *link;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
    window = webkit_dom_document_get_default_view (document);
    dom_selection = webkit_dom_dom_window_get_selection (window);

    range = webkit_dom_dom_selection_get_range_at (dom_selection, 0, NULL);
    link = e_html_editor_dom_node_find_parent_element (
            webkit_dom_range_get_start_container (range, NULL), "A");

    if (!link) {
        gchar *text;
        /* get element that was clicked on */
        link = e_html_editor_view_get_element_under_mouse_click (view);
        if (!WEBKIT_DOM_IS_HTML_ANCHOR_ELEMENT (link))
            link = NULL;

        text = webkit_dom_html_element_get_inner_text (
                WEBKIT_DOM_HTML_ELEMENT (link));
        webkit_dom_html_element_set_outer_html (WEBKIT_DOM_HTML_ELEMENT (link), text, NULL);
        g_free (text);
    } else {
        command = E_HTML_EDITOR_VIEW_COMMAND_UNLINK;
        e_html_editor_view_exec_command (view, command, NULL);
    }
    g_object_unref (view);
}

/**
 * e_html_editor_selection_create_link:
 * @selection: an #EHTMLEditorSelection
 * @uri: destination of the new link
 *
 * Converts current selection into a link pointing to @url.
 */
void
e_html_editor_selection_create_link (EHTMLEditorSelection *selection,
                                     const gchar *uri)
{
    EHTMLEditorView *view;
    EHTMLEditorViewCommand command;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));
    g_return_if_fail (uri != NULL && *uri != '\0');

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    command = E_HTML_EDITOR_VIEW_COMMAND_CREATE_LINK;
    e_html_editor_view_exec_command (view, command, uri);

    g_object_unref (view);
}

/**
 * e_html_editor_selection_insert_text:
 * @selection: an #EHTMLEditorSelection
 * @plain_text: text to insert
 *
 * Inserts @plain_text at current cursor position. When a text range is selected,
 * it will be replaced by @plain_text.
 */
void
e_html_editor_selection_insert_text (EHTMLEditorSelection *selection,
                                     const gchar *plain_text)
{
    EHTMLEditorView *view;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));
    g_return_if_fail (plain_text != NULL);

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    e_html_editor_view_convert_and_insert_plain_text (view, plain_text);

    g_object_unref (view);
}

/**
 * e_html_editor_selection_insert_html:
 * @selection: an #EHTMLEditorSelection
 * @html_text: an HTML code to insert
 *
 * Insert @html_text into document at current cursor position. When a text range
 * is selected, it will be replaced by @html_text.
 */
void
e_html_editor_selection_insert_html (EHTMLEditorSelection *selection,
                                     const gchar *html_text)
{
    EHTMLEditorView *view;
    EHTMLEditorViewCommand command;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));
    g_return_if_fail (html_text != NULL);

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    command = E_HTML_EDITOR_VIEW_COMMAND_INSERT_HTML;
    if (e_html_editor_view_get_html_mode (view)) {
        e_html_editor_view_exec_command (view, command, html_text);
        e_html_editor_view_check_magic_links (view, FALSE);
        e_html_editor_view_force_spell_check (view);

        e_html_editor_selection_scroll_to_caret (selection);
    } else
        e_html_editor_view_convert_and_insert_html_to_plain_text (
            view, html_text);

    g_object_unref (view);
}

void
e_html_editor_selection_insert_as_text (EHTMLEditorSelection *selection,
                                        const gchar *html_text)
{
    EHTMLEditorView *view;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));
    g_return_if_fail (html_text != NULL);

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    e_html_editor_view_convert_and_insert_html_to_plain_text (view, html_text);

    g_object_unref (view);
}

/************************* image_load_and_insert_async() *************************/

typedef struct _LoadContext LoadContext;

struct _LoadContext {
    EHTMLEditorSelection *selection;
    WebKitDOMElement *element;
    GInputStream *input_stream;
    GOutputStream *output_stream;
    GFile *file;
    GFileInfo *file_info;
    goffset total_num_bytes;
    gssize bytes_read;
    const gchar *content_type;
    const gchar *filename;
    gchar buffer[4096];
};

/* Forward Declaration */
static void
image_load_stream_read_cb (GInputStream *input_stream,
                           GAsyncResult *result,
                           LoadContext *load_context);

static LoadContext *
image_load_context_new (EHTMLEditorSelection *selection)
{
    LoadContext *load_context;

    load_context = g_slice_new0 (LoadContext);
    load_context->selection = selection;

    return load_context;
}

static void
image_load_context_free (LoadContext *load_context)
{
    if (load_context->input_stream != NULL)
        g_object_unref (load_context->input_stream);

    if (load_context->output_stream != NULL)
        g_object_unref (load_context->output_stream);

    if (load_context->file_info != NULL)
        g_object_unref (load_context->file_info);

    if (load_context->file != NULL)
        g_object_unref (load_context->file);

    g_slice_free (LoadContext, load_context);
}

static void
replace_base64_image_src (EHTMLEditorSelection *selection,
                          WebKitDOMElement *element,
                          const gchar *base64_content,
                          const gchar *filename,
                          const gchar *uri)
{
    EHTMLEditorView *view;

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    e_html_editor_view_set_changed (view, TRUE);
    g_object_unref (view);

    if (WEBKIT_DOM_IS_HTML_IMAGE_ELEMENT (element))
        webkit_dom_html_image_element_set_src (
            WEBKIT_DOM_HTML_IMAGE_ELEMENT (element),
            base64_content);
    else
        webkit_dom_element_set_attribute (
            WEBKIT_DOM_ELEMENT (element),
            "background",
            base64_content,
            NULL);
    webkit_dom_element_set_attribute (
        WEBKIT_DOM_ELEMENT (element), "data-uri", uri, NULL);
    webkit_dom_element_set_attribute (
        WEBKIT_DOM_ELEMENT (element), "data-inline", "", NULL);
    webkit_dom_element_set_attribute (
        WEBKIT_DOM_ELEMENT (element), "data-name",
        filename ? filename : "", NULL);
}

static void
insert_base64_image (EHTMLEditorSelection *selection,
                     const gchar *base64_content,
                     const gchar *filename,
                     const gchar *uri)
{
    EHTMLEditorView *view;
    WebKitDOMDocument *document;
    WebKitDOMElement *element, *caret_position, *resizable_wrapper;
    WebKitDOMText *text;

    caret_position = e_html_editor_selection_save_caret_position (selection);

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    document = webkit_web_view_get_dom_document (
        WEBKIT_WEB_VIEW (view));

    e_html_editor_view_set_changed (view, TRUE);
    g_object_unref (view);

    resizable_wrapper =
        webkit_dom_document_create_element (document, "span", NULL);
    webkit_dom_element_set_attribute (
        resizable_wrapper, "class", "-x-evo-resizable-wrapper", NULL);

    element = webkit_dom_document_create_element (document, "img", NULL);
    webkit_dom_html_image_element_set_src (
        WEBKIT_DOM_HTML_IMAGE_ELEMENT (element),
        base64_content);
    webkit_dom_element_set_attribute (
        WEBKIT_DOM_ELEMENT (element), "data-uri", uri, NULL);
    webkit_dom_element_set_attribute (
        WEBKIT_DOM_ELEMENT (element), "data-inline", "", NULL);
    webkit_dom_element_set_attribute (
        WEBKIT_DOM_ELEMENT (element), "data-name",
        filename ? filename : "", NULL);
    webkit_dom_node_append_child (
        WEBKIT_DOM_NODE (resizable_wrapper),
        WEBKIT_DOM_NODE (element),
        NULL);

    webkit_dom_node_insert_before (
        webkit_dom_node_get_parent_node (
            WEBKIT_DOM_NODE (caret_position)),
        WEBKIT_DOM_NODE (resizable_wrapper),
        WEBKIT_DOM_NODE (caret_position),
        NULL);

    /* We have to again use UNICODE_ZERO_WIDTH_SPACE character to restore
     * caret on right position */
    text = webkit_dom_document_create_text_node (
        document, UNICODE_ZERO_WIDTH_SPACE);

    webkit_dom_node_insert_before (
        webkit_dom_node_get_parent_node (
            WEBKIT_DOM_NODE (caret_position)),
        WEBKIT_DOM_NODE (text),
        WEBKIT_DOM_NODE (caret_position),
        NULL);

    e_html_editor_selection_restore_caret_position (selection);
}

static void
image_load_finish (LoadContext *load_context)
{
    EHTMLEditorSelection *selection;
    GMemoryOutputStream *output_stream;
    gchar *base64_encoded, *mime_type, *output, *uri;
    gsize size;
    gpointer data;

    output_stream = G_MEMORY_OUTPUT_STREAM (load_context->output_stream);

    selection = load_context->selection;

    mime_type = g_content_type_get_mime_type (load_context->content_type);

    data = g_memory_output_stream_get_data (output_stream);
    size = g_memory_output_stream_get_data_size (output_stream);
    uri = g_file_get_uri (load_context->file);

    base64_encoded = g_base64_encode ((const guchar *) data, size);
    output = g_strconcat ("data:", mime_type, ";base64,", base64_encoded, NULL);
    if (load_context->element)
        replace_base64_image_src (
            selection, load_context->element, output, load_context->filename, uri);
    else
        insert_base64_image (selection, output, load_context->filename, uri);

    g_free (base64_encoded);
    g_free (output);
    g_free (mime_type);
    g_free (uri);

    image_load_context_free (load_context);
}

static void
image_load_write_cb (GOutputStream *output_stream,
                     GAsyncResult *result,
                     LoadContext *load_context)
{
    GInputStream *input_stream;
    gssize bytes_written;
    GError *error = NULL;

    bytes_written = g_output_stream_write_finish (
        output_stream, result, &error);

    if (error) {
        image_load_context_free (load_context);
        return;
    }

    input_stream = load_context->input_stream;

    if (bytes_written < load_context->bytes_read) {
        g_memmove (
            load_context->buffer,
            load_context->buffer + bytes_written,
            load_context->bytes_read - bytes_written);
        load_context->bytes_read -= bytes_written;

        g_output_stream_write_async (
            output_stream,
            load_context->buffer,
            load_context->bytes_read,
            G_PRIORITY_DEFAULT, NULL,
            (GAsyncReadyCallback) image_load_write_cb,
            load_context);
    } else
        g_input_stream_read_async (
            input_stream,
            load_context->buffer,
            sizeof (load_context->buffer),
            G_PRIORITY_DEFAULT, NULL,
            (GAsyncReadyCallback) image_load_stream_read_cb,
            load_context);
}

static void
image_load_stream_read_cb (GInputStream *input_stream,
                           GAsyncResult *result,
                           LoadContext *load_context)
{
    GOutputStream *output_stream;
    gssize bytes_read;
    GError *error = NULL;

    bytes_read = g_input_stream_read_finish (
        input_stream, result, &error);

    if (error) {
        image_load_context_free (load_context);
        return;
    }

    if (bytes_read == 0) {
        image_load_finish (load_context);
        return;
    }

    output_stream = load_context->output_stream;
    load_context->bytes_read = bytes_read;

    g_output_stream_write_async (
        output_stream,
        load_context->buffer,
        load_context->bytes_read,
        G_PRIORITY_DEFAULT, NULL,
        (GAsyncReadyCallback) image_load_write_cb,
        load_context);
}

static void
image_load_file_read_cb (GFile *file,
                         GAsyncResult *result,
                         LoadContext *load_context)
{
    GFileInputStream *input_stream;
    GOutputStream *output_stream;
    GError *error = NULL;

    /* Input stream might be NULL, so don't use cast macro. */
    input_stream = g_file_read_finish (file, result, &error);
    load_context->input_stream = (GInputStream *) input_stream;

    if (error) {
        image_load_context_free (load_context);
        return;
    }

    /* Load the contents into a GMemoryOutputStream. */
    output_stream = g_memory_output_stream_new (
        NULL, 0, g_realloc, g_free);

    load_context->output_stream = output_stream;

    g_input_stream_read_async (
        load_context->input_stream,
        load_context->buffer,
        sizeof (load_context->buffer),
        G_PRIORITY_DEFAULT, NULL,
        (GAsyncReadyCallback) image_load_stream_read_cb,
        load_context);
}

static void
image_load_query_info_cb (GFile *file,
                          GAsyncResult *result,
                          LoadContext *load_context)
{
    GFileInfo *file_info;
    GError *error = NULL;

    file_info = g_file_query_info_finish (file, result, &error);
    if (error) {
        image_load_context_free (load_context);
        return;
    }

    load_context->content_type = g_file_info_get_content_type (file_info);
    load_context->total_num_bytes = g_file_info_get_size (file_info);
    load_context->filename = g_file_info_get_name (file_info);

    g_file_read_async (
        file, G_PRIORITY_DEFAULT,
        NULL, (GAsyncReadyCallback)
        image_load_file_read_cb, load_context);
}

static void
image_load_and_insert_async (EHTMLEditorSelection *selection,
                             WebKitDOMElement *element,
                             const gchar *uri)
{
    LoadContext *load_context;
    GFile *file;

    g_return_if_fail (uri && *uri);

    file = g_file_new_for_uri (uri);
    g_return_if_fail (file != NULL);

    load_context = image_load_context_new (selection);
    load_context->file = file;
    load_context->element = element;

    g_file_query_info_async (
        file, "standard::*",
        G_FILE_QUERY_INFO_NONE,G_PRIORITY_DEFAULT,
        NULL, (GAsyncReadyCallback)
        image_load_query_info_cb, load_context);
}

/**
 * e_html_editor_selection_insert_image:
 * @selection: an #EHTMLEditorSelection
 * @image_uri: an URI of the source image
 *
 * Inserts image at current cursor position using @image_uri as source. When a
 * text range is selected, it will be replaced by the image.
 */
void
e_html_editor_selection_insert_image (EHTMLEditorSelection *selection,
                                      const gchar *image_uri)
{
    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));
    g_return_if_fail (image_uri != NULL);

    if (is_in_html_mode (selection)) {
        if (strstr (image_uri, ";base64,")) {
            if (g_str_has_prefix (image_uri, "data:"))
                insert_base64_image (selection, image_uri, "", "");
            if (strstr (image_uri, ";data")) {
                const gchar *base64_data = strstr (image_uri, ";") + 1;
                gchar *filename;
                glong filename_length;

                filename_length =
                    g_utf8_strlen (image_uri, -1) -
                    g_utf8_strlen (base64_data, -1) - 1;
                filename = g_strndup (image_uri, filename_length);

                insert_base64_image (selection, base64_data, filename, "");
                g_free (filename);
            }
        } else
            image_load_and_insert_async (selection, NULL, image_uri);
    }
}

/**
 * e_html_editor_selection_replace_image_src:
 * @selection: an #EHTMLEditorSelection
 * @element: #WebKitDOMElement element
 * @image_uri: an URI of the source image
 *
 * If given @element is image we will replace the src attribute of it with base64
 * data from given @image_uri. Otherwise we will set the base64 data to
 * the background attribute of given @element.
 */
void
e_html_editor_selection_replace_image_src (EHTMLEditorSelection *selection,
                                           WebKitDOMElement *element,
                                           const gchar *image_uri)
{
    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));
    g_return_if_fail (image_uri != NULL);
    g_return_if_fail (element && WEBKIT_DOM_IS_ELEMENT (element));

    if (strstr (image_uri, ";base64,")) {
        if (g_str_has_prefix (image_uri, "data:"))
            replace_base64_image_src (
                selection, element, image_uri, "", "");
        if (strstr (image_uri, ";data")) {
            const gchar *base64_data = strstr (image_uri, ";") + 1;
            gchar *filename;
            glong filename_length;

            filename_length =
                g_utf8_strlen (image_uri, -1) -
                g_utf8_strlen (base64_data, -1) - 1;
            filename = g_strndup (image_uri, filename_length);

            replace_base64_image_src (
                selection, element, base64_data, filename, "");
            g_free (filename);
        }
    } else
        image_load_and_insert_async (selection, element, image_uri);
}

/**
 * e_html_editor_selection_clear_caret_position_marker:
 * @selection: an #EHTMLEditorSelection
 *
 * Removes previously set caret position marker from composer.
 */
void
e_html_editor_selection_clear_caret_position_marker (EHTMLEditorSelection *selection)
{
    EHTMLEditorView *view;
    WebKitDOMDocument *document;
    WebKitDOMElement *element;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));

    element = webkit_dom_document_get_element_by_id (document, "-x-evo-caret-position");

    if (element)
        remove_node (WEBKIT_DOM_NODE (element));

    g_object_unref (view);
}

WebKitDOMNode *
e_html_editor_selection_get_caret_position_node (WebKitDOMDocument *document)
{
    WebKitDOMElement *element;

    element = webkit_dom_document_create_element (document, "SPAN", NULL);
    webkit_dom_element_set_id (element, "-x-evo-caret-position");
    webkit_dom_element_set_attribute (
        element, "style", "color: red", NULL);
    webkit_dom_html_element_set_inner_html (
        WEBKIT_DOM_HTML_ELEMENT (element), "*", NULL);

    return WEBKIT_DOM_NODE (element);
}

/**
 * e_html_editor_selection_save_caret_position:
 * @selection: an #EHTMLEditorSelection
 *
 * Saves current caret position in composer.
 *
 * Returns: #WebKitDOMElement that was created on caret position
 */
WebKitDOMElement *
e_html_editor_selection_save_caret_position (EHTMLEditorSelection *selection)
{
    EHTMLEditorView *view;
    WebKitDOMDocument *document;
    WebKitDOMNode *split_node;
    WebKitDOMNode *start_offset_node;
    WebKitDOMNode *caret_node;
    WebKitDOMRange *range;
    gulong start_offset;

    g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), NULL);

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_val_if_fail (view != NULL, NULL);

    document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
    g_object_unref (view);

    e_html_editor_selection_clear_caret_position_marker (selection);

    range = html_editor_selection_get_current_range (selection);
    if (!range)
        return NULL;

    start_offset = webkit_dom_range_get_start_offset (range, NULL);
    start_offset_node = webkit_dom_range_get_end_container (range, NULL);

    caret_node = e_html_editor_selection_get_caret_position_node (document);

    if (WEBKIT_DOM_IS_TEXT (start_offset_node) && start_offset != 0) {
        WebKitDOMText *split_text;

        split_text = webkit_dom_text_split_text (
                WEBKIT_DOM_TEXT (start_offset_node),
                start_offset, NULL);
        split_node = WEBKIT_DOM_NODE (split_text);
    } else {
        split_node = start_offset_node;
    }

    webkit_dom_node_insert_before (
        webkit_dom_node_get_parent_node (start_offset_node),
        caret_node,
        split_node,
        NULL);

    return WEBKIT_DOM_ELEMENT (caret_node);
}

static void
fix_quoting_nodes_after_caret_restoration (WebKitDOMDOMSelection *window_selection,
                                           WebKitDOMNode *prev_sibling,
                                           WebKitDOMNode *next_sibling)
{
    WebKitDOMNode *tmp_node;

    if (!element_has_class (WEBKIT_DOM_ELEMENT (prev_sibling), "-x-evo-temp-text-wrapper"))
        return;

    webkit_dom_dom_selection_modify (
        window_selection, "move", "forward", "character");
    tmp_node = webkit_dom_node_get_next_sibling (
        webkit_dom_node_get_first_child (prev_sibling));

    webkit_dom_node_insert_before (
        webkit_dom_node_get_parent_node (prev_sibling),
        tmp_node,
        next_sibling,
        NULL);

    tmp_node = webkit_dom_node_get_first_child (prev_sibling);

    webkit_dom_node_insert_before (
        webkit_dom_node_get_parent_node (prev_sibling),
        tmp_node,
        webkit_dom_node_get_previous_sibling (next_sibling),
        NULL);

    remove_node (prev_sibling);

    webkit_dom_dom_selection_modify (
        window_selection, "move", "backward", "character");
}

/**
 * e_html_editor_selection_restore_caret_position:
 * @selection: an #EHTMLEditorSelection
 *
 * Restores previously saved caret position in composer.
 */
void
e_html_editor_selection_restore_caret_position (EHTMLEditorSelection *selection)
{
    EHTMLEditorView *view;
    WebKitDOMDocument *document;
    WebKitDOMElement *element;
    gboolean fix_after_quoting;
    gboolean swap_direction = FALSE;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
    g_object_unref (view);

    element = webkit_dom_document_get_element_by_id (
        document, "-x-evo-caret-position");
    fix_after_quoting = element_has_class (element, "-x-evo-caret-quoting");

    if (element) {
        WebKitDOMDOMWindow *window;
        WebKitDOMNode *parent_node;
        WebKitDOMDOMSelection *window_selection;
        WebKitDOMNode *prev_sibling;
        WebKitDOMNode *next_sibling;

        if (!webkit_dom_node_get_previous_sibling (WEBKIT_DOM_NODE (element)))
            swap_direction = TRUE;

        window = webkit_dom_document_get_default_view (document);
        window_selection = webkit_dom_dom_window_get_selection (window);
        parent_node = webkit_dom_node_get_parent_node (WEBKIT_DOM_NODE (element));
        /* If parent is BODY element, we try to restore the position on the 
         * element that is next to us */
        if (WEBKIT_DOM_IS_HTML_BODY_ELEMENT (parent_node)) {
            /* Look if we have DIV on right */
            next_sibling = webkit_dom_node_get_next_sibling (
                WEBKIT_DOM_NODE (element));
            if (!WEBKIT_DOM_IS_ELEMENT (next_sibling)) {
                e_html_editor_selection_clear_caret_position_marker (selection);
                return;
            }

            if (element_has_class (WEBKIT_DOM_ELEMENT (next_sibling), "-x-evo-paragraph")) {
                remove_node (WEBKIT_DOM_NODE (element));

                move_caret_into_element (
                    document, WEBKIT_DOM_ELEMENT (next_sibling));

                goto out;
            }
        }

        move_caret_into_element (document, element);

        if (fix_after_quoting) {
            prev_sibling = webkit_dom_node_get_previous_sibling (
                WEBKIT_DOM_NODE (element));
            next_sibling = webkit_dom_node_get_next_sibling (
                WEBKIT_DOM_NODE (element));
            if (!next_sibling)
                fix_after_quoting = FALSE;
        }

        remove_node (WEBKIT_DOM_NODE (element));

        if (fix_after_quoting)
            fix_quoting_nodes_after_caret_restoration (
                window_selection, prev_sibling, next_sibling);
 out:
        /* FIXME If caret position is restored and afterwards the
         * position is saved it is not on the place where it supposed
         * to be (it is in the beginning of parent's element. It can
         * be avoided by moving with the caret. */
        if (swap_direction) {
            webkit_dom_dom_selection_modify (
                window_selection, "move", "forward", "character");
            webkit_dom_dom_selection_modify (
                window_selection, "move", "backward", "character");
        } else {
            webkit_dom_dom_selection_modify (
                window_selection, "move", "backward", "character");
            webkit_dom_dom_selection_modify (
                window_selection, "move", "forward", "character");
        }
    }
}

static gint
find_where_to_break_line (WebKitDOMNode *node,
                          gint max_len,
              gint word_wrap_length)
{
    gchar *str, *text_start;
    gunichar uc;
    gint pos;
    gint last_space = 0;
    gint length;
    gint ret_val = 0;
    gchar* position;

    text_start =  webkit_dom_character_data_get_data (WEBKIT_DOM_CHARACTER_DATA (node));
    length = g_utf8_strlen (text_start, -1);

    pos = 1;
    last_space = 0;
    str = text_start;
    do {
        uc = g_utf8_get_char (str);
        if (!uc) {
            g_free (text_start);
            if (pos <= max_len)
                return pos;
            else
                return last_space;
        }

        /* If last_space is zero then the word is longer than
         * word_wrap_length characters, so continue until we find
         * a space */
        if ((pos > max_len) && (last_space > 0)) {
            if (last_space > word_wrap_length) {
                g_free (text_start);
                return last_space - 1;
            }

            if (last_space > max_len) {
                if (g_unichar_isspace (g_utf8_get_char (text_start)))
                    ret_val = 1;

                g_free (text_start);
                return ret_val;
            }

            if (last_space == max_len - 1) {
                uc = g_utf8_get_char (str);
                if (g_unichar_isspace (uc))
                    last_space++;
            }

            g_free (text_start);
            return last_space;
        }

        if (g_unichar_isspace (uc))
            last_space = pos;

        pos += 1;
        str = g_utf8_next_char (str);
    } while (*str);

    position = g_utf8_offset_to_pointer (text_start, max_len);

    if (g_unichar_isspace (g_utf8_get_char (position))) {
        ret_val = max_len + 1;
    } else {
        if (last_space == 0) {
            /* If word is longer than word_wrap_length, we cannot wrap it */
            ret_val = length;
        } else if (last_space < max_len) {
            ret_val = last_space;
        } else {
            if (length > word_wrap_length)
                ret_val = last_space;
            else
                ret_val = 0;
        }
    }

    g_free (text_start);

    return ret_val;
}

static WebKitDOMElement *
wrap_lines (EHTMLEditorSelection *selection,
        WebKitDOMNode *paragraph,
        WebKitDOMDocument *document,
        gboolean remove_all_br,
        gint word_wrap_length)
{
    WebKitDOMNode *node, *start_node;
    WebKitDOMNode *paragraph_clone;
    WebKitDOMDocumentFragment *fragment;
    WebKitDOMElement *element;
    WebKitDOMNodeList *wrap_br;
    gint len, ii, br_count;
    gulong length_left;
    glong paragraph_char_count;
    gchar *text_content;

    if (selection) {
        paragraph_char_count = g_utf8_strlen (
            e_html_editor_selection_get_string (selection), -1);

        fragment = webkit_dom_range_clone_contents (
            html_editor_selection_get_current_range (selection), NULL);

        /* Select all BR elements or just ours that are used for wrapping.
         * We are not removing user BR elements when this function is activated
         * from Format->Wrap Lines action */
        wrap_br = webkit_dom_document_fragment_query_selector_all (
            fragment,
            remove_all_br ? "br" : "br.-x-evo-wrap-br",
            NULL);
    } else {
        WebKitDOMElement *caret_node;

        if (!webkit_dom_node_has_child_nodes (paragraph))
            return WEBKIT_DOM_ELEMENT (paragraph);

        paragraph_clone = webkit_dom_node_clone_node (paragraph, TRUE);
        caret_node = webkit_dom_element_query_selector (
            WEBKIT_DOM_ELEMENT (paragraph_clone),
            "span#-x-evo-caret-position", NULL);
        text_content = webkit_dom_node_get_text_content (paragraph_clone);
        paragraph_char_count = g_utf8_strlen (text_content, -1);
        if (caret_node)
            paragraph_char_count--;
        g_free (text_content);

        wrap_br = webkit_dom_element_query_selector_all (
            WEBKIT_DOM_ELEMENT (paragraph_clone),
            remove_all_br ? "br" : "br.-x-evo-wrap-br",
            NULL);
    }

    /* And remove them */
    br_count = webkit_dom_node_list_get_length (wrap_br);
    for (ii = 0; ii < br_count; ii++)
        remove_node (webkit_dom_node_list_item (wrap_br, ii));

    if (selection)
        node = WEBKIT_DOM_NODE (fragment);
    else {
        webkit_dom_node_normalize (paragraph_clone);
        node = webkit_dom_node_get_first_child (paragraph_clone);
        if (node) {
            text_content = webkit_dom_node_get_text_content (node);
            if (g_strcmp0 ("\n", text_content) == 0)
                node = webkit_dom_node_get_next_sibling (node);
            g_free (text_content);
        }
    }

    start_node = node;
    len = 0;
    while (node) {
        gint offset = 0;

        if (WEBKIT_DOM_IS_TEXT (node)) {
            const gchar *newline;
            WebKitDOMNode *next_sibling;

            /* If there is temporary hidden space we remove it */
            text_content = webkit_dom_node_get_text_content (node);
            if (strstr (text_content, UNICODE_ZERO_WIDTH_SPACE)) {
                if (g_str_has_prefix (text_content, UNICODE_ZERO_WIDTH_SPACE))
                    webkit_dom_character_data_delete_data (
                        WEBKIT_DOM_CHARACTER_DATA (node),
                        0,
                        1,
                        NULL);
                if (g_str_has_suffix (text_content, UNICODE_ZERO_WIDTH_SPACE))
                    webkit_dom_character_data_delete_data (
                        WEBKIT_DOM_CHARACTER_DATA (node),
                        g_utf8_strlen (text_content, -1) - 1,
                        1,
                        NULL);
                g_free (text_content);
                text_content = webkit_dom_node_get_text_content (node);
            }
            newline = g_strstr_len (text_content, -1, "\n");

            next_sibling = node;
            while (newline) {
                WebKitDOMElement *element;

                next_sibling = WEBKIT_DOM_NODE (webkit_dom_text_split_text (
                    WEBKIT_DOM_TEXT (next_sibling),
                    g_utf8_pointer_to_offset (text_content, newline),
                    NULL));

                if (!next_sibling)
                    break;

                element = webkit_dom_document_create_element (
                    document, "BR", NULL);
                element_add_class (element, "-x-evo-temp-wrap-text-br");

                webkit_dom_node_insert_before (
                    webkit_dom_node_get_parent_node (next_sibling),
                    WEBKIT_DOM_NODE (element),
                    next_sibling,
                    NULL);

                g_free (text_content);

                text_content = webkit_dom_node_get_text_content (next_sibling);
                if (g_str_has_prefix (text_content, "\n")) {
                    webkit_dom_character_data_delete_data (
                        WEBKIT_DOM_CHARACTER_DATA (next_sibling), 0, 1, NULL);
                    g_free (text_content);
                    text_content =
                        webkit_dom_node_get_text_content (next_sibling);
                }
                newline = g_strstr_len (text_content, -1, "\n");
            }
            g_free (text_content);
        } else {
            /* If element is ANCHOR we wrap it separately */
            if (WEBKIT_DOM_IS_HTML_ANCHOR_ELEMENT (node)) {
                glong anchor_length;

                text_content = webkit_dom_node_get_text_content (node);
                anchor_length = g_utf8_strlen (text_content, -1);
                if (len + anchor_length > word_wrap_length) {
                    element = webkit_dom_document_create_element (
                        document, "BR", NULL);
                    element_add_class (element, "-x-evo-wrap-br");
                    webkit_dom_node_insert_before (
                        webkit_dom_node_get_parent_node (node),
                        WEBKIT_DOM_NODE (element),
                        node,
                        NULL);
                    len = anchor_length;
                } else
                    len += anchor_length;

                g_free (text_content);
                /* If there is space after the anchor don't try to
                 * wrap before it */
                node = webkit_dom_node_get_next_sibling (node);
                if (WEBKIT_DOM_IS_TEXT (node)) {
                    text_content = webkit_dom_node_get_text_content (node);
                    if (g_strcmp0 (text_content, " ") == 0) {
                        node = webkit_dom_node_get_next_sibling (node);
                        len++;
                    }
                    g_free (text_content);
                }
                continue;
            }

            if (is_caret_position_node (node)) {
                node = webkit_dom_node_get_next_sibling (node);
                continue;
            }

            /* When we are not removing user-entered BR elements (lines wrapped by user),
             * we need to skip those elements */
            if (!remove_all_br && WEBKIT_DOM_IS_HTMLBR_ELEMENT (node)) {
                if (!element_has_class (WEBKIT_DOM_ELEMENT (node), "-x-evo-wrap-br")) {
                    len = 0;
                    node = webkit_dom_node_get_next_sibling (node);
                    continue;
                }
            }
            goto next_node;
        }

        /* If length of this node + what we already have is still less
         * then word_wrap_length characters, then just join it and continue to next
         * node */
        length_left = webkit_dom_character_data_get_length (
            WEBKIT_DOM_CHARACTER_DATA (node));

        if ((length_left + len) < word_wrap_length) {
            len += length_left;
            goto next_node;
        }

        /* wrap until we have something */
        while ((length_left + len) > word_wrap_length) {
            gint max_length;

            max_length = word_wrap_length - len;
            if (max_length < 0)
                max_length = word_wrap_length;
            /* Find where we can line-break the node so that it
             * effectively fills the rest of current row */
            offset = find_where_to_break_line (
                node, max_length, word_wrap_length);

            element = webkit_dom_document_create_element (document, "BR", NULL);
            element_add_class (element, "-x-evo-wrap-br");

            if (offset > 0 && offset <= word_wrap_length) {
                if (offset != length_left)
                    webkit_dom_text_split_text (
                        WEBKIT_DOM_TEXT (node), offset, NULL);

                if (webkit_dom_node_get_next_sibling (node)) {
                    gchar *nd_content;
                    WebKitDOMNode *nd = webkit_dom_node_get_next_sibling (node);

                    nd = webkit_dom_node_get_next_sibling (node);
                    nd_content = webkit_dom_node_get_text_content (nd);
                    if (nd_content && *nd_content) {
                        if (g_str_has_prefix (nd_content, " "))
                            webkit_dom_character_data_replace_data (
                                WEBKIT_DOM_CHARACTER_DATA (nd), 0, 1, "", NULL);
                        g_free (nd_content);
                        nd_content = webkit_dom_node_get_text_content (nd);
                        if (g_strcmp0 (nd_content, UNICODE_NBSP) == 0)
                            remove_node (nd);
                        g_free (nd_content);
                    }

                    webkit_dom_node_insert_before (
                        webkit_dom_node_get_parent_node (node),
                        WEBKIT_DOM_NODE (element),
                        nd,
                        NULL);
                } else {
                    webkit_dom_node_append_child (
                        webkit_dom_node_get_parent_node (node),
                        WEBKIT_DOM_NODE (element),
                        NULL);
                }
            } else if (offset > word_wrap_length) {
                if (offset != length_left)
                    webkit_dom_text_split_text (
                        WEBKIT_DOM_TEXT (node), offset + 1, NULL);

                if (webkit_dom_node_get_next_sibling (node)) {
                    gchar *nd_content;
                    WebKitDOMNode *nd = webkit_dom_node_get_next_sibling (node);

                    nd = webkit_dom_node_get_next_sibling (node);
                    nd_content = webkit_dom_node_get_text_content (nd);
                    if (nd_content && *nd_content) {
                        if (g_str_has_prefix (nd_content, " "))
                            webkit_dom_character_data_replace_data (
                                WEBKIT_DOM_CHARACTER_DATA (nd), 0, 1, "", NULL);
                        g_free (nd_content);
                        nd_content = webkit_dom_node_get_text_content (nd);
                        if (g_strcmp0 (nd_content, UNICODE_NBSP) == 0)
                            remove_node (nd);
                        g_free (nd_content);
                    }

                    webkit_dom_node_insert_before (
                        webkit_dom_node_get_parent_node (node),
                        WEBKIT_DOM_NODE (element),
                        nd,
                        NULL);
                } else {
                    webkit_dom_node_append_child (
                        webkit_dom_node_get_parent_node (node),
                        WEBKIT_DOM_NODE (element),
                        NULL);
                }
                len = 0;
                break;
            } else {
                webkit_dom_node_insert_before (
                    webkit_dom_node_get_parent_node (node),
                    WEBKIT_DOM_NODE (element),
                    node,
                    NULL);
            }
            length_left = webkit_dom_character_data_get_length (
                WEBKIT_DOM_CHARACTER_DATA (node));

            len = 0;
        }
        len += length_left - offset;
 next_node:
        if (WEBKIT_DOM_IS_HTMLLI_ELEMENT (node))
            len = 0;

        /* Move to next node */
        if (webkit_dom_node_has_child_nodes (node)) {
            node = webkit_dom_node_get_first_child (node);
        } else if (webkit_dom_node_get_next_sibling (node)) {
            node = webkit_dom_node_get_next_sibling (node);
        } else {
            if (webkit_dom_node_is_equal_node (node, start_node))
                break;

            node = webkit_dom_node_get_parent_node (node);
            if (node)
                node = webkit_dom_node_get_next_sibling (node);
        }
    }

    if (selection) {
        gchar *html;

        /* Create a wrapper DIV and put the processed content into it */
        element = webkit_dom_document_create_element (document, "DIV", NULL);
        element_add_class (element, "-x-evo-paragraph");
        webkit_dom_node_append_child (
            WEBKIT_DOM_NODE (element),
            WEBKIT_DOM_NODE (start_node),
            NULL);

        webkit_dom_node_normalize (WEBKIT_DOM_NODE (element));
        /* Get HTML code of the processed content */
        html = webkit_dom_html_element_get_inner_html (WEBKIT_DOM_HTML_ELEMENT (element));

        /* Overwrite the current selection be the processed content */
        e_html_editor_selection_insert_html (selection, html);

        g_free (html);

        return NULL;
    } else {
        webkit_dom_node_normalize (paragraph_clone);

        node = webkit_dom_node_get_parent_node (paragraph);
        if (node) {
            /* Replace paragraph with wrapped one */
            webkit_dom_node_replace_child (
                node, paragraph_clone, paragraph, NULL);
        }

        return WEBKIT_DOM_ELEMENT (paragraph_clone);
    }
}

void
e_html_editor_selection_set_indented_style (EHTMLEditorSelection *selection,
                                            WebKitDOMElement *element,
                                            gint width)
{
    EHTMLEditorSelectionAlignment alignment;
    gchar *style;
    const gchar *align_value;
    gint word_wrap_length = (width == -1) ? selection->priv->word_wrap_length : width;
    gint start = 0, end = 0;

    alignment = e_html_editor_selection_get_alignment (selection);
    align_value = get_css_alignment_value (alignment);

    if (alignment == E_HTML_EDITOR_SELECTION_ALIGNMENT_LEFT)
        start = SPACES_PER_INDENTATION;

    if (alignment == E_HTML_EDITOR_SELECTION_ALIGNMENT_CENTER) {
        start = SPACES_PER_INDENTATION;
    }

    if (alignment == E_HTML_EDITOR_SELECTION_ALIGNMENT_RIGHT) {
        start = 0;
        end = SPACES_PER_INDENTATION;
    }

    webkit_dom_element_set_class_name (element, "-x-evo-indented");
    /* We don't want vertical space bellow and above blockquote inserted by
     * WebKit's User Agent Stylesheet. We have to override it through style attribute. */
    if (is_in_html_mode (selection))
        style = g_strdup_printf (
            "-webkit-margin-start: %dch; -webkit-margin-end : %dch; %s",
            start, end, align_value);
    else
        style = g_strdup_printf (
            "-webkit-margin-start: %dch; -webkit-margin-end : %dch; "
            "word-wrap: normal; width: %dch; %s",
            start, end, word_wrap_length, align_value);

    webkit_dom_element_set_attribute (element, "style", style, NULL);
    g_free (style);
}

WebKitDOMElement *
e_html_editor_selection_get_indented_element (EHTMLEditorSelection *selection,
                                              WebKitDOMDocument *document,
                                              gint width)
{
    WebKitDOMElement *element;

    element = webkit_dom_document_create_element (document, "BLOCKQUOTE", NULL);
    e_html_editor_selection_set_indented_style (selection, element, width);

    return element;
}

void
e_html_editor_selection_set_paragraph_style (EHTMLEditorSelection *selection,
                                             WebKitDOMElement *element,
                                             gint width,
                                             gint offset,
                                             const gchar *style_to_add)
{
    EHTMLEditorSelectionAlignment alignment;
    const gchar *align_value = NULL;
    char *style = NULL;
    gint word_wrap_length = (width == -1) ? selection->priv->word_wrap_length : width;

    alignment = e_html_editor_selection_get_alignment (selection);
    align_value = get_css_alignment_value (alignment);

    element_add_class (element, "-x-evo-paragraph");
    if (!is_in_html_mode (selection)) {
        style = g_strdup_printf (
            "width: %dch; word-wrap: normal; %s %s",
            (word_wrap_length + offset), align_value, style_to_add);
    } else {
        if (*align_value || *style_to_add)
            style = g_strdup_printf (
                "%s %s", align_value, style_to_add);
    }
    if (style) {
        webkit_dom_element_set_attribute (element, "style", style, NULL);
        g_free (style);
    }
}

WebKitDOMElement *
e_html_editor_selection_get_paragraph_element (EHTMLEditorSelection *selection,
                                               WebKitDOMDocument *document,
                                               gint width,
                                               gint offset)
{
    WebKitDOMElement *element;

    element = webkit_dom_document_create_element (document, "DIV", NULL);
    e_html_editor_selection_set_paragraph_style (selection, element, width, offset, "");

    return element;
}

WebKitDOMElement *
e_html_editor_selection_put_node_into_paragraph (EHTMLEditorSelection *selection,
                                                 WebKitDOMDocument *document,
                                                 WebKitDOMNode *node,
                                                 WebKitDOMNode *caret_position)
{
    WebKitDOMRange *range;
    WebKitDOMElement *container;

    range = webkit_dom_document_create_range (document);
    container = e_html_editor_selection_get_paragraph_element (selection, document, -1, 0);
    webkit_dom_range_select_node (range, node, NULL);
    webkit_dom_range_surround_contents (range, WEBKIT_DOM_NODE (container), NULL);
    /* We have to move caret position inside this container */
    webkit_dom_node_append_child (WEBKIT_DOM_NODE (container), caret_position, NULL);

    return container;
}

/**
 * e_html_editor_selection_wrap_lines:
 * @selection: an #EHTMLEditorSelection
 *
 * Wraps all lines in current selection to be 71 characters long.
 */
void
e_html_editor_selection_wrap_lines (EHTMLEditorSelection *selection)
{
    EHTMLEditorView *view;
    WebKitDOMRange *range;
    WebKitDOMDocument *document;
    WebKitDOMElement *active_paragraph, *caret;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
    g_object_unref (view);

    caret = e_html_editor_selection_save_caret_position (selection);
    if (g_strcmp0 (e_html_editor_selection_get_string (selection), "") == 0) {
        WebKitDOMNode *end_container;
        WebKitDOMNode *parent;
        WebKitDOMNode *paragraph;
        gchar *text_content;

        /* We need to save caret position and restore it after
         * wrapping the selection, but we need to save it before we
         * start to modify selection */
        range = html_editor_selection_get_current_range (selection);
        if (!range)
            return;

        end_container = webkit_dom_range_get_common_ancestor_container (range, NULL);

        /* Wrap only text surrounded in DIV and P tags */
        parent = webkit_dom_node_get_parent_node(end_container);
        if (WEBKIT_DOM_IS_HTML_DIV_ELEMENT (parent) ||
            WEBKIT_DOM_IS_HTML_PARAGRAPH_ELEMENT (parent)) {
            element_add_class (
                WEBKIT_DOM_ELEMENT (parent), "-x-evo-paragraph");
            paragraph = parent;
        } else {
            WebKitDOMElement *parent_div =
                e_html_editor_dom_node_find_parent_element (parent, "DIV");

            if (element_has_class (parent_div, "-x-evo-paragraph")) {
                paragraph = WEBKIT_DOM_NODE (parent_div);
            } else {
                if (!caret)
                    return;

                /* We try to select previous sibling */
                paragraph = webkit_dom_node_get_previous_sibling (
                    WEBKIT_DOM_NODE (caret));
                if (paragraph) {
                    /* When there is just text without container
                     * we have to surround it with paragraph div */
                    if (WEBKIT_DOM_IS_TEXT (paragraph))
                        paragraph = WEBKIT_DOM_NODE (
                            e_html_editor_selection_put_node_into_paragraph (
                                selection, document, paragraph,
                                WEBKIT_DOM_NODE (caret)));
                } else {
                    /* When some weird element is selected, return */
                    e_html_editor_selection_clear_caret_position_marker (selection);
                    return;
                }
            }
        }

        if (!paragraph)
            return;

        webkit_dom_element_remove_attribute (
            WEBKIT_DOM_ELEMENT (paragraph), "style");
        webkit_dom_element_set_id (
            WEBKIT_DOM_ELEMENT (paragraph), "-x-evo-active-paragraph");

        text_content = webkit_dom_node_get_text_content (paragraph);
        /* If there is hidden space character in the beginning we remove it */
        if (strstr (text_content, UNICODE_ZERO_WIDTH_SPACE)) {
            if (g_str_has_prefix (text_content, UNICODE_ZERO_WIDTH_SPACE)) {
                WebKitDOMNode *node;

                node = webkit_dom_node_get_first_child (paragraph);

                webkit_dom_character_data_delete_data (
                    WEBKIT_DOM_CHARACTER_DATA (node),
                    0,
                    1,
                    NULL);
            }
            if (g_str_has_suffix (text_content, UNICODE_ZERO_WIDTH_SPACE)) {
                WebKitDOMNode *node;

                node = webkit_dom_node_get_last_child (paragraph);

                webkit_dom_character_data_delete_data (
                    WEBKIT_DOM_CHARACTER_DATA (node),
                    g_utf8_strlen (text_content, -1) -1,
                    1,
                    NULL);
            }
        }
        g_free (text_content);

        wrap_lines (
            NULL, paragraph, document, FALSE,
            selection->priv->word_wrap_length);

    } else {
        e_html_editor_selection_save_caret_position (selection);
        /* If we have selection -> wrap it */
        wrap_lines (
            selection, NULL, document, FALSE,
            selection->priv->word_wrap_length);
    }

    active_paragraph = webkit_dom_document_get_element_by_id (
        document, "-x-evo-active-paragraph");
    /* We have to move caret on position where it was before modifying the text */
    e_html_editor_selection_restore_caret_position (selection);

    /* Set paragraph as non-active */
    if (active_paragraph)
        webkit_dom_element_remove_attribute (
            WEBKIT_DOM_ELEMENT (active_paragraph), "id");
}

WebKitDOMElement *
e_html_editor_selection_wrap_paragraph_length (EHTMLEditorSelection *selection,
                                               WebKitDOMElement *paragraph,
                                               gint length)
{
    WebKitDOMDocument *document;

    g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), NULL);
    g_return_val_if_fail (WEBKIT_DOM_IS_ELEMENT (paragraph), NULL);
    g_return_val_if_fail (length > 10, NULL);

    document = webkit_dom_node_get_owner_document (WEBKIT_DOM_NODE (paragraph));

    return wrap_lines (
        NULL, WEBKIT_DOM_NODE (paragraph), document, FALSE, length);
}

void
e_html_editor_selection_wrap_paragraphs_in_document (EHTMLEditorSelection *selection,
                                                     WebKitDOMDocument *document)
{
    WebKitDOMNodeList *list;
    gint ii, length;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    list = webkit_dom_document_query_selector_all (
        document, "div.-x-evo-paragraph:not(#-x-evo-input-start)", NULL);

    length = webkit_dom_node_list_get_length (list);

    for (ii = 0; ii < length; ii++) {
        gint quote, citation_level;
        WebKitDOMNode *node = webkit_dom_node_list_item (list, ii);

        citation_level = get_citation_level (node);
        quote = citation_level ? citation_level * 2 : 0;

        if (node_is_list (node)) {
            WebKitDOMNode *item = webkit_dom_node_get_first_child (node);

            while (item && WEBKIT_DOM_IS_HTMLLI_ELEMENT (item)) {
                e_html_editor_selection_wrap_paragraph_length (
                    selection,
                    WEBKIT_DOM_ELEMENT (item),
                    selection->priv->word_wrap_length - quote);
                item = webkit_dom_node_get_next_sibling (item);
            }
        } else {
            e_html_editor_selection_wrap_paragraph_length (
                selection,
                WEBKIT_DOM_ELEMENT (node),
                selection->priv->word_wrap_length - quote);
        }
    }
}

WebKitDOMElement *
e_html_editor_selection_wrap_paragraph (EHTMLEditorSelection *selection,
                                        WebKitDOMElement *paragraph)
{
    gint indentation_level, citation_level, quote;
    gint final_width, word_wrap_length, offset = 0;

    g_return_val_if_fail (E_IS_HTML_EDITOR_SELECTION (selection), NULL);
    g_return_val_if_fail (WEBKIT_DOM_IS_ELEMENT (paragraph), NULL);

    word_wrap_length = selection->priv->word_wrap_length;
    indentation_level = get_indentation_level (paragraph);
    citation_level = get_citation_level (WEBKIT_DOM_NODE (paragraph));

    if (node_is_list (WEBKIT_DOM_NODE (paragraph)) ||
        WEBKIT_DOM_IS_HTMLLI_ELEMENT (paragraph)) {

        gint list_level = get_list_level (WEBKIT_DOM_NODE (paragraph));
        indentation_level = 0;

        if (list_level > 0)
            offset = list_level * -SPACES_PER_LIST_LEVEL;
        else
            offset = -SPACES_PER_LIST_LEVEL;
    }

    quote = citation_level ? citation_level * 2 : 0;

    final_width = word_wrap_length - quote + offset;
    final_width -= SPACES_PER_INDENTATION * indentation_level;

    return e_html_editor_selection_wrap_paragraph_length (
        selection, WEBKIT_DOM_ELEMENT (paragraph), final_width);
}

static WebKitDOMNode *
in_empty_block_in_quoted_content (WebKitDOMNode *element)
{
    WebKitDOMNode *first_child, *next_sibling;

    if (!WEBKIT_DOM_IS_HTML_DIV_ELEMENT (element))
        return NULL;

    first_child = webkit_dom_node_get_first_child (element);
    if (!WEBKIT_DOM_IS_ELEMENT (first_child))
        return NULL;

    if (!element_has_class (WEBKIT_DOM_ELEMENT (first_child), "-x-evo-quoted"))
        return NULL;

    next_sibling = webkit_dom_node_get_next_sibling (first_child);
    if (WEBKIT_DOM_IS_HTMLBR_ELEMENT (next_sibling))
        return next_sibling;

    if (!WEBKIT_DOM_IS_ELEMENT (next_sibling))
        return NULL;

    if (!element_has_id (WEBKIT_DOM_ELEMENT (next_sibling), "-x-evo-selection-start-marker"))
        return NULL;

    next_sibling = webkit_dom_node_get_next_sibling (next_sibling);
    if (WEBKIT_DOM_IS_HTMLBR_ELEMENT (next_sibling))
        return next_sibling;

    return NULL;
}

/**
 * e_html_editor_selection_save:
 * @selection: an #EHTMLEditorSelection
 *
 * Saves current cursor position or current selection range. The selection can
 * be later restored by calling e_html_editor_selection_restore().
 *
 * Note that calling e_html_editor_selection_save() overwrites previously saved
 * position.
 *
 * Note that this method inserts special markings into the HTML code that are
 * used to later restore the selection. It can happen that by deleting some
 * segments of the document some of the markings are deleted too. In that case
 * restoring the selection by e_html_editor_selection_restore() can fail. Also by
 * moving text segments (Cut & Paste) can result in moving the markings
 * elsewhere, thus e_html_editor_selection_restore() will restore the selection
 * incorrectly.
 *
 * It is recommended to use this method only when you are not planning to make
 * bigger changes to content or structure of the document (formatting changes
 * are usually OK).
 */
void
e_html_editor_selection_save (EHTMLEditorSelection *selection)
{
    EHTMLEditorView *view;
    WebKitWebView *web_view;
    WebKitDOMDocument *document;
    WebKitDOMRange *range;
    WebKitDOMNode *container, *next_sibling;
    WebKitDOMElement *marker;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    web_view = WEBKIT_WEB_VIEW (view);

    document = webkit_web_view_get_dom_document (web_view);
    g_object_unref (view);

    /* First remove all markers (if present) */
    marker = webkit_dom_document_get_element_by_id (
        document, "-x-evo-selection-start-marker");
    if (marker != NULL)
        remove_node (WEBKIT_DOM_NODE (marker));

    marker = webkit_dom_document_get_element_by_id (
        document, "-x-evo-selection-end-marker");
    if (marker != NULL)
        remove_node (WEBKIT_DOM_NODE (marker));

    range = html_editor_selection_get_current_range (selection);

    if (range != NULL) {
        WebKitDOMNode *marker_node;
        WebKitDOMNode *parent_node;
        WebKitDOMNode *split_node;
        glong offset;

        marker = webkit_dom_document_create_element (
            document, "SPAN", NULL);
        webkit_dom_element_set_id (
            marker, "-x-evo-selection-start-marker");

        container = webkit_dom_range_get_start_container (range, NULL);
        offset = webkit_dom_range_get_start_offset (range, NULL);

        if (WEBKIT_DOM_IS_TEXT (container)) {
            if (offset != 0) {
                WebKitDOMText *split_text;

                split_text = webkit_dom_text_split_text (
                    WEBKIT_DOM_TEXT (container), offset, NULL);
                split_node = WEBKIT_DOM_NODE (split_text);
            } else {
                marker_node = webkit_dom_node_insert_before (
                    webkit_dom_node_get_parent_node (container),
                    WEBKIT_DOM_NODE (marker),
                    container,
                    NULL);
                goto end_marker;
            }
        } else if (WEBKIT_DOM_IS_HTMLLI_ELEMENT (container)) {
            marker_node = webkit_dom_node_insert_before (
                container,
                WEBKIT_DOM_NODE (marker),
                webkit_dom_node_get_first_child (container),
                NULL);
            goto end_marker;
        } else {
            /* Insert the selection marker on the right position in
             * an empty paragraph in the quoted content */
            if ((next_sibling = in_empty_block_in_quoted_content (container))) {
                marker_node = webkit_dom_node_insert_before (
                    container,
                    WEBKIT_DOM_NODE (marker),
                    next_sibling,
                    NULL);
                goto end_marker;
            }
            if (!webkit_dom_node_get_previous_sibling (container)) {
                marker_node = webkit_dom_node_insert_before (
                    container,
                    WEBKIT_DOM_NODE (marker),
                    webkit_dom_node_get_first_child (container),
                    NULL);
                goto end_marker;
            } else if (!webkit_dom_node_get_next_sibling (container)) {
                marker_node = webkit_dom_node_append_child (
                    container,
                    WEBKIT_DOM_NODE (marker),
                    NULL);
                goto end_marker;
            } else {
                if (webkit_dom_node_get_first_child (container)) {
                    marker_node = webkit_dom_node_insert_before (
                        container,
                        WEBKIT_DOM_NODE (marker),
                        webkit_dom_node_get_first_child (container),
                        NULL);
                    goto end_marker;
                }
                split_node = container;
            }
        }

        /* Don't save selection straight into body */
        if (WEBKIT_DOM_IS_HTML_BODY_ELEMENT (split_node))
            return;

        if (!split_node) {
            marker_node = webkit_dom_node_insert_before (
                container,
                WEBKIT_DOM_NODE (marker),
                webkit_dom_node_get_first_child (
                    WEBKIT_DOM_NODE (container)),
                NULL);
        } else {
            marker_node = WEBKIT_DOM_NODE (marker);
            parent_node = webkit_dom_node_get_parent_node (split_node);

            webkit_dom_node_insert_before (
                parent_node, marker_node, split_node, NULL);
        }
 end_marker:
        marker = webkit_dom_document_create_element (
            document, "SPAN", NULL);
        webkit_dom_element_set_id (
            marker, "-x-evo-selection-end-marker");

        if (webkit_dom_range_get_collapsed (range, NULL)) {
            webkit_dom_node_insert_before (
                /* Selection start marker */
                webkit_dom_node_get_parent_node (marker_node),
                WEBKIT_DOM_NODE (marker),
                webkit_dom_node_get_next_sibling (marker_node),
                NULL);
            return;
        }

        container = webkit_dom_range_get_end_container (range, NULL);
        offset = webkit_dom_range_get_end_offset (range, NULL);

        if (WEBKIT_DOM_IS_TEXT (container)) {
            if (offset != 0) {
                WebKitDOMText *split_text;

                split_text = webkit_dom_text_split_text (
                    WEBKIT_DOM_TEXT (container), offset, NULL);
                split_node = WEBKIT_DOM_NODE (split_text);
            } else {
                marker_node = webkit_dom_node_insert_before (
                    webkit_dom_node_get_parent_node (container),
                    WEBKIT_DOM_NODE (marker),
                    container,
                    NULL);
                goto check;

            }
        } else if (WEBKIT_DOM_IS_HTMLLI_ELEMENT (container)) {
            webkit_dom_node_append_child (
                container, WEBKIT_DOM_NODE (marker), NULL);
            return;
        } else {
            /* Insert the selection marker on the right position in
             * an empty paragraph in the quoted content */
            if ((next_sibling = in_empty_block_in_quoted_content (container))) {
                webkit_dom_node_insert_before (
                    container,
                    WEBKIT_DOM_NODE (marker),
                    next_sibling,
                    NULL);
                return;
            }
            if (!webkit_dom_node_get_previous_sibling (container)) {
                split_node = webkit_dom_node_get_parent_node (
                    container);
            } else if (!webkit_dom_node_get_next_sibling (container)) {
                split_node = webkit_dom_node_get_parent_node (
                    container);
                split_node = webkit_dom_node_get_next_sibling (
                    split_node);
            } else
                split_node = container;
        }

        /* Don't save selection straight into body */
        if (WEBKIT_DOM_IS_HTML_BODY_ELEMENT (split_node)) {
            marker = webkit_dom_document_get_element_by_id (
                document, "-x-evo-selection-start-marker");
            remove_node (WEBKIT_DOM_NODE (marker));
            return;
        }

        marker_node = WEBKIT_DOM_NODE (marker);

        if (split_node) {
            parent_node = webkit_dom_node_get_parent_node (split_node);

            webkit_dom_node_insert_before (
                parent_node, marker_node, split_node, NULL);
        } else {
            webkit_dom_node_append_child (
                WEBKIT_DOM_NODE (container),
                marker_node,
                NULL);
        }
 check:
        if ((next_sibling = webkit_dom_node_get_next_sibling (marker_node))) {
            if (!WEBKIT_DOM_IS_ELEMENT (next_sibling))
                next_sibling = webkit_dom_node_get_next_sibling (next_sibling);
            /* If the selection is collapsed ensure that the selection start marker
             * is before the end marker */
            if (next_sibling && WEBKIT_DOM_IS_ELEMENT (next_sibling) &&
                element_has_id (WEBKIT_DOM_ELEMENT (next_sibling), "-x-evo-selection-start-marker")) {
                webkit_dom_node_insert_before (
                    webkit_dom_node_get_parent_node (marker_node),
                    next_sibling,
                    marker_node,
                    NULL);
            }
        }

    }
}

/**
 * e_html_editor_selection_restore:
 * @selection: an #EHTMLEditorSelection
 *
 * Restores cursor position or selection range that was saved by
 * e_html_editor_selection_save().
 *
 * Note that calling this function without calling e_html_editor_selection_save()
 * before is a programming error and the behavior is undefined.
 */
void
e_html_editor_selection_restore (EHTMLEditorSelection *selection)
{
    EHTMLEditorView *view;
    WebKitWebView *web_view;
    WebKitDOMDocument *document;
    WebKitDOMDOMWindow *window;
    WebKitDOMDOMSelection *dom_selection;
    WebKitDOMRange *range;
    WebKitDOMElement *marker;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    web_view = WEBKIT_WEB_VIEW (view);

    document = webkit_web_view_get_dom_document (web_view);
    g_object_unref (view);
    window = webkit_dom_document_get_default_view (document);
    dom_selection = webkit_dom_dom_window_get_selection (window);
    range = webkit_dom_document_create_range (document);

    if (range != NULL) {
        marker = webkit_dom_document_get_element_by_id (
            document, "-x-evo-selection-start-marker");
        if (!marker) {
            marker = webkit_dom_document_get_element_by_id (
                document, "-x-evo-selection-end-marker");
            if (marker)
                remove_node (WEBKIT_DOM_NODE (marker));
            return;
        }

        webkit_dom_range_set_start_after (
            range, WEBKIT_DOM_NODE (marker), NULL);
        remove_node (WEBKIT_DOM_NODE (marker));

        marker = webkit_dom_document_get_element_by_id (
            document, "-x-evo-selection-end-marker");
        if (!marker) {
            marker = webkit_dom_document_get_element_by_id (
                document, "-x-evo-selection-start-marker");
            if (marker)
                remove_node (WEBKIT_DOM_NODE (marker));
            return;
        }

        webkit_dom_range_set_end_before (
            range, WEBKIT_DOM_NODE (marker), NULL);
        remove_node (WEBKIT_DOM_NODE (marker));

        webkit_dom_dom_selection_remove_all_ranges (dom_selection);
        webkit_dom_dom_selection_add_range (dom_selection, range);
    }
}

static void
html_editor_selection_modify (EHTMLEditorSelection *selection,
                              const gchar *alter,
                              gboolean forward,
                              EHTMLEditorSelectionGranularity granularity)
{
    EHTMLEditorView *view;
    WebKitWebView *web_view;
    WebKitDOMDocument *document;
    WebKitDOMDOMWindow *window;
    WebKitDOMDOMSelection *dom_selection;
    const gchar *granularity_str;

    g_return_if_fail (E_IS_HTML_EDITOR_SELECTION (selection));

    view = e_html_editor_selection_ref_html_editor_view (selection);
    g_return_if_fail (view != NULL);

    web_view = WEBKIT_WEB_VIEW (view);

    document = webkit_web_view_get_dom_document (web_view);
    window = webkit_dom_document_get_default_view (document);
    dom_selection = webkit_dom_dom_window_get_selection (window);

    switch (granularity) {
        case E_HTML_EDITOR_SELECTION_GRANULARITY_CHARACTER:
            granularity_str = "character";
            break;
        case E_HTML_EDITOR_SELECTION_GRANULARITY_WORD:
            granularity_str = "word";
            break;
    }

    webkit_dom_dom_selection_modify (
        dom_selection, alter,
        forward ? "forward" : "backward",
        granularity_str);

    g_object_unref (view);
}

/**
 * e_html_editor_selection_extend:
 * @selection: an #EHTMLEditorSelection
 * @forward: whether to extend selection forward or backward
 * @granularity: granularity of the extension
 *
 * Extends current selection in given direction by given granularity.
 */
void
e_html_editor_selection_extend (EHTMLEditorSelection *selection,
                                gboolean forward,
                                EHTMLEditorSelectionGranularity granularity)
{
    html_editor_selection_modify (selection, "extend", forward, granularity);
}

/**
 * e_html_editor_selection_move:
 * @selection: an #EHTMLEditorSelection
 * @forward: whether to move the selection forward or backward
 * @granularity: granularity of the movement
 *
 * Moves current selection in given direction by given granularity
 */
void
e_html_editor_selection_move (EHTMLEditorSelection *selection,
                              gboolean forward,
                              EHTMLEditorSelectionGranularity granularity)
{
    html_editor_selection_modify (selection, "move", forward, granularity);
}

void
e_html_editor_selection_scroll_to_caret (EHTMLEditorSelection *selection)
{
    glong element_top, element_left;
    glong window_top, window_left, window_right, window_bottom;
    EHTMLEditorView *view;
    WebKitDOMDocument *document;
    WebKitDOMDOMWindow *window;
    WebKitDOMElement *caret;

    caret = e_html_editor_selection_save_caret_position (selection);
    if (!caret)
        return;

    view = e_html_editor_selection_ref_html_editor_view (selection);
    document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (view));
    g_object_unref (view);

    window = webkit_dom_document_get_default_view (document);

    window_top = webkit_dom_dom_window_get_scroll_y (window);
    window_left = webkit_dom_dom_window_get_scroll_x (window);
    window_bottom = window_top + webkit_dom_dom_window_get_inner_height (window);
    window_right = window_left + webkit_dom_dom_window_get_inner_width (window);

    element_left = webkit_dom_element_get_offset_left (caret);
    element_top = webkit_dom_element_get_offset_top (caret);

    /* Check if caret is inside viewport, if not move to it */
    if (!(element_top >= window_top && element_top <= window_bottom &&
         element_left >= window_left && element_left <= window_right)) {
        webkit_dom_element_scroll_into_view (caret, TRUE);
    }

    e_html_editor_selection_clear_caret_position_marker (selection);
}