W3C

CSSOM View Module

W3C Working Draft 17 December 2013

This Version:
http://www.w3.org/TR/2013/WD-cssom-view-20131217/
Latest Version:
http://www.w3.org/TR/cssom-view/
Latest Editor Draft:
http://dev.w3.org/csswg/cssom-view/
Previous Version:
http://www.w3.org/TR/2011/WD-cssom-view-20110804/
Editors:
Simon Pieters (Opera Software ASA) <[email protected]>
Glenn Adams (Cox Communications, Inc.) <[email protected]>
Previous Editor:
Anne van Kesteren (Opera Software ASA) <[email protected]>

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.

Status of This Document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.

This is the 5 December 2013 W3C Working Draft of CSSOM View. Please send comments to [email protected] (archived) with [cssom-view] at the start of the subject line.

This document was produced by the CSS Working Group (part of the Style Activity).

This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

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 interoperable manner. The specification also defines a couple of new features that will hopefully be useful to authors. (If they are not you can bug us!)

2 Conformance

All diagrams, examples, and notes in this specification are non-normative, as are all sections explicitly marked non-normative. Everything else in this specification is normative.

The key words "MUST", "MUST NOT", "REQUIRED", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this document are to be interpreted as described in RFC2119. For readability, these words do not appear in all uppercase letters in this specification. [RFC2119]

Requirements phrased in the imperative as part of algorithms (such as "strip any leading space characters" or "return false and terminate these steps") are to be interpreted with the meaning of the key word ("must", "should", "may", etc) used in introducing the algorithm.

Conformance requirements phrased as algorithms or specific steps may be implemented in any manner, so long as the end result is equivalent. (In particular, the algorithms defined in this specification are intended to be easy to follow, and not intended to be performant.)

User agents may impose implementation-specific limits on otherwise unconstrained inputs, e.g. to prevent denial of service attacks, to guard against running out of memory, or to work around platform-specific limitations.

When a method or an attribute is said to call another method or attribute, the user agent must invoke its internal API for that attribute or method so that e.g. the author can't change the behavior by overriding attributes or methods with custom properties or functions in ECMAScript.

Unless otherwise stated, string comparisons are done in a case-sensitive manner.

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

2.1 Web IDL

The IDL fragments in this specification must be interpreted as required for conforming IDL fragments, as described in the Web IDL specification. [WEBIDL]

3 Terminology

Terminology used in this specification is from DOM, CSSOM and HTML. [DOM] [CSSOM] [HTML]

MouseEvent and MouseEventInit are defined in …

DOMPoint, DOMPointReadOnly, DOMPointInit, DOMRect, DOMRectReadOnly, DOMRectList and DOMQuad are defined in Geometry Interfaces Module. [GEOMETRY]

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

Content edge, padding edge, border edge, margin edge and canvas are defined by CSS.

Viewport and initial containing block are defined by CSS 2.1.

Block flow direction and inline base direction are defined in CSS Writing Modes Module. [CSSWRITINGMODES]

Elements and viewports have an associated scrolling box if has a scrolling mechanism or it overflows its content area and the used value of the 'overflow-x' or 'overflow-y' property is 'hidden'. [CSSBOX]

A scrolling box of a viewport or element has two overflow directions, depending on the viewport's or element's block flow direction and inline base direction, as follows:

If the block flow direction is top-to-bottom and the inline base direction is left-to-right
If the block flow direction is left-to-right and the inline base direction is left-to-right

Rightward and downward.

If the block flow direction is top-to-bottom and the inline base direction is right-to-left
If the block flow direction is right-to-left and the inline base direction is left-to-right

Leftward and downward.

If the block flow direction is right-to-left and the inline base direction is right-to-left

Leftward and upward.

If the block flow direction is left-to-right and the inline base direction is right-to-left

Righward and upward.

The term scrolling area refers to a box of a viewport or an element that has the following edges, depending on the viewport's or element's scrolling box's overflow directions.

If the overflow directions are… For a viewport For an element
rightward and downward
top edge

The top edge of the initial containing block.

right edge

The right-most edge of the right edge of the initial containing block and the right margin edge of the viewport's all descendant boxes.

bottom edge

The bottom-most edge of the bottom edge of the initial containing block and the bottom margin edge of the viewport's all descendant boxes.

left edge

The left edge of the initial containing block.

top edge

The element's top padding edge.

right edge

The right-most edge of the element's right padding edge and the right margin edge of the element's all descendant boxes, excluding boxes that have an ancestor of the element as their containing block.

bottom edge

The bottom-most edge of the element's bottom padding edge and the bottom margin edge of the element's all descendant boxes, excluding boxes that have an ancestor of the element as their containing block.

left edge

The element's left padding edge.

leftward and downward
top edge

The top edge of the initial containing block.

right edge

The right edge of the initial containing block.

bottom edge

The bottom-most edge of the bottom edge of the initial containing block and the bottom margin edge of the viewport's all descendant boxes.

left edge

The left-most edge of the left edge of the initial containing block and the left margin edge of the viewport's all descendant boxes.

top edge

The element's top padding edge.

right edge

The element's right padding edge.

bottom edge

The bottom-most edge of the element's bottom padding edge and the bottom margin edge of the element's all descendant boxes, excluding boxes that have an ancestor of the element as their containing block.

left edge

The left-most edge of the element's left padding edge and the left margin edge of the element's all descendant boxes, excluding boxes that have an ancestor of the element as their containing block.

leftward and upward
top edge

The top-most edge of the top edge of the initial containing block and the top margin edge of the viewport's all descendant boxes.

right edge

The right edge of the initial containing block.

bottom edge

The bottom edge of the initial containing block.

left edge

The left-most edge of the left edge of the initial containing block and the left margin edge of the viewport's all descendant boxes.

