Comments (20)
I don't see your point here:
-
Adding a setAttribute and setAttributes methods (and even the removeAttribute + getAttribute methods added by minas) won't change anything to the users that don't use it. It won't make the lib slower or anything as the code is run only if we need to change an attribute
-
Parsing html and xml and not being able to edit it is really a BIG limitation.
from node-html-parser.
I will close this becuase setAttributes is already added .
from node-html-parser.
Thanks. I've made some change, please let me know if it still bothers you.
from node-html-parser.
Thanks. I've made some change, please let me know if it still bothers you.
from node-html-parser.
Thanks @taoqf!
from node-html-parser.
That was my bad, I forgot to put back the quotes in the raw strings, sorry. But @taoqf isn't your fix a little overcomplicating things? Or did I miss something?
Moreover, with your approach, references made on attributes by an end-user won't be kept updated after using setAttribute
. There is also the issue with encoding: if you decode attributes from raw string, you should probably encode them when you write them back don't you think?
from node-html-parser.
My tests were bad, sorry. I'll write a better one if you want.
from node-html-parser.
- Both
attribute
andrawAttribute
would be changed aftersetAttribute(s)
. You may check the test https://github.com/taoqf/node-html-parser/blob/master/test/html.js#L323 - I could find any encode/decode thing at
rawAttributes
, only decodeattributes
, this may comes from origin registry, and I did know why should we do theencode/decode
thing at attributes. if you know some thing about this, please show me.
Thanks for your report.
from node-html-parser.
And what about this one:
it('should edit the attributes of the element', function () {
var root = parseHTML('<p a=12></p>');
var attr = root.firstChild.attributes;
root.firstChild.setAttribute('a', 13);
attr.should.eql({
'a': '13',
});
root.firstChild.toString().should.eql('<p a=13></p>');
});
from node-html-parser.
About your other question @taoqf , check this
from node-html-parser.
I added your tests and fixed it. Would this be OK?
from node-html-parser.
If I may afford a few remarks.
- In this code:
const val = JSON.stringify(attrs[name]);
if (val === undefined || val === null) {
return name;
return name
is unreachable as val will never be null
or undefined
. It may only be "null"
or "undefined"
-
You fixed the issue with
setAttribute
, but not withsetAttributes
. -
You seem to be taking into account the abilty to handle attributes like:
<input required/>
, which is great. But you'll probably need to makeattributes['required']
andgetAttribute('required')
to returntrue
instead ofundefined
. -
With you implementation,in
setAttributes
andsetAttribute
, you consider that the attributes must be provided encoded. Is this really user friendly?
from node-html-parser.
Thanks for your report.
- I have fixed that.
- I suggest we use
setAttribute
,getAttribute
,hasAttribute
andremoveAttribute
,actually there is no method namedsetAttributes
in browser, just a getterattributes
which is not same behavior in our library. and I think reference is a really bad thing, maybe we just should return a copy in getterattributes
. getAttribute('required')
would return''
, nottrue
.- No, I thin user would never concern about
encode/decode
, see 193a685#diff-8e5f1331d13915fbd871f0a650422099R507
from node-html-parser.
- Yes, there are different points of view about this. Some code architectures make references acceptable and useful, and some make them prohibited and sources of bugs. Do as you wish, but personnally, I consider that
getXXX
will return an independant object whereasobject.XXX
will give me a reference.
there is no method named setAttributes in browser
-> Ok, but how should we edit the attributes then? Or do you want to create a set attributes
setter?
-
You're right, my bad. However, how are we supposed to set such an attribute with setAttribute?
setAttribute('required', '')
will generate<p required=""></p>
-
What I mean is that it would probably be better for users that:
root.firstChild.setAttribute('alt', '«Sogno'); root.firstChild.rawAttributes.alt.should.eql('«Sogno');
Don't you think?
- line 520:
if (arguments.length < 2) {
throw new Error('Failed to execute \'setAttribute\' on \'Element\'');
}
This is not explicit at all... Why not precise that setAttribute requires 2 parameters and received only arguments.length?
from node-html-parser.
Personnally, I would vote for this code:
/**
* Set an attribute value to the HTMLElement
* @param {string} key The attribute name
* @param {string} value The value to set
*/
setAttribute(key: string, value: string) {
//Update the attributes map
const attrs = this.attributes;
attrs[key] = value+'';
//Update the raw attributes
if(this._rawAttrs) this._rawAttrs[key] = encode(value+'');
//Update rawString
this.rawAttrs = Object.keys(attrs).map(attr => attr+(attrs[attr]==='' ? '' : ('="'+encode(attrs[attr])+'"'))).join(' ');
}
/**
* Replace all the attributes of the HTMLElement by the provided attributes
* @param {Attributes} attributes the new attribute set
*/
setAttributes(attributes: Attributes) {
//Update the attributes map
if(this.attributes) {
Object.keys(this.attributes).forEach(key => delete this.attributes[key]);
Object.keys(attributes).forEach(key => this.attributes[key] = attributes[key]+'');
}
//Update the raw attributes map
if(this.rawAttributes) {
Object.keys(this.rawAttributes).forEach(key => delete this.rawAttributes[key]);
Object.keys(attributes).forEach(key => this.rawAttributes[key] = encode(attributes[key]+''));
}
//Update rawString
this.rawAttrs = Object.keys(attributes).map(attr => attr+(attributes[attr]==='' ? '' : ('="'+encode(attributes[attr]+'')+'"'))).join(' ');
}
}
from node-html-parser.
- I don't know either.
- now it is like that. the test will passed.
- I did this just because browser act like this.
from node-html-parser.
Should I propose a PR to fix 3, 4 and 5 and propose an implementation of setAttributes that avoid doing a serie of removeAttribute() / setAttribute() when replacing the current set of attributes?
And about point 2, what is your final position?
from node-html-parser.
About 2,I will say let use setAttribute
. and as you see, I have removed attributes
in doc, https://github.com/taoqf/node-html-parser#htmlelementsetattributekey-string-value-string
prs are always welcomed
from node-html-parser.
Tell me if this looks ok.
I have removed attributes in doc
Why would you do that? How are users supposed to retrieve the list of attributes of a node then?
from node-html-parser.
As a html parser, I think maybe we have gone too far.
I'm afraid I cannot merge your pr for now, I want more voice about this.
Users can use attributes
as before, but I don't encourage them to do that.#23 (review)
First, I would the methods act like browser's behavior, second, I prefer all methods keep running as fast
as before. as I said, jsdom
may be a better choice when users want more ability.
from node-html-parser.
Related Issues (20)
- Line breaks are ignored by innerText HOT 6
- Compile error: Cannot find name 'IterableIterator'. HOT 1
- Mermaid diagram in README.md is overflowing the call stack HOT 1
- abbr tag not parsed correctly
- multiple HTMLElement wrapping after parse HOT 1
- removeAttribute makes boolean attributes render incorrectly.
- querySelectorAll does not work as expected HOT 1
- 100% cpu while parsing document HOT 6
- node_modules/node-html-parser/dist/nodes/comment.d.ts(18,1) | TS1128: Declaration or statement expected. HOT 1
- does it support chrome extension service worker in MV3/V3?
- Bug when parsing <![CDATA[]]> tag which contains <> (angle brackets)
- Enquiry HOT 1
- Incorrect Handling of Empty Class Attributes in SVG Parsing
- Wrong output on malformed HTML
- Missing TS definition for `.rawTagName`
- Comment parsing doesn't seem to work
- Add replaceWith method to text node HOT 1
- null root node when parsing the following HOT 1
- removeWhitespace + outerHTML don't remove meaningless white-spaces inside start and end tags
- BUG: outerHTML drop <script>'s start tag HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-html-parser.