예제 #1
0
        private void Transform(ElementNode node, IList <Node> body)
        {
            AttributeNode forAttribute = node.GetAttribute("for");
            AttributeNode forType      = node.GetAttribute("fortype");
            AttributeNode forProperty  = node.GetAttribute("forproperty");

            if (forAttribute != null)
            {
                // put in as content property
                node.Attributes.Remove(forAttribute);
                node.RemoveAttributesByName("name");
                node.RemoveAttributesByName("value");
                var nameNode = new ExpressionNode(forAttribute.Value.GetPropertyNameSnippet());

                var valueNode = new ConditionNode("resource!=null")
                {
                    Nodes = new List <Node>()
                    {
                        new ExpressionNode(forAttribute.Value)
                    }
                };
                SetNodeNameAndValue(node, valueNode, nameNode, body, forAttribute);
            }
            else if (forType != null)
            {
                if (forProperty == null)
                {
                    throw new Exception("Must have both a forProperty attribute if using the forType attribute.");
                }
                node.Attributes.Remove(forType);
                node.Attributes.Remove(forProperty);
                node.RemoveAttributesByName("name");
                SetNodeNameAndValue(node, null, new TextNode(string.Concat(forType.Value, ".", forProperty.Value)), body, forAttribute);
            }
        }
        public override IList <IHtmlValidationError> ValidateElement(ElementNode element)
        {
            var results = new ValidationErrorCollection();

            if (element.Name != "meta" || element.GetAttribute("property") == null)
            {
                return(results);
            }

            AttributeNode property = element.GetAttribute("property");

            if (property.Value.StartsWith("og:", StringComparison.Ordinal))
            {
                ElementNode head = element.Parent;

                if (head != null && head.Name == "head")
                {
                    AttributeNode prefix = head.GetAttribute("prefix");
                    int           index  = element.Attributes.IndexOf(property);

                    if (prefix == null)
                    {
                        results.AddAttributeError(element, "The 'prefix' attribute on <head> for OpenGraph is missing", HtmlValidationErrorLocation.AttributeValue, index);
                    }
                    else if (prefix.Value.IndexOf("og:", StringComparison.Ordinal) == -1)
                    {
                        results.AddAttributeError(element, "To use OpenGraph, you must add the value 'og: http://ogp.me/ns#' to 'prefix' in <head>", HtmlValidationErrorLocation.AttributeValue, index);
                    }
                }
            }

            return(results);
        }
예제 #3
0
    private MarkdownPage ParsePage(string fileName)
    {
        string html = CommonMark.CommonMarkConverter.Convert(File.ReadAllText(fileName));

        HtmlTree tree = new HtmlTree(new TextStream(html));

        tree.Build();

        ElementNode firstChild = tree.RootNode.Children[0];
        ElementNode prop       = firstChild.Children[0];

        MarkdownPage page = new MarkdownPage();

        page.Title        = AttrValue(prop, "pageTitle", Path.GetFileNameWithoutExtension(fileName));
        page.Description  = AttrValue(prop, "description", page.Title);
        page.Content      = html.Substring(firstChild.End, tree.RootNode.Length - firstChild.End).Trim();
        page.Keywords     = AttrValue(prop, "keywords", page.Title);
        page.Slug         = AttrValue(prop, "slug", page.Title.ToLowerInvariant());
        page.DateModified = File.GetLastWriteTime(fileName);
        page.FileName     = fileName.Replace(BaseDirectory, string.Empty).Replace("\\", "/");
        page.ShowInMenu   = Path.GetFileName(fileName).StartsWith("_") ? false : true;

        if (prop.GetAttribute("order") != null)
        {
            page.Order = int.Parse(prop.GetAttribute("order").Value);
        }
        else
        {
            page.Order = 1000 + page.Title[0];
        }

        return(page);
    }
