HTMLFrameSetElement.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "HTMLFrameSetElement.h"
  6. #include "mozilla/dom/HTMLFrameSetElementBinding.h"
  7. #include "mozilla/dom/EventHandlerBinding.h"
  8. #include "nsContentUtils.h"
  9. #include "nsGlobalWindow.h"
  10. #include "mozilla/UniquePtrExtensions.h"
  11. #include "nsAttrValueOrString.h"
  12. NS_IMPL_NS_NEW_HTML_ELEMENT(FrameSet)
  13. namespace mozilla {
  14. namespace dom {
  15. HTMLFrameSetElement::~HTMLFrameSetElement()
  16. {
  17. }
  18. JSObject*
  19. HTMLFrameSetElement::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto)
  20. {
  21. return HTMLFrameSetElementBinding::Wrap(aCx, this, aGivenProto);
  22. }
  23. NS_IMPL_ISUPPORTS_INHERITED(HTMLFrameSetElement, nsGenericHTMLElement,
  24. nsIDOMHTMLFrameSetElement)
  25. NS_IMPL_ELEMENT_CLONE(HTMLFrameSetElement)
  26. NS_IMETHODIMP
  27. HTMLFrameSetElement::SetCols(const nsAString& aCols)
  28. {
  29. ErrorResult rv;
  30. SetCols(aCols, rv);
  31. return rv.StealNSResult();
  32. }
  33. NS_IMETHODIMP
  34. HTMLFrameSetElement::GetCols(nsAString& aCols)
  35. {
  36. DOMString cols;
  37. GetCols(cols);
  38. cols.ToString(aCols);
  39. return NS_OK;
  40. }
  41. NS_IMETHODIMP
  42. HTMLFrameSetElement::SetRows(const nsAString& aRows)
  43. {
  44. ErrorResult rv;
  45. SetRows(aRows, rv);
  46. return rv.StealNSResult();
  47. }
  48. NS_IMETHODIMP
  49. HTMLFrameSetElement::GetRows(nsAString& aRows)
  50. {
  51. DOMString rows;
  52. GetRows(rows);
  53. rows.ToString(aRows);
  54. return NS_OK;
  55. }
  56. nsresult
  57. HTMLFrameSetElement::BeforeSetAttr(int32_t aNamespaceID, nsIAtom* aName,
  58. const nsAttrValueOrString* aValue,
  59. bool aNotify)
  60. {
  61. /* The main goal here is to see whether the _number_ of rows or
  62. * columns has changed. If it has, we need to reframe; otherwise
  63. * we want to reflow.
  64. * Ideally, the style hint would be changed back to reflow after the reframe
  65. * has been performed. Unfortunately, however, the reframe will be performed
  66. * by the call to nsNodeUtils::AttributeChanged, which occurs *after*
  67. * AfterSetAttr is called, leaving us with no convenient way of changing the
  68. * value back to reflow afterwards. However, nsNodeUtils::AttributeChanged is
  69. * effectively the only consumer of this value, so as long as we always set
  70. * the value correctly here, we should be fine.
  71. */
  72. mCurrentRowColHint = NS_STYLE_HINT_REFLOW;
  73. if (aNamespaceID == kNameSpaceID_None) {
  74. if (aName == nsGkAtoms::rows) {
  75. if (aValue) {
  76. int32_t oldRows = mNumRows;
  77. ParseRowCol(aValue->String(), mNumRows, &mRowSpecs);
  78. if (mNumRows != oldRows) {
  79. mCurrentRowColHint = nsChangeHint_ReconstructFrame;
  80. }
  81. }
  82. } else if (aName == nsGkAtoms::cols) {
  83. if (aValue) {
  84. int32_t oldCols = mNumCols;
  85. ParseRowCol(aValue->String(), mNumCols, &mColSpecs);
  86. if (mNumCols != oldCols) {
  87. mCurrentRowColHint = nsChangeHint_ReconstructFrame;
  88. }
  89. }
  90. }
  91. }
  92. return nsGenericHTMLElement::BeforeSetAttr(aNamespaceID, aName, aValue, aNotify);
  93. }
  94. nsresult
  95. HTMLFrameSetElement::GetRowSpec(int32_t *aNumValues,
  96. const nsFramesetSpec** aSpecs)
  97. {
  98. NS_PRECONDITION(aNumValues, "Must have a pointer to an integer here!");
  99. NS_PRECONDITION(aSpecs, "Must have a pointer to an array of nsFramesetSpecs");
  100. *aNumValues = 0;
  101. *aSpecs = nullptr;
  102. if (!mRowSpecs) {
  103. const nsAttrValue* value = GetParsedAttr(nsGkAtoms::rows);
  104. if (value && value->Type() == nsAttrValue::eString) {
  105. nsresult rv = ParseRowCol(value->GetStringValue(), mNumRows,
  106. &mRowSpecs);
  107. NS_ENSURE_SUCCESS(rv, rv);
  108. }
  109. if (!mRowSpecs) { // we may not have had an attr or had an empty attr
  110. mRowSpecs = MakeUnique<nsFramesetSpec[]>(1);
  111. mNumRows = 1;
  112. mRowSpecs[0].mUnit = eFramesetUnit_Relative;
  113. mRowSpecs[0].mValue = 1;
  114. }
  115. }
  116. *aSpecs = mRowSpecs.get();
  117. *aNumValues = mNumRows;
  118. return NS_OK;
  119. }
  120. nsresult
  121. HTMLFrameSetElement::GetColSpec(int32_t *aNumValues,
  122. const nsFramesetSpec** aSpecs)
  123. {
  124. NS_PRECONDITION(aNumValues, "Must have a pointer to an integer here!");
  125. NS_PRECONDITION(aSpecs, "Must have a pointer to an array of nsFramesetSpecs");
  126. *aNumValues = 0;
  127. *aSpecs = nullptr;
  128. if (!mColSpecs) {
  129. const nsAttrValue* value = GetParsedAttr(nsGkAtoms::cols);
  130. if (value && value->Type() == nsAttrValue::eString) {
  131. nsresult rv = ParseRowCol(value->GetStringValue(), mNumCols,
  132. &mColSpecs);
  133. NS_ENSURE_SUCCESS(rv, rv);
  134. }
  135. if (!mColSpecs) { // we may not have had an attr or had an empty attr
  136. mColSpecs = MakeUnique<nsFramesetSpec[]>(1);
  137. mNumCols = 1;
  138. mColSpecs[0].mUnit = eFramesetUnit_Relative;
  139. mColSpecs[0].mValue = 1;
  140. }
  141. }
  142. *aSpecs = mColSpecs.get();
  143. *aNumValues = mNumCols;
  144. return NS_OK;
  145. }
  146. bool
  147. HTMLFrameSetElement::ParseAttribute(int32_t aNamespaceID,
  148. nsIAtom* aAttribute,
  149. const nsAString& aValue,
  150. nsAttrValue& aResult)
  151. {
  152. if (aNamespaceID == kNameSpaceID_None) {
  153. if (aAttribute == nsGkAtoms::bordercolor) {
  154. return aResult.ParseColor(aValue);
  155. }
  156. if (aAttribute == nsGkAtoms::frameborder) {
  157. return nsGenericHTMLElement::ParseFrameborderValue(aValue, aResult);
  158. }
  159. if (aAttribute == nsGkAtoms::border) {
  160. return aResult.ParseIntWithBounds(aValue, 0, 100);
  161. }
  162. }
  163. return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
  164. aResult);
  165. }
  166. nsChangeHint
  167. HTMLFrameSetElement::GetAttributeChangeHint(const nsIAtom* aAttribute,
  168. int32_t aModType) const
  169. {
  170. nsChangeHint retval =
  171. nsGenericHTMLElement::GetAttributeChangeHint(aAttribute, aModType);
  172. if (aAttribute == nsGkAtoms::rows ||
  173. aAttribute == nsGkAtoms::cols) {
  174. retval |= mCurrentRowColHint;
  175. }
  176. return retval;
  177. }
  178. /**
  179. * Translate a "rows" or "cols" spec into an array of nsFramesetSpecs
  180. */
  181. nsresult
  182. HTMLFrameSetElement::ParseRowCol(const nsAString & aValue,
  183. int32_t& aNumSpecs,
  184. UniquePtr<nsFramesetSpec[]>* aSpecs)
  185. {
  186. if (aValue.IsEmpty()) {
  187. aNumSpecs = 0;
  188. *aSpecs = nullptr;
  189. return NS_OK;
  190. }
  191. static const char16_t sAster('*');
  192. static const char16_t sPercent('%');
  193. static const char16_t sComma(',');
  194. nsAutoString spec(aValue);
  195. // remove whitespace (Bug 33699) and quotation marks (bug 224598)
  196. // also remove leading/trailing commas (bug 31482)
  197. spec.StripChars(" \n\r\t\"\'");
  198. spec.Trim(",");
  199. // Count the commas. Don't count more than X commas (bug 576447).
  200. static_assert(NS_MAX_FRAMESET_SPEC_COUNT * sizeof(nsFramesetSpec) < (1 << 30),
  201. "Too many frameset specs allowed to allocate");
  202. int32_t commaX = spec.FindChar(sComma);
  203. int32_t count = 1;
  204. while (commaX != kNotFound && count < NS_MAX_FRAMESET_SPEC_COUNT) {
  205. count++;
  206. commaX = spec.FindChar(sComma, commaX + 1);
  207. }
  208. auto specs = MakeUniqueFallible<nsFramesetSpec[]>(count);
  209. if (!specs) {
  210. *aSpecs = nullptr;
  211. aNumSpecs = 0;
  212. return NS_ERROR_OUT_OF_MEMORY;
  213. }
  214. // Pre-grab the compat mode; we may need it later in the loop.
  215. bool isInQuirks = InNavQuirksMode(OwnerDoc());
  216. // Parse each comma separated token
  217. int32_t start = 0;
  218. int32_t specLen = spec.Length();
  219. for (int32_t i = 0; i < count; i++) {
  220. // Find our comma
  221. commaX = spec.FindChar(sComma, start);
  222. NS_ASSERTION(i == count - 1 || commaX != kNotFound,
  223. "Failed to find comma, somehow");
  224. int32_t end = (commaX == kNotFound) ? specLen : commaX;
  225. // Note: If end == start then it means that the token has no
  226. // data in it other than a terminating comma (or the end of the spec).
  227. // So default to a fixed width of 0.
  228. specs[i].mUnit = eFramesetUnit_Fixed;
  229. specs[i].mValue = 0;
  230. if (end > start) {
  231. int32_t numberEnd = end;
  232. char16_t ch = spec.CharAt(numberEnd - 1);
  233. if (sAster == ch) {
  234. specs[i].mUnit = eFramesetUnit_Relative;
  235. numberEnd--;
  236. } else if (sPercent == ch) {
  237. specs[i].mUnit = eFramesetUnit_Percent;
  238. numberEnd--;
  239. // check for "*%"
  240. if (numberEnd > start) {
  241. ch = spec.CharAt(numberEnd - 1);
  242. if (sAster == ch) {
  243. specs[i].mUnit = eFramesetUnit_Relative;
  244. numberEnd--;
  245. }
  246. }
  247. }
  248. // Translate value to an integer
  249. nsAutoString token;
  250. spec.Mid(token, start, numberEnd - start);
  251. // Treat * as 1*
  252. if ((eFramesetUnit_Relative == specs[i].mUnit) &&
  253. (0 == token.Length())) {
  254. specs[i].mValue = 1;
  255. }
  256. else {
  257. // Otherwise just convert to integer.
  258. nsresult err;
  259. specs[i].mValue = token.ToInteger(&err);
  260. if (NS_FAILED(err)) {
  261. specs[i].mValue = 0;
  262. }
  263. }
  264. // Treat 0* as 1* in quirks mode (bug 40383)
  265. if (isInQuirks) {
  266. if ((eFramesetUnit_Relative == specs[i].mUnit) &&
  267. (0 == specs[i].mValue)) {
  268. specs[i].mValue = 1;
  269. }
  270. }
  271. // Catch zero and negative frame sizes for Nav compatibility
  272. // Nav resized absolute and relative frames to "1" and
  273. // percent frames to an even percentage of the width
  274. //
  275. //if (isInQuirks && (specs[i].mValue <= 0)) {
  276. // if (eFramesetUnit_Percent == specs[i].mUnit) {
  277. // specs[i].mValue = 100 / count;
  278. // } else {
  279. // specs[i].mValue = 1;
  280. // }
  281. //} else {
  282. // In standards mode, just set negative sizes to zero
  283. if (specs[i].mValue < 0) {
  284. specs[i].mValue = 0;
  285. }
  286. start = end + 1;
  287. }
  288. }
  289. aNumSpecs = count;
  290. // Transfer ownership to caller here
  291. *aSpecs = Move(specs);
  292. return NS_OK;
  293. }
  294. bool
  295. HTMLFrameSetElement::IsEventAttributeName(nsIAtom *aName)
  296. {
  297. return nsContentUtils::IsEventAttributeName(aName,
  298. EventNameType_HTML |
  299. EventNameType_HTMLBodyOrFramesetOnly);
  300. }
  301. #define EVENT(name_, id_, type_, struct_) /* nothing; handled by the shim */
  302. // nsGenericHTMLElement::GetOnError returns
  303. // already_AddRefed<EventHandlerNonNull> while other getters return
  304. // EventHandlerNonNull*, so allow passing in the type to use here.
  305. #define WINDOW_EVENT_HELPER(name_, type_) \
  306. type_* \
  307. HTMLFrameSetElement::GetOn##name_() \
  308. { \
  309. if (nsPIDOMWindowInner* win = OwnerDoc()->GetInnerWindow()) { \
  310. nsGlobalWindow* globalWin = nsGlobalWindow::Cast(win); \
  311. return globalWin->GetOn##name_(); \
  312. } \
  313. return nullptr; \
  314. } \
  315. void \
  316. HTMLFrameSetElement::SetOn##name_(type_* handler) \
  317. { \
  318. nsPIDOMWindowInner* win = OwnerDoc()->GetInnerWindow(); \
  319. if (!win) { \
  320. return; \
  321. } \
  322. \
  323. nsGlobalWindow* globalWin = nsGlobalWindow::Cast(win); \
  324. return globalWin->SetOn##name_(handler); \
  325. }
  326. #define WINDOW_EVENT(name_, id_, type_, struct_) \
  327. WINDOW_EVENT_HELPER(name_, EventHandlerNonNull)
  328. #define BEFOREUNLOAD_EVENT(name_, id_, type_, struct_) \
  329. WINDOW_EVENT_HELPER(name_, OnBeforeUnloadEventHandlerNonNull)
  330. #include "mozilla/EventNameList.h" // IWYU pragma: keep
  331. #undef BEFOREUNLOAD_EVENT
  332. #undef WINDOW_EVENT
  333. #undef WINDOW_EVENT_HELPER
  334. #undef EVENT
  335. } // namespace dom
  336. } // namespace mozilla