1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-06-04 21:50:52 +00:00

Compare commits

...

8 Commits

Author SHA1 Message Date
Witold Filipczyk
16cbdc2501 [mujs] element.outerHTML setter 2024-05-08 18:57:11 +02:00
Witold Filipczyk
58bf489719 [quickjs] element.outerHTML setter 2024-05-08 18:47:21 +02:00
Witold Filipczyk
46a213ae49 [spidermonkey] element.outerHTML 2024-05-08 18:32:59 +02:00
Witold Filipczyk
b3b6f66c51 [assert] test case for outerHTML 2024-05-08 16:14:43 +02:00
Witold Filipczyk
e11b52b496 [assert] lowercase in some assertions for outerHTML and innerHTML 2024-05-08 15:57:56 +02:00
Witold Filipczyk
ea2dbb05e2 [ecmascript] More add_lowercase_to_string 2024-05-08 15:37:00 +02:00
Witold Filipczyk
abc307e86b [libdom] Added add_lowercase_to_string
Replace uppercase with lowercase in tag names in html "rewrites"
2024-05-08 15:27:48 +02:00
Witold Filipczyk
a9ae51ae9f [mujs] nodelist.forEach passes test case 2024-05-07 19:33:34 +02:00
22 changed files with 500 additions and 33 deletions

View File

@ -85,3 +85,22 @@ free_document(void *doc)
dom_node *ddd = (dom_node *)doc;
dom_node_unref(ddd);
}
void
add_lowercase_to_string(struct string *buf, const char *str, int len)
{
char *tmp = memacpy(str, len);
int i;
if (!tmp) {
return;
}
for (i = 0; i < len; i++) {
if (tmp[i] >= 'A' && tmp[i] <= 'Z') {
tmp[i] += 32;
}
}
add_bytes_to_string(buf, tmp, len);
mem_free(tmp);
}

View File

@ -12,6 +12,7 @@ void *document_parse_text(const char *charset, char *data, size_t length);
void *document_parse(struct document *document, struct string *source);
void free_document(void *doc);
void *el_match_selector(const char *selector, void *node);
void add_lowercase_to_string(struct string *buf, const char *str, int len);
#ifdef __cplusplus
}

View File

@ -51,7 +51,7 @@ dump_dom_element_closing(struct string *buf, dom_node *node)
/* Get string data and print element name */
add_to_string(buf, "</");
add_bytes_to_string(buf, dom_string_data(node_name), dom_string_byte_length(node_name));
add_lowercase_to_string(buf, dom_string_data(node_name), dom_string_byte_length(node_name));
add_char_to_string(buf, '>');
/* Finished with the node_name dom_string */
@ -146,7 +146,7 @@ dump_dom_element(void *mapa, struct string *buf, dom_node *node, int depth)
save_in_map(mapa, node, buf->length);
/* Get string data and print element name */
add_bytes_to_string(buf, dom_string_data(node_name), dom_string_byte_length(node_name));
add_lowercase_to_string(buf, dom_string_data(node_name), dom_string_byte_length(node_name));
exc = dom_node_get_attributes(node, &attrs);

View File

