Esempio n. 1
0
        /// <summary> Validates a basic selector against the policy
        ///
        /// </summary>
        /// <param name="selector">the object representation of the selector
        /// </param>
        /// <param name="results">the <code>CleanResults</code> object to add any error
        /// messages to
        /// </param>
        /// <returns> true if this selector name is valid; false otherwise
        /// </returns>
        private bool validateSimpleSelector(SimpleSelector selector, CleanResults results)
        {
            // ensure the name follows the valid pattern and is not blacklisted
            // by the exclusion pattern.
            // NOTE: intentionally using non-short-circuited AND operator to
            // generate all relevant error messages
            //return policy.getRegularExpression("cssElementSelector").Pattern.matcher(selector.toString().toLowerCase()).matches() & !policy.getRegularExpression("cssElementExclusion").Pattern.matcher(selector.toString().toLowerCase()).matches();

            string name = ((ElementSelector)selector).getLocalName().ToLower();

            string css = (policy.getRegularExpression("cssElementSelector") != null ? policy.getRegularExpression("cssElementSelector") : "");
            string exc = (policy.getRegularExpression("cssElementExclusion") != null ? policy.getRegularExpression("cssElementExclusion") : "");

            css = "^" + css + "$";
            exc = "^" + exc + "$";

            Match m1 = Regex.Match(name, css);
            Match m2 = Regex.Match(name, exc);

            return(m1.Success & !m2.Success);
        }
Esempio n. 2
0
        /// <summary> Validates a basic selector against the policy
        /// 
        /// </summary>
        /// <param name="selector">the object representation of the selector
        /// </param>
        /// <param name="results">the <code>CleanResults</code> object to add any error
        /// messages to
        /// </param>
        /// <returns> true if this selector name is valid; false otherwise
        /// </returns>
        private bool validateSimpleSelector(SimpleSelector selector, CleanResults results)
        {
            // ensure the name follows the valid pattern and is not blacklisted
            // by the exclusion pattern.
            // NOTE: intentionally using non-short-circuited AND operator to
            // generate all relevant error messages
            //return policy.getRegularExpression("cssElementSelector").Pattern.matcher(selector.toString().toLowerCase()).matches() & !policy.getRegularExpression("cssElementExclusion").Pattern.matcher(selector.toString().toLowerCase()).matches();

            string name = ((ElementSelector)selector).getLocalName().ToLower();

            string css = (policy.getRegularExpression("cssElementSelector") != null ? policy.getRegularExpression("cssElementSelector") : "");
            string exc = (policy.getRegularExpression("cssElementExclusion") != null ? policy.getRegularExpression("cssElementExclusion") : "");

            css = "^" + css + "$";
            exc = "^" + exc + "$";

            Match m1 = Regex.Match(name, css);
            Match m2 = Regex.Match(name, exc);

            return m1.Success & !m2.Success;
        }
Esempio n. 3
0
 /// <summary> Validates a basic condition against the white list pattern and the
 /// blacklist pattern
 /// 
 /// </summary>
 /// <param name="condition">the object representation of the condition
 /// </param>
 /// <param name="pattern">the positive pattern of valid conditions
 /// </param>
 /// <param name="exclusionPattern">the negative pattern of excluded conditions
 /// </param>
 /// <param name="results">the <code>CleanResults</code> object to add any error
 /// messages to
 /// </param>
 /// <returns> true if this selector name is valid; false otherwise
 /// </returns>
 private bool validateCondition(AttributeCondition condition, string pattern, string exclusionPattern, CleanResults results)
 {
     // check that the name of the condition matches valid pattern and does
     // not match exclusion pattern
     // NOTE: intentionally using non-short-circuited AND operator to
     // generate all relevant error messages
     //return pattern.Pattern.matcher(condition.toString().toLowerCase()).matches() & !exclusionPattern.Pattern.matcher(condition.toString().toLowerCase()).matches();
     return true;
 }
Esempio n. 4
0
        /// <summary> Determines whether the given selector name is valid according to this
        /// validator's policy.
        /// 
        /// </summary>
        /// <param name="selectorName">the name of the selector
        /// </param>
        /// <param name="selector">the object representation of the selector
        /// </param>
        /// <param name="results">the <code>CleanResults</code> object to add any error
        /// messages to
        /// </param>
        /// <returns> true if this selector name is valid; false otherwise
        /// </returns>
        public virtual bool isValidSelector(System.String selectorName, Selector selector, CleanResults results)
        {
            // determine correct behavior
            switch (selector.getSelectorType())
            {

                case CssValidator.SAC_ANY_NODE_SELECTOR:
                case CssValidator.SAC_ELEMENT_NODE_SELECTOR:
                case CssValidator.SAC_PSEUDO_ELEMENT_SELECTOR:
                case CssValidator.SAC_ROOT_NODE_SELECTOR:
                    // these selectors are the most base selectors
                    return validateSimpleSelector((SimpleSelector)selector, results);

                case CssValidator.SAC_CHILD_SELECTOR:
                case CssValidator.SAC_DESCENDANT_SELECTOR:
                    // these are compound selectors - decompose into simple selectors
                    DescendantSelector descSelector = (DescendantSelector)selector;
                    return isValidSelector(selectorName, descSelector.getSimpleSelector(), results) & isValidSelector(selectorName, descSelector.getAncestorSelector(), results);

                case CssValidator.SAC_CONDITIONAL_SELECTOR:
                    // this is a compound selector - decompose into simple selectors
                    ConditionalSelector condSelector = (ConditionalSelector)selector;
                    return isValidSelector(selectorName, condSelector.getSimpleSelector(), results) & isValidCondition(selectorName, condSelector.getCondition(), results);

                case CssValidator.SAC_DIRECT_ADJACENT_SELECTOR:
                    // this is a compound selector - decompose into simple selectors
                    SiblingSelector sibSelector = (SiblingSelector)selector;
                    return isValidSelector(selectorName, sibSelector.getSiblingSelector(), results) & isValidSelector(selectorName, sibSelector.getSelector(), results);

                case CssValidator.SAC_NEGATIVE_SELECTOR:
                    // this is a compound selector with one simple selector
                    return validateSimpleSelector((NegativeSelector)selector, results);

                case CssValidator.SAC_CDATA_SECTION_NODE_SELECTOR:
                case CssValidator.SAC_COMMENT_NODE_SELECTOR:
                case CssValidator.SAC_PROCESSING_INSTRUCTION_NODE_SELECTOR:
                case CssValidator.SAC_TEXT_NODE_SELECTOR:
                default:
                    results.addErrorMessage("Unknown selector in selector " + HTMLEntityEncoder.htmlEntityEncode(selectorName) + " encountered");
                    return false;
            }
        }
Esempio n. 5
0
        /// <summary> Determines whether the given condition is valid according to this
        /// validator's policy.
        /// 
        /// </summary>
        /// <param name="selectorName">the name of the selector that contains this condition
        /// </param>
        /// <param name="condition">the object representation of this condition
        /// </param>
        /// <param name="results">the <code>CleanResults</code> object to add any error
        /// messages to
        /// </param>
        /// <returns> true if this condition is valid; false otherwise
        /// </returns>
        public virtual bool isValidCondition(string selectorName, Condition condition, CleanResults results)
        {
            switch (condition.getConditionType())
            {

                //case CssValidator.SAC_AND_CONDITION:
                case CssValidator.SAC_OR_CONDITION:
                    // these are compound condition - decompose into simple conditions
                    CombinatorCondition comboCondition = (CombinatorCondition)condition;
                    return isValidCondition(selectorName, comboCondition.getFirstCondition(), results) & isValidCondition(selectorName, comboCondition.getSecondCondition(), results);

                case CssValidator.SAC_CLASS_CONDITION:
                    // this is a basic class condition; compare condition against
                    // valid pattern and is not blacklisted by exclusion pattern
                    return validateCondition((AttributeCondition)condition, policy.getRegularExpression("cssClassSelector"), policy.getRegularExpression("cssClassExclusion"), results);

                case CssValidator.SAC_ID_CONDITION:
                    // this is a basic ID condition; compare condition against
                    // valid pattern and is not blacklisted by exclusion pattern
                    return validateCondition((AttributeCondition)condition, policy.getRegularExpression("cssIDSelector"), policy.getRegularExpression("cssIDExclusion"), results);

                case CssValidator.SAC_PSEUDO_CLASS_CONDITION:
                    // this is a basic psuedo element condition; compare condition
                    // against valid pattern and is not blacklisted by exclusion pattern
                    return validateCondition((AttributeCondition)condition, policy.getRegularExpression("cssPseudoElementSelector"), policy.getRegularExpression("cssPsuedoElementExclusion"), results);

                case CssValidator.SAC_BEGIN_HYPHEN_ATTRIBUTE_CONDITION:
                case CssValidator.SAC_ONE_OF_ATTRIBUTE_CONDITION:
                case CssValidator.SAC_ATTRIBUTE_CONDITION:
                    // this is a basic class condition; compare condition against
                    // valid pattern and is not blacklisted by exclusion pattern
                    return validateCondition((AttributeCondition)condition, policy.getRegularExpression("cssAttributeSelector"), policy.getRegularExpression("cssAttributeExclusion"), results);

                case CssValidator.SAC_NEGATIVE_CONDITION:
                    // this is a compound condition; decompose to simple condition
                    return isValidCondition(selectorName, ((NegativeCondition)condition).getCondition(), results);

                case CssValidator.SAC_ONLY_CHILD_CONDITION:
                case CssValidator.SAC_ONLY_TYPE_CONDITION:
                    // :only-child and :only-of-type are constants
                    return true;

                case CssValidator.SAC_POSITIONAL_CONDITION:
                case CssValidator.SAC_CONTENT_CONDITION:
                case CssValidator.SAC_LANG_CONDITION:
                default:
                    results.addErrorMessage("Unknown condition for selector " + HTMLEntityEncoder.htmlEntityEncode(selectorName) + " encountered");
                    return false;
            }
        }
Esempio n. 6
0
 /// <summary> Validates a basic condition against the white list pattern and the
 /// blacklist pattern
 ///
 /// </summary>
 /// <param name="condition">the object representation of the condition
 /// </param>
 /// <param name="pattern">the positive pattern of valid conditions
 /// </param>
 /// <param name="exclusionPattern">the negative pattern of excluded conditions
 /// </param>
 /// <param name="results">the <code>CleanResults</code> object to add any error
 /// messages to
 /// </param>
 /// <returns> true if this selector name is valid; false otherwise
 /// </returns>
 private bool validateCondition(AttributeCondition condition, string pattern, string exclusionPattern, CleanResults results)
 {
     // check that the name of the condition matches valid pattern and does
     // not match exclusion pattern
     // NOTE: intentionally using non-short-circuited AND operator to
     // generate all relevant error messages
     //return pattern.Pattern.matcher(condition.toString().toLowerCase()).matches() & !exclusionPattern.Pattern.matcher(condition.toString().toLowerCase()).matches();
     return(true);
 }
Esempio n. 7
0
        /// <summary> Determines whether the given condition is valid according to this
        /// validator's policy.
        ///
        /// </summary>
        /// <param name="selectorName">the name of the selector that contains this condition
        /// </param>
        /// <param name="condition">the object representation of this condition
        /// </param>
        /// <param name="results">the <code>CleanResults</code> object to add any error
        /// messages to
        /// </param>
        /// <returns> true if this condition is valid; false otherwise
        /// </returns>
        public virtual bool isValidCondition(string selectorName, Condition condition, CleanResults results)
        {
            switch (condition.getConditionType())
            {
            //case CssValidator.SAC_AND_CONDITION:
            case CssValidator.SAC_OR_CONDITION:
                // these are compound condition - decompose into simple conditions
                CombinatorCondition comboCondition = (CombinatorCondition)condition;
                return(isValidCondition(selectorName, comboCondition.getFirstCondition(), results) & isValidCondition(selectorName, comboCondition.getSecondCondition(), results));

            case CssValidator.SAC_CLASS_CONDITION:
                // this is a basic class condition; compare condition against
                // valid pattern and is not blacklisted by exclusion pattern
                return(validateCondition((AttributeCondition)condition, policy.getRegularExpression("cssClassSelector"), policy.getRegularExpression("cssClassExclusion"), results));

            case CssValidator.SAC_ID_CONDITION:
                // this is a basic ID condition; compare condition against
                // valid pattern and is not blacklisted by exclusion pattern
                return(validateCondition((AttributeCondition)condition, policy.getRegularExpression("cssIDSelector"), policy.getRegularExpression("cssIDExclusion"), results));

            case CssValidator.SAC_PSEUDO_CLASS_CONDITION:
                // this is a basic psuedo element condition; compare condition
                // against valid pattern and is not blacklisted by exclusion pattern
                return(validateCondition((AttributeCondition)condition, policy.getRegularExpression("cssPseudoElementSelector"), policy.getRegularExpression("cssPsuedoElementExclusion"), results));

            case CssValidator.SAC_BEGIN_HYPHEN_ATTRIBUTE_CONDITION:
            case CssValidator.SAC_ONE_OF_ATTRIBUTE_CONDITION:
            case CssValidator.SAC_ATTRIBUTE_CONDITION:
                // this is a basic class condition; compare condition against
                // valid pattern and is not blacklisted by exclusion pattern
                return(validateCondition((AttributeCondition)condition, policy.getRegularExpression("cssAttributeSelector"), policy.getRegularExpression("cssAttributeExclusion"), results));

            case CssValidator.SAC_NEGATIVE_CONDITION:
                // this is a compound condition; decompose to simple condition
                return(isValidCondition(selectorName, ((NegativeCondition)condition).getCondition(), results));

            case CssValidator.SAC_ONLY_CHILD_CONDITION:
            case CssValidator.SAC_ONLY_TYPE_CONDITION:
                // :only-child and :only-of-type are constants
                return(true);

            case CssValidator.SAC_POSITIONAL_CONDITION:
            case CssValidator.SAC_CONTENT_CONDITION:
            case CssValidator.SAC_LANG_CONDITION:
            default:
                results.addErrorMessage("Unknown condition for selector " + HTMLEntityEncoder.htmlEntityEncode(selectorName) + " encountered");
                return(false);
            }
        }
Esempio n. 8
0
        /// <summary> Determines whether the given selector name is valid according to this
        /// validator's policy.
        ///
        /// </summary>
        /// <param name="selectorName">the name of the selector
        /// </param>
        /// <param name="selector">the object representation of the selector
        /// </param>
        /// <param name="results">the <code>CleanResults</code> object to add any error
        /// messages to
        /// </param>
        /// <returns> true if this selector name is valid; false otherwise
        /// </returns>
        public virtual bool isValidSelector(System.String selectorName, Selector selector, CleanResults results)
        {
            // determine correct behavior
            switch (selector.getSelectorType())
            {
            case CssValidator.SAC_ANY_NODE_SELECTOR:
            case CssValidator.SAC_ELEMENT_NODE_SELECTOR:
            case CssValidator.SAC_PSEUDO_ELEMENT_SELECTOR:
            case CssValidator.SAC_ROOT_NODE_SELECTOR:
                // these selectors are the most base selectors
                return(validateSimpleSelector((SimpleSelector)selector, results));

            case CssValidator.SAC_CHILD_SELECTOR:
            case CssValidator.SAC_DESCENDANT_SELECTOR:
                // these are compound selectors - decompose into simple selectors
                DescendantSelector descSelector = (DescendantSelector)selector;
                return(isValidSelector(selectorName, descSelector.getSimpleSelector(), results) & isValidSelector(selectorName, descSelector.getAncestorSelector(), results));

            case CssValidator.SAC_CONDITIONAL_SELECTOR:
                // this is a compound selector - decompose into simple selectors
                ConditionalSelector condSelector = (ConditionalSelector)selector;
                return(isValidSelector(selectorName, condSelector.getSimpleSelector(), results) & isValidCondition(selectorName, condSelector.getCondition(), results));

            case CssValidator.SAC_DIRECT_ADJACENT_SELECTOR:
                // this is a compound selector - decompose into simple selectors
                SiblingSelector sibSelector = (SiblingSelector)selector;
                return(isValidSelector(selectorName, sibSelector.getSiblingSelector(), results) & isValidSelector(selectorName, sibSelector.getSelector(), results));

            case CssValidator.SAC_NEGATIVE_SELECTOR:
                // this is a compound selector with one simple selector
                return(validateSimpleSelector((NegativeSelector)selector, results));

            case CssValidator.SAC_CDATA_SECTION_NODE_SELECTOR:
            case CssValidator.SAC_COMMENT_NODE_SELECTOR:
            case CssValidator.SAC_PROCESSING_INSTRUCTION_NODE_SELECTOR:
            case CssValidator.SAC_TEXT_NODE_SELECTOR:
            default:
                results.addErrorMessage("Unknown selector in selector " + HTMLEntityEncoder.htmlEntityEncode(selectorName) + " encountered");
                return(false);
            }
        }