Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Live Text] Avoid injecting recognized text consisting of single words or letters in tiny images #4512

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

PASS internals.shadowRoot(image)?.getElementById("div#image-overlay") is undefined.
PASS successfullyParsed is true

TEST COMPLETE

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="../../../resources/js-test.js"></script>
<style>
body, html {
margin: 0;
}
img {
width: 32px;
height: 32px;
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<img src="../resources/green-400x400.png"></img>
<pre id="console"></pre>
<script>
jsTestIsAsync = true;

addEventListener("load", () => {
image = document.querySelector("img");
internals.installImageOverlay(image, [
{
topLeft : new DOMPointReadOnly(0, 0),
topRight : new DOMPointReadOnly(0.9, 0),
bottomRight : new DOMPointReadOnly(0.9, 0.9),
bottomLeft : new DOMPointReadOnly(0, 0.9),
children: [
{
text : "e",
topLeft : new DOMPointReadOnly(0, 0),
topRight : new DOMPointReadOnly(0.9, 0),
bottomRight : new DOMPointReadOnly(0.9, 0.9),
bottomLeft : new DOMPointReadOnly(0, 0.9),
}
]
}
]);

shouldBeUndefined(`internals.shadowRoot(image)?.getElementById("div#image-overlay")`);
finishJSTest();
});
</script>
</body>
</html>
40 changes: 29 additions & 11 deletions Source/WebCore/dom/ImageOverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,19 +469,9 @@ static RotatedRect fitElementToQuad(HTMLElement& container, const FloatQuad& qua

void updateWithTextRecognitionResult(HTMLElement& element, const TextRecognitionResult& result, CacheTextRecognitionResults cacheTextRecognitionResults)
{
auto elements = updateSubtree(element, result);
if (!elements.root)
return;

Ref document = element.document();
document->updateLayoutIgnorePendingStylesheets();

auto* renderer = element.renderer();
if (!is<RenderImage>(renderer))
return;

downcast<RenderImage>(*renderer).setHasImageOverlay();

auto containerRect = ImageOverlay::containerRect(element);
auto convertToContainerCoordinates = [&](const FloatQuad& normalizedQuad) {
auto quad = normalizedQuad;
Expand All @@ -490,7 +480,35 @@ void updateWithTextRecognitionResult(HTMLElement& element, const TextRecognition
return quad;
};

bool applyUserSelectAll = document->isImageDocument() || renderer->style().userSelect() != UserSelect::None;
bool smallImageWithSingleWord = [&] {
constexpr auto smallImageDimensionThreshold = 64;
if (containerRect.width() > smallImageDimensionThreshold || containerRect.height() > smallImageDimensionThreshold)
return false;

return result.blocks.isEmpty() && result.lines.size() == 1 && result.lines[0].children.size() == 1;
}();

if (smallImageWithSingleWord)
return;

auto elements = updateSubtree(element, result);
if (!elements.root)
return;

{
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this separate scope needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, I wrote it this way to ensure that nothing below tries to reuse renderer here. If anything updates layout below and causes the image to recreate its renderer, then this pointer could point to a destroyed object.

document->updateLayoutIgnorePendingStylesheets();
auto* renderer = dynamicDowncast<RenderImage>(element.renderer());
if (!renderer)
return;

renderer->setHasImageOverlay();
}

bool applyUserSelectAll = [&] {
auto* renderer = dynamicDowncast<RenderImage>(element.renderer());
return document->isImageDocument() || (renderer && renderer->style().userSelect() != UserSelect::None);
}();

for (size_t lineIndex = 0; lineIndex < result.lines.size(); ++lineIndex) {
auto& lineElements = elements.lines[lineIndex];
auto& lineContainer = lineElements.line;
Expand Down