top edge

The top-most edge of the element's top padding edge and the top margin edge of the element's all descendant boxes, excluding boxes that have an ancestor of the element as their containing block.

right edge

The element's right padding edge.

bottom edge

The element's bottom padding edge.

left edge

The left-most edge of the element's left padding edge and the left margin edge of the element's all descendant boxes, excluding boxes that have an ancestor of the element as their containing block.

rightward and upward
top edge

The top-most edge of the top edge of the initial containing block and the top margin edge of the viewport's all descendant boxes.

right edge

The right-most edge of the right edge of the initial containing block and the right margin edge of the viewport's all descendant boxes.

bottom edge

The bottom edge of the initial containing block.

left edge

The left edge of the initial containing block.

top edge

The top-most edge of the element's top padding edge and the top margin edge of the element's all descendant boxes, excluding boxes that have an ancestor of the element as their containing block.

right edge

The right-most edge of the element's right padding edge and the right margin edge of the element's all descendant boxes, excluding boxes that have an ancestor of the element as their containing block.

bottom edge

The element's bottom padding edge.

left edge

The element's left padding edge.

The beginning edges of a particular set of edges of a box or element are the following edges:

If the overflow directions are rightward and downward

The top and left edges.

If the overflow directions are leftward and downward

The top and right edges.

If the overflow directions are leftward and upward

The bottom and right edges.

If the overflow directions are rightward and upward

The bottom and left edges.

The ending edges of a particular set of edges of a box or element are the following edges:

If the overflow directions are rightward and downward

The bottom and right edges.

If the overflow directions are leftward and downward

The bottom and left edges.

If the overflow directions are leftward and upward

The top and left edges.

If the overflow directions are rightward and upward

The top and right edges.

The term CSS layout box refers to the same term in CSS. For the purpose of the requirements in this specification, elements that have a computed value of the 'display' property that is 'table-column' or 'table-column-group' must be considered to have an associated CSS layout box (the column or column group, respectively).

The term SVG layout box refers to the same term in SVG.

The terms CSS layout box and SVG layout box are not currently defined by CSS or SVG.

The term layout box refers to either a CSS layout box or an SVG layout box.

The term transforms refers to SVG transforms and CSS transforms. [SVG] [CSSTRANSFORMS]

3.1 CSS pixels

All coordinates and dimensions for the APIs defined in this specification are in CSS pixels, unless otherwise specified.

This does not apply to e.g. matchMedia() as the units are explicitly given there.

3.2 Zooming

There are two kinds of zoom, page zoom which affects the size of the initial viewport, and pinch zoom which acts like a magnifying glass and does not affect the initial viewport or actual viewport. [CSSDEVICEADAPT]

4 Common Infrastructure

4.1 Scrolling

When a user agent is to perform a scroll of a scrolling box box, to a given position position, with a set of steps task, an associated element element and optionally a scroll behavior behavior (which is auto if omitted), the following steps must be run:

  1. Abort any ongoing smooth scroll for box.

  2. If the user agent honors the 'scroll-behavior' property and one of the following are true:

    ...then follow these substeps:

    1. Perform a smooth scroll of box to position.

    2. Queue a task to run task, unless a task to run task is in the queue.

    3. Repeat the previous step in a user-agent-defined interval while the smooth scroll is ongoing, as well as when the smooth scroll is completed (but not if it is aborted).

  3. Otherwise, follow these substeps:

    1. Perform an instant scroll of box to position.

    2. Queue a task to run task, unless a task to run task is in the queue.

When a user agent is to perform a smooth scroll of a scrolling box box to position, it must update the scroll position of box in a user-agent-defined fashion over a user-agent-defined amount of time. When the scroll is completed, the scroll position of box must be position. The scroll can also be aborted, either by an algorithm or by the user.

When a user agent is to perform an instant scroll of a scrolling box box to position, it must update the scroll position of box to position.

To scroll to the beginning of the document for a document document, follow these steps:

  1. Let viewport be the viewport that is associated with document.

  2. Let position be the the scroll position viewport would have by aligning the beginning edges of the scrolling area with the beginning edges of viewport.

  3. If position is the same as viewport's current scroll position, and viewport does not have an ongoing smooth scroll, abort these steps.

  4. Let task be these steps:

    1. Fire an event named scroll that bubbles at document.

  5. Perform a scroll of viewport to position, with the set of steps task, and document's root element as the associated element, if there is one, or null otherwise.

This algorithm is used when navigating to the #top fragment identifier, as defined in HTML. [HTML]

5 Extensions to the Window Interface

enum ScrollBehavior { "auto", "instant", "smooth" };

dictionary ScrollOptions {
  ScrollBehavior behavior = "auto";
};

partial interface Window {
  MediaQueryList matchMedia(DOMString query);
  [SameObject] readonly attribute Screen screen;

  // browsing context
  void moveTo(double x, double y);
  void moveBy(double x, double y);
  void resizeTo(double x, double y);
  void resizeBy(double x, double y);

  // viewport
  readonly attribute double innerWidth;
  readonly attribute double innerHeight;

  // viewport scrolling
  readonly attribute double scrollX;
  readonly attribute double pageXOffset;
  readonly attribute double scrollY;
  readonly attribute double pageYOffset;
  void scroll(double x, double y, optional ScrollOptions options);
  void scrollTo(double x, double y, optional ScrollOptions options);
  void scrollBy(double x, double y, optional ScrollOptions options);

  // client
  readonly attribute double screenX;
  readonly attribute double screenY;
  readonly attribute double outerWidth;
  readonly attribute double outerHeight;
  readonly attribute double devicePixelRatio;
};

When the matchMedia(query) method is invoked these steps must be run:

  1. Let parsed media query list be the result of parsing query.

  2. Return a new MediaQueryList object, associated with the Window object, with parsed media query list as its associated media query list.

