O'reilly Programming Firefox.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
Programming Firefox Kenneth C. Feldt Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo 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 Indexer: Reg Aubry Production Editor: Rachel Monaghan Cover Designer: Karen Montgomery Copyeditor: Audrey Doyle Interior Designer: David Futato Proofreader: Rachel Monaghan 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] Table of Contents Preface . ix 1. Firefox and Friends . 1 Mozilla to Firefox and Thunderbird 1 XML Technologies 3 At the Top of It All: The DOM 6 Mixing Document Types 7 Getting Started 7 2. XUL Basics . 12 File Structure 12 XUL Widgets 15 Introducing Input Controls 24 More Complex Containers 28 Managing the Display Space 35 Content Display Panels 38 Miscellaneous Widgets 39 Helper Features 40 Mozilla Style Declarations 42 Summary 44 3. Coding and Testing for the Real World . 45 Defining a Target Application 45 Adding Logic 47 Simple Authentication Script 59 When Things Don’t Work 68 Summary 73 v 4. Configuring for Chrome and a Server . 76 Chrome Overview 76 Running as a Local Installation 78 XUL-to-Server Communications 80 Serving XUL Files 100 Summary 107 5. Multiframe XUL . 109 Dividing the Display Area 109 Editing Documents 113 Adding Dialog Windows 138 Summary 152 6. Trees, Templates, and Datasources . 154 Trees 154 Modifying Datasources 188 Summary 220 7. DOM Manipulation and Input/Output . 221 A Design Review 221 Browser Elements 222 Moving Text Between Frames 227 Exporting Note Document Content 231 Adding Interactivity to DOM Elements 239 Summary 263 8. Graphics . 264 A Sample Graphing Project 265 XHTML Review 266 SVG Overview 267 Data-to-Graphics Transformation 272 HTML Canvas 292 Summary 299 9. Extending the Interface . 301 Overlay Files 301 Adding Logic 308 XBL 312 HTTP Request Widget 321 Summary 333 vi | Table of Contents 10. XForms . 334 Basic XForms Structure 335 An Example Transfer to the Server 336 XForms Validation Features 341 XForms Events and Actions 359 User Interaction and Dynamic Presentation 363 What to Do When Things Go Wrong 373 Summary 373 11. Installation and Deployment . 374 Deploying Standalone Applications 375 Deploying Themes and Skins 382 Adding Locales 386 Deploying Extensions 391 Deploying the Extension 395 What to Do When Things Go Wrong 400 Summary 403 12. XUL Widget Reference . 405 Browser Package Files 405 Developer Reference 410 Glossary: XUL Widgets: Attributes, Properties, and Methods . 463 Index . 479 Table of Contents | vii Preface1 A technology is only a tool. No matter how creative its design, innovative its approach, or impressive its perfor- mance, a technology is still defined (according to Webster) as “a manner of accom- plishing 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 com- monly 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 contri- bution of the open source community, a team of developers whose mission is to develop the best possible software for browsing the Internet (Firefox) and exchang- ing messages (Thunderbird). This community is also strongly committed to demon- strating 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 flexi- bility of interface design, and more expressive graphics. ix 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 interna- tional 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 innova- tive 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 (pro- nounced “zool”). A precious element of constant availability characterizes the web- centric 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, gen- erally speaking, are read for only a few minutes at a time. Such spontaneous access x | Preface 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 intro- duction to some of these evolving standards that allow developers to combine cre- ativity 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.