Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/xml/XMLStylesheetProcessingInstruction.cpp
Line
Count
Source (jump to first uncovered line)
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/. */
6
7
#include "XMLStylesheetProcessingInstruction.h"
8
#include "nsContentUtils.h"
9
#include "nsNetUtil.h"
10
11
namespace mozilla {
12
namespace dom {
13
14
// nsISupports implementation
15
16
NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED(XMLStylesheetProcessingInstruction,
17
                                             ProcessingInstruction,
18
                                             nsIStyleSheetLinkingElement)
19
20
NS_IMPL_CYCLE_COLLECTION_CLASS(XMLStylesheetProcessingInstruction)
21
22
0
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(XMLStylesheetProcessingInstruction,
23
0
                                                  ProcessingInstruction)
24
0
  tmp->nsStyleLinkElement::Traverse(cb);
25
0
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
26
27
0
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(XMLStylesheetProcessingInstruction,
28
0
                                                ProcessingInstruction)
29
0
  tmp->nsStyleLinkElement::Unlink();
30
0
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
31
32
33
XMLStylesheetProcessingInstruction::~XMLStylesheetProcessingInstruction()
34
0
{
35
0
}
36
37
// nsIContent
38
39
nsresult
40
XMLStylesheetProcessingInstruction::BindToTree(nsIDocument* aDocument,
41
                                               nsIContent* aParent,
42
                                               nsIContent* aBindingParent)
43
0
{
44
0
  nsresult rv = ProcessingInstruction::BindToTree(aDocument, aParent,
45
0
                                                  aBindingParent);
46
0
  NS_ENSURE_SUCCESS(rv, rv);
47
0
48
0
  void (XMLStylesheetProcessingInstruction::*update)() =
49
0
    &XMLStylesheetProcessingInstruction::UpdateStyleSheetInternal;
50
0
  nsContentUtils::AddScriptRunner(NewRunnableMethod(
51
0
    "dom::XMLStylesheetProcessingInstruction::BindToTree", this, update));
52
0
53
0
  return rv;
54
0
}
55
56
void
57
XMLStylesheetProcessingInstruction::UnbindFromTree(bool aDeep, bool aNullParent)
58
0
{
59
0
  nsCOMPtr<nsIDocument> oldDoc = GetUncomposedDoc();
60
0
61
0
  ProcessingInstruction::UnbindFromTree(aDeep, aNullParent);
62
0
  Unused << UpdateStyleSheetInternal(oldDoc, nullptr);
63
0
}
64
65
// nsINode
66
67
void
68
XMLStylesheetProcessingInstruction::SetNodeValueInternal(const nsAString& aNodeValue,
69
                                                         ErrorResult& aError)
70
0
{
71
0
  CharacterData::SetNodeValueInternal(aNodeValue, aError);
72
0
  if (!aError.Failed()) {
73
0
    Unused << UpdateStyleSheetInternal(nullptr, nullptr, ForceUpdate::Yes);
74
0
  }
75
0
}
76
77
// nsStyleLinkElement
78
79
void
80
XMLStylesheetProcessingInstruction::GetCharset(nsAString& aCharset)
81
0
{
82
0
  if (!GetAttrValue(nsGkAtoms::charset, aCharset)) {
83
0
    aCharset.Truncate();
84
0
  }
85
0
}
86
87
/* virtual */ void
88
XMLStylesheetProcessingInstruction::OverrideBaseURI(nsIURI* aNewBaseURI)
89
0
{
90
0
  mOverriddenBaseURI = aNewBaseURI;
91
0
}
92
93
Maybe<nsStyleLinkElement::SheetInfo>
94
XMLStylesheetProcessingInstruction::GetStyleSheetInfo()
95
0
{
96
0
  // xml-stylesheet PI is special only in prolog
97
0
  if (!nsContentUtils::InProlog(this)) {
98
0
    return Nothing();
99
0
  }
100
0
101
0
  nsAutoString href;
102
0
  if (!GetAttrValue(nsGkAtoms::href, href)) {
103
0
    return Nothing();
104
0
  }
105
0
106
0
  nsAutoString data;
107
0
  GetData(data);
108
0
109
0
  nsAutoString title;
110
0
  nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::title, title);
111
0
112
0
  nsAutoString alternateAttr;
113
0
  nsContentUtils::GetPseudoAttributeValue(data,
114
0
                                          nsGkAtoms::alternate,
115
0
                                          alternateAttr);
116
0
117
0
  bool alternate = alternateAttr.EqualsLiteral("yes");
118
0
  if (alternate && title.IsEmpty()) {
119
0
    // alternates must have title
120
0
    return Nothing();
121
0
  }
122
0
123
0
  nsAutoString media;
124
0
  nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::media, media);
125
0
126
0
  // Make sure the type handling here matches
127
0
  // nsXMLContentSink::HandleProcessingInstruction
128
0
  nsAutoString type;
129
0
  nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::type, type);
130
0
131
0
  nsAutoString mimeType, notUsed;
132
0
  nsContentUtils::SplitMimeType(type, mimeType, notUsed);
133
0
  if (!mimeType.IsEmpty() && !mimeType.LowerCaseEqualsLiteral("text/css")) {
134
0
    return Nothing();
135
0
  }
136
0
137
0
  nsIDocument* doc = OwnerDoc();
138
0
  nsIURI* baseURL =
139
0
    mOverriddenBaseURI ? mOverriddenBaseURI.get() : doc->GetDocBaseURI();
140
0
  auto encoding = doc->GetDocumentCharacterSet();
141
0
  nsCOMPtr<nsIURI> uri;
142
0
  NS_NewURI(getter_AddRefs(uri), href, encoding, baseURL);
143
0
  return Some(SheetInfo {
144
0
    *doc,
145
0
    this,
146
0
    uri.forget(),
147
0
    nullptr,
148
0
    net::RP_Unset,
149
0
    CORS_NONE,
150
0
    title,
151
0
    media,
152
0
    alternate ? HasAlternateRel::Yes : HasAlternateRel::No,
153
0
    IsInline::No,
154
0
  });
155
0
}
156
157
already_AddRefed<CharacterData>
158
XMLStylesheetProcessingInstruction::CloneDataNode(mozilla::dom::NodeInfo *aNodeInfo,
159
                                                  bool aCloneText) const
160
0
{
161
0
  nsAutoString data;
162
0
  GetData(data);
163
0
  RefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
164
0
  return do_AddRef(new XMLStylesheetProcessingInstruction(ni.forget(), data));
165
0
}
166
167
} // namespace dom
168
} // namespace mozilla