|
Line 0
WebCore/dom/ViewportArguments.cpp_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
| 3 |
* (C) 1999 Antti Koivisto (koivisto@kde.org) |
| 4 |
* (C) 2001 Dirk Mueller (mueller@kde.org) |
| 5 |
* (C) 2006 Alexey Proskuryakov (ap@webkit.org) |
| 6 |
* Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. |
| 7 |
* Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/) |
| 8 |
* |
| 9 |
* This library is free software; you can redistribute it and/or |
| 10 |
* modify it under the terms of the GNU Library General Public |
| 11 |
* License as published by the Free Software Foundation; either |
| 12 |
* version 2 of the License, or (at your option) any later version. |
| 13 |
* |
| 14 |
* This library is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
* Library General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU Library General Public License |
| 20 |
* along with this library; see the file COPYING.LIB. If not, write to |
| 21 |
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 22 |
* Boston, MA 02110-1301, USA. |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
#include "ViewportArguments.h" |
| 28 |
|
| 29 |
#include "Chrome.h" |
| 30 |
#include "Console.h" |
| 31 |
#include "Document.h" |
| 32 |
#include "DOMWindow.h" |
| 33 |
#include "Frame.h" |
| 34 |
#include "Page.h" |
| 35 |
#include "PlatformString.h" |
| 36 |
#include "Tokenizer.h" |
| 37 |
|
| 38 |
namespace WebCore { |
| 39 |
|
| 40 |
void setViewportFeature(const String& keyString, const String& valueString, Document* document, void* data) |
| 41 |
{ |
| 42 |
ViewportArguments* arguments = static_cast<ViewportArguments*>(data); |
| 43 |
float value = ViewportArguments::ValueUndefined; |
| 44 |
bool didUseConstants = false; |
| 45 |
|
| 46 |
if (equalIgnoringCase(valueString, "yes")) |
| 47 |
value = 1; |
| 48 |
else if (equalIgnoringCase(valueString, "device-width")) { |
| 49 |
didUseConstants = true; |
| 50 |
if (document->page()) |
| 51 |
value = document->page()->chrome()->windowRect().width(); |
| 52 |
} else if (equalIgnoringCase(valueString, "device-height")) { |
| 53 |
didUseConstants = true; |
| 54 |
if (document->page()) |
| 55 |
value = document->page()->chrome()->windowRect().height(); |
| 56 |
} else if (equalIgnoringCase(valueString, "default")) // This allows us to distinguish the omission of a key from asking for the default value. |
| 57 |
value = -2; |
| 58 |
else if (valueString.length()) // listing a key with no value is shorthand for key=default |
| 59 |
value = valueString.toFloat(); |
| 60 |
|
| 61 |
if (keyString == "initial-scale") |
| 62 |
arguments->initialScale = value; |
| 63 |
else if (keyString == "minimum-scale") |
| 64 |
arguments->minimumScale = value; |
| 65 |
else if (keyString == "maximum-scale") { |
| 66 |
arguments->maximumScale = value; |
| 67 |
if (value > 10.0) |
| 68 |
reportViewportWarning(document, MaximumScaleTooLargeError, keyString); |
| 69 |
} else if (keyString == "user-scalable") |
| 70 |
arguments->userScalable = value; |
| 71 |
else if (keyString == "width") { |
| 72 |
if (document->page() && value == document->page()->chrome()->windowRect().width() && !didUseConstants) |
| 73 |
reportViewportWarning(document, DeviceWidthShouldBeUsedWarning, keyString); |
| 74 |
else if (document->page() && value == document->page()->chrome()->windowRect().height() && !didUseConstants) |
| 75 |
reportViewportWarning(document, DeviceHeightShouldBeUsedWarning, keyString); |
| 76 |
|
| 77 |
arguments->width = value; |
| 78 |
} else if (keyString == "height") { |
| 79 |
if (document->page() && value == document->page()->chrome()->windowRect().width() && !didUseConstants) |
| 80 |
reportViewportWarning(document, DeviceWidthShouldBeUsedWarning, keyString); |
| 81 |
else if (document->page() && value == document->page()->chrome()->windowRect().height() && !didUseConstants) |
| 82 |
reportViewportWarning(document, DeviceHeightShouldBeUsedWarning, keyString); |
| 83 |
|
| 84 |
arguments->height = value; |
| 85 |
} else |
| 86 |
reportViewportWarning(document, UnrecognizedViewportArgumentError, keyString); |
| 87 |
} |
| 88 |
|
| 89 |
static const char* viewportErrorMessageTemplate(ViewportErrorCode errorCode) |
| 90 |
{ |
| 91 |
static const char* const errors[] = { |
| 92 |
"Viewport width or height set to physical device width, try using \"device-width\" constant instead for future compatibility.", |
| 93 |
"Viewport height or height set to physical device height, try using \"device-height\" constant instead for future compatibility.", |
| 94 |
"Viewport argument \"%replacement\" not recognized. Content ignored.", |
| 95 |
"Viewport maximum-scale cannot be larger than 10.0. The maximum-scale will be set to 10.0." |
| 96 |
}; |
| 97 |
|
| 98 |
return errors[errorCode]; |
| 99 |
} |
| 100 |
|
| 101 |
static MessageLevel viewportErrorMessageLevel(ViewportErrorCode errorCode) |
| 102 |
{ |
| 103 |
return errorCode == UnrecognizedViewportArgumentError || errorCode == MaximumScaleTooLargeError ? ErrorMessageLevel : TipMessageLevel; |
| 104 |
} |
| 105 |
|
| 106 |
void reportViewportWarning(Document* document, ViewportErrorCode errorCode, const String& replacement) |
| 107 |
{ |
| 108 |
Tokenizer* tokenizer = document->tokenizer(); |
| 109 |
|
| 110 |
Frame* frame = document->frame(); |
| 111 |
if (!frame) |
| 112 |
return; |
| 113 |
|
| 114 |
String message = viewportErrorMessageTemplate(errorCode); |
| 115 |
message.replace("%replacement", replacement); |
| 116 |
|
| 117 |
frame->domWindow()->console()->addMessage(HTMLMessageSource, LogMessageType, viewportErrorMessageLevel(errorCode), message, tokenizer ? tokenizer->lineNumber() + 1 : 0, document->url().string()); |
| 118 |
} |
| 119 |
|
| 120 |
} // namespace WebCore |