예제 #4
0
        private bool TryGetPath(out string path)
        {
            int position = TextView.Caret.Position.BufferPosition.Position;

            path = null;

            ElementNode   element = null;
            AttributeNode attr    = null;

            _tree.GetPositionElement(position, out element, out attr);

            if (element == null)
            {
                return(false);
            }

            attr = element.GetAttribute("src") ?? element.GetAttribute("href");

            if (attr != null)
            {
                path = attr.Value;
                return(true);
            }

            return(false);
        }
예제 #5
0
        public IEnumerable <ISuggestedAction> GetSuggestedActions(ITextView textView, ITextBuffer textBuffer, int caretPosition, ElementNode element, AttributeNode attribute, HtmlPositionType positionType)
        {
            AttributeNode ngController = element.GetAttribute("ng-controller") ?? element.GetAttribute("data-ng-controller");

            return(new ISuggestedAction[] {
                new HtmlAngularControllerLightBulbAction(textView, textBuffer, element, ngController)
            });
        }
        public IHtmlSmartTag TryCreateSmartTag(ITextView textView, ITextBuffer textBuffer, ElementNode element, AttributeNode attribute, int caretPosition, HtmlPositionType positionType)
        {
            AttributeNode attr = element.GetAttribute("src") ?? element.GetAttribute("href");

            if (attr == null)
                return null;

            Uri url = NormalizeUrl(attr);

            if (url == null || (!attr.Value.StartsWith("//", StringComparison.Ordinal) && !attr.Value.Contains("://")))
                return null;

            return new RemoteDownloaderSmartTag(textView, textBuffer, element, attr);
        }
        private static bool IsEnabled(ElementNode element)
        {
            if (element.Name != "img")
                return false;

            return element.GetAttribute("src", true) != null;
        }
예제 #8
0
파일: PackageInfo.cs 프로젝트: xoriath/RTVS
            private static NamedItemType GetItemType(string name, ElementNode td)
            {
                if (Constants.IsConstant(name) || Logicals.IsLogical(name) || name.StartsWithIgnoreCase("R_"))
                {
                    return(NamedItemType.Constant);
                }

                if (td.Children.Count == 1)
                {
                    ElementNode   a    = td.Children[0];
                    AttributeNode href = a.GetAttribute("href");

                    if (href != null && href.Value != null)
                    {
                        if (href.Value.IndexOf("constant", StringComparison.OrdinalIgnoreCase) >= 0)
                        {
                            return(NamedItemType.Constant);
                        }
                        else if (href.Value.IndexOf("-package", StringComparison.OrdinalIgnoreCase) >= 0)
                        {
                            return(NamedItemType.None);
                        }
                    }
                }

                return(NamedItemType.Function);
            }
예제 #9
0
        public override IList <IHtmlValidationError> ValidateElement(ElementNode element)
        {
            var results = new ValidationErrorCollection();

            if (!WESettings.Instance.Html.EnableBootstrapValidation)
            {
                return(results);
            }

            var elementClasses = element.GetAttribute("class");

            if (elementClasses == null || !elementClasses.Value.Split(' ').Any(x => x.StartsWith("col-", StringComparison.CurrentCulture)))
            {
                return(results);
            }

            // Bootstrap grid system require a parent <div class='row ... to work
            if (IsParentDivElementMissingRowClass(element))
            {
                results.AddAttributeError(element,
                                          string.Format(CultureInfo.CurrentCulture, _errorRowMissing, elementClasses.Value.Split(' ').Where(x => x.StartsWith("col-", StringComparison.CurrentCulture)).First()),
                                          HtmlValidationErrorLocation.AttributeValue,
                                          element.Attributes.IndexOf(elementClasses));
            }

            return(results);
        }
