Laravel dom crawler search img in javascript - php

I have part of html content:
$("table:first").after("<img src=\"https://example.com/images/banners/image.png\" alt=\"Example\" width=\"88px\" style=\"display: block; margin-left: auto; margin-right: auto; \">");
I need search img and attr src with value https://example.com/images/banners/image.png.
I do:
$html = $content ? new Crawler($content) : [];
$res = false;
foreach ($html->filter('img')->previousAll() as $element) {
if (in_array($element->attr('src'), [asset('images/banners/image.png')])) {
$res = true;
} else {
$res = false;
}
}
But I get false. Why? If img is not inside in jQuery, then everything is working. But if img is in jquery then how can I get true?

It looks like the symfony/dom-crawler package doesn't support Javascript. It only loads static html and xml documents, and doesn't load and render Javascript like a browser would. It looks like fabpot/goutte or symfony/panther are packages that you can use together with dom-crawler to crawl sites where you want the Javascript to be loaded and run. They use BrowserKit in the background which simulates a browser.

Related

How to compile SCSS with scssphp

I'm currently trying to compile some SCSS which I'm receiving via a form request. The current workflow is that the user submits form data as follows:
{"$background_color":"#f3f3f3","$logo_url":"https:\/\/logo.co\/random"}
I then transform this input to the following:
$background_color: '#f3f3f3';
$logo_url: 'https://logo.co/random'
This should be valid for the purposes of compiling it to CSS so I run it through SCSSPHP with the following:
$scss->addImportPath(Storage::disk('tmp'));
$output = $scss->compile("#import 'test'; $statement ");
No errors are triggered when I run this and the output is:
#import 'test';
My test.scss is as follows:
$background_colour: 'red'
$logo_url: 'https://test.com'
.logo {
background-image: $logo_url;
}
.background_colour {
background-colour: $background_colour;
}
What am I doing wrong here?
The problem is that you're not passing a string to this function.
Parse your scss as a string by using the function file_get_contents
require_once "scssphp/scss.inc.php";
use Leafo\ScssPhp\Compiler;
$scss = new Compiler();
// Gets the content of the scss file in string format
$scss_string = file_get_contents(path/to/scss.file);
echo $scss->compile($scss_string);

create css with php laravel

