"Fossies" - the Fresh Open Source Software Archive 
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "add_ids_filter.h" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
1.13.35.2_vs_1.14.36.1.
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20
21 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_ADD_IDS_FILTER_H_
22 #define NET_INSTAWEB_REWRITER_PUBLIC_ADD_IDS_FILTER_H_
23
24 #include <vector>
25
26 #include "net/instaweb/rewriter/public/rewrite_driver.h"
27 #include "pagespeed/kernel/base/string.h"
28 #include "pagespeed/kernel/html/empty_html_filter.h"
29 #include "pagespeed/kernel/html/html_element.h"
30
31 namespace net_instaweb {
32
33 // This filter as it stands adds an id to all div-like DOM elements that lack
34 // one. The ids represent the div's location in the DOM, based on the tag
35 // structure of the page. The hope is that this is moderately stable between
36 // page accesses.
37 //
38 // Ids are of the form PageSpeed-nearestParentId-n-n-n... where -n-n-n is a path
39 // encoded as a series of indexes at that depth below nearestParentId. For
40 // example:
41 // PageSpeed-7-0 : The 0th child of the 7th child of the root of the document
42 // PageSpeed-content-11: The 11th child of the node with id='content'.
43 class AddIdsFilter : public EmptyHtmlFilter {
44 public:
45 static const char kIdPrefix[];
46
47 explicit AddIdsFilter(RewriteDriver* driver);
48 virtual ~AddIdsFilter();
49
50 virtual void StartDocument();
51 virtual void StartElement(HtmlElement* element);
52 virtual void EndElement(HtmlElement* element);
53 virtual const char* Name() const { return "AddIdsFilter"; }
54
55 private:
56 GoogleString GetDivCountStackEncoding();
57
58 // We represent our current DOM location with two stacks. The
59 // div_count_stack_ contains our path through the divs in the DOM. When a div
60 // has an id, kIsId is pushed immediately after its position, and the id is
61 // pushed onto id_stack_. So if we erase all kIsId entries, we obtain a pure
62 // path through the tree; to create an encoded id we use the top entry of
63 // id_stack_ followed by the encoding of the topmost elements of
64 // div_count_stack_ above the topmost kIsId.
65 static const int kIsId;
66 std::vector<int> div_count_stack_;
67 std::vector<const HtmlElement::Attribute*> id_stack_;
68 RewriteDriver* driver_;
69 };
70
71 } // namespace net_instaweb
72
73 #endif // NET_INSTAWEB_REWRITER_PUBLIC_ADD_IDS_FILTER_H_