Backed out changeset b462e7b742d8 (bug 1908261) for causing multiple reftest failures...
[gecko.git] / dom / base / ViewportMetaData.cpp
bloba0669e500d6ce6eaac76d61ac606690df5a319a9
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsContentUtils.h"
8 #include "nsCRT.h"
9 #include "ViewportMetaData.h"
11 using namespace mozilla;
12 using namespace mozilla::dom;
15 * Helper function for ViewportMetaData::ProcessViewportInfo.
17 * Handles a single key=value pair. If it corresponds to a valid viewport
18 * attribute, add it to the document header data. No validation is done on the
19 * value itself (this is done at display time).
21 static void ProcessViewportToken(ViewportMetaData& aData,
22 const nsAString& token) {
23 /* Iterators. */
24 nsAString::const_iterator tip, tail, end;
25 token.BeginReading(tip);
26 tail = tip;
27 token.EndReading(end);
29 /* Move tip to the '='. */
30 while ((tip != end) && (*tip != '=')) {
31 ++tip;
34 /* If we didn't find an '=', punt. */
35 if (tip == end) {
36 return;
39 /* Extract the key and value. */
40 const nsAString& key = nsContentUtils::TrimWhitespace<nsCRT::IsAsciiSpace>(
41 Substring(tail, tip), true);
42 const nsAString& value = nsContentUtils::TrimWhitespace<nsCRT::IsAsciiSpace>(
43 Substring(++tip, end), true);
45 /* Check for known keys. If we find a match, insert the appropriate
46 * information into the document header. */
47 RefPtr<nsAtom> key_atom = NS_Atomize(key);
48 if (key_atom == nsGkAtoms::height) {
49 aData.mHeight.Assign(value);
50 } else if (key_atom == nsGkAtoms::width) {
51 aData.mWidth.Assign(value);
52 } else if (key_atom == nsGkAtoms::initial_scale) {
53 aData.mInitialScale.Assign(value);
54 } else if (key_atom == nsGkAtoms::minimum_scale) {
55 aData.mMinimumScale.Assign(value);
56 } else if (key_atom == nsGkAtoms::maximum_scale) {
57 aData.mMaximumScale.Assign(value);
58 } else if (key_atom == nsGkAtoms::user_scalable) {
59 aData.mUserScalable.Assign(value);
60 } else if (key_atom == nsGkAtoms::viewport_fit) {
61 aData.mViewportFit.Assign(value);
62 } else if (key_atom == nsGkAtoms::interactive_widget) {
63 aData.mInteractiveWidgetMode.Assign(value);
67 #define IS_SEPARATOR(c) \
68 (((c) == '=') || ((c) == ',') || ((c) == ';') || ((c) == '\t') || \
69 ((c) == '\n') || ((c) == '\r'))
71 ViewportMetaData::ViewportMetaData(const nsAString& aViewportInfo) {
72 /* Iterators. */
73 nsAString::const_iterator tip, tail, end;
74 aViewportInfo.BeginReading(tip);
75 tail = tip;
76 aViewportInfo.EndReading(end);
78 /* Read the tip to the first non-separator character. */
79 while ((tip != end) && (IS_SEPARATOR(*tip) || nsCRT::IsAsciiSpace(*tip))) {
80 ++tip;
83 /* Read through and find tokens separated by separators. */
84 while (tip != end) {
85 /* Synchronize tip and tail. */
86 tail = tip;
88 /* Advance tip past non-separator characters. */
89 while ((tip != end) && !IS_SEPARATOR(*tip)) {
90 ++tip;
93 /* Allow white spaces that surround the '=' character */
94 if ((tip != end) && (*tip == '=')) {
95 ++tip;
97 while ((tip != end) && nsCRT::IsAsciiSpace(*tip)) {
98 ++tip;
101 while ((tip != end) &&
102 !(IS_SEPARATOR(*tip) || nsCRT::IsAsciiSpace(*tip))) {
103 ++tip;
107 /* Our token consists of the characters between tail and tip. */
108 ProcessViewportToken(*this, Substring(tail, tip));
110 /* Skip separators. */
111 while ((tip != end) && (IS_SEPARATOR(*tip) || nsCRT::IsAsciiSpace(*tip))) {
112 ++tip;
117 #undef IS_SEPARATOR