← Back to team overview

anewt-developers team mailing list archive

[Branch ~uws/anewt/anewt.uws] Rev 1747: [xhtml] Implement ax_join() analogous to join()

 

------------------------------------------------------------
revno: 1747
committer: Wouter Bolsterlee <uws@xxxxxxxxx>
branch nick: anewt.uws
timestamp: Tue 2010-02-16 00:32:03 +0100
message:
  [xhtml] Implement ax_join() analogous to join()
  
  ...but fully XML DOM node aware (including safe escaping).
  
  Part of bug #503550.
modified:
  xhtml/api.lib.php
  xhtml/xhtml.test.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 'xhtml/api.lib.php'
--- xhtml/api.lib.php	2010-02-15 23:31:12 +0000
+++ xhtml/api.lib.php	2010-02-15 23:32:03 +0000
@@ -124,6 +124,40 @@
 }
 
 
+/**
+ * Join array elements with a string or DOM node.
+ *
+ * This is an XHTML-aware version of the built-in join() function.
+ * 
+ * \param $glue
+ *   The glue string (or DOM node) to insert between each subsequent pair of
+ *   values.
+ *
+ * \param $values
+ *   The values to join together. These can be strings or DOM nodes.
+ *
+ * \return
+ *   A DOM node instance containing the joined values.
+ */
+function ax_join($glue, $values)
+{
+	assert('is_numeric_array($values);');
+
+	/* Make sure the glue is escaped properly */
+	if ($glue instanceof AnewtXMLDomNode) {
+		$glue = to_string($glue);
+	} else {
+		assert('is_string($glue)');
+		$glue = htmlspecialchars($glue);
+	}
+	
+	/* Build a format string so that ax_vsprintf() can do the real work */
+	$glue = str_replace('%', '%%', $glue);
+	$format = join($glue, array_fill(0, count($values), '%s'));
+	return ax_vsprintf(ax_raw($format), $values);
+}
+
+
 /* Text (See text.lib.php) */
 
 /* Text: Phrase elements */

=== modified file 'xhtml/xhtml.test.php'
--- xhtml/xhtml.test.php	2010-02-15 23:31:12 +0000
+++ xhtml/xhtml.test.php	2010-02-15 23:32:03 +0000
@@ -183,6 +183,10 @@
 $r[] = ax_p(ax_sprintf('%s & %s', ax_span_class('Sugar', 'sweet'), 'Spice'));
 $r[] = ax_p(ax_vsprintf('%s & %s', array(ax_span_class('Sugar', 'sweet'), 'Spice')));
 
+$values = array('this', ax_strong('is'), 'a', ax_span('test'));
+$r[] = ax_p(ax_join(', ', $values));
+$r[] = ax_p(ax_join(ax_em(' & '), $values));
+
 $fragment->append_child(ax_fragment($r, ax_p('final paragraph')));