-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add handlers for when the rewriting is finished. (#27)
* Allow handlers for the end of parsing. * Make the end handler a document content handler. * Allow appending at the end of the document. * Append chunks on the fly. * Remove unneeded example. * Propagate error to `end()`. * Propagate the error immediately. * Store a reference to the OutputSink in DocumentEnd. * Test if appending at the end works. * Extend the C API to support document end handlers. * Refactor condition. * Pass user data to the document end handler. * Document the C API function. * Add regression test.
- Loading branch information
Showing
23 changed files
with
420 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use super::*; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn lol_html_doc_end_append( | ||
document_end: *mut DocumentEnd, | ||
content: *const c_char, | ||
content_len: size_t, | ||
is_html: bool, | ||
) -> c_int { | ||
content_insertion_fn_body! { document_end.append(content, content_len, is_html) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#include "../../include/lol_html.h" | ||
#include "deps/picotest/picotest.h" | ||
#include "tests.h" | ||
#include "test_util.h" | ||
|
||
static int EXPECTED_USER_DATA = 43; | ||
|
||
//------------------------------------------------------------------------- | ||
EXPECT_OUTPUT( | ||
append_to_empty_doc_output_sink, | ||
"<!--appended text-->hello & world", | ||
&EXPECTED_USER_DATA, | ||
sizeof(EXPECTED_USER_DATA) | ||
); | ||
|
||
static lol_html_rewriter_directive_t append_to_empty_doc( | ||
lol_html_doc_end_t *doc_end, | ||
void *user_data | ||
) { | ||
note("Append at at the end of an empty document"); | ||
ok(*(int*)user_data == EXPECTED_USER_DATA); | ||
|
||
const char *append_html = "<!--appended text-->"; | ||
ok(!lol_html_doc_end_append(doc_end, append_html, strlen(append_html), true)); | ||
|
||
const char *append_text = "hello & world"; | ||
ok(!lol_html_doc_end_append(doc_end, append_text, strlen(append_text), false)); | ||
|
||
return LOL_HTML_CONTINUE; | ||
} | ||
|
||
static void test_append_to_empty_doc(void *user_data) { | ||
lol_html_rewriter_builder_t *builder = lol_html_rewriter_builder_new(); | ||
|
||
lol_html_rewriter_builder_add_document_content_handlers( | ||
builder, | ||
NULL, | ||
NULL, | ||
NULL, | ||
NULL, | ||
NULL, | ||
NULL, | ||
append_to_empty_doc, | ||
user_data | ||
); | ||
|
||
run_rewriter( | ||
builder, | ||
"", | ||
append_to_empty_doc_output_sink, | ||
user_data | ||
); | ||
} | ||
|
||
//------------------------------------------------------------------------- | ||
EXPECT_OUTPUT( | ||
append_at_end_output_sink, | ||
"<html><div>Hello</div></html><!--appended text-->hello & world", | ||
&EXPECTED_USER_DATA, | ||
sizeof(EXPECTED_USER_DATA) | ||
); | ||
|
||
static lol_html_rewriter_directive_t append_at_end( | ||
lol_html_doc_end_t *doc_end, | ||
void *user_data | ||
) { | ||
note("Append at at the end"); | ||
ok(*(int*)user_data == EXPECTED_USER_DATA); | ||
|
||
const char *append_html = "<!--appended text-->"; | ||
ok(!lol_html_doc_end_append(doc_end, append_html, strlen(append_html), true)); | ||
|
||
const char *append_text = "hello & world"; | ||
ok(!lol_html_doc_end_append(doc_end, append_text, strlen(append_text), false)); | ||
|
||
return LOL_HTML_CONTINUE; | ||
} | ||
|
||
static void test_append_at_end(void *user_data) { | ||
lol_html_rewriter_builder_t *builder = lol_html_rewriter_builder_new(); | ||
|
||
lol_html_rewriter_builder_add_document_content_handlers( | ||
builder, | ||
NULL, | ||
NULL, | ||
NULL, | ||
NULL, | ||
NULL, | ||
NULL, | ||
append_at_end, | ||
user_data | ||
); | ||
|
||
run_rewriter( | ||
builder, | ||
"<html><div>Hello</div></html>", | ||
append_at_end_output_sink, | ||
user_data | ||
); | ||
} | ||
|
||
void document_end_api_test() { | ||
int user_data = 43; | ||
|
||
test_append_to_empty_doc(&user_data); | ||
test_append_at_end(&user_data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.