← Back to team overview

anewt-developers team mailing list archive

[Branch ~uws/anewt/anewt.uws] Rev 1824: [page] Implement AnewtPage::add_conditional_stylesheet()

 

------------------------------------------------------------
revno: 1824
committer: Wouter Bolsterlee <uws@xxxxxxxxx>
branch nick: anewt
timestamp: Thu 2011-03-17 19:28:04 +0100
message:
  [page] Implement AnewtPage::add_conditional_stylesheet()
  
  Add a gross hack to support conditional stylesheets based on
  an abuse of XML comment syntax and the IE HTML parser. See
  the docs for more information.
modified:
  page/page.lib.php


--
lp:anewt
https://code.launchpad.net/~uws/anewt/anewt.uws

Your team Anewt developers is subscribed to branch lp:anewt.
To unsubscribe from this branch go to https://code.launchpad.net/~uws/anewt/anewt.uws/+edit-subscription
=== modified file 'page/page.lib.php'
--- page/page.lib.php	2011-03-17 16:21:49 +0000
+++ page/page.lib.php	2011-03-17 18:28:04 +0000
@@ -136,6 +136,27 @@
 		$this->add_stylesheet(ax_stylesheet_href_media($href, $media));
 	}
 
+	/**
+	 * Add a conditional stylesheet to the page.
+	 *
+	 * This a gross hack that is very useful in some cases because of browser
+	 * differences. The hack abuses the HTML comment syntax to instruct Internet
+	 * Explorer to conditionally load style sheets, while browsers that conform
+	 * to XML standards ignore it. If you use this method, you will already know
+	 * what you are doing.
+	 *
+	 * \param $condition
+	 *   A short string describing the condition, e.g. <code>lte IE7</code>
+	 * \param $node
+	 *   A AnewtXHTMLLink node created using ax_stylesheet_href() or
+	 *   ax_stylesheet_href_media().
+	 */
+	public function add_conditional_stylesheet($condition, $node)
+	{
+		assert('$node instanceof AnewtXHTMLLink;');
+		$this->_links[] = new AnewtPageConditionalStylesheetWrapper($condition, $node);
+	}
+
 	/** \} */
 
 
@@ -721,4 +742,39 @@
 	/** \} */
 }
 
+
+/**
+ * Private wrapper class for use with AnewtPage::add_conditional_stylesheet().
+ *
+ * This custom XHTML block element must never be used directly.
+ */
+class AnewtPageConditionalStylesheetWrapper extends AnewtXHTMLBlockElement
+{
+	/** Condition string */
+	private $condition;
+	/** AnewtXHTMLLink instance */
+	private $link;
+
+	/**
+	 * Create a new conditional stylesheet wrapper node.
+	 *
+	 * \param $condition
+	 * \param $link
+	 */
+	public function __construct($condition, $link)
+	{
+		$this->condition = $condition;
+		$this->link = $link;
+	}
+
+	public function render($indent_level=0)
+	{
+		return sprintf(
+			"\n%s<!--[if %s]>%s<![endif]-->",
+			str_repeat("\t", $indent_level),
+			$this->condition,
+			$this->link);
+	}
+}
+
 ?>