programming firefox

www.it-ebooks.info Programming Firefox www.it-ebooks.info Other resources from O’Reilly Related titles oreilly.com...

1 downloads 86 Views
www.it-ebooks.info

Programming Firefox

www.it-ebooks.info

Other resources from O’Reilly Related titles

oreilly.com

Ajax Design Patterns Ajax Hacks™ Creating Applications with Mozilla CSS: The Definitive Guide Greasemonkey Hacks™

JavaScript: The Definitive Guide Learning JavaScript Practical RDF XML Hacks™ XML in a Nutshell

oreilly.com is more than a complete catalog of O’Reilly books. You’ll also find links to news, events, articles, weblogs, sample chapters, and code examples. oreillynet.com is the essential portal for developers interested in open and emerging technologies, including new platforms, programming languages, and operating systems.

Conferences

O’Reilly brings diverse innovators together to nurture the ideas that spark revolutionary industries. We specialize in documenting the latest tools and systems, translating the innovator’s knowledge into useful skills for those in the trenches. Visit conferences.oreilly.com for our upcoming events. Safari Bookshelf (safari.oreilly.com) is the premier online reference library for programmers and IT professionals. Conduct searches across more than 1,000 books. Subscribers can zero in on answers to time-critical questions in a matter of seconds. Read the books on your Bookshelf from cover to cover or simply flip to the page you need. Try it today for free.

www.it-ebooks.info

Programming Firefox

Kenneth C. Feldt

Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo

www.it-ebooks.info

Programming Firefox by Kenneth C. Feldt Copyright © 2007 O’Reilly Media, Inc. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected].

Editor: Simon St.Laurent Production Editor: Rachel Monaghan Copyeditor: Audrey Doyle Proofreader: Rachel Monaghan

Indexer: Reg Aubry Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrators: Robert Romano and Jessamyn Read

Printing History: April 2007:

First Edition.

Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Programming Firefox, the image of a red fox, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.

This book uses RepKover™, a durable and flexible lay-flat binding. ISBN-10: 0-596-10243-7 ISBN-13: 978-0-596-10243-2 [M]

www.it-ebooks.info

Table of Contents

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix 1. Firefox and Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Mozilla to Firefox and Thunderbird XML Technologies At the Top of It All: The DOM Mixing Document Types Getting Started

1 3 6 7 7

2. XUL Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 File Structure XUL Widgets Introducing Input Controls More Complex Containers Managing the Display Space Content Display Panels Miscellaneous Widgets Helper Features Mozilla Style Declarations Summary

12 15 24 28 35 38 39 40 42 44

3. Coding and Testing for the Real World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Defining a Target Application Adding Logic Simple Authentication Script When Things Don’t Work Summary

45 47 59 68 73

v

www.it-ebooks.info

4. Configuring for Chrome and a Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Chrome Overview Running as a Local Installation XUL-to-Server Communications Serving XUL Files Summary

76 78 80 100 107

5. Multiframe XUL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Dividing the Display Area Editing Documents Adding Dialog Windows Summary

109 113 138 152

6. Trees, Templates, and Datasources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Trees Modifying Datasources Summary

154 188 220

7. DOM Manipulation and Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 A Design Review Browser Elements Moving Text Between Frames Exporting Note Document Content Adding Interactivity to DOM Elements Summary

221 222 227 231 239 263

8. Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 A Sample Graphing Project XHTML Review SVG Overview Data-to-Graphics Transformation HTML Canvas Summary

265 266 267 272 292 299

9. Extending the Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 Overlay Files Adding Logic XBL HTTP Request Widget Summary vi |

301 308 312 321 333

Table of Contents

www.it-ebooks.info

10. XForms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 Basic XForms Structure An Example Transfer to the Server XForms Validation Features XForms Events and Actions User Interaction and Dynamic Presentation What to Do When Things Go Wrong Summary

335 336 341 359 363 373 373

11. Installation and Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 Deploying Standalone Applications Deploying Themes and Skins Adding Locales Deploying Extensions Deploying the Extension What to Do When Things Go Wrong Summary

375 382 386 391 395 400 403

12. XUL Widget Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405 Browser Package Files Developer Reference

405 410

