r42702 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r42701‎ | r42702 | r42703 >
Date:23:58, 27 October 2008
Author:simetrical
Status:old
Tags:
Comment:
Create new, better tooltip/accesskey methods

Old Linker::tooltipAndAccesskey() and Linker::tooltip(), which pass
around raw bits of HTML, are now wrappers for Linker::titleAttrib() and
Linker::accesskey(). Actual usages have not been changed for now.

Note that this might have some slight performance implication, since
the accesskey message will typically be retrieved twice per view
instead of once: once for the accesskey attribute, once for the bit
we're appending to the title attribute. This used to be avoided by
using a single tooltipAndAccesskey() method, but that's pretty ugly.
Of course, the second hit should reach the local accesskey cache and
not result in a second memcached hit, but I recall Tim saying that that
still takes 100 us or something unreasonable like that.

Causes no new parser test failures, and I didn't notice any difference
in the UI with a quick check.
Modified paths:
  • /trunk/phase3/includes/Linker.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Linker.php
@@ -1698,38 +1698,37 @@
16991699 }
17001700
17011701 /**
1702 - * Given the id of an interface element, constructs the appropriate title
1703 - * and accesskey attributes from the system messages. (Note, this is usu-
1704 - * ally the id but isn't always, because sometimes the accesskey needs to
1705 - * go on a different element than the id, for reverse-compatibility, etc.)
1706 - *
1707 - * @param string $name Id of the element, minus prefixes.
1708 - * @return string title and accesskey attributes, ready to drop in an
1709 - * element (e.g., ' title="This does something [x]" accesskey="x"').
 1702+ * @deprecated Returns raw bits of HTML, use titleAttrib() and accesskey()
17101703 */
17111704 public function tooltipAndAccesskey( $name ) {
1712 - wfProfileIn( __METHOD__ );
1713 - $attribs = array();
1714 -
1715 - $tooltip = wfMsg( "tooltip-$name" );
1716 - if( !wfEmptyMsg( "tooltip-$name", $tooltip ) && $tooltip != '-' ) {
1717 - // Compatibility: formerly some tooltips had [alt-.] hardcoded
1718 - $tooltip = preg_replace( "/ ?\[alt-.\]$/", '', $tooltip );
1719 - $attribs['title'] = $tooltip;
 1705+ # FIXME: If Sanitizer::expandAttributes() treated "false" as "output
 1706+ # no attribute" instead of "output '' as value for attribute", this
 1707+ # would be three lines.
 1708+ $attribs = array(
 1709+ 'title' => $this->titleAttrib( $name, 'withaccess' ),
 1710+ 'accesskey' => $this->accesskey( $name )
 1711+ );
 1712+ if ( $attribs['title'] === false ) {
 1713+ unset( $attribs['title'] );
17201714 }
 1715+ if ( $attribs['accesskey'] === false ) {
 1716+ unset( $attribs['accesskey'] );
 1717+ }
 1718+ return Xml::expandAttributes( $attribs );
 1719+ }
17211720
1722 - $accesskey = wfMsg( "accesskey-$name" );
1723 - if( $accesskey != '' && $accesskey != '-' &&
1724 - !wfEmptyMsg( "accesskey-$name", $accesskey ) ) {
1725 - if( isset( $attribs['title'] ) ) {
1726 - $attribs['title'] .= " [$accesskey]";
1727 - }
1728 - $attribs['accesskey'] = $accesskey;
 1721+ /** @deprecated Returns raw bits of HTML, use titleAttrib() */
 1722+ public function tooltip( $name, $options = null ) {
 1723+ # FIXME: If Sanitizer::expandAttributes() treated "false" as "output
 1724+ # no attribute" instead of "output '' as value for attribute", this
 1725+ # would be two lines.
 1726+ $tooltip = $this->titleAttrib( $name, $options );
 1727+ if ( $tooltip === false ) {
 1728+ return '';
17291729 }
1730 -
1731 - $ret = Xml::expandAttributes( $attribs );
1732 - wfProfileOut( __METHOD__ );
1733 - return $ret;
 1730+ return Xml::expandAttributes( array(
 1731+ 'title' => $this->titleAttrib( $name, $options )
 1732+ ) );
17341733 }
17351734
17361735 /**
@@ -1741,29 +1740,57 @@
17421741 * @param string $name Id of the element, minus prefixes.
17431742 * @param mixed $options null or the string 'withaccess' to add an access-
17441743 * key hint
1745 - * @return string title attribute, ready to drop in an element
1746 - * (e.g., ' title="This does something"').
 1744+ * @return string Contents of the title attribute (which you must HTML-
 1745+ * escape), or false for no title attribute
17471746 */
1748 - public function tooltip( $name, $options = null ) {
 1747+ public function titleAttrib( $name, $options = null ) {
17491748 wfProfileIn( __METHOD__ );
17501749
1751 - $attribs = array();
 1750+ $tooltip = wfMsg( "tooltip-$name" );
 1751+ # Compatibility: formerly some tooltips had [alt-.] hardcoded
 1752+ $tooltip = preg_replace( "/ ?\[alt-.\]$/", '', $tooltip );
17521753
1753 - $tooltip = wfMsg( "tooltip-$name" );
1754 - if( !wfEmptyMsg( "tooltip-$name", $tooltip ) && $tooltip != '-' ) {
1755 - $attribs['title'] = $tooltip;
 1754+ # Message equal to '-' means suppress it.
 1755+ if ( wfEmptyMsg( "tooltip-$name", $tooltip ) || $tooltip == '-' ) {
 1756+ $tooltip = false;
17561757 }
17571758
1758 - if( isset( $attribs['title'] ) && $options == 'withaccess' ) {
1759 - $accesskey = wfMsg( "accesskey-$name" );
1760 - if( $accesskey != '' && $accesskey != '-' &&
1761 - !wfEmptyMsg( "accesskey-$name", $accesskey ) ) {
1762 - $attribs['title'] .= " [$accesskey]";
 1759+ if ( $options == 'withaccess' ) {
 1760+ $accesskey = $this->accesskey( $name );
 1761+ if( $accesskey !== false ) {
 1762+ if ( $tooltip === false || $tooltip === '' ) {
 1763+ $tooltip = "[$accesskey]";
 1764+ } else {
 1765+ $tooltip .= " [$accesskey]";
 1766+ }
17631767 }
17641768 }
17651769
1766 - $ret = Xml::expandAttributes( $attribs );
17671770 wfProfileOut( __METHOD__ );
1768 - return $ret;
 1771+ return $tooltip;
17691772 }
 1773+
 1774+ /**
 1775+ * Given the id of an interface element, constructs the appropriate
 1776+ * accesskey attribute from the system messages. (Note, this is usually
 1777+ * the id but isn't always, because sometimes the accesskey needs to go on
 1778+ * a different element than the id, for reverse-compatibility, etc.)
 1779+ *
 1780+ * @param string $name Id of the element, minus prefixes.
 1781+ * @return string Contents of the accesskey attribute (which you must HTML-
 1782+ * escape), or false for no accesskey attribute
 1783+ */
 1784+ public function accesskey( $name ) {
 1785+ $accesskey = wfMsg( "accesskey-$name" );
 1786+
 1787+ # FIXME: Per standard MW behavior, a value of '-' means to suppress the
 1788+ # attribute, but this is broken for accesskey: that might be a useful
 1789+ # value.
 1790+ if( $accesskey != ''
 1791+ && $accesskey != '-'
 1792+ && !wfEmptyMsg( "accesskey-$name", $accesskey ) ) {
 1793+ return $accesskey;
 1794+ }
 1795+ return false;
 1796+ }
17701797 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r105999bring back Linker::tooltip()...hashar09:52, 13 December 2011

Status & tagging log