/// <summary> /// Sets a new default stylesheet defined by the provided string. /// </summary> /// <param name="sourceCode">The source for a new base stylesheet.</param> /// <returns>The CSSOM of the parsed source.</returns> public ICssStyleSheet SetDefault(String sourceCode) { var parser = new CssParser(_options); var source = new TextSource(sourceCode); var sheet = new CssStyleSheet(parser, default(String), default(ICssStyleSheet)); _default = Parse(parser, sheet, source).Result; return _default; }
internal async Task LoadStylesheetFrom(Document document) { if (document != null) { var loader = document.Loader; var baseUrl = Url.Create(Owner.Href ?? document.BaseUri); var url = new Url(baseUrl, _href); if (!IsRecursion(url) && loader != null) { var element = Owner.OwnerNode; var request = element.CreateRequestFor(url); var download = loader.DownloadAsync(request); using (var response = await download.Task.ConfigureAwait(false)) { var sheet = new CssStyleSheet(this, response.Address.Href); var source = new TextSource(response.Content); _styleSheet = await Parser.ParseStylesheetAsync(sheet, source).ConfigureAwait(false); } } } }
public void CssStyleSheetInsertShouldSetParentStyleSheetCorrectly() { var parser = new CssParser(); var s = new CssStyleSheet(parser); s.Insert("a {color: blue}", 0); Assert.AreEqual(s, s.Rules[0].Owner); }
public void CssStyleSheetInsertAndDeleteShouldWork() { var parser = new CssParser(); var s = new CssStyleSheet(parser); Assert.AreEqual(0, s.Rules.Length); s.Insert("a {color: blue}", 0); Assert.AreEqual(1, s.Rules.Length); s.Insert("a *:first-child, a img {border: none}", 1); Assert.AreEqual(2, s.Rules.Length); s.RemoveAt(1); Assert.AreEqual(1, s.Rules.Length); s.RemoveAt(0); Assert.AreEqual(0, s.Rules.Length); }
/// <summary> /// Creates a style sheet for the given source. /// </summary> /// <param name="sourceCode"> /// The source code describing the style sheet. /// </param> /// <param name="options"> /// The options with the parameters for evaluating the style. /// </param> /// <returns>The created style sheet.</returns> public IStyleSheet ParseStylesheet(String sourceCode, StyleOptions options) { var parser = new CssParser(_options, options.Configuration); var sheet = new CssStyleSheet(parser, default(String), options.Element) { IsDisabled = options.IsDisabled }; var source = new TextSource(sourceCode); return Parse(parser, sheet, source).Result; }
async Task<CssStyleSheet> Parse(CssParser parser, CssStyleSheet sheet, TextSource source) { var evt = new CssParseStartEvent(sheet); var events = parser.Config.Events; if (events != null) events.Publish(evt); await parser.ParseStylesheetAsync(sheet, source).ConfigureAwait(false); evt.FireEnd(); return sheet; }
/// <summary> /// Creates a style sheet for the given response from a request. /// </summary> /// <param name="response"> /// The response with the stream representing the source of the /// stylesheet. /// </param> /// <param name="options"> /// The options with the parameters for evaluating the style. /// </param> /// <returns>The created style sheet.</returns> public IStyleSheet ParseStylesheet(IResponse response, StyleOptions options) { var parser = new CssParser(_options, options.Configuration); var sheet = new CssStyleSheet(parser, response.Address.Href, options.Element) { IsDisabled = options.IsDisabled }; var source = new TextSource(response.Content); return Parse(parser, sheet, source).Result; }
/// <summary> /// Creates as many rules as possible. /// </summary> /// <returns>The found rules.</returns> public TextPosition CreateRules(CssStyleSheet sheet) { var token = NextToken(); _nodes.Push(sheet); CollectTrivia(ref token); while (token.Type != CssTokenType.EndOfFile) { var rule = CreateRule(token); token = NextToken(); CollectTrivia(ref token); sheet.Rules.Add(rule); } _nodes.Pop(); return token.Position; }
/// <summary> /// Creates a style sheet for the given response asynchronously. /// </summary> /// <param name="response"> /// The response with the stream representing the source of the /// stylesheet. /// </param> /// <param name="options"> /// The options with the parameters for evaluating the style. /// </param> /// <param name="cancel">The cancellation token.</param> /// <returns>The task resulting in the style sheet.</returns> public async Task<IStyleSheet> ParseStylesheetAsync(IResponse response, StyleOptions options, CancellationToken cancel) { var context = options.Context; var configuration = context.Configuration; var parser = new CssParser(_options, configuration); var url = response.Address?.Href; var sheet = new CssStyleSheet(parser, url, options.Element) { IsDisabled = options.IsDisabled }; var source = new TextSource(response.Content); var tokenizer = new CssTokenizer(source); tokenizer.Error += (_, ev) => context.Fire(ev); var builder = new CssBuilder(tokenizer, parser); context.Fire(new CssParseEvent(sheet, completed: false)); await parser.ParseStylesheetAsync(sheet, source).ConfigureAwait(false); context.Fire(new CssParseEvent(sheet, completed: true)); return sheet; }
/// <summary> /// Takes a text source and populate the provided CSS sheet. /// </summary> internal ICssStyleSheet ParseStylesheet(CssStyleSheet sheet, TextSource source) { var tokenizer = CreateTokenizer(source, _config); var token = tokenizer.Get(); do { var rule = tokenizer.CreateRule(token, this); sheet.AddRule(rule); token = tokenizer.Get(); } while (token.Type != CssTokenType.Eof); return sheet; }
/// <summary> /// Takes a text source and transforms it into a CSS sheet. /// </summary> internal ICssStyleSheet ParseStylesheet(TextSource source) { var sheet = new CssStyleSheet(this); return ParseStylesheet(sheet, source); }
/// <summary> /// Creates a style sheet for the given source. /// </summary> /// <param name="sourceCode"> /// The source code describing the style sheet. /// </param> /// <param name="options"> /// The options with the parameters for evaluating the style. /// </param> /// <returns>The created style sheet.</returns> public IStyleSheet ParseStylesheet(String sourceCode, StyleOptions options) { var parser = new CssParser(_options, options.Configuration); var sheet = new CssStyleSheet(parser) { OwnerNode = options.Element, IsDisabled = options.IsDisabled, Title = options.Title }; var source = new TextSource(sourceCode); return Parse(parser, sheet, source); }
CssStyleSheet Parse(CssParser parser, CssStyleSheet sheet, TextSource source) { using (var evt = new CssParseStartEvent(sheet)) { var events = parser.Config.Events; if (events != null) events.Publish(evt); parser.ParseStylesheet(sheet, source); } return sheet; }
/// <summary> /// Creates a style sheet for the given response from a request. /// </summary> /// <param name="response"> /// The response with the stream representing the source of the /// stylesheet. /// </param> /// <param name="options"> /// The options with the parameters for evaluating the style. /// </param> /// <returns>The created style sheet.</returns> public IStyleSheet ParseStylesheet(IResponse response, StyleOptions options) { var parser = new CssParser(_options, options.Configuration); var sheet = new CssStyleSheet(parser) { Href = response.Address.Href, OwnerNode = options.Element, IsDisabled = options.IsDisabled, Title = options.Title }; var source = new TextSource(response.Content); return Parse(parser, sheet, source); }
protected override void ReplaceWith(ICssRule rule) { var newRule = rule as CssImportRule; _href = newRule._href; _styleSheet = null; //TODO Load New StyleSheet base.ReplaceWith(rule); }