@ -60,7 +60,7 @@ dump_dom_element_closing(struct string *buf, dom_node *node)
if (strcmp(dom_string_data(node_name), "BR")) {
add_to_string(buf, "</");
add_bytes_to_string(buf, dom_string_data(node_name), dom_string_byte_length(node_name));
add_lowercase_to_string(buf, dom_string_data(node_name), dom_string_byte_length(node_name));
add_char_to_string(buf, '>');
}
@ -169,7 +169,7 @@ dump_dom_element(void *mapa, void *mapa_rev, struct string *buf, dom_node *node,
save_offset_in_map(mapa_rev, node, buf->length);
/* Get string data and print element name */
add_bytes_to_string(buf, dom_string_data(node_name), dom_string_byte_length(node_name));
add_lowercase_to_string(buf, dom_string_data(node_name), dom_string_byte_length(node_name));
if (strcmp(dom_string_data(node_name), "BR") == 0) {
add_char_to_string(buf, '/');

View File

@ -1302,7 +1302,7 @@ dump_element(struct string *buf, dom_node *node, bool toSortAttrs)
//save_in_map(mapa, node, buf->length);
/* Get string data and print element name */
add_bytes_to_string(buf, dom_string_data(node_name), dom_string_byte_length(node_name));
add_lowercase_to_string(buf, dom_string_data(node_name), dom_string_byte_length(node_name));
exc = dom_node_get_attributes(node, &attrs);
@ -1386,7 +1386,7 @@ walk_tree(struct string *buf, void *nod, bool start, bool toSortAttrs)
if (exc == DOM_NO_ERR && node_name) {
add_to_string(buf, "</");
add_bytes_to_string(buf, dom_string_data(node_name), dom_string_length(node_name));
add_lowercase_to_string(buf, dom_string_data(node_name), dom_string_length(node_name));
add_char_to_string(buf, '>');
dom_string_unref(node_name);
}
@ -1854,6 +1854,150 @@ mjs_element_set_property_lang(js_State *J)
js_pushundefined(J);
}
static void
mjs_element_set_property_outerHtml(js_State *J)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)js_getcontext(J);
struct view_state *vs = interpreter->vs;
if (!vs) {
js_pushundefined(J);
return;
}
dom_node *el = (dom_node *)(mjs_getprivate(J, 0));
if (!el) {
js_pushundefined(J);
return;
}
dom_node *parent = NULL;
dom_exception exc = dom_node_get_parent_node(el, &parent);
if (exc != DOM_NO_ERR) {
js_pushundefined(J);
return;
}
size_t size;
const char *s = js_tostring(J, 1);
if (!s) {
js_error(J, "out of memory");
return;
}
size = strlen(s);
struct dom_node *cref = NULL;
dom_hubbub_parser_params parse_params;
dom_hubbub_error error;
dom_hubbub_parser *parser = NULL;
struct dom_document *doc = NULL;
struct dom_document_fragment *fragment = NULL;
struct dom_node *child = NULL, *html = NULL, *body = NULL;
struct dom_nodelist *bodies = NULL;
exc = dom_node_get_owner_document(el, &doc);
if (exc != DOM_NO_ERR) goto out;
parse_params.enc = "UTF-8";
parse_params.fix_enc = true;
parse_params.enable_script = false;
parse_params.msg = NULL;
parse_params.script = NULL;
parse_params.ctx = NULL;
parse_params.daf = NULL;
error = dom_hubbub_fragment_parser_create(&parse_params,
doc,
&parser,
&fragment);
if (error != DOM_HUBBUB_OK) {
fprintf(stderr, "Unable to create fragment parser!");
goto out;
}
error = dom_hubbub_parser_parse_chunk(parser, (const uint8_t*)s, size);
if (error != DOM_HUBBUB_OK) {
fprintf(stderr, "Unable to parse HTML chunk");
goto out;
}
error = dom_hubbub_parser_completed(parser);
if (error != DOM_HUBBUB_OK) {
fprintf(stderr, "Unable to complete parser");
goto out;
}
/* The first child in the fragment will be an HTML element
* because that's how hubbub works, walk through that to the body
* element hubbub will have created, we want to migrate that element's
* children into ourself.
*/
exc = dom_node_get_first_child(fragment, &html);
if (exc != DOM_NO_ERR) goto out;
/* We can then ask that HTML element to give us its body */
exc = dom_element_get_elements_by_tag_name(html, corestring_dom_BODY, &bodies);
if (exc != DOM_NO_ERR) goto out;
/* And now we can get the body which will be the zeroth body */
exc = dom_nodelist_item(bodies, 0, &body);
if (exc != DOM_NO_ERR) goto out;
/* Migrate the children */
exc = dom_node_get_first_child(body, &child);
if (exc != DOM_NO_ERR) goto out;
while (child != NULL) {
exc = dom_node_remove_child(body, child, &cref);
if (exc != DOM_NO_ERR) goto out;
dom_node_unref(cref);
dom_node *spare = NULL;
exc = dom_node_insert_before(parent, child, el, &spare);
if (exc != DOM_NO_ERR) goto out;
dom_node_unref(spare);
dom_node_unref(cref);
dom_node_unref(child);
child = NULL;
exc = dom_node_get_first_child(body, &child);
if (exc != DOM_NO_ERR) goto out;
}
exc = dom_node_remove_child(parent, el, &cref);
if (exc != DOM_NO_ERR) goto out;
out:
if (parser != NULL) {
dom_hubbub_parser_destroy(parser);
}
if (doc != NULL) {
dom_node_unref(doc);
}
if (fragment != NULL) {
dom_node_unref(fragment);
}
if (child != NULL) {
dom_node_unref(child);
}
if (html != NULL) {
dom_node_unref(html);
}
if (bodies != NULL) {
dom_nodelist_unref(bodies);
}
if (body != NULL) {
dom_node_unref(body);
}
if (cref != NULL) {
dom_node_unref(cref);
}
dom_node_unref(parent);
interpreter->changed = 1;
js_pushundefined(J);
}
static void
mjs_element_set_property_title(js_State *J)
{
@ -3023,7 +3167,7 @@ mjs_push_element(js_State *J, void *node)
addproperty(J, "offsetParent", mjs_element_get_property_offsetParent, NULL);
// addproperty(J, "offsetTop", mjs_element_get_property_offsetTop, NULL);
// addproperty(J, "offsetWidth", mjs_element_get_property_offsetWidth, NULL);
addproperty(J, "outerHTML", mjs_element_get_property_outerHtml, NULL);
addproperty(J, "outerHTML", mjs_element_get_property_outerHtml, mjs_element_set_property_outerHtml);
addproperty(J, "ownerDocument", mjs_element_get_property_ownerDocument, NULL);
addproperty(J, "parentElement", mjs_element_get_property_parentElement, NULL);
addproperty(J, "parentNode", mjs_element_get_property_parentNode, NULL);

View File

@ -88,9 +88,10 @@ mjs_nodeList_set_items(js_State *J, void *node)
continue;
}
mjs_push_element(J, element);
js_setindex(J, 1, i);
js_setindex(J, -2, i);
dom_node_unref(element);
}
js_setlength(J, -1, length);
}
static void
@ -138,10 +139,10 @@ mjs_push_nodelist(js_State *J, void *node)
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
js_newobject(J);
js_newarray(J);
{
js_newuserdata(J, "nodelist", node, mjs_nodeList_finalizer);
addproperty(J, "length", mjs_nodeList_get_property_length, NULL);
// addproperty(J, "length", mjs_nodeList_get_property_length, NULL);
addmethod(J, "item", mjs_nodeList_item, 1);
addmethod(J, "toString", mjs_nodeList_toString, 0);
mjs_nodeList_set_items(J, node);

View File

@ -18,6 +18,7 @@
#include "dialogs/status.h"
#include "document/document.h"
#include "document/libdom/corestrings.h"
#include "document/libdom/doc.h"
#include "document/libdom/mapa.h"
#include "document/libdom/renderer2.h"
#include "document/view.h"
@ -1351,7 +1352,7 @@ dump_element(struct string *buf, dom_node *node, bool toSortAttrs)
//save_in_map(mapa, node, buf->length);
/* Get string data and print element name */
add_bytes_to_string(buf, dom_string_data(node_name), dom_string_byte_length(node_name));
add_lowercase_to_string(buf, dom_string_data(node_name), dom_string_byte_length(node_name));
exc = dom_node_get_attributes(node, &attrs);
@ -1435,7 +1436,7 @@ walk_tree(struct string *buf, void *nod, bool start, bool toSortAttrs)
if (exc == DOM_NO_ERR && node_name) {
add_to_string(buf, "</");
add_bytes_to_string(buf, dom_string_data(node_name), dom_string_length(node_name));
add_lowercase_to_string(buf, dom_string_data(node_name), dom_string_length(node_name));
add_char_to_string(buf, '>');
dom_string_unref(node_name);
}
@ -1870,6 +1871,147 @@ js_element_set_property_lang(JSContext *ctx, JSValueConst this_val, JSValue val)
return JS_UNDEFINED;
}
static JSValue
js_element_set_property_outerHtml(JSContext *ctx, JSValueConst this_val, JSValue val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(this_val);
REF_JS(val);
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx);
struct view_state *vs = interpreter->vs;
if (!vs) {
return JS_UNDEFINED;
}
dom_node *el = (dom_node *)(js_getopaque(this_val, js_element_class_id));
if (!el) {
return JS_UNDEFINED;
}
dom_node *parent = NULL;
dom_exception exc = dom_node_get_parent_node(el, &parent);
if (exc != DOM_NO_ERR) {
return JS_UNDEFINED;
}
size_t size;
const char *s = JS_ToCStringLen(ctx, &size, val);
if (!s) {
return JS_EXCEPTION;
}
struct dom_node *cref = NULL;
dom_hubbub_parser_params parse_params;
dom_hubbub_error error;
dom_hubbub_parser *parser = NULL;
struct dom_document *doc = NULL;
struct dom_document_fragment *fragment = NULL;
struct dom_node *child = NULL, *html = NULL, *body = NULL;
struct dom_nodelist *bodies = NULL;
exc = dom_node_get_owner_document(el, &doc);
if (exc != DOM_NO_ERR) goto out;
parse_params.enc = "UTF-8";
parse_params.fix_enc = true;
parse_params.enable_script = false;
parse_params.msg = NULL;
parse_params.script = NULL;
parse_params.ctx = NULL;
parse_params.daf = NULL;
error = dom_hubbub_fragment_parser_create(&parse_params,
doc,
&parser,
&fragment);
if (error != DOM_HUBBUB_OK) {
fprintf(stderr, "Unable to create fragment parser!");
goto out;
}
error = dom_hubbub_parser_parse_chunk(parser, (const uint8_t*)s, size);
if (error != DOM_HUBBUB_OK) {
fprintf(stderr, "Unable to parse HTML chunk");
goto out;
}
error = dom_hubbub_parser_completed(parser);
if (error != DOM_HUBBUB_OK) {
fprintf(stderr, "Unable to complete parser");
goto out;
}
/* The first child in the fragment will be an HTML element
* because that's how hubbub works, walk through that to the body
* element hubbub will have created, we want to migrate that element's
* children into ourself.
*/
exc = dom_node_get_first_child(fragment, &html);
if (exc != DOM_NO_ERR) goto out;
/* We can then ask that HTML element to give us its body */
exc = dom_element_get_elements_by_tag_name(html, corestring_dom_BODY, &bodies);
if (exc != DOM_NO_ERR) goto out;
/* And now we can get the body which will be the zeroth body */
exc = dom_nodelist_item(bodies, 0, &body);
if (exc != DOM_NO_ERR) goto out;
/* Migrate the children */
exc = dom_node_get_first_child(body, &child);
if (exc != DOM_NO_ERR) goto out;
while (child != NULL) {
exc = dom_node_remove_child(body, child, &cref);
if (exc != DOM_NO_ERR) goto out;
dom_node_unref(cref);
dom_node *spare = NULL;
exc = dom_node_insert_before(parent, child, el, &spare);
if (exc != DOM_NO_ERR) goto out;
dom_node_unref(spare);
dom_node_unref(cref);
dom_node_unref(child);
child = NULL;
exc = dom_node_get_first_child(body, &child);
if (exc != DOM_NO_ERR) goto out;
}
exc = dom_node_remove_child(parent, el, &cref);
if (exc != DOM_NO_ERR) goto out;
out:
if (parser != NULL) {
dom_hubbub_parser_destroy(parser);
}
if (doc != NULL) {
dom_node_unref(doc);
}
if (fragment != NULL) {
dom_node_unref(fragment);
}
if (child != NULL) {
dom_node_unref(child);
}
if (html != NULL) {
dom_node_unref(html);
}
if (bodies != NULL) {
dom_nodelist_unref(bodies);
}
if (body != NULL) {
dom_node_unref(body);
}
if (cref != NULL) {
dom_node_unref(cref);
}
dom_node_unref(parent);
JS_FreeCString(ctx, s);
interpreter->changed = 1;
return JS_UNDEFINED;
}
static JSValue
js_element_set_property_title(JSContext *ctx, JSValueConst this_val, JSValue val)
{
@ -3106,7 +3248,7 @@ static const JSCFunctionListEntry js_element_proto_funcs[] = {
JS_CGETSET_DEF("offsetParent", js_element_get_property_offsetParent, NULL),
// JS_CGETSET_DEF("offsetTop", js_element_get_property_offsetTop, NULL),
// JS_CGETSET_DEF("offsetWidth", js_element_get_property_offsetWidth, NULL),
JS_CGETSET_DEF("outerHTML", js_element_get_property_outerHtml, NULL),
JS_CGETSET_DEF("outerHTML", js_element_get_property_outerHtml, js_element_set_property_outerHtml),
JS_CGETSET_DEF("ownerDocument", js_element_get_property_ownerDocument, NULL),
JS_CGETSET_DEF("parentElement", js_element_get_property_parentElement, NULL),
JS_CGETSET_DEF("parentNode", js_element_get_property_parentNode, NULL),

View File

@ -2512,7 +2512,7 @@ dump_element(struct string *buf, dom_node *node, bool toSortAttrs)
//save_in_map(mapa, node, buf->length);
/* Get string data and print element name */
add_bytes_to_string(buf, dom_string_data(node_name), dom_string_byte_length(node_name));
add_lowercase_to_string(buf, dom_string_data(node_name), dom_string_byte_length(node_name));
exc = dom_node_get_attributes(node, &attrs);
@ -2596,7 +2596,7 @@ walk_tree(struct string *buf, void *nod, bool start, bool toSortAttrs)
if (exc == DOM_NO_ERR && node_name) {
add_to_string(buf, "</");
add_bytes_to_string(buf, dom_string_data(node_name), dom_string_length(node_name));
add_lowercase_to_string(buf, dom_string_data(node_name), dom_string_length(node_name));
add_char_to_string(buf, '>');
dom_string_unref(node_name);
}
@ -3380,11 +3380,138 @@ element_set_property_outerHtml(JSContext *ctx, unsigned int argc, JS::Value *vp)
#endif
return false;
}
// TODO
args.rval().setUndefined();
struct view_state *vs = interpreter->vs;
if (!vs) {
return true;
}
dom_node *el = (dom_node *)JS::GetMaybePtrFromReservedSlot<dom_node>(hobj, 0);
if (!el) {
return true;
}
dom_node *parent = NULL;
dom_exception exc = dom_node_get_parent_node(el, &parent);
if (exc != DOM_NO_ERR) {
return true;
}
char *s = jsval_to_string(ctx, args[0]);
if (!s) {
return false;
}
size_t size = strlen(s);
struct dom_node *cref = NULL;
dom_hubbub_parser_params parse_params;
dom_hubbub_error error;
dom_hubbub_parser *parser = NULL;
struct dom_document *doc = NULL;
struct dom_document_fragment *fragment = NULL;
struct dom_node *child = NULL, *html = NULL, *body = NULL;
struct dom_nodelist *bodies = NULL;
exc = dom_node_get_owner_document(el, &doc);
if (exc != DOM_NO_ERR) goto out;
parse_params.enc = "UTF-8";
parse_params.fix_enc = true;
parse_params.enable_script = false;
parse_params.msg = NULL;
parse_params.script = NULL;
parse_params.ctx = NULL;
parse_params.daf = NULL;
error = dom_hubbub_fragment_parser_create(&parse_params,
doc,
&parser,
&fragment);
if (error != DOM_HUBBUB_OK) {
fprintf(stderr, "Unable to create fragment parser!");
goto out;
}
error = dom_hubbub_parser_parse_chunk(parser, (const uint8_t*)s, size);
if (error != DOM_HUBBUB_OK) {
fprintf(stderr, "Unable to parse HTML chunk");
goto out;
}
error = dom_hubbub_parser_completed(parser);
if (error != DOM_HUBBUB_OK) {
fprintf(stderr, "Unable to complete parser");
goto out;
}
/* The first child in the fragment will be an HTML element
* because that's how hubbub works, walk through that to the body
* element hubbub will have created, we want to migrate that element's
* children into ourself.
*/
exc = dom_node_get_first_child(fragment, &html);
if (exc != DOM_NO_ERR) goto out;
/* We can then ask that HTML element to give us its body */
exc = dom_element_get_elements_by_tag_name(html, corestring_dom_BODY, &bodies);
if (exc != DOM_NO_ERR) goto out;
/* And now we can get the body which will be the zeroth body */
exc = dom_nodelist_item(bodies, 0, &body);
if (exc != DOM_NO_ERR) goto out;
/* Migrate the children */
exc = dom_node_get_first_child(body, &child);
if (exc != DOM_NO_ERR) goto out;
while (child != NULL) {
exc = dom_node_remove_child(body, child, &cref);
if (exc != DOM_NO_ERR) goto out;
dom_node_unref(cref);
dom_node *spare = NULL;
exc = dom_node_insert_before(parent, child, el, &spare);
if (exc != DOM_NO_ERR) goto out;
dom_node_unref(spare);
dom_node_unref(cref);
dom_node_unref(child);
child = NULL;
exc = dom_node_get_first_child(body, &child);
if (exc != DOM_NO_ERR) goto out;
}
exc = dom_node_remove_child(parent, el, &cref);
if (exc != DOM_NO_ERR) goto out;
out:
if (parser != NULL) {
dom_hubbub_parser_destroy(parser);
}
if (doc != NULL) {
dom_node_unref(doc);
}
if (fragment != NULL) {
dom_node_unref(fragment);
}
if (child != NULL) {
dom_node_unref(child);
}
if (html != NULL) {
dom_node_unref(html);
}
if (bodies != NULL) {
dom_nodelist_unref(bodies);
}
if (body != NULL) {
dom_node_unref(body);
}
if (cref != NULL) {
dom_node_unref(cref);
}
dom_node_unref(parent);
mem_free(s);
interpreter->changed = 1;
return true;
}

