1
1
mirror of https://github.com/ariya/phantomjs.git synced 2024-09-11 12:55:33 +03:00

Start to implement support for CoffeeScript.

This commit is contained in:
Ariya Hidayat 2011-03-02 00:41:23 -08:00
parent 3e60ae6b9b
commit 158362ed16
9 changed files with 138 additions and 1 deletions

View File

@ -22,6 +22,8 @@ Version 1.1.0
Added support for rasterizing as GIF image (Ariya Hidayat).
Added support for CoffeeScript (Ariya Hidayat).
2011-01-17: Version 1.0.0
Initial launch.

2
examples/hello.coffee Normal file
View File

@ -0,0 +1,2 @@
console.log 'Hello, world!'
phantom.exit()

7
examples/tweets.coffee Normal file
View File

@ -0,0 +1,7 @@
if phantom.state.length == 0
phantom.state = 'tweets'
phantom.open 'http://mobile.twitter.com/SenchaInc'
else
list = document.querySelectorAll 'span.status'
console.log i.innerHTML.replace(/<.*?>/g, '') for i in list
phantom.exit()

8
src/coffee-script.js Normal file

File diff suppressed because one or more lines are too long

60
src/csconverter.cpp Normal file
View File

@ -0,0 +1,60 @@
/*
This file is part of the PhantomJS project from Ofi Labs.
Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "csconverter.h"
#include <iostream>
#include <QFile>
#include <QWebFrame>
#include <QDebug>
CSConverter::CSConverter(QObject *parent)
: QObject(parent)
{
QFile file(":/coffee-script.js");
if (!file.open(QFile::ReadOnly)) {
std::cerr << "CoffeeScript compiler is not available!" << std::endl << std::endl;
exit(1);
}
QString script = QString::fromUtf8(file.readAll());
file.close();
m_webPage.mainFrame()->evaluateJavaScript(script);
m_webPage.mainFrame()->addToJavaScriptWindowObject("converter", this);
}
QString CSConverter::convert(const QString &script)
{
setProperty("source", script);
QWebFrame *frame = m_webPage.mainFrame();
QVariant result = frame->evaluateJavaScript("this.CoffeeScript.compile(converter.source)");
if (result.type() == QVariant::String)
return result.toString();
return QString();
}

47
src/csconverter.h Normal file
View File

@ -0,0 +1,47 @@
/*
This file is part of the PhantomJS project from Ofi Labs.
Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CSCONVERTER_H
#define CSCONVERTER_H
#include <QObject>
#include <QWebPage>
class CSConverter: public QObject
{
public:
CSConverter(QObject *parent = 0);
QString convert(const QString &script);
private:
QWebPage m_webPage;
};
#endif // CSCONVERTER_H

View File

@ -33,6 +33,7 @@
#include <iostream>
#include <gifwriter.h>
#include "csconverter.h"
#if QT_VERSION < QT_VERSION_CHECK(4, 5, 0)
#error Use Qt 4.5 or later version
@ -169,11 +170,13 @@ private:
int m_returnValue;
QString m_script;
QString m_state;
CSConverter *m_converter;
};
Phantom::Phantom(QObject *parent)
: QObject(parent)
, m_returnValue(0)
, m_converter(0)
{
QPalette palette = m_page.palette();
palette.setBrush(QPalette::Base, Qt::transparent);
@ -298,6 +301,12 @@ bool Phantom::execute()
m_script.prepend("//");
}
if (m_scriptFile.endsWith(".coffee")) {
if (!m_converter)
m_converter = new CSConverter(this);
m_script = m_converter->convert(m_script);
}
m_page.mainFrame()->evaluateJavaScript(m_script);
return true;
}

View File

@ -1,7 +1,8 @@
TEMPLATE = app
TARGET = phantomjs
DESTDIR = ../bin
SOURCES = phantomjs.cpp
HEADERS += csconverter.h
SOURCES = phantomjs.cpp csconverter.cpp
RESOURCES = phantomjs.qrc
QT += network webkit
CONFIG += console

View File

@ -1,5 +1,6 @@
<RCC>
<qresource prefix="/">
<file>phantomjs-icon.png</file>
<file>coffee-script.js</file>
</qresource>
</RCC>