i need create get css files with laraven in my controller, because i have one problem my return result is one normal file (text file) and not a css file
my routes.php file contain:
Route::get('/css/{css}.css', 'FileController#GetCss')
->where('css','[A-Za-z]+');
FileController.php:
<?php
class FileController extends \BaseController {
public function GetCss($css)
{
$file = File::get(app_path()."\css\\".$css.".css");
return $file;
}
}
and my css.css file contain:
body
{
background: #f00;
}
browser return is:
body{ background: #f00; }
and source (inspect element / ctrl+u):
body
{
background: #f00;
}
what I must do to work as a css file?
Your question need more clarification, I guess you are trying to build a CDN or something like that.
When you say "what I must do to work as a css file?", I think you need to specify a header for the response:
public function GetCss($css)
{
$file = File::get(app_path()."\css\\".$css.".css");
$response = Response::make($file);
$response->header('Content-Type', "text/css");
return $response;
}

CSS minify with PHP extension

How can I minify a .php file with CSS contents?
Currently I get a 400 error.
Normally I call minify like this
<link rel="stylesheet" type="text/css"
href="{$workspace}/min/f=workspace/css/common.css" />
EDIT
The answer is changing the minify source code, but what is the change I should make?
In other words.. this call should work and process as CSS..
<link rel="stylesheet" type="text/css"
href="{$workspace}/min/f=workspace/css/common.php" />
Maybe with an optional declaration?
<link rel="stylesheet" type="text/css"
href="{$workspace}/min/f=workspace/css/common.php&type=css" />
EDIT
I created the project here # https://github.com/into/less-less
Your CSS+PHP script outputs CSS only after it's requested from a server and parsed by PHP. Minify reads files directly from the server, skipping the HTTP request. So I see two paths:
Less optimal [?]: make minify download the CSS like this:
<link rel="stylesheet" type="text/css" href="{$workspace}/min/f=http://site.com/workspace/css/common.php" />
Include Minify lib in your common.php file and use its classes (e.g. Minify_CSS) before output. Something like echo Minify_CSS::minify($css)
Update:
Your example repo contains a strange file name which wouldn't let me pull/push appropriately, so here's the changed report.php:
<pre>
<strong>LESS in</strong>
<?= file_get_contents('workspace/less/common.less') ?>
- - - - -
<strong>CSS out</strong>
<?
require 'workspace/php/lessc.inc.php';
$lc = new lessc();
$contents = file_get_contents( 'workspace/less/common.less' );
$css = $lc->parse( $contents );
echo $css;
?>
<strong>Minified</strong>
<?php
require 'workspace/min/lib/Minify/CSS/Compressor.php';
echo Minify_CSS_Compressor::process($css);
?>
</pre>
No, you can't easily do it as minify heavily depends on file extensions (css,js,?). For example it is used to determine what HTTP headers send to client(application/x-javascript,text/css,?), what minifier class to use, is this file safe to parse etc.
But I'm almost certain that this situation can be avoided. Could you please describe why exactly you want to do this?
If you insist on doing it this way I can propose a few dirty hacks to make it work, but it requires changing minify's source code so I don't really know if that is a good idea.
Upd:
There is no nice way to change this source: it has really bad structure. In minify v2.1.3 you can simply change the following:
Path: lib/Minify/Controller/Base.php##Minify_Controller_Base::_fileIsSafe()
return in_array(strrev($revExt), array('js', 'css', 'html', 'txt'));
-->
return in_array(strrev($revExt), array('js', 'css', 'html', 'txt', 'php'));
Path: lib/Minify/Controller/MinApp.php##Minify_Controller_MinApp::setupSources()
preg_match('/^[^,]+\\.(css|js)(?:,[^,]+\\.\\1)*$/', $_GET['f'])
-->
preg_match('/^[^,]+\\.(css|js|php)(?:,[^,]+\\.\\1)*$/', $_GET['f'])
Path: lib/Minify/##Minify_Source::__construct()
case 'css' : $this->contentType = 'text/css';
-->
case 'php': case 'css': $this->contentType = 'text/css';
and everything will work, but you must set $min_serveOptions['minApp']['allowDirs'] in configuration carefully as any user may be able to view any php file from this directories.
Using CSS Min, you are freely could do whatsoever and you could also "processing" your stlyesheet in php script, then minify it on the fly : its DEAD simple to do that, and guess what, it just A SINGLE FILE.
Another way is, dont use any PHP script to process or doing some logical at your css file, instead, you could have separated small css file then you just load whatever you want by building new cache file or just combine and output the link tag.
However, if you are now have something like this in your common.php (php file/script that outputing the css, yes?)
<?php
$style = '';
$bodyStyle = 'body {
background-color: #000;
margin: 40px;
font: 13px/20px normal Helvetica, Arial, sans-serif;
color: #fff;
}';
// I assumed you are about proccesing something here..
// ...
// Then you merged all style into one string, and output it as css file
$style = $bodyStyle + $otherStyle + $whateverStyle;
header('Content-Type: text/css');
echo $style;
?>
And you still want to make your app bloated and make your code more unreadable (wait, there still more...), also want to modify Minify class/lib to minify and cache you pseudo-css-php, then you need to "hacking" the source as follow :
lib/Minify/Controller/Base.php : 135, change to :
return in_array(strrev($revExt), array('js', 'css', 'html', 'txt', 'php'));
lib/Minify/Controller/MinApp.php : 75, change to :
! preg_match('/^[^,]+\\.(css|js|php)(?:,[^,]+\\.\\1)*$/', $_GET['f'])
lib/Minify/Source.php, change several things :
Add one variable as a PHP flag, in , after line 41 perhaps
/**
* #var bool
*/
public $isPHP = FALSE;
In same file, at line : 67, add a condition :
case 'php' : $this->isPHP = TRUE;
$this->contentType = 'text/css';
break;
Last, replace getContent() function, into :
public function getContent()
{
if($this->isPHP)
{
include($this->filepath);
}
else
{
$content = (null !== $this->filepath)
? file_get_contents($this->filepath)
: ((null !== $this->_content)
? $this->_content
: call_user_func($this->_getContentFunc, $this->_id)
);
}
// remove UTF-8 BOM if present
return (pack("CCC",0xef,0xbb,0xbf) === substr($content, 0, 3))
? substr($content, 3)
: $content;
}
You also need to change your common.php into Minify spec, so now, your common.php should looks like :
You need to put all your stylesheet as string and assign it into $content variable
<?php
//$style = '';
$bodyStyle = 'body {
background-color: #000;
margin: 40px;
font: 13px/20px normal Helvetica, Arial, sans-serif;
color: #fff;
}';
// I assumed you are about proccesing something here..
// ...
// Then you merged all style into one string, and output it as css file
// $style = $bodyStyle + $otherStyle + $whateverStyle;
// header('Content-Type: text/css');
// echo $style;
$content = $bodyStyle + $otherStyle + $whateverStyle;
?>
Yes, there is one and it works pretty well:
https://github.com/c9s/pecl-cssmin
The API is pretty simple:
<?php
echo cssmin("body { .... } .rule { } .rule2 { color: #fff; }");

