⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 291712 in webkit


Ignore:
Timestamp:
Mar 22, 2022, 3:22:09 PM (5 years ago)
Author:
Patrick Angle
Message:

Source/WebInspectorUI:
Web Inspector: console.log format strings containing invalid specifiers results in [Object object] replacing the specifier instead of ignoring the invalid specifier
​https://bugs.webkit.org/show_bug.cgi?id=235061

Reviewed by Devin Rousso.

Logged console messages previously allows an specifier, even invalid ones, to be replaced with values. We should
instead ignore the invalid specifier to match Firefox's behavior. (Chrome's behavior currently is to remove the
invalid specifier from the result without replacing it with the first next available substitution value.)

Other call sites using String.format are handling localized strings or other backend-provided strings that
should always be well-formatted, so falling back to handling a format specifier as a string is not a particularly
useful behavior.

  • UserInterface/Base/Utilities.js:

(value):

LayoutTests:
Web Inspector: console.log print strings contains '%_' result is Object
​https://bugs.webkit.org/show_bug.cgi?id=235061

Reviewed by Devin Rousso.

  • inspector/unit-tests/string-utilities-expected.txt:
  • inspector/unit-tests/string-utilities.html:
Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r291710 r291712  
     12022-03-22  Patrick Angle  <pangle@apple.com>
     2
     3        Web Inspector: console.log print strings contains '%_' result is Object
     4        https://bugs.webkit.org/show_bug.cgi?id=235061
     5
     6        Reviewed by Devin Rousso.
     7
     8        * inspector/unit-tests/string-utilities-expected.txt:
     9        * inspector/unit-tests/string-utilities.html:
     10       
    1112022-03-22  Matteo Flores  <matteo_flores@apple.com>
    212
  • trunk/LayoutTests/inspector/unit-tests/string-utilities-expected.txt

    r286611 r291712  
    2020PASS: integer format specifier with Infinity argument should show "NaN"
    2121PASS: integer format specifier with NaN argument should show "NaN"
     22Testing string with unknown specifier.
     23WARN: String.format("%_ %s", "first", "second"): Unsupported format specifier "%_" will be ignored.
     24PASS: Formatter string with unknown specifier should not replace unknown specifier.
     25PASS: Formatter string with unknown specifier should have an unused substitution value.
    2226
    2327-- Running test case: String.prototype.extendedLocaleCompare
  • trunk/LayoutTests/inspector/unit-tests/string-utilities.html

    r286611 r291712  
    3131            InspectorTest.expectThat("%d".format(Infinity) === "NaN", "integer format specifier with Infinity argument should show \"NaN\"");
    3232            InspectorTest.expectThat("%d".format(NaN) === "NaN", "integer format specifier with NaN argument should show \"NaN\"");
     33
     34            InspectorTest.log("Testing string with unknown specifier.");
     35            let formatWithUnknownSpecifiers = String.format("%_ %s", ["first", "second"], String.standardFormatters, "", function(a, b) { return a + b; });
     36            InspectorTest.expectThat(formatWithUnknownSpecifiers.formattedResult === "%_ first", "Formatter string with unknown specifier should not replace unknown specifier.");
     37            InspectorTest.expectThat(formatWithUnknownSpecifiers.unusedSubstitutions[0] === "second", "Formatter string with unknown specifier should have an unused substitution value.");
     38
    3339            return true;
    3440        }
  • trunk/Source/WebInspectorUI/ChangeLog

    r291628 r291712  
     12022-03-22  Patrick Angle  <pangle@apple.com>
     2
     3        Web Inspector: `console.log` format strings containing invalid specifiers results in `[Object object]` replacing the specifier instead of ignoring the invalid specifier
     4        https://bugs.webkit.org/show_bug.cgi?id=235061
     5
     6        Reviewed by Devin Rousso.
     7
     8        Logged console messages previously allows an specifier, even invalid ones, to be replaced with values. We should
     9        instead ignore the invalid specifier to match Firefox's behavior. (Chrome's behavior currently is to remove the
     10        invalid specifier from the result without replacing it with the first next available substitution value.)
     11
     12        Other call sites using `String.format` are handling localized strings or other backend-provided strings that
     13        should always be well-formatted, so falling back to handling a format specifier as a string is not a particularly
     14        useful behavior.
     15
     16        * UserInterface/Base/Utilities.js:
     17        (value):
     18
    1192022-03-22  Nikita Vasilyev  <nvasilyev@apple.com>
    220
  • trunk/Source/WebInspectorUI/UserInterface/Base/Utilities.js

    r287590 r291712  
    11261126        var tokens = String.tokenizeFormatString(format);
    11271127        var usedSubstitutionIndexes = {};
     1128        let ignoredUnknownSpecifierCount = 0;
    11281129
    11291130        for (var i = 0; i < tokens.length; ++i) {
    … …  
    11401141            }
    11411142
    1142             if (token.substitutionIndex >= substitutions.length) {
     1143            let substitutionIndex = token.substitutionIndex - ignoredUnknownSpecifierCount;
     1144            if (substitutionIndex >= substitutions.length) {
    11431145                // If there are not enough substitutions for the current substitutionIndex
    11441146                // just output the format specifier literally and move on.
    1145                 error("not enough substitution arguments. Had " + substitutions.length + " but needed " + (token.substitutionIndex + 1) + ", so substitution was skipped.");
     1147                error("not enough substitution arguments. Had " + substitutions.length + " but needed " + (substitutionIndex + 1) + ", so substitution was skipped.");
    11461148                result = append(result, "%" + (token.precision > -1 ? token.precision : "") + token.specifier);
    11471149                continue;
    11481150            }
    11491151
    1150             usedSubstitutionIndexes[token.substitutionIndex] = true;
    1151 
    11521152            if (!(token.specifier in formatters)) {
    1153                 // Encountered an unsupported format character, treat as a string.
    1154                 warn("unsupported format character \u201C" + token.specifier + "\u201D. Treating as a string.");
    1155                 result = append(result, substitutions[token.substitutionIndex]);
     1153                warn(`Unsupported format specifier "%${token.specifier}" will be ignored.`);
     1154                result = append(result, "%" + token.specifier);
     1155                ++ignoredUnknownSpecifierCount;
    11561156                continue;
    11571157            }
    11581158
    1159             result = append(result, formatters[token.specifier](substitutions[token.substitutionIndex], token));
     1159            usedSubstitutionIndexes[substitutionIndex] = true;
     1160            result = append(result, formatters[token.specifier](substitutions[substitutionIndex], token));
    11601161        }
    11611162
Note: See TracChangeset for help on using the changeset viewer.