gecko-dev/dom/base/ViewportMetaData.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

114 строки
3.5 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).
*/
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 23:51:23 +03:00
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) {
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 23:51:23 +03:00
aData.mHeight.Assign(value);
} else if (key_atom == nsGkAtoms::width) {
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 23:51:23 +03:00
aData.mWidth.Assign(value);
} else if (key_atom == nsGkAtoms::initial_scale) {
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 23:51:23 +03:00
aData.mInitialScale.Assign(value);
} else if (key_atom == nsGkAtoms::minimum_scale) {
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 23:51:23 +03:00
aData.mMinimumScale.Assign(value);
} else if (key_atom == nsGkAtoms::maximum_scale) {
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 23:51:23 +03:00
aData.mMaximumScale.Assign(value);
} else if (key_atom == nsGkAtoms::user_scalable) {
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 23:51:23 +03:00
aData.mUserScalable.Assign(value);
} else if (key_atom == nsGkAtoms::viewport_fit) {
aData.mViewportFit.Assign(value);
}
}
#define IS_SEPARATOR(c) \
(((c) == '=') || ((c) == ',') || ((c) == ';') || ((c) == '\t') || \
((c) == '\n') || ((c) == '\r'))
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 23:51:23 +03:00
ViewportMetaData::ViewportMetaData(const nsAString& aViewportInfo) {
/* Iterators. */
nsAString::const_iterator tip, tail, end;
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 23:51:23 +03:00
aViewportInfo.BeginReading(tip);
tail = tip;
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 23:51:23 +03:00
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. */
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 23:51:23 +03:00
ProcessViewportToken(*this, Substring(tail, tip));
/* Skip separators. */
while ((tip != end) && (IS_SEPARATOR(*tip) || nsCRT::IsAsciiSpace(*tip))) {
++tip;
}
}
}
#undef IS_SEPARATOR