Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for RequireJS #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ A forgiving HTML/XML/RSS parser written in JS for both the browser and NodeJS (y
###Run tests under node:
node runtests.js

###Run tests under node using RequireJS:
node r.js runtests.js

###Run tests in browser:
View runtests.html in any browser

Expand Down
1 change: 1 addition & 0 deletions lib/node-htmlparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ exports.DefaultHandler = htmlparser.DefaultHandler;
exports.RssHandler = htmlparser.RssHandler;
exports.ElementType = htmlparser.ElementType;
exports.DomUtils = htmlparser.DomUtils;

56 changes: 56 additions & 0 deletions r.js

Large diffs are not rendered by default.

74 changes: 74 additions & 0 deletions runtests.min.require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/***********************************************
Copyright 2010, Chris Winberry <[email protected]>. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
***********************************************/
require(['sys', 'fs', 'lib/node-htmlparser.min'], function(sys, fs, htmlparser) {

var testFolder = "tests";
var chunkSize = 5;

var testFiles = fs.readdirSync(testFolder);
var testCount = 0;
var failedCount = 0;
for (var i in testFiles) {
testCount++;
var fileParts = testFiles[i].split(".");
fileParts.pop();
var moduleName = fileParts.join(".");
require([testFolder + "/" + moduleName], function(test) {
var handlerCallback = function handlerCallback (error) {
if (error)
sys.puts("Handler error: " + error);
};
var handler = (test.type == "rss") ?
new htmlparser.RssHandler(handlerCallback, test.options)
:
new htmlparser.DefaultHandler(handlerCallback, test.options)
;
var parser = new htmlparser.Parser(handler);
parser.parseComplete(test.html);
var resultComplete = handler.dom;
var chunkPos = 0;
parser.reset();
while (chunkPos < test.html.length) {
parser.parseChunk(test.html.substring(chunkPos, chunkPos + chunkSize));
chunkPos += chunkSize;
}
parser.done();
var resultChunk = handler.dom;
var testResult =
sys.inspect(resultComplete, false, null) === sys.inspect(test.expected, false, null)
&&
sys.inspect(resultChunk, false, null) === sys.inspect(test.expected, false, null)
;
sys.puts("[" + test.name + "\]: " + (testResult ? "passed" : "FAILED"));
if (!testResult) {
failedCount++;
sys.puts("== Complete ==");
sys.puts(sys.inspect(resultComplete, false, null));
sys.puts("== Chunked ==");
sys.puts(sys.inspect(resultChunk, false, null));
sys.puts("== Expected ==");
sys.puts(sys.inspect(test.expected, false, null));
}
});
}
sys.puts("Total tests: " + testCount);
sys.puts("Failed tests: " + failedCount);
});
74 changes: 74 additions & 0 deletions runtests.require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/***********************************************
Copyright 2010, Chris Winberry <[email protected]>. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
***********************************************/
require(['sys', 'fs', 'lib/node-htmlparser'], function(sys, fs, htmlparser) {

var testFolder = "tests";
var chunkSize = 5;

var testFiles = fs.readdirSync(testFolder);
var testCount = 0;
var failedCount = 0;
for (var i in testFiles) {
testCount++;
var fileParts = testFiles[i].split(".");
fileParts.pop();
var moduleName = fileParts.join(".");
require([testFolder + "/" + moduleName], function(test) {
var handlerCallback = function handlerCallback (error) {
if (error)
sys.puts("Handler error: " + error);
};
var handler = (test.type == "rss") ?
new htmlparser.RssHandler(handlerCallback, test.options)
:
new htmlparser.DefaultHandler(handlerCallback, test.options)
;
var parser = new htmlparser.Parser(handler);
parser.parseComplete(test.html);
var resultComplete = handler.dom;
var chunkPos = 0;
parser.reset();
while (chunkPos < test.html.length) {
parser.parseChunk(test.html.substring(chunkPos, chunkPos + chunkSize));
chunkPos += chunkSize;
}
parser.done();
var resultChunk = handler.dom;
var testResult =
sys.inspect(resultComplete, false, null) === sys.inspect(test.expected, false, null)
&&
sys.inspect(resultChunk, false, null) === sys.inspect(test.expected, false, null)
;
sys.puts("[" + test.name + "\]: " + (testResult ? "passed" : "FAILED"));
if (!testResult) {
failedCount++;
sys.puts("== Complete ==");
sys.puts(sys.inspect(resultComplete, false, null));
sys.puts("== Chunked ==");
sys.puts(sys.inspect(resultChunk, false, null));
sys.puts("== Expected ==");
sys.puts(sys.inspect(test.expected, false, null));
}
});
}
sys.puts("Total tests: " + testCount);
sys.puts("Failed tests: " + failedCount);
});
8 changes: 2 additions & 6 deletions tests/01-basic.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
(function () {

function RunningInNode () {
function runningInCommonJSEnv () {
return(
(typeof require) == "function"
&&
(typeof exports) == "object"
&&
(typeof module) == "object"
&&
(typeof __filename) == "string"
&&
(typeof __dirname) == "string"
);
}

if (!RunningInNode()) {
if (!runningInCommonJSEnv()) {
if (!this.Tautologistics)
this.Tautologistics = {};
if (!this.Tautologistics.NodeHtmlParser)
Expand Down
8 changes: 2 additions & 6 deletions tests/02-single_tag_1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
(function () {

function RunningInNode () {
function runningInCommonJSEnv () {
return(
(typeof require) == "function"
&&
(typeof exports) == "object"
&&
(typeof module) == "object"
&&
(typeof __filename) == "string"
&&
(typeof __dirname) == "string"
);
}

if (!RunningInNode()) {
if (!runningInCommonJSEnv()) {
if (!this.Tautologistics)
this.Tautologistics = {};
if (!this.Tautologistics.NodeHtmlParser)
Expand Down
8 changes: 2 additions & 6 deletions tests/03-single_tag_2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
(function () {

function RunningInNode () {
function runningInCommonJSEnv () {
return(
(typeof require) == "function"
&&
(typeof exports) == "object"
&&
(typeof module) == "object"
&&
(typeof __filename) == "string"
&&
(typeof __dirname) == "string"
);
}

if (!RunningInNode()) {
if (!runningInCommonJSEnv()) {
if (!this.Tautologistics)
this.Tautologistics = {};
if (!this.Tautologistics.NodeHtmlParser)
Expand Down
8 changes: 2 additions & 6 deletions tests/04-unescaped_in_script.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
(function () {

function RunningInNode () {
function runningInCommonJSEnv () {
return(
(typeof require) == "function"
&&
(typeof exports) == "object"
&&
(typeof module) == "object"
&&
(typeof __filename) == "string"
&&
(typeof __dirname) == "string"
);
}

if (!RunningInNode()) {
if (!runningInCommonJSEnv()) {
if (!this.Tautologistics)
this.Tautologistics = {};
if (!this.Tautologistics.NodeHtmlParser)
Expand Down
8 changes: 2 additions & 6 deletions tests/05-tags_in_comment.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
(function () {

function RunningInNode () {
function runningInCommonJSEnv () {
return(
(typeof require) == "function"
&&
(typeof exports) == "object"
&&
(typeof module) == "object"
&&
(typeof __filename) == "string"
&&
(typeof __dirname) == "string"
);
}

if (!RunningInNode()) {
if (!runningInCommonJSEnv()) {
if (!this.Tautologistics)
this.Tautologistics = {};
if (!this.Tautologistics.NodeHtmlParser)
Expand Down
8 changes: 2 additions & 6 deletions tests/06-comment_in_script.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
(function () {

function RunningInNode () {
function runningInCommonJSEnv () {
return(
(typeof require) == "function"
&&
(typeof exports) == "object"
&&
(typeof module) == "object"
&&
(typeof __filename) == "string"
&&
(typeof __dirname) == "string"
);
}

if (!RunningInNode()) {
if (!runningInCommonJSEnv()) {
if (!this.Tautologistics)
this.Tautologistics = {};
if (!this.Tautologistics.NodeHtmlParser)
Expand Down
8 changes: 2 additions & 6 deletions tests/07-unescaped_in_style.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
(function () {

function RunningInNode () {
function runningInCommonJSEnv () {
return(
(typeof require) == "function"
&&
(typeof exports) == "object"
&&
(typeof module) == "object"
&&
(typeof __filename) == "string"
&&
(typeof __dirname) == "string"
);
}

if (!RunningInNode()) {
if (!runningInCommonJSEnv()) {
if (!this.Tautologistics)
this.Tautologistics = {};
if (!this.Tautologistics.NodeHtmlParser)
Expand Down
8 changes: 2 additions & 6 deletions tests/08-extra_spaces_in_tag.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
(function () {

function RunningInNode () {
function runningInCommonJSEnv () {
return(
(typeof require) == "function"
&&
(typeof exports) == "object"
&&
(typeof module) == "object"
&&
(typeof __filename) == "string"
&&
(typeof __dirname) == "string"
);
}

if (!RunningInNode()) {
if (!runningInCommonJSEnv()) {
if (!this.Tautologistics)
this.Tautologistics = {};
if (!this.Tautologistics.NodeHtmlParser)
Expand Down
8 changes: 2 additions & 6 deletions tests/09-unquoted_attrib.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
(function () {

function RunningInNode () {
function runningInCommonJSEnv () {
return(
(typeof require) == "function"
&&
(typeof exports) == "object"
&&
(typeof module) == "object"
&&
(typeof __filename) == "string"
&&
(typeof __dirname) == "string"
);
}

if (!RunningInNode()) {
if (!runningInCommonJSEnv()) {
if (!this.Tautologistics)
this.Tautologistics = {};
if (!this.Tautologistics.NodeHtmlParser)
Expand Down
8 changes: 2 additions & 6 deletions tests/10-singular_attribute.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
(function () {

function RunningInNode () {
function runningInCommonJSEnv () {
return(
(typeof require) == "function"
&&
(typeof exports) == "object"
&&
(typeof module) == "object"
&&
(typeof __filename) == "string"
&&
(typeof __dirname) == "string"
);
}

if (!RunningInNode()) {
if (!runningInCommonJSEnv()) {
if (!this.Tautologistics)
this.Tautologistics = {};
if (!this.Tautologistics.NodeHtmlParser)
Expand Down
Loading