JDocumentHTML: how to remove stylesheet?

826 views
Skip to first unread message

piotr_cz

unread,
Mar 19, 2013, 8:55:22 AM3/19/13
to Joomla! General Development, he...@piotr.cz
What is the recommended way to remove stylesheet from document, when
there's only one (template.css)?

case:
JDocumentHTML Object
(
[_stylesheets] => Array
(
['template.css'] => ...
)
)


Doesn't work:
- $headData = &$doc->getHeadData(); unset ($headData['styleSheets']
['template.css']);
- $doc->setHeadData(array('styleSheets') => $headData['styleSheets']);

- $doc->setHeadData(array('styleSheets') => array());

Above doesn't work because $headData['styleSheets'] becomes an empty
array:
https://github.com/joomla/joomla-cms/blob/master/libraries/joomla/document/html/html.php#L165


Works (but doesn't feel right):
- unset($doc->_styleSheets['template.css']);

r...@osdcs.com

unread,
Mar 19, 2013, 9:16:56 AM3/19/13
to joomla-de...@googlegroups.com
Well, this is how I unload mootools.
Don't know if it will help or not.

$document =& JFactory::getDocument();
$parameter_script = 'scripts';
$headerstuff=$document->getHeadData();
$key = '/media/system/js/mootools-core.js';
unset( $headerstuff['scripts'][$key] );
$document->setHeadData( $headerstuff );
--
You received this message because you are subscribed to the Google Groups
"Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at
http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.


Denis Ryabov

unread,
Mar 19, 2013, 9:49:49 AM3/19/13
to joomla-de...@googlegroups.com
> What is the recommended way to remove stylesheet from document, when
> there's only one (template.css)?
>
> Works (but doesn't feel right):
> - unset($doc->_styleSheets['template.css']);

Yes, the best way is to use $_styleSheets property as it's public even in Joomla!3.0.
Except of this one, I know 2 solutions only (I keep them in mind in the case Joomla! team would make $_styleSheets private):


1. Add "empty" css file using data-uri scheme:

$headData = $doc->getHeadData();
unset ($headData['styleSheets']['template.css']);
if(!count($headData['styleSheets']))
$headData['styleSheets'] = array('data:text/css,'=>array('mime'=>'text/css'));
$doc->setHeadData($headData);


2. Add a marker instead of filename and remove that link tag in onAfterRender event:

$headData = $doc->getHeadData();
unset($headData['styleSheets']['template.css']);
if(!count($headData['styleSheets']))
{
$headData['styleSheets'] = array('MY_MARKER_HERE'=>array('mime'=>'text/css'));
$app = JFactory::getApplication();
$app->registerEvent('onAfterRender', 'RemoveMarkerEvent');
}
$doc->setHeadData($headData);

class RemoveMarkerEvent extends JEvent
{
public function onAfterRender()
{
$doc = JFactory::getDocument();
$buffer = $doc->getBuffer('component');
$buffer = preg_replace('/<link [^>]*href="MY_MARKER_HERE"[^>]*>/s', '', $buffer);
$doc->setBuffer($buffer, 'component');
}
}

--
Best regards,
Denis Ryabov

piotr_cz

unread,
Mar 19, 2013, 10:57:05 AM3/19/13
to Joomla! General Development
r...: I think your method won't work in case when in '_scripts' array
there is only '..mootools.js'.

Denis: Thanks!

Folks, I think this behavior is buggy. We need to fix that for our
sake and to contribute to our children's better future.


I suggest replacing
$this->_styleSheets = (isset($data['styleSheets']) && !
empty($data['styleSheets'])) ? $data['styleSheets'] : $this-
>_styleSheets;

with
$this->_styleSheets = (isset($data['styleSheets']) && !
empty($data['styleSheets'])) ? $data['styleSheets'] : array();

in https://github.com/joomla/joomla-cms/blob/master/libraries/joomla/document/html/html.php#L165.

What do you think?
The reason is that at the moment 'setHeadData' method behaves kind
like 'merge', so there's no way how to remove head items (just add).

Denis Ryabov

unread,
Mar 19, 2013, 11:30:02 AM3/19/13
to joomla-de...@googlegroups.com
I think it's better to replace !empty by is_array, it should be more backward-compatible (most likely underlying idea is to don't pass full set of fields to setHeadData):

$this->_styleSheets = (isset($data['styleSheets']) && is_array($data['styleSheets'])) ? $data['styleSheets'] : $this-_styleSheets;

-------
Best regards,
Denis Ryabov

19.03.2013, 18:57, "piotr_cz" <pkoni...@hotmail.com>:
> r...: I think your method won't work in case when in '_scripts' array
> there is only '..mootools.js'.
>
> Denis: Thanks!
>
> Folks, I think this behavior is buggy. We need to fix that for our
> sake and to contribute to our children's better future.
>
> I suggest replacing
> $this->_styleSheets = (isset($data['styleSheets']) && !
> empty($data['styleSheets'])) ? $data['styleSheets'] : $this-
>
>> _styleSheets;
>
> with
> $this->_styleSheets = (isset($data['styleSheets']) && !
> empty($data['styleSheets'])) ? $data['styleSheets'] : array();
>
> in https://github.com/joomla/joomla-cms/blob/master/libraries/joomla/document/html/html.php#L165.
>
> What do you think?
> The reason is that at the moment 'setHeadData' method behaves kind
> like 'merge', so there's no way how to remove head items (just add).
>
> On Mar 19, 2:49О©╫pm, Denis Ryabov <drya...@yandex.ru> wrote:
>
>>> О©╫What is the recommended way to remove stylesheet from document, when
>>> О©╫there's only one (template.css)?
>>> О©╫Works (but doesn't feel right):
>>> О©╫- unset($doc->_styleSheets['template.css']);
>> О©╫Yes, the best way is to use $_styleSheets property as it's public even in Joomla!3.0.
>> О©╫Except of this one, I know 2 solutions only (I keep them in mind in the case Joomla! team would make $_styleSheets private):
>>
>> О©╫1. Add "empty" css file using data-uri scheme:
>>
>> О©╫$headData = $doc->getHeadData();
>> О©╫unset ($headData['styleSheets']['template.css']);
>> О©╫if(!count($headData['styleSheets']))
>> О©╫О©╫ О©╫ $headData['styleSheets'] = array('data:text/css,'=>array('mime'=>'text/css'));
>> О©╫$doc->setHeadData($headData);
>>
>> О©╫2. Add a marker instead of filename and remove that link tag in onAfterRender event:
>>
>> О©╫$headData = $doc->getHeadData();
>> О©╫unset($headData['styleSheets']['template.css']);
>> О©╫if(!count($headData['styleSheets']))
>> О©╫{
>> О©╫О©╫ О©╫ $headData['styleSheets'] = array('MY_MARKER_HERE'=>array('mime'=>'text/css'));
>> О©╫О©╫ О©╫ $app = JFactory::getApplication();
>> О©╫О©╫ О©╫ $app->registerEvent('onAfterRender', 'RemoveMarkerEvent');}
>>
>> О©╫$doc->setHeadData($headData);
>>
>> О©╫class RemoveMarkerEvent extends JEvent
>> О©╫{
>> О©╫О©╫ О©╫ public function onAfterRender()
>> О©╫О©╫ О©╫ {
>> О©╫О©╫ О©╫ О©╫ О©╫ $doc = JFactory::getDocument();
>> О©╫О©╫ О©╫ О©╫ О©╫ $buffer = $doc->getBuffer('component');
>> О©╫О©╫ О©╫ О©╫ О©╫ $buffer = preg_replace('/<link [^>]*href="MY_MARKER_HERE"[^>]*>/s', '', $buffer);
>> О©╫О©╫ О©╫ О©╫ О©╫ $doc->setBuffer($buffer, 'component');
>> О©╫О©╫ О©╫ }
>>
>> О©╫}
>>
>> О©╫--
>> О©╫Best regards,
>> О©╫Denis Ryabov

piotr_cz

unread,
Mar 19, 2013, 1:38:54 PM3/19/13
to Joomla! General Development
Thanks, this makes more sense.
I've submitted proposed fix to JBS
https://groups.google.com/group/joomlabugsquad/browse_frm/thread/82145b13d8e80f0a#

On Mar 19, 4:30 pm, Denis Ryabov <drya...@yandex.ru> wrote:
> I think it's better to replace !empty by is_array, it should be more backward-compatible (most likely underlying idea is to don't pass full set of fields to setHeadData):
>
> $this->_styleSheets = (isset($data['styleSheets']) && is_array($data['styleSheets'])) ? $data['styleSheets'] : $this-_styleSheets;
>
> -------
> Best regards,
> Denis Ryabov
>
> 19.03.2013, 18:57, "piotr_cz" <pkoniec...@hotmail.com>:
>
>
>
>
>
>
>
> > r...: I think your method won't work in case when in '_scripts' array
> > there is only '..mootools.js'.
>
> > Denis: Thanks!
>
> > Folks, I think this behavior is buggy. We need to fix that for our
> > sake and to contribute to our children's better future.
>
> > I suggest replacing
> > $this->_styleSheets = (isset($data['styleSheets']) && !
> > empty($data['styleSheets'])) ? $data['styleSheets'] : $this-
>
> >> _styleSheets;
>
> > with
> > $this->_styleSheets = (isset($data['styleSheets']) && !
> > empty($data['styleSheets'])) ? $data['styleSheets'] : array();
>
> > inhttps://github.com/joomla/joomla-cms/blob/master/libraries/joomla/doc....
>
> > What do you think?
> > The reason is that at the moment 'setHeadData' method behaves kind
> > like 'merge', so there's no way how to remove head items (just add).
>
> > On Mar 19, 2:49 pm, Denis Ryabov <drya...@yandex.ru> wrote:
>
> >>> What is the recommended way to remove stylesheet from document, when
> >>> there's only one (template.css)?
> >>> Works (but doesn't feel right):
> >>> - unset($doc->_styleSheets['template.css']);
> >> Yes, the best way is to use $_styleSheets property as it's public even in Joomla!3.0.
> >> Except of this one, I know 2 solutions only (I keep them in mind in the case Joomla! team would make $_styleSheets private):
>
> >> 1. Add "empty" css file using data-uri scheme:
>
> >> $headData = $doc->getHeadData();
> >> unset ($headData['styleSheets']['template.css']);
> >> if(!count($headData['styleSheets']))
> >> $headData['styleSheets'] = array('data:text/css,'=>array('mime'=>'text/css'));
> >> $doc->setHeadData($headData);
>
> >> 2. Add a marker instead of filename and remove that link tag in onAfterRender event:
>
> >> $headData = $doc->getHeadData();
> >> unset($headData['styleSheets']['template.css']);
> >> if(!count($headData['styleSheets']))
> >> {
> >> $headData['styleSheets'] = array('MY_MARKER_HERE'=>array('mime'=>'text/css'));
> >> $app = JFactory::getApplication();
> >> $app->registerEvent('onAfterRender', 'RemoveMarkerEvent');}
>
> >> $doc->setHeadData($headData);
>
> >> class RemoveMarkerEvent extends JEvent
> >> {
> >> public function onAfterRender()
> >> {
> >> $doc = JFactory::getDocument();
> >> $buffer = $doc->getBuffer('component');
> >> $buffer = preg_replace('/<link [^>]*href="MY_MARKER_HERE"[^>]*>/s', '', $buffer);
> >> $doc->setBuffer($buffer, 'component');
> >> }
>
> >> }
>
> >> --
> >> Best regards,
Reply all
Reply to author
Forward
0 new messages