2014-06-24 22:12:07 -07:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
2013-03-06 20:57:00 -08:00
|
|
|
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
|
|
|
|
const DOMUtils = Cc["@mozilla.org/inspector/dom-utils;1"].getService(Ci.inIDOMUtils);
|
|
|
|
|
|
|
|
function FontInspector(inspector, window)
|
|
|
|
{
|
|
|
|
this.inspector = inspector;
|
|
|
|
this.chromeDoc = window.document;
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
FontInspector.prototype = {
|
|
|
|
init: function FI_init() {
|
|
|
|
this.update = this.update.bind(this);
|
|
|
|
this.onNewNode = this.onNewNode.bind(this);
|
|
|
|
this.inspector.selection.on("new-node", this.onNewNode);
|
|
|
|
this.inspector.sidebar.on("fontinspector-selected", this.onNewNode);
|
2014-01-24 08:49:37 -08:00
|
|
|
this.showAll = this.showAll.bind(this);
|
|
|
|
this.showAllButton = this.chromeDoc.getElementById("showall");
|
|
|
|
this.showAllButton.addEventListener("click", this.showAll);
|
2013-03-06 20:57:00 -08:00
|
|
|
this.update();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the fontinspector visible in the sidebar?
|
|
|
|
*/
|
|
|
|
isActive: function FI_isActive() {
|
|
|
|
return this.inspector.sidebar &&
|
|
|
|
this.inspector.sidebar.getCurrentTabID() == "fontinspector";
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove listeners.
|
|
|
|
*/
|
|
|
|
destroy: function FI_destroy() {
|
|
|
|
this.chromeDoc = null;
|
|
|
|
this.inspector.sidebar.off("layoutview-selected", this.onNewNode);
|
|
|
|
this.inspector.selection.off("new-node", this.onNewNode);
|
2014-01-24 08:49:37 -08:00
|
|
|
this.showAllButton.removeEventListener("click", this.showAll);
|
2013-03-06 20:57:00 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Selection 'new-node' event handler.
|
|
|
|
*/
|
|
|
|
onNewNode: function FI_onNewNode() {
|
|
|
|
if (this.isActive() &&
|
2013-06-07 14:32:32 -07:00
|
|
|
this.inspector.selection.isLocal() &&
|
2013-03-06 20:57:00 -08:00
|
|
|
this.inspector.selection.isConnected() &&
|
2014-01-09 03:36:01 -08:00
|
|
|
this.inspector.selection.isElementNode()) {
|
2013-03-06 20:57:00 -08:00
|
|
|
this.undim();
|
|
|
|
this.update();
|
|
|
|
} else {
|
|
|
|
this.dim();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hide the font list. No node are selected.
|
|
|
|
*/
|
|
|
|
dim: function FI_dim() {
|
|
|
|
this.chromeDoc.body.classList.add("dim");
|
|
|
|
this.chromeDoc.querySelector("#all-fonts").innerHTML = "";
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the font list. A node is selected.
|
|
|
|
*/
|
|
|
|
undim: function FI_undim() {
|
|
|
|
this.chromeDoc.body.classList.remove("dim");
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve all the font related info we have for the selected
|
|
|
|
* node and display them.
|
|
|
|
*/
|
|
|
|
update: function FI_update() {
|
|
|
|
if (!this.isActive() ||
|
|
|
|
!this.inspector.selection.isConnected() ||
|
|
|
|
!this.inspector.selection.isElementNode() ||
|
|
|
|
this.chromeDoc.body.classList.contains("dim")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let node = this.inspector.selection.node;
|
|
|
|
let contentDocument = node.ownerDocument;
|
|
|
|
|
|
|
|
// We don't get fonts for a node, but for a range
|
|
|
|
let rng = contentDocument.createRange();
|
|
|
|
rng.selectNode(node);
|
|
|
|
let fonts = DOMUtils.getUsedFontFaces(rng);
|
|
|
|
let fontsArray = [];
|
|
|
|
for (let i = 0; i < fonts.length; i++) {
|
|
|
|
fontsArray.push(fonts.item(i));
|
|
|
|
}
|
|
|
|
fontsArray = fontsArray.sort(function(a, b) {
|
|
|
|
return a.srcIndex < b.srcIndex;
|
|
|
|
});
|
|
|
|
this.chromeDoc.querySelector("#all-fonts").innerHTML = "";
|
|
|
|
for (let f of fontsArray) {
|
|
|
|
this.render(f, contentDocument);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the information of one font.
|
|
|
|
*/
|
|
|
|
render: function FI_render(font, document) {
|
|
|
|
let s = this.chromeDoc.querySelector("#template > section");
|
|
|
|
s = s.cloneNode(true);
|
|
|
|
|
|
|
|
s.querySelector(".font-name").textContent = font.name;
|
|
|
|
s.querySelector(".font-css-name").textContent = font.CSSFamilyName;
|
|
|
|
s.querySelector(".font-format").textContent = font.format;
|
|
|
|
|
|
|
|
if (font.srcIndex == -1) {
|
|
|
|
s.classList.add("is-local");
|
|
|
|
} else {
|
|
|
|
s.classList.add("is-remote");
|
|
|
|
}
|
|
|
|
|
|
|
|
s.querySelector(".font-url").value = font.URI;
|
|
|
|
|
|
|
|
let iframe = s.querySelector(".font-preview");
|
|
|
|
if (font.rule) {
|
|
|
|
// This is the @font-face{…} code.
|
|
|
|
let cssText = font.rule.style.parentRule.cssText;
|
|
|
|
|
|
|
|
s.classList.add("has-code");
|
|
|
|
s.querySelector(".font-css-code").textContent = cssText;
|
|
|
|
|
|
|
|
// We guess the base URL of the stylesheet to make
|
|
|
|
// sure the font will be accessible in the preview.
|
|
|
|
// If the font-face is in an inline <style>, we get
|
|
|
|
// the location of the page.
|
|
|
|
let origin = font.rule.style.parentRule.parentStyleSheet.href;
|
|
|
|
if (!origin) { // Inline stylesheet
|
|
|
|
origin = document.location.href;
|
|
|
|
}
|
|
|
|
// We remove the last part of the URL to get a correct base.
|
|
|
|
let base = origin.replace(/\/[^\/]*$/,"/")
|
|
|
|
|
|
|
|
// From all this information, we build a preview.
|
|
|
|
this.buildPreview(iframe, font.CSSFamilyName, cssText, base);
|
|
|
|
} else {
|
|
|
|
this.buildPreview(iframe, font.CSSFamilyName, "", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.chromeDoc.querySelector("#all-fonts").appendChild(s);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a preview of the font in an iframe.
|
|
|
|
*/
|
|
|
|
buildPreview: function FI_buildPreview(iframe, name, cssCode, base) {
|
|
|
|
/* The HTML code of the preview is:
|
|
|
|
* <!DOCTYPE HTML>
|
|
|
|
* <head>
|
|
|
|
* <base href="{base}"></base>
|
|
|
|
* </head>
|
|
|
|
* <style>
|
|
|
|
* p {font-family: {name};}
|
|
|
|
* * {font-size: 40px;line-height:60px;padding:0 10px;margin:0};
|
|
|
|
* </style>
|
2013-07-08 03:14:17 -07:00
|
|
|
* <p contenteditable spellcheck='false'>Abc</p>
|
2013-03-06 20:57:00 -08:00
|
|
|
*/
|
|
|
|
let extraCSS = "* {padding:0;margin:0}";
|
2013-03-27 15:20:38 -07:00
|
|
|
extraCSS += ".theme-dark {color: white}";
|
2013-03-06 20:57:00 -08:00
|
|
|
extraCSS += "p {font-size: 40px;line-height:60px;padding:0 10px;margin:0;}";
|
|
|
|
cssCode += extraCSS;
|
2013-07-08 03:14:17 -07:00
|
|
|
let src = "data:text/html;charset=utf-8,<!DOCTYPE HTML><head><base></base></head><style></style><p contenteditable spellcheck='false'>Abc</p>";
|
2013-03-06 20:57:00 -08:00
|
|
|
iframe.addEventListener("load", function onload() {
|
|
|
|
iframe.removeEventListener("load", onload, true);
|
|
|
|
let doc = iframe.contentWindow.document;
|
|
|
|
// We could have done that earlier, but we want to avoid any URL-encoding
|
|
|
|
// nightmare.
|
|
|
|
doc.querySelector("base").href = base;
|
|
|
|
doc.querySelector("style").textContent = cssCode;
|
2013-05-25 01:51:31 -07:00
|
|
|
doc.querySelector("p").style.fontFamily = name;
|
2013-03-27 15:20:38 -07:00
|
|
|
// Forward theme
|
|
|
|
doc.documentElement.className = document.documentElement.className;
|
2013-03-06 20:57:00 -08:00
|
|
|
}, true);
|
|
|
|
iframe.src = src;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Select the <body> to show all the fonts included in the document.
|
|
|
|
*/
|
|
|
|
showAll: function FI_showAll() {
|
|
|
|
if (!this.isActive() ||
|
|
|
|
!this.inspector.selection.isConnected() ||
|
|
|
|
!this.inspector.selection.isElementNode()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-01-23 07:54:04 -08:00
|
|
|
|
|
|
|
// Select the body node to show all fonts
|
|
|
|
let walker = this.inspector.walker;
|
|
|
|
|
|
|
|
walker.getRootNode().then(root => walker.querySelector(root, "body")).then(body => {
|
|
|
|
this.inspector.selection.setNodeFront(body, "fontinspector");
|
|
|
|
});
|
2013-03-06 20:57:00 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
window.setPanel = function(panel) {
|
|
|
|
window.fontInspector = new FontInspector(panel, window);
|
|
|
|
}
|
|
|
|
|
|
|
|
window.onunload = function() {
|
2013-05-24 04:06:00 -07:00
|
|
|
if (window.fontInspector) {
|
|
|
|
window.fontInspector.destroy();
|
|
|
|
}
|
2013-03-06 20:57:00 -08:00
|
|
|
}
|