browser/components/sessionstore/src/PageStyle.jsm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 this.EXPORTED_SYMBOLS = ["PageStyle"];
michael@0 8
michael@0 9 const Ci = Components.interfaces;
michael@0 10
michael@0 11 /**
michael@0 12 * The external API exported by this module.
michael@0 13 */
michael@0 14 this.PageStyle = Object.freeze({
michael@0 15 collect: function (docShell, frameTree) {
michael@0 16 return PageStyleInternal.collect(docShell, frameTree);
michael@0 17 },
michael@0 18
michael@0 19 restore: function (docShell, frameList, pageStyle) {
michael@0 20 PageStyleInternal.restore(docShell, frameList, pageStyle);
michael@0 21 },
michael@0 22
michael@0 23 restoreTree: function (docShell, data) {
michael@0 24 PageStyleInternal.restoreTree(docShell, data);
michael@0 25 }
michael@0 26 });
michael@0 27
michael@0 28 // Signifies that author style level is disabled for the page.
michael@0 29 const NO_STYLE = "_nostyle";
michael@0 30
michael@0 31 let PageStyleInternal = {
michael@0 32 /**
michael@0 33 * Collects the selected style sheet sets for all reachable frames.
michael@0 34 */
michael@0 35 collect: function (docShell, frameTree) {
michael@0 36 let result = frameTree.map(({document: doc}) => {
michael@0 37 let style;
michael@0 38
michael@0 39 if (doc) {
michael@0 40 // http://dev.w3.org/csswg/cssom/#persisting-the-selected-css-style-sheet-set
michael@0 41 style = doc.selectedStyleSheetSet || doc.lastStyleSheetSet;
michael@0 42 }
michael@0 43
michael@0 44 return style ? {pageStyle: style} : null;
michael@0 45 });
michael@0 46
michael@0 47 let markupDocumentViewer =
michael@0 48 docShell.contentViewer.QueryInterface(Ci.nsIMarkupDocumentViewer);
michael@0 49
michael@0 50 if (markupDocumentViewer.authorStyleDisabled) {
michael@0 51 result = result || {};
michael@0 52 result.disabled = true;
michael@0 53 }
michael@0 54
michael@0 55 return result && Object.keys(result).length ? result : null;
michael@0 56 },
michael@0 57
michael@0 58 /**
michael@0 59 * Restore the selected style sheet of all the frames in frameList
michael@0 60 * to match |pageStyle|.
michael@0 61 * @param docShell the root docshell of all the frames
michael@0 62 * @param frameList a list of [frame, data] pairs, where frame is a
michael@0 63 * DOM window and data is the session restore data associated with
michael@0 64 * it.
michael@0 65 * @param pageStyle the title of the style sheet to apply
michael@0 66 */
michael@0 67 restore: function (docShell, frameList, pageStyle) {
michael@0 68 let disabled = pageStyle == NO_STYLE;
michael@0 69
michael@0 70 let markupDocumentViewer =
michael@0 71 docShell.contentViewer.QueryInterface(Ci.nsIMarkupDocumentViewer);
michael@0 72 markupDocumentViewer.authorStyleDisabled = disabled;
michael@0 73
michael@0 74 for (let [frame, data] of frameList) {
michael@0 75 Array.forEach(frame.document.styleSheets, function(aSS) {
michael@0 76 aSS.disabled = aSS.title && aSS.title != pageStyle;
michael@0 77 });
michael@0 78 }
michael@0 79 },
michael@0 80
michael@0 81 /**
michael@0 82 * Restores pageStyle data for the current frame hierarchy starting at the
michael@0 83 * |docShell's| current DOMWindow using the given pageStyle |data|.
michael@0 84 *
michael@0 85 * Warning: If the current frame hierarchy doesn't match that of the given
michael@0 86 * |data| object we will silently discard data for unreachable frames. We may
michael@0 87 * as well assign page styles to the wrong frames if some were reordered or
michael@0 88 * removed.
michael@0 89 *
michael@0 90 * @param docShell (nsIDocShell)
michael@0 91 * @param data (object)
michael@0 92 * {
michael@0 93 * disabled: true, // when true, author styles will be disabled
michael@0 94 * pageStyle: "Dusk",
michael@0 95 * children: [
michael@0 96 * null,
michael@0 97 * {pageStyle: "Mozilla", children: [ ... ]}
michael@0 98 * ]
michael@0 99 * }
michael@0 100 */
michael@0 101 restoreTree: function (docShell, data) {
michael@0 102 let disabled = data.disabled || false;
michael@0 103 let markupDocumentViewer =
michael@0 104 docShell.contentViewer.QueryInterface(Ci.nsIMarkupDocumentViewer);
michael@0 105 markupDocumentViewer.authorStyleDisabled = disabled;
michael@0 106
michael@0 107 function restoreFrame(root, data) {
michael@0 108 if (data.hasOwnProperty("pageStyle")) {
michael@0 109 root.document.selectedStyleSheetSet = data.pageStyle;
michael@0 110 }
michael@0 111
michael@0 112 if (!data.hasOwnProperty("children")) {
michael@0 113 return;
michael@0 114 }
michael@0 115
michael@0 116 let frames = root.frames;
michael@0 117 data.children.forEach((child, index) => {
michael@0 118 if (child && index < frames.length) {
michael@0 119 restoreFrame(frames[index], child);
michael@0 120 }
michael@0 121 });
michael@0 122 }
michael@0 123
michael@0 124 let ifreq = docShell.QueryInterface(Ci.nsIInterfaceRequestor);
michael@0 125 restoreFrame(ifreq.getInterface(Ci.nsIDOMWindow), data);
michael@0 126 }
michael@0 127 };

mercurial