3 lines
28 KiB
JavaScript
3 lines
28 KiB
JavaScript
|
|
(function(global,factory){typeof exports=="object"&&typeof module<"u"?factory(exports):typeof define=="function"&&define.amd?define(["exports"],factory):(global=typeof globalThis<"u"?globalThis:global||self,factory(global.Parchment={}))})(this,function(exports2){"use strict";var Scope=(Scope2=>(Scope2[Scope2.TYPE=3]="TYPE",Scope2[Scope2.LEVEL=12]="LEVEL",Scope2[Scope2.ATTRIBUTE=13]="ATTRIBUTE",Scope2[Scope2.BLOT=14]="BLOT",Scope2[Scope2.INLINE=7]="INLINE",Scope2[Scope2.BLOCK=11]="BLOCK",Scope2[Scope2.BLOCK_BLOT=10]="BLOCK_BLOT",Scope2[Scope2.INLINE_BLOT=6]="INLINE_BLOT",Scope2[Scope2.BLOCK_ATTRIBUTE=9]="BLOCK_ATTRIBUTE",Scope2[Scope2.INLINE_ATTRIBUTE=5]="INLINE_ATTRIBUTE",Scope2[Scope2.ANY=15]="ANY",Scope2))(Scope||{});class Attributor{constructor(attrName,keyName,options={}){this.attrName=attrName,this.keyName=keyName;const attributeBit=Scope.TYPE&Scope.ATTRIBUTE;this.scope=options.scope!=null?options.scope&Scope.LEVEL|attributeBit:Scope.ATTRIBUTE,options.whitelist!=null&&(this.whitelist=options.whitelist)}static keys(node){return Array.from(node.attributes).map(item=>item.name)}add(node,value){return this.canAdd(node,value)?(node.setAttribute(this.keyName,value),!0):!1}canAdd(_node,value){return this.whitelist==null?!0:typeof value=="string"?this.whitelist.indexOf(value.replace(/["']/g,""))>-1:this.whitelist.indexOf(value)>-1}remove(node){node.removeAttribute(this.keyName)}value(node){const value=node.getAttribute(this.keyName);return this.canAdd(node,value)&&value?value:""}}class ParchmentError extends Error{constructor(message){message="[Parchment] "+message,super(message),this.message=message,this.name=this.constructor.name}}const _Registry=class _Registry{constructor(){this.attributes={},this.classes={},this.tags={},this.types={}}static find(node,bubble=!1){if(node==null)return null;if(this.blots.has(node))return this.blots.get(node)||null;if(bubble){let parentNode=null;try{parentNode=node.parentNode}catch{return null}return this.find(parentNode,bubble)}return null}create(scroll,input,value){const match2=this.query(input);if(match2==null)throw new ParchmentError(`Unable to create ${input} blot`);const blotClass=match2,node=input instanceof Node||input.nodeType===Node.TEXT_NODE?input:blotClass.create(value),blot=new blotClass(scroll,node,value);return _Registry.blots.set(blot.domNode,blot),blot}find(node,bubble=!1){return _Registry.find(node,bubble)}query(query,scope=Scope.ANY){let match2;return typeof query=="string"?match2=this.types[query]||this.attributes[query]:query instanceof Text||query.nodeType===Node.TEXT_NODE?match2=this.types.text:typeof query=="number"?query&Scope.LEVEL&Scope.BLOCK?match2=this.types.block:query&Scope.LEVEL&Scope.INLINE&&(match2=this.types.inline):query instanceof Element&&((query.getAttribute("class")||"").split(/\s+/).some(name=>(match2=this.classes[name],!!match2)),match2=match2||this.tags[query.tagName]),match2==null?null:"scope"in match2&&scope&Scope.LEVEL&match2.scope&&scope&Scope.TYPE&match2.scope?match2:null}register(...definitions){return definitions.map(definition=>{const isBlot="blotName"in definition,isAttr="attrName"in definition;if(!isBlot&&!isAttr)throw new ParchmentError("Invalid definition");if(isBlot&&definition.blotName==="abstract")throw new ParchmentError("Cannot register abstract class");const key=isBlot?definition.blotName:isAttr?definition.attrName:void 0;return this.types[key]=definition,isAttr?typeof definition.keyName=="string"&&(this.attributes[definition.keyName]=definition):isBlot&&(definition.className&&(this.classes[definition.className]=definition),definition.tagName&&(Array.isArray(definition.tagName)?definition.tagName=definition.tagName.map(tagName=>tagName.toUpperCase()):definition.tagName=definition.tagName.toUpperCase(),(Array.isArray(definition.tagName)?definition.tagName:[definition.tagName]).forEach(tag=>{(this.tags[tag]==null||definition.className==null)&&(this.tags[tag]=definition)}))),definition})}};_Registry.blots=new WeakMap;let Registry=_Registry;function match(node,prefix){return(node.getAttribute("class")||"").split(/\s+/).filter(name=>n
|
||
|
|
//# sourceMappingURL=parchment.umd.cjs.map
|