Page 1 of 1
Can we get element name where cursor is placed
Posted: Mon Oct 31, 2022 9:20 am
by SmitaPatil
Hi Team,
I want to know that if there is any JavaScript function which you have to know the element name where cursor is placed instead of calling java method every time to check it. I would call that JavaScript method. I have tried following one but couldn't able to get method which will return me element name instead of id.
this.editor.getSelectionManager().getSelection().getCaretPositionInformation()
If you need any more clarification please let me know.
Thanks & Regards,
Smita
Re: Can we get element name where cursor is placed
Posted: Mon Oct 31, 2022 10:35 am
by cristi_talau
Hello,
You can obtain the selection as you mentioned:
Code: Select all
var sel = this.editor.getSelectionManager().getSelection()
Then use one of the methods here:
https://www.oxygenxml.com/maven/com/oxy ... ction.html, for example:
Code: Select all
sel.getNodeAtSelection().getTagName()
Best,
Cristian
Re: Can we get element name where cursor is placed
Posted: Mon Oct 31, 2022 11:09 am
by SmitaPatil
Hi Cristian,
Thank you so much!!.
just one more question, can you please tell the method for just checking if the given element has child element with some "xyz" name.
Thanks & Regards,
Smita
Re: Can we get element name where cursor is placed
Posted: Mon Oct 31, 2022 11:15 am
by cristi_talau
Hello,
The getNodeAtSelection() method returns a node which implements the DOM level 3 API. So, you ca use properties like childNodes on it.
Or do you want to check whether that node can have a child "xyz"?
Best,
Cristian
Re: Can we get element name where cursor is placed
Posted: Mon Oct 31, 2022 1:00 pm
by SmitaPatil
Hi Cristian,
I want to check whether that node can have a child "xyz"?
Thanks & Regards,
Smita
Re: Can we get element name where cursor is placed
Posted: Tue Nov 01, 2022 11:53 am
by cristi_talau
Hello,
There is no JS API for that. You have two options:
1. Use a hard-coded list of possible children / possible parents.
2. Use a Java API:
https://www.oxygenxml.com/InstData/Edit ... reContext- .
Best,
Cristian
Re: Can we get element name where cursor is placed
Posted: Wed Nov 02, 2022 11:43 am
by SmitaPatil
Hi Cristian,
Thank you.
Can you tell me how we can differentiate between other elements or tag and comment tag.
just wanted to know if there any method to get to know whether element is comment or not where cursor has placed.
Thanks & Regards,
Smita
Re: Can we get element name where cursor is placed
Posted: Wed Nov 02, 2022 11:59 am
by cristi_talau
Hello,
You can use the "nodeType" property of the node:
https://developer.mozilla.org/en-US/doc ... e/nodeType .
Best,
Cristian
Re: Can we get element name where cursor is placed
Posted: Wed Nov 02, 2022 12:48 pm
by SmitaPatil
Hi Cristian,
Thank you.
I tried unwrapping <#comment> element but its not working.
Can you please tell how we can just remove comment tag and just keep the text which is present inside comment.
Thanks & Regards,
Smita
Re: Can we get element name where cursor is placed
Posted: Wed Nov 02, 2022 1:41 pm
by cristi_talau
Hello,
If you talk about using UnwrapTagsOperation, it does not work for comments. What you can do instead is to use a custom AuthorOperation that reads the content of an XML comment, delete the XML comment node and insert the content back.
Best,
Cristian