Skip to content

Commit

Permalink
feat: expose TextNode OriginalText
Browse files Browse the repository at this point in the history
Merge pull request #20 from SomeHats/master
expose TextNode originalText
  • Loading branch information
karlwestin authored Oct 24, 2016
2 parents fcba2ba + 296c940 commit e8357cb
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ TextNode:
nodeName (string) #text or #cdata-section
nodeType (number) 3
textContent (string)
originalText (string)
startPos (position)

note: In DOM3, CDATA is marked as nodeType 4. However, after checking that neither
Expand Down
4 changes: 4 additions & 0 deletions node-gumbo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ Local<Object> read_text(GumboNode* node) {
Nan::Set(obj, Nan::New("nodeType").ToLocalChecked(), type);
Nan::Set(obj, Nan::New("nodeName").ToLocalChecked(), Nan::New(name).ToLocalChecked());
Nan::Set(obj, Nan::New("textContent").ToLocalChecked(), Nan::New(node->v.text.text).ToLocalChecked());
Nan::Set(obj,
Nan::New("originalText").ToLocalChecked(),
Nan::New(node->v.text.original_text.data,
node->v.text.original_text.length).ToLocalChecked());

record_location(obj, &node->v.text.start_pos, "startPos");
return obj;
Expand Down
1 change: 1 addition & 0 deletions test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</head>
<!-- this is a comment -->
<body bgcolor="#ff0fff">
&lt;text content&gt;
<header>

</header>
Expand Down
4 changes: 3 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ reader("/test.html", function(text) {
assert.equal(root.childNodes[4].attributes[0].name, "bgcolor", "should parse attrs");
assert.equal(root.childNodes[4].attributes[0].value, "#ff0fff", "should parse attrs");
console.log("Parses attributes");

assert.equal(root.childNodes[4].childNodes[0].textContent.trim(), "<text content>");
assert.equal(root.childNodes[4].childNodes[0].originalText.trim(), "&lt;text content&gt;");
console.log("Parsers text");
console.log("...done!");
});

Expand Down

0 comments on commit e8357cb

Please sign in to comment.