org-element--collect-affiliated-keywords: Fix keywords before comment

* lisp/org-element.el (org-element--collect-affiliated-keywords):
Disallow affiliated keywords before a comment.
*
testing/lisp/test-org-element.el (test-org-element/affiliated-keywords-parser):
Add new test.
*
testing/lisp/test-org-element.el (test-org-element/cache-affiliated):
Fix test.

Reported-by: Tom Alexander <tom@fizz.buzz>
Link: https://orgmode.org/list/87c20267-d12e-4641-a1eb-3a7ac8181069@app.fastmail.com
This commit is contained in:
Ihor Radchenko 2023-10-12 14:24:35 +03:00
parent 07ae6ba58d
commit 0e3d0e3d19
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 11 additions and 2 deletions

View File

@ -4790,7 +4790,10 @@ When PARSE is non-nil, values from keywords belonging to
(forward-line)))
;; If affiliated keywords are orphaned: move back to first one.
;; They will be parsed as a paragraph.
(when (looking-at-p "[ \t]*$") (goto-char origin) (setq output nil))
(when (or (looking-at-p "[ \t]*$")
;; Affiliated keywords are not allowed before comments.
(looking-at-p org-comment-regexp))
(goto-char origin) (setq output nil))
;; Return value.
(cons origin output))))

View File

@ -872,6 +872,12 @@ Some other text
(should-not
(org-test-with-temp-text "- item\n #+name: name\nSome paragraph"
(progn (search-forward "Some")
(org-element-property :name (org-element-at-point)))))
;; Corner case: orphaned keyword before comment.
;; Comments cannot have affiliated keywords.
(should-not
(org-test-with-temp-text "#+name: foo\n# bar"
(progn (search-forward "bar")
(org-element-property :name (org-element-at-point))))))
@ -5077,7 +5083,7 @@ Text
line"
(org-element-cache-map #'ignore :granularity 'element)
(should (eq 'keyword (org-element-type (org-element-at-point))))
(insert "#")
(insert "1")
(should (eq 2 (org-element-property :begin (org-element-at-point)))))))
(ert-deftest test-org-element/cache-table ()