The screen attribute must return the Screen object associated with the Window object.

Accessing screen through a WindowProxy object might yield different results when the Document is navigated.

The moveTo(x, y) method must follow these steps:

  1. Optionally, terminate these steps.

  2. Let target be the browsing context of the context object.

  3. Let source be the responsible browsing context of the incumbent settings object.

  4. If source is not allowed to resize and move target, terminate these steps.

  5. Optionally, clamp x and y in a user-agent-defined manner so that the window does not move outside the available space.

  6. Move target's window such that the window's top left corner is at coordinates (x, y) relative to the top left corner of the output device, measured in CSS pixels of target. The positive axes are rightward and downward.

The moveBy(x, y) method must follow these steps:

  1. Optionally, terminate these steps.

  2. Let target be the browsing context of the context object.

  3. Let source be the responsible browsing context of the incumbent settings object.

  4. If source is not allowed to resize and move target, terminate these steps.

  5. Optionally, clamp x and y in a user-agent-defined manner so that the window does not move outside the available space.

  6. Move target's window x CSS pixels of target righward and y CSS pixels of target downward.

The resizeTo(x, y) method must follow these steps:

  1. Optionally, terminate these steps.

  2. Let target be the browsing context of the context object.

  3. Let source be the responsible browsing context of the incumbent settings object.

  4. If source is not allowed to resize and move target, terminate these steps.

  5. Optionally, clamp x and y in a user-agent-defined manner so that the window does not get too small or bigger than the available space.

  6. Resize target's window by moving its right and bottom edges such that the distance between the left and right edges of the viewport are x CSS pixels of target and the distance between the top and bottom edges of the viewport are y CSS pixels of target.

  7. Optionally, move target's window in a user-agent-defined manner so that it does not grow outside the available space.

The resizeBy(x, y) method must follow these steps:

  1. Optionally, terminate these steps.

  2. Let target be the browsing context of the context object.

  3. Let source be the responsible browsing context of the incumbent settings object.

  4. If source is not allowed to resize and move target, terminate these steps.

  5. Optionally, clamp x and y in a user-agent-defined manner so that the window does not get too small or bigger than the available space.

  6. Resize target's window by moving its right edge x CSS pixels of target righward and its bottom edge y CSS pixels of target downward.

  7. Optionally, move target's window in a user-agent-defined manner so that it does not grow outside the available space.

A browsing context A is allowed to resize and move a browsing context B if all the following conditions are met:

The innerWidth attribute must return the viewport width including the size of a rendered scroll bar (if any), or zero if there is no viewport.

The following snippet shows how to obtain the width of the viewport:

var viewportWidth = innerWidth

The innerHeight attribute must return the viewport height including the size of a rendered scroll bar (if any), or zero if there is no viewport.

The scrollX attribute attribute must return the x-coordinate, relative to the initial containing block origin, of the left of the viewport, or zero if there is no viewport.

The pageXOffset attribute must return the value returned by the scrollX attribute.

The scrollY attribute attribute must return the y-coordinate, relative to the initial containing block origin, of the top of the viewport, or zero if there is no viewport.

The pageYOffset attribute must return the value returned by the scrollY attribute.

When the scroll(x, y, options) method is invoked these steps must be run:

  1. If there is no viewport, abort these steps.

  2. Let viewport width be the width of the viewport excluding the width of the scroll bar, if any.

  3. Let viewport height be the height of the viewport excluding the height of the scroll bar, if any.

  4. If the viewport has rightward overflow direction

    Let x be max(0, min(x, viewport scrolling area width - viewport width)).

    If the viewport has leftward overflow direction

    Let x be min(0, max(x, viewport width - viewport scrolling area width)).

  5. If the viewport has downward overflow direction

    Let y be max(0, min(y, viewport scrolling area height - viewport height)).

    If the viewport has upward overflow direction

    Let y be min(0, max(y, viewport height - viewport scrolling area height)).

  6. Let position be the scroll position the viewport would have by aligning the x-coordinate x of the viewport scrolling area with the left of the viewport and aligning the y-coordinate y of the viewport scrolling area with the top of the viewport.

  7. If position is the same as the viewport's current scroll position, and the viewport does not have an ongoing smooth scroll, abort these steps.

  8. Let task be these steps:

    1. Fire an event named scroll that bubbles at the Document object.

  9. Perform a scroll of the viewport to position, with the set of steps task, and the Document's root element as the associated element, if there is one, or null otherwise, and the scroll behavior being the value of the behavior dictionary member of options.

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

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

The screenX attribute must return the x-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 screenY attribute must return the y-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 outerWidth attribute must return the width of the client window. If there is no client window this attribute must return zero.

The outerHeight attribute must return the height of the client window. If there is no client window this attribute must return zero.

The devicePixelRatio attribute must return the result of the following algorithm:

  1. If there is no output device, return 1 and abort these steps.

  2. Let CSS pixel size be the size of a CSS pixel at the current page zoom scale factor and at a pinch zoom scale factor of 1.0.

  3. Let device pixel size be the size of a device pixel of the output device, in one dimension (vertical or horizontal). If the device pixel is not square, then use the smaller of the vertical or horizontal size of the smallest bounding box of the device pixel.

  4. Return the result of dividing CSS pixel size by device pixel size.

5.1 The features argument to the open() method

HTML defines the open() method but has no defined effect for the third argument, features. [HTML]

