"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_head_filter.cc" 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 #include "net/instaweb/rewriter/public/add_head_filter.h"
22
23 #include "base/logging.h"
24 #include "pagespeed/kernel/html/html_element.h"
25 #include "pagespeed/kernel/html/html_name.h"
26 #include "pagespeed/kernel/html/html_parse.h"
27
28 namespace net_instaweb {
29
30 AddHeadFilter::AddHeadFilter(HtmlParse* html_parse, bool combine_multiple_heads)
31 : html_parse_(html_parse),
32 combine_multiple_heads_(combine_multiple_heads),
33 found_head_(false),
34 head_element_(NULL) {
35 }
36
37 AddHeadFilter::~AddHeadFilter() {}
38
39 void AddHeadFilter::StartDocument() {
40 found_head_ = false;
41 head_element_ = NULL;
42 }
43
44 void AddHeadFilter::StartElement(HtmlElement* element) {
45 if (!found_head_) {
46 if (element->keyword() == HtmlName::kHead) {
47 found_head_ = true;
48 head_element_ = element;
49 } else if (element->keyword() != HtmlName::kHtml) {
50 // Add head before first non-head non-html element (if no head found yet).
51 head_element_ = html_parse_->NewElement(
52 element->parent(), HtmlName::kHead);
53 html_parse_->InsertNodeBeforeNode(element, head_element_);
54 found_head_ = true;
55 }
56 }
57 }
58
59 void AddHeadFilter::EndElement(HtmlElement* element) {
60 if (combine_multiple_heads_ &&
61 (element->keyword() == HtmlName::kHead) && (element != head_element_) &&
62 (head_element_ != NULL) && html_parse_->IsRewritable(head_element_)) {
63 // Combine heads
64 if (!(html_parse_->MoveCurrentInto(head_element_) &&
65 html_parse_->DeleteSavingChildren(element))) {
66 LOG(DFATAL) << "Failed to move or delete head in " << html_parse_->url();
67 }
68 }
69 }
70
71 void AddHeadFilter::Flush() {
72 // Cannot combine heads across flush, so we NULL the pointer.
73 head_element_ = NULL;
74 }
75
76 void AddHeadFilter::EndDocument() {
77 if (!found_head_) {
78 // Degenerate case: page contains no elements (or only <html> elements).
79 head_element_ = html_parse_->NewElement(NULL, HtmlName::kHead);
80 html_parse_->InsertNodeBeforeCurrent(head_element_);
81 found_head_ = true;
82 }
83 }
84
85 } // namespace net_instaweb