예제 #10
0
        public override IList <IHtmlValidationError> ValidateElement(ElementNode element)
        {
            var results = new ValidationErrorCollection();

            if (!WESettings.Instance.Html.EnableBootstrapValidation)
            {
                return(results);
            }

            var           classNames         = element.GetAttribute("class");
            List <string> childrenClassNames = null;

            if (classNames == null)
            {
                return(results);
            }

            foreach (string token in _tokens)
            {
                childrenClassNames = GetIfTokenIsDependent(token, element);

                if (!IsCorrect(classNames.Value, token, childrenClassNames))
                {
                    int    index    = element.Attributes.IndexOf(classNames);
                    string offender = GetOffendingClassName(classNames.Value, token);
                    string error    = string.Format(CultureInfo.CurrentCulture, _error, offender, token);

                    results.AddAttributeError(element, error, HtmlValidationErrorLocation.AttributeValue, index);
                }
            }

            return(results);
        }
        public override IList <IHtmlValidationError> ValidateElement(ElementNode element)
        {
            var results = new ValidationErrorCollection();

            if (!WESettings.Instance.Html.EnableFoundationValidation)
            {
                return(results);
            }

            var classNames = element.GetAttribute("class");

            if (classNames == null)
            {
                return(results);
            }

            if (!ColumnPairElementsOk(classNames.Value))
            {
                int index = element.Attributes.IndexOf(classNames);
                var specificErrorMessage = classNames.Value.Contains("column") ? _errorMissingSize : _errorMissingColumns;

                results.AddAttributeError(element, specificErrorMessage, HtmlValidationErrorLocation.AttributeValue, index);
            }

            return(results);
        }
예제 #12
0
        public bool HasSuggestedActions(ITextView textView, ITextBuffer textBuffer, int caretPosition, ElementNode element, AttributeNode attribute, HtmlPositionType positionType)
        {
            if (!element.StartTag.Contains(caretPosition))
            {
                return(false);
            }

            string url = (element.GetAttribute("src") ?? element.GetAttribute("href"))?.Value;

            if (string.IsNullOrEmpty(url) || (!url.Contains("://") && !url.StartsWith("//")))
            {
                return(false);
            }

            return(element.IsElement("style") || element.IsElement("script"));
        }
예제 #13
0
            public async override void Invoke()
            {
                ITextBuffer     textBuffer = this.HtmlSmartTag.TextBuffer;
                ElementNode     element    = this.HtmlSmartTag.Element;
                AttributeNode   src        = element.GetAttribute("src", true);
                ImageCompressor compressor = new ImageCompressor();

                bool isDataUri = src.Value.StartsWith("data:image/", StringComparison.Ordinal);

                if (isDataUri)
                {
                    string dataUri = await compressor.CompressDataUriAsync(src.Value);

                    if (dataUri.Length < src.Value.Length)
                    {
                        using (WebEssentialsPackage.UndoContext("Optimize image"))
                        {
                            Span span = Span.FromBounds(src.ValueRangeUnquoted.Start, src.ValueRangeUnquoted.End);
                            textBuffer.Replace(span, dataUri);
                        }
                    }
                }
                else
                {
                    var fileName = ImageQuickInfo.GetFullUrl(src.Value, textBuffer);

                    if (string.IsNullOrEmpty(fileName) || !ImageCompressor.IsFileSupported(fileName) || !File.Exists(fileName))
                    {
                        return;
                    }

                    await compressor.CompressFilesAsync(fileName);
                }
            }
        public override void DoReplace(ElementNode node, IList <Node> body)
        {
            body.Clear();
            Node newBody = node.GetAttribute("for").Value.GetPropertyValueNode();

            body.Add(newBody);
        }
        public IEnumerable <ISuggestedAction> GetSuggestedActions(ITextView textView, ITextBuffer textBuffer, int caretPosition, ElementNode element, AttributeNode attribute, HtmlPositionType positionType)
        {
            AttributeNode src = element.GetAttribute("src");

            return(new ISuggestedAction[] {
                new HtmlBase64DecodeLightBulbAction(textView, textBuffer, element, src)
            });
        }
예제 #16
0
        public IHtmlSmartTag TryCreateSmartTag(ITextView textView, ITextBuffer textBuffer, ElementNode element, AttributeNode attribute, int caretPosition, HtmlPositionType positionType)
        {
            if (element.GetAttribute("class") == null)
            {
                return(new $safeitemname$SmartTag(textView, textBuffer, element));
            }

            return(null);
        }
