@@ -410,7 +410,8 @@ bool InitializeICUDirectory(const std::string& path) {
410410
411411int32_t ToUnicode (MaybeStackBuffer<char >* buf,
412412 const char * input,
413- size_t length) {
413+ size_t length,
414+ bool lenient) {
414415 UErrorCode status = U_ZERO_ERROR;
415416 uint32_t options = UIDNA_DEFAULT;
416417 options |= UIDNA_NONTRANSITIONAL_TO_UNICODE;
@@ -435,7 +436,7 @@ int32_t ToUnicode(MaybeStackBuffer<char>* buf,
435436 &status);
436437 }
437438
438- if (U_FAILURE (status)) {
439+ if (U_FAILURE (status) || (!lenient && info. errors != 0 ) ) {
439440 len = -1 ;
440441 buf->SetLength (0 );
441442 } else {
@@ -448,7 +449,8 @@ int32_t ToUnicode(MaybeStackBuffer<char>* buf,
448449
449450int32_t ToASCII (MaybeStackBuffer<char >* buf,
450451 const char * input,
451- size_t length) {
452+ size_t length,
453+ bool lenient) {
452454 UErrorCode status = U_ZERO_ERROR;
453455 uint32_t options = UIDNA_DEFAULT;
454456 options |= UIDNA_NONTRANSITIONAL_TO_ASCII;
@@ -473,7 +475,7 @@ int32_t ToASCII(MaybeStackBuffer<char>* buf,
473475 &status);
474476 }
475477
476- if (U_FAILURE (status)) {
478+ if (U_FAILURE (status) || (!lenient && info. errors != 0 ) ) {
477479 len = -1 ;
478480 buf->SetLength (0 );
479481 } else {
@@ -489,8 +491,11 @@ static void ToUnicode(const FunctionCallbackInfo<Value>& args) {
489491 CHECK_GE (args.Length (), 1 );
490492 CHECK (args[0 ]->IsString ());
491493 Utf8Value val (env->isolate (), args[0 ]);
494+ // optional arg
495+ bool lenient = args[1 ]->BooleanValue (env->context ()).FromJust ();
496+
492497 MaybeStackBuffer<char > buf;
493- int32_t len = ToUnicode (&buf, *val, val.length ());
498+ int32_t len = ToUnicode (&buf, *val, val.length (), lenient );
494499
495500 if (len < 0 ) {
496501 return env->ThrowError (" Cannot convert name to Unicode" );
@@ -508,8 +513,11 @@ static void ToASCII(const FunctionCallbackInfo<Value>& args) {
508513 CHECK_GE (args.Length (), 1 );
509514 CHECK (args[0 ]->IsString ());
510515 Utf8Value val (env->isolate (), args[0 ]);
516+ // optional arg
517+ bool lenient = args[1 ]->BooleanValue (env->context ()).FromJust ();
518+
511519 MaybeStackBuffer<char > buf;
512- int32_t len = ToASCII (&buf, *val, val.length ());
520+ int32_t len = ToASCII (&buf, *val, val.length (), lenient );
513521
514522 if (len < 0 ) {
515523 return env->ThrowError (" Cannot convert name to ASCII" );
0 commit comments