Must pass uppercase to set MSHTML element attribute (.setAttribute) correctly, why? And CaseInsensitive .setAttribute doesn't work

前提是你 提交于 2019-12-25 00:48:44

问题


Basically, I am using .setAttribute to try and add/change the target attributes value in an anchor tag (). The whole problem started because when using .setAttribute "target", "_self" the HTML is written without any double quotes surrounding the "_self", ie: <a href="..." target=_self>link</a>. The only way to get "_self" saved with double quotes around it is to ensure that there is at least one uppercase letter (or more or all uppercase) in the attribute name TARGET, so doing a .setAttribute "TARGET", "_self" will result in the correct HTML, i.e.: <a href="..." TARGET="_self">link</a>.

Turns out, if we try to set like this (.setAttribute "TARGET", "_self") a TARGET as all caps as an attribute for our anchor tag, and there is a lowercase or mixed case target in the anchor HTML element already, then it will just add it as a second attribute, won't overwrite the old one unless its case is exactly the same! i.e.: resulting in: <a href="..." target=_self TARGET="_self">

Odd, since we are required to have at least one upper case character when using .setAttribute like Target or tarGet or targeT for the value to be saved with double quotes (of course, TARGET works too), our options are limited. So, to ensure any non-uppercase targets are removed, we will need to call .removeAttribute on "target" and "Target" and "TARGET" which should cover almost all possibilities, but it's not a 100% guarantee since any person can write it as "TaRgEt".

I tried to get the name of the attribute as it was saved/written (so I can know the exact casing to pass to .removeAttribute), but there seems to be no way to do this unless you parse the .outerHTML of the element. I have tried doing an anElement.Attributes.getNamedItem("target").name and even though the name in the Dom/element was definitely uppercase, it returned it to me as lowercase (target), and there doesn't seem to be any flags in .getNamedItem to override this.

My second method was to loop through all the attributes in the .attributes collection, with For i = 0 to anElement.attributes.length - 1, this showed all attributes (147 of them, including those that weren't specified) and looped through and returned target in lowercase, even though Target stored with camel casing ("Target"). Can't think of other ways to retrieve or figure out exact casing used so I can reliably delete the existing attribute, in order to re-create the attribute TARGET in uppercase, which in turn ensures that the value for TARGET is saved with quotes around it!

Also, just to add, I’ve gotten the same results with .setNamedAttribute, makes no difference. In addition, I have tried the iFlags in .removeAttribute, .getAttribute and .setAttribute to ensure all searches/actions are NOT case sensitive, they do NOT work, and produces results different to what it claims, I can get into a listed analysis of it if you want, but it doesn't solve the problem, and after studying the input/results using the flags, I realized why Microsoft said in their MSDN documentation on those methods to "NOT USE THEM", "DO NOT USE iFlags" etc... warning against their use. Even with the erroneous results, I've tried all combinations, they do not work, not does the case insensitive flag work as expected and as needed.

Just to add, all this applies to anything-MSHTML, if it's the MS implementation of the DOM, the above applies. Although I'm using the WebBrowser control, the IHTMLElement's and IHTMLDomAttribute2's and what not are all MSHTML items.

Basically, this is a question which came out of another question located here: How to Set Attribute Value of <A> Tag in live HTMLDocument with quotes around it?

That question was me trying to figure out how to ensure I save attribute values in MSHTML with quotes around them, after experimenting I realized that the only way to do this was by setting the attribute with its name in UPPERCASE. Then, this resulted in another problem, in that if a non-uppercase version of the attribute name exists already, then it will save it as a second, third etc attribute (as outlined before), and so to prevent this from happening, I need to figure out how to remove all TARGET attributes regardless of case in order to be able to add my own uppercase version where the double quotes around the value are honoured.

Basically, we have to remove all combinations of an attribute before we can ensure the one we create will not be a second one with the same name. In my case its the target attribute, so will have to do a remove for "Target", "tArget, "taRget" etc. until all combinations and permutations of "target" is removed in order to safely add my own. Surely, there has got to be a more elegant way to do this, while still being reliable, any ideas anyone? Thanks for taking the time to read this.

来源:https://stackoverflow.com/questions/9226106/must-pass-uppercase-to-set-mshtml-element-attribute-setattribute-correctly-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!