예제 #17
0
        public IHtmlSmartTag TryCreateSmartTag(ITextView textView, ITextBuffer textBuffer, ElementNode element, AttributeNode attribute, int caretPosition, HtmlPositionType positionType)
        {
            if (element.GetAttribute("class") == null)
            {
                return new $safeitemname$SmartTag(textView, textBuffer, element);
            }

            return null;
        }
예제 #18
0
        public IHtmlSmartTag TryCreateSmartTag(ITextView textView, ITextBuffer textBuffer, ElementNode element, AttributeNode attribute, int caretPosition, HtmlPositionType positionType)
        {
            AttributeNode attr = element.GetAttribute("src") ?? element.GetAttribute("href");

            if (attr == null)
            {
                return(null);
            }

            Uri url = NormalizeUrl(attr);

            if (url == null || (!attr.Value.StartsWith("//", StringComparison.Ordinal) && !attr.Value.Contains("://")))
            {
                return(null);
            }

            return(new RemoteDownloaderSmartTag(textView, textBuffer, element, attr));
        }
예제 #19
0
        public IHtmlSmartTag TryCreateSmartTag(ITextView textView, ITextBuffer textBuffer, ElementNode element, AttributeNode attribute, int caretPosition, HtmlPositionType positionType)
        {
            if (element.GetAttribute("ng-controller") != null)
            {
                return(new HtmlAngularControllerSmartTag(textView, textBuffer, element));
            }

            return(null);
        }
예제 #20
0
        public static void RemoveAttributesByName(this ElementNode node, string name)
        {
            var attribute = node.GetAttribute(name);

            if (attribute != null)
            {
                node.Attributes.Remove(attribute);
            }
        }
        public IHtmlSmartTag TryCreateSmartTag(ITextView textView, ITextBuffer textBuffer, ElementNode element, AttributeNode attribute, int caretPosition, HtmlPositionType positionType)
        {
            if (element.GetAttribute("ng-controller") != null)
            {
                return new HtmlAngularControllerSmartTag(textView, textBuffer, element);
            }

            return null;
        }
        public bool Visit(ElementNode element, object parameter)
        {
            if (element.Name.Equals("datalist", StringComparison.OrdinalIgnoreCase) && element.HasAttribute("id"))
            {
                var list = (HashSet <string>)parameter;
                list.Add(element.GetAttribute("id").Value);
            }

            return(true);
        }
예제 #23
0
        public bool Visit(ElementNode element, object parameter)
        {
            if (element.HasAttribute("id"))
            {
                var list = (HashSet <string>)parameter;
                list.Add(element.GetAttribute("id").Value);
            }

            return(true);
        }
        public bool HasSuggestedActions(ITextView textView, ITextBuffer textBuffer, int caretPosition, ElementNode element, AttributeNode attribute, HtmlPositionType positionType)
        {
            if (element.Name != "img")
            {
                return(false);
            }

            AttributeNode src = element.GetAttribute("src");

            return(src != null && src.Value.Trim().Length > 0);
        }
예제 #25
0
        private static bool IsRequireValid(HtmlAttribute attribute, ElementNode element, out string error)
        {
            error = string.Empty;

            if (!string.IsNullOrEmpty(attribute.Require) && element.GetAttribute(attribute.Require) == null)
            {
                error = $"When using \"{attribute.Name}\" you must also specify \"{attribute.Require}\".";
                return(false);
            }

            return(true);
        }
