Bug 1892041 - Part 1: Update test262 features. r=spidermonkey-reviewers,dminor
[gecko.git] / dom / base / ViewportMetaData.cpp
blob2646621629cee6f03fa12d1f6714f2ae3d2da3cc
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);
65 #define IS_SEPARATOR(c) \
66 (((c) == '=') || ((c) == ',') || ((c) == ';') || ((c) == '\t') || \
67 ((c) == '\n') || ((c) == '\r'))
69 ViewportMetaData::ViewportMetaData(const nsAString& aViewportInfo) {
70 /* Iterators. */
71 nsAString::const_iterator tip, tail, end;
72 aViewportInfo.BeginReading(tip);
73 tail = tip;
74 aViewportInfo.EndReading(end);
76 /* Read the tip to the first non-separator character. */
77 while ((tip != end) && (IS_SEPARATOR(*tip) || nsCRT::IsAsciiSpace(*tip))) {
78 ++tip;
81 /* Read through and find tokens separated by separators. */
82 while (tip != end) {
83 /* Synchronize tip and tail. */
84 tail = tip;
86 /* Advance tip past non-separator characters. */
87 while ((tip != end) && !IS_SEPARATOR(*tip)) {
88 ++tip;
91 /* Allow white spaces that surround the '=' character */
92 if ((tip != end) && (*tip == '=')) {
93 ++tip;
95 while ((tip != end) && nsCRT::IsAsciiSpace(*tip)) {
96 ++tip;
99 while ((tip != end) &&
100 !(IS_SEPARATOR(*tip) || nsCRT::IsAsciiSpace(*tip))) {
101 ++tip;
105 /* Our token consists of the characters between tail and tip. */
106 ProcessViewportToken(*this, Substring(tail, tip));
108 /* Skip separators. */
109 while ((tip != end) && (IS_SEPARATOR(*tip) || nsCRT::IsAsciiSpace(*tip))) {
110 ++tip;
115 #undef IS_SEPARATOR