Glossary: XUL Widgets: Attributes, Properties, and Methods . . . . . . . . . . . . . . . . . 463 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479

Table of Contents |

www.it-ebooks.info

vii

www.it-ebooks.info

Preface

1

A technology is only a tool. No matter how creative its design, innovative its approach, or impressive its performance, a technology is still defined (according to Webster) as “a manner of accomplishing a task.” The successful adoption of a technology into the community of those who rely on it to conduct business is a complicated journey—one that starts with the unyielding work of designers who see something that others don’t. But without the efforts of those who believe in its value and are willing to promote (evangelize), to educate, and to adapt the tool to the needs of the community, the technology remains little more than a subject of academic interest. The Mozilla component framework, and its implementation in a form more commonly known as the Firefox browser, represents one technology that aspires to be a useful tool for the community that relies on the Internet to communicate, learn, and express (we often coldly refer to these people as “users”). The evangelists of the Mozilla framework promote its technology as a premier contribution of the open source community, a team of developers whose mission is to develop the best possible software for browsing the Internet (Firefox) and exchanging messages (Thunderbird). This community is also strongly committed to demonstrating how applying the most current standards in electronic document rendition and data exchange can make possible new techniques that improve the richness of expression and the ability to move those rich ideas throughout the community. But to evangelize is not enough. I hope this text will play a modest role in helping to educate application developers in how to use Mozilla technology—not for the sake of using a different technical platform, but to demonstrate what is possible when the collective knowledge of the international standards community finds its voice in the marketplace. With such a diverse and creative pool of contributors, new Internet applications are possible that feature improvements in responsiveness, greater flexibility of interface design, and more expressive graphics.

ix

www.it-ebooks.info

The goal of this book has little to do with searching for a victor in the browser wars. Rather, this book is intended to discuss, through example, the application of international standards in helping to launch the “next generation” of Internet applications. The Mozilla technology is one of the best technologies, and one of the best tools available, to make such a discussion possible.

Intended Audience This book is intended for designers and developers charged with delivering innovative standards-based Internet applications. This includes those responsible for server applications or for the development of Internet-enabled desktop applications. This book is designed to cover many of the practical issues related to the nuances of XML User Interface (XUL)-based design. Rather than trying to be the authoritative resource on browser internals, it focuses on the nuts and bolts of using the existing tools to take advantage of the Firefox framework. The development setting for this audience may involve any number of tools—the Personal Hypertext Processor (PHP) or Java on the server side, and the Firefox browser engine on the client side. The dominant language for the client platform is most likely to be JavaScript, and developers should have a good understanding of HTML and, hopefully, some exposure to eXtensible Markup Language (XML)-based documents such as XHTML. Members of the target audience could be developers of commercial applications, extensions to the Firefox browser, or corporate applications. Some knowledge of JavaScript will be very helpful, although most developers can pick up the language quickly enough to learn it on the fly. Most importantly, this book is targeted to those developers who are interested in (and maybe committed to) using this technology to see what the next generation of Internet applications will look like.

Why Buy This Book? There is no doubt that online documentation, Wikis, and newsgroups provide the lion’s share of information to developers using technologies such as XUL (pronounced “zool”). A precious element of constant availability characterizes the webcentric support community—you need not carry around a 500-page book to guarantee access to important information. But web access to online documentation can go only so far. Online documents are best designed for quick searches and linked references that point to pages that, generally speaking, are read for only a few minutes at a time. Such spontaneous access

x

|

Preface

www.it-ebooks.info

works well for reference lookups or quick reminders, but thoroughly covering a topic from start to finish (as in the case of building XUL applications) requires a more comprehensive approach that involves text, illustrations, and code. The Web is also at a crossroads. Browser technologies are now stabilizing to the point where security and reliability are a given. Growing use of web standards to encode page content is also helping to improve the consistent look of content across rendering engines. The time is now ripe for the browser community to explore the next generation of web standards, and to initiate a wider range of support for new document models to render graphics, deliver music, and audit user input without needlessly imposing simple tasks on tomorrow’s web servers. This book doesn’t serve as just a reference source; it is a practical, hands-on introduction to some of these evolving standards that allow developers to combine creativity with technology. Programming Firefox is less of a how-to book and more of a what-if exploration that encourages developers to push the envelope of the Internet experience.