예제 #26
0
        public static bool HasClass(this ElementNode element, string className)
        {
            AttributeNode attr = element.GetAttribute("class", true);

            if (attr == null)
            {
                return(false);
            }

            string[] names = attr.Value.Split(' ');
            return(names.Contains(className));
        }
        public bool Visit(ElementNode element, object parameter)
        {
            if (_inputTypes.Contains(element.Name.ToLowerInvariant()))
            {
                var list = (List<string>)parameter;
                var id = element.GetAttribute("id");

                if (id != null && !list.Contains(id.Value))
                    list.Add(id.Value);
            }

            return true;
        }
        public bool Visit(ElementNode element, object parameter)
        {
            if (element.Name == "label")
            {
                var list = (HashSet<string>)parameter;
                var forAttr = element.GetAttribute("for");

                if (forAttr != null)
                    list.Add(forAttr.Value);
            }

            return true;
        }
예제 #29
0
        public override void Reflow(DrawContext dc, BoundingContext bounds, bool incremental)
        {
            ReflowStart(dc);

            if (incremental)
            {
                bounds = bounds.Narrow(style.Left, style.Right);
            }

            MarkupItem tag = new StartTag(CurrentLine, ElementNode);

            ReflowMarkup(tag, dc, style, bounds);

            ImageStyle s = style as ImageStyle;

            if (s == null)
            {
                throw new InvalidOperationException("Expected style for image to be Custom");
            }

            string imgPath = ElementNode.GetAttribute(s.SourceAttribute);

            if (imgPath == null)
            {
                throw new InvalidOperationException("SourceAttribute for image is missing");
            }

            // think about relative to doc
            Bitmap bm;

            Uri docUri = new Uri(elementNode.BaseURI);
            Uri uri    = new Uri(docUri, imgPath);

            if (uri.IsFile && File.Exists(uri.AbsolutePath))
            {
                bm = new Bitmap(uri.AbsolutePath);
            }
            else
            {
                bm = ErrorBitmap;
            }

            ImageLineItem ili = new ImageLineItem(CurrentLine, ElementNode, bm);

            ReflowMarkup(ili, dc, style, bounds);

            tag = new EndTag(CurrentLine, ElementNode);
            ReflowMarkup(tag, dc, style, bounds);

            ReflowEnd(dc);
        }
예제 #30
0
        public bool HasSuggestedActions(ITextView textView, ITextBuffer textBuffer, int caretPosition, ElementNode element, AttributeNode attribute, HtmlPositionType positionType)
        {
            if (!element.IsElement("img"))
            {
                return(false);
            }

            if (element.Parent.IsElement("picture"))
            {
                return(false);
            }

            var src = element.GetAttribute("src");

            if (string.IsNullOrEmpty(src?.Value))
            {
                src = element.GetAttribute("data-src");
            }

            var srcValue = src?.Value?.ToUpperInvariant() ?? string.Empty;

            return(!string.IsNullOrEmpty(srcValue) && !srcValue.Contains(".SVG"));
        }
        private static bool IsEnabled(ElementNode element)
        {
            if (element.Name != "img")
                return false;

            AttributeNode src = element.GetAttribute("src", true);

            if (src != null && src.Value.StartsWith("data:image/", StringComparison.Ordinal))
            {
                return true;
            }

            return false;
        }
        public IEnumerable <ISuggestedAction> GetSuggestedActions(ITextView textView, ITextBuffer textBuffer, int caretPosition, ElementNode element, AttributeNode attribute, HtmlPositionType positionType)
        {
            AttributeNode src = element.GetAttribute("src");

            if (src.Value.StartsWith("data:image/", StringComparison.Ordinal))
            {
                yield return(new HtmlBase64DecodeLightBulbAction(textView, textBuffer, element, src));
            }

            if (!src.Value.StartsWith("http:") && !src.Value.StartsWith("https:") && !src.Value.StartsWith("//"))
            {
                yield return(new HtmlOptimizeImageLightBulbAction(textView, textBuffer, element, src));
            }
        }
        public override IList <IHtmlValidationError> ValidateElement(ElementNode element)
        {
            var results = new ValidationErrorCollection();

            if (!WESettings.Instance.Html.EnableBootstrapValidation)
            {
                return(results);
            }

            var elementClasses = element.GetAttribute("class");

            if (elementClasses == null || !elementClasses.Value.Split(' ').Any(x => x.StartsWith("col-", StringComparison.CurrentCulture)))
            {
                return(results);
            }

            // Bootstrap grid system require a parent <div class='row ... to work
            if (IsParentDivElementMissingRowClass(element))
            {
                int    index        = element.Attributes.IndexOf(elementClasses);
                string columnsClass = elementClasses.Value.Split(' ').Where(x => x.StartsWith("col-", StringComparison.CurrentCulture)).First();
                string error        = string.Format(CultureInfo.CurrentCulture, _errorRowMissing, columnsClass, "");

                results.AddAttributeError(element, error, HtmlValidationErrorLocation.AttributeValue, index);
            }

            // Sum of columns size of a row must egal 12.
            var classList = elementClasses.Value.Split(' ')
                            .Select(x => x.Trim())
                            .Where(x => x.StartsWith("col-", StringComparison.CurrentCulture));

            foreach (var c in classList)
            {
                // Find the type (size) of column
                var columnSize           = c.Replace("col-", string.Empty).Substring(0, 2);
                var sumColumnsCurrentRow = GetSumOfColumns(element, columnSize);

                if (sumColumnsCurrentRow > 12)
                {
                    int    index      = element.Attributes.IndexOf(elementClasses);
                    var    columnType = string.Format(CultureInfo.CurrentCulture, "col-{0}-*", columnSize);
                    string error      = string.Format(CultureInfo.CurrentCulture, _errorInvalidSum, columnType);

                    results.AddAttributeError(element, error, HtmlValidationErrorLocation.AttributeValue, index);
                }
            }

            return(results);
        }
