Rose::HTML::Object(3p)

NAME
Rose::HTML::Object - HTML object base class.

SYNOPSIS
  package MyTag;

  use Rose::HTML::Object;
  our @ISA = qw(Rose::HTML::Object);

  __PACKAGE__->add_valid_html_attrs
  (
    'foo',
    'bar',
    'baz',
    ...
  );

  __PACKAGE__->add_required_html_attrs(
  {
    foo => 5,  # with default value
    goo => '', # required implies valid
  });

  __PACKAGE__->add_boolean_html_attrs
  (
    'selected', # boolean implies valid
  );

  sub html_element  { 'mytag' }
  sub xhtml_element { 'mytag' }
  ...

  my $o = MyTag->new(bar => 'hello', selected => 1);

  # prints: bar="hello" foo="5" goo="" selected
  print $o->html_attrs_string;

  # prints: bar="hello" foo="5" goo="" selected="selected"
  print $o->xhtml_attrs_string;

  $o->html_attr(selected => 0);

  print "Has bar"  if($o->html_attr_exists('bar'));
  $o->delete_html_attr('bar');

  print $o->html_tag;  # <mytag foo="5" goo="" selected>
  print $o->xhtml_tag; # <mytag foo="5" goo="" selected="selected" />
  ...

DESCRIPTION
Rose::HTML::Object is the base class for HTML objects. It defines the HTML element name, provides methods for specifying, manipulating, and validating HTML attributes, and can serialize itself as either HTML or XHTML.

This class inherits from, and follows the conventions of, Rose::Object. See the Rose::Object documentation for more information.

VALIDATION
Although several methods, data structures, and policies exist to aid the creation of valid HTML, they are in no way a replacement for real markup validation.

This class and those that inherit from it try to support a superset of the elements and attributes specified in the HTML 4.01 and XHTML 1.x specifications. As a result, these classes will tend to be more permissive than actual validation. The support of these standards is not exhaustive, and will inevitably expand. Also remember that there are several variant DTDs that make up XHTML 1.x. By trying to support a superset of these standards, this class simply can't correctly enforce the rules of any individual standard.

So I say again: these classes are not a replacement for real markup validation! Use an external validator!

Going forward, the compatibility policy of these classes is that attribute specifications may be added in the future, but existing attribute specifications will never be removed (unless they originally existed in error, i.e., were never part of any HTML 4.01 or XHTML 1.x standard).

This support policy is pragmatic rather than ideological. There is enough default validation to catch most typos or other unintentional errors, but not so much that the entire class hierarchy is weighed down by language lawyering and bookkeeping.

If the runtime overhead of validating every HTML attribute is deemed too onerous, it can be turned off on a per-object basis with the validate_html_attrs() method. Subclasses can set this attribute during object construction to make the effect class-wide. (You will also want to look at the autoload_html_attr_methods() class attribute.)

There are also methods for adding and removing valid, required, and boolean HTML attributes for a class.

Finally, all element and attribute names are case-sensitive and lowercase in order to comply with XHTML (and to be easy to type).

CLASS METHODS
These class methods can be called with a class name or an object as the invocant. Either way, remember that the data structures and attributes affected are part of the class as a whole, not any individual object. For example, adding a valid HTML attribute makes it valid for all objects of the class, including any objects that already exist.

Many of the class methods manipulate "inheritable sets" or "inherited sets." See the Rose::Class::MakeMethods::Set documentation for an explanation off these method types.

The sets of valid and boolean HTML attributes are "inherited sets." The set of required HTML attributes is an "inheritable set."

The inheritance behavior of these sets is noted here in order to facilitate subclassing. But it is an implementation detail, not a part of the public API. The requirements of the APIs themselves do not include any particular inheritance behavior.

add_boolean_html_attr NAME
    Adds a value to the list of boolean HTML attributes for this class. Boolean HTML attributes appear without values in HTML tags, (e.g., <dl compact>) or with fixed values in XHTML tags (e.g., <dl compact="compact">)

add_boolean_html_attrs NAME1, NAME2, ...
    Adds one or more values to the list of boolean HTML attributes for this class. Boolean HTML attributes appear without values in HTML tags, (e.g., <dl compact>) or with fixed values in XHTML tags (e.g., <dl compact="compact">)