This specification defines the effect of the features argument for user agents that do not opt to ignore it, as follows:

  1. If the method does not result in a new auxiliary browsing context being created, terminate these steps.

  2. Let target be the new auxiliary browsing context.

  3. Let tokens be the result of splitting features on commas.

  4. Let parsed features be a new empty dictionary.

  5. Token loop: For each token token in tokens, follow these substeps:

    1. Let input be token.

    2. Let position point at the first character of input.

    3. Skip whitespace.

    4. Collect a sequence of characters that are not space characters nor "=" (U+003D). Let name be the collected characters, converted to ASCII lowercase.

    5. If name is in parsed features or if name is not a supported open() feature name, continue token loop.

    6. Skip whitespace.

    7. If the character at position is not "=" (U+003D), continue token loop.

    8. Advance position by one.

    9. If position is past the end of input, continue token loop.

    10. Collect a sequence of characters that are any characters. Let raw value be the collected characters.

    11. Let value be the result of invoking the rules for parsing integers on raw value.

    12. If value is an error, continue token loop.

    13. Set name in parsed features to value.

  6. If left is present in parsed features, follow these substeps:

    1. Let x be the value of left.

    2. Optionally, clamp x in a user-agent-defined manner so that the window does not move outside the available space.

    3. Optionally, move target's window such that the window's left edge is at the horizontal coordinate x relative to the left edge of the output device, measured in CSS pixels of target. The positive axis is rightward.

  7. If top is present in parsed features, follow these substeps:

    1. Let y be the value of top.

    2. Optionally, clamp y in a user-agent-defined manner so that the window does not move outside the available space.

    3. Optionally, move target's window such that the window's top edge is at the vertical coordinate y relative to the top edge of the output device, measured in CSS pixels of target. The positive axis is downward.

  8. If width is present in parsed features, follow these substeps:

    1. Let x be the value of width.

    2. Optionally, clamp x in a user-agent-defined manner so that the window does not get too small or bigger than the available space.

    3. Optionally, size target's window by moving its right edge such that the distance between the left and right edges of the viewport are x CSS pixels of target.

    4. Optionally, move target's window in a user-agent-defined manner so that it does not grow outside the available space.

  9. If height is present in parsed features, follow these substeps:

    1. Let y be the value of height.

    2. Optionally, clamp y in a user-agent-defined manner so that the window does not get too small or bigger than the available space.

    3. Optionally, size target's window by moving its bottom edge such that the distance between the top and bottom edges of the viewport are y CSS pixels of target.

    4. Optionally, move target's window in a user-agent-defined manner so that it does not grow outside the available space.

A supported open() feature name is one of the following:

width

The width of the viewport.

height

The height of the viewport.

left

The left position of the window.

top

The top position of the window.

5.2 The MediaQueryList Interface

A MediaQueryList object has an associated media query list set on creation and an associated list of media query list listeners, which is initially empty.

If the associated media query list changes in evaluation then, for each listener in the list of media query list listeners — in appending order, queue a task that invokes the listener, passing as argument the MediaQueryList object.

A simple piece of code that detects changes in the orientation of the viewport can be written as follows:

function handleOrientationChange(mql) {
    if(mql.matches) // landscape
      …
    else
      …
  }
  var mql = matchMedia("(orientation:landscape)")
  mql.addListener(handleOrientationChange)
interface MediaQueryList {
  readonly attribute DOMString media;
  readonly attribute boolean matches;
  void addListener(MediaQueryListListener listener);
  void removeListener(MediaQueryListListener listener);
};

callback MediaQueryListListener = void (MediaQueryList list);

The media attribute must return the serialized form of the associated media query list.

The matches attribute must return true if the associated media query list matches the state of the rendered Document and false if it does not.

When the addListener(listener) method is invoked listener must be appended to the list of media query list listeners, unless it is already in the list of media query list listeners.

When the removeListener(listener) method is invoked listener must be removed from the list of media query list listeners.

5.3 The Screen Interface

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

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

The availWidth attribute must return the available width of the rendering surface of the output device, in CSS pixels.

The availHeight attribute must return the available height of the rendering surface of the output device, in CSS pixels.

The width attribute must return the width of the output device, in CSS pixels.

The height attribute must return the height of the output device, in CSS pixels.

The colorDepth attribute must return 24.

The pixelDepth attribute must return 24.

The colorDepth and pixelDepth attributes are useless but are included for compatibility.

6 Extensions to the Document Interface

partial interface Document {
  Element? elementFromPoint(double x, double y);
  sequence<Element> elementsFromPoint(double x, double y);
  CaretPosition? caretPositionFromPoint(double x, double y);
};

The elementFromPoint(x, y) method must follow these steps:

  1. If either argument is negative, x is greater than the viewport width excluding the size of a rendered scroll bar (if any), or y is greater than the viewport height excluding the size of a rendered scroll bar (if any), or there is no viewport associated with the document, return null and terminate these steps.

  2. If there is a layout box in the viewport that would be a target for hit testing at coordinates x,y, when applying the transforms that apply to the descendants of the viewport, return the associated element and terminate these steps.

  3. If the document has a root element, return the root element and terminate these steps.

  4. Return null.

The elementFromPoint() method does not necessarily return the top-most painted element. For instance, an element can be excluded from being a target for hit testing by using the 'pointer-events' CSS property.

The elementsFromPoint(x, y) method must follow these steps:

  1. Let sequence be a new empty sequence.

  2. If either argument is negative, x is greater than the viewport width excluding the size of a rendered scroll bar (if any), or y is greater than the viewport height excluding the size of a rendered scroll bar (if any), or there is no viewport associated with the document, return sequence and terminate these steps.

  3. For each layout box in the viewport, in paint order, starting with the topmost box, that would be a target for hit testing at coordinates x,y even if nothing would be overlapping it, when applying the transforms that apply to the descendants of the viewport, append the associated element to sequence.

  4. If the document has a root element, and the last item in sequence is not the root element, append the root element to sequence.

  5. Return sequence.