View File

@ -17,7 +17,7 @@ First Name: <input name="fname" type="text" value="Doug">
<script>
function myFunction() {
var x = document.anchors.item(1).outerHTML;
console.assert(x === '<A href="/home" name="bbb">/home</A>', 'anchors');
console.assert(x === '<a href="/home" name="bbb">/home</a>', 'anchors');
}
console.error('document.anchors.html');

View File

@ -12,7 +12,7 @@
<script>
function myFunction() {
var x = document.documentElement.innerHTML;
console.assert(x.startsWith('<HEAD></HEAD><BODY id="test">'), 'head was added');
console.assert(x.startsWith('<head></head><body id="test">'), 'head was added');
}
console.error('document.documentElement.html');

View File

@ -14,7 +14,7 @@ First Name: <input name="fname" type="text" value="Doug">
<script>
function myFunction() {
var x = document.getElementsByTagName("P").item(1).outerHTML;
console.assert(x === '<P id="demo"></P>', 'demo');
console.assert(x === '<p id="demo"></p>', 'demo');
}
console.error('document.getElementsByTagName.html');

View File

@ -19,7 +19,7 @@ function myFunction() {
var textnode = document.createTextNode("Water");
node.appendChild(textnode);
document.getElementById("myList").appendChild(node);
console.assert(document.getElementsByTagName("UL")[0].outerHTML === '<UL id="myList">\n <LI>Coffee</LI>\n <LI>Tea</LI>\n<LI>Water</LI></UL>', 'Water');
console.assert(document.getElementsByTagName("UL")[0].outerHTML === '<ul id="myList">\n <li>Coffee</li>\n <li>Tea</li>\n<li>Water</li></ul>', 'Water');
}
console.error('element.appendChild.html');
myFunction();