Conventions Used in This Book The following conventions are used throughout this book: Constant width

Used in code segments, or in terms and phrases in the text that represent code entries. Constant width bold

Code that is being referenced in the text. Constant width italic

Used in code segments meant to be customized by the reader. Italic Used to introduce new technical terms, to emphasize certain terms, and to refer to filenames or pathnames. Menu items Used with arrows to illustrate a menu hierarchy, such as File ➝ Open. This icon signifies a tip, suggestion, or general note.

This icon signifies a warning or caution.

Preface |

www.it-ebooks.info

xi

Terms and Usage This book discusses applications of a software engine consisting of a collection of cross-platform libraries written in C++. This collection of libraries was first wrapped together as a browser named Mozilla. Technically, I should call this book’s main topic the Mozilla Cross-Platform Component Model (XPCOM) framework. Not all XPCOM libraries are used in the Firefox browser, however, so I use the term Firefox framework—those libraries that are distributed as part of the browser-only components supported by the Mozilla Foundation.

A Tag or an Element? This book is about interface elements, document elements, and technologies, each having its own terminology. Several terms are used repeatedly throughout this book and should be clarified here: Widget The actual physical representation of an interface element. The term widget is most often used when discussing the physical appearance of a document. Widgets include buttons, listboxes, and checkboxes. Element The basic unit that composes XHTML documents. Tables and divs are examples of elements. Tag The XML encoding of a document element. Examples of tags are ,
, and
.... some other stuff


Any XML syntax checking would use the rules for HTML tables to validate the first table, and the rules for XUL tables to validate the second table. Now that we’ve discussed the basics of XML formatting, we can use our text editor or development tool to create theWindow.xul, which looks like this:

If we were to open this file with the Firefox browser, we would be disappointed to see a blank window, which almost gives the impression that something is wrong with the source file. But using the Firefox tool to view the source file (View ➝ Page Source) displays the source code of our file. The colorized formatting (the standard display mode for XML files) shows that the file is correctly formed, as shown in Figure 2-1. Figure 2-1. Firefox view page source window

We see nothing in our browser window because our window has no content.

14 |

Chapter 2: XUL Basics

www.it-ebooks.info

XUL Widgets XUL widgets are at the heart of building Firefox applications.

Boxes The XUL layout hierarchy is based on a primitive container—all XUL interfaces are a collection of nested boxes that contain any number of other boxes. Most developers will use two box subclasses: a that lays out child elements in a vertical alignment, and an that presents children in a horizontal alignment. A designer can control how a box positions child content by using the orient attribute: a value of horizontal will result in a box’s children being placed along a horizontal axis and a value of vertical will result in content being placed along the vertical axis. The Firefox framework supports and elements as a shortcut alternative to using the orient attribute. For our first test, we will combine our boxes with the simple XUL label element. Adding child elements to the different forms of boxes illustrates the difference in how vboxes and hboxes work:

The resulting window now illustrates the difference, as shown in Figure 2-2.

XUL Widgets

www.it-ebooks.info

|

15

Figure 2-2. hbox and vbox layouts

Adding Styling One technique to use when learning (and when debugging advanced designs) is to create a specialized appearance, or style, that is associated with a class of elements being tested. By setting distinctive colors and borders, it is sometimes easier to spot problems with a design. The style assignment follows the CSS style: style="stylePropery:propertyValue"

One style attribute can include several property assignments in one statement: style="background-color:blue; border-style:solid;"

(You can find the specific format of properties supported by the CSS Level 2 specification at http://www.w3.org/TR/REC-CSS2.) Rather than assigning this distinctive style inline, by attaching a style attribute to an element as:

we will use an internal CSS declaration to provide a style to a class name that we can move to any element being tested (later in this book, we will migrate to using an externally linked stylesheet). We make some changes to our source file to see how such styling can help clarify the details of our XUL elements:
16 |

Chapter 2: XUL Basics

www.it-ebooks.info

border-style:dashed; border-width: thin; } "?> . . .

The highlighted text shows how stylesheets are attached to XML files; the reference uses a class selector to bind a gray box with a dashed outline to any element with a class attribute of test_a. Changing one of the box references to add the class attribute: