private function cleanAttributes(DOMElement $elem)
    {
        $tagName = strtolower($elem->nodeName);
        $attributes = $elem->attributes;
        $attributesWhiteList = $this->config->WhiteListHtmlGlobalAttributes;
        $attributesFilterMap = array();
        $allowDataAttribute = in_array("data-*", $attributesWhiteList);
        $whiteListAttr = $this->config->getWhiteListAttr($tagName);
        foreach ($whiteListAttr as $key => $val) {
            if (is_string($val)) {
                $attributesWhiteList[] = $val;
            }
            if ($val instanceof Closure) {
                $attributesWhiteList[] = $key;
                $attributesFilterMap[$key] = $val;
            }
        }
        $index = $attributes->length;
        while (--$index >= 0) {
        /*
        foreach会有一个严重的的问题：
            href="JavaScript:alert("customAttr="xxx"");"
            这样嵌套的不合法的属性，会被解析出两个属性：
                1. href="JavaScript:alert("
                2. customAttr="xxx"
            但是foreach只能拿到一个。
        foreach ($elem->attributes->item() as $attrName => $domAttr) {
        */
            /* @var $domAttr DOMAttr */
            $domAttr = $attributes->item($index);
            $attrName = strtolower($domAttr->name);
            $attrValue = $domAttr->value;
            // 如果不在白名单attr中，而且允许data-*，且不是data-*，则删除
            if (!in_array($attrName, $attributesWhiteList) && $allowDataAttribute && (stripos($attrName, "data-") !== 0)) {
                $elem->removeAttribute($attrName);
            } else {
                if (isset($attributesFilterMap[$attrName])) {
                    $filteredAttrValue = $attributesFilterMap[$attrName]($attrValue);
                    if ($filteredAttrValue === false ) {
                        $elem->removeAttribute($attrName);
                    } else {
                        // https://stackoverflow.com/questions/25475936/updating-domattr-value-with-url-results-in-parameters-being-lost-unless-htmlenti
                        // Fix ampersands issue: https://github.com/lincanbin/White-HTML-Filter/issues/1
                        if ($filteredAttrValue !== $attrValue) {
                            $elem->setAttribute($attrName, $filteredAttrValue);
                        }
                    }
                } else {
                    $this->cleanAttrValue($domAttr);
                }
            }
        }
    }