Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

innerText #43

Open
christianbundy opened this issue Apr 14, 2020 · 3 comments
Open

innerText #43

christianbundy opened this issue Apr 14, 2020 · 3 comments

Comments

@christianbundy
Copy link
Contributor

Hi! I have some HTML like:

<p><strong>Hello</strong> world</p>

And I want:

Hello world

Usually I'd use innerText, but it doesn't seem to be available here. Is there another way of getting the text that a browser would render? Thanks!

@tdumitrescu
Copy link
Collaborator

How about textContent? Assuming you don't need the fine-grained display awareness of innerText but just a blunt instrument to grab text like in your example...

html-element/index.js

Lines 346 to 359 in b8a15b3

Element.prototype.__defineGetter__('textContent', function() {
var s = '';
this.childNodes.forEach(function(e) {
s += e.textContent;
});
return s;
});
Element.prototype.__defineSetter__('textContent', function(v) {
var textNode = new Text();
textNode.textContent = v;
this.childNodes = [textNode];
return v;
});

@christianbundy
Copy link
Contributor Author

Oh, thanks for the tip. I tried it but received empty string, but I think that's a bug? Am I doing something wrong here?

var document = require('html-element').document;
var div = document.createElement('div')
div.innerHTML = '<p><strong>Hello</strong> world</p>'
div.textContent // => ''

@tdumitrescu
Copy link
Collaborator

Yeah, it's an anemic innerHTML implementation:

html-element/index.js

Lines 260 to 267 in b8a15b3

Element.prototype.__defineSetter__('innerHTML', function(v) {
//only handle this simple case that doesn't need parsing
//this case is useful... parsing is hard and will need added deps!
this.childNodes.length = 0;
// hack to preserve set innerHTML - no parsing just regurgitation
this.childNodes.html = v;
});

AFAICT right now with this lib you'd need to create the elements explicitly like with document.createElement().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants