import { TextNode , Node , Element } from "parse5" ; import { getAttribute , hasClassNameIntersect , getClassNames , } from "./attributes" ; import { backcompatRoots , hasBackcompatMicroformatProperty , BackcompatRoot , } from "../backcompat" ; const classRegex = ( prefix ) : RegExp => new RegExp ( `^${ prefix }-([a-z0-9]+-)?([a-z]+-)*[a-z]+$` ) ; const rootClassRegex = classRegex ( "h" ) ; const propClassRegex = classRegex ( "(p|e|u|dt)" ) ; export const isElement = ( node ) : node is Element => "tagName" in node && "childNodes" in node ; export const isTextNode = ( node ) : node is TextNode => "value" in node ; export const isMicroformatV2Root = ( node ) : boolean => getClassNames ( node ) . some ( ( cl ) => cl . match ( rootClassRegex ) ) ; const isMicroformatV1Root = ( node ) : boolean => hasClassNameIntersect ( node , backcompatRoots ) ; export const isMicroformatRoot = ( node ) : boolean => isMicroformatV2Root ( node ) || isMicroformatV1Root ( node ) ; export const isMicroformatV1Property = ( node , roots ) : boolean => hasBackcompatMicroformatProperty ( node , roots ) ; export const isMicroformatV2Property = ( node ) : boolean => getClassNames ( node , propClassRegex ) . length > 0 ; export const isMicroformatChild = ( node , roots ) : boolean => ! isMicroformatV2Property ( node ) && ! isMicroformatV1Property ( node , roots ) && isMicroformatRoot ( node ) ; export const isBase = ( node ) : boolean => Boolean ( isElement ( node ) && node . tagName === "base" && getAttribute ( node , "href" ) ) ; export const isValueClass = ( node ) : boolean => isElement ( node ) && hasClassNameIntersect ( node , [ "value" , "value-title" ] ) ; export const isRel = ( node ) : boolean => Boolean ( isElement ( node ) && node . attrs . some ( ( attr ) => attr . name === "rel" ) && node . attrs . some ( ( attr ) => attr . name === "href" ) ) ;