3 ms·
XPath works with XML, class is a mini language. We had same problem in JavaScript until classList $(element).attr("class").split(/\s+/) It is side effect
by sergeykish 6y ago
XPath works with XML, class is a mini language. We had same problem in JavaScript until classList
$(element).attr("class").split(/\s+/)
It is side effect of one attribute name per element
<a class=foo class=bar>
//*[@class = 'foo']
//*[@class = 'bar']
or having attributes at all
<a>
<class>foo</class>
<class>bar</class>
</a>
//*[class = 'foo']
//*[class = 'bar']
Of course `class` access would be optimized like it is today.
- masklinn 6y ago> XPath works with XML, class is mini language. So? Also class is just a token list. > We had same problem in JavaScript until classList 1. it's much less common to select using javascript than it would be using an xpath selector, hence the issue. 2. because JS is a full-blown programming language, splitting the class into a list is not difficult, and can furthermore be trivially factored into a helper function, or a set thereof (as class modification would really be what you'd want to do) > It is side effect of one attribute name per element It's a side-effect of token lists not being much of a use-case at the time for XPath, despite having been a CSS use-case for 3 years at that point. As my comment notes, XPath 3.1 literally has a contains-token function, that function is compatible with XPath 1.0. If you have an XML processor which allows extension (which would be the case for more or less all of them outside browsers), you can trivially implement your own, or an even more specialised `has-class` function. But contains-token was added in 2017, not in 1999, to say nothing of 1996.
- sergeykish 6y agoIt looks strange to me, language designed to work with trees so turn everything to tree. No need to change serialization, we know format, just parse it — class, URL, CSS. That's what we do in JavaScript [1], [2], [3]. location.host === 'example.com' span.style.color === 'blue' datet.getFullYear() === 2020 //a[href/host = 'example.com'] //span[xstyle/color = 'blue'] //date[datetime/year = '2020'] Just like your proposal but node knows how to parse itself //a[url-host(@href) = 'example.com'] //span[css-color(@style) = 'blue'] //time[datetime-year(@datetime) = '2020'] And it is actual syntax, I've checked on <a><href><host>example.com</host></href></a> <span><xstyle><color>blue</color></style></span> <date><datetime><year>2020</year></datetime></date> * <style> is CDATA, emulated with <xstyle> [1] https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenLis... [2] https://developer.mozilla.org/en-US/docs/Web/API/URL https://developer.mozilla.org/en-US/docs/Web/API/URL [3] https://developer.mozilla.org/en-US/docs/Web/API/CSS_Object_Model https://developer.mozilla.org/en-US/docs/Web/API/CSS_Object_...