W3C

CSSOM View Module

Editor's Draft 24 April 2008

This Version:
http://www.w3.org/TR/2008/ED-cssom-view-20080424/
Latest Version:
http://www.w3.org/TR/cssom-view/
Previous Versions:
http://www.w3.org/TR/2008/WD-cssom-view-20080222/
http://www.w3.org/TR/2000/REC-DOM-Level-2-Views-20001113/
Editor:
Anne van Kesteren (Opera Software ASA) <annevk@opera.com>

Abstract

The APIs introduced by this specification provide authors with a way to inspect and manipulate the visual view of a document. This includes getting the position of element layout boxes, obtaining the width of the viewport through script, and also scrolling an element.

The CSSOM View Module also supercedes DOM Level 2 Views and therefore defines the AbstractView and DocumentView interfaces and extensions for them.

Status of this Document

This is a public copy of the editors' draft. It is provided for discussion only and may change at any moment. It probably contains errors. Its publication here does not imply endorsement of its contents by W3C.

Table of Contents

1. Background

Many of the features defined in this specification have been supported by browsers for a long period of time. The goal of this specification is to define these features in such a way that they can be implemented by all browsers in an identical way without differences between no quirks, limited quirks and no quirks mode, as defined by HTML 5. [HTML5]

As well as serving as a specification for that "de facto" standard this specification also obsoletes DOM Level 2 Views by taking the concepts defined in that specification, making them more concrete, and integrating them with the various "de facto" concepts.

Finally, the specification defines a couple of new features that the CSS WG considers to be useful for authors.

2. Conformance Criteria

Everything in this specification is normative except for diagrams, examples, notes and sections marked non-normative.

The key word must in this document is to be interpreted as described in RFC 2119. [RFC2119]

A conforming user agent implements all the requirements made by this specification.

2.1 Dependencies

This specification depends on several definitions and features defined in the HTML 5 specification. [HTML5]

This specification also depends on several features defined in the DOM Level 2 Events and DOM Level 3 Core specifications. [DOM2Events] [DOM3Core]

2.2 Terminology

The terms HTML element, view and default view are defined by HTML 5. [HTML5]

The HTML body element is the first body HTML element child of the root HTML element html.

The terms CSS pixel, content edge, padding edge, border edge, and canvas are defined by CSS 2.1. [CSS21]

The terms viewport, initial containing block are defined by CSS 2.1 unless there's an ancestor foreignObject element in the http://www.w3.org/2000/svg namespace in which case that element acts as viewport and initial containing block. [CSS21] [SVG]

The term content refers to the dimensions of the element's content area, including overflown content. [CSS21]

The term document content refers to the area on the canvas that is rendered upon, excluding content on negative axis.

3. DOM Features

This specification defines the DOM feature "Views" for versions "2.0" and "3.0". User agents supporting this specification must support that feature and versions. [DOM3Core]

Authors are encouraged to stay away from DOM features as user agents claiming to support the DOM feature might not support the feature you need and user agents supporting parts of the specification might not claim support for the DOM feature.

4. The AbstractView Interface

The AbstractView interface is a base interface that all views must derive from.

interface AbstractView {
  readonly attribute DocumentView document;
  readonly attribute Media media;
};

The document attribute, on getting, must return the object implementing the DocumentView interface that is associated with the view.

The media attribute, on getting, must return an object implementing the Media interface. It must always return the same object.

4.1 The Media Interface

interface Media {
  readonly attribute DOMString type;
  boolean matchMedium(in DOMString mediaquery);
};

The type attribute, on getting, must return the string that represents the media type of the current view used for rendering the Document. The media types are defined by CSS 2.1 (screen, print, et cetera). [CSS21]

The following snippet determines whether the page is rendered using the projection media type:

var isProjection = media.type == "projection"

The matchMedium(mediaquery) method, when invoked, must return the result of the following algorithm:

  1. If mediaquery can be parsed according to the rules given in the Media Queries specification and matches the state of the current view return true and terminate this algorithm. [MQ]

  2. Otherwise, return false.

The following snippet determines whether the device is a color device:

var isColorDevice = media.matchMedium("(color)")

5. The ScreenView Interface

An object implementing the ScreenView interface represents a view for visual media. If the default view is visual the defaultView attribute on the Document object must return an object implementing this interface.

interface ScreenView : AbstractView {
  // viewport
  readonly attribute long innerWidth;
  readonly attribute long innerHeight;
  readonly attribute long pageXOffset;
  readonly attribute long pageYOffset;
  void scroll(in long x, in long y);
  void scrollTo(in long x, in long y);
  void scrollBy(in long x, in long y);

