Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle CDATA with UTF-8 characters when partial parsing #133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions lib/saxy/parser/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -889,12 +889,9 @@ defmodule Saxy.Parser.Builder do
token in unquote(edge_ngrams("]]")) when more? ->
halt!(element_cdata(token, more?, original, pos, state, len))

char <> rest when is_ascii(char) ->
char <> rest ->
element_cdata(rest, more?, original, pos, state, len + 1)

<<codepoint::utf8>> <> rest ->
element_cdata(rest, more?, original, pos, state, len + Utils.compute_char_len(codepoint))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can the same way how dangling UTF-8 fragments is handled

For example https://github.com/qcam/saxy/blob/master/lib/saxy/parser/builder.ex#L540-L541


_ ->
Utils.parse_error(original, pos + len, state, {:token, :"]]"})
end
Expand Down
5 changes: 5 additions & 0 deletions test/saxy/parser/element_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ defmodule Saxy.Parser.ElementTest do
assert Exception.message(error) == "unexpected end of input, expected token: :\"]]\""
end

test "handles CDATA with UTF-8 encoded £ symbol" do
events = assert_parse("<salaryTo><![CDATA[£26,000]]></salaryTo>")
assert find_events(events, :characters) == [{:characters, "£26,000"}]
end

test "parses processing instruction" do
events = assert_parse("<foo><?hello the instruction?></foo>")
assert length(events) == 2
Expand Down