The caretPositionFromPoint(x, y) method must return the result of running these steps:

  1. If there is no viewport associated with the document, return null.

  2. If either argument is negative, x is greater than the viewport width excluding the size of a rendered scroll bar (if any), y is greather than the viewport height excluding the size of a rendered scroll bar (if any) return null.

  3. If at the coordinates x,y in the viewport no text insertion point indicator would have been inserted when applying the transforms that apply to the descendants of the viewport, return null.

  4. If at the coordinates x,y in the viewport a text insertion point indicator would have been inserted in a text entry widget which is also a replaced element, when applying the transforms that apply to the descendants of the viewport, return a caret position with its properties set as follows:

    caret node

    The node corresponding to the text entry widget.

    caret offset

    The amount of 16-bit units to the left of where the text insertion point indicator would have inserted.

    caret range

    null

  5. Otherwise, return a caret position where the caret range is a collapsed Range object for the position where the text insertion point indicator would have been inserted when applying the transforms that apply to the descendants of the viewport, and the other properties are set as follows:

    caret node

    The startContainer of the caret range.

    caret offset

    The startOffset of the caret range.

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

6.1 The CaretPosition Interface

A caret position gives the position of a text insertion point indicator. It always has an associated caret node, caret offset, and caret range. It is represented by a CaretPosition object.

interface CaretPosition {
  readonly attribute Node offsetNode;
  readonly attribute unsigned long offset;
  DOMRect? getClientRect();
};

The offsetNode attribute must return the caret node.

The offset attribute must return the caret offset.

The getClientRect() method must follow these steps, aborting on the first step that returns a value:

  1. If caret range is not null:

    1. Let list be the result of invoking the getClientRects() method on the range.

    2. If list is empty, return null.

    3. Return the DOMRect object in list at index 0.

  2. If caret node is a text entry widget that is a replaced element, and that is in the document, return a DOMRect object for the caret in the widget as represented by the caret offset value. The transforms that apply to the element and its ancestors are applied.

  3. Return null.

7 Extensions to the Element Interface

dictionary ScrollOptionsHorizontal : ScrollOptions {
  double x;
};
dictionary ScrollOptionsVertical : ScrollOptions  {
  double y;
};

partial interface Element {
  DOMRectList getClientRects();
  DOMRect getBoundingClientRect();
  void scrollIntoView();
  void scrollIntoView(boolean top, optional ScrollOptions options);
  attribute (double or ScrollOptionsVertical) scrollTop;
  attribute (double or ScrollOptionsHorizontal) scrollLeft;
  readonly attribute double scrollWidth;
  readonly attribute double scrollHeight;
  readonly attribute double clientTop;
  readonly attribute double clientLeft;
  readonly attribute double clientWidth;
  readonly attribute double clientHeight;
};

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 layout box return an empty DOMRectList object and stop this algorithm.

  2. If the element has an associated SVG layout box return a DOMRectList object containing a single DOMRect object that describes the bounding box of the element as defined by the SVG specification, applying the transforms that apply to the element and its ancestors.

  3. Return a DOMRectList object containing a list of DOMRect objects in content order describing the bounding 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 DOMRect object whose x, y, width and height members are zero.

  3. Otherwise, return a DOMRect 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;

The scrollIntoView(top, options) method must run these steps:

  1. If invoked with no arguments, let top be true and let options be a ScrollOptions dictionary with its members not present or present with the default value, as appropriate.

  2. If the element does not have any associated layout box terminate these steps.

  3. Scroll the element into view with the align to top flag set if top is true, and the scroll behavior being the value of the behavior dictionary member of options.

  4. Optionally perform some other action that brings the element to the user's attention.

The scrollTop attribute, on getting, must return the result of running these steps:

  1. If the element is the root element and the Document is in quirks mode, return zero and terminate these steps.

  2. If the element is the root element return the value of scrollY.

  3. If the element is the HTML body element, the Document is in quirks mode, and the element does not have an associated scrolling box, return the value of scrollY.

  4. If the element does not have any associated CSS layout box, return zero and terminate these steps.

  5. Return the y-coordinate of the scrolling area at the alignment point with the top of the padding edge of the element.

When setting the scrollTop attribute these steps must be run:

  1. If the given value is a ScrollOptionsVertical object, and the y dictionary member is not present, abort these steps.

  2. If the given value is a ScrollOptionsVertical object, let y be the value of the y dictionary member. Otherwise, let y be the given value.

  3. If the element is the root element and the Document is in quirks mode, terminate these steps.

  4. If the element is the root element invoke scroll() with scrollX as first argument and y as second, and, if the given value is a ScrollOptionsVertical object, the given value as the third argument.

  5. If the element is the HTML body element, the Document is in quirks mode, and the element does not have an associated scrolling box, invoke scroll() with scrollX as first argument and y as second, and, if the given value is a ScrollOptionsVertical object, the given value as the third argument, and terminate these steps.

  6. If the element does not have any associated CSS layout box, the element has no associated scrolling box, or the element has no overflow, terminate these steps.

  7. Scroll the element to scrollLeft,y, with the scroll behavior being the value of the behavior dictionary member if the given value is a ScrollOptionsVertical object, or auto otherwise.

The scrollLeft attribute, on getting, must return the result of running these steps:

  1. If the element is the root element and the Document is in quirks mode, return zero and terminate these steps.

  2. If the element is the root element return the value of scrollX.

  3. If the element is the HTML body element, the Document is in quirks mode, and the element does not have an associated scrolling box, return the value of scrollX.

  4. If the element does not have any associated CSS layout box, return zero and terminate these steps.

  5. Return the x-coordinate of the scrolling area at the alignment point with the left of the padding edge of the element.