How do i run a js function from an iframe in the main frame?

In my iframe i have a function:
function CheckDebug(){
if($('#debug').length > 0){
debugConsole.debugOn = true;
debugConsole.IP = "<?=$_SERVER['REMOTE_ADDR']?>";
debugConsole.log('START DEBUG MODE');
}
}
How do i run that function in the main frame so that it affects the iframe's debugConsole object?
To access the parent iframe use window.parent, you can call:
window.parent
So you would do:
window.parent.debugConsole(..);
as an example.
To have the parent access the iframe, you can do the following:
document.getElementById('frameId').contentWindow.funcThatLivesInIframe();
where the iframe has an id attribute with the value 'frameId'.
Just access the "top" context:
function CheckDebug(){
if (top.$('#debug').length > 0){
top.debugConsole.debugOn = true;
top.debugConsole.IP = "<?=$_SERVER['REMOTE_ADDR']?>";
top.debugConsole.log('START DEBUG MODE');
}
}
Now that's for the top-most window. If you just want to go up one level, you can look at "window.parent."

Change relative path to full in css

I am using simple html dom to extract data from a website and pharse it. I cannot however change one of the realative paths in the style tag to a full one. I have tried many combinations.
I found a post here to use a PEAR script with simple html dom and it has worked on all links except below.
require_once 'includes/URL2.php';
$uri = new Net_URL2('http://www.stormcinemas.ie'); // URI of the resource
$baseURI = $uri;
foreach ($htmlcss->find('background[url]') as $elem) {
$elem->url = $baseURI->resolve($elem->url)->__toString();
}
foreach ($html->find('*[src]') as $elem) {
$elem->src = $baseURI->resolve($elem->src)->__toString();
}
foreach ($html->find('*[href]') as $elem) {
if (strtoupper($elem->tag) === 'BASE') continue;
$elem->href = $baseURI->resolve($elem->href)->__toString();
}
foreach ($html->find('form[action]') as $elem) {
$elem->action = $baseURI->resolve($elem->action)->__toString();
}
style.css
<style>
div.spriteImgSmall { background: url(/images/css_sprites/film_sprites/smallimages_sprite.jpg); }
</style>
Thanks
The solution was provided here but was deleted unfortunately. Thanks again, it actualy did solve my question.
Here it is for future ref.
$htmlcss = preg_replace('/url\(\s*[\'"]?\/?(.+?)[\'"]?\s*\)/i', 'url('.
$baseURI.'/$1)', $htmlcss);
I would still be interested if someone know's how to use simple html dom on css as there is nothing anywhere on the net. It may not even be possible.

Categories