예제 #34
0
        public bool Visit(ElementNode element, object parameter)
        {
            if (element.Name == "label")
            {
                var list    = (HashSet <string>)parameter;
                var forAttr = element.GetAttribute("for");

                if (forAttr != null)
                {
                    list.Add(forAttr.Value);
                }
            }

            return(true);
        }
예제 #35
0
        public override IList <IHtmlValidationError> ValidateElement(ElementNode element)
        {
            var results = new ValidationErrorCollection();

            if (element.Name != "link" || !element.HasAttribute("rel") || !element.HasAttribute("type"))
            {
                return(results);
            }

            AttributeNode rel = element.GetAttribute("rel");

            if (rel.Value.Equals("logo", StringComparison.Ordinal))
            {
                AttributeNode type = element.GetAttribute("type");

                if (!type.Value.Equals("image/svg", StringComparison.OrdinalIgnoreCase))
                {
                    int index = element.Attributes.IndexOf(type);
                    results.AddAttributeError(element, "The type attribute value must be \"image/svg\" for rel=\"logo\" links.", HtmlValidationErrorLocation.AttributeValue, index);
                }
            }

            return(results);
        }
예제 #36
0
        public bool Visit(ElementNode element, object parameter)
        {
            if (_inputTypes.Contains(element.Name.ToLowerInvariant()))
            {
                var list = (HashSet <string>)parameter;
                var id   = element.GetAttribute("id");

                if (id != null)
                {
                    list.Add(id.Value);
                }
            }

            return(true);
        }
        private static bool IsEnabled(ElementNode element)
        {
            if (element.Name != "img")
            {
                return(false);
            }

            AttributeNode src = element.GetAttribute("src", true);

            if (src != null && src.Value.StartsWith("data:image/", StringComparison.Ordinal))
            {
                return(true);
            }

            return(false);
        }
 public override void DoReplace(ElementNode node, IList<Node> body)
 {
     body.Clear();
     Node newBody = node.GetAttribute("for").Value.GetPropertyValueNode();
     body.Add(newBody);
 }
 public override void DoReplace(ElementNode node, IList<Node> body)
 {
     AttributeNode forAttribute = node.GetAttribute(ReplacementSpecification.OriginalAttributeName);
     AddAttribute(node, "name", new ExpressionNode(forAttribute.Value.GetPropertyNameSnippet()));
 }