View File

@ -14,10 +14,10 @@
<script>
function myFunction() {
var itm = document.getElementById("myList2").lastChild;
console.assert(itm.outerHTML === '<LI>Milk</LI>', 'Milk');
console.assert(itm.outerHTML === '<li>Milk</li>', 'Milk');
var cln = itm.cloneNode(true);
document.getElementById("myList1").appendChild(cln);
console.assert(document.getElementById("myList1").innerHTML === '<LI>Coffee</LI><LI>Tea</LI><LI>Milk</LI>', 'Coffee, Tea, Milk');
console.assert(document.getElementById("myList1").innerHTML === '<li>Coffee</li><li>Tea</li><li>Milk</li>', 'Coffee, Tea, Milk');
}
console.error('element.cloneNode.html');

View File

@ -13,8 +13,7 @@ var element = document.getElementById("myElement");
var closes2 = element.closest("div.container");
console.error('element.closest.html');
console.assert(closes2, 'not null');
//console.assert(closes2.outerHTML === '<DIV class="demo container">Parent\n <DIV id="myElement" class="demo">The outer HTML of closest element will be shown.</DIV>\n </DIV>', 'TODO');
console.assert(closes2 != null, 'not null');
closes2 = element.closest("p.test");
console.assert(closes2 === null, 'NULL');

