You are here

function advagg_mod_xpath_script_external_dns in Advanced CSS/JS Aggregation 7.2

Use DOMDocument's loadHTML along with DOMXPath's query to find script tags.

Once found, it will add the src to the dns prefetch list.

Parameters

string $html: HTML fragments.

1 call to advagg_mod_xpath_script_external_dns()
advagg_mod_js_inline_processor in advagg_mod/advagg_mod.module
Given html, do some processing on the script tags included inside it.

File

advagg_mod/advagg_mod.module, line 2835
Advanced aggregation modifier module.

Code

function advagg_mod_xpath_script_external_dns($html) {

  // Do not throw errors when parsing the html.
  libxml_use_internal_errors(TRUE);
  $dom = new DOMDocument();

  // Load html with full tags all around.
  $dom
    ->loadHTML('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body>' . $html . '</body></html>');
  $xpath = new DOMXPath($dom);

  // Get all script tags that
  // are not inside of a textarea
  // have a src attribute.
  $nodes = $xpath
    ->query("//script[@src][not(ancestor::textarea)]");

  // Add the src attribute to dns-prefetch.
  foreach ($nodes as $node) {
    advagg_add_dns_prefetch($node->attributes
      ->getNamedItem('src')->nodeValue);
  }

  // Clear any errors.
  libxml_clear_errors();
}