1
// Copyright 2013 Google Inc. All Rights Reserved.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
15
// Author: sligocki@google.com (Shawn Ligocki)
17
// Encoder for Source Map Revision 3. Specification and other info:
18
// * https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit
19
// * http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/
20
// * http://en.wikipedia.org/wiki/Variable-length_quantity
22
#ifndef PAGESPEED_KERNEL_UTIL_SOURCE_MAP_H_
23
#define PAGESPEED_KERNEL_UTIL_SOURCE_MAP_H_
27
#include "pagespeed/kernel/base/basictypes.h"
28
#include "pagespeed/kernel/base/string.h"
29
#include "pagespeed/kernel/base/string_util.h"
31
namespace net_instaweb {
33
namespace source_map {
35
// Declares a mapping between a line # and column # in generated file
36
// with the corresponding source file #, line # and column #.
45
Mapping() : gen_line(0), gen_col(0), src_file(0), src_line(0), src_col(0) {}
46
Mapping(int gen_line_arg, int gen_col_arg,
47
int src_file_arg, int src_line_arg, int src_col_arg)
48
: gen_line(gen_line_arg),
50
src_file(src_file_arg),
51
src_line(src_line_arg),
52
src_col(src_col_arg) {
56
typedef std::vector<Mapping> MappingVector;
58
// Encodes generated_url, source_url and mappings into encoded_source_map
59
// which will be the contents of a JSON Source Map v3 file.
60
// Bool returned answers question "Did this succeed?"
61
bool Encode(StringPiece generated_url, // optional: "" to ignore.
62
StringPiece source_url,
63
// mappings MUST already be sorted by gen_line and then gen_col.
64
const MappingVector& mappings,
65
GoogleString* encoded_source_map);
67
// TODO(sligocki)-maybe: Do we want a decoder as well? Might be nice for
68
// testing purposes, then we could throw a lot of random examples at it and
69
// make sure they Encode -> Decode back to the original.
71
// Internal methods. These should not be called directly. Included here for
74
// Simply convert val 0-63 into a base64 char.
75
char EncodeBase64(int val);
76
// Encodes an arbitrary 32-bit integer into VLQ (Variable Length Quantity)
77
// base64 (A sequence of base64 chars which use continuation bits to encode
78
// arbitrary length values.)
79
GoogleString EncodeVlq(int32 val);
80
// Encodes mappings into a sequence of ; and , separated VLQ base64 values.
81
bool EncodeMappings(const MappingVector& mappings,
82
GoogleString* result);
83
GoogleString PercentEncode(StringPiece url);
85
} // namespace source_map
87
} // namespace net_instaweb
89
#endif // PAGESPEED_KERNEL_UTIL_SOURCE_MAP_H_