Commit ca22cf41 authored by junov@chromium.org's avatar junov@chromium.org

Prevent crash when calling text-related methods on a 2D canvas in a frame-less document.

BUG=343801
R=senorblanco@chromium.org

Review URL: https://codereview.chromium.org/186063003

git-svn-id: svn://svn.chromium.org/blink/trunk@168407 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent e87d293c
This verifies that the browser does not crash when drawing text to a canvas in a frame-less document.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE HTML>
<html>
<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<script src="script-tests/canvas-frameless-document-text.js"></script>
</body>
</html>
description("This verifies that the browser does not crash when drawing text to a canvas in a frame-less document.");
// It is not clear frome the spec whether this is supposed to work and how.
// Therefore, we do not validate the rendering results. We just make sure
// this does not crash the browser.
var canvas1 = document.createElement('canvas');
var ctx1 = canvas1.getContext('2d');
var htmlDoc = document.implementation.createHTMLDocument('', '', null);
htmlDoc.adoptNode(canvas1);
var canvas2 = htmlDoc.createElement('canvas');
var ctx2 = canvas2.getContext('2d');
ctx1.font = 'italic 30px Arial';
ctx2.font = 'italic 30px Arial';
ctx1.fillText('Text1', 0, 30);
ctx2.fillText('Text1', 0, 30);
ctx1.strokeText('Text2', 0, 60);
ctx2.strokeText('Text2', 0, 60);
ctx1.measureText('Text3');
ctx2.measureText('Text3');
\ No newline at end of file
...@@ -2081,6 +2081,10 @@ String CanvasRenderingContext2D::font() const ...@@ -2081,6 +2081,10 @@ String CanvasRenderingContext2D::font() const
void CanvasRenderingContext2D::setFont(const String& newFont) void CanvasRenderingContext2D::setFont(const String& newFont)
{ {
// The style resolution required for rendering text is not available in frame-less documents.
if (!canvas()->document().frame())
return;
MutableStylePropertyMap::iterator i = m_fetchedFonts.find(newFont); MutableStylePropertyMap::iterator i = m_fetchedFonts.find(newFont);
RefPtr<MutableStylePropertySet> parsedStyle = i != m_fetchedFonts.end() ? i->value : nullptr; RefPtr<MutableStylePropertySet> parsedStyle = i != m_fetchedFonts.end() ? i->value : nullptr;
...@@ -2199,8 +2203,13 @@ void CanvasRenderingContext2D::strokeText(const String& text, float x, float y, ...@@ -2199,8 +2203,13 @@ void CanvasRenderingContext2D::strokeText(const String& text, float x, float y,
PassRefPtr<TextMetrics> CanvasRenderingContext2D::measureText(const String& text) PassRefPtr<TextMetrics> CanvasRenderingContext2D::measureText(const String& text)
{ {
FontCachePurgePreventer fontCachePurgePreventer;
RefPtr<TextMetrics> metrics = TextMetrics::create(); RefPtr<TextMetrics> metrics = TextMetrics::create();
// The style resolution required for rendering text is not available in frame-less documents.
if (!canvas()->document().frame())
return metrics.release();
FontCachePurgePreventer fontCachePurgePreventer;
canvas()->document().updateStyleIfNeeded(); canvas()->document().updateStyleIfNeeded();
metrics->setWidth(accessFont().width(TextRun(text))); metrics->setWidth(accessFont().width(TextRun(text)));
return metrics.release(); return metrics.release();
...@@ -2218,6 +2227,10 @@ static void replaceCharacterInString(String& text, WTF::CharacterMatchFunctionPt ...@@ -2218,6 +2227,10 @@ static void replaceCharacterInString(String& text, WTF::CharacterMatchFunctionPt
void CanvasRenderingContext2D::drawTextInternal(const String& text, float x, float y, bool fill, float maxWidth, bool useMaxWidth) void CanvasRenderingContext2D::drawTextInternal(const String& text, float x, float y, bool fill, float maxWidth, bool useMaxWidth)
{ {
// The style resolution required for rendering text is not available in frame-less documents.
if (!canvas()->document().frame())
return;
// accessFont needs the style to be up to date, but updating style can cause script to run, // accessFont needs the style to be up to date, but updating style can cause script to run,
// (e.g. due to autofocus) which can free the GraphicsContext, so update style before grabbing // (e.g. due to autofocus) which can free the GraphicsContext, so update style before grabbing
// the GraphicsContext. // the GraphicsContext.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment