Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 9 additions & 2 deletions javascript/atoms/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -1173,11 +1173,18 @@ bot.dom.appendVisibleTextLinesFromTextNode_ = function (textNode, lines,
}

if (textTransform == 'capitalize') {
// the unicode regex ending with /gu does not work in IE
var re = goog.userAgent.IE ? /(^|\s|\b)(\S)/g : /(^|\s|\b)(\S)/gu;
// 1) don't treat '_' as a separator (protects snake_case)
var re = /(^|[^'_0-9A-Za-z\u00C0-\u02AF\u1E00-\u1EFF\u24B6-\u24E9\u0300-\u036F\u1AB0-\u1AFF\u1DC0-\u1DFF])([A-Za-z\u00C0-\u02AF\u1E00-\u1EFF\u24B6-\u24E9])/g;
text = text.replace(re, function () {
return arguments[1] + arguments[2].toUpperCase();
});

// 2) capitalize after opening "_" or "*"
// Preceded by start or a non-word (so it won't fire for snake_case)
re = /(^|[^'_0-9A-Za-z\u00C0-\u02AF\u1E00-\u1EFF\u24B6-\u24E9])([_*])([A-Za-z\u00C0-\u02AF\u1E00-\u1EFF\u24D0-\u24E9])/g;
text = text.replace(re, function () {
return arguments[1] + arguments[2] + arguments[3].toUpperCase();

Choose a reason for hiding this comment

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

Here you can remove the magic numbers and leave something more descriptive like:

    text = text.replace(re, function (_match, prefix, divider, char) {
      return prefix + divider + char.toUpperCase();
    });

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I just followed the existing implementation and coding style

Choose a reason for hiding this comment

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

No worries, it's just a coding tip.

});
} else if (textTransform == 'uppercase') {
text = text.toUpperCase();
} else if (textTransform == 'lowercase') {
Expand Down
14 changes: 12 additions & 2 deletions javascript/atoms/test/text_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,13 @@
function testShouldRetainTheFormattingOfTextWithinAPreElement() {
var text = getVisibleTextByElementId("preformatted");

assertEquals(" This section has a preformatted\n text block \n split in four lines\n ", text);
assertEquals(" This section has a preformatted\n text block\n split in four lines\n ", text);
}

function testShouldRetainTheFormattingOfTextWithinAPreElementThatIsWithinARegularBlock() {
var text = getVisibleTextByElementId("div-with-pre");

assertEquals("before pre\n This section has a preformatted\n text block \n split in four lines\n \nafter pre", text);
assertEquals("before pre\n This section has a preformatted\n text block\n split in four lines\n \nafter pre", text);
}

function testGetVisibleTextShouldHandleCssContentReplacement() {
Expand All @@ -264,6 +264,11 @@
text = getVisibleTextByElementId("uppercased");
assertEquals("HELLO, WORLD! BLA-BLA-BLA", text);

text = getVisibleTextByElementId("capitalized_accented_character");
assertEquals("Fecha De Expiración", text);
text = getVisibleTextByElementId("capitalized_enye");
assertEquals("Mañana", text);

text = getVisibleTextByElementId("capitalized-1");
assertEquals("Äåìî", text);
text = getVisibleTextByElementId("capitalized-2");
Expand Down Expand Up @@ -423,6 +428,11 @@
<a id="uppercased" style="text-transform: uppercase">hello, world! bla-bla-BLA</a><br/>
</div>

<div>
<a id="capitalized_accented_character" style="text-transform: capitalize">Fecha de expiración</a><br/>
<a id="capitalized_enye" style="text-transform: capitalize">mañana</a><br/>
</div>

<div>
<a lang="ru" id="capitalized-1" style="text-transform: capitalize">äåìî</a><br/>
<a id="capitalized-2" style="text-transform: capitalize">Manipulowanie przepływem</a><br/>
Expand Down