add_required_html_attr NAME [, DEFAULT]
    Adds a value to the list of required HTML attributes for this class. Required HTML attributes will always appear in the HTML tag, with or without a non-empty value. You can set the default value for a required HTML attribute using the required_html_attr_value() method or by passing the DEFAULT parameter to this method.

add_required_html_attrs NAME1, NAME2, ... | HASHREF
    Adds one or more values to the list of required HTML attributes for this class. Required HTML attributes will always appear in the HTML tag, with or without a non-empty value. You can set the default value for a required HTML attribute using the required_html_attr_value() method or by passing a reference to a hash containing name/default pairs.

add_valid_html_attr NAME
    Adds a value to the list of valid HTML attributes for this class. If the object property "validate_html_attrs" is true, then only valid attributes can be added to an object of this class.

add_valid_html_attrs NAME1, NAME2, ...
    Adds one or more values to the list of valid HTML attributes for this class. If the object property "validate_html_attrs" is true, then only valid attributes can be added to an object of this class.

autoload_html_attr_methods [BOOL]
    Get or set the boolean flag that determines whether or not any valid HTML attribute can be used as a method call of the same name. The default is true, and the value is inherited by subclasses unless overridden.

    In the case of a name conflict, the existing method is called and a new method is not auto-created for the HTML attribute of the same name.

    Examples:

        MyTag->add_valid_httml_attrs('foo', 'bar', 'error');

        $o = MyTag->new;

        # Auto-created method, equivalent to $o->html_attr(foo => 5)
        $o->foo(5);

        # Fatal error: invalid HTML attribute and not an existing method
        print $o->blah;

        MyTag->autoload_html_attr_methods(0); # stop autoloading

        # Fatal error: method does not exist and was never auto-created
        print $o->bar;

        # This still works: once the method is auto-created, it stays
        print $o->foo; # prints "5"

        # Calls the existing error() object method; does not affect
        # the HTML attribute named "error"
        $o->error(99);

    Yes, the existence of this capability means that adding a method to a future version of a Rose::HTML::Object-derived class that has the same name as a valid HTML attribute may cause older code that calls the auto-created method of the same name to break.

    You have a few options. You can simply choose not to use any auto-created methods, opting instead to use html_attr() everywhere (and you can set "autoload_html_attr_methods" to false to make sure that you don't accidentally use such a method). Or you can simply trust that I will not add new methods with the same names as existing valid HTML attributes. Since I use auto-created methods extensively, you can have some faith that I will do everything I can to avoid breaking my own code.

boolean_html_attrs
    Returns a reference to a sorted list of boolean HTML attributes in scalar context, or a sorted list of boolean HTML attributes in list context. The default set of boolean HTML attributes is empty.

    See the introduction to the "CLASS METHODS" section for more information about the "inherited set" implementation used by the set of boolean HTML attributes.

default_html_attr_value NAME [, VALUE]
    Returns the default value for the HTML attribute NAME.

    If passed both an attribute NAME and a VALUE, it adds NAME to the set of required HTML attributes and sets its default value to VALUE.

delete_boolean_html_attr NAME
    Removes the HTML attribute NAME from the set of boolean HTML attributes.

delete_required_html_attr NAME
    Removes the HTML attribute NAME from the set of required HTML attributes.

delete_valid_html_attr NAME
    Removes the HTML attribute NAME from the set of valid HTML attributes. The attribute is also removed from the set of required and boolean HTML attributes, if it existed in either set.

html_attr_is_boolean NAME
    Returns a boolean value indicating whether or not the attribute NAME is a boolean HTML attribute. A boolean attribute must also be a valid attribute.

html_attr_is_required NAME
    Returns a boolean value indicating whether or not the attribute NAME is a required HTML attribute. A required attribute must also be a valid attribute.

html_attr_is_valid NAME
    Returns a boolean value indicating whether or not the attribute NAME is a valid HTML attribute.

html_element [NAME]
    Get or set the name of the HTML element. The HTML element is the name of the tag, e.g. "img", "p", "a", "select", "textarea", etc.

    This attribute may be read-only in subclasses, but is read/write here for increased flexibility. The value is inherited by subclasses.

required_html_attrs
    Returns a reference to a sorted list of required HTML attributes in scalar context, or a sorted list of required HTML attributes in list context. The default set of required HTML attributes is empty.

    Required HTML attributes are included in the strings generated by html_attrs_string() and xhtml_attrs_string() even if they have been deleted by delete_html_attr() or one of its variants. If a required HTML attribute does not have a default value, its value defaults to an empty string or, if the attribute is also boolean, the name of the attribute.

    See the introduction to the "CLASS METHODS" section for more information about the "inheritable set" implementation used by the set of boolean HTML attributes.

required_html_attr_value ATTR [, VALUE]
    Get or set the default value of the required HTML attrbute ATTR. If both ATTR and VALUE are passed, the value is set. The current value is returned.

valid_html_attrs
    Returns a reference to a sorted list of valid HTML attributes in scalar context, or a sorted list of valid HTML attributes in list context. The default set is:

        id
        class
        style
        title
        lang
        xml:lang
        dir
        onclick
        ondblclick
        onmousedown
        onmouseup
        onmouseover
        onmousemove
        onmouseout
        onkeypress
        onkeydown
        onkeyup

    See the "VALIDATION" section for more on the philosophy and policy of validation. See the introduction to the "CLASS METHODS" section for more information about the "inherited set" implementation used by the set of valid HTML attributes.

xhtml_element [NAME]
    Get or set the name of the XHTML element. The XHTML element is the name of the tag, e.g. "img", "p", "a", "select", "textarea", etc.

    This attribute may be read-only in subclasses, but is read/write here for increased flexibility. The value is inherited by subclasses.

CONSTRUCTOR

new PARAMS
    Constructs a new Rose::HTML::Object object based on PARAMS, where PARAMS are name/value pairs. Any object method is a valid parameter name.

OBJECT METHODS

children
    Returns a list (in list context) or a reference to an array (in scalar context) of Rose::HTML::Object-derived objects that are contained within, or otherwise "children of" this object.

    The array reference return value should be treated as read-only. The individual items may be treated as read/write provided that you understand that you're modifying the actual children, not copies.

clear_all_html_attrs
    Clears all the HTML attributes by settings their values to undef.

clear_html_attr NAME
    Clears the HTML attribute NAME by settings its value to undef.

clear_html_attrs NAME1, NAME2, ...
    Clears the HTML attributes specified by NAME1, NAME2, etc. by settings their values to undef.

delete_all_html_attrs
    Deletes all the HTML attributes.

delete_html_attr NAME
    Deletes the HTML attribute NAME.

delete_html_attrs NAME1, NAME2, ...
    Deletes the HTML attributes specified by NAME1, NAME2, etc.

error [TEXT]
    Get or set an error string.

escape_html [BOOL]
    This flag may be used by other methods to decide whether or not to escape HTML. It is set to true by default. The only method in Rose::HTML::Object that references it is html_error. All other HTML is escaped as appropriate regardless of the escape_html setting (e.g. the text returned by "html_attrs_string" always has its attribute values escaped). Subclasses may consult this flag for similar purposes (which they must document, of course).

has_error
    Returns true if an error is set, false otherwise.

html
    A synonym for html_tag().

html_attr NAME [, VALUE]
    Get or set the HTML attribute NAME. If just NAME is passed, it returns the value of the HTML attribute specified by NAME, or undef if there is no such attribute.

    If both NAME and VALUE are passed, it sets the HTML attribute NAME to VALUE.

    If NAME is not a valid attribute, a fatal error is thrown.

    Examples:

        $o->html_attr(color => 'red');   # set color to red
        $color = $o->html_attr('color'); # get color

html_attrs [ATTRS]
    If called with an argument, this method sets and/or adds the HTML attributes specified by ATTRS, where ATTRS is a series of name/value pairs or a reference to a hash of name/value pairs.

    Returns all of the existing HTML attributes as a hash (in list context) or a reference to a hash (in scalar context).

    Note that the reference returned in scalar context is a reference to the object's actual hash of attributes; modifying it will change the state of the object! I recommend that you treat the contents of the referenced hash as read-only, and I cannot promise that I will not find a way to force it to be read-only in the future.

    The order of the attributes in the return value is indeterminate.

    Examples:

        # Set/add attributes
        $o->html_attrs(color => 'red', age => 5); # name/value pairs
        $o->html_attrs({ style => fancy });       # hashref

        %h = $o->html_attrs; # get all three attributes as a hash
        $h = $o->html_attrs; # get all three attributes as a hash ref

html_attrs_string
    If there are any HTML attributes, it returns a sorted list of HTML attributes and their values in a string suitable for use in an HTML tag. The string includes a leading space.

    If there are no HTML attributes, an empty string is returned.

    Examples:

        MyTag->add_valid_html_attrs('color', 'age');
        MyTag->add_boolean_html_attr('happy');

        $o = MyTag->new;

        $o->html_attrs(color => 'red<', age => 5, happy => 12345);

        $s = $o->html_attrs_string; # ' age="5" color="red&lt;" happy'

html_attr_hook NAME [, CODE]
    If called with two arguments, it sets the hook method for the attribute NAME to the code reference CODE.

    If called with one or two arguments, it returns the hook method for the HTML attribute NAME as a code reference, or undef if there is no hook method.

    Hook methods are called whenever their corresponding HTML attribute is set or retrieved. When the attribute is set, the hook method gets the proposed value of the attribute as an argument. The return value of the hook method is then used as the actual value of the attribute.

    When an attribute is retrieved, the hook method is called with no arguments, and its return value is what is actually returned to the caller.

    In both cases, the default variable $_ is localized and then set to the new or existing value of the attribute before the hook method is called.

    Examples:

        # Set hook for 'color' attribute
        $o->html_attr_hook(color => sub
        {
          my($self) = shift;

          if(@_) # attribute is being set
          {
            return uc shift; # make it uppercase
          }

          # attribute being retrieved:
          return $_; # return the existing attribute value as-is
        });

        $o->html_attr(color => 'red');   # color set to 'RED'
        $color = $o->html_attr('color'); # $color = 'RED'

html_error
    Returns the error text, if any, as a snippet of HTML that looks like this:

        <span class="error">Error text goes here</span>

    If the escape_html flag is set to true (the default), then the error text has any HTML in it escaped.

html_tag
    Serializes the object as an HTML tag. In other words, it is the concatenation of the strings returned by html_element() and html_attrs_string(), wrapped with the appropriate angled brackets.

set_error
    Set the error to a defined but "invisible" (zero-length) value. This value will not be displayed by the html_error or xhtml_error. Use this method when you want to flag a field as having an error, but don't want a visible error message.

unset_error
    Set the error to a undef.

validate_html_attrs BOOL
    If set to true, HTML attribute arguments to "html_attr" and "html_attr_hook" will be validated by calling "html_attr_is_valid(ATTR)", where ATTR is the name of the attribute being set or read. The default value is true.

xhtml
    A synonym for xhtml_tag().

xhtml_error
    Returns the error text, if any, as a snippet of XHTML that looks like this:

        <span class="error">Error text goes here</span>

    If the escape_html flag is set to true (the default), then the error text has any HTML in it escaped.

xhtml_tag
    Serializes the object as an XHTML tag. In other words, it is the concatenation of the strings returned by xhtml_element() and xhtml_attrs_string(), wrapped with the appropriate angled brackets and forward slash character.

xhtml_attrs_string
    If there are any HTML attributes, it returns a sorted list of HTML attributes and their values in a string suitable for use in an XHTML tag. The string includes a leading space.

    If there are no HTML attributes, an empty string is returned.

    Examples:

        MyTag->add_valid_html_attrs('color', 'age');
        MyTag->add_boolean_html_attr('happy');

        $o = MyTag->new;

        $o->html_attrs(color => 'red<', age => 5, happy => 12345);

        # ' age="5" color="red&lt;" happy="happy"'
        $s = $o->html_attrs_string;

SUPPORT
Any Rose::HTML::Objects questions or problems can be posted to the Rose::HTML::Objects mailing list. To subscribe to the list or view the archives, go here:

<http://lists.sourceforge.net/lists/listinfo/rose-html-objects>

Although the mailing list is the preferred support mechanism, you can also email the author (see below) or file bugs using the CPAN bug tracking system:

<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Rose-HTML-Objects>

AUTHOR
John C. Siracusa (siracusa@gmail.com)

COPYRIGHT
Copyright (c) 2007 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.