Document.php 23.4 KB
Newer Older
Prosper De Laure's avatar
Prosper De Laure committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
/*
 * Copyright (C) 2015 Maarch
 *
 * This file is part of dependency html.
 *
 * Dependency html is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Dependency html is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with dependency html.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace dependency\html;

/**
 * Represents an entire HTML document; serves as the root of the document tree.
 *
 * @package Dependency\Html
 * @author  Cyril VAZQUEZ <cyril.vazquez@maarch.org>
 */
class Document extends \dependency\xml\Document
{
    use IncludeTrait,
        LocalisationTrait;

    /* Constants */
    /* -------------------------------------------------------------------------
    - Properties
    ------------------------------------------------------------------------- */
    protected $layout;
    protected $classes;
40
    public $plugins;
41
42
    protected $headers;
    protected $layoutData;
Prosper De Laure's avatar
Prosper De Laure committed
43
44
45
    public $XPath;
    public $translator;
    public $dateTimeFormatter;
46
    public $pluginsParameters = [];
Prosper De Laure's avatar
Prosper De Laure committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
    /**
     *   -- document --
     *   <html>
     *       <head>
     *       <body>
     *   -- body --
     *       -- layout --
     *           <main> or <* role="main">
     */
    /* -------------------------------------------------------------------------
    - Methods
    ------------------------------------------------------------------------- */
    /*************************************************************************/
    /* DOM Document override
    /*************************************************************************/
    /**
     * Construct a new Html document
     * @param string $layout            The uri to a resource for the default layout to use
     * @param string $layoutData        The uri to a service method to merge with the layout
     * @param array  $extensions        An associative array of Html node type+name and Php classes that will extend it
     * @param array  $plugins           An associative array of Html classes and Php classes that will be automatically plugged-in
     * @param array  $headers           Uris of html ressources that will be imported into the <head> tag
     * @param object $translator        The localisation/translator object to translate the texts of the document
     * @param object $dateTimeFormatter The localisation/dateTimeFormatter object to format the dates of the document
     *
     * @return void
     **/
    public function __construct($layout = null, $layoutData = null, $extensions = null, $plugins = null, $headers = null, \dependency\localisation\TranslatorInterface $translator = null, \dependency\localisation\DateTimeFormatter $dateTimeFormatter = null)
    {
        parent::__construct(null, null, $extensions);
        $this->formatOutput = true;

        // Register node classes for Html
        $this->registerNodeClass('DOMElement', '\dependency\html\Element');
        $this->registerNodeClass('DOMDocument', '\dependency\html\Document');
        $this->registerNodeClass('DOMDocumentFragment', '\dependency\html\DocumentFragment');
        //$this->registerNodeClass('DOMProcessingInstruction'   , '\dependency\html\ProcessingInstruction');

        // Keep plugins and classes (extensions are already managed by Xml\Document)
        $this->plugins = $plugins;
        $this->headers = $headers;
        $this->layout = $layout;
        $this->layoutData = $layoutData;

        $this->translator = $translator;
        $this->dateTimeFormatter = $dateTimeFormatter;

        $this->XPath = new XPath($this);

        if ($this->layoutRequested()) {
            $this->addHtmlStructure();

            $this->useLayout();
        } else {
            $container = $this->createElement('div');
            $this->appendChild($container);
        }

    }

    /**
     * Check if layout and headers requested
     * @return bool
     */
    public function layoutRequested()
    {
        // Verbosely request layout
        if (isset($_REQUEST['HTML_USE_LAYOUT'])) {
            return true;
        }

        // Verbosely refuse layout
        if (isset($_REQUEST['HTML_NO_LAYOUT'])) {
            return false;
        }

        // Ajax call
        if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest") {
            return false;
        }

