-
Notifications
You must be signed in to change notification settings - Fork 6
/
kite-dom.el
1544 lines (1382 loc) · 59.5 KB
/
kite-dom.el
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
;;; kite-dom.el --- Kite DOM module implementation
;; Copyright (C) 2012 Julian Scheid
;; Author: Julian Scheid <[email protected]>
;; Keywords: tools
;; Package: kite
;; Compatibility: GNU Emacs 24
;; This file is not part of GNU Emacs.
;; Kite is free software: you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; Kite 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 General Public
;; License for more details.
;; You should have received a copy of the GNU General Public License
;; along with Kite. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package implements the WebKit DOM inspector.
;;
;; It is part of Kite, a WebKit inspector front-end.
;;; Code:
(require 'kite-global)
(require 'kite-color)
(require 'kite-dom-css)
(require 'cl)
(require 'widget)
;; Try loading nxml-mode so we can steal their faces
(require 'nxml-mode nil t)
(eval-when-compile
(require 'wid-edit))
(defstruct (kite-dom-node)
line-begin
line-end
outer-begin
outer-end
inner-begin
inner-end
indent
attr-alist
widget
parent
child-count
children
id
type
value
local-name
openp)
(defstruct (kite-dom-attr)
name
value
outer-begin
outer-end
value-begin
value-end
value-widget)
;; Per https://developer.mozilla.org/en-US/docs/DOM/Node.nodeType
(defconst kite-dom-element-node 1)
(defconst kite-dom-attribute-node 2)
(defconst kite-dom-text-node 3)
(defconst kite-dom-cdata-section-node 4)
(defconst kite-dom-entity-reference-node 5)
(defconst kite-dom-entity-node 6)
(defconst kite-dom-processing-instruction-node 7)
(defconst kite-dom-comment-node 8)
(defconst kite-dom-document-node 9)
(defconst kite-dom-document-type-node 10)
(defconst kite-dom-document-fragment-node 11)
(defconst kite-dom-notation-node 12)
;;; Below default colors and face definitions shamelessly stolen from
;;; nxml. However, we try to derive from nxml faces if possible in
;;; case we we inherit any changes the user might have made to them.
;; The following are the colors we use with a light background.
;; The two blues have the same hue but contrasting saturation/value.
;; The hue of the green is 120 degrees different from that of the
;; blue. The red used for highlighting errors is 120 degrees
;; different again. We use the light blue only for refs and
;; delimiters, since these are short (long stretches in a light color
;; would be too hard to read). The dark blue is closest to black
;; (which we use by default for text), so we use it for attribute
;; values, which are similar to text.
(defconst kite-light-blue-color "#9292C9") ; hue 240
(defconst kite-dark-blue-color "#3A3A7B") ; hue 240
(defconst kite-green-color "#257A25") ; hue 120
;; Similar principles apply with a dark background. However,
;; we switch green and blue, because darker blues are very hard to
;; read (for me anyway) on a dark background.
(defconst kite-sky-blue-color "#ACACFC") ; hue 240
(defconst kite-dark-green-color "#00AD00") ; hue 120
(defconst kite-light-green-color "#70F170") ; hue 120
(defface kite-delimited-data-face
(if (facep 'nxml-delimited-data-face)
'((t (:inherit nxml-delimited-data-face)))
`((((class color) (background light))
(:foreground ,kite-dark-blue-color))
(((class color) (background dark))
(:foreground ,kite-light-green-color))))
"Face used to highlight data enclosed between delimiters.
By default, this is inherited by `kite-attribute-value-face'
and `kite-processing-instruction-content-face'."
:group 'kite-highlighting-faces)
(defface kite-name-face
(if (facep 'nxml-name-face)
'((t (:inherit nxml-name-face)))
`((((class color) (background light))
(:foreground ,kite-green-color))
(((class color) (background dark))
(:foreground ,kite-sky-blue-color))))
"Face used to highlight various names.
This includes element and attribute names, processing
instruction targets and the CDATA keyword in a CDATA section.
This is not used directly, but only via inheritance by other faces."
:group 'kite-highlighting-faces)
(defface kite-ref-face
(if (facep 'nxml-ref-face)
'((t (:inherit nxml-ref-face)))
`((((class color) (background light))
(:foreground ,kite-light-blue-color))
(((class color) (background dark))
(:foreground ,kite-dark-green-color))))
"Face used to highlight character and entity references.
This is not used directly, but only via inheritance by other faces."
:group 'kite-highlighting-faces)
(defface kite-delimiter-face
(if (facep 'nxml-delimiter-face)
'((t (:inherit nxml-delimiter-face)))
`((((class color) (background light))
(:foreground ,kite-light-blue-color))
(((class color) (background dark))
(:foreground ,kite-dark-green-color))
(t (:bold t))))
"Face used to highlight delimiters.
This is not used directly, but only via inheritance by other faces."
:group 'kite-highlighting-faces)
(defface kite-text-face
(if (facep 'nxml-text-face)
`((((class color) (background light))
(:inherit nxml-text-face :background ,kite-light-blue-color))
(((class color) (background dark))
(:inherit nxml-text-face :background ,kite-dark-blue-color)))
`((((class color) (background light))
(:background ,kite-light-blue-color))
(((class color) (background dark))
(:background ,kite-dark-blue-color))))
"Face used to highlight text."
:group 'kite-highlighting-faces)
(defface kite-comment-content-face
(if (facep 'nxml-comment-content-face)
'((t (:inherit nxml-comment-content-face)))
'((t (:italic t))))
"Face used to highlight the content of comments."
:group 'kite-highlighting-faces)
(defface kite-comment-delimiter-face
'((t (:inherit kite-delimiter-face)))
"Face used for the delimiters of comments, i.e <!-- and -->."
:group 'kite-highlighting-faces)
(defface kite-processing-instruction-delimiter-face
'((t (:inherit kite-delimiter-face)))
"Face used for the delimiters of processing instructions, i.e
<? and ?>."
:group 'kite-highlighting-faces)
(defface kite-processing-instruction-target-face
'((t (:inherit kite-name-face)))
"Face used for the target of processing instructions."
:group 'kite-highlighting-faces)
(defface kite-processing-instruction-content-face
'((t (:inherit kite-delimited-data-face)))
"Face used for the content of processing instructions."
:group 'kite-highlighting-faces)
(defface kite-cdata-section-delimiter-face
'((t (:inherit kite-delimiter-face)))
"Face used for the delimiters of CDATA sections, i.e <![, [,
and ]]>."
:group 'kite-highlighting-faces)
(defface kite-cdata-section-CDATA-face
'((t (:inherit kite-name-face)))
"Face used for the CDATA keyword in CDATA sections."
:group 'kite-highlighting-faces)
(defface kite-cdata-section-content-face
'((t (:inherit kite-text-face)))
"Face used for the content of CDATA sections."
:group 'kite-highlighting-faces)
(defface kite-char-ref-number-face
'((t (:inherit kite-ref-face)))
"Face used for the number in character references.
This includes ths `x' in hex references."
:group 'kite-highlighting-faces)
(defface kite-char-ref-delimiter-face
'((t (:inherit kite-ref-face)))
"Face used for the delimiters of character references, i.e &#
and ;."
:group 'kite-highlighting-faces)
(defface kite-entity-ref-name-face
'((t (:inherit kite-ref-face)))
"Face used for the entity name in general entity references."
:group 'kite-highlighting-faces)
(defface kite-entity-ref-delimiter-face
'((t (:inherit kite-ref-face)))
"Face used for the delimiters of entity references, i.e & and ;."
:group 'kite-highlighting-faces)
(defface kite-tag-delimiter-face
'((t (:inherit kite-delimiter-face)))
"Face used for the angle brackets delimiting tags.
`kite-tag-slash-face' is used for slashes."
:group 'kite-highlighting-faces)
(defface kite-tag-slash-face
'((t (:inherit kite-name-face)))
"Face used for slashes in tags, both in end-tags and
empty-elements."
:group 'kite-highlighting-faces)
(defface kite-element-prefix-face
'((t (:inherit kite-name-face)))
"Face used for the prefix of elements."
:group 'kite-highlighting-faces)
(defface kite-element-colon-face
'((t (:inherit kite-name-face)))
"Face used for the colon in element names."
:group 'kite-highlighting-faces)
(defface kite-element-local-name-face
'((t (:inherit kite-name-face)))
"Face used for the local name of elements."
:group 'kite-highlighting-faces)
(defface kite-modified-element-local-name-face
'((t :inherit kite-element-local-name-face :background "#555"))
"Face used for the local name of attributes."
:group 'kite-highlighting-faces)
(defface kite-attribute-prefix-face
'((t (:inherit kite-name-face)))
"Face used for the prefix of attributes."
:group 'kite-highlighting-faces)
(defface kite-attribute-colon-face
'((t (:inherit kite-name-face)))
"Face used for the colon in attribute names."
:group 'kite-highlighting-faces)
(defface kite-attribute-local-name-face
'((t (:inherit kite-name-face)))
"Face used for the local name of attributes."
:group 'kite-highlighting-faces)
(defface kite-modified-attribute-local-name-face
'((t :inherit kite-attribute-local-name-face :background "#555"))
"Face used for the local name of attributes."
:group 'kite-highlighting-faces)
(defface kite-namespace-attribute-xmlns-face
'((t (:inherit kite-name-face)))
"Face used for `xmlns' in namespace attributes."
:group 'kite-highlighting-faces)
(defface kite-namespace-attribute-colon-face
'((t (:inherit kite-name-face)))
"Face used for the colon in namespace attributes."
:group 'kite-highlighting-faces)
(defface kite-namespace-attribute-prefix-face
'((t (:inherit kite-name-face)))
"Face used for the prefix declared in namespace attributes."
:group 'kite-highlighting-faces)
(defface kite-attribute-value-face
'((t (:inherit kite-delimited-data-face)))
"Face used for the value of attributes."
:group 'kite-highlighting-faces)
(defface kite-modified-attribute-value-face
'((t :inherit kite-attribute-value-face :background "#555"))
"Face used for the value of attributes."
:group 'kite-highlighting-faces)
(defface kite-attribute-value-delimiter-face
'((t (:inherit kite-delimiter-face)))
"Face used for the delimiters of attribute values."
:group 'kite-highlighting-faces)
(defface kite-namespace-attribute-value-face
'((t (:inherit kite-attribute-value-face)))
"Face used for the value of namespace attributes."
:group 'kite-highlighting-faces)
(defface kite-namespace-attribute-value-delimiter-face
'((t (:inherit kite-attribute-value-delimiter-face)))
"Face used for the delimiters of namespace attribute values."
:group 'kite-highlighting-faces)
(defface kite-prolog-literal-delimiter-face
'((t (:inherit kite-delimiter-face)))
"Face used for the delimiters of literals in the prolog."
:group 'kite-highlighting-faces)
(defface kite-prolog-literal-content-face
'((t (:inherit kite-delimited-data-face)))
"Face used for the content of literals in the prolog."
:group 'kite-highlighting-faces)
(defface kite-prolog-keyword-face
'((t (:inherit kite-name-face)))
"Face used for keywords in the prolog."
:group 'kite-highlighting-faces)
(defface kite-markup-declaration-delimiter-face
'((t (:inherit kite-delimiter-face)))
"Face used for the delimiters of markup declarations in the prolog.
The delimiters are <! and >."
:group 'kite-highlighting-faces)
(defface kite-hash-face
'((t (:inherit kite-name-face)))
"Face used for # before a name in the prolog."
:group 'kite-highlighting-faces)
(defface kite-node-highlight-face
'((t (:background "#444")))
"Face used for highlighting the DOM element under the mouse cursor."
:group 'kite-highlighting-faces)
;;; End of stolen nxml colors and faces.
(defvar kite--dom-widget-field-keymap
(let ((map (copy-keymap widget-field-keymap)))
(define-key map "\M-\C-n" 'kite-dom-forward-element)
(define-key map "\M-\C-p" 'kite-dom-backward-element)
(define-key map "\C-m" 'kite--widget-field-activate)
map)
"Keymap to use in DOM inspector widget fields.")
(defvar kite-dom-mode-map
(let ((map (make-composed-keymap
(copy-keymap widget-keymap)
(copy-keymap special-mode-map))))
(suppress-keymap map t)
(kite--define-global-mode-keys map)
(define-key map (kbd "RET") 'kite-dom-toggle-node)
(define-key map (kbd "DEL") 'kite-dom-delete-node-or-attribute)
(define-key map "\C-Cp" 'kite-dom-pick-node)
(define-key map "\C-Ch" 'kite-dom-highlight-node)
(define-key map "\C-CH" 'kite-dom-hide-highlight)
(define-key map "\C-Cc" 'kite-dom-show-matched-css)
(define-key map "\C-CC" 'kite-dom-show-computed-css)
(define-key map "\C-xnd" 'kite-dom-narrow-to-node)
(define-key map "\M-\C-n" 'kite-dom-forward-element)
(define-key map "\M-\C-p" 'kite-dom-backward-element)
(define-key map [mouse-movement] 'kite--dom-mouse-movement)
map)
"Local keymap for `kite-dom-mode' buffers.")
(defvar kite--dom-highlighted-node-id)
(defvar kite--dom-pending-set-node-values nil
"An alist of (NODE-ID . CHANGED-VALUE) that keeps track of
which node value changes have been sent to the remote debugging
server but not acknowledged yet. `DOM.characterDataModified'
uses this to see which notifications to ignore.")
(defvar kite--dom-pending-set-attribute-values nil
"A list of (NODE-ID ATTR-NAME CHANGED-VALUE) that keeps track
of which attribute value changes have been sent to the remote
debugging server but not acknowledged yet.
`DOM.attributeModified' uses this to see which notifications to
ignore.")
(defvar kite--dom-setting-remote-value nil
"Scope variable (should be set with a let binding) that
signifies that a DOM change originated on the remote end. Used
by nested functions to ensure the change isn't sent back to the
server.")
(defvar kite-dom-node-modified-hooks nil
"List of functions to call each time a DOM node (that Kite
knows about) is changed in any way. Each function is called once
for the node that has changed and for each of its
parents (recursively, in bottom-top order), with a
`kite-dom-node' structure as the single argument.
Note that it won't be called when a node was modified that Kite
doesn't yet know about, even if Kite might know about one of its
parents.")
(defvar kite-dom-node-removed-hooks nil
"List of functions to call when a DOM node (that Kite knows
about) is removed. Each function is called once for the removed
node's children and for the node itself (recursively, in
bottom-top order), with a `kite-dom-node' structure as the single
argument.
Note that it won't be called when a node was removed that Kite
doesn't yet know about, even if Kite might know about one of its
parents.")
(defun kite--dom-mouse-movement (event)
"Called on mouse movement in a kite-dom buffer. Highlights the
line under mouse and the corresponding DOM node in the browser."
(interactive "e")
(let ((mouse-point (nth 1 (nth 1 event))))
(when (numberp mouse-point)
(let ((node-id (kite--dom-node-at-point mouse-point)))
(when (not (eq node-id kite--dom-highlighted-node-id))
(setq kite--dom-highlighted-node-id node-id)
(remove-overlays nil nil 'kite-dom-node-highlight t)
(if (null node-id)
(kite-send "DOM.hideHighlight")
(let* ((dom-node (kite--dom-node-for-id node-id))
(overlay (make-overlay
(kite-dom-node-line-begin dom-node)
(kite-dom-node-line-end dom-node))))
(overlay-put overlay 'kite-dom-node-highlight t)
(overlay-put overlay 'priority 10)
(overlay-put overlay 'face 'kite-node-highlight-face))
(kite-send
"DOM.highlightNode"
:params
(list
:nodeId node-id
:highlightConfig
`((showInfo . nil)
(contentColor . ,(kite--rgba 255 0 0 0.5))
(paddingColor . ,(kite--rgba 0 255 0 0.5))
(borderColor . ,(kite--rgba 0 0 255 0.5))
(marginColor . ,(kite--rgba 255 255 0 0.5)))))))))))
(define-derived-mode kite-dom-mode special-mode "kite-dom"
"Toggle kite dom mode."
:group 'kite
(setq kite-buffer-type 'dom)
(setq buffer-read-only nil)
(set (make-local-variable 'kite--dom-highlighted-node-id) nil)
(set (make-local-variable 'kite--dom-pending-set-node-values) nil)
(set (make-local-variable 'track-mouse) t)
(let ((inhibit-read-only t))
(erase-buffer)
(remove-overlays)
(save-excursion
(when (kite-session-document-root kite-session)
(kite--dom-insert-document
(kite-session-document-root kite-session))
(widget-setup))))
(run-mode-hooks 'kite-dom-mode-hook)
(kite-send
"CSS.enable"
:success-function
(lambda (result)
(kite-send
"CSS.getAllStyleSheets"
:success-function
(lambda (result)
(kite--log "CSS.getAllStyleSheets got result" result)
(run-hooks 'kite-async-init-hook))))))
(defun kite--dom-insert-document (root-plist)
"Insert the whole HTML document into the DOM buffer, given the
ROOT-PLIST as received in the response to `DOM.getDocument'."
(let ((html-plist
(find-if
(lambda (child-plist)
(string= (plist-get child-plist :nodeName) "HTML"))
(plist-get root-plist :children))))
(if html-plist
(progn
(kite--dom-insert-element
(or (kite--dom-node-for-id (plist-get html-plist :nodeId))
(kite--dom-create-node html-plist
nil)))
(insert "\n"))
(error "Document doesn't seem to contain html element"))))
(defconst kite-dom-offset 2
"Number of spaces to use for each DOM indentation level.")
(defun kite--dom-notify-widget (widget &rest ignore)
"Experimental callback for widget-validate. Highlight the
widget with `modified-face' to show that its contents have been
edited."
(let ((modified-face (widget-get widget :modified-value-face)))
(unless (or (null modified-face)
(eq (widget-get widget :value-face)
modified-face))
(widget-put widget :value-face modified-face)
(overlay-put (widget-get widget :field-overlay)
'face modified-face))))
(defun kite--dom-validate-widget (widget)
"Experimental callback for widget-validate."
(let ((val (widget-apply widget :value-get)))
(unless (> (length val) 0)
(widget-put widget :error "too short!")
widget)))
(defun kite--dom-render-attribute (dom-node dom-attr)
"Render an attribute of DOM-NODE, the one designated by
DOM-ATTR, at point."
(setf (kite-dom-attr-outer-begin dom-attr) (point-marker))
(widget-insert (concat
" "
(propertize
(kite-dom-attr-name dom-attr)
'face 'kite-attribute-local-name-face
'font-lock-face 'kite-attribute-local-name-face)
"="))
(setf (kite-dom-attr-value-begin dom-attr) (point-marker))
(setf (kite-dom-attr-value-widget dom-attr)
(widget-create
'editable-field
:size 0
:format (propertize
"\"%v\""
'face 'kite-attribute-value-delimiter-face)
:value-face 'kite-attribute-value-face
:modified-value-face 'kite-modified-attribute-value-face
:kite-node-id (kite-dom-node-id dom-node)
:kite-attr dom-attr
:keymap kite--dom-widget-field-keymap
:notify
(lambda (widget &rest ignore)
(unless (or kite--dom-setting-remote-value
(string=
(widget-value widget)
(kite-dom-attr-value
(widget-get widget :kite-attr))))
(lexical-let ((lex-widget widget))
(push
(list (widget-get widget :kite-node-id)
(kite-dom-attr-name
(widget-get widget :kite-attr))
(widget-value widget))
kite--dom-pending-set-attribute-values)
(kite-send
"DOM.setAttributeValue"
:params
(list :nodeId
(widget-get widget :kite-node-id)
:name
(kite-dom-attr-name
(widget-get widget :kite-attr))
:value
(widget-value widget))
:success-function
(lambda (result)
(setcdr
(last kite--dom-pending-set-attribute-values 2)
nil))
:error-function
(lambda (error-result)
(setcdr
(last kite--dom-pending-set-attribute-values 2)
nil)
(kite--default-error-handler error-result))))))
(kite-dom-attr-value dom-attr)))
(setf (kite-dom-attr-value-end dom-attr) (point-marker))
(setf (kite-dom-attr-outer-end dom-attr) (point-marker))
(put-text-property (kite-dom-attr-outer-begin dom-attr)
(kite-dom-attr-outer-end dom-attr)
'kite-attr dom-attr))
(defun kite--dom-render-attr-alist (dom-node)
"Insert all attributes of ELEMENT. ELEMENT is a plist
representing the element and its attributes, as provided by the
remote debugger. Return a list of (ATTR-NAME . KITE-DOM-ATTR) cons
cells in the same order as the attributes in the element plist."
(dolist (dom-attr (mapcar 'cdr
(kite-dom-node-attr-alist dom-node)))
(kite--dom-render-attribute dom-node dom-attr)))
(defun kite--dom-leading-whitespace (node1 node2)
"Return the whitespace required before NODE2 given that it is
preceded by NODE1. NODE1 may be nil, which indicates that NODE2
is the first child."
(cond
((eq (kite-dom-node-type node2) kite-dom-element-node)
(concat
"\n"
(make-string (* kite-dom-offset (kite-dom-node-indent node2))
32)))
(t
"")))
(defun kite--dom-trailing-whitespace (node1 node2)
"Return the whitespace required after NODE1 given that it is
followed by NODE2. NODE2 may be nil, which indicates that NODE1
is the last child."
(cond
((or (eq (kite-dom-node-type node1) kite-dom-text-node)
(and (not (null node2))
(eq (kite-dom-node-type node2) kite-dom-text-node)))
"")
((null node2)
(concat
"\n "
(make-string (* kite-dom-offset (- (kite-dom-node-indent node1)
1))
32)))
(t
"")))
(defun kite--dom-update-inner-whitespace (dom-node)
"For each child, update leading and trailing whitespace."
(save-excursion
(let* ((children (kite-dom-node-children dom-node))
(last-child (- (length children) 1))
(overlays (overlays-in 0 (buffer-size)))
(overlay-lengths (mapcar (lambda (overlay)
(- (overlay-end overlay)
(overlay-start overlay)))
overlays)))
(loop for index from 0 upto last-child do
(let* ((prev-child (and (> index 0)
(nth (- index 1) children)))
(child (nth index children))
(next-child (and (< index last-child)
(nth (1+ index) children))))
;; delete any leading whitespace
(delete-region (kite-dom-node-line-begin child)
(kite-dom-node-outer-begin child))
;; insert leading whitespace
(goto-char (kite-dom-node-line-begin child))
(let ((save-point (point)))
(widget-insert
(propertize
(kite--dom-leading-whitespace prev-child child)
'kite-node-id (kite-dom-node-id child)))
(set-marker (kite-dom-node-line-begin child)
save-point)
(set-marker (kite-dom-node-outer-begin child)
(point)))
;; delete any trailing whitespace
(delete-region (kite-dom-node-outer-end child)
(kite-dom-node-line-end child))
;; insert trailing whitespace
(goto-char (kite-dom-node-outer-end child))
(let ((save-point (point)))
(widget-insert
(propertize
(kite--dom-trailing-whitespace child next-child)
'kite-node-id (kite-dom-node-id child)))
(set-marker (kite-dom-node-outer-end child)
save-point)
(set-marker (kite-dom-node-line-end child)
(point)))))
(mapcar*
(lambda (overlay old-length)
(kite--log "overlay %s old-length %s" overlay old-length)
(move-overlay overlay
(overlay-start overlay)
(+ (overlay-start overlay)
old-length)))
overlays overlay-lengths))))
(defun kite--dom-create-node (element-plist parent)
"Create a `kite-dom-node' structure with the information from
the given ELEMENT-PLIST and the given PARENT node, which should
also be a `kite-dom-node' structure or nil. Also add the new
node to the session's `kite-session-dom-nodes' hash map.
Return the newly created node."
(let ((dom-node
(make-kite-dom-node
:parent parent
:id (plist-get element-plist :nodeId)
:type (plist-get element-plist :nodeType)
:value (plist-get element-plist :nodeValue)
:child-count (plist-get element-plist :childNodeCount)
:local-name (plist-get element-plist :localName)
:indent (if parent (1+ (kite-dom-node-indent parent)) 0)
:attr-alist
(let* (attribute-structs
(attributes (plist-get element-plist :attributes))
(attr-index (- (length attributes) 2)))
(while (>= attr-index 0)
(push (cons
(elt attributes attr-index)
(make-kite-dom-attr
:name (elt attributes attr-index)
:value (elt attributes (1+ attr-index))))
attribute-structs)
(setq attr-index (- attr-index 2)))
attribute-structs))))
(setf (kite-dom-node-children dom-node)
(mapcar (lambda (child)
(kite--dom-create-node child dom-node))
(plist-get element-plist :children)))
(puthash (kite-dom-node-id dom-node)
dom-node
(kite-session-dom-nodes kite-session))
dom-node))
(defun kite--dom-insert-element (dom-node)
"Render given ELEMENT at point."
(setf (kite-dom-node-line-begin dom-node) (point-marker))
(flet
((indent-prefix (indent node-id)
(propertize
(make-string (* kite-dom-offset indent) 32)
'kite-node-id node-id
'read-only t)))
(let ((inhibit-read-only t)
(inhibit-modification-hooks t))
(cond
((and (eq (kite-dom-node-type dom-node)
kite-dom-element-node)
(or (eq 0 (kite-dom-node-child-count dom-node))
(kite-dom-node-children dom-node)))
(if (> (kite-dom-node-child-count dom-node) 0)
(widget-insert (if (kite-dom-node-children dom-node)
"-"
"+"))
(widget-insert " "))
(widget-insert (propertize "<"
'kite-node-id (kite-dom-node-id
dom-node)
'face 'kite-tag-delimiter-face
'field 'boundary))
(setf (kite-dom-node-widget dom-node)
(widget-create
'editable-field
:size 1
:value-face 'kite-element-local-name-face
:modified-value-face
'kite-modified-element-local-name-face
:notify (function kite--dom-notify-widget)
:validate (function kite--dom-validate-widget)
:match (lambda (x) (> (length (widget-value x)) 0))
:kite-node-id (kite-dom-node-id dom-node)
:keymap kite--dom-widget-field-keymap
(kite-dom-node-local-name dom-node)))
(kite--dom-render-attr-alist dom-node)
(widget-insert
(concat (propertize ">"
'kite-node-id (kite-dom-node-id dom-node)
'read-only t
'face 'kite-tag-delimiter-face)))
(setf (kite-dom-node-inner-begin dom-node) (point-marker))
(put-text-property (kite-dom-node-line-begin dom-node)
(kite-dom-node-inner-begin dom-node)
'kite-node-id
(kite-dom-node-id dom-node))
(mapc #'kite--dom-insert-element
(kite-dom-node-children dom-node))
(setf (kite-dom-node-inner-end dom-node) (point-marker))
(widget-insert
(concat
(propertize "<"
'face 'kite-tag-delimiter-face
'field 'boundary)
(propertize "/" 'face 'kite-tag-slash-face)
(propertize (kite-dom-node-local-name dom-node)
'face 'kite-element-local-name-face)
(propertize ">" 'face 'kite-tag-delimiter-face)))
(put-text-property (kite-dom-node-inner-end dom-node)
(point)
'kite-node-id
(kite-dom-node-id dom-node)))
((eq (kite-dom-node-type dom-node)
kite-dom-element-node)
(widget-insert "+")
(widget-insert (propertize "<"
'face 'kite-tag-delimiter-face
'field 'boundary))
(setf (kite-dom-node-widget dom-node)
(widget-create
'editable-field
:size 1
:value-face 'kite-element-local-name-face
:modified-value-face
'kite-modified-element-local-name-face
:notify (function kite--dom-notify-widget)
:validate (function kite--dom-validate-widget)
:match (lambda (x) (> (length (widget-value x)) 0))
:kite-node-id (kite-dom-node-id dom-node)
:keymap kite--dom-widget-field-keymap
(kite-dom-node-local-name dom-node)))
(kite--dom-render-attr-alist dom-node)
(widget-insert (propertize ">"
'face 'kite-tag-delimiter-face))
(setf (kite-dom-node-inner-begin dom-node) (point-marker))
(widget-insert "...")
(setf (kite-dom-node-inner-end dom-node) (point-marker))
(widget-insert
(concat
(propertize "<"
'face 'kite-tag-delimiter-face
'field 'boundary)
(propertize "/" 'face 'kite-tag-slash-face)
(propertize (kite-dom-node-local-name dom-node)
'face 'kite-element-local-name-face)
(propertize ">" 'face 'kite-tag-delimiter-face)))
(put-text-property (kite-dom-node-line-begin dom-node)
(point)
'kite-node-id
(kite-dom-node-id dom-node)))
((eq (kite-dom-node-type dom-node)
kite-dom-text-node)
(setf (kite-dom-node-widget dom-node)
(widget-create
'editable-field
:value-face 'kite-text-face
:value-create
(lambda (widget)
"Evil hack to avoid newline in editable field."
;; Note: we have to use a :size of nil (rather than
;; 0) because otherwise `widget-after-change' will
;; trim trailing whitespace.
;; Temporarily set field size to 0 because otherwise
;; `widget-field-value-create' will insert a trailing
;; newline
(widget-put widget :size 0)
(widget-field-value-create widget)
(widget-put widget :size nil)
;; Since no trailing newline was inserted the overlay
;; is one character too long, so make it shorter
(let ((overlay-end
(cdr (widget-get widget :field-overlay))))
(move-marker overlay-end
(1- (marker-position overlay-end)))))
:notify
(lambda (widget child &optional event)
(let ((dom-node (kite--dom-node-for-id
(widget-get widget :kite-node-id))))
(when (and
(or (not (boundp
'kite--dom-setting-remote-value))
(not kite--dom-setting-remote-value))
(not (string=
(widget-value widget)
(kite-dom-node-value dom-node))))
(push (cons
(kite-dom-node-id dom-node)
(widget-value widget))
kite--dom-pending-set-node-values)
(kite-send
"DOM.setNodeValue"
:params
(list :nodeId (widget-get widget
:kite-node-id)
:value (widget-value widget))
:success-function
(lambda (result)
(setcdr
(last kite--dom-pending-set-node-values 2)
nil))
:error-function
(lambda (error-result)
(setcdr
(last kite--dom-pending-set-node-values 2)
nil)
(kite--default-error-handler error-result))))))
:validate (function kite--dom-validate-widget)
:kite-node-id (kite-dom-node-id dom-node)
:keymap kite--dom-widget-field-keymap
(kite-dom-node-value dom-node))))
((eq (kite-dom-node-type dom-node)
kite-dom-comment-node)
(widget-insert
(propertize
"<!--"
'face 'kite-comment-delimiter-face
'field 'boundary))
(setf (kite-dom-node-widget dom-node)
(widget-create
'editable-field
:size 1
:value-face 'kite-comment-content-face
:modified-value-face
'kite-modified-comment-content-face
:notify (function kite--dom-notify-widget)
:validate (function kite--dom-validate-widget)
:kite-node-id (kite-dom-node-id dom-node)
:keymap kite--dom-widget-field-keymap
(kite-dom-node-value dom-node)))
(widget-insert
(propertize
"-->"
'face 'kite-comment-delimiter-face)
"\n")
(put-text-property (kite-dom-node-line-begin dom-node)
(point)
'kite-node-id
(kite-dom-node-id dom-node)))
(t
(error "Unknown node type %s" (kite-dom-node-type dom-node))))
(setf (kite-dom-node-line-end dom-node) (point-marker))
(setf (kite-dom-node-outer-begin dom-node)
(copy-marker (kite-dom-node-line-begin dom-node)))
(setf (kite-dom-node-outer-end dom-node)
(copy-marker (kite-dom-node-line-end dom-node)))
(when (eq (kite-dom-node-type dom-node)
kite-dom-element-node)
(kite--dom-update-inner-whitespace dom-node)))
(when (kite-dom-node-inner-begin dom-node)
(set-marker-insertion-type (kite-dom-node-inner-begin dom-node)
nil)
(set-marker-insertion-type (kite-dom-node-inner-end dom-node)
t))
(when (kite-dom-node-outer-begin dom-node)
(set-marker-insertion-type (kite-dom-node-outer-begin dom-node)
t)
(set-marker-insertion-type (kite-dom-node-outer-end dom-node)
nil))
(when (kite-dom-node-line-begin dom-node)
(set-marker-insertion-type (kite-dom-node-line-begin dom-node)
t)
(set-marker-insertion-type (kite-dom-node-line-end dom-node)
nil))))
(defun kite--dom-delete-child-widgets (dom-node)
"Delete all widgets in the dom-node's children,
recursively."
(dolist (child (kite-dom-node-children dom-node))
(kite--dom-delete-widgets child)))
(defun kite--dom-delete-widgets (dom-node)
"Delete the dom-node's widgets and all its children's
widgets, recursively"
(when (kite-dom-node-widget dom-node)
(widget-delete (kite-dom-node-widget dom-node))
(setf (kite-dom-node-widget dom-node)))
(dolist (dom-attr (mapcar 'cdr
(kite-dom-node-attr-alist dom-node)))
(when (kite-dom-attr-value-widget dom-attr)
(widget-delete (kite-dom-attr-value-widget dom-attr))
(setf (kite-dom-attr-value-widget dom-attr))))
(kite--dom-delete-child-widgets dom-node))
(defun kite--dom-set-element-toggle-char (dom-node char)
"Set the character denoting whether an element is opened,
closed, or doesn't have any child nodes, for DOM-NODE to
CHARACTER."
(save-excursion
(goto-char (kite-dom-node-outer-begin dom-node))