  // client
  readonly attribute long screenX;
  readonly attribute long screenY;
  readonly attribute long outerWidth;
  readonly attribute long outerHeight;

  // output device
  readonly attribute Screen screen;
};

All coordinates and dimensions on the ScreenView interface are expressed in CSS pixels.

The innerWidth attribute, on getting, must return the viewport width.

The following snippet shows how to obtain the width of the viewport, including the rendered scrollbar:

var viewportWidth = innerWidth

The innerHeight attribute, on getting, must return the viewport height.

The pageXOffset attribute, on getting, must return the x-coordinate, relative to the initial containing block origin, of the left of the viewport.

The pageYOffset attribute, on getting, must return the y-coordinate, relative to the initial containing block origin, of the top of the viewport.

When the scroll(x,y) method is invoked, the user agent must run the following algorithm:

  1. If either x or y is infinite or NaN stop this algorithm.

  2. If document content can have overflow to the right
    1. Let x be min(x, content width - content edge width).

    2. If x is negative let it be zero.

    If document content can have overflow to the left (under right-to-left conditions)
    1. Let x be max(x, content edge width - content width).

    2. If x is non-negative let it be zero.

  3. Let y be min(y, document content height - viewport height).

  4. If y is negative let it be zero.

  5. Align the x-coordinate x of the document content with the left of the viewport and align the y-coordinate y of the document content with the top of the viewport.

When the scrollTo(x,y) method is invoked, the user agent must act as if the scroll() method was invoked with the same arguments.

When the scrollBy(x,y) method is invoked, the user agent must act as if the scroll() method was invoked with x plus pageXOffset as first argument and y plus pageYOffset as second argument.

The screenX attribute, on getting, must return the x-coordinate, relative to the origin of the screen of the output device, of the top of the client window as number of pixels, or zero if there is no such thing.

The screenY attribute, on getting, must return the y-coordinate, relative to the origin of the screen of the output device, of the left of the client window as number of pixels, or zero if there is no such thing.

The outerWidth attribute, on getting, must return the width of the client window as number of CSS pixels. If there is no client window this attribute must return zero.

The outerHeight attribute, on getting, must return the height of the client window as number of CSS pixels. If there is no client window this attribute must return zero.

The screen attribute, on getting, must return an object implementing the Screen interface. It must always return the same object.

5.1 The Screen Interface

As its name suggests, the Screen interface represents information about the screen of the output device.

interface Screen {
  readonly attribute unsigned long availWidth;
  readonly attribute unsigned long availHeight;
  readonly attribute unsigned long width;
  readonly attribute unsigned long height;
  readonly attribute unsigned long colorDepth;
  readonly attribute unsigned long pixelDepth;
};

All coordinates and dimensions on the Screen interface are expressed in CSS pixels.

The availWidth attribute, on getting, must return the available width of the rendering surface of the output device.

The availHeight attribute, on getting, must return the available height of the rendering surface of the output device.

The width attribute, on getting, must return the width of the output device.

The height attribute, on getting, must return the height of the output device.

The colorDepth and pixelDepth attributes must both return the number of bits allocated to colors in the output device. If the output device does not support colors these attributes must return zero.

Bits allocated to handling transparancy are not taken into account.

6. The DocumentView Interface

Objects implementing the Document interface, as defined in DOM Level 3 Core, must also implement the DocumentView interface. [DOM3Core]

interface DocumentView {
  readonly attribute AbstractView defaultView;
  Element elementFromPoint(in float x, in float y);
  Range caretRangeFromPoint(in float x, in float y);
};

The defaultView attribute, on getting, must return an object implementing the AbstractView interface that is the default view for this document. If there is no default view available this attribute must return null.

The elementFromPoint(x, y) method, when invoked, must return the element at coordinates x,y in the viewport. The element to be returned is determined through hit testing. If either argument is negative, x is greater than the viewport width, or y is greather than the viewport height, the method must return null. If there is no element at the given position the method must return the root element, if any, or null otherwise.

The caretRangeFromPoint(x, y) method, when invoked, must return an empty text range for the position where a text insertion point indicator would have been inserted if editing was enabled and hit testing was performed at the coordinates x,y in the viewport. If either argument is negative, x is greater than the viewport width, y is greather than the viewport height, no insertion point indicator would have been inserted, or editing is not supported, the method must return null. [DOM2TR]

