Skip to content

Commit 2a4bdc9

Browse files
authored
CALL TRANSFORMATION: fix byte order mark (#901)
1 parent 3ca1e63 commit 2a4bdc9

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

src/conv/cl_abap_conv_in_ce.clas.abap

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,15 @@ CLASS cl_abap_conv_in_ce IMPLEMENTATION.
102102

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

src/kernel/call_transformation/kernel_call_transformation.clas.locals_imp.abap

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,11 +548,22 @@ ENDCLASS.
548548

549549
CLASS lcl_string_to_string IMPLEMENTATION.
550550
METHOD run.
551+
DATA lv_str_bom TYPE string.
552+
DATA lv_hex_bom TYPE xstring.
553+
551554
* this is not right, but works for the unit test
552555
WRITE '@KERNEL result.set(INPUT.source.get());'.
553556

557+
lv_hex_bom = cl_abap_char_utilities=>byte_order_mark_little.
558+
lv_str_bom = cl_abap_codepage=>convert_from(
559+
source = lv_hex_bom
560+
codepage = 'UTF-16' ).
561+
554562
IF options-xml_header = 'no'.
555-
REPLACE FIRST OCCURRENCE OF REGEX '<?.*?>' IN result WITH ''.
563+
REPLACE FIRST OCCURRENCE OF REGEX '<\?.*\?>' IN result WITH ''.
564+
" WRITE '@KERNEL console.dir(lv_str_bom);'.
565+
CONCATENATE lv_str_bom result INTO result.
566+
" WRITE '@KERNEL console.dir(result);'.
556567
ENDIF.
557568
ENDMETHOD.
558569
ENDCLASS.

src/kernel/call_transformation/kernel_call_transformation.clas.testclasses.abap

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ CLASS ltcl_call_transformation DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATI
128128
METHODS suppress3 FOR TESTING RAISING cx_static_check.
129129
METHODS xml_to_xml FOR TESTING RAISING cx_static_check.
130130
METHODS xml_to_xml_rm_header FOR TESTING RAISING cx_static_check.
131+
METHODS xml_to_xml_rm_header_bom FOR TESTING RAISING cx_static_check.
131132
ENDCLASS.
132133

133134
CLASS ltcl_call_transformation IMPLEMENTATION.
@@ -1119,4 +1120,26 @@ CLASS ltcl_call_transformation IMPLEMENTATION.
11191120

11201121
ENDMETHOD.
11211122

1123+
METHOD xml_to_xml_rm_header_bom.
1124+
1125+
DATA lv_xml TYPE string.
1126+
DATA lv_str_bom TYPE string.
1127+
DATA lv_hex_bom TYPE xstring.
1128+
1129+
lv_xml = |<foo>2</foo>|.
1130+
CALL TRANSFORMATION id SOURCE XML lv_xml RESULT XML lv_xml OPTIONS xml_header = 'no'.
1131+
1132+
lv_hex_bom = cl_abap_char_utilities=>byte_order_mark_little.
1133+
lv_str_bom = cl_abap_codepage=>convert_from(
1134+
source = lv_hex_bom
1135+
codepage = 'UTF-16' ).
1136+
1137+
lv_xml = lv_xml(1).
1138+
1139+
cl_abap_unit_assert=>assert_equals(
1140+
act = lv_xml
1141+
exp = lv_str_bom ).
1142+
1143+
ENDMETHOD.
1144+
11221145
ENDCLASS.

0 commit comments

Comments
 (0)