When setting the scrollLeft attribute these steps must be run:

  1. If the given value is a ScrollOptionsHorizontal object, and the x dictionary member is not present, abort these steps.

  2. If the given value is a ScrollOptionsHorizontal object, let x be the value of the x dictionary member. Otherwise, let x be the given value.

  3. If the element is the root element and the Document is in quirks mode, terminate these steps.

  4. If the element is the root element invoke scroll() with x as first argument and scrollY as second, and, if the given value is a ScrollOptionsHorizontal object, the given value as the third argument.

  5. If the element is the HTML body element, the Document is in quirks mode, and the element does not have an associated scrolling box, invoke scroll() with x as first argument and scrollY as second, and, if the given value is a ScrollOptionsHorizontal object, the given value as the third argument, and terminate these steps.

  6. If the element does not have any associated CSS layout box, the element has no associated scrolling box, or the element has no overflow, terminate these steps.

  7. Scroll the element to x,scrollTop, with the scroll behavior being the value of the behavior dictionary member if the given value is a ScrollOptionsHorizontal object, or auto otherwise.

The scrollWidth attribute must return the result of running these steps:

  1. Let viewport width be the width of the viewport excluding the width of the scroll bar, if any, or zero if there is no viewport.

  2. If the element is the root element and the Document is not in quirks mode return max(viewport scrolling area width, viewport width).

  3. If the element is the HTML body element, the Document is in quirks mode and the element has no associated scrolling box, return max(viewport scrolling area width, viewport width).

  4. If the element does not have any associated CSS layout box return zero and terminate these steps.

  5. Return the width of the element's scrolling area.

The scrollHeight attribute must return the result of running these steps:

  1. Let viewport height be the height of the viewport excluding the height of the scroll bar, if any, or zero if there is no viewport.

  2. If the element is the root element and the Document is not in quirks mode return max(viewport scrolling area height, viewport height).

  3. If the element is the HTML body element, the Document is in quirks mode and the element has no associated scrolling box, return max(viewport scrolling area height, viewport height).

  4. If the element does not have any associated CSS layout box return zero and terminate these steps.

  5. Return the height of the element's scrolling area.

The clientTop attribute must run these steps:

  1. If the element has no associated CSS layout box or if the CSS layout box is inline, return zero.

  2. Return the computed value of the 'border-top-width' property plus the height of any scrollbar rendered between the top padding edge and the top border edge, ignoring any transforms that apply to the element and its ancestors.

The clientLeft attribute must run these steps:

  1. If the element has no associated CSS layout box or if the CSS layout box is inline, return zero.

  2. Return 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, ignoring any transforms that apply to the element and its ancestors.

The clientWidth attribute must run these steps:

  1. If the element has no associated CSS layout box or if the CSS layout box is inline, return zero.

  2. If the element is the root element and the element's document is not in quirks mode, or if the element is the HTML body element and the element's document is in quirks mode, return the viewport width excluding the size of a rendered scroll bar (if any).

  3. Return the width of the padding edge excluding the width of any rendered scrollbar between the padding edge and the border edge, ignoring any transforms that apply to the element and its ancestors.

The clientHeight attribute must run these steps:

  1. If the element has no associated CSS layout box or if the CSS layout box is inline, return zero.

  2. If the element is the root element and the element's document is not in quirks mode, or if the element is the HTML body element and the element's document is in quirks mode, return the viewport height excluding the size of a rendered scroll bar (if any).

  3. Return the height of the padding edge excluding the height of any rendered scrollbar between the padding edge and the border edge, ignoring any transforms that apply to the element and its ancestors.

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.

7.2 Element Scrolling Members

To scroll an element into view element, optionally with an align to top flag set, and optionally with a scroll behavior behavior (which is auto if omitted), means to run these steps for each ancestor element or viewport that establishes a scrolling box scrolling box, in order of innermost to outermost scrolling box:

  1. If the Document associated with element is not same origin with the Document associated with the element or viewport associated with box, terminate these steps.

  2. Let element bounding border box be the box that the return value of invoking getBoundingClientRect() on element represents.

  3. Let scrolling box edge A be the beginning edge in the block flow direction of scrolling box, and let element edge A be element bounding border box's edge on the same physical side as that of scrolling box edge A.

  4. Let scrolling box edge B be the ending edge in the block flow direction of scrolling box, and let element edge B be element bounding border box's edge on the same physical side as that of scrolling box edge B.

  5. Let scrolling box edge C be the beginning edge in the inline base direction of scrolling box, and let element edge C be element bounding border box's edge on the same physical side as that of scrolling box edge C.

  6. Let scrolling box edge D be the ending edge in the inline base direction of scrolling box, and let element edge D be element bounding border box's edge on the same physical side as that of box edge D.

  7. Let element width be the distance between element edge C and element edge D.

  8. Let scrolling box width be the distance between scrolling box edge C and scrolling box edge D.

  9. Let position be the scroll position scrolling box would have by following these steps:

    1. If the align to top flag is set align element edge A with scrolling box edge A.

    2. Otherwise, the align to top flag is not set; align element edge B with scrolling box edge B.

    3. If element edge C and element edge D are both outside scrolling box edge C and scrolling box edge D

      Do nothing.

      If element edge C is outside scrolling box edge C and element width is less than scrolling box width
      If element edge D is outside scrolling box edge D and element width is greater than scrolling box width

      Align element edge C with scrolling box edge C.

      If element edge C is outside scrolling box edge C and element width is greater than scrolling box width
      If element edge D is outside scrolling box edge D and element width is less than scrolling box width

      Align element edge D with scrolling box edge D.

  10. If position is the same as scrolling box's current scroll position, and scrolling box does not have an ongoing smooth scroll, abort these steps.

  11. If scrolling box is associated with an element

    Let associated element be the element.

    Let task be these steps:

    1. Fire an event named scroll at associated element.

    If scrolling box is associated with a viewport

    Let associated element be the Document's root element, if there is one, or null otherwise.

    Let task be these steps:

    1. Fire an event named scroll that bubbles at the Document object associated with the viewport.

  12. Perform a scroll of scrolling box to position, with the set of steps task, associated element as the associated element and behavior as the scroll behavior.