The specifics of hit testing are out of scope of this specification and therefore the exact details of elementFromPoint() and caretRangeFromPoint() are too. Hit testing might be defined in a future revision of CSS or HTML 5.

7. The ElementView Interface

Objects implementing the Element interface, as defined by DOM Level 3 Core, must also implement the ElementView interface. [DOM3Core]

interface ElementView {
  ClientRectList getClientRects();
  ClientRect getBoundingClientRect();

           attribute long scrollTop;   // scroll on setting
           attribute long scrollLeft;  // scroll on setting
  readonly attribute long scrollWidth;
  readonly attribute long scrollHeight;

  readonly attribute long clientTop;
  readonly attribute long clientLeft;
  readonly attribute long clientWidth;
  readonly attribute long clientHeight;

};

7.1 The getClientRects() and getBoundingClientRect() methods

The getClientRects() and getBoundingClientRect methods provide information about the position of the border box edges of an element relative to the viewport. The objects these methods return must be static. That is, changes to the underlying document are not reflected in the objects.

The getClientRects() method, when invoked, must return the result of the following algorithm:

  1. If the element on which it was invoked does not have an associated CSS layout box return an empty ClientRectList object and stop this algorithm.

  2. If the element is in the http://www.w3.org/2000/svg namespace return a ClientRectList object containing a single ClientRect object that describes the bounding box of the element as defined by SVG specification. [SVG]

  3. Return a ClientRectList object containing a list of ClientRect objects in content order describing the border boxes (including those with a height or width of zero) with the following constraints:

The getBoundingClientRect() method, when invoked, must return the result of the following algorithm:

  1. Let list be the result of invoking getClientRects() on the same element this method was invoked on.

  2. If the list is empty return a ClientRect object whose top, right, bottom and left members are zero.

  3. Otherwise, return a ClientRect object describing the smallest rectangle that includes the first rectangle in list and all of the remaining rectangles of which the height or width is not zero.

The following snippet gets the dimensions of the first div element in a document:

var example = document.getElementsByTagName("div")[0].getBoundingClientRect();
var exampleWidth = example.width;
var exampleHeight = example.height;

7.2 The scrollTop, scrollLeft, scrollWidth, and scrollHeight attributes

The scrollTop attribute, when called on element A, must return the value that is the result of running the following algorithm:

  1. If A does not have an associated CSS layout box return zero and stop this algorithm.

  2. Return the y-coordinate of the content at the alignment point with the top of the content edge of A.

When setting the scrollTop attribute to y on element A user agents must run the following algorithm:

  1. If A does not have an associated CSS layout box or vertical overflow stop this algorithm.

  2. If y is infinite or NaN stop this algorithm.

  3. Let y be min(y, content height - content edge height).

  4. If y is negative let it be zero.

  5. Align content y-coordinate y with the top of the content edge of A.

The scrollLeft attribute, when called on element A, must return the value that is the result of running the following algorithm:

  1. If A does not have an associated CSS layout box return zero and stop this algorithm.

  2. Return the x-coordinate of the content at the alignment point with the left of the content edge of A.

When setting the scrollLeft attribute to x on element A user agents must run the following algorithm:

  1. If A does not have an associated CSS layout box or horizontal overflow stop this algorithm.

  2. If x is infinite or NaN stop this algorithm.

  3. If A can have overflow to the right
    1. Let x be min(x, content width - content edge width).

    2. If x is negative let it be zero.

    If A can have overflow to the left (under right-to-left conditions)
    1. Let x be max(x, content edge width - content width).

    2. If x is non-negative let it be zero.

  4. Align content x-coordinate x with the left of the content edge of A.

The scrollWidth attribute, when called on element A, must return value that is the result of the following algorithm:

  1. If A does not have an associated CSS layout box return zero and stop this algorithm.

  2. Return the computed value of the padding-left property of A, plus the computed value of the padding-right property of A, plus the content width of A.

The scrollHeight attribute, when called on element A, must return value that is the result of the following algorithm:

  1. If A does not have an associated CSS layout box return zero and stop this algorithm.

  2. Return the computed value of the padding-top property of A, plus the computed value of the padding-bottom property of A, plus the content height of A.

7.3 The clientTop, clientLeft, clientWidth, and clientHeight attributes

The clientTop, clientLeft, clientWidth, and clientHeight attributes, on getting, must return zero if the element does not have an associated CSS layout box or if the layout box is inline. Otherwise, these attributes must behave as defined in the remainder of this section.

On getting, the clientTop attribute returns the computed value of the border-top-width property plus the width of any scrollbar rendered between the top padding edge and the top border edge.

