Skip to content

Commit 85aeb13

Browse files
committed
DXML-13 keep whitespace text nodes by default
1 parent 5235ed0 commit 85aeb13

2 files changed

Lines changed: 47 additions & 8 deletions

File tree

src/main/clojure/clojure/data/xml/jvm/parse.clj

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
(defn pull-seq
6868
"Creates a seq of events. The XMLStreamConstants/SPACE clause below doesn't seem to
6969
be triggered by the JDK StAX parser, but is by others. Leaving in to be more complete."
70-
[^XMLStreamReader sreader {:keys [include-node? location-info] :as opts} ns-envs]
70+
[^XMLStreamReader sreader {:keys [include-node? location-info skip-whitespace] :as opts} ns-envs]
7171
(lazy-seq
7272
(loop []
7373
(let [location (when location-info
@@ -94,10 +94,13 @@
9494
(recur))
9595
XMLStreamConstants/CHARACTERS
9696
(if-let [text (and (include-node? :characters)
97-
(not (.isWhiteSpace sreader))
97+
(not (and skip-whitespace
98+
(.isWhiteSpace sreader)))
9899
(.getText sreader))]
99-
(cons (->CharsEvent text)
100-
(pull-seq sreader opts ns-envs))
100+
(if (zero? (.length ^CharSequence text))
101+
(recur)
102+
(cons (->CharsEvent text)
103+
(pull-seq sreader opts ns-envs)))
101104
(recur))
102105
XMLStreamConstants/COMMENT
103106
(if (include-node? :comment)

src/test/clojure/clojure/data/xml/test_parse.clj

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@
4141

4242
(deftest test-xml-with-whitespace
4343
(let [input (str "<a>\n<b with-attr=\"s p a c e\">123</b>\n<c>1 2 3</c>\n\n</a>")
44-
expected (element :a {}
45-
(element :b {:with-attr "s p a c e"} "123")
46-
(element :c {} "1 2 3"))]
44+
expected (element :a {}
45+
"\n"
46+
(element :b {:with-attr "s p a c e"} "123")
47+
"\n"
48+
(element :c {} "1 2 3")
49+
"\n\n")]
4750
(is (= expected (lazy-parse* input)))))
4851

4952
(deftest test-cdata-parse
@@ -89,7 +92,40 @@
8992
(is (= 1 (-> input parse-str location-meta :column-number)))
9093
(is (= 1 (-> input parse-str :content first location-meta :line-number)))
9194
(is (= 4 (-> input parse-str :content first location-meta :column-number)))
92-
(is (= 2 (-> input parse-str :content second location-meta :line-number)))
95+
(is (= 2 (-> input (parse-str :skip-whitespace true) :content second location-meta :line-number)))
9396
(is (nil? (-> input
9497
(parse-str :location-info false)
9598
location-meta)))))
99+
100+
(deftest test-ignorable-whitespace
101+
;; FIXME implement clojure.lang.MapEquivalence for records
102+
(clojure.lang.APersistentMap/mapEquals
103+
(parse-str "<?xml version=\"1.0\"?>
104+
<!DOCTYPE methodCall [
105+
<!ELEMENT methodCall (methodName, params)>
106+
<!ELEMENT params (param+)>
107+
<!ELEMENT param (value)>
108+
<!ELEMENT value (string)>
109+
<!ELEMENT methodName (#PCDATA)>
110+
<!ELEMENT string (#PCDATA)>
111+
]>
112+
<methodCall>
113+
<methodName>lookupSymbol</methodName>
114+
<params>
115+
<param>
116+
<value>
117+
<string>
118+
Clojure XML &lt;3
119+
</string>
120+
</value>
121+
</param>
122+
</params>
123+
</methodCall>")
124+
{:tag :methodCall, :attrs {}, :content
125+
[{:tag :methodName, :attrs {}, :content
126+
["lookupSymbol"]}
127+
{:tag :params, :attrs {}, :content
128+
[{:tag :param, :attrs {}, :content
129+
[{:tag :value, :attrs {}, :content
130+
[{:tag :string, :attrs {}, :content
131+
["\n Clojure XML <3 \n "]}]}]}]}]}))

0 commit comments

Comments
 (0)