View File

@ -9,7 +9,7 @@
function aa()
{
var x = document.getElementById('blabla').firstElementChild.outerHTML;
console.assert(x === '<B id="b1">AAA</B>', 'B');
console.assert(x === '<b id="b1">AAA</b>', 'B');
}
console.error('element.firstElementChild');

View File

@ -14,7 +14,7 @@ First Name: <input name="fname" type="text" value="Doug">
<script>
function myFunction() {
var x = document.getElementById('body').getElementsByTagName("P").item(0).outerHTML;
console.assert(x === '<P>Click the button to get the tag name of the first element in the body that has tag P.</P>', 'first p');
console.assert(x === '<p>Click the button to get the tag name of the first element in the body that has tag P.</p>', 'first p');
}
console.error('element.getElementByTagName.html');

View File

@ -10,7 +10,7 @@
function aa()
{
var a1 = document.getElementById('blabla').innerHTML;
console.assert(a1 === '\n<B>AAA</B><U id="ble">UUU</U>AAAAAAA\n', a1);
console.assert(a1 === '\n<b>AAA</b><u id="ble">UUU</u>AAAAAAA\n', a1);
var a2 = document.getElementById('ble').innerHTML;
console.assert(a2 === 'UUU', 'UUU');
}
@ -19,7 +19,7 @@ function bb()
{
document.getElementById('blabla').innerHTML = '<u>test</u><b>OK</b>';
var b1 = document.getElementById('blabla').outerHTML;
console.assert(b1 === '<A id="blabla" href="/"><U>test</U><B>OK</B></A>', 'Changed');
console.assert(b1 === '<a id="blabla" href="/"><u>test</u><b>OK</b></a>', 'Changed');
}
console.error('element.innerHTML.html');

