taoqf / node-html-parser

A very fast HTML parser, generating a simplified DOM, with basic element query support.
MIT License
1.12k stars 112 forks source link

getAttribute should be case insensitive #98

Closed binary-person closed 3 years ago

binary-person commented 3 years ago
const { parse } = require('node-html-parser');
const assert = require('assert');

// works as expected
assert.strictEqual(parse('<a onclick="listener()"></a>').childNodes[0].getAttribute('onclick'), 'listener()');

// should work, since attributes are case-insensitive according to the spec
assert.strictEqual(parse('<a onClick="listener()"></a>').childNodes[0].getAttribute('onclick'), 'listener()');

when parsing, I suggest that the users be given the option to convert all the attributes to lower case because of a performance penalty cost, similar to the option lowerCaseTagName.

taoqf commented 3 years ago

Thank you for your advice. you can try 2.2.0;

binary-person commented 3 years ago

pardon me, but there's one more I forgot to mention. the setAttribute should also be case-insensitive

const { parse } = require('node-html-parser');
const assert = require('assert');

const allLowerCase = parse('<a onclick="listener()"></a>');
allLowerCase.childNodes[0].setAttribute('onCLICK', 'newListener()');

// setAttribute's first argument should be auto-converted to lower case
assert.strictEqual(allLowerCase.toString(), '<a onclick="newListener()"></a>');

const capitalized = parse('<a onClick="listener()"></a>');
capitalized.childNodes[0].setAttribute('onclick', 'newSecondListener()');

// setAttribute should change onclick's attribute regardless of case
assert.strictEqual(capitalized.toString(), '<a onclick="newSecondListener()"></a>');
taoqf commented 3 years ago

My bad.

taoqf commented 3 years ago

2.2.1 fix this.

binary-person commented 3 years ago

the last test doesn't work, but I think that's alright. thank you!