gecko-dev/dom/base/ViewportMetaData.cpp
Hiroyuki Ikezoe 2feab69a17 Bug 1498729 - Store each viewport meta data by the viewport meta tag and use the last one. r=smaug,botond
This is what Chrome and Safari do.
See https://webcompat.com/issues/20701#issuecomment-436054739


So for exmaple, if there are two viewport meta tags like this;

 <meta name="viewport" content="width=980">
 <meta name="viewport" content="initial-scale=1,maximum-scale=1">

We will use "initial-scale=1,maximum-scale=1".  Before this change we used to
use merged "width=980,initial-scale=1,maximum-scale=1".

Another example is to replace the content of a single viewport meta tag like this;

 <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1">

what will happen when this tag is replaced by below;

 <meta id="viewport" name="viewport" content="width=1080">

We will use the replacing one (i.e. "width=1080"), before this change, we used
to use merged "width=1080,initial-scale=1".

As of this commit, we don't properly remove corresponding viewport meta data
when a) viewport meta tag is detached from document and b) `name` attribute is
changed from 'viewport'.  These cases will be handled in subsequent commits.

Note that we no longer store invididual viewport meta data in Document::mHeaderData
so that nsIDOMWindowUtils.getDocumentMetadata doesn't work any more for the
invididual viewport meta data, but there is no use cases for them other than
two test cases which are removed in this commit.

Differential Revision: https://phabricator.services.mozilla.com/D38922

--HG--
extra : moz-landing-system : lando
2019-07-25 20:51:23 +00:00

112 lines
3.4 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ViewportMetaData.h"
using namespace mozilla;
using namespace mozilla::dom;
/*
* Helper function for ViewportMetaData::ProcessViewportInfo.
*
* Handles a single key=value pair. If it corresponds to a valid viewport
* attribute, add it to the document header data. No validation is done on the
* value itself (this is done at display time).
*/
static void ProcessViewportToken(ViewportMetaData& aData,
const nsAString& token) {
/* Iterators. */
nsAString::const_iterator tip, tail, end;
token.BeginReading(tip);
tail = tip;
token.EndReading(end);
/* Move tip to the '='. */
while ((tip != end) && (*tip != '=')) {
++tip;
}
/* If we didn't find an '=', punt. */
if (tip == end) {
return;
}
/* Extract the key and value. */
const nsAString& key = nsContentUtils::TrimWhitespace<nsCRT::IsAsciiSpace>(
Substring(tail, tip), true);
const nsAString& value = nsContentUtils::TrimWhitespace<nsCRT::IsAsciiSpace>(
Substring(++tip, end), true);
/* Check for known keys. If we find a match, insert the appropriate
* information into the document header. */
RefPtr<nsAtom> key_atom = NS_Atomize(key);
if (key_atom == nsGkAtoms::height) {
aData.mHeight.Assign(value);
} else if (key_atom == nsGkAtoms::width) {
aData.mWidth.Assign(value);
} else if (key_atom == nsGkAtoms::initial_scale) {
aData.mInitialScale.Assign(value);
} else if (key_atom == nsGkAtoms::minimum_scale) {
aData.mMinimumScale.Assign(value);
} else if (key_atom == nsGkAtoms::maximum_scale) {
aData.mMaximumScale.Assign(value);
} else if (key_atom == nsGkAtoms::user_scalable) {
aData.mUserScalable.Assign(value);
}
}
#define IS_SEPARATOR(c) \
(((c) == '=') || ((c) == ',') || ((c) == ';') || ((c) == '\t') || \
((c) == '\n') || ((c) == '\r'))
ViewportMetaData::ViewportMetaData(const nsAString& aViewportInfo) {
/* Iterators. */
nsAString::const_iterator tip, tail, end;
aViewportInfo.BeginReading(tip);
tail = tip;
aViewportInfo.EndReading(end);
/* Read the tip to the first non-separator character. */
while ((tip != end) && (IS_SEPARATOR(*tip) || nsCRT::IsAsciiSpace(*tip))) {
++tip;
}
/* Read through and find tokens separated by separators. */
while (tip != end) {
/* Synchronize tip and tail. */
tail = tip;
/* Advance tip past non-separator characters. */
while ((tip != end) && !IS_SEPARATOR(*tip)) {
++tip;
}
/* Allow white spaces that surround the '=' character */
if ((tip != end) && (*tip == '=')) {
++tip;
while ((tip != end) && nsCRT::IsAsciiSpace(*tip)) {
++tip;
}
while ((tip != end) &&
!(IS_SEPARATOR(*tip) || nsCRT::IsAsciiSpace(*tip))) {
++tip;
}
}
/* Our token consists of the characters between tail and tip. */
ProcessViewportToken(*this, Substring(tail, tip));
/* Skip separators. */
while ((tip != end) && (IS_SEPARATOR(*tip) || nsCRT::IsAsciiSpace(*tip))) {
++tip;
}
}
}
#undef IS_SEPARATOR