1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2001 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2003, 2010 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "core/html/HTMLMetaElement.h"
#include "core/HTMLNames.h"
#include "core/dom/Document.h"
#include "core/dom/ElementTraversal.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/Settings.h"
#include "core/html/HTMLHeadElement.h"
#include "core/inspector/ConsoleMessage.h"
#include "core/loader/FrameLoaderClient.h"
#include "platform/RuntimeEnabledFeatures.h"
namespace blink {
#define DEFINE_ARRAY_FOR_MATCHING(name, source, maxMatchLength) \
const UChar* name; \
const unsigned uMaxMatchLength = maxMatchLength; \
UChar characterBuffer[uMaxMatchLength]; \
if (!source.is8Bit()) { \
name = source.characters16(); \
} else { \
unsigned bufferLength = std::min(uMaxMatchLength, source.length()); \
const LChar* characters8 = source.characters8(); \
for (unsigned i = 0; i < bufferLength; ++i) \
characterBuffer[i] = characters8[i]; \
name = characterBuffer; \
}
using namespace HTMLNames;
inline HTMLMetaElement::HTMLMetaElement(Document& document)
: HTMLElement(metaTag, document)
{
}
DEFINE_NODE_FACTORY(HTMLMetaElement)
static bool isInvalidSeparator(UChar c)
{
return c == ';';
}
// Though isspace() considers \t and \v to be whitespace, Win IE doesn't.
static bool isSeparator(UChar c)
{
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '=' || c == ',' || c == '\0';
}
void HTMLMetaElement::parseContentAttribute(const String& content, KeyValuePairCallback callback, void* data)
{
bool error = false;
// Tread lightly in this code -- it was specifically designed to mimic Win IE's parsing behavior.
unsigned keyBegin, keyEnd;
unsigned valueBegin, valueEnd;
String buffer = content.lower();
unsigned length = buffer.length();
for (unsigned i = 0; i < length; /* no increment here */) {
// skip to first non-separator, but don't skip past the end of the string
while (isSeparator(buffer[i])) {
if (i >= length)
break;
i++;
}
keyBegin = i;
// skip to first separator
while (!isSeparator(buffer[i])) {
error |= isInvalidSeparator(buffer[i]);
if (i >= length)
break;
i++;
}
keyEnd = i;
// skip to first '=', but don't skip past a ',' or the end of the string
while (buffer[i] != '=') {
error |= isInvalidSeparator(buffer[i]);
if (buffer[i] == ',' || i >= length)
break;
i++;
}
// skip to first non-separator, but don't skip past a ',' or the end of the string
while (isSeparator(buffer[i])) {
if (buffer[i] == ',' || i >= length)
break;
i++;
}
valueBegin = i;
// skip to first separator
while (!isSeparator(buffer[i])) {
error |= isInvalidSeparator(buffer[i]);
if (i >= length)
break;
i++;
}
valueEnd = i;
ASSERT_WITH_SECURITY_IMPLICATION(i <= length);
String keyString = buffer.substring(keyBegin, keyEnd - keyBegin);
String valueString = buffer.substring(valueBegin, valueEnd - valueBegin);
(this->*callback)(keyString, valueString, data);
}
if (error) {
String message = "Error parsing a meta element's content: ';' is not a valid key-value pair separator. Please use ',' instead.";
document().addConsoleMessage(ConsoleMessage::create(RenderingMessageSource, WarningMessageLevel, message));
}
}
static inline float clampLengthValue(float value)
{
// Limits as defined in the css-device-adapt spec.
if (value != ViewportDescription::ValueAuto)
return std::min(float(10000), std::max(value, float(1)));
return value;
}
static inline float clampScaleValue(float value)
{
// Limits as defined in the css-device-adapt spec.
if (value != ViewportDescription::ValueAuto)
return std::min(float(10), std::max(value, float(0.1)));
return value;
}
float HTMLMetaElement::parsePositiveNumber(const String& keyString, const String& valueString, bool* ok)
{
size_t parsedLength;
float value;
if (valueString.is8Bit())
value = charactersToFloat(valueString.characters8(), valueString.length(), parsedLength);
else
value = charactersToFloat(valueString.characters16(), valueString.length(), parsedLength);
if (!parsedLength) {
reportViewportWarning(UnrecognizedViewportArgumentValueError, valueString, keyString);
if (ok)
*ok = false;
return 0;
}
if (parsedLength < valueString.length())
reportViewportWarning(TruncatedViewportArgumentValueError, valueString, keyString);
if (ok)
*ok = true;
return value;
}
Length HTMLMetaElement::parseViewportValueAsLength(const String& keyString, const String& valueString)
{
// 1) Non-negative number values are translated to px lengths.
// 2) Negative number values are translated to auto.
// 3) device-width and device-height are used as keywords.
// 4) Other keywords and unknown values translate to 0.0.
unsigned length = valueString.length();
DEFINE_ARRAY_FOR_MATCHING(characters, valueString, 13);
SWITCH(characters, length) {
CASE("device-width") {
return Length(DeviceWidth);
}
CASE("device-height") {
return Length(DeviceHeight);
}
}
float value = parsePositiveNumber(keyString, valueString);
if (value < 0)
return Length(); // auto
return Length(clampLengthValue(value), Fixed);
}
float HTMLMetaElement::parseViewportValueAsZoom(const String& keyString, const String& valueString, bool& computedValueMatchesParsedValue)
{
// 1) Non-negative number values are translated to <number> values.
// 2) Negative number values are translated to auto.
// 3) yes is translated to 1.0.
// 4) device-width and device-height are translated to 10.0.
// 5) no and unknown values are translated to 0.0
computedValueMatchesParsedValue = false;
unsigned length = valueString.length();
DEFINE_ARRAY_FOR_MATCHING(characters, valueString, 13);
SWITCH(characters, length) {
CASE("yes") {
return 1;
}
CASE("no") {
return 0;
}
CASE("device-width") {
return 10;
}
CASE("device-height") {
return 10;
}
}
float value = parsePositiveNumber(keyString, valueString);
if (value < 0)
return ViewportDescription::ValueAuto;
if (value > 10.0)
reportViewportWarning(MaximumScaleTooLargeError, String(), String());
if (!value && document().settings() && document().settings()->viewportMetaZeroValuesQuirk())
return ViewportDescription::ValueAuto;
float clampedValue = clampScaleValue(value);
if (clampedValue == value)
computedValueMatchesParsedValue = true;
return clampedValue;
}
bool HTMLMetaElement::parseViewportValueAsUserZoom(const String& keyString, const String& valueString, bool& computedValueMatchesParsedValue)
{
// yes and no are used as keywords.
// Numbers >= 1, numbers <= -1, device-width and device-height are mapped to yes.
// Numbers in the range <-1, 1>, and unknown values, are mapped to no.
computedValueMatchesParsedValue = false;
unsigned length = valueString.length();
DEFINE_ARRAY_FOR_MATCHING(characters, valueString, 13);
SWITCH(characters, length) {
CASE("yes") {
computedValueMatchesParsedValue = true;
return true;
}
CASE("no") {
computedValueMatchesParsedValue = true;
return false;
}
CASE("device-width") {
return true;
}
CASE("device-height") {
return true;
}
}
float value = parsePositiveNumber(keyString, valueString);
if (fabs(value) < 1)
return false;
return true;
}
float HTMLMetaElement::parseViewportValueAsDPI(const String& keyString, const String& valueString)
{
unsigned length = valueString.length();
DEFINE_ARRAY_FOR_MATCHING(characters, valueString, 10);
SWITCH(characters, length) {
CASE("device-dpi") {
return ViewportDescription::ValueDeviceDPI;
}
CASE("low-dpi") {
return ViewportDescription::ValueLowDPI;
}
CASE("medium-dpi") {
return ViewportDescription::ValueMediumDPI;
}
CASE("high-dpi") {
return ViewportDescription::ValueHighDPI;
}
}
bool ok;
float value = parsePositiveNumber(keyString, valueString, &ok);
if (!ok || value < 70 || value > 400)
return ViewportDescription::ValueAuto;
return value;
}
void HTMLMetaElement::processViewportKeyValuePair(const String& keyString, const String& valueString, void* data)
{
ViewportDescription* description = static_cast<ViewportDescription*>(data);
unsigned length = keyString.length();
DEFINE_ARRAY_FOR_MATCHING(characters, keyString, 17);
SWITCH(characters, length) {
CASE("width") {
const Length& width = parseViewportValueAsLength(keyString, valueString);
if (width.isAuto())
return;
description->minWidth = Length(ExtendToZoom);
description->maxWidth = width;
return;
}
CASE("height") {
const Length& height = parseViewportValueAsLength(keyString, valueString);
if (height.isAuto())
return;
description->minHeight = Length(ExtendToZoom);
description->maxHeight = height;
return;
}
CASE("initial-scale") {
description->zoom = parseViewportValueAsZoom(keyString, valueString, description->zoomIsExplicit);
return;
}
CASE("minimum-scale") {
description->minZoom = parseViewportValueAsZoom(keyString, valueString, description->minZoomIsExplicit);
return;
}
CASE("maximum-scale") {
description->maxZoom = parseViewportValueAsZoom(keyString, valueString, description->maxZoomIsExplicit);
return;
}
CASE("user-scalable") {
description->userZoom = parseViewportValueAsUserZoom(keyString, valueString, description->userZoomIsExplicit);
return;
}
CASE("target-densitydpi") {
description->deprecatedTargetDensityDPI = parseViewportValueAsDPI(keyString, valueString);
reportViewportWarning(TargetDensityDpiUnsupported, String(), String());
return;
}
CASE("minimal-ui") {
// Ignore vendor-specific argument.
return;
}
}
reportViewportWarning(UnrecognizedViewportArgumentKeyError, keyString, String());
}
static const char* viewportErrorMessageTemplate(ViewportErrorCode errorCode)
{
static const char* const errors[] = {
"The key \"%replacement1\" is not recognized and ignored.",
"The value \"%replacement1\" for key \"%replacement2\" is invalid, and has been ignored.",
"The value \"%replacement1\" for key \"%replacement2\" was truncated to its numeric prefix.",
"The value for key \"maximum-scale\" is out of bounds and the value has been clamped.",
"The key \"target-densitydpi\" is not supported.",
};
return errors[errorCode];
}
static MessageLevel viewportErrorMessageLevel(ViewportErrorCode errorCode)
{
switch (errorCode) {
case TruncatedViewportArgumentValueError:
case TargetDensityDpiUnsupported:
case UnrecognizedViewportArgumentKeyError:
case UnrecognizedViewportArgumentValueError:
case MaximumScaleTooLargeError:
return WarningMessageLevel;
}
ASSERT_NOT_REACHED();
return ErrorMessageLevel;
}
void HTMLMetaElement::reportViewportWarning(ViewportErrorCode errorCode, const String& replacement1, const String& replacement2)
{
if (!document().frame())
return;
String message = viewportErrorMessageTemplate(errorCode);
if (!replacement1.isNull())
message.replace("%replacement1", replacement1);
if (!replacement2.isNull())
message.replace("%replacement2", replacement2);
// FIXME: This message should be moved off the console once a solution to https://bugs.webkit.org/show_bug.cgi?id=103274 exists.
document().addConsoleMessage(ConsoleMessage::create(RenderingMessageSource, viewportErrorMessageLevel(errorCode), message));
}
void HTMLMetaElement::processViewportContentAttribute(const String& content, ViewportDescription::Type origin)
{
ASSERT(!content.isNull());
if (!document().shouldOverrideLegacyDescription(origin))
return;
ViewportDescription descriptionFromLegacyTag(origin);
if (document().shouldMergeWithLegacyDescription(origin))
descriptionFromLegacyTag = document().viewportDescription();
parseContentAttribute(content, &HTMLMetaElement::processViewportKeyValuePair, (void*)&descriptionFromLegacyTag);
if (descriptionFromLegacyTag.minZoom == ViewportDescription::ValueAuto)
descriptionFromLegacyTag.minZoom = 0.25;
if (descriptionFromLegacyTag.maxZoom == ViewportDescription::ValueAuto) {
descriptionFromLegacyTag.maxZoom = 5;
descriptionFromLegacyTag.minZoom = std::min(descriptionFromLegacyTag.minZoom, float(5));
}
document().setViewportDescription(descriptionFromLegacyTag);
}
void HTMLMetaElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
{
if (name == http_equivAttr || name == contentAttr) {
process();
return;
}
if (name != nameAttr)
HTMLElement::parseAttribute(name, value);
}
Node::InsertionNotificationRequest HTMLMetaElement::insertedInto(ContainerNode* insertionPoint)
{
HTMLElement::insertedInto(insertionPoint);
return InsertionShouldCallDidNotifySubtreeInsertions;
}
void HTMLMetaElement::didNotifySubtreeInsertionsToDocument()
{
process();
}
static bool inDocumentHead(HTMLMetaElement* element)
{
if (!element->inDocument())
return false;
return Traversal<HTMLHeadElement>::firstAncestor(*element);
}
void HTMLMetaElement::process()
{
if (!inDocument())
return;
// All below situations require a content attribute (which can be the empty string).
const AtomicString& contentValue = fastGetAttribute(contentAttr);
if (contentValue.isNull())
return;
const AtomicString& nameValue = fastGetAttribute(nameAttr);
if (!nameValue.isEmpty()) {
if (equalIgnoringCase(nameValue, "viewport"))
processViewportContentAttribute(contentValue, ViewportDescription::ViewportMeta);
else if (equalIgnoringCase(nameValue, "referrer"))
document().processReferrerPolicy(contentValue);
else if (equalIgnoringCase(nameValue, "handheldfriendly") && equalIgnoringCase(contentValue, "true"))
processViewportContentAttribute("width=device-width", ViewportDescription::HandheldFriendlyMeta);
else if (equalIgnoringCase(nameValue, "mobileoptimized"))
processViewportContentAttribute("width=device-width, initial-scale=1", ViewportDescription::MobileOptimizedMeta);
else if (equalIgnoringCase(nameValue, "theme-color") && document().frame())
document().frame()->loader().client()->dispatchDidChangeThemeColor();
}
// Get the document to process the tag, but only if we're actually part of DOM
// tree (changing a meta tag while it's not in the tree shouldn't have any effect
// on the document).
const AtomicString& httpEquivValue = fastGetAttribute(http_equivAttr);
if (!httpEquivValue.isEmpty())
document().processHttpEquiv(httpEquivValue, contentValue, inDocumentHead(this));
}
const AtomicString& HTMLMetaElement::content() const
{
return getAttribute(contentAttr);
}
const AtomicString& HTMLMetaElement::httpEquiv() const
{
return getAttribute(http_equivAttr);
}
const AtomicString& HTMLMetaElement::name() const
{
return getNameAttribute();
}
}
|