function _autofloat in AutoFloat 6.2
Same name and namespace in other branches
- 6 autofloat.module \_autofloat()
Find a rejected 'div', a selected 'span', a link with image or an image.
1 call to _autofloat()
- autofloat_filter in ./
autofloat.module - Implements hook_filter_info().
File
- ./
autofloat.module, line 85 - Autofloat module: A filter that floats images left and right automatically.
Code
function _autofloat($text, $format) {
$selector = variable_get('autofloat_span', 'flickr-wrap');
// Divide the variable into two selectors if a comma is found.
$selector = explode(',', $selector);
// Make sure both array keys exist.
if (isset($selector[0]) == FALSE) {
$selector[0] = 'non-existing-class-value';
}
if (isset($selector[1]) == FALSE) {
$selector[1] = 'non-existing-class-value';
}
$rejector = variable_get('autofloat_div', 'flickr-photoset');
// Divide the variable into two rejectors if a comma is found.
$rejector = explode(',', $rejector);
// Make sure both array keys exist, if not the regex breaks.
if (isset($rejector[0]) == FALSE) {
$rejector[0] = 'non-existing-class-value';
}
if (isset($rejector[1]) == FALSE) {
$rejector[1] = 'non-existing-class-value';
}
$count = 0;
// Load string to DOMDocument.
$doc = new DOMDocument();
// Ignore warnings during HTML soup loading.
@$doc
->loadHTML('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>' . $text . '</body></html>');
$xpath = new DOMXPath($doc);
// Iterate through the string neglecting elements nested inside those already
// processed or those to reject.
foreach ($xpath
->query('//span[contains(@class, "float") or contains(@class, "' . $selector[0] . '") or contains(@class, "' . $selector[1] . '")][not(ancestor::*[@class="nofloat"] or ancestor::*[@class="' . $rejector[0] . '"] or ancestor::*[@class="' . $rejector[1] . '"])] | //img[not(ancestor-or-self::*[contains(@class, "nofloat")] or ancestor-or-self::*[contains(@class, "' . $rejector[0] . '")] or ancestor-or-self::*[contains(@class, "' . $rejector[1] . '")] or ancestor::span[contains(@class, "float") or contains(@class, "' . $selector[0] . '") or contains(@class, "' . $selector[1] . '")])]') as $tag) {
// Start on the left or right depending on the settings.
if (variable_get('autofloat_start', 0) == 0) {
$zebra = $count % 2 ? 'even' : 'odd';
}
else {
$zebra = $count % 2 ? 'odd' : 'even';
}
$count++;
// Add an alternating class value but maintain the existing ones.
$tag
->setAttribute('class', 'autofloat-' . $zebra . ' ' . $tag
->getAttribute('class'));
}
// Convert a DOM object back to an HTML snippet.
$body_node = $doc
->getElementsByTagName('body')
->item(0);
$body_content = '';
foreach ($body_node
->getElementsByTagName('script') as $node) {
filter_dom_serialize_escape_cdata_element($doc, $node);
}
foreach ($body_node
->getElementsByTagName('style') as $node) {
filter_dom_serialize_escape_cdata_element($doc, $node, '/*', '*/');
}
foreach ($body_node->childNodes as $child_node) {
$body_content .= $doc
->saveXML($child_node);
}
return preg_replace('|<([^> ]*)/>|i', '<$1 />', $body_content);
}