To scroll an element element to x,y optionally with a scroll behavior behavior (which is auto if omitted) means to:

  1. Let box be element's associated scrolling box.

  2. If box has rightward overflow direction

    Let x be max(0, min(x, element scrolling area width - element padding edge width)).

    If box has leftward overflow direction

    Let x be min(0, max(x, element padding edge width - element scrolling area width)).

  3. If box has downward overflow direction

    Let y be max(0, min(y, element scrolling area height - element padding edge height)).

    If box has upward overflow direction

    Let y be min(0, max(y, element padding edge height - element scrolling area height)).

  4. Let position be the scroll position box would have by aligning scrolling area x-coordinate x with the left of box and aligning scrolling area y-coordinate y with the top of box.

  5. If position is the same as box's current scroll position, and box does not have an ongoing smooth scroll, abort these steps.

  6. Let task be these steps:

    1. Fire an event named scroll at the element.

  7. Perform a scroll of box to position, with the set of steps task, element as the associated element and behavior as the scroll behavior.

8 Extensions to the HTMLElement Interface

partial interface HTMLElement {
  readonly attribute Element? offsetParent;
  readonly attribute double offsetTop;
  readonly attribute double offsetLeft;
  readonly attribute double offsetWidth;
  readonly attribute double offsetHeight;
};

The offsetParent attribute must return the result of running these steps:

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

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

  3. Return null.

The offsetTop attribute must return the result of running these steps:

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

  2. If the offsetParent of the element is null return the y-coordinate of the top border edge of the first CSS layout box associated with the element, relative to the initial containing block origin, ignoring any transforms that apply to the element and its ancestors, and terminate this algorithm.

  3. Return the result of subtracting the y-coordinate of the top padding edge of the first CSS layout box associated with the offsetParent of the element from the y-coordinate of the top border edge of the first CSS layout box associated with the element, relative to the initial containing block origin, ignoring any transforms that apply to the element and its ancestors.

    An inline element that consists of multiple line boxes will only have its first CSS layout box considered.

The offsetLeft attribute must return the result of running these steps:

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

  2. If the offsetParent of the element is null return the x-coordinate of the left border edge of the first CSS layout box associated with the element, relative to the initial containing block origin, , ignoring any transforms that apply to the element and its ancestors, and terminate this algorithm.

  3. Return the result of subtracting the x-coordinate of the left padding edge of the first CSS layout box associated with the offsetParent of the element from the x-coordinate of the left border edge of the first CSS layout box associated with the element, relative to the initial containing block origin, ignoring any transforms that apply to the element and its ancestors.

The offsetWidth attribute must return the result of running these steps:

  1. If the element does not have any associated CSS layout box return zero and terminate this algorithm.

  2. Return the border edge width of the first CSS layout box associated with the element, ignoring any transforms that apply to the element and its ancestors.

The offsetHeight attribute must return the result of running these steps:

  1. If the element does not have any associated CSS layout box return zero and terminate this algorithm.

  2. Return the border edge height of the first CSS layout box associated with the element, ignoring any transforms that apply to the element and its ancestors.

9 Excensions to the HTMLImageElement Interface

partial interface HTMLImageElement {
  readonly attribute double x;
  readonly attribute double y;
};

The x attribute, on getting, must return the x-coordinate of the left border edge of the first CSS layout box associated with the element, relative to the initial containing block origin, ignoring any transforms that apply to the element and its ancestors, or zero if there is no CSS layout box.

The y attribute, on getting, must return the y-coordinate of the top border edge of the first CSS layout box associated with the element, relative to the initial containing block origin, ignoring any transforms that apply to the element and its ancestors, or zero if there is no CSS layout box.

10 Extensions to the Range Interface

The objects the methods described below return must be static.

partial interface Range {
  DOMRectList getClientRects();
  DOMRect getBoundingClientRect();
};

The getClientRects() method, when invoked, must return an empty DOMRectList object if the range is not in the document and otherwise a DOMRectList object containing a list of DOMRect 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 DOMRect object whose x, y, width and height members are zero.

  3. Otherwise, return a DOMRect 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.

11 Extensions to the MouseEvent Interface

The object IDL fragment redefines some members. Can we resolve this somehow?

partial interface MouseEvent {
  readonly attribute double screenX;
  readonly attribute double screenY;
  readonly attribute double pageX;
  readonly attribute double pageY;
  readonly attribute double clientX;
  readonly attribute double clientY;
  readonly attribute double x;
  readonly attribute double y;
  readonly attribute double offsetX;
  readonly attribute double offsetY;
};

partial dictionary MouseEventInit {
  double screenX = 0.0;
  double screenY = 0.0;
  double clientX = 0.0;
  double clientY = 0.0;
};

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

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

The pageX attribute must follow these steps:

  1. If the event's dispatch flag is set, return the horizontal coordinate of the position where the event occurred relative to the origin of the initial containing block and terminate these steps.

  2. Let offset be the value of the scrollX attribute of the event's associated Window object, if there is one, or zero otherwise.

  3. Return the sum of offset and the value of the event's clientX attribute.

The pageY attribute must follow these steps:

  1. If the event's dispatch flag is set, return the vertical coordinate of the position where the event occurred relative to the origin of the initial containing block and terminate these steps.

  2. Let offset be the value of the scrollX attribute of the event's associated Window object, if there is one, or zero otherwise.

  3. Return the sum of offset and the value of the event's clientY attribute.

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

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

The x attribute must return the value of clientX.

The y attribute must return the value of clientY.

