Skip to content

Commit

Permalink
CALL TRANSFORMATION: fix byte order mark (#901)
Browse files Browse the repository at this point in the history
  • Loading branch information
larshp authored Aug 7, 2024
1 parent 3ca1e63 commit 2a4bdc9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/conv/cl_abap_conv_in_ce.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,15 @@ CLASS cl_abap_conv_in_ce IMPLEMENTATION.

" Try TextDecoder first, if it runs in browser,
WRITE '@KERNEL const decoder = TextDecoder || await import("util").TextDecoder;'.
WRITE '@KERNEL const td = new decoder(this.mv_js_encoding.get(), {fatal: this.mv_ignore_cerr.get() !== "X"});'.
* https://stackoverflow.com/questions/62334608/textdecoder-prototype-ignorebom-not-working-as-expected
WRITE '@KERNEL const td = new decoder(this.mv_js_encoding.get(), {fatal: this.mv_ignore_cerr.get() !== "X", ignoreBOM: true});'.
WRITE '@KERNEL try {'.
" WRITE '@KERNEL console.dir(buf);'.
WRITE '@KERNEL data.set(td.decode(buf));'.
" WRITE '@KERNEL console.dir(td.decode(buf).charCodeAt( 0 ));'.
WRITE '@KERNEL } catch (e) {'.
WRITE '@KERNEL console.dir(e);'.
WRITE '@KERNEL console.dir(this.mv_js_encoding.get());'.
* WRITE '@KERNEL console.dir(e);'.
* WRITE '@KERNEL console.dir(this.mv_js_encoding.get());'.
lv_error = abap_true.
WRITE '@KERNEL }'.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,11 +548,22 @@ ENDCLASS.

CLASS lcl_string_to_string IMPLEMENTATION.
METHOD run.
DATA lv_str_bom TYPE string.
DATA lv_hex_bom TYPE xstring.

* this is not right, but works for the unit test
WRITE '@KERNEL result.set(INPUT.source.get());'.

lv_hex_bom = cl_abap_char_utilities=>byte_order_mark_little.
lv_str_bom = cl_abap_codepage=>convert_from(
source = lv_hex_bom
codepage = 'UTF-16' ).

IF options-xml_header = 'no'.
REPLACE FIRST OCCURRENCE OF REGEX '<?.*?>' IN result WITH ''.
REPLACE FIRST OCCURRENCE OF REGEX '<\?.*\?>' IN result WITH ''.
" WRITE '@KERNEL console.dir(lv_str_bom);'.
CONCATENATE lv_str_bom result INTO result.
" WRITE '@KERNEL console.dir(result);'.
ENDIF.
ENDMETHOD.
ENDCLASS.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ CLASS ltcl_call_transformation DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATI
METHODS suppress3 FOR TESTING RAISING cx_static_check.
METHODS xml_to_xml FOR TESTING RAISING cx_static_check.
METHODS xml_to_xml_rm_header FOR TESTING RAISING cx_static_check.
METHODS xml_to_xml_rm_header_bom FOR TESTING RAISING cx_static_check.
ENDCLASS.

CLASS ltcl_call_transformation IMPLEMENTATION.
Expand Down Expand Up @@ -1119,4 +1120,26 @@ CLASS ltcl_call_transformation IMPLEMENTATION.

ENDMETHOD.

METHOD xml_to_xml_rm_header_bom.

DATA lv_xml TYPE string.
DATA lv_str_bom TYPE string.
DATA lv_hex_bom TYPE xstring.

lv_xml = |<foo>2</foo>|.
CALL TRANSFORMATION id SOURCE XML lv_xml RESULT XML lv_xml OPTIONS xml_header = 'no'.

lv_hex_bom = cl_abap_char_utilities=>byte_order_mark_little.
lv_str_bom = cl_abap_codepage=>convert_from(
source = lv_hex_bom
codepage = 'UTF-16' ).

lv_xml = lv_xml(1).

cl_abap_unit_assert=>assert_equals(
act = lv_xml
exp = lv_str_bom ).

ENDMETHOD.

ENDCLASS.

0 comments on commit 2a4bdc9

Please sign in to comment.