View File

@ -21,7 +21,7 @@ function myFunction() {
var list = document.getElementById("myList");
list.insertBefore(newItem, list.firstChild);
var result = document.getElementsByTagName("UL")[0].outerHTML;
console.assert(result === '<UL id="myList"><LI>Water</LI>\n <LI>Coffee</LI>\n <LI>Tea</LI>\n</UL>', 'Water, Coffee, Tea');
console.assert(result === '<ul id="myList"><li>Water</li>\n <li>Coffee</li>\n <li>Tea</li>\n</ul>', 'Water, Coffee, Tea');
}
console.error('element.insertBefore.html');

View File

@ -9,7 +9,7 @@
function aa()
{
var x = document.getElementById('blabla').lastElementChild.outerHTML;
console.assert(x === '<U dir="auto" id="ble" title="test">UUU</U>', 'u');
console.assert(x === '<u dir="auto" id="ble" title="test">UUU</u>', 'u');
}
console.error('element.lastElementChild.html');

View File

@ -0,0 +1,32 @@
<html>
<body>
<b id="test">Test</b>
<div id="container">
<div id="d">This is a div.</div>
</div>
<script>
function aa()
{
var a1 = document.getElementById('test').outerHTML;
console.assert(a1 === '<b id="test">Test</b>', a1);
}
function bb()
{
var container = document.getElementById("container");
var d = document.getElementById("d");
console.assert(container.firstElementChild.nodeName === 'DIV', 'DIV');
d.outerHTML = '<p>' + d.outerHTML + '</p>';
console.assert(container.firstElementChild.nodeName === 'P', 'P');
console.assert(container.innerHTML === '\n <p></p><div id="d">This is a div.</div><p></p>\n', 'div p div');
}
console.error('element.outerHTML.html');
aa();
bb();
console.exit(0);
</script>
</body>
</html>

View File

@ -18,10 +18,10 @@
<script>
function myFunction() {
var before = document.getElementsByTagName("H1")[0].outerHTML;
console.assert(before === '<H1>Hello World</H1>', 'H1');
console.assert(before === '<h1>Hello World</h1>', 'H1');
document.getElementsByTagName("H1")[0].setAttribute("class", "democlass");
var after = document.getElementsByTagName("H1")[0].outerHTML;
console.assert(after === '<H1 class="democlass">Hello World</H1>', 'H1 with class');
console.assert(after === '<h1 class="democlass">Hello World</h1>', 'H1 with class');
}
console.error("element.setAttribute.html");
myFunction();

View File

@ -9,11 +9,13 @@
console.error('nodelist.forEach.html');
var birds = document.querySelectorAll('li');
console.assert(birds.length === 3, birds.length);
var counter = 0;
birds.forEach(function(b) {
counter++;
});
console.assert(counter === 3, 'Three');
console.assert(counter === 3, counter);
console.exit(0);