The offsetX attribute must follow these steps:

  1. If the event's dispatch flag is set, return the x-coordinate of the position where the event occurred relative to the origin of the padding edge of the target node, ignoring the transforms that apply to the element and its ancestors, and terminate these steps.

  2. Return the value of the event's pageX attribute.

The offsetY attribute must follow these steps:

  1. If the event's dispatch flag is set, return the y-coordinate of the position where the event occurred relative to the origin of the padding edge of the target node, ignoring the transforms that apply to the element and its ancestors, and terminate these steps.

  2. Return the value of the event's pageY attribute.

12 Geometry

12.1 The GeometryUtils Interface

enum CSSBoxType { "margin", "border", "padding", "content" };
dictionary BoxQuadOptions {
  CSSBoxType box = "border";
  GeometryNode relativeTo;
};

dictionary ConvertCoordinateOptions {
  CSSBoxType fromBox = "border";
  CSSBoxType toBox = "border";
};

[NoInterfaceObject]
interface GeometryUtils {
  sequence<DOMQuad> getBoxQuads(optional BoxQuadOptions options);
  DOMQuad convertQuadFromNode(DOMQuad quad, GeometryNode from, optional ConvertCoordinateOptions options);
  DOMQuad convertRectFromNode(DOMRectReadOnly rect, GeometryNode from, optional ConvertCoordinateOptions options);
  DOMPoint convertPointFromNode(DOMPointInit point, GeometryNode from, optional ConvertCoordinateOptions options);
};

Text implements GeometryUtils;
Element implements GeometryUtils;
PseudoElement implements GeometryUtils;
Document implements GeometryUtils;

typedef (Text or Element or PseudoElement or Document) GeometryNode;

The getBoxQuads(options) method must run the following steps:

  1. ...

The convertQuadFromNode(quad, from, options) method must run the following steps:

  1. ...

The convertRectFromNode(rect, from, options) method must run the following steps:

  1. ...

The convertPointFromNode(point, from, options) method must run the following steps:

  1. ...

13 Events

13.1 Resizing viewports

Whenever a viewport has its width or height changed (e.g. as a result of the user resizing the browser window, or changing the page zoom scale factor, or an iframe element's dimensions are changed), the user agent must queue a task to fire an event event named resize at the Window object associated with that viewport. If such a resize is ongoing over a period of time, the user agent must queue that task at a user-agent-defined interval while the resize is ongoing.

13.2 Scrolling

Whenever a viewport gets scrolled in response to user interaction, the user agent must queue a task to fire an event named scroll that bubbles at the Document object associated with that viewport. If such a scroll is ongoing over a period of time, the user agent must queue that task at a user-agent-defined interval while the scroll is ongoing.

Whenever an element gets scrolled in response to user interaction, the user agent must queue a task to fire an event named scroll at the element. If such a scroll is ongoing over a period of time, the user agent must queue that task at a user-agent-defined interval while the scroll is ongoing.

14 CSS properties

The features in this section should be moved to some other specification.

14.1 Smooth Scrolling: The 'scroll-behavior' Property

Name: scroll-behavior
Value: instant | smooth
Initial: instant
Applies to: scrolling boxes
Inherited: no
Computed value:	specified value
Media: visual
Animatable: no
Canonical Order: per grammar

The 'scroll-behavior' property specifies the scrolling behavior for a scrolling box, when scrolling happens due to navigation or CSSOM scrolling APIs. Scrolls that are performed by the user are not affected by this property. When this property is specified on the root element, it applies to the viewport instead.

The 'scroll-behavior' property of the HTML body element is not propagated to the viewport.

'instant'

The scrolling box is scrolled in an instant fashion.

'smooth'

The scrolling box is scrolled in a smooth fashion using a user-agent-defined timing function over a user-agent-defined period of time. User agents should follow platform convensions, if any.

User agents may ignore this property.

References

[CSSBOX]
CSS basic box model, Bert Bos and Anton Prowse. W3C.
[CSSDEVICEADAPT]
CSS Device Adaptation, Rune Lillesveen, Øyvind Stenhaug and Florian Rivoal.
[CSSOM]
CSS Object Model (CSSOM), Simon Pieters and Glenn Adams. W3C.
[CSSTRANSFORMS]
CSS Transforms, Simon Fraser, Dean Jackson, Edward O'Connor et al.. W3C.
[CSSWRITINGMODES]
CSS Writing Modes Module, Elika J. Etemad and Koji Ishii. W3C.
[DOM]
DOM, Anne van Kesteren, Aryeh Gregor and Ms2ger. WHATWG.
[GEOMETRY]
Geometry Interfaces Module, Simon Pieters, Dirk Schulze and Rik Cabanier. W3C.
[HTML]
HTML, Ian Hickson. WHATWG.
[RFC2119]
Key words for use in RFCs to Indicate Requirement Levels, Scott Bradner. IETF.
[SVG]
Scalable Vector Graphics (SVG), O. Andersson, R. Berjon, E. Dahlström et al.. W3C.
[WEBIDL]
Web IDL, Cameron McCormack. W3C.

Acknowledgments

The editors would like to thank Alan Stearns, Alexey Feldgendler, Antonio Gomes, Björn Höhrmann, Dan Bates, David Vest, Elliott Sprehn, Garrett Smith, Henrik Andersson, Hallvord R. M. Steen, Kang-Hao Lu, Leif Arne Storset, Luiz Agostini, Maciej Stachowiak, Michael Dyck, Mike Wilson, Morten Stenshorne, Olli Pettay, Pavel Curtis, Peter-Paul Koch, Rachel Kmetz, Robert O'Callahan, Sam Weinig, Scott Johnson, Sebastian Zartner, Stewart Brodie, Sylvain Galineau, Tab Atkins, Tarquin Wilton-Jones, Thomas Moore, and Xiaomei Ji 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.