Skip to content

Commit da36841

Browse files
authored
fix: properly escape slash inside attributes (#1205)
microsoft/playwright#20471
1 parent 8dfb745 commit da36841

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

playwright/src/main/java/com/microsoft/playwright/impl/LocatorUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private static String escapeForAttributeSelector(String value, boolean exact) {
118118
// cssEscape(value).replace(/\\ /g, ' ')
119119
// However, our attribute selectors do not conform to CSS parsing spec,
120120
// so we escape them differently.
121-
return '"' + value.replaceAll("\"", "\\\\\"") + '"' + (exact ? "" : "i");
121+
return '"' + value.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"") + '"' + (exact ? "" : "i");
122122
}
123123

124124
private static String toJsRegExp(Pattern pattern) {

playwright/src/test/java/com/microsoft/playwright/TestSelectorsGetBy.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.microsoft.playwright;
22

3+
import com.microsoft.playwright.assertions.LocatorAssertions;
34
import com.microsoft.playwright.options.AriaRole;
45
import org.junit.jupiter.api.Test;
56

@@ -147,6 +148,11 @@ void getByEscaping() {
147148
assertThat(page.getByPlaceholder("hello my\nworld")).hasAttribute("id", "control");
148149
assertThat(page.getByAltText("hello my\nworld")).hasAttribute("id", "control");
149150
assertThat(page.getByTitle("hello my\nworld")).hasAttribute("id", "control");
151+
152+
page.setContent("<div id=target title='my title'>Text here</div>");
153+
assertThat(page.getByTitle("my title", new Page.GetByTitleOptions().setExact(true))).hasCount(1, new LocatorAssertions.HasCountOptions().setTimeout(500));
154+
assertThat(page.getByTitle("my t\\itle", new Page.GetByTitleOptions().setExact(true))).hasCount(0, new LocatorAssertions.HasCountOptions().setTimeout(500));
155+
assertThat(page.getByTitle("my t\\\\itle", new Page.GetByTitleOptions().setExact(true))).hasCount(0, new LocatorAssertions.HasCountOptions().setTimeout(500));
150156
}
151157

152158
@Test
@@ -178,6 +184,16 @@ void getByRoleEscaping() {
178184
assertEquals(
179185
asList("<a href=\"https://playwright.dev\">he llo 56</a>"),
180186
page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName(" he \n llo 56 ").setExact(true)).evaluateAll("els => els.map(e => e.outerHTML)"));
187+
188+
assertEquals(
189+
asList("<button>Click me</button>"),
190+
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Click me").setExact(true)).evaluateAll("els => els.map(e => e.outerHTML)"));
191+
assertEquals(
192+
asList(),
193+
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Click \\me").setExact(true)).evaluateAll("els => els.map(e => e.outerHTML)"));
194+
assertEquals(
195+
asList(),
196+
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Click \\\\me").setExact(true)).evaluateAll("els => els.map(e => e.outerHTML)"));
181197
}
182198

183199
@Test

0 commit comments

Comments
 (0)