1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-09-27 02:56:18 -04:00

[js] textContent (read)

This commit is contained in:
Witold Filipczyk 2021-05-05 15:21:37 +02:00
parent 9b738edb18
commit b07d39364c
2 changed files with 138 additions and 0 deletions

View File

@ -65,6 +65,8 @@ static bool element_set_property_lang(JSContext *ctx, unsigned int argc, JS::Val
static bool element_get_property_outerHtml(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_set_property_outerHtml(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_get_property_tagName(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_get_property_textContent(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_set_property_textContent(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_get_property_title(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_set_property_title(JSContext *ctx, unsigned int argc, JS::Value *vp);
@ -88,6 +90,7 @@ JSPropertySpec element_props[] = {
JS_PSGS("lang", element_get_property_lang, element_set_property_lang, JSPROP_ENUMERATE),
JS_PSGS("outerHTML", element_get_property_outerHtml, element_set_property_outerHtml, JSPROP_ENUMERATE),
JS_PSG("tagName", element_get_property_tagName, JSPROP_ENUMERATE),
JS_PSGS("textContent", element_get_property_textContent, element_set_property_textContent, JSPROP_ENUMERATE),
JS_PSGS("title", element_get_property_title, element_set_property_title, JSPROP_ENUMERATE),
JS_PS_END
};
@ -381,6 +384,34 @@ walk_tree(struct string *buf, tree<HTML::Node> const &dom, const unsigned int of
}
}
static void
walk_tree_content(struct string *buf, tree<HTML::Node> const &dom, const unsigned int offset)
{
int local = 0;
tree<HTML::Node>::iterator it = dom.begin();
if (was_el && !it->isTag()) add_to_string(buf, it->text().c_str());
if (!was_el && it->isTag()) {
if (it->offset() == offset) {
was_el = 1;
local = 1;
}
}
for (tree<HTML::Node>::sibling_iterator childIt = dom.begin(it); childIt != dom.end(it); ++childIt)
{
walk_tree_content(buf, childIt, offset);
}
if (was_el) {
if (!local) {
//add_to_string(buf, it->closingText().c_str());
} else {
was_el = 0;
}
}
}
static void
walk_tree_outer(struct string *buf, tree<HTML::Node> const &dom, const unsigned int offset)
{
@ -507,6 +538,57 @@ element_get_property_outerHtml(JSContext *ctx, unsigned int argc, JS::Value *vp)
return true;
}
static bool
element_get_property_textContent(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct view_state *vs;
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, hobj, &element_class, NULL))
return false;
vs = interpreter->vs;
if (!vs) {
return false;
}
tree<HTML::Node> *el = JS_GetPrivate(hobj);
if (!el) {
args.rval().setNull();
return true;
}
struct document_view *doc_view = vs->doc_view;
struct document *document = doc_view->document;
tree<HTML::Node> *dom = document->dom;
struct string buf;
init_string(&buf);
was_el = 0;
walk_tree_content(&buf, *dom, el->begin()->offset());
args.rval().setString(JS_NewStringCopyZ(ctx, buf.source));
done_string(&buf);
return true;
}
static bool
element_set_property_className(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
@ -677,6 +759,35 @@ element_set_property_outerHtml(JSContext *ctx, unsigned int argc, JS::Value *vp)
return true;
}
static bool
element_set_property_textContent(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, hobj, &element_class, NULL))
return false;
struct view_state *vs = interpreter->vs;
if (!vs) {
return true;
}
return true;
}
static bool
element_set_property_title(JSContext *ctx, unsigned int argc, JS::Value *vp)
{

View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<body>
<h3>Differences between innerText and textContent.</h3>
<p id="demo">This p element contains a <span style="display:none">hidden span element</span> and a <span> visible span element</span>.</p>kkkk
<button onclick="getTextContent()">Get textContent</button>
<button onclick="getInnerText()">Get innerText</button>
<p><strong>Note:</strong> The textContent property is not supported in Internet Explorer 8 and earlier, and the innerText property is not supported in IE9 and earlier.</p>
<p id="demo"></p>
<script>
function getTextContent() {
alert(document.getElementById("demo").textContent)
}
function getInnerText() {
alert(document.getElementById("demo").innerText)
}
</script>
</body>
</html>