        // Not an ajax call
        if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || $_SERVER['HTTP_X_REQUESTED_WITH'] != "XMLHttpRequest") {
            return true;
        }

        // Ajax call but referer has requested layout so no need to send it again
        if (isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'HTML_USE_LAYOUT')) {
            return false;
        }

        // No referer : refresh or first request
        if (!isset($_SERVER['HTTP_REFERER'])) {
            return true;
        }

        if (isset($_SERVER['SCRIPT_URL']) && $_SERVER['SCRIPT_URL'] == '/') {
            return true;
        }

        if (isset($_SERVER['HTTP_HOST'])) {
            if ($_SERVER['HTTP_REFERER'] == $_SERVER['SCRIPT_URI']) {
                //return true;
            }

            //http://<host>/uri?...
            $parts = parse_url($_SERVER['HTTP_REFERER']);

            if ($parts["host"] != $_SERVER['HTTP_HOST']) {
                //return true;
            }
        }

        return false;
    }

    /**
     * Load an html string
     * @param string  $html
     * @param integer $options
     */
    public function loadHtml($html, $options = null)
    {
        parent::loadHtml($html);

        $this->XPath = new XPath($this);
    }

    /**
     * Save the entire Html document or the given node into a Html string; also resolves the plugins to automatically insert necessary data
     * @param mixed $node The html node to save or null to save entire document
     *
     * @return string The Html string
     */
    public function saveHtml($node = null)
    {
        // Deploy js of plugins
        $this->savePlugins($node);

        // All document is to save
        if (!$node) {
            // If html document, save and add HTML 5 declaration
            if ($this->documentElement->tagName == 'html') {
                $html = '<!DOCTYPE html>'.parent::saveHtml();
            } else {
                $html = parent::saveHtml($this->documentElement);
            }
        } else {
            $html = parent::saveHtml($node);
        }

        return $html;
    }

    /*************************************************************************/
    /* DOM Shortcuts
    /*************************************************************************/
    /**
     * Set default document structure
     * @access protected
     */
    protected function addHtmlStructure()
    {
        $html = $this->createElement('html');
        $this->appendChild($html);

        $head = $this->createElement('head');
        $this->documentElement->appendChild($head);

        $body = $this->createElement('body');
        $this->documentElement->appendChild($body);
    }

    /**
     * Insert header contents onto the <head> tag of the document
     * @param array $headers Uris of html head content ressources or null to use default headers declared at consruction
     */
    public function addHeaders(array $headers = null)
    {
        if ($this->documentElement->tagName == 'div') {
            $this->removeChild($this->documentElement);
            $this->addHtmlStructure();
        }

        $head = $this->getHead();

        if ($headers) {
            foreach ($headers as $header) {
                $this->addContentFile($header, $head);
            }
        } else {
            foreach ((array) $this->headers as $header) {
                $this->addContentFile($header, $head);
            }
        }

        // add css
244
        $this->addStyle("/public/css/bootstrap-toggle/bootstrap-toggle.css");
245
        $this->addStyle("/public/css/bootstrap-datetimepicker/bootstrap-datetimepicker.css");
Prosper De Laure's avatar
Prosper De Laure committed
246
247

        // Add js scripts
248
249
250
        $this->addScript("/public/js/jQuery_1.11.0/jQuery.js");
        $this->addScript("/public/js/jQueryUI_1.12.1/jquery-ui.min.js");
        $this->addScript("/public/js/jQueryUI_touch-punch_0.2.3/jquery.ui.touch-punch.js");
Prosper De Laure's avatar
Prosper De Laure committed
251
        //less compiler
252
        $this->addScript("/public/js/less_1.7.0/less.js");
Prosper De Laure's avatar
Prosper De Laure committed
253
        //bootstrap.affix
254
        $this->addScript("/public/js/bootstrap_3.1.1/affix.js");
Prosper De Laure's avatar
Prosper De Laure committed
255
        //bootstrap.alert
256
        $this->addScript("/public/js/bootstrap_3.1.1/alert.js");
Prosper De Laure's avatar
Prosper De Laure committed
257
        //bootstrap.button
258
        $this->addScript("/public/js/bootstrap_3.1.1/button.js");
Prosper De Laure's avatar
Prosper De Laure committed
259
        //bootstrap.carousel
260
        $this->addScript("/public/js/bootstrap_3.1.1/carousel.js");
Prosper De Laure's avatar
Prosper De Laure committed
261
        //bootstrap.collapse
262
        $this->addScript("/public/js/bootstrap_3.1.1/collapse.js");
Prosper De Laure's avatar
Prosper De Laure committed
263
        //bootstrap.dropdown
264
        $this->addScript("/public/js/bootstrap_3.1.1/dropdown.js");
Prosper De Laure's avatar
Prosper De Laure committed
265
        //bootstrap.modal
266
        $this->addScript("/public/js/bootstrap_3.1.1/modal.js");
Prosper De Laure's avatar
Prosper De Laure committed
267
        //bootstrap.tooltip
268
        $this->addScript("/public/js/bootstrap_3.1.1/tooltip.js");
Prosper De Laure's avatar
Prosper De Laure committed
269
        //bootstrap.popover
270
        $this->addScript("/public/js/bootstrap_3.1.1/popover.js");
Prosper De Laure's avatar
Prosper De Laure committed
271
        //bootstrap.scrollspy
272
        $this->addScript("/public/js/bootstrap_3.1.1/scrollspy.js");
Prosper De Laure's avatar
Prosper De Laure committed
273
        //bootstrap.tab
274
        $this->addScript("/public/js/bootstrap_3.1.1/tab.js");
Prosper De Laure's avatar
Prosper De Laure committed
275
        //bootstrap.transition
276
        $this->addScript("/public/js/bootstrap_3.1.1/transition.js");
Prosper De Laure's avatar
Prosper De Laure committed
277
        //metisMenu
278
        $this->addScript("/public/js/metisMenu_1.0.1/metisMenu.js");
Prosper De Laure's avatar
Prosper De Laure committed
279
        //dataForm
280
        $this->addScript("/public/js/dataForm_0.0.1/dataForm.js");
Prosper De Laure's avatar
Prosper De Laure committed
281
282

        //gritter
283
284
        $this->addScript("/public/js/gritter_1.7.4/gritter.min.js");
        $this->addScript("/public/js/gritter_1.7.4/gritter.js");
Prosper De Laure's avatar
Prosper De Laure committed
285
286

        //typeahead
Alexandre Morin's avatar
Alexandre Morin committed
287
        $this->addScript("/public/js/typeahead_0.11.1/typeahead.js");
Prosper De Laure's avatar
Prosper De Laure committed
288

289
        $this->addScript("/public/js/konami-code/jquery.raptorize.1.0.js");
Prosper De Laure's avatar
Prosper De Laure committed
290

291
292
        $this->addScript("/public/js/bootstrap-tree/bootstrap-tree.js");
        $this->addScript("/public/js/dataList_0.0.1/dataList.js");
293
294
        $this->addScript("/public/js/datePicker/bootstrap-datepicker.js");

Cyril Vazquez's avatar
Cyril Vazquez committed
295

296
297
298
        $this->addScript("/public/js/moment_2.14.1/moment.js");
        $this->addScript("/public/js/dateTimePicker/bootstrap-datetimepicker.js");

299
300
        $this->addScript("/public/js/csrf/csrfprotector.js");

301
        //$this->addScript("/public/js/webodf.js-0.5.8/webodf.js");
Prosper De Laure's avatar
Prosper De Laure committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339

    }

    /**
     * Deploy a layout on the empty document
     * @param string $layout     The uri of a resource for the layout html content or null to use the default layout declared at construction
     * @param string $layoutData Some data to merge with layout
     */
    public function useLayout($layout=false, $layoutData = null)
    {
        $this->addHeaders();

        if (!$layout) {
            $layout = $this->layout;
        }
        if (!$layout) {
            return;
        }

        if ($this->documentElement->tagName == 'div') {
            $this->removeChild($this->documentElement);
            $this->addHtmlStructure();
        }

        $body = $this->getBody();
        $layoutFragment = $this->createDocumentFragment();
        $layoutFragment->appendHtmlFile($layout);

        $body->appendChild($layoutFragment);

        if (!$layoutData) {
            if ($this->layoutData) {
                $methodRoute = new \core\Route\MethodRouter($this->layoutData);
                $layoutService = $methodRoute->service->call();
                $layoutData = $methodRoute->method->call($layoutService);
            }
        }

340
341
        $layoutData->localisation = \laabs::configuration()['dependency.localisation']->getArrayCopy();

Prosper De Laure's avatar
Prosper De Laure committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
        if ($layoutData) {
            $this->merge($this, $layoutData);
        }
    }

    /**
     * Add html content to the document. If no container is provided as argument, the method will guess where to append the html
     * @param string $content   The html content to add
     * @param object $container An html node to append the content. If ignored, the method will search for the best place to append content to
     *
     * @return object The first node appended to the container
     */
    public function addContent($content, $container = false)
    {
        $contentFragment = $this->createDocumentFragment();
        $contentFragment->appendHtml($content);
        if (!$container) {
            $container = $this->getContainer();
        }

        return $container->appendChild($contentFragment);
    }

    /**
     * Add a resource content to the document. If no container is provided as argument, the method will guess where to append the html
     * @param string $contentResource The uri to a resource filr holding the html content to add
     * @param object $container       An html node to append the content. If ignored, the method will search for the best place to append content to
     *
     * @return object The first node appended to the container
     */
    public function addContentFile($contentResource, $container = false)
    {
        $contentFragment = $this->createDocumentFragment();
        $contentFragment->appendHtmlFile($contentResource);

        if (!$container) {
            $container = $this->getContainer();
        }

        return $container->appendChild($contentFragment);
    }

    /**
     * Get the contents of the view
     * @return DOMNodeList
     */
    public function getContents()
    {
        $container = $this->getContainer();

        if ($container) {
            return $container->childNodes;
        }
    }

    /*************************************************************************/
    /* HTML plugins management
    /*************************************************************************/
    /**
     * Add the plugins to a html tag
     * @param object $node An html node to add plugins to
     */
    public function addPlugins($node = null)
    {
        $elements = $this->XPath->query("descendant-or-self::*[@class]", $node);
        foreach ($elements as $element) {
408
            $element->addPlugins();
Prosper De Laure's avatar
Prosper De Laure committed
409
410
411
412
413
414
415
416
417
418
419
        }
    }

    /**
     * Save the plugins of a html tag
     * @param object $node An html node to save plugins of
     */
    public function savePlugins($node = null)
    {
        $elements = $this->XPath->query("descendant-or-self::*[@class]", $node);
        foreach ($elements as $element) {
420
            $element->savePlugins();
Prosper De Laure's avatar
Prosper De Laure committed
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
        }
    }

    /**
     * Get the plugins of a given html class for a given html node
     * @param string $class The class name the plugins are linked with
     * @param object $node  An html node to get plugins
     *
     * @return array The plugin objects
     */
    public function getPlugins($class, $node = null)
    {
        $elements = $this->getElementsByClass($class, $node);
        $plugins = array();
        foreach ($elements as $element) {
            $plugins[] = $element->plugin[$class];
        }

        return $plugins;
    }

    /*-------------------------------------------------------------------------
    - get Methods
    ------------------------------------------------------------------------- */
    /**
     * Get a node with given id attribute
     * @param string $elementId The id attribute
     * @param object $node      The html node for relative search
     *
     * @return The element or false
     */
    public function getElementById($elementId, $node = null)
    {
        if ($nodeList = $this->XPath->query("descendant-or-self::*[@id='$elementId']", $node)) {
            return $nodeList->item(0);
        }
    }

    /**
     * Get a node list of elements having the given name attribute
     * @param string $name The name attribute value
     * @param object $node The html node for relative search
     *
     * @return The node list of elements
     */
    public function getElementsByName($name, $node = null)
    {
        return $this->XPath->query("descendant-or-self::*[@name='$name']", $node);
    }

    /**
     * Get a node list of elements having the given class attribute
     * @param string $class The class attribute value
     * @param object $node  The html node for relative search
     *
     * @return The node list of elements
     */
    public function getElementsByClass($class, $node = null)
    {
        return $this->XPath->query("descendant-or-self::*[contains(concat(' ', normalize-space(@class), ' '), ' $class ')]", $node);
    }

    /**
     * Get a node list of elements having the given role attribute
     * @param string $role The role attribute value
     * @param object $node The html node for relative search
     *
     * @return The node list of elements
     */
    public function getElementsByRole($role, $node = null)
    {
        return $this->XPath->query("descendant-or-self::*[@role='$role']", $node);
    }

    /**
     * Retrieve the <head> element of the document
     * @return The head element
     */
    public function getHead()
    {
        $head = $this->getElementsByTagName('head')->item(0);

        return $head;
    }

    /**
     * Retrieve the <body> element of the document
     * @return The body element
     */
    public function getBody()
    {
        return $this->getElementsByTagName('body')->item(0);
    }

    /**
     * Retrieve the main container element of the document
     *
     * @return The main container element
     */
    public function getContainer()
    {
        if ($mainElement = $this->getElementsByTagName("main")->item(0)) {
            return $mainElement;
        }
        if ($mainRole = $this->getElementsByRole("main")->item(0)) {
            return $mainRole;
        }
        if ($containerClass = $this->getElementsByClass("container")->item(0)) {
            return $containerClass;
        }

        if ($body = $this->getBody()) {
            return $body;
        }

        return $this->documentElement;
    }

    /**
     * Get the script element by its src attribute
     * @param string $src The src of script
     *
     * @return \DOMElement
     */
    public function getScript($src)
    {
        $script = $this->XPath->query("//script[@src='$src']")->item(0);

        return $script;
    }

    /**
     * Add all package scripts
     * @param string $package The directory
     *
     * @return array The script elements
     */
    public function addScriptPackage($package)
    {
        $elements = array();

        $webdir = "..".DIRECTORY_SEPARATOR.LAABS_WEB.DIRECTORY_SEPARATOR;
        $srcdir = $webdir.str_replace(LAABS_URI_SEPARATOR, DIRECTORY_SEPARATOR, $package);
        if (is_dir($srcdir)) {
            foreach (glob($srcdir.DIRECTORY_SEPARATOR."*.js") as $srcfile) {
                $src = str_replace(DIRECTORY_SEPARATOR, LAABS_URI_SEPARATOR, substr($srcfile, strlen($webdir)));
                $elements[] = $this->addScript($src);
            }
        }

        return $elements;
    }

    /**
     * Add a script element if not already included
     * @param string $src The src of script
     *
     * @return \DOMElement The created or already included element
     */
    public function addScript($src)
    {
        if (!($script = $this->getScript($src))) {

            $script = $this->createElement('script');
            $script->setAttribute('src', $src);
            $script->setAttribute('type', 'text/javascript');
            $script->setAttribute('data-auto', '1');

            $this->appendScript($script);
        }

        return $script;
    }

    protected function appendScript($script)
    {
        // Parent is either <head> or container
        if ($head = $this->getHead()) {
            // previous sibling is the last auto-append script OR last meta
            $autos = $head->getElementsByTagName('script');
            $metas = $head->getElementsByTagName('meta');
            if ($autos->length) {
                $lastAuto = $autos->item($autos->length-1);
                $head->insertBefore($script, $lastAuto->nextSibling);
            } elseif ($metas->length) {
                $lastMeta = $metas->item($metas->length-1);
                if ($lastMeta->nextSibling) {
                    $head->insertBefore($script, $lastMeta->nextSibling);
                } else {
                    $head->appendChild($script);
                }
            } else {
                $head->appendChild($script);
            }
        } else {
            $container = $this->getContainer();
            $container->appendChild($script);
        }
    }

    /**
     * Add a script element from source
     * @param string $src The src of script
     *
     * @return \DOMElement The created or already included element
     */
    public function addScriptSrc($src)
    {
        $script = $this->createElement('script');
        $script->setAttribute('type', 'text/javascript');

        $CDATASection = $this->createCDATASection($src);
        $script->appendChild($CDATASection);

        $this->appendScript($script);
    }

    /**
     * Get the style element by its src attribute
     * @param string $href The href of style
     *
     * @return \DOMElement
     */
    public function getStyle($href)
    {
        $style = $this->XPath->query("//style[@href='$href']")->item(0);

        return $style;
    }

    /**
     * Add all package styles
     * @param string $package The directory
     *
     * @return array The style link elements
     */
    public function addStylePackage($package)
    {
        $elements = array();

        $webdir = "..".DIRECTORY_SEPARATOR.LAABS_WEB.DIRECTORY_SEPARATOR;
        $srcdir = $webdir.str_replace(LAABS_URI_SEPARATOR, DIRECTORY_SEPARATOR, $package);
        if (is_dir($srcdir)) {
            foreach (glob($srcdir.DIRECTORY_SEPARATOR."*.css") as $srcfile) {
                $src = str_replace(DIRECTORY_SEPARATOR, LAABS_URI_SEPARATOR, substr($srcfile, strlen($webdir)));
                $elements[] = $this->addStyle($src);
            }
        }

        return $elements;
    }

    /**
     * Add a script element if not already included
     * @param string $href The href of script
     *
     * @return \DOMElement The created or already included element
     */
    public function addStyle($href)
    {
        if (!($style = $this->getStyle($href))) {

            $style = $this->createElement('link');
            $style->setAttribute('href', $href);
            $style->setAttribute('rel', 'stylesheet');
            $style->setAttribute('data-auto', '1');

            $parent = $this->getHead();

            if (!$parent) {
                $parent = $this->getContainer();
            }

            $autos = $this->XPath->query('./link[@data-auto]', $parent);
            if ($autos->length) {
                $lastAuto = $autos->item($autos->length-1);
                $parent->insertBefore($style, $lastAuto->nextSibling);

                return $style;
            }

            $metas = $this->XPath->query('./meta', $parent);
            if ($metas->length) {
                $lastMeta = $metas->item($metas->length-1);
                if ($lastMeta->nextSibling) {
                    $parent->insertBefore($style, $lastMeta->nextSibling);
                } else {
                    $parent->appendChild($style);
                }
            } else {
                $parent->appendChild($style);
            }
        }

        return $style;
    }
}