Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
132 changes: 132 additions & 0 deletions fixtures/html/script-text-property-test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test HTMLScriptElement.text Getter/Setter</title>
<style>
body { font-family: sans-serif; }
.result-pass { color: green; }
.result-fail { color: red; }
.result { margin-bottom: 4px; }
pre { background: #f6f8fa; padding: 6px 10px; border-radius: 3px; }
</style>
</head>
<body>
<h2>Test <code>HTMLScriptElement.text</code> Getter/Setter</h2>
<div id="results"></div>
<pre id="details"></pre>
<script id="script1" type="text/javascript">
// Hello from script1
var x = 42;
</script>
<script>
function logResult(pass, message) {
const div = document.createElement('div');
div.className = 'result ' + (pass ? 'result-pass' : 'result-fail');
div.textContent = (pass ? "PASS: " : "FAIL: ") + message;
document.getElementById('results').appendChild(div);
console.log((pass ? "PASS: " : "FAIL: ") + message);
}
function appendDetails(text) {
document.getElementById('details').textContent += text + "\n";
}

const s1 = document.getElementById('script1');
appendDetails("Original .text: " + JSON.stringify(s1.text));
appendDetails("Original .textContent: " + JSON.stringify(s1.textContent));
logResult(
typeof s1.text === "string" && s1.text.includes('var x = 42;'),
"Getter: .text should contain script content"
);

s1.text = "// Replaced!\nlet y = 99;";
appendDetails("After set .text: " + JSON.stringify(s1.text));
logResult(
s1.text === "// Replaced!\nlet y = 99;",
"Setter: .text should replace script content"
);
logResult(
s1.textContent === s1.text,
".textContent should equal .text after set"
);

s1.text = "";
appendDetails("After set .text to empty: " + JSON.stringify(s1.text));
logResult(
s1.text === "",
"Setter: .text='' should clear script content"
);
logResult(
s1.childNodes.length === 0,
"Setter: .text='' should remove all child nodes"
);

s1.text = "console.log('test again');";
appendDetails("After set .text again: " + JSON.stringify(s1.text));
logResult(
s1.text === "console.log('test again');",
"Setter: .text can be set multiple times"
);

// Test multiple text nodes case
const scriptMulti = document.createElement('script');
const textNode1 = document.createTextNode('var a = 1;');
const textNode2 = document.createTextNode('var b = 2;');
const comment = document.createComment('this is a comment');

scriptMulti.appendChild(textNode1);
scriptMulti.appendChild(comment);
scriptMulti.appendChild(textNode2);

appendDetails("Multiple text nodes .text: " + JSON.stringify(scriptMulti.text));
logResult(
scriptMulti.text === 'var a = 1;var b = 2;',
"Getter: .text should concatenate multiple text nodes, ignoring comments"
);
appendDetails("Multiple text nodes .textContent: " + JSON.stringify(scriptMulti.textContent));
logResult(
scriptMulti.textContent === 'var a = 1;var b = 2;',
"Multiple text nodes: .textContent should equal .text"
);

// Test no text nodes case
const scriptEmpty = document.createElement('script');
const spanElement = document.createElement('span');
spanElement.textContent = 'nested content';
const commentOnly = document.createComment('only comment');

scriptEmpty.appendChild(spanElement);
scriptEmpty.appendChild(commentOnly);

appendDetails("No text nodes .text: " + JSON.stringify(scriptEmpty.text));
logResult(
scriptEmpty.text === '',
"Getter: .text should return empty string when no direct text children"
);
appendDetails("No text nodes .textContent: " + JSON.stringify(scriptEmpty.textContent));
logResult(
scriptEmpty.textContent === 'nested content',
"No text nodes: .textContent should include nested element content"
);
logResult(
scriptEmpty.text !== scriptEmpty.textContent,
"No text nodes: .text should differ from .textContent (spec compliance)"
);

// Test single text node fast path
const scriptSingle = document.createElement('script');
const singleTextNode = document.createTextNode('single text content');
scriptSingle.appendChild(singleTextNode);

appendDetails("Single text node .text: " + JSON.stringify(scriptSingle.text));
logResult(
scriptSingle.text === 'single text content',
"Getter: .text should handle single text node (fast path)"
);
logResult(
scriptSingle.text === scriptSingle.textContent,
"Single text node: .text should equal .textContent"
);
</script>
</body>
</html>
1 change: 1 addition & 0 deletions src/client/html/html_script_element.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <algorithm>
#include <iostream>
#include <limits>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this #include

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the #include <limits> line from html_script_element.cpp.

#include <crates/bindings.hpp>
#include <client/dom/document.hpp>
#include <client/dom/browsing_context.hpp>
Expand Down
8 changes: 8 additions & 0 deletions src/client/html/html_script_element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ namespace endor
src = value;
setAttribute("src", value);
}
inline void setText(const string &value)
{
setTextContent(value);
}
inline string getText() const
{
return textContent();
}

protected:
void createdCallback(bool from_scripting) override;
Expand Down
4 changes: 2 additions & 2 deletions src/client/script_bindings/html/html_script_element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ namespace endor
Isolate *isolate = info.GetIsolate();
HandleScope scope(isolate);
info.GetReturnValue().Set(String::NewFromUtf8(isolate,
handle()->text.c_str())
handle()->getText().c_str())
.ToLocalChecked());
}

Expand All @@ -176,7 +176,7 @@ namespace endor
}

String::Utf8Value text(isolate, value);
handle()->text = *text ? *text : "";
handle()->setText(*text ? *text : "");
}

void HTMLScriptElement::CharsetGetter(const PropertyCallbackInfo<Value> &info)
Expand Down