On getting, the clientLeft attribute returns the computed value of the border-left-width property plus the width of any scrollbar rendered between the left padding edge and the left border edge.

On getting, the clientWidth attribute returns the viewport width if the element is the root element and the width of the padding edge (excluding the width of any rendered scrollbar between the padding edge and the border edge) otherwise.

On getting, the clientHeight attribute returns the viewport height if the element is the root element and the height of the padding edge (excluding the width of any rendered scrollbar between the padding edge and the border edge) otherwise.

8. The HTMLElementView Interface

Objects implementing the HTMLElement interface, as defined by HTML 5, must also implement the HTMLElementView interface. [HTML5]

interface HTMLElementView {
  readonly attribute Element offsetParent;
  readonly attribute long offsetTop;
  readonly attribute long offsetLeft;
  readonly attribute long offsetWidth;
  readonly attribute long offsetHeight;
};

All coordinates and dimensions on the HTMLElementView interface are expressed in CSS pixels.

8.1 The offsetParent, offsetTop, offsetLeft, offsetWidth, and offsetHeight attributes

The offsetParent attribute, when called on element A, must return the element determined by the following algorithm:

  1. If any of the following holds true return null and stop this algorithm:

  2. If A is an area HTML element which has a map HTML element somewhere in the ancestor chain return the nearest ancestor map HTML element and stop this algorithm.

  3. Return the nearest ancestor element of A for which at least one of the following is true and stop this algorithm if such an ancestor is found:

  4. Return null.

The offsetTop attribute, when called on element A, must return the value that is the result of the following algorithm:

  1. If A is the HTML body element or does not have an associated CSS layout box return zero and stop this algorithm.

  2. If the offsetParent of A is null or the HTML body element return the y-coordinate of the top border edge of A and stop this algorithm.

  3. Return the result of subtracting the y-coordinate of the top padding edge of the offsetParent of A from the y-coordinate of the top border edge of A, relative to the initial containing block origin.

The offsetLeft attribute, when called on element A, must return the value that is the result of the following algorithm:

  1. If element A is the HTML body element or does not have an associated CSS layout box return zero and stop this algorithm.

  2. If the offsetParent of A is null or the HTML body element return the x-coordinate of the left border edge of A and stop this algorithm.

  3. Return the result of subtracting the x-coordinate of the left padding edge of the offsetParent of A from the x-coordinate of the left border edge of A, relative to the initial containing block origin.

The offsetWidth attribute, when called on element A, must return value that is the result of the following algorithm:

  1. If A does not have an associated CSS layout box return zero and stop this algorithm.

  2. Return the border edge width of A.

The offsetHeight attribute, when called on element A, must return value that is the result of the following algorithm:

  1. If A does not have an associated CSS layout box return zero and stop this algorithm.

  2. Return the border edge height of A.

9. The RangeView Interface

Objects implementing the Range interface, as defined by DOM Level 2 Traversal and Range, must also implement the RangeView interface. [DOM2TR]

interface RangeView {
  ClientRectList getClientRects();
  ClientRect getBoundingClientRect();
};

The objects the methods described below return must be static.

The getClientRects() method, when invoked, must return an empty ClientRectList object if the range is empty (if the boundary-points are identical) and otherwise a ClientRectList object containing a list of ClientRect objects in content order that matches the following constraints:

The getBoundingClientRect() method, when invoked, must return the result of the following algorithm:

  1. Let list be the result of invoking getClientRects() on the same range this method was invoked on.

  2. If list is empty return a ClientRect object whose top, right, bottom and left members are zero.

  3. Otherwise, return a ClientRect object describing the smallest rectangle that includes the first rectangle in list and all of the remaining rectangles of which the height or width is not zero.

10. The MouseEventView Interface

Objects implementing the MouseEvent interface, as defined by DOM Level 2 Events, must also implement the MouseEventView interface. In case of conflict with the MouseEvent interface the implementation of the conflicting members must be as prescribed by this specification. [DOM2Events]

interface MouseEventView {
  readonly attribute long screenX;
  readonly attribute long screenY;

  readonly attribute long pageX;
  readonly attribute long pageY;

  readonly attribute long clientX;
  readonly attribute long clientY;

  readonly attribute long offsetX;
  readonly attribute long offsetY;

  // historical; equivalent to clientX, clientY
  readonly attribute long x;
  readonly attribute long y;
};

All coordinates and dimensions on the MouseEventView interface are expressed in CSS pixels.

The screenX attribute, on getting, must return the x-coordinate of the position where the event occurred relative to the origin of the screen.

