Skip to content

Commit b6661d4

Browse files
CopilotKinneyzhang
andcommitted
Fix spaces tracking in AST nodes
- Add spaces-before field to parser to accumulate whitespace - Fix css-make-node to create unique spaces plist for each node (was sharing same list) - Update css-parser-new-node to attach accumulated spaces to node's :before field - Update css-parser-space to accumulate whitespace instead of skipping - Fix css-parser-word to only apply accumulated spaces to first node in compound selector - Now correctly preserves whitespace when parsing and stringifying selectors Co-authored-by: Kinneyzhang <38454496+Kinneyzhang@users.noreply.github.com>
1 parent bffa4ee commit b6661d4

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

css-selector-parser.el

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ TYPE是节点类型,PROPS是属性列表。"
307307
(setq node (plist-put node (car props) (cadr props))
308308
props (cddr props)))
309309
(unless (plist-get node :spaces)
310-
(setq node (plist-put node :spaces '(:before "" :after ""))))
310+
(setq node (plist-put node :spaces (list :before "" :after ""))))
311311
node))
312312

313313
(defun css-make-root (&rest props)
@@ -406,7 +406,8 @@ TYPE是节点类型,PROPS是属性列表。"
406406
tokens ; token数组
407407
position ; 当前位置
408408
root ; 根节点
409-
current) ; 当前选择器节点
409+
current ; 当前选择器节点
410+
spaces-before) ; 累积的前置空白
410411

411412
(defun css-parser-curr-token (parser)
412413
"获取解析器的当前token。"
@@ -430,12 +431,20 @@ TYPE是节点类型,PROPS是属性列表。"
430431

431432
(defun css-parser-new-node (parser node)
432433
"添加新节点到当前选择器。"
434+
;; 将累积的空白附加到节点的 :before
435+
(when (css-parser-spaces-before parser)
436+
(let ((spaces (plist-get node :spaces)))
437+
(plist-put spaces :before (css-parser-spaces-before parser)))
438+
(setf (css-parser-spaces-before parser) ""))
433439
(css-node-append (css-parser-current parser) node)
434440
node)
435441

436442
(defun css-parser-space (parser)
437443
"处理空白字符。"
438-
;; 简化实现:直接跳过空白
444+
;; 累积空白字符,稍后附加到下一个节点
445+
(let ((space-content (css-parser-content parser)))
446+
(setf (css-parser-spaces-before parser)
447+
(concat (or (css-parser-spaces-before parser) "") space-content)))
439448
(cl-incf (css-parser-position parser)))
440449

441450
(defun css-parser-comment (parser)
@@ -459,7 +468,8 @@ TYPE是节点类型,PROPS是属性列表。"
459468
(let* ((content (css-parser-content parser))
460469
(i 0)
461470
(len (length content))
462-
nodes)
471+
nodes
472+
(first-node t))
463473
;; 分割单词为多个节点
464474
(while (< i len)
465475
(let ((ch (aref content i)))
@@ -492,9 +502,14 @@ TYPE是节点类型,PROPS是属性列表。"
492502
(push (css-make-tag (substring content start end)) nodes))
493503
(setq i end))))))
494504

495-
;; 添加节点
505+
;; 添加节点 - 只有第一个节点获得累积的空白
496506
(dolist (node (nreverse nodes))
497-
(css-parser-new-node parser node))
507+
(if first-node
508+
(progn
509+
(css-parser-new-node parser node)
510+
(setq first-node nil))
511+
;; 后续节点直接添加,不获得累积的空白
512+
(css-node-append (css-parser-current parser) node)))
498513
(cl-incf (css-parser-position parser))))
499514

500515
(defun css-parser-universal (parser)
@@ -649,7 +664,8 @@ SELECTOR-STRING是要解析的CSS选择器字符串。
649664
:tokens (vconcat tokens)
650665
:position 0
651666
:root root
652-
:current selector))
667+
:current selector
668+
:spaces-before ""))
653669
(css-parser-loop parser)))
654670

655671
(defun css-selector-walk (ast func)

0 commit comments

Comments
 (0)