AccessibilityModel.js 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. export class AccessibilityNode{constructor(accessibilityModel,payload){this._accessibilityModel=accessibilityModel;this._agent=accessibilityModel._agent;this._id=payload.nodeId;accessibilityModel._setAXNodeForAXId(this._id,this);if(payload.backendDOMNodeId){accessibilityModel._setAXNodeForBackendDOMNodeId(payload.backendDOMNodeId,this);this._backendDOMNodeId=payload.backendDOMNodeId;this._deferredDOMNode=new SDK.DeferredDOMNode(accessibilityModel.target(),payload.backendDOMNodeId);}else{this._backendDOMNodeId=null;this._deferredDOMNode=null;}
  2. this._ignored=payload.ignored;if(this._ignored&&'ignoredReasons'in payload){this._ignoredReasons=payload.ignoredReasons;}
  3. this._role=payload.role||null;this._name=payload.name||null;this._description=payload.description||null;this._value=payload.value||null;this._properties=payload.properties||null;this._childIds=payload.childIds||null;this._parentNode=null;}
  4. accessibilityModel(){return this._accessibilityModel;}
  5. ignored(){return this._ignored;}
  6. ignoredReasons(){return this._ignoredReasons||null;}
  7. role(){return this._role||null;}
  8. coreProperties(){const properties=[];if(this._name){properties.push(({name:'name',value:this._name}));}
  9. if(this._description){properties.push(({name:'description',value:this._description}));}
  10. if(this._value){properties.push(({name:'value',value:this._value}));}
  11. return properties;}
  12. name(){return this._name||null;}
  13. description(){return this._description||null;}
  14. value(){return this._value||null;}
  15. properties(){return this._properties||null;}
  16. parentNode(){return this._parentNode;}
  17. _setParentNode(parentNode){this._parentNode=parentNode;}
  18. isDOMNode(){return!!this._backendDOMNodeId;}
  19. backendDOMNodeId(){return this._backendDOMNodeId;}
  20. deferredDOMNode(){return this._deferredDOMNode;}
  21. highlightDOMNode(){if(!this.deferredDOMNode()){return;}
  22. this.deferredDOMNode().highlight();}
  23. children(){const children=[];if(!this._childIds){return children;}
  24. for(const childId of this._childIds){const child=this._accessibilityModel.axNodeForId(childId);if(child){children.push(child);}}
  25. return children;}
  26. numChildren(){if(!this._childIds){return 0;}
  27. return this._childIds.length;}
  28. hasOnlyUnloadedChildren(){if(!this._childIds||!this._childIds.length){return false;}
  29. return!this._childIds.some(id=>this._accessibilityModel.axNodeForId(id)!==undefined);}
  30. printSelfAndChildren(inspectedNode,leadingSpace){let string=leadingSpace||'';if(this._role){string+=this._role.value;}else{string+='<no role>';}
  31. string+=(this._name?' '+this._name.value:'');string+=' '+this._id;if(this._domNode){string+=' ('+this._domNode.nodeName()+')';}
  32. if(this===inspectedNode){string+=' *';}
  33. for(const child of this.children()){string+='\n'+child.printSelfAndChildren(inspectedNode,(leadingSpace||'')+' ');}
  34. return string;}}
  35. export default class AccessibilityModel extends SDK.SDKModel{constructor(target){super(target);this._agent=target.accessibilityAgent();this._axIdToAXNode=new Map();this._backendDOMNodeIdToAXNode=new Map();}
  36. clear(){this._axIdToAXNode.clear();}
  37. async requestPartialAXTree(node){const payloads=await this._agent.getPartialAXTree(node.id,undefined,undefined,true);if(!payloads){return;}
  38. for(const payload of payloads){new Accessibility.AccessibilityNode(this,payload);}
  39. for(const axNode of this._axIdToAXNode.values()){for(const axChild of axNode.children()){axChild._setParentNode(axNode);}}}
  40. axNodeForId(axId){return this._axIdToAXNode.get(axId);}
  41. _setAXNodeForAXId(axId,axNode){this._axIdToAXNode.set(axId,axNode);}
  42. axNodeForDOMNode(domNode){if(!domNode){return null;}
  43. return this._backendDOMNodeIdToAXNode.get(domNode.backendNodeId());}
  44. _setAXNodeForBackendDOMNodeId(backendDOMNodeId,axNode){this._backendDOMNodeIdToAXNode.set(backendDOMNodeId,axNode);}
  45. logTree(inspectedNode){let rootNode=inspectedNode;while(rootNode.parentNode()){rootNode=rootNode.parentNode();}
  46. console.log(rootNode.printSelfAndChildren(inspectedNode));}}
  47. self.Accessibility=self.Accessibility||{};Accessibility=Accessibility||{};Accessibility.AccessibilityNode=AccessibilityNode;Accessibility.AccessibilityModel=AccessibilityModel;SDK.SDKModel.register(Accessibility.AccessibilityModel,SDK.Target.Capability.DOM,false);