The screenY attribute, on getting, must return the y-coordinate of the position where the event occurred relative to the origin of the screen.

The pageX attribute, on getting, must return the horizontal coordinate of the position where the event occurred relative to the origin of the initial containing block.

The pageY attribute, on getting, must return the y-coordinate of the position where the event occurred relative to the origin of the initial containing block.

The clientX attribute, on getting, must return the x-coordinate of the position where the event occurred relative to the origin of the viewport.

The clientY attribute, on getting, must return the y-coordinate of the position where the event occurred relative to the origin of the viewport.

The offsetX attribute, on getting, must return the x-coordinate of the position where the event occurred relative to the origin of the padding edge of the target node.

The offsetY attribute, on getting, must return the y-coordinate of the position where the event occurred relative to the origin of the padding edge of the target node.

The x attribute, on getting, must return the value of clientX.

The y attribute, on getting, must return the value of clientY.

The x and y attributes are included for historical reasons.

11. Rectangles

11.1 The ClientRectList Interface

The ClientRectList object consists of an ordered list of ClientRect objects.

interface ClientRectList {
  readonly attribute unsigned long length;
  ClientRect item(in unsigned long index);
};

The length attribute must return the total number of ClientRect objects associated with the object.

The item(index) method, when invoked, must raise an INDEX_SIZE_ERR exception when index is negative or greater than the number of ClientRect objects associated with the object. Otherwise, the ClientRect object at index must be returned.

In ECMAScript implementations, objects that implement the ClientRectList interface must also have a [[Get]] method that, when invoked with a number, acts like the item() method would when invoked with that argument.

11.2 The ClientRect Interface

Objects implementing the ClientRect interface represent a rectangular box. The type of box is specified by the method that returns a ClientRect object.

interface ClientRect {
  readonly attribute float top;
  readonly attribute float right;
  readonly attribute float bottom;
  readonly attribute float left;
  readonly attribute float width;
  readonly attribute float height;
};

All coordinates and dimensions on the ClientRect interface are expressed in CSS pixels.

The top attribute, on getting, must return the y-coordinate, relative to the viewport origin, of the top of the rectangle box.

The right attribute, on getting, must return the x-coordinate, relative to the viewport origin, of the right of the rectangle box.

The bottom attribute, on getting, must return the y-coordinate, relative to the viewport origin, of the bottom of the rectangle box.

The left attribute, on getting, must return the x-coordinate, relative to the viewport origin, of the left of the rectangle box.

The width attribute, on getting, must return the width of the rectangle box.

This is identical to right minus left.

The height attribute, on getting, must return the height of the rectangle box.

This is identical to bottom minus top.

References

All references are normative.

[CSS21]
Cascading Style Sheets Level 2 Revision 1, B. Bos, T. Çelik, I. Hickson, H. Wium Lie, editors. W3C, June 2007.
[DOM2Events]
Document Object Model (DOM) Level 2 Events Specification, T. Pixley, editor. W3C, November 2000.
[DOM2TR]
Document Object Model (DOM) Level 2 Traversal and Range Specification, J. Kesselman, J. Robie, M. Champion, P. Sharpe, V. Apparao, L. Wood, editors. W3C, November 2000.
[DOM3Core]
Document Object Model (DOM) Level 3 Core Specification, A. Le Hors, P. Le Hégaret, L. Wood, G. Nicol, J. Robie, M. Champion, S. Byrne, editors. W3C, April 2004.
[HTML5]
HTML 5 (work in progress), I. Hickson, D. Hyatt, editors. W3C, 2008.
HTML 5 (work in progress), I. Hickson, editor. WHATWG, 2008.
[MQ]
Media Queries, H. Wium Lie T. Çelik, D. Glazman, editors. W3C, June 2007.
[RFC2119]
Key words for use in RFCs to Indicate Requirement Levels, S. Bradner. IETF, March 1997.
[SVG]
Scalable Vector Graphics (SVG) 1.1 Specification, J. Ferraiolo, 藤沢 淳, D. Jackson, editors. W3C, January 2003.

Acknowledgments

The editor would like to thank Alexey Feldgendler, Björn Höhrmann, David Vest, Garrett Smith, Hallvord R. M. Steen, Mike Wilson, Morten Stenshorne, Peter-Paul Koch, Robert O'Callahan, Simon Pieters, and Tarquin Wilton-Jones for their contributions to this document.

Special thanks to the Microsoft employees who first implemented many of the features specified in this draft, which were